Skip to content

Commit

Permalink
Fix native contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jul 9, 2020
1 parent bbed8cf commit 1abdbdf
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public UInt160 ScriptHash
}
}

int ISerializable.Size => sizeof(int) + Script.GetVarSize() + Manifest.ToJson().ToString().GetVarSize();
int ISerializable.Size => sizeof(int) + Script.GetVarSize() + Manifest.Size;

ContractState ICloneable<ContractState>.Clone()
{
Expand Down
11 changes: 9 additions & 2 deletions src/neo/SmartContract/Manifest/ContractManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ public class ContractManifest : ISerializable
/// <summary>
/// Serialized size
/// </summary>
public int Size => ToJson().ToString().GetVarSize();
public int Size
{
get
{
int size = Utility.StrictUTF8.GetByteCount(ToString());
return IO.Helper.GetVarSize(size) + size;
}
}

/// <summary>
/// Contract hash
Expand Down Expand Up @@ -150,7 +157,7 @@ public ContractManifest Clone()

public void Serialize(BinaryWriter writer)
{
writer.WriteVarString(ToJson().ToString());
writer.WriteVarString(ToString());
}

public void Deserialize(BinaryReader reader)
Expand Down
13 changes: 6 additions & 7 deletions src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public abstract class NativeContract
public UInt160 Hash { get; }
public abstract int Id { get; }
public ContractManifest Manifest { get; }
[ContractMethod(0, CallFlags.None)]
public virtual string[] SupportedStandards { get; } = { "NEP-10" };

protected NativeContract()
{
Expand Down Expand Up @@ -60,18 +58,19 @@ protected NativeContract()
}
this.Manifest = new ContractManifest
{
Permissions = new[] { ContractPermission.DefaultPermission },
Groups = System.Array.Empty<ContractGroup>(),
Features = ContractFeatures.NoProperty,
SupportedStandards = new string[0],
Abi = new ContractAbi()
{
Hash = Hash,
Events = System.Array.Empty<ContractEventDescriptor>(),
Methods = descriptors.ToArray()
},
Features = ContractFeatures.NoProperty,
Groups = System.Array.Empty<ContractGroup>(),
SafeMethods = WildcardContainer<string>.Create(safeMethods.ToArray()),
Permissions = new[] { ContractPermission.DefaultPermission },
Trusts = WildcardContainer<UInt160>.Create(),
Extra = null,
SafeMethods = WildcardContainer<string>.Create(safeMethods.ToArray()),
Extra = null
};
contractsList.Add(this);
contractsNameDictionary.Add(Name, this);
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/Tokens/Nep5Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Neo.SmartContract.Native.Tokens
public abstract class Nep5Token<TState> : NativeContract
where TState : AccountState, new()
{
public override string[] SupportedStandards { get; } = { "NEP-5", "NEP-10" };
[ContractMethod(0, CallFlags.None)]
public abstract string Symbol { get; }
[ContractMethod(0, CallFlags.None)]
Expand All @@ -28,6 +27,7 @@ protected Nep5Token()
this.Factor = BigInteger.Pow(10, Decimals);

Manifest.Features = ContractFeatures.HasStorage;
Manifest.SupportedStandards = new[] { "NEP-5" };

var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
{
Expand Down
23 changes: 0 additions & 23 deletions tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Neo.VM;
using System;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Neo.UnitTests.Extensions
Expand Down Expand Up @@ -68,28 +67,6 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by
return result.GetBoolean();
}

public static string[] SupportedStandards(this NativeContract contract)
{
var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true);

engine.LoadScript(contract.Script);

var script = new ScriptBuilder();
script.EmitPush(0);
script.Emit(OpCode.PACK);
script.EmitPush("supportedStandards");
engine.LoadScript(script.ToArray());

engine.Execute().Should().Be(VMState.HALT);

var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.Array));

return (result as VM.Types.Array).ToArray()
.Select(u => u.GetString())
.ToArray();
}

public static BigInteger TotalSupply(this NativeContract contract, StoreView snapshot)
{
var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void TestDeserialize()
public void TestGetSize()
{
ISerializable newContract = contract;
newContract.Size.Should().Be(239);
newContract.Size.Should().Be(265);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class UT_ContractManifest
[TestMethod]
public void ParseFromJson_Default()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);

Assert.AreEqual(manifest.ToString(), json);
Expand All @@ -22,7 +22,7 @@ public void ParseFromJson_Default()
[TestMethod]
public void ParseFromJson_Features()
{
var json = @"{""groups"":[],""features"":{""storage"":true,""payable"":true},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""features"":{""storage"":true,""payable"":true},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToJson().ToString(), json);

Expand All @@ -34,7 +34,7 @@ public void ParseFromJson_Features()
[TestMethod]
public void ParseFromJson_Permissions()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -53,7 +53,7 @@ public void ParseFromJson_Permissions()
[TestMethod]
public void ParseFromJson_SafeMethods()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[""balanceOf""],""extra"":null}";
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[""balanceOf""],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -65,7 +65,7 @@ public void ParseFromJson_SafeMethods()
[TestMethod]
public void ParseFromJson_Trust()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -77,7 +77,7 @@ public void ParseFromJson_Trust()
[TestMethod]
public void ParseFromJson_Groups()
{
var json = @"{""groups"":[{""pubkey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ==""}],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[{""pubkey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ==""}],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -89,7 +89,7 @@ public void ParseFromJson_Groups()
[TestMethod]
public void ParseFromJson_Extra()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":{""key"":""value""}}";
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":{""key"":""value""}}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(json, json);
Assert.AreEqual("value", manifest.Extra["key"].AsString(), false);
Expand Down Expand Up @@ -122,7 +122,7 @@ public void TestGetHash()
public void TestGetSize()
{
var temp = TestUtils.CreateDefaultManifest(UInt160.Zero);
Assert.AreEqual(233, temp.Size);
Assert.AreEqual(259, temp.Size);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public void TestSetup()
[TestMethod]
public void Check_Decimals() => NativeContract.GAS.Decimals().Should().Be(8);

[TestMethod]
public void Check_SupportedStandards() => NativeContract.GAS.SupportedStandards().Should().BeEquivalentTo(new string[] { "NEP-5", "NEP-10" });

[TestMethod]
public void Check_BalanceOfTransferAndBurn()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public void TestSetup()
[TestMethod]
public void Check_Decimals() => NativeContract.NEO.Decimals().Should().Be(0);

[TestMethod]
public void Check_SupportedStandards() => NativeContract.NEO.SupportedStandards().Should().BeEquivalentTo(new string[] { "NEP-5", "NEP-10" });

[TestMethod]
public void Check_Vote()
{
Expand Down
3 changes: 0 additions & 3 deletions tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public void TestSetup()
TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void Check_SupportedStandards() => NativeContract.Policy.SupportedStandards().Should().BeEquivalentTo(new string[] { "NEP-10" });

[TestMethod]
public void Check_Initialize()
{
Expand Down
11 changes: 6 additions & 5 deletions tests/neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ public static ContractManifest CreateDefaultManifest(UInt160 hash)
{
return new ContractManifest()
{
Permissions = new[] { ContractPermission.DefaultPermission },
Groups = new ContractGroup[0],
Features = ContractFeatures.NoProperty,
SupportedStandards = Array.Empty<string>(),
Abi = new ContractAbi()
{
Hash = hash,
Events = new ContractEventDescriptor[0],
Methods = new ContractMethodDescriptor[0]
},
Features = ContractFeatures.NoProperty,
Groups = new ContractGroup[0],
SafeMethods = WildcardContainer<string>.Create(),
Permissions = new[] { ContractPermission.DefaultPermission },
Trusts = WildcardContainer<UInt160>.Create(),
Extra = null,
SafeMethods = WildcardContainer<string>.Create(),
Extra = null
};
}

Expand Down

0 comments on commit 1abdbdf

Please sign in to comment.