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

Improve SYSCALLs: Neo.Crypto.* #1190

Merged
merged 7 commits into from
Nov 13, 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
6 changes: 3 additions & 3 deletions neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public void TestContainsTransaction()
[TestMethod]
public void TestGetCurrentBlockHash()
{
Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0x0d492ce0f38090a65b2b01af50f7a6d685b6b76fbc41672762e96b05d15d742c"));
}

[TestMethod]
public void TestGetCurrentHeaderHash()
{
Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0x0d492ce0f38090a65b2b01af50f7a6d685b6b76fbc41672762e96b05d15d742c"));
}

[TestMethod]
Expand All @@ -88,7 +88,7 @@ public void TestGetBlock()
[TestMethod]
public void TestGetBlockHash()
{
Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0x0d492ce0f38090a65b2b01af50f7a6d685b6b76fbc41672762e96b05d15d742c"));
Blockchain.Singleton.GetBlockHash(10).Should().BeNull();
}

Expand Down
31 changes: 15 additions & 16 deletions neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ public void FeeIsMultiSigContract()
}

var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
Assert.AreEqual(verificationGas, 2000540);
Assert.AreEqual(sizeGas, 358000);
Assert.AreEqual(verificationGas + sizeGas, 2358540);
Assert.AreEqual(tx.NetworkFee, 2358540);
Assert.AreEqual(verificationGas, 2000570);
Assert.AreEqual(sizeGas, 359000);
Assert.AreEqual(tx.NetworkFee, 2359570);
}
}

Expand Down Expand Up @@ -214,7 +213,7 @@ public void FeeIsSignatureContractDetailed()
Assert.IsNull(tx.Witnesses);

// check pre-computed network fee (already guessing signature sizes)
tx.NetworkFee.Should().Be(1257240);
tx.NetworkFee.Should().Be(1258270);

// ----
// Sign
Expand Down Expand Up @@ -251,12 +250,12 @@ public void FeeIsSignatureContractDetailed()
verificationGas += engine.GasConsumed;
}
}
Assert.AreEqual(verificationGas, 1000240);
Assert.AreEqual(verificationGas, 1000270);

// ------------------
// check tx_size cost
// ------------------
Assert.AreEqual(tx.Size, 257);
Assert.AreEqual(tx.Size, 258);

// will verify tx size, step by step

Expand All @@ -272,16 +271,16 @@ public void FeeIsSignatureContractDetailed()
// Part III
Assert.AreEqual(tx.Script.GetVarSize(), 82);
// Part IV
Assert.AreEqual(tx.Witnesses.GetVarSize(), 107);
Assert.AreEqual(tx.Witnesses.GetVarSize(), 108);
// I + II + III + IV
Assert.AreEqual(tx.Size, 45 + 23 + 82 + 107);
Assert.AreEqual(tx.Size, 45 + 23 + 82 + 108);

Assert.AreEqual(NativeContract.Policy.GetFeePerByte(snapshot), 1000);
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
Assert.AreEqual(sizeGas, 257000);
Assert.AreEqual(sizeGas, 258000);

// final check on sum: verification_cost + tx_size
Assert.AreEqual(verificationGas + sizeGas, 1257240);
Assert.AreEqual(verificationGas + sizeGas, 1258270);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -372,7 +371,7 @@ public void FeeIsSignatureContract_TestScope_Global()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(verificationGas + sizeGas, 1257240);
Assert.AreEqual(verificationGas + sizeGas, 1258270);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -464,7 +463,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(verificationGas + sizeGas, 1278240);
Assert.AreEqual(verificationGas + sizeGas, 1279270);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -559,7 +558,7 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(verificationGas + sizeGas, 1278240);
Assert.AreEqual(verificationGas + sizeGas, 1279270);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -714,7 +713,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(verificationGas + sizeGas, 1298240);
Assert.AreEqual(verificationGas + sizeGas, 1299270);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -1062,7 +1061,7 @@ public void FeeIsSignatureContract_TestScope_Global_Default()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(verificationGas + sizeGas, 1257240);
Assert.AreEqual(verificationGas + sizeGas, 1258270);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public void MaxSize_OK()

// Check max size

witness.Size.Should().Be(1003);
witness.Size.Should().Be(1004);
witness.InvocationScript.GetVarSize().Should().Be(653);
witness.VerificationScript.GetVarSize().Should().Be(350);
witness.VerificationScript.GetVarSize().Should().Be(351);

Assert.IsTrue(witness.Size <= 1024);

Expand Down
42 changes: 24 additions & 18 deletions neo.UnitTests/SmartContract/UT_Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public void TestGetAddress()
KeyPair key = new KeyPair(privateKey);
Contract contract = Contract.CreateSignatureContract(key.PublicKey);
byte[] script = contract.Script;
byte[] expectedArray = new byte[39];
byte[] expectedArray = new byte[40];
expectedArray[0] = 0x21;
Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33);
expectedArray[34] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckSig), 0, expectedArray, 35, 4);
expectedArray[34] = 0x50;
expectedArray[35] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 36, 4);
Assert.AreEqual(expectedArray.ToScriptHash().ToAddress(), contract.Address);
}

Expand All @@ -38,11 +39,12 @@ public void TestGetScriptHash()
KeyPair key = new KeyPair(privateKey);
Contract contract = Contract.CreateSignatureContract(key.PublicKey);
byte[] script = contract.Script;
byte[] expectedArray = new byte[39];
byte[] expectedArray = new byte[40];
expectedArray[0] = 0x21;
Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33);
expectedArray[34] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckSig), 0, expectedArray, 35, 4);
expectedArray[34] = 0x50;
expectedArray[35] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 36, 4);
Assert.AreEqual(expectedArray.ToScriptHash(), contract.ScriptHash);
}

Expand Down Expand Up @@ -73,15 +75,16 @@ public void TestCreateMultiSigContract()
publicKeys[1] = key2.PublicKey;
publicKeys = publicKeys.OrderBy(p => p).ToArray();
Contract contract = Contract.CreateMultiSigContract(2, publicKeys);
byte[] expectedArray = new byte[75];
byte[] expectedArray = new byte[76];
expectedArray[0] = 0x52;
expectedArray[1] = 0x21;
Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 2, 33);
expectedArray[35] = 0x21;
Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 36, 33);
expectedArray[69] = 0x52;
expectedArray[70] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckMultiSig), 0, expectedArray, 71, 4);
expectedArray[70] = 0x50;
expectedArray[71] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaCheckMultiSig), 0, expectedArray, 72, 4);
Assert.AreEqual(Encoding.Default.GetString(expectedArray), Encoding.Default.GetString(contract.Script));
Assert.AreEqual(2, contract.ParameterList.Length);
Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]);
Expand All @@ -106,15 +109,16 @@ public void TestCreateMultiSigRedeemScript()
Action action = () => Contract.CreateMultiSigRedeemScript(0, publicKeys);
action.Should().Throw<ArgumentException>();
byte[] script = Contract.CreateMultiSigRedeemScript(2, publicKeys);
byte[] expectedArray = new byte[75];
byte[] expectedArray = new byte[76];
expectedArray[0] = 0x52;
expectedArray[1] = 0x21;
Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 2, 33);
expectedArray[35] = 0x21;
Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 36, 33);
expectedArray[69] = 0x52;
expectedArray[70] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckMultiSig), 0, expectedArray, 71, 4);
expectedArray[70] = 0x50;
expectedArray[71] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaCheckMultiSig), 0, expectedArray, 72, 4);
Assert.AreEqual(Encoding.Default.GetString(expectedArray), Encoding.Default.GetString(script));
}

Expand All @@ -127,11 +131,12 @@ public void TestCreateSignatureContract()
KeyPair key = new KeyPair(privateKey);
Contract contract = Contract.CreateSignatureContract(key.PublicKey);
byte[] script = contract.Script;
byte[] expectedArray = new byte[39];
byte[] expectedArray = new byte[40];
expectedArray[0] = 0x21;
Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33);
expectedArray[34] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckSig), 0, expectedArray, 35, 4);
expectedArray[34] = 0x50;
expectedArray[35] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 36, 4);
Assert.AreEqual(Encoding.Default.GetString(expectedArray), Encoding.Default.GetString(script));
Assert.AreEqual(1, contract.ParameterList.Length);
Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]);
Expand All @@ -145,11 +150,12 @@ public void TestCreateSignatureRedeemScript()
rng.GetBytes(privateKey);
KeyPair key = new KeyPair(privateKey);
byte[] script = Contract.CreateSignatureRedeemScript(key.PublicKey);
byte[] expectedArray = new byte[39];
byte[] expectedArray = new byte[40];
expectedArray[0] = 0x21;
Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33);
expectedArray[34] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckSig), 0, expectedArray, 35, 4);
expectedArray[34] = 0x50;
expectedArray[35] = 0x68;
Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 36, 4);
Assert.AreEqual(Encoding.Default.GetString(expectedArray), Encoding.Default.GetString(script));
}
}
Expand Down
18 changes: 9 additions & 9 deletions neo.UnitTests/SmartContract/UT_ContractParameterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void ClassSetUp(TestContext context)
public void TestGetComplete()
{
Transaction tx = TestUtils.GetTransaction();
tx.Sender = UInt160.Parse("0xbecaad15c0ea585211faf99738a4354014f177f2");
tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c");
var context = new ContractParametersContext(tx);
context.Completed.Should().BeFalse();
}
Expand All @@ -42,11 +42,11 @@ public void TestGetComplete()
public void TestToString()
{
Transaction tx = TestUtils.GetTransaction();
tx.Sender = UInt160.Parse("0xbecaad15c0ea585211faf99738a4354014f177f2");
tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c");
var context = new ContractParametersContext(tx);
context.Add(contract, 0, new byte[] { 0x01 });
string str = context.ToString();
str.Should().Be("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"hex\":\"AAAAAADyd\\/EUQDWkOJf5\\u002BhFSWOrAFa3KvgAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA==\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFmh0dHaq\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"AQ==\"}]}}}");
str.Should().Be(@"{""type"":""Neo.Network.P2P.Payloads.Transaction"",""hex"":""AAAAAAB8l3ZIRRctgn08hjdDKTkxppEnGgAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA=="",""items"":{""0x1a2791a63139294337863c7d822d17454876977c"":{""script"":""IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFlBoCpBq1A=="",""parameters"":[{""type"":""Signature"",""value"":""AQ==""}]}}}");
}

[TestMethod]
Expand All @@ -60,7 +60,7 @@ public void TestParse()
[TestMethod]
public void TestFromJson()
{
Action action = () => ContractParametersContext.Parse("{\"type\":\"wrongType\",\"hex\":\"0000000000f277f1144035a43897f9fa115258eac015adcabe0000000000000000000000000000000000000000000100\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1668747476aa\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}");
Action action = () => ContractParametersContext.Parse("{\"type\":\"wrongType\",\"hex\":\"00000000007c97764845172d827d3c863743293931a691271a0000000000000000000000000000000000000000000100\",\"items\":{\"0x1a2791a63139294337863c7d822d17454876977c\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1650680a906ad4\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}");
action.Should().Throw<FormatException>();
}

Expand All @@ -71,7 +71,7 @@ public void TestAdd()
var context1 = new ContractParametersContext(tx);
context1.Add(contract, 0, new byte[] { 0x01 }).Should().BeFalse();

tx.Sender = UInt160.Parse("0xbecaad15c0ea585211faf99738a4354014f177f2");
tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c");
var context2 = new ContractParametersContext(tx);
context2.Add(contract, 0, new byte[] { 0x01 }).Should().BeTrue();
//test repeatlly createItem
Expand All @@ -82,7 +82,7 @@ public void TestAdd()
public void TestGetParameter()
{
Transaction tx = TestUtils.GetTransaction();
tx.Sender = UInt160.Parse("0xbecaad15c0ea585211faf99738a4354014f177f2");
tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c");
var context = new ContractParametersContext(tx);
context.GetParameter(tx.Sender, 0).Should().BeNull();

Expand All @@ -95,7 +95,7 @@ public void TestGetParameter()
public void TestGetWitnesses()
{
Transaction tx = TestUtils.GetTransaction();
tx.Sender = UInt160.Parse("0xbecaad15c0ea585211faf99738a4354014f177f2");
tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c");
var context = new ContractParametersContext(tx);
context.Add(contract, 0, new byte[] { 0x01 });
Witness[] witnesses = context.GetWitnesses();
Expand All @@ -108,7 +108,7 @@ public void TestGetWitnesses()
public void TestAddSignature()
{
Transaction tx = TestUtils.GetTransaction();
var singleSender = UInt160.Parse("0xbecaad15c0ea585211faf99738a4354014f177f2");
var singleSender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c");
tx.Sender = singleSender;

//singleSign
Expand Down Expand Up @@ -138,7 +138,7 @@ public void TestAddSignature()
key.PublicKey,
key2.PublicKey
});
var multiSender = UInt160.Parse("0xa4712ed1a8d813561b28ec828930d85e6e08ec7a");
var multiSender = UInt160.Parse("0xfc8b59f1a337dcc17b1a201d327a2081d41fac8d");
tx.Sender = multiSender;
context = new ContractParametersContext(tx);
context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue();
Expand Down
Loading