Esempio n. 1
0
        public void Init()
        {
            _engine = new TestEngine();

            // Deploy native contracts

            ((TestSnapshot)_engine.Snapshot).SetPersistingBlock(new Network.P2P.Payloads.Block()
            {
                Index         = 0,
                ConsensusData = new Network.P2P.Payloads.ConsensusData(),
                Transactions  = new Network.P2P.Payloads.Transaction[0],
                Witness       = new Network.P2P.Payloads.Witness()
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = new byte[0]
                },
                NextConsensus = UInt160.Zero,
                MerkleRoot    = UInt256.Zero,
                PrevHash      = UInt256.Zero
            });

            using (var script = new ScriptBuilder())
            {
                script.EmitSysCall(TestEngine.Native_Deploy);
                _engine.LoadScript(script.ToArray());
                Assert.AreEqual(VMState.HALT, _engine.Execute());
            }

            _engine.Reset();
            _engine.AddEntryScript("./TestClasses/Contract_Native.cs");
        }
Esempio n. 2
0
        public void Test_GetTransactionScript()
        {
            var contract = _engine.EntryScriptHash;

            using ScriptBuilder sb = new();
            sb.EmitDynamicCall(contract, "getTransactionScript");

            var tx = BuildTransaction(UInt160.Zero, sb.ToArray());

            tx.NetworkFee = 200;
            var engine = new TestEngine(TriggerType.Application, tx, new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });
            engine.LoadScript(sb.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(1, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(ByteString));
            Assert.AreEqual(tx.Script.ToHexString(), item.GetSpan().ToHexString());
        }
Esempio n. 3
0
        public void Test_GetTransactionValidUntilBlock()
        {
            var contract = _engine.EntryScriptHash;

            using ScriptBuilder sb = new();
            sb.EmitDynamicCall(contract, "getTransactionValidUntilBlock");

            var tx = BuildTransaction(UInt160.Zero, sb.ToArray());

            tx.ValidUntilBlock = 1111;
            var engine = new TestEngine(TriggerType.Application, tx, new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });
            engine.LoadScript(sb.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(1, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Neo.VM.Types.Integer));
            Assert.AreEqual(tx.ValidUntilBlock, item.GetInteger());
        }
Esempio n. 4
0
        public void Test_GetTransactionSender()
        {
            var contract = _engine.EntryScriptHash;

            using ScriptBuilder sb = new();
            sb.EmitDynamicCall(contract, "getTransactionSender");
            var sender = "NMA2FKN8up2cEwaJgtmAiDrZWB69ApnDfp".ToScriptHash(ProtocolSettings.Default.AddressVersion);
            var tx     = BuildTransaction(sender, sb.ToArray());
            var engine = new TestEngine(TriggerType.Application, tx, new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });
            engine.LoadScript(sb.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(1, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Neo.VM.Types.ByteString));
            Assert.AreEqual(tx.Sender, new UInt160(item.GetSpan()));
        }
Esempio n. 5
0
        public void Init()
        {
            // Deploy native contracts
            var block = new Network.P2P.Payloads.Block()
            {
                Index         = 0,
                ConsensusData = new Network.P2P.Payloads.ConsensusData(),
                Transactions  = new Network.P2P.Payloads.Transaction[0],
                Witness       = new Network.P2P.Payloads.Witness()
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = Contract.CreateSignatureRedeemScript(ECPoint.FromBytes(pubKey, ECCurve.Secp256k1))
                },
                NextConsensus = UInt160.Zero,
                MerkleRoot    = UInt256.Zero,
                PrevHash      = UInt256.Zero
            };

            _engine = new TestEngine(TriggerType.Application, block);
            ((TestSnapshot)_engine.Snapshot).SetPersistingBlock(block);

            using (var script = new ScriptBuilder())
            {
                script.EmitSysCall(TestEngine.Native_Deploy);
                _engine.LoadScript(script.ToArray());
                Assert.AreEqual(VMState.HALT, _engine.Execute());
            }

            _engine.Reset();
            _engine.AddEntryScript("./TestClasses/Contract_Native.cs");
        }
Esempio n. 6
0
        /// <summary>
        /// Execute this test
        /// </summary>
        /// <param name="ut">Test</param>
        public void ExecuteTest(VMUT ut)
        {
            foreach (var test in ut.Tests)
            {
                Assert.IsFalse(string.IsNullOrEmpty(test.Name), "Name is required");

                using (var engine = new TestEngine())
                {
                    Debugger debugger = new Debugger(engine);

                    if (test.Script.Length > 0)
                    {
                        engine.LoadScript(test.Script);
                    }

                    // Execute Steps

                    if (test.Steps != null)
                    {
                        foreach (var step in test.Steps)
                        {
                            // Actions

                            if (step.Actions != null)
                            {
                                foreach (var run in step.Actions)
                                {
                                    switch (run)
                                    {
                                    case VMUTActionType.Execute: debugger.Execute(); break;

                                    case VMUTActionType.StepInto: debugger.StepInto(); break;

                                    case VMUTActionType.StepOut: debugger.StepOut(); break;

                                    case VMUTActionType.StepOver: debugger.StepOver(); break;
                                    }
                                }
                            }

                            // Review results

                            var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name;

                            AssertResult(step.Result, engine, $"{ut.Category}-{ut.Name}-{test.Name}{add}: ");
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public void Test_InvocationCounter()
        {
            // Build script

            _engine.Reset();

            var contract = _engine.EntryScriptHash;

            _engine.Snapshot.Contracts.Add(contract, new Ledger.ContractState()
            {
                Script   = _engine.InvocationStack.Peek().Script,
                Manifest = new NEOSmartContract.Manifest.ContractManifest()
                {
                }
            });

            _engine.InvocationStack.Clear();

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                // First
                sb.EmitAppCall(contract, "getInvocationCounter");
                // Second
                sb.EmitAppCall(contract, "getInvocationCounter");

                _engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, _engine.Execute());
            Assert.AreEqual(2, _engine.ResultStack.Count);

            var item = _engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetBigInteger());

            item = _engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetBigInteger());
        }
Esempio n. 8
0
        public void Test_Init()
        {
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy);
                script = sb.ToArray();
            }

            // Fake native deploy

            typeof(TestSnapshot).GetProperty("PersistingBlock").SetValue(snapshot, new Network.P2P.Payloads.Block()
            {
                Index = 0
            });
            var testengine = new TestEngine(TriggerType.System, null, snapshot);

            testengine.LoadScript(script);
            Assert.AreEqual(VMState.HALT, testengine.Execute());
        }
Esempio n. 9
0
        public void Test_InvocationCounter()
        {
            _engine.AddEntryScript("./TestClasses/Contract_Runtime.cs");

            // We need a new TestEngine because invocationCounter it's shared between them

            var contract = _engine.EntryScriptHash;
            var engine   = new TestEngine(TriggerType.Application, new DummyVerificable(), new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });

            using (ScriptBuilder sb = new())
            {
                // First
                sb.EmitDynamicCall(contract, "getInvocationCounter");
                // Second
                sb.EmitDynamicCall(contract, "getInvocationCounter");
                engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(2, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }
Esempio n. 10
0
        public void Test_InvocationCounter()
        {
            // We need a new TestEngine because invocationCounter it's shared between them

            var contract = _engine.EntryScriptHash;
            var engine   = new TestEngine(TriggerType.Application, new DummyVerificable());

            engine.Snapshot.Contracts.Add(contract, new Ledger.ContractState()
            {
                Script   = _engine.InvocationStack.Peek().Script,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract_Runtime.cs").finalManifest)),
            });

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                // First
                sb.EmitAppCall(contract, "getInvocationCounter");
                // Second
                sb.EmitAppCall(contract, "getInvocationCounter");

                engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(2, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }