Exemplo n.º 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);

            using var script = new ScriptBuilder();
            script.EmitDynamicCall(NativeContract.Ledger.Hash, "getTransactionHeight", state.Transaction.Hash);
            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);
        }
Exemplo n.º 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   = Array.Empty <byte>(),
                        VerificationScript = Array.Empty <byte>()
                    },
                    PrevHash      = UInt256.Zero,
                    MerkleRoot    = UInt256.Zero,
                    PrimaryIndex  = 1,
                    NextConsensus = UInt160.Zero,
                },
                Hashes = new[] { tx.Hash }
            };

            var snapshot = TestBlockchain.GetTestSnapshot();

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

            // Without block

            var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheVauthSystem.Settings);

            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
            }));

            engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheVauthSystem.Settings);
            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, settings: TestBlockchain.TheVauthSystem.Settings);
            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()));
        }