Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get contract name in ToJson #2039

Closed
wants to merge 14 commits into from
25 changes: 25 additions & 0 deletions src/neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
Expand Down Expand Up @@ -71,6 +72,30 @@ public JObject ToJson()
{
JObject json = new JObject();
json["id"] = Id;

if (Id < 0)
{
var native = NativeContract.GetContract(Id);
if (native != null)
{
json["name"] = native.Name;
json["isnative"] = true;
}
else
{
json["isnative"] = false;
}
}
else
{
if (Manifest.Name != null)
{
json["name"] = Manifest.Name;
}

json["isnative"] = false;
}

json["hash"] = ScriptHash.ToString();
json["script"] = Convert.ToBase64String(Script);
json["manifest"] = Manifest.ToJson();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is included in the manifest, and I think we don't need to add it to the top level of the json.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is included in the manifest, and I think we don't need to add it to the top level of the json.

Agree. I'll close this.

Expand Down
8 changes: 8 additions & 0 deletions src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Neo.SmartContract.Native
public abstract class NativeContract
{
private static readonly List<NativeContract> contractsList = new List<NativeContract>();
private static readonly Dictionary<int, NativeContract> contractsIdDictionary = new Dictionary<int, NativeContract>();
private static readonly Dictionary<string, NativeContract> contractsNameDictionary = new Dictionary<string, NativeContract>();
private static readonly Dictionary<UInt160, NativeContract> contractsHashDictionary = new Dictionary<UInt160, NativeContract>();
private readonly Dictionary<string, ContractMethodMetadata> methods = new Dictionary<string, ContractMethodMetadata>();
Expand Down Expand Up @@ -77,6 +78,7 @@ protected NativeContract()
Extra = null
};
contractsList.Add(this);
contractsIdDictionary.Add(Id, this);
contractsNameDictionary.Add(Name, this);
contractsHashDictionary.Add(Hash, this);
}
Expand Down Expand Up @@ -104,6 +106,12 @@ public static NativeContract GetContract(string name)
return contract;
}

public static NativeContract GetContract(int id)
{
contractsIdDictionary.TryGetValue(id, out var contract);
return contract;
}

internal void Invoke(ApplicationEngine engine)
{
if (!engine.CurrentScriptHash.Equals(Hash))
Expand Down
2 changes: 1 addition & 1 deletion src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00251" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00252" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions tests/neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,22 @@ public void TestGetSize()
public void TestToJson()
{
JObject json = contract.ToJson();
json["name"].AsString().Should().Be("testManifest");
json["hash"].AsString().Should().Be("0x820944cfdc70976602d71b0091445eedbc661bc5");
json["script"].AsString().Should().Be("AQ==");
json["manifest"].AsString().Should().Be(manifest.ToJson().AsString());
json["isnative"].AsString().Should().Be("false");
}

[TestMethod]
public void TestToJsonNative()
{
ContractState neocontract = contract;
neocontract.Id = -1;
JObject json = neocontract.ToJson();
json["id"].AsString().Should().Be("-1");
json["name"].AsString().Should().Be("NEO");
json["isnative"].AsString().Should().Be("true");
}
}
}