Exemple #1
0
        public void TestBlockchain_GetTransactionHeight()
        {
            var engine = GetEngine(hasSnapshot: true, addScript: false);
            var state  = new TransactionState()
            {
                BlockIndex  = 0,
                Transaction = TestUtils.CreateRandomHashTransaction()
            };

            UT_SmartContractHelper.TransactionAdd(engine.Snapshot, state);
            engine.LoadScript(NativeContract.Ledger.Script, configureState: p => p.ScriptHash = NativeContract.Ledger.Hash);

            var script = new ScriptBuilder();

            script.EmitPush(state.Transaction.Hash.ToArray());
            script.EmitPush("getTransactionHeight");
            engine.LoadScript(script.ToArray());
            engine.Execute();
            Assert.AreEqual(engine.State, VMState.HALT);

            var result = engine.ResultStack.Pop();

            result.Should().BeOfType(typeof(VM.Types.Integer));
            result.GetInteger().Should().Be(0);
        }
Exemple #2
0
        public void System_Blockchain_GetBlock()
        {
            var tx = new Transaction()
            {
                Script          = new byte[] { 0x01 },
                Attributes      = Array.Empty <TransactionAttribute>(),
                Signers         = Array.Empty <Signer>(),
                NetworkFee      = 0x02,
                SystemFee       = 0x03,
                Nonce           = 0x04,
                ValidUntilBlock = 0x05,
                Version         = 0x06,
                Witnesses       = new Witness[] { new Witness()
                                                  {
                                                      VerificationScript = new byte[] { 0x07 }
                                                  } },
            };

            var block = new TrimmedBlock()
            {
                Header = new Header
                {
                    Index     = 0,
                    Timestamp = 2,
                    Witness   = new Witness()
                    {
                        InvocationScript   = new byte[0],
                        VerificationScript = new byte[0]
                    },
                    PrevHash      = UInt256.Zero,
                    MerkleRoot    = UInt256.Zero,
                    PrimaryIndex  = 1,
                    NextConsensus = UInt160.Zero,
                },
                Hashes = new[] { tx.Hash }
            };

            var snapshot = TestBlockchain.GetTestSnapshot();

            using (var script = new ScriptBuilder())
            {
                script.EmitDynamicCall(NativeContract.Ledger.Hash, "getBlock", block.Hash.ToArray());

                // Without block

                var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
                engine.LoadScript(script.ToArray());

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

                // Not traceable block

                const byte Prefix_Transaction  = 11;
                const byte Prefix_CurrentBlock = 12;

                var height = snapshot[NativeContract.Ledger.CreateStorageKey(Prefix_CurrentBlock)].GetInteroperable <HashIndexState>();
                height.Index = block.Index + ProtocolSettings.Default.MaxTraceableBlocks;

                UT_SmartContractHelper.BlocksAdd(snapshot, block.Hash, block);
                snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Transaction, tx.Hash), new StorageItem(new TransactionState
                {
                    BlockIndex  = block.Index,
                    Transaction = tx
                }, true));

                engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
                engine.LoadScript(script.ToArray());

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

                // With block

                height.Index = block.Index;

                engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
                engine.LoadScript(script.ToArray());

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

                var array = engine.ResultStack.Pop <VM.Types.Array>();
                Assert.AreEqual(block.Hash, new UInt256(array[0].GetSpan()));
            }
        }