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

update #1

Merged
merged 8 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ public static class Nep5NativeContractExtensions
{
internal class ManualWitness : IVerifiable
{
private readonly UInt160[] _hashForVerify;
private readonly UInt160 _hashForVerify;

public Witness[] Witnesses => throw new NotImplementedException();
public Witness Witness
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

public int Size => 0;

public ManualWitness(UInt160[] hashForVerify)
public ManualWitness(UInt160 hashForVerify)
{
_hashForVerify = hashForVerify ?? new UInt160[0];
_hashForVerify = hashForVerify;
}

public void Deserialize(BinaryReader reader) { }

public void DeserializeUnsigned(BinaryReader reader) { }

public UInt160[] GetScriptHashesForVerifying(Persistence.Snapshot snapshot)
public UInt160 GetScriptHashForVerification(Persistence.Snapshot snapshot)
{
return _hashForVerify;
}
Expand All @@ -43,7 +47,7 @@ public void SerializeUnsigned(BinaryWriter writer) { }
public static bool Transfer(this NativeContract contract, Persistence.Snapshot snapshot, byte[] from, byte[] to, BigInteger amount, bool signFrom)
{
var engine = new ApplicationEngine(TriggerType.Application,
new ManualWitness(signFrom ? new[] { new UInt160(from) } : null), snapshot, 0, true);
new ManualWitness(signFrom ? new UInt160(from) : null), snapshot, 0, true);

engine.LoadScript(contract.Script);

Expand All @@ -69,7 +73,7 @@ public static bool Transfer(this NativeContract contract, Persistence.Snapshot s

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

engine.LoadScript(contract.Script);

Expand Down Expand Up @@ -132,7 +136,7 @@ public static BigInteger BalanceOf(this NativeContract contract, Persistence.Sna

public static BigInteger Decimals(this NativeContract contract)
{
var engine = new ApplicationEngine(TriggerType.Application, null, null, 0);
var engine = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true);

engine.LoadScript(contract.Script);

Expand All @@ -152,7 +156,7 @@ public static BigInteger Decimals(this NativeContract contract)

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

engine.LoadScript(contract.Script);

Expand All @@ -172,7 +176,7 @@ public static string Symbol(this NativeContract contract)

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

engine.LoadScript(contract.Script);

Expand Down
12 changes: 10 additions & 2 deletions neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public static Transaction GetTransaction()
Script = new byte[1],
Sender = UInt160.Zero,
Attributes = new TransactionAttribute[0],
Witnesses = new Witness[0]
Witness = new Witness
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
}
};
}

Expand Down Expand Up @@ -82,7 +86,11 @@ public static Transaction CreateRandomHashTransaction()
Script = randomBytes,
Sender = UInt160.Zero,
Attributes = new TransactionAttribute[0],
Witnesses = new Witness[0]
Witness = new Witness
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
}
};
}

Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/TestVerifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TestVerifiable : IVerifiable
{
private string testStr = "testStr";

public Witness[] Witnesses { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public Witness Witness { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public int Size => throw new NotImplementedException();

Expand All @@ -23,7 +23,7 @@ public void DeserializeUnsigned(BinaryReader reader)
throw new NotImplementedException();
}

public UInt160[] GetScriptHashesForVerifying(Snapshot snapshot)
public UInt160 GetScriptHashForVerification(Snapshot snapshot)
{
throw new NotImplementedException();
}
Expand Down
31 changes: 11 additions & 20 deletions neo.UnitTests/UT_Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ public void Transactions_Get()
uut.Transactions.Should().BeNull();
}

[TestMethod]
public void Transactions_Set()
{
Transaction[] val = new Transaction[10];
uut.Transactions = val;
uut.Transactions.Length.Should().Be(10);
}



[TestMethod]
public void Header_Get()
{
Expand Down Expand Up @@ -63,9 +53,9 @@ public void Size_Get()
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 1 + 3
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 3
// block 9 + 1
uut.Size.Should().Be(110);
uut.Size.Should().Be(109);
}

[TestMethod]
Expand All @@ -79,7 +69,8 @@ public void Size_Get_1_Transaction()
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);

uut.Transactions = new Transaction[1] {
uut.Transactions = new[]
{
TestUtils.GetTransaction()
};

Expand All @@ -97,13 +88,14 @@ public void Size_Get_3_Transaction()
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);

uut.Transactions = new Transaction[3] {
uut.Transactions = new[]
{
TestUtils.GetTransaction(),
TestUtils.GetTransaction(),
TestUtils.GetTransaction()
};

uut.Size.Should().Be(257);
uut.Size.Should().Be(259);
}

[TestMethod]
Expand All @@ -127,7 +119,7 @@ public void Serialize()
}
}

byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

data.Length.Should().Be(requiredData.Length);
for (int i = 0; i < data.Length; i++)
Expand All @@ -149,7 +141,7 @@ public void Deserialize()

uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid

byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
{
Expand Down Expand Up @@ -266,18 +258,17 @@ public void ToJson()
jObj["index"].AsNumber().Should().Be(0);
jObj["nextconsensus"].AsString().Should().Be("AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM");

JObject scObj = jObj["script"];
JObject scObj = jObj["witness"];
scObj["invocation"].AsString().Should().Be("");
scObj["verification"].AsString().Should().Be("51");

jObj["tx"].Should().NotBeNull();
JArray txObj = (JArray)jObj["tx"];
txObj[0]["txid"].AsString().Should().Be("0x7647acf1ef8a841b87f2369398a0e9f0ccde0ed9e835d980657103da6da59580");
txObj[0]["size"].AsNumber().Should().Be(49);
txObj[0]["size"].AsNumber().Should().Be(50);
txObj[0]["version"].AsNumber().Should().Be(0);
((JArray)txObj[0]["attributes"]).Count.Should().Be(0);
txObj[0]["net_fee"].AsString().Should().Be("0");
((JArray)txObj[0]["witnesses"]).Count.Should().Be(0);
}
}
}
2 changes: 1 addition & 1 deletion neo.UnitTests/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart()
// Creating proposed block
Header header = new Header();
TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out uint timestampVal, out uint indexVal, out Witness scriptVal);
header.Size.Should().Be(101);
header.Size.Should().Be(100);

Console.WriteLine($"header {header} hash {header.Hash} timstamp {timestampVal}");

Expand Down
88 changes: 88 additions & 0 deletions neo.UnitTests/UT_ContractManifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.SmartContract.Manifest;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ContractManifest
{
[TestMethod]
public void ParseFromJson_Default()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";
var manifest = ContractManifest.Parse(json);

Assert.AreEqual(manifest.ToString(), json);
Assert.AreEqual(manifest.ToString(), ContractManifest.CreateDefault(UInt160.Zero).ToString());
Assert.IsTrue(manifest.IsValid(UInt160.Zero));
}

[TestMethod]
public void ParseFromJson_Features()
{
var json = @"{""groups"":[],""features"":{""storage"":true,""payable"":true},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToJson().ToString(), json);

var check = ContractManifest.CreateDefault(UInt160.Zero);
check.Features = ContractFeatures.HasStorage | ContractFeatures.Payable;
Assert.AreEqual(manifest.ToString(), check.ToString());
}

[TestMethod]
public void ParseFromJson_Permissions()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safeMethods"":[]}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

var check = ContractManifest.CreateDefault(UInt160.Zero);
check.Permissions = new[]
{
new ContractPermission()
{
Contract = ContractPermissionDescriptor.Create(UInt160.Zero),
Methods = WildCardContainer<string>.Create("method1", "method2")
}
};
Assert.AreEqual(manifest.ToString(), check.ToString());
}

[TestMethod]
public void ParseFromJson_SafeMethods()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[""balanceOf""]}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

var check = ContractManifest.CreateDefault(UInt160.Zero);
check.SafeMethods = WildCardContainer<string>.Create("balanceOf");
Assert.AreEqual(manifest.ToString(), check.ToString());
}

[TestMethod]
public void ParseFromJson_Trust()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safeMethods"":[]}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

var check = ContractManifest.CreateDefault(UInt160.Zero);
check.Trusts = WildCardContainer<UInt160>.Create(UInt160.Parse("0x0000000000000000000000000000000000000001"));
Assert.AreEqual(manifest.ToString(), check.ToString());
}

[TestMethod]
public void ParseFromJson_Groups()
{
var json = @"{""groups"":[{""pubKey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""41414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141""}],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

var check = ContractManifest.CreateDefault(UInt160.Zero);
check.Groups = new ContractGroup[] { new ContractGroup() { PubKey = ECPoint.Parse("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", ECCurve.Secp256r1), Signature = "41414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141".HexToBytes() } };
Assert.AreEqual(manifest.ToString(), check.ToString());
}
}
}
Loading