From 830c2a02fdb908485a5785f59e11c24c0f9047e2 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 12 Dec 2019 18:06:52 +0800 Subject: [PATCH 1/3] Neo.VM.3.0.0-CI00190 --- src/neo/Ledger/Blockchain.cs | 6 +- src/neo/Network/P2P/Payloads/Witness.cs | 4 +- .../ApplicationEngine.OpCodePrices.cs | 134 ++++------- src/neo/SmartContract/ApplicationEngine.cs | 7 +- .../ContractParametersContext.cs | 8 +- .../SmartContract/ExecutionContextState.cs | 7 +- src/neo/SmartContract/Helper.cs | 42 ++-- src/neo/SmartContract/InteropService.cs | 2 + src/neo/SmartContract/StackItemSerializer.cs | 2 +- src/neo/VM/Helper.cs | 5 +- src/neo/Wallets/Wallet.cs | 10 +- src/neo/neo.csproj | 2 +- tests/neo.UnitTests/Consensus/UT_Consensus.cs | 6 +- tests/neo.UnitTests/Ledger/UT_Blockchain.cs | 6 +- tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs | 2 +- .../Network/P2P/Payloads/UT_Block.cs | 10 +- .../Network/P2P/Payloads/UT_Header.cs | 4 +- .../Network/P2P/Payloads/UT_Transaction.cs | 50 ++-- .../Network/P2P/Payloads/UT_Witness.cs | 6 +- .../SmartContract/UT_Contract.cs | 109 +++++---- .../UT_ContractParameterContext.cs | 21 +- .../SmartContract/UT_InteropPrices.cs | 10 +- .../SmartContract/UT_InteropService.cs | 6 - .../SmartContract/UT_StackItemSerializer.cs | 11 +- tests/neo.UnitTests/TestUtils.cs | 6 +- tests/neo.UnitTests/VM/UT_Helper.cs | 223 +++++++++--------- .../Wallets/SQLite/UT_VerificationContract.cs | 10 +- tests/neo.UnitTests/Wallets/UT_Wallet.cs | 4 +- 28 files changed, 346 insertions(+), 367 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index d6c2840526..819289b333 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -45,7 +45,7 @@ private class ParallelVerified { public Transaction Transaction; public bool Sho Witness = new Witness { InvocationScript = Array.Empty(), - VerificationScript = new[] { (byte)OpCode.PUSHT } + VerificationScript = new[] { (byte)OpCode.PUSH1 } }, ConsensusData = new ConsensusData { @@ -164,7 +164,7 @@ private static Transaction DeployNativeContracts() { Version = 0, Script = script, - Sender = (new[] { (byte)OpCode.PUSHT }).ToScriptHash(), + Sender = (new[] { (byte)OpCode.PUSH1 }).ToScriptHash(), SystemFee = 0, Attributes = new TransactionAttribute[0], Cosigners = new Cosigner[0], @@ -173,7 +173,7 @@ private static Transaction DeployNativeContracts() new Witness { InvocationScript = Array.Empty(), - VerificationScript = new[] { (byte)OpCode.PUSHT } + VerificationScript = new[] { (byte)OpCode.PUSH1 } } } }; diff --git a/src/neo/Network/P2P/Payloads/Witness.cs b/src/neo/Network/P2P/Payloads/Witness.cs index 1cd5bf7ee8..55b2c7ae4b 100644 --- a/src/neo/Network/P2P/Payloads/Witness.cs +++ b/src/neo/Network/P2P/Payloads/Witness.cs @@ -30,9 +30,9 @@ void ISerializable.Deserialize(BinaryReader reader) { // This is designed to allow a MultiSig 10/10 (around 1003 bytes) ~1024 bytes // Invocation = 10 * 64 + 10 = 650 ~ 664 (exact is 653) - InvocationScript = reader.ReadVarBytes(664); + InvocationScript = reader.ReadVarBytes(663); // Verification = 10 * 33 + 10 = 340 ~ 360 (exact is 351) - VerificationScript = reader.ReadVarBytes(360); + VerificationScript = reader.ReadVarBytes(361); } void ISerializable.Serialize(BinaryWriter writer) diff --git a/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs b/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs index b6b65b37da..a1a2ffd400 100644 --- a/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs +++ b/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs @@ -7,87 +7,19 @@ partial class ApplicationEngine { public static readonly IReadOnlyDictionary OpCodePrices = new Dictionary { - [OpCode.PUSH0] = 30, - [OpCode.PUSHBYTES1] = 120, - [OpCode.PUSHBYTES2] = 120, - [OpCode.PUSHBYTES3] = 120, - [OpCode.PUSHBYTES4] = 120, - [OpCode.PUSHBYTES5] = 120, - [OpCode.PUSHBYTES6] = 120, - [OpCode.PUSHBYTES7] = 120, - [OpCode.PUSHBYTES8] = 120, - [OpCode.PUSHBYTES9] = 120, - [OpCode.PUSHBYTES10] = 120, - [OpCode.PUSHBYTES11] = 120, - [OpCode.PUSHBYTES12] = 120, - [OpCode.PUSHBYTES13] = 120, - [OpCode.PUSHBYTES14] = 120, - [OpCode.PUSHBYTES15] = 120, - [OpCode.PUSHBYTES16] = 120, - [OpCode.PUSHBYTES17] = 120, - [OpCode.PUSHBYTES18] = 120, - [OpCode.PUSHBYTES19] = 120, - [OpCode.PUSHBYTES20] = 120, - [OpCode.PUSHBYTES21] = 120, - [OpCode.PUSHBYTES22] = 120, - [OpCode.PUSHBYTES23] = 120, - [OpCode.PUSHBYTES24] = 120, - [OpCode.PUSHBYTES25] = 120, - [OpCode.PUSHBYTES26] = 120, - [OpCode.PUSHBYTES27] = 120, - [OpCode.PUSHBYTES28] = 120, - [OpCode.PUSHBYTES29] = 120, - [OpCode.PUSHBYTES30] = 120, - [OpCode.PUSHBYTES31] = 120, - [OpCode.PUSHBYTES32] = 120, - [OpCode.PUSHBYTES33] = 120, - [OpCode.PUSHBYTES34] = 120, - [OpCode.PUSHBYTES35] = 120, - [OpCode.PUSHBYTES36] = 120, - [OpCode.PUSHBYTES37] = 120, - [OpCode.PUSHBYTES38] = 120, - [OpCode.PUSHBYTES39] = 120, - [OpCode.PUSHBYTES40] = 120, - [OpCode.PUSHBYTES41] = 120, - [OpCode.PUSHBYTES42] = 120, - [OpCode.PUSHBYTES43] = 120, - [OpCode.PUSHBYTES44] = 120, - [OpCode.PUSHBYTES45] = 120, - [OpCode.PUSHBYTES46] = 120, - [OpCode.PUSHBYTES47] = 120, - [OpCode.PUSHBYTES48] = 120, - [OpCode.PUSHBYTES49] = 120, - [OpCode.PUSHBYTES50] = 120, - [OpCode.PUSHBYTES51] = 120, - [OpCode.PUSHBYTES52] = 120, - [OpCode.PUSHBYTES53] = 120, - [OpCode.PUSHBYTES54] = 120, - [OpCode.PUSHBYTES55] = 120, - [OpCode.PUSHBYTES56] = 120, - [OpCode.PUSHBYTES57] = 120, - [OpCode.PUSHBYTES58] = 120, - [OpCode.PUSHBYTES59] = 120, - [OpCode.PUSHBYTES60] = 120, - [OpCode.PUSHBYTES61] = 120, - [OpCode.PUSHBYTES62] = 120, - [OpCode.PUSHBYTES63] = 120, - [OpCode.PUSHBYTES64] = 120, - [OpCode.PUSHBYTES65] = 120, - [OpCode.PUSHBYTES66] = 120, - [OpCode.PUSHBYTES67] = 120, - [OpCode.PUSHBYTES68] = 120, - [OpCode.PUSHBYTES69] = 120, - [OpCode.PUSHBYTES70] = 120, - [OpCode.PUSHBYTES71] = 120, - [OpCode.PUSHBYTES72] = 120, - [OpCode.PUSHBYTES73] = 120, - [OpCode.PUSHBYTES74] = 120, - [OpCode.PUSHBYTES75] = 120, + [OpCode.PUSHINT8] = 30, + [OpCode.PUSHINT16] = 30, + [OpCode.PUSHINT32] = 30, + [OpCode.PUSHINT64] = 30, + [OpCode.PUSHINT128] = 120, + [OpCode.PUSHINT256] = 120, + [OpCode.PUSHA] = 120, + [OpCode.PUSHNULL] = 30, [OpCode.PUSHDATA1] = 180, [OpCode.PUSHDATA2] = 13000, [OpCode.PUSHDATA4] = 110000, [OpCode.PUSHM1] = 30, - [OpCode.PUSHNULL] = 30, + [OpCode.PUSH0] = 30, [OpCode.PUSH1] = 30, [OpCode.PUSH2] = 30, [OpCode.PUSH3] = 30, @@ -106,29 +38,51 @@ partial class ApplicationEngine [OpCode.PUSH16] = 30, [OpCode.NOP] = 30, [OpCode.JMP] = 70, + [OpCode.JMP_L] = 70, [OpCode.JMPIF] = 70, + [OpCode.JMPIF_L] = 70, [OpCode.JMPIFNOT] = 70, + [OpCode.JMPIFNOT_L] = 70, + [OpCode.JMPEQ] = 70, + [OpCode.JMPEQ_L] = 70, + [OpCode.JMPNE] = 70, + [OpCode.JMPNE_L] = 70, + [OpCode.JMPGT] = 70, + [OpCode.JMPGT_L] = 70, + [OpCode.JMPGE] = 70, + [OpCode.JMPGE_L] = 70, + [OpCode.JMPLT] = 70, + [OpCode.JMPLT_L] = 70, + [OpCode.JMPLE] = 70, + [OpCode.JMPLE_L] = 70, [OpCode.CALL] = 22000, + [OpCode.CALL_L] = 22000, + [OpCode.CALLA] = 22000, + [OpCode.THROW] = 30, + [OpCode.THROWIF] = 30, + [OpCode.THROWIFNOT] = 30, [OpCode.RET] = 40, [OpCode.SYSCALL] = 0, - [OpCode.DUPFROMALTSTACKBOTTOM] = 60, - [OpCode.DUPFROMALTSTACK] = 60, - [OpCode.TOALTSTACK] = 60, - [OpCode.FROMALTSTACK] = 60, - [OpCode.ISNULL] = 60, - [OpCode.XDROP] = 400, - [OpCode.XSWAP] = 60, - [OpCode.XTUCK] = 400, [OpCode.DEPTH] = 60, [OpCode.DROP] = 60, - [OpCode.DUP] = 60, [OpCode.NIP] = 60, + [OpCode.XDROP] = 400, + [OpCode.CLEAR] = 400, + [OpCode.DUP] = 60, [OpCode.OVER] = 60, [OpCode.PICK] = 60, - [OpCode.ROLL] = 400, - [OpCode.ROT] = 60, - [OpCode.SWAP] = 60, [OpCode.TUCK] = 60, + [OpCode.SWAP] = 60, + [OpCode.ROT] = 60, + [OpCode.ROLL] = 400, + [OpCode.REVERSE3] = 60, + [OpCode.REVERSE4] = 60, + [OpCode.REVERSEN] = 400, + [OpCode.TOALTSTACK] = 60, + [OpCode.FROMALTSTACK] = 60, + [OpCode.DUPFROMALTSTACK] = 60, + [OpCode.DUPFROMALTSTACKBOTTOM] = 60, + [OpCode.ISNULL] = 60, [OpCode.CAT] = 80000, [OpCode.SUBSTR] = 80000, [OpCode.LEFT] = 80000, @@ -178,8 +132,6 @@ partial class ApplicationEngine [OpCode.HASKEY] = 270000, [OpCode.KEYS] = 500, [OpCode.VALUES] = 7000, - [OpCode.THROW] = 30, - [OpCode.THROWIFNOT] = 30 }; } } diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index bc3694f712..4439012f9c 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -25,7 +25,7 @@ public partial class ApplicationEngine : ExecutionEngine public StoreView Snapshot { get; } public long GasConsumed { get; private set; } = 0; public UInt160 CurrentScriptHash => CurrentContext?.GetState().ScriptHash; - public UInt160 CallingScriptHash => InvocationStack.Count > 1 ? InvocationStack.Peek(1).GetState().ScriptHash : null; + public UInt160 CallingScriptHash => CurrentContext?.GetState().CallingScriptHash; public UInt160 EntryScriptHash => EntryContext?.GetState().ScriptHash; public IReadOnlyList Notifications => notifications; internal Dictionary InvocationCounter { get; } = new Dictionary(); @@ -55,10 +55,7 @@ protected override void LoadContext(ExecutionContext context) { // Set default execution context state - context.SetState(new ExecutionContextState() - { - ScriptHash = ((byte[])context.Script).ToScriptHash() - }); + context.GetState().ScriptHash ??= ((byte[])context.Script).ToScriptHash(); base.LoadContext(context); } diff --git a/src/neo/SmartContract/ContractParametersContext.cs b/src/neo/SmartContract/ContractParametersContext.cs index 2b0b99623f..f57f1f9679 100644 --- a/src/neo/SmartContract/ContractParametersContext.cs +++ b/src/neo/SmartContract/ContractParametersContext.cs @@ -144,16 +144,16 @@ public bool AddSignature(Contract contract, ECPoint pubkey, byte[] signature) int i = 0; switch (contract.Script[i++]) { - case 1: + case (byte)OpCode.PUSHINT8: ++i; break; - case 2: + case (byte)OpCode.PUSHINT16: i += 2; break; } - while (contract.Script[i++] == 33) + while (contract.Script[i++] == (byte)OpCode.PUSHDATA1) { - points.Add(ECPoint.DecodePoint(contract.Script.AsSpan(i, 33), ECCurve.Secp256r1)); + points.Add(ECPoint.DecodePoint(contract.Script.AsSpan(++i, 33), ECCurve.Secp256r1)); i += 33; } } diff --git a/src/neo/SmartContract/ExecutionContextState.cs b/src/neo/SmartContract/ExecutionContextState.cs index d26641f7dd..929048fee1 100644 --- a/src/neo/SmartContract/ExecutionContextState.cs +++ b/src/neo/SmartContract/ExecutionContextState.cs @@ -1,10 +1,15 @@ namespace Neo.SmartContract { - public class ExecutionContextState + internal class ExecutionContextState { /// /// Script hash /// public UInt160 ScriptHash { get; set; } + + /// + /// Calling script hash + /// + public UInt160 CallingScriptHash { get; set; } } } diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 28c6045dcd..0bb328bd0a 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -15,44 +15,49 @@ public static bool IsMultiSigContract(this byte[] script, out int m, out int n) { m = 0; n = 0; int i = 0; - if (script.Length < 42) return false; - if (script[i] > (byte)OpCode.PUSH16) return false; - if (script[i] < (byte)OpCode.PUSH1 && script[i] != 1 && script[i] != 2) return false; + if (script.Length < 43) return false; switch (script[i]) { - case 1: + case (byte)OpCode.PUSHINT8: m = script[++i]; ++i; break; - case 2: + case (byte)OpCode.PUSHINT16: m = BinaryPrimitives.ReadUInt16LittleEndian(script.AsSpan(++i)); i += 2; break; - default: - m = script[i++] - 80; + case byte b when b >= (byte)OpCode.PUSH1 && b <= (byte)OpCode.PUSH16: + m = b - (byte)OpCode.PUSH0; + ++i; break; + default: + return false; } if (m < 1 || m > 1024) return false; - while (script[i] == 33) + while (script[i] == (byte)OpCode.PUSHDATA1) { + if (script.Length <= i + 35) return false; + if (script[++i] != 33) return false; i += 34; - if (script.Length <= i) return false; ++n; } if (n < m || n > 1024) return false; switch (script[i]) { - case 1: + case (byte)OpCode.PUSHINT8: if (n != script[++i]) return false; ++i; break; - case 2: + case (byte)OpCode.PUSHINT16: if (script.Length < i + 3 || n != BinaryPrimitives.ReadUInt16LittleEndian(script.AsSpan(++i))) return false; i += 2; break; - default: - if (n != script[i++] - 80) return false; + case byte b when b >= (byte)OpCode.PUSH1 && b <= (byte)OpCode.PUSH16: + if (n != b - (byte)OpCode.PUSH0) return false; + ++i; break; + default: + return false; } if (script[i++] != (byte)OpCode.PUSHNULL) return false; if (script[i++] != (byte)OpCode.SYSCALL) return false; @@ -64,11 +69,12 @@ public static bool IsMultiSigContract(this byte[] script, out int m, out int n) public static bool IsSignatureContract(this byte[] script) { - if (script.Length != 40) return false; - if (script[0] != (byte)OpCode.PUSHBYTES33 - || script[34] != (byte)OpCode.PUSHNULL - || script[35] != (byte)OpCode.SYSCALL - || BitConverter.ToUInt32(script, 36) != InteropService.Neo_Crypto_ECDsaVerify) + if (script.Length != 41) return false; + if (script[0] != (byte)OpCode.PUSHDATA1 + || script[1] != 33 + || script[35] != (byte)OpCode.PUSHNULL + || script[36] != (byte)OpCode.SYSCALL + || BitConverter.ToUInt32(script, 37) != InteropService.Neo_Crypto_ECDsaVerify) return false; return true; } diff --git a/src/neo/SmartContract/InteropService.cs b/src/neo/SmartContract/InteropService.cs index def6184619..56205d96d1 100644 --- a/src/neo/SmartContract/InteropService.cs +++ b/src/neo/SmartContract/InteropService.cs @@ -486,7 +486,9 @@ private static bool Contract_Call(ApplicationEngine engine) engine.InvocationCounter[contract.ScriptHash] = 1; } + UInt160 callingScriptHash = engine.CurrentScriptHash; ExecutionContext context_new = engine.LoadScript(contract.Script, 1); + context_new.GetState().CallingScriptHash = callingScriptHash; context_new.EvaluationStack.Push(args); context_new.EvaluationStack.Push(method); return true; diff --git a/src/neo/SmartContract/StackItemSerializer.cs b/src/neo/SmartContract/StackItemSerializer.cs index a7f59ad974..73b828a582 100644 --- a/src/neo/SmartContract/StackItemSerializer.cs +++ b/src/neo/SmartContract/StackItemSerializer.cs @@ -47,7 +47,7 @@ private static StackItem Deserialize(BinaryReader reader, uint maxItemSize, Refe deserialized.Push(new Boolean(reader.ReadBoolean())); break; case StackItemType.Integer: - deserialized.Push(new Integer(new BigInteger(reader.ReadVarBytes(ExecutionEngine.MaxSizeForBigInteger)))); + deserialized.Push(new Integer(new BigInteger(reader.ReadVarBytes(Integer.MaxSize)))); break; case StackItemType.Array: case StackItemType.Struct: diff --git a/src/neo/VM/Helper.cs b/src/neo/VM/Helper.cs index 735df63c85..a921f4411a 100644 --- a/src/neo/VM/Helper.cs +++ b/src/neo/VM/Helper.cs @@ -312,7 +312,10 @@ private static StackItem ToStackItem(ContractParameter parameter, List<(StackIte (stackItem, _) = context.FirstOrDefault(p => ReferenceEquals(p.Item2, parameter)); if (stackItem is null) { - stackItem = new Map(((IList>)parameter.Value).ToDictionary(p => (PrimitiveType)ToStackItem(p.Key, context), p => ToStackItem(p.Value, context))); + Map map = new Map(); + foreach (var pair in (IList>)parameter.Value) + map[(PrimitiveType)ToStackItem(pair.Key, context)] = ToStackItem(pair.Value, context); + stackItem = map; context.Add((stackItem, parameter)); } break; diff --git a/src/neo/Wallets/Wallet.cs b/src/neo/Wallets/Wallet.cs index 954923df8f..a616c971db 100644 --- a/src/neo/Wallets/Wallet.cs +++ b/src/neo/Wallets/Wallet.cs @@ -353,17 +353,17 @@ public static long CalculateNetWorkFee(byte[] witness_script, ref int size) if (witness_script.IsSignatureContract()) { - size += 66 + witness_script.GetVarSize(); - networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHBYTES64] + ApplicationEngine.OpCodePrices[OpCode.PUSHBYTES33] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Neo_Crypto_ECDsaVerify, null); + size += 67 + witness_script.GetVarSize(); + networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Neo_Crypto_ECDsaVerify, null); } else if (witness_script.IsMultiSigContract(out int m, out int n)) { - int size_inv = 65 * m; + int size_inv = 66 * m; size += IO.Helper.GetVarSize(size_inv) + size_inv + witness_script.GetVarSize(); - networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHBYTES64] * m; + networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * m; using (ScriptBuilder sb = new ScriptBuilder()) networkFee += ApplicationEngine.OpCodePrices[(OpCode)sb.EmitPush(m).ToArray()[0]]; - networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHBYTES33] * n; + networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * n; using (ScriptBuilder sb = new ScriptBuilder()) networkFee += ApplicationEngine.OpCodePrices[(OpCode)sb.EmitPush(n).ToArray()[0]]; networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Neo_Crypto_ECDsaVerify, null) * n; diff --git a/src/neo/neo.csproj b/src/neo/neo.csproj index 593839600d..ebac81c4bc 100644 --- a/src/neo/neo.csproj +++ b/src/neo/neo.csproj @@ -27,7 +27,7 @@ - + diff --git a/tests/neo.UnitTests/Consensus/UT_Consensus.cs b/tests/neo.UnitTests/Consensus/UT_Consensus.cs index 06ca0f0cd7..97ebacfdbe 100644 --- a/tests/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/tests/neo.UnitTests/Consensus/UT_Consensus.cs @@ -153,8 +153,8 @@ public void ConsensusService_SingleNodeActors_OnStart_PrepReq_PrepResponses_Comm Contract originalContract = Contract.CreateMultiSigContract(mockContext.Object.M, mockContext.Object.Validators); Console.WriteLine($"\nORIGINAL Contract is: {originalContract.ScriptHash}"); Console.WriteLine($"ORIGINAL NextConsensus: {mockContext.Object.Block.NextConsensus}\nENSURING values..."); - originalContract.ScriptHash.Should().Be(UInt160.Parse("0xbdbe3ca30e9d74df12ce57ebc95a302dfaa0828c")); - mockContext.Object.Block.NextConsensus.Should().Be(UInt160.Parse("0xbdbe3ca30e9d74df12ce57ebc95a302dfaa0828c")); + originalContract.ScriptHash.Should().Be(UInt160.Parse("0x9412c3107a59fa732ccd94866976f7bbb3d9c372")); + mockContext.Object.Block.NextConsensus.Should().Be(UInt160.Parse("0x9412c3107a59fa732ccd94866976f7bbb3d9c372")); Console.WriteLine("\n=========================="); Console.WriteLine("will trigger OnPersistCompleted again with OnStart flag!"); @@ -175,7 +175,7 @@ public void ConsensusService_SingleNodeActors_OnStart_PrepReq_PrepResponses_Comm Console.WriteLine("will create template MakePrepareRequest..."); mockContext.Object.PrevHeader.Timestamp = defaultTimestamp; - mockContext.Object.PrevHeader.NextConsensus.Should().Be(UInt160.Parse("0xbdbe3ca30e9d74df12ce57ebc95a302dfaa0828c")); + mockContext.Object.PrevHeader.NextConsensus.Should().Be(UInt160.Parse("0x9412c3107a59fa732ccd94866976f7bbb3d9c372")); var prepReq = mockContext.Object.MakePrepareRequest(); var ppToSend = (PrepareRequest)prepReq.ConsensusMessage; // Forcing hashes to 0 because mempool is currently shared diff --git a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs index d93555262a..622f156b16 100644 --- a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs +++ b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs @@ -70,13 +70,13 @@ public void TestContainsTransaction() [TestMethod] public void TestGetCurrentBlockHash() { - Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0x0d492ce0f38090a65b2b01af50f7a6d685b6b76fbc41672762e96b05d15d742c")); + Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0x2b8a21dfaf989dc1a5f2694517aefdbda1dd340f3cf177187d73e038a58ad2bb")); } [TestMethod] public void TestGetCurrentHeaderHash() { - Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0x0d492ce0f38090a65b2b01af50f7a6d685b6b76fbc41672762e96b05d15d742c")); + Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0x2b8a21dfaf989dc1a5f2694517aefdbda1dd340f3cf177187d73e038a58ad2bb")); } [TestMethod] @@ -88,7 +88,7 @@ public void TestGetBlock() [TestMethod] public void TestGetBlockHash() { - Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0x0d492ce0f38090a65b2b01af50f7a6d685b6b76fbc41672762e96b05d15d742c")); + Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0x2b8a21dfaf989dc1a5f2694517aefdbda1dd340f3cf177187d73e038a58ad2bb")); Blockchain.Singleton.GetBlockHash(10).Should().BeNull(); } diff --git a/tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs b/tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs index 482297f7ab..b74a0b2a9e 100644 --- a/tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs +++ b/tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs @@ -25,7 +25,7 @@ public static TrimmedBlock GetTrimmedBlockWithNoTransaction() Witness = new Witness { InvocationScript = new byte[0], - VerificationScript = new[] { (byte)OpCode.PUSHT } + VerificationScript = new[] { (byte)OpCode.PUSH1 } }, Hashes = new UInt256[0] }; diff --git a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Block.cs b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Block.cs index fd8f821c6a..2090acda87 100644 --- a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Block.cs +++ b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Block.cs @@ -84,7 +84,7 @@ public void Serialize() UInt256 val256 = UInt256.Zero; TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 1); - var hex = "0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d8e913ff854c000000000000000000000000000000000000000000000000000000010001510200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000"; + var hex = "0000000000000000000000000000000000000000000000000000000000000000000000007227ba7b747f1a98f68679d4a98b68927646ab195a6f56b542ca5a0e6a412662e913ff854c000000000000000000000000000000000000000000000000000000010001110200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000"; uut.ToArray().ToHexString().Should().Be(hex); } @@ -94,7 +94,7 @@ public void Deserialize() UInt256 val256 = UInt256.Zero; TestUtils.SetupBlockWithValues(new Block(), val256, out var merkRoot, out var val160, out var timestampVal, out var indexVal, out var scriptVal, out var transactionsVal, 1); - var hex = "0000000000000000000000000000000000000000000000000000000000000000000000007227ba7b747f1a98f68679d4a98b68927646ab195a6f56b542ca5a0e6a412662e913ff854c000000000000000000000000000000000000000000000000000000010001510200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000"; + var hex = "0000000000000000000000000000000000000000000000000000000000000000000000007227ba7b747f1a98f68679d4a98b68927646ab195a6f56b542ca5a0e6a412662e913ff854c000000000000000000000000000000000000000000000000000000010001110200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000"; using (MemoryStream ms = new MemoryStream(hex.HexToBytes(), false)) using (BinaryReader reader = new BinaryReader(ms)) @@ -199,18 +199,18 @@ public void ToJson() JObject jObj = uut.ToJson(); jObj.Should().NotBeNull(); - jObj["hash"].AsString().Should().Be("0x4e1d8392e7c44830e7e45c18e5e0e3ef3c36af883868846d3691a436a62494b2"); + jObj["hash"].AsString().Should().Be("0x865b348426952d2eaa978cf5d37dc85ec5f95bc56c57c1379d1be21898e74c1e"); jObj["size"].AsNumber().Should().Be(166); jObj["version"].AsNumber().Should().Be(0); jObj["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000"); - jObj["merkleroot"].AsString().Should().Be("0xd841af3d6bd7adb4bca24306725f9aec363edb10de3cafc5f8cca948d7b0290f"); + jObj["merkleroot"].AsString().Should().Be("0x6226416a0e5aca42b5566f5a19ab467692688ba9d47986f6981a7f747bba2772"); jObj["time"].AsNumber().Should().Be(328665601001); jObj["index"].AsNumber().Should().Be(0); jObj["nextconsensus"].AsString().Should().Be("NKuyBkoGdZZSLyPbJEetheRhMjeznFZszf"); JObject scObj = ((JArray)jObj["witnesses"])[0]; scObj["invocation"].AsString().Should().Be(""); - scObj["verification"].AsString().Should().Be("UQ=="); + scObj["verification"].AsString().Should().Be("EQ=="); jObj["tx"].Should().NotBeNull(); JArray txObj = (JArray)jObj["tx"]; diff --git a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Header.cs b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Header.cs index 3eabd054b6..4db51e9bef 100644 --- a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Header.cs +++ b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Header.cs @@ -35,7 +35,7 @@ public void Deserialize() uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid - var hex = "0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d8e913ff854c0000000000000000000000000000000000000000000000000000000100015100"; + var hex = "0000000000000000000000000000000000000000000000000000000000000000000000007227ba7b747f1a98f68679d4a98b68927646ab195a6f56b542ca5a0e6a412662e913ff854c0000000000000000000000000000000000000000000000000000000100011100"; using (MemoryStream ms = new MemoryStream(hex.HexToBytes(), false)) { @@ -96,7 +96,7 @@ public void Serialize() UInt256 val256 = UInt256.Zero; TestUtils.SetupHeaderWithValues(uut, val256, out _, out _, out _, out _, out _); - var hex = "0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d8e913ff854c0000000000000000000000000000000000000000000000000000000100015100"; + var hex = "0000000000000000000000000000000000000000000000000000000000000000000000007227ba7b747f1a98f68679d4a98b68927646ab195a6f56b542ca5a0e6a412662e913ff854c0000000000000000000000000000000000000000000000000000000100011100"; uut.ToArray().ToHexString().Should().Be(hex); } } diff --git a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs index 3430d84390..679103a470 100644 --- a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs +++ b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs @@ -164,9 +164,9 @@ public void FeeIsMultiSigContract() } var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot); - Assert.AreEqual(verificationGas, 2000570); - Assert.AreEqual(sizeGas, 359000); - Assert.AreEqual(tx.NetworkFee, 2359570); + Assert.AreEqual(2000810, verificationGas); + Assert.AreEqual(367000, sizeGas); + Assert.AreEqual(2367810, tx.NetworkFee); } } @@ -214,7 +214,7 @@ public void FeeIsSignatureContractDetailed() Assert.IsNull(tx.Witnesses); // check pre-computed network fee (already guessing signature sizes) - tx.NetworkFee.Should().Be(1258270); + tx.NetworkFee.Should().Be(1264390); // ---- // Sign @@ -251,37 +251,37 @@ public void FeeIsSignatureContractDetailed() verificationGas += engine.GasConsumed; } } - Assert.AreEqual(verificationGas, 1000270); + Assert.AreEqual(verificationGas, 1000390); // ------------------ // check tx_size cost // ------------------ - Assert.AreEqual(tx.Size, 258); + Assert.AreEqual(264, tx.Size); // will verify tx size, step by step // Part I - Assert.AreEqual(Transaction.HeaderSize, 45); + Assert.AreEqual(45, Transaction.HeaderSize); // Part II - Assert.AreEqual(tx.Attributes.GetVarSize(), 1); - Assert.AreEqual(tx.Attributes.Length, 0); - Assert.AreEqual(tx.Cosigners.Length, 1); - Assert.AreEqual(tx.Cosigners.GetVarSize(), 22); + Assert.AreEqual(1, tx.Attributes.GetVarSize()); + Assert.AreEqual(0, tx.Attributes.Length); + Assert.AreEqual(1, tx.Cosigners.Length); + Assert.AreEqual(22, tx.Cosigners.GetVarSize()); // Note that Data size and Usage size are different (because of first byte on GetVarSize()) - Assert.AreEqual(tx.Cosigners[0].Size, 21); + Assert.AreEqual(21, tx.Cosigners[0].Size); // Part III - Assert.AreEqual(tx.Script.GetVarSize(), 82); + Assert.AreEqual(86, tx.Script.GetVarSize()); // Part IV - Assert.AreEqual(tx.Witnesses.GetVarSize(), 108); + Assert.AreEqual(110, tx.Witnesses.GetVarSize()); // I + II + III + IV - Assert.AreEqual(tx.Size, 45 + 23 + 82 + 108); + Assert.AreEqual(45 + 23 + 86 + 110, tx.Size); - Assert.AreEqual(NativeContract.Policy.GetFeePerByte(snapshot), 1000); + Assert.AreEqual(1000, NativeContract.Policy.GetFeePerByte(snapshot)); var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot); - Assert.AreEqual(sizeGas, 258000); + Assert.AreEqual(264000, sizeGas); // final check on sum: verification_cost + tx_size - Assert.AreEqual(verificationGas + sizeGas, 1258270); + Assert.AreEqual(1264390, verificationGas + sizeGas); // final assert Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas); } @@ -374,7 +374,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, 1258270); + Assert.AreEqual(1264390, verificationGas + sizeGas); // final assert Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas); } @@ -468,7 +468,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, 1279270); + Assert.AreEqual(1285390, verificationGas + sizeGas); // final assert Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas); } @@ -565,7 +565,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, 1279270); + Assert.AreEqual(1285390, verificationGas + sizeGas); // final assert Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas); } @@ -722,7 +722,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, 1299270); + Assert.AreEqual(1305390, verificationGas + sizeGas); // final assert Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas); } @@ -838,7 +838,7 @@ public void Transaction_Serialize_Deserialize_Simple() "04030201" + // timelimit "00" + // no attributes "00" + // no cosigners - "0151" + // push1 script + "0111" + // push1 script "00"); // no witnesses // try to deserialize @@ -889,7 +889,7 @@ public void Transaction_Serialize_Deserialize_DistinctCosigners() byte[] sTx = txDoubleCosigners.ToArray(); // no need for detailed hexstring here (see basic tests for it) - sTx.ToHexString().Should().Be("0004030201000000000000000000000000000000000000000000e1f505000000000100000000000000040302010002090807060504030201000908070605040302010000090807060504030201000908070605040302010001015100"); + sTx.ToHexString().Should().Be("0004030201000000000000000000000000000000000000000000e1f505000000000100000000000000040302010002090807060504030201000908070605040302010000090807060504030201000908070605040302010001011100"); // back to transaction (should fail, due to non-distinct cosigners) Transaction tx2 = null; @@ -1072,7 +1072,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, 1258270); + Assert.AreEqual(1264390, verificationGas + sizeGas); // final assert Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas); } diff --git a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs index 87c82124b5..aebda4d9da 100644 --- a/tests/neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs +++ b/tests/neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs @@ -82,9 +82,9 @@ public void MaxSize_OK() // Check max size - witness.Size.Should().Be(1004); - witness.InvocationScript.GetVarSize().Should().Be(653); - witness.VerificationScript.GetVarSize().Should().Be(351); + witness.Size.Should().Be(1024); + witness.InvocationScript.GetVarSize().Should().Be(663); + witness.VerificationScript.GetVarSize().Should().Be(361); Assert.IsTrue(witness.Size <= 1024); diff --git a/tests/neo.UnitTests/SmartContract/UT_Contract.cs b/tests/neo.UnitTests/SmartContract/UT_Contract.cs index c0f4a86cf1..36efd787cb 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Contract.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Contract.cs @@ -1,11 +1,11 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.SmartContract; +using Neo.VM; using Neo.Wallets; using System; using System.Linq; using System.Security.Cryptography; -using System.Text; namespace Neo.UnitTests.SmartContract { @@ -20,13 +20,13 @@ public void TestGetAddress() rng.GetBytes(privateKey); KeyPair key = new KeyPair(privateKey); Contract contract = Contract.CreateSignatureContract(key.PublicKey); - byte[] script = contract.Script; - byte[] expectedArray = new byte[40]; - expectedArray[0] = 0x21; - Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33); - expectedArray[34] = 0x50; - expectedArray[35] = 0x68; - Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 36, 4); + byte[] expectedArray = new byte[41]; + expectedArray[0] = (byte)OpCode.PUSHDATA1; + expectedArray[1] = 0x21; + Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 2, 33); + expectedArray[35] = (byte)OpCode.PUSHNULL; + expectedArray[36] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 37, 4); Assert.AreEqual(expectedArray.ToScriptHash().ToAddress(), contract.Address); } @@ -38,13 +38,13 @@ public void TestGetScriptHash() rng.GetBytes(privateKey); KeyPair key = new KeyPair(privateKey); Contract contract = Contract.CreateSignatureContract(key.PublicKey); - byte[] script = contract.Script; - byte[] expectedArray = new byte[40]; - expectedArray[0] = 0x21; - Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33); - expectedArray[34] = 0x50; - expectedArray[35] = 0x68; - Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 36, 4); + byte[] expectedArray = new byte[41]; + expectedArray[0] = (byte)OpCode.PUSHDATA1; + expectedArray[1] = 0x21; + Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 2, 33); + expectedArray[35] = (byte)OpCode.PUSHNULL; + expectedArray[36] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 37, 4); Assert.AreEqual(expectedArray.ToScriptHash(), contract.ScriptHash); } @@ -75,17 +75,19 @@ public void TestCreateMultiSigContract() publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); Contract contract = Contract.CreateMultiSigContract(2, publicKeys); - 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] = 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)); + byte[] expectedArray = new byte[78]; + expectedArray[0] = (byte)OpCode.PUSH2; + expectedArray[1] = (byte)OpCode.PUSHDATA1; + expectedArray[2] = 0x21; + Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 3, 33); + expectedArray[36] = (byte)OpCode.PUSHDATA1; + expectedArray[37] = 0x21; + Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 38, 33); + expectedArray[71] = (byte)OpCode.PUSH2; + expectedArray[72] = (byte)OpCode.PUSHNULL; + expectedArray[73] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaCheckMultiSig), 0, expectedArray, 74, 4); + CollectionAssert.AreEqual(expectedArray, contract.Script); Assert.AreEqual(2, contract.ParameterList.Length); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[1]); @@ -109,17 +111,19 @@ public void TestCreateMultiSigRedeemScript() Action action = () => Contract.CreateMultiSigRedeemScript(0, publicKeys); action.Should().Throw(); byte[] script = Contract.CreateMultiSigRedeemScript(2, publicKeys); - 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] = 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)); + byte[] expectedArray = new byte[78]; + expectedArray[0] = (byte)OpCode.PUSH2; + expectedArray[1] = (byte)OpCode.PUSHDATA1; + expectedArray[2] = 0x21; + Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 3, 33); + expectedArray[36] = (byte)OpCode.PUSHDATA1; + expectedArray[37] = 0x21; + Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 38, 33); + expectedArray[71] = (byte)OpCode.PUSH2; + expectedArray[72] = (byte)OpCode.PUSHNULL; + expectedArray[73] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaCheckMultiSig), 0, expectedArray, 74, 4); + CollectionAssert.AreEqual(expectedArray, script); } [TestMethod] @@ -130,14 +134,14 @@ public void TestCreateSignatureContract() rng.GetBytes(privateKey); KeyPair key = new KeyPair(privateKey); Contract contract = Contract.CreateSignatureContract(key.PublicKey); - byte[] script = contract.Script; - byte[] expectedArray = new byte[40]; - expectedArray[0] = 0x21; - Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33); - 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)); + byte[] expectedArray = new byte[41]; + expectedArray[0] = (byte)OpCode.PUSHDATA1; + expectedArray[1] = 0x21; + Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 2, 33); + expectedArray[35] = (byte)OpCode.PUSHNULL; + expectedArray[36] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 37, 4); + CollectionAssert.AreEqual(expectedArray, contract.Script); Assert.AreEqual(1, contract.ParameterList.Length); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]); } @@ -150,13 +154,14 @@ public void TestCreateSignatureRedeemScript() rng.GetBytes(privateKey); KeyPair key = new KeyPair(privateKey); byte[] script = Contract.CreateSignatureRedeemScript(key.PublicKey); - byte[] expectedArray = new byte[40]; - expectedArray[0] = 0x21; - Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 1, 33); - 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)); + byte[] expectedArray = new byte[41]; + expectedArray[0] = (byte)OpCode.PUSHDATA1; + expectedArray[1] = 0x21; + Array.Copy(key.PublicKey.EncodePoint(true), 0, expectedArray, 2, 33); + expectedArray[35] = (byte)OpCode.PUSHNULL; + expectedArray[36] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_ECDsaVerify), 0, expectedArray, 37, 4); + CollectionAssert.AreEqual(expectedArray, script); } } } diff --git a/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs b/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs index f5fd304243..68a11c2ff1 100644 --- a/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs +++ b/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs @@ -3,6 +3,7 @@ using Neo.Cryptography.ECC; using Neo.Network.P2P.Payloads; using Neo.SmartContract; +using Neo.VM; using Neo.Wallets; using System; @@ -33,7 +34,7 @@ public static void ClassSetUp(TestContext context) public void TestGetComplete() { Transaction tx = TestUtils.GetTransaction(); - tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c"); + tx.Sender = UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); var context = new ContractParametersContext(tx); context.Completed.Should().BeFalse(); } @@ -42,11 +43,11 @@ public void TestGetComplete() public void TestToString() { Transaction tx = TestUtils.GetTransaction(); - tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c"); + tx.Sender = UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); 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"":""AAAAAAB8l3ZIRRctgn08hjdDKTkxppEnGgAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA=="",""items"":{""0x1a2791a63139294337863c7d822d17454876977c"":{""script"":""IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFlBoCpBq1A=="",""parameters"":[{""type"":""Signature"",""value"":""AQ==""}]}}}"); + str.Should().Be(@"{""type"":""Neo.Network.P2P.Payloads.Transaction"",""hex"":""AAAAAABmUJDLobcPtqo9vZKIdjXsd8fVGwAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA=="",""items"":{""0x1bd5c777ec35768892bd3daab60fb7a1cb905066"":{""script"":""DCECb/A7lJJBzh2t1DUZ5pYOCoW0GmmgXDKBA6orzhWUyhYLQQqQatQ="",""parameters"":[{""type"":""Signature"",""value"":""AQ==""}]}}}"); } [TestMethod] @@ -60,7 +61,7 @@ public void TestParse() [TestMethod] public void TestFromJson() { - Action action = () => ContractParametersContext.Parse("{\"type\":\"wrongType\",\"hex\":\"00000000007c97764845172d827d3c863743293931a691271a0000000000000000000000000000000000000000000100\",\"items\":{\"0x1a2791a63139294337863c7d822d17454876977c\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1650680a906ad4\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}"); + Action action = () => ContractParametersContext.Parse("{\"type\":\"wrongType\",\"hex\":\"00000000007c97764845172d827d3c863743293931a691271a0000000000000000000000000000000000000000000100\",\"items\":{\"0x1bd5c777ec35768892bd3daab60fb7a1cb905066\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1650680a906ad4\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}"); action.Should().Throw(); } @@ -71,7 +72,7 @@ public void TestAdd() var context1 = new ContractParametersContext(tx); context1.Add(contract, 0, new byte[] { 0x01 }).Should().BeFalse(); - tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c"); + tx.Sender = UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); var context2 = new ContractParametersContext(tx); context2.Add(contract, 0, new byte[] { 0x01 }).Should().BeTrue(); //test repeatlly createItem @@ -82,7 +83,7 @@ public void TestAdd() public void TestGetParameter() { Transaction tx = TestUtils.GetTransaction(); - tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c"); + tx.Sender = UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); var context = new ContractParametersContext(tx); context.GetParameter(tx.Sender, 0).Should().BeNull(); @@ -95,12 +96,12 @@ public void TestGetParameter() public void TestGetWitnesses() { Transaction tx = TestUtils.GetTransaction(); - tx.Sender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c"); + tx.Sender = UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); var context = new ContractParametersContext(tx); context.Add(contract, 0, new byte[] { 0x01 }); Witness[] witnesses = context.GetWitnesses(); witnesses.Length.Should().Be(1); - witnesses[0].InvocationScript.ToHexString().Should().Be(new byte[] { 0x01, 0x01 }.ToHexString()); + witnesses[0].InvocationScript.ToHexString().Should().Be(new byte[] {(byte)OpCode.PUSHDATA1, 0x01, 0x01 }.ToHexString()); witnesses[0].VerificationScript.ToHexString().Should().Be(contract.Script.ToHexString()); } @@ -108,7 +109,7 @@ public void TestGetWitnesses() public void TestAddSignature() { Transaction tx = TestUtils.GetTransaction(); - var singleSender = UInt160.Parse("0x1a2791a63139294337863c7d822d17454876977c"); + var singleSender = UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); tx.Sender = singleSender; //singleSign @@ -138,7 +139,7 @@ public void TestAddSignature() key.PublicKey, key2.PublicKey }); - var multiSender = UInt160.Parse("0xfc8b59f1a337dcc17b1a201d327a2081d41fac8d"); + var multiSender = UInt160.Parse("0xd8e21c5f8b2e48c409220a3aff34a7fc4c87fbe9"); tx.Sender = multiSender; context = new ContractParametersContext(tx); context.AddSignature(multiSignContract, key.PublicKey, new byte[] { 0x01 }).Should().BeTrue(); diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs b/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs index d6b9b8254b..ac8a017d1f 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs @@ -40,18 +40,18 @@ public void ApplicationEngineFixedPrices() public void ApplicationEngineVariablePrices() { // Neo.Contract.Create: f66ca56e (requires push properties on fourth position) - byte[] SyscallContractCreateHash00 = new byte[] { 0x01, 0x00, 0x02, 0x00, 0x00, 0x68, 0xf6, 0x6c, 0xa5, 0x6e }; + byte[] SyscallContractCreateHash00 = new byte[] { (byte)OpCode.PUSHDATA1, 0x01, 0x00, (byte)OpCode.PUSHDATA1, 0x02, 0x00, 0x00, (byte)OpCode.SYSCALL, 0xf6, 0x6c, 0xa5, 0x6e }; using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true)) { Debugger debugger = new Debugger(ae); ae.LoadScript(SyscallContractCreateHash00); - debugger.StepInto(); // PUSHBYTES1 - debugger.StepInto(); // PUSHBYTES2 + debugger.StepInto(); // PUSHDATA1 + debugger.StepInto(); // PUSHDATA1 InteropService.GetPrice(InteropService.Neo_Contract_Create, ae.CurrentContext.EvaluationStack).Should().Be(0_00300000L); } // System.Storage.Put: e63f1884 (requires push key and value) - byte[] SyscallStoragePutHash = new byte[] { 0x53, 0x53, 0x00, 0x68, 0xe6, 0x3f, 0x18, 0x84 }; + byte[] SyscallStoragePutHash = new byte[] { (byte)OpCode.PUSH3, (byte)OpCode.PUSH3, (byte)OpCode.PUSH0, (byte)OpCode.SYSCALL, 0xe6, 0x3f, 0x18, 0x84 }; using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true)) { Debugger debugger = new Debugger(ae); @@ -63,7 +63,7 @@ public void ApplicationEngineVariablePrices() } // System.Storage.PutEx: 73e19b3a (requires push key and value) - byte[] SyscallStoragePutExHash = new byte[] { 0x53, 0x53, 0x00, 0x68, 0x73, 0xe1, 0x9b, 0x3a }; + byte[] SyscallStoragePutExHash = new byte[] { (byte)OpCode.PUSH3, (byte)OpCode.PUSH3, (byte)OpCode.PUSH0, (byte)OpCode.SYSCALL, 0x73, 0xe1, 0x9b, 0x3a }; using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0, testMode: true)) { Debugger debugger = new Debugger(ae); diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs index e1f29ad4ad..fd20a7afea 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs @@ -226,12 +226,6 @@ public void TestExecutionEngine_GetCallingScriptHash() var engine = GetEngine(true); InteropService.Invoke(engine, InteropService.System_ExecutionEngine_GetCallingScriptHash).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().Should().Be(StackItem.Null); - - engine = GetEngine(true); - engine.LoadScript(new byte[] { 0x01 }); - InteropService.Invoke(engine, InteropService.System_ExecutionEngine_GetCallingScriptHash).Should().BeTrue(); - engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString() - .Should().Be(engine.CallingScriptHash.ToArray().ToHexString()); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/UT_StackItemSerializer.cs b/tests/neo.UnitTests/SmartContract/UT_StackItemSerializer.cs index 80acd2082d..9bd1db4a69 100644 --- a/tests/neo.UnitTests/SmartContract/UT_StackItemSerializer.cs +++ b/tests/neo.UnitTests/SmartContract/UT_StackItemSerializer.cs @@ -4,6 +4,7 @@ using Neo.VM.Types; using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace Neo.UnitTests.SmartContract @@ -60,8 +61,7 @@ public void TestSerialize() }; Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7)); - Dictionary list8 = new Dictionary { [2] = 1 }; - StackItem stackItem82 = new Map(list8); + StackItem stackItem82 = new Map { [2] = 1 }; byte[] result8 = StackItemSerializer.Serialize(stackItem82); byte[] expectedArray8 = new byte[] { 0x82,0x01,0x02,0x01,0x02,0x02,0x01,0x01 @@ -116,13 +116,12 @@ public void TestDeserializeStackItem() Assert.AreEqual(((Struct)stackItem62).Count, ((Struct)result6).Count); Assert.AreEqual(((Struct)stackItem62).GetEnumerator().Current, ((Struct)result6).GetEnumerator().Current); - Dictionary list7 = new Dictionary { [2] = 1 }; - StackItem stackItem72 = new Map(list7); + StackItem stackItem72 = new Map { [2] = 1 }; byte[] byteArray7 = StackItemSerializer.Serialize(stackItem72); StackItem result7 = StackItemSerializer.Deserialize(byteArray7, (uint)byteArray7.Length); Assert.AreEqual(((Map)stackItem72).Count, ((Map)result7).Count); - Assert.AreEqual(((Map)stackItem72).Keys.GetEnumerator().Current, ((Map)result7).Keys.GetEnumerator().Current); - Assert.AreEqual(((Map)stackItem72).Values.GetEnumerator().Current, ((Map)result7).Values.GetEnumerator().Current); + CollectionAssert.AreEqual(((Map)stackItem72).Keys.ToArray(), ((Map)result7).Keys.ToArray()); + CollectionAssert.AreEqual(((Map)stackItem72).Values.ToArray(), ((Map)result7).Values.ToArray()); } } } diff --git a/tests/neo.UnitTests/TestUtils.cs b/tests/neo.UnitTests/TestUtils.cs index 83f3d6515e..a68eba49a0 100644 --- a/tests/neo.UnitTests/TestUtils.cs +++ b/tests/neo.UnitTests/TestUtils.cs @@ -8,6 +8,7 @@ using Neo.Wallets.NEP6; using System; using System.IO; +using System.Linq; namespace Neo.UnitTests { @@ -83,12 +84,13 @@ public static void SetupBlockWithValues(Block block, UInt256 val256, out UInt256 block.ConsensusData = new ConsensusData(); block.Transactions = transactionsVal; + block.MerkleRoot = merkRootVal = Block.CalculateMerkleRoot(block.ConsensusData.Hash, block.Transactions.Select(p => p.Hash)); } private static void setupBlockBaseWithValues(BlockBase bb, UInt256 val256, out UInt256 merkRootVal, out UInt160 val160, out ulong timestampVal, out uint indexVal, out Witness scriptVal) { bb.PrevHash = val256; - merkRootVal = UInt256.Parse("0xd841af3d6bd7adb4bca24306725f9aec363edb10de3cafc5f8cca948d7b0290f"); + merkRootVal = UInt256.Parse("0x6226416a0e5aca42b5566f5a19ab467692688ba9d47986f6981a7f747bba2772"); bb.MerkleRoot = merkRootVal; timestampVal = new DateTime(1980, 06, 01, 0, 0, 1, 001, DateTimeKind.Utc).ToTimestampMS(); // GMT: Sunday, June 1, 1980 12:00:01.001 AM bb.Timestamp = timestampVal; @@ -99,7 +101,7 @@ private static void setupBlockBaseWithValues(BlockBase bb, UInt256 val256, out U scriptVal = new Witness { InvocationScript = new byte[0], - VerificationScript = new[] { (byte)OpCode.PUSHT } + VerificationScript = new[] { (byte)OpCode.PUSH1 } }; bb.Witness = scriptVal; } diff --git a/tests/neo.UnitTests/VM/UT_Helper.cs b/tests/neo.UnitTests/VM/UT_Helper.cs index 5f1632824d..1923f48a66 100644 --- a/tests/neo.UnitTests/VM/UT_Helper.cs +++ b/tests/neo.UnitTests/VM/UT_Helper.cs @@ -23,27 +23,28 @@ public void TestEmit() { ScriptBuilder sb = new ScriptBuilder(); sb.Emit(new OpCode[] { OpCode.PUSH0 }); - Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0x00 }), Encoding.Default.GetString(sb.ToArray())); + CollectionAssert.AreEqual(new[] { (byte)OpCode.PUSH0 }, sb.ToArray()); } [TestMethod] public void TestEmitAppCall1() { - //format:(byte)0x00+(byte)OpCode.NEWARRAY+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call + //format:(byte)0x10+(byte)OpCode.NEWARRAY+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call ScriptBuilder sb = new ScriptBuilder(); sb.EmitAppCall(UInt160.Zero, "AAAAA"); - byte[] tempArray = new byte[34]; - tempArray[0] = 0x00;//0 - tempArray[1] = 0xC5;//OpCode.NEWARRAY - tempArray[2] = 5;//operation.Length - Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 3, 5);//operation.data - tempArray[8] = 0x14;//scriptHash.Length - Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 9, 20);//operation.data + byte[] tempArray = new byte[36]; + tempArray[0] = (byte)OpCode.PUSH0; + tempArray[1] = (byte)OpCode.NEWARRAY; + tempArray[2] = (byte)OpCode.PUSHDATA1; + tempArray[3] = 5;//operation.Length + Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 4, 5);//operation.data + tempArray[9] = (byte)OpCode.PUSHDATA1; + tempArray[10] = 0x14;//scriptHash.Length + Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 11, 20);//operation.data uint api = InteropService.System_Contract_Call; - tempArray[29] = 0x68;//OpCode.SYSCALL - Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 30, 4);//api.data - byte[] resultArray = sb.ToArray(); - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(resultArray)); + tempArray[31] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 32, 4);//api.data + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } [TestMethod] @@ -52,19 +53,20 @@ public void TestEmitAppCall2() //format:(ContractParameter[])ContractParameter+(byte)OpCode.PACK+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call ScriptBuilder sb = new ScriptBuilder(); sb.EmitAppCall(UInt160.Zero, "AAAAA", new ContractParameter[] { new ContractParameter(ContractParameterType.Integer) }); - byte[] tempArray = new byte[35]; - tempArray[0] = 0x00;//0 - tempArray[1] = 0x51;//ContractParameter.Length - tempArray[2] = 0xC1;//OpCode.PACK - tempArray[3] = 0x05;//operation.Length - Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 4, 5);//operation.data - tempArray[9] = 0x14;//scriptHash.Length - Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 10, 20);//operation.data + byte[] tempArray = new byte[37]; + tempArray[0] = (byte)OpCode.PUSH0; + tempArray[1] = (byte)OpCode.PUSH1; + tempArray[2] = (byte)OpCode.PACK; + tempArray[3] = (byte)OpCode.PUSHDATA1; + tempArray[4] = 0x05;//operation.Length + Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 5, 5);//operation.data + tempArray[10] = (byte)OpCode.PUSHDATA1; + tempArray[11] = 0x14;//scriptHash.Length + Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 12, 20);//operation.data uint api = InteropService.System_Contract_Call; - tempArray[30] = 0x68;//OpCode.SYSCALL - Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 31, 4);//api.data - byte[] resultArray = sb.ToArray(); - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(resultArray)); + tempArray[32] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 33, 4);//api.data + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } [TestMethod] @@ -73,19 +75,20 @@ public void TestEmitAppCall3() //format:(object[])args+(byte)OpCode.PACK+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call ScriptBuilder sb = new ScriptBuilder(); sb.EmitAppCall(UInt160.Zero, "AAAAA", true); - byte[] tempArray = new byte[35]; - tempArray[0] = 0x51;//arg - tempArray[1] = 0x51;//args.Length - tempArray[2] = 0xC1;//OpCode.PACK - tempArray[3] = 0x05;//operation.Length - Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 4, 5);//operation.data - tempArray[9] = 0x14;//scriptHash.Length - Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 10, 20);//operation.data + byte[] tempArray = new byte[37]; + tempArray[0] = (byte)OpCode.PUSH1;//arg + tempArray[1] = (byte)OpCode.PUSH1;//args.Length + tempArray[2] = (byte)OpCode.PACK; + tempArray[3] = (byte)OpCode.PUSHDATA1; + tempArray[4] = 0x05;//operation.Length + Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 5, 5);//operation.data + tempArray[10] = (byte)OpCode.PUSHDATA1; + tempArray[11] = 0x14;//scriptHash.Length + Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 12, 20);//operation.data uint api = InteropService.System_Contract_Call; - tempArray[30] = 0x68;//OpCode.SYSCALL - Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 31, 4);//api.data - byte[] resultArray = sb.ToArray(); - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(resultArray)); + tempArray[32] = (byte)OpCode.SYSCALL; + Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 33, 4);//api.data + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } [TestMethod] @@ -93,7 +96,7 @@ public void TestMakeScript() { byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero); - Assert.AreEqual("14000000000000000000000000000000000000000051c10962616c616e63654f66142582d1b275e86c8f0e93a9b2facd5fdb760976a168627d5b52", + Assert.AreEqual("0c14000000000000000000000000000000000000000011c10c0962616c616e63654f660c143b7d3711c6f0ccf9b1dca903d1bfa1d896f1238c41627d5b52", testScript.ToHexString()); } @@ -115,7 +118,7 @@ public void TestToParameter() StackItem arrayItem = new VM.Types.Array(new[] { byteItem, boolItem, intItem, interopItem }); Assert.AreEqual(1000, (BigInteger)(arrayItem.ToParameter().Value as List)[2].Value); - StackItem mapItem = new Map(new Dictionary { [(PrimitiveType)byteItem] = intItem }); + StackItem mapItem = new Map { [(PrimitiveType)byteItem] = intItem }; Assert.AreEqual(1000, (BigInteger)(mapItem.ToParameter().Value as List>)[0].Value.Value); } @@ -158,9 +161,10 @@ public void TestEmitPush1() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(UInt160.Zero); - byte[] tempArray = new byte[21]; - tempArray[0] = 0x14; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[22]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x14; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } [TestMethod] @@ -192,47 +196,51 @@ private void TestEmitPush2Array() parameter.Value = values; sb.EmitPush(parameter); byte[] tempArray = new byte[4]; - tempArray[0] = 0x00; - tempArray[1] = 0x00; - tempArray[2] = 0x52; - tempArray[3] = 0xC1; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + tempArray[1] = (byte)OpCode.PUSH0; + tempArray[2] = (byte)OpCode.PUSH2; + tempArray[3] = (byte)OpCode.PACK; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2String() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.String)); - byte[] tempArray = new byte[1]; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[2]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2PublicKey() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.PublicKey)); - byte[] tempArray = new byte[34]; - tempArray[0] = 0x21; - Array.Copy(ECCurve.Secp256r1.G.EncodePoint(true), 0, tempArray, 1, 33); - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[35]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x21; + Array.Copy(ECCurve.Secp256r1.G.EncodePoint(true), 0, tempArray, 2, 33); + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2Hash256() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.Hash256)); - byte[] tempArray = new byte[33]; - tempArray[0] = 0x20; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[34]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x20; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2Hash160() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.Hash160)); - byte[] tempArray = new byte[21]; - tempArray[0] = 0x14; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[22]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x14; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2BigInteger() @@ -244,8 +252,8 @@ private void TestEmitPush2BigInteger() }; sb.EmitPush(parameter); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2Integer() @@ -254,8 +262,8 @@ private void TestEmitPush2Integer() ContractParameter parameter = new ContractParameter(ContractParameterType.Integer); sb.EmitPush(parameter); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2Boolean() @@ -263,26 +271,28 @@ private void TestEmitPush2Boolean() ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.Boolean)); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2ByteArray() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.ByteArray)); - byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[2]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x00; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush2Signature() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new ContractParameter(ContractParameterType.Signature)); - byte[] tempArray = new byte[65]; - tempArray[0] = 0x40; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[66]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x40; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } enum TestEnum : byte @@ -319,8 +329,8 @@ private void TestEmitPush3Enum() ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(TestEnum.case1); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Ulong() @@ -329,8 +339,8 @@ private void TestEmitPush3Ulong() ulong temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Long() @@ -339,8 +349,8 @@ private void TestEmitPush3Long() long temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Uint() @@ -349,8 +359,8 @@ private void TestEmitPush3Uint() uint temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Int() @@ -359,8 +369,8 @@ private void TestEmitPush3Int() int temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Ushort() @@ -369,8 +379,8 @@ private void TestEmitPush3Ushort() ushort temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Short() @@ -379,8 +389,8 @@ private void TestEmitPush3Short() short temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Byte() @@ -389,8 +399,8 @@ private void TestEmitPush3Byte() byte temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Sbyte() @@ -399,17 +409,18 @@ private void TestEmitPush3Sbyte() sbyte temp = 0; VM.Helper.EmitPush(sb, temp); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3ISerializable() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(UInt160.Zero); - byte[] tempArray = new byte[21]; - tempArray[0] = 0x14; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[22]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x14; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3BigInteger() @@ -417,27 +428,29 @@ private void TestEmitPush3BigInteger() ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(BigInteger.Zero); byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3String() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(""); - byte[] tempArray = new byte[1]; - tempArray[0] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + byte[] tempArray = new byte[2]; + tempArray[0] = (byte)OpCode.PUSHDATA1; + tempArray[1] = 0x00; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3ByteArray() { ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(new byte[] { 0x01 }); - byte[] tempArray = new byte[2]; - tempArray[0] = 0x01; + byte[] tempArray = new byte[3]; + tempArray[0] = (byte)OpCode.PUSHDATA1; tempArray[1] = 0x01; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[2] = 0x01; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } private void TestEmitPush3Bool() @@ -445,8 +458,8 @@ private void TestEmitPush3Bool() ScriptBuilder sb = new ScriptBuilder(); sb.EmitPush(true); byte[] tempArray = new byte[1]; - tempArray[0] = 0x51; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + tempArray[0] = (byte)OpCode.PUSH1; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } [TestMethod] @@ -455,13 +468,13 @@ public void TestEmitSysCall() ScriptBuilder sb = new ScriptBuilder(); sb.EmitSysCall(0, true); byte[] tempArray = new byte[6]; - tempArray[0] = 0x51; - tempArray[1] = 0x68; + tempArray[0] = (byte)OpCode.PUSH1; + tempArray[1] = (byte)OpCode.SYSCALL; tempArray[2] = 0x00; tempArray[3] = 0x00; tempArray[4] = 0x00; tempArray[5] = 0x00; - Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + CollectionAssert.AreEqual(tempArray, sb.ToArray()); } [TestMethod] diff --git a/tests/neo.UnitTests/Wallets/SQLite/UT_VerificationContract.cs b/tests/neo.UnitTests/Wallets/SQLite/UT_VerificationContract.cs index 70d0ba2e4b..19eaa434dc 100644 --- a/tests/neo.UnitTests/Wallets/SQLite/UT_VerificationContract.cs +++ b/tests/neo.UnitTests/Wallets/SQLite/UT_VerificationContract.cs @@ -120,12 +120,12 @@ public void TestSerialize() byte[] byteArray = new byte[stream.Length]; stream.Read(byteArray, 0, (int)stream.Length); byte[] script = Neo.SmartContract.Contract.CreateSignatureRedeemScript(key.PublicKey); - byte[] result = new byte[63]; + byte[] result = new byte[64]; result[20] = 0x01; result[21] = 0x00; - result[22] = 0x28; - Array.Copy(script, 0, result, 23, 40); - Assert.AreEqual(Encoding.Default.GetString(result), Encoding.Default.GetString(byteArray)); + result[22] = 0x29; + Array.Copy(script, 0, result, 23, 41); + CollectionAssert.AreEqual(result, byteArray); } [TestMethod] @@ -142,7 +142,7 @@ public void TestGetSize() Script = Neo.SmartContract.Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature } }; - Assert.AreEqual(63, contract1.Size); + Assert.AreEqual(64, contract1.Size); } } } diff --git a/tests/neo.UnitTests/Wallets/UT_Wallet.cs b/tests/neo.UnitTests/Wallets/UT_Wallet.cs index ed02f4d61e..a210cf581e 100644 --- a/tests/neo.UnitTests/Wallets/UT_Wallet.cs +++ b/tests/neo.UnitTests/Wallets/UT_Wallet.cs @@ -166,9 +166,9 @@ public void TestGetVersion() public void TestGetAccount1() { MyWallet wallet = new MyWallet(); - wallet.CreateAccount(UInt160.Parse("0xc43d04da83afcf7df3b2908c169cfbfbf7512d7f")); + wallet.CreateAccount(UInt160.Parse("0x7e471cf52f27edc291e29ec8f2d1ea2d210d6725")); WalletAccount account = wallet.GetAccount(ECCurve.Secp256r1.G); - account.ScriptHash.Should().Be(UInt160.Parse("0xc43d04da83afcf7df3b2908c169cfbfbf7512d7f")); + account.ScriptHash.Should().Be(UInt160.Parse("0x7e471cf52f27edc291e29ec8f2d1ea2d210d6725")); } [TestMethod] From 8a27c839489624667ddf7d345608006491ded43b Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 12 Dec 2019 18:13:02 +0800 Subject: [PATCH 2/3] dotnet format --- .../neo.UnitTests/SmartContract/UT_ContractParameterContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs b/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs index 68a11c2ff1..f5110b00ea 100644 --- a/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs +++ b/tests/neo.UnitTests/SmartContract/UT_ContractParameterContext.cs @@ -101,7 +101,7 @@ public void TestGetWitnesses() context.Add(contract, 0, new byte[] { 0x01 }); Witness[] witnesses = context.GetWitnesses(); witnesses.Length.Should().Be(1); - witnesses[0].InvocationScript.ToHexString().Should().Be(new byte[] {(byte)OpCode.PUSHDATA1, 0x01, 0x01 }.ToHexString()); + witnesses[0].InvocationScript.ToHexString().Should().Be(new byte[] { (byte)OpCode.PUSHDATA1, 0x01, 0x01 }.ToHexString()); witnesses[0].VerificationScript.ToHexString().Should().Be(contract.Script.ToHexString()); } From 1695f3e78a37ef66c06f0bfd18986b99bc0ed622 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 12 Dec 2019 13:44:19 +0100 Subject: [PATCH 3/3] Add UT for CallingScriptHash --- .../SmartContract/UT_InteropService.cs | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs index fd20a7afea..7421c73ca8 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs @@ -223,9 +223,35 @@ public void TestExecutionEngine_GetExecutingScriptHash() [TestMethod] public void TestExecutionEngine_GetCallingScriptHash() { + // Test without + var engine = GetEngine(true); InteropService.Invoke(engine, InteropService.System_ExecutionEngine_GetCallingScriptHash).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().Should().Be(StackItem.Null); + + // Test real + + using ScriptBuilder scriptA = new ScriptBuilder(); + scriptA.Emit(OpCode.DROP); // Drop arguments + scriptA.Emit(OpCode.DROP); // Drop method + scriptA.EmitSysCall(InteropService.System_ExecutionEngine_GetCallingScriptHash); + + var contract = new ContractState() + { + Manifest = ContractManifest.CreateDefault(scriptA.ToArray().ToScriptHash()), + Script = scriptA.ToArray() + }; + + engine = GetEngine(true, true, false); + engine.Snapshot.Contracts.Add(contract.ScriptHash, contract); + + using ScriptBuilder scriptB = new ScriptBuilder(); + scriptB.EmitAppCall(contract.ScriptHash, ""); + engine.LoadScript(scriptB.ToArray()); + + Assert.AreEqual(VMState.HALT, engine.Execute()); + + engine.ResultStack.Pop().GetSpan().ToHexString().Should().Be(scriptB.ToArray().ToScriptHash().ToArray().ToHexString()); } [TestMethod] @@ -802,28 +828,31 @@ public static void LogEvent(object sender, LogEventArgs args) tx.Script = new byte[] { 0x01, 0x02, 0x03 }; } - private static ApplicationEngine GetEngine(bool hasContainer = false, bool hasSnapshot = false) + private static ApplicationEngine GetEngine(bool hasContainer = false, bool hasSnapshot = false, bool addScript = true) { var tx = TestUtils.GetTransaction(); var snapshot = Blockchain.Singleton.GetSnapshot(); ApplicationEngine engine; if (hasContainer && hasSnapshot) { - engine = new ApplicationEngine(TriggerType.Application, tx, snapshot, 0); + engine = new ApplicationEngine(TriggerType.Application, tx, snapshot, 0, true); } else if (hasContainer && !hasSnapshot) { - engine = new ApplicationEngine(TriggerType.Application, tx, null, 0); + engine = new ApplicationEngine(TriggerType.Application, tx, null, 0, true); } else if (!hasContainer && hasSnapshot) { - engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); + engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); } else { - engine = new ApplicationEngine(TriggerType.Application, null, null, 0); + engine = new ApplicationEngine(TriggerType.Application, null, null, 0, true); + } + if (addScript) + { + engine.LoadScript(new byte[] { 0x01 }); } - engine.LoadScript(new byte[] { 0x01 }); return engine; } }