Exemplo n.º 1
0
        public void tload_in_delegatecall(Instruction callType, int expectedResult)
        {
            byte[] contractCode = Prepare.EvmCode
                                  .LoadDataFromTransientStorage(1)
                                  .DataOnStackToMemory(0)
                                  .PushData(32)
                                  .PushData(0)
                                  .Op(Instruction.RETURN)
                                  .Done;

            TestState.CreateAccount(TestItem.AddressD, 1.Ether());
            Keccak contractCodeHash = TestState.UpdateCode(contractCode);

            TestState.UpdateCodeHash(TestItem.AddressD, contractCodeHash, Spec);

            byte[] code = Prepare.EvmCode
                          .StoreDataInTransientStorage(1, 7)
                          .DynamicCallWithInput(callType, TestItem.AddressD, 50000, new byte[32])
                          // Return response from nested call
                          .ReturnInnerCallResult()
                          .Done;

            TestAllTracerWithOutput result = Execute(MainnetSpecProvider.ShanghaiBlockNumber, 100000, code);

            Assert.AreEqual(expectedResult, (int)result.ReturnValue.ToUInt256());
        }
Exemplo n.º 2
0
        protected override TestAllTracerWithOutput CreateTracer()
        {
            TestAllTracerWithOutput tracer = base.CreateTracer();

            tracer.IsTracingAccess = false;
            return(tracer);
        }
Exemplo n.º 3
0
        public void Base_fee_opcode_should_return_expected_results(bool eip3198Enabled, int baseFee)
        {
            _processor = new TransactionProcessor(SpecProvider, TestState, Storage, Machine, LimboLogs.Instance);
            byte[] code = Prepare.EvmCode
                          .Op(Instruction.BASEFEE)
                          .PushData(0)
                          .Op(Instruction.SSTORE)
                          .Done;

            long blockNumber = eip3198Enabled ? LondonTestBlockNumber : LondonTestBlockNumber - 1;

            (Block block, Transaction transaction) = PrepareTx(blockNumber, 100000, code);
            block.Header.BaseFee = (UInt256)baseFee;
            transaction.FeeCap   = (UInt256)baseFee;
            TestAllTracerWithOutput tracer = CreateTracer();

            _processor.Execute(transaction, block.Header, tracer);

            if (eip3198Enabled)
            {
                AssertStorage((UInt256)0, (UInt256)baseFee);
            }
            else
            {
                tracer.Error.Should().Be(EvmExceptionType.BadInstruction.ToString());
                AssertStorage((UInt256)0, (UInt256)0);
            }
        }
Exemplo n.º 4
0
        public void after_istanbul_selfbalance_opcode_puts_current_address_balance_onto_the_stack()
        {
            byte[] contractCode = Prepare.EvmCode
                                  .Op(Instruction.SELFBALANCE)
                                  .PushData(0)
                                  .Op(Instruction.SSTORE)
                                  .Done;

            Keccak codeHash = TestState.UpdateCode(contractCode);

            TestState.CreateAccount(TestItem.AddressC, 1.Ether());
            TestState.UpdateCodeHash(TestItem.AddressC, codeHash, Spec);

            TestState.CreateAccount(TestItem.AddressD, 1.Ether());
            TestState.UpdateCodeHash(TestItem.AddressD, codeHash, Spec);

            byte[] code = Prepare.EvmCode
                          .Call(TestItem.AddressC, 50000)
                          .DelegateCall(TestItem.AddressD, 50000)
                          .Op(Instruction.SELFBALANCE)
                          .PushData(1)
                          .Op(Instruction.SSTORE)
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            Assert.AreEqual(StatusCode.Success, result.StatusCode);
            AssertGas(result, 21000 + 2 * GasCostOf.CallEip150 + 24 + 21 + GasCostOf.VeryLow + 3 * GasCostOf.SelfBalance + 3 * GasCostOf.SSet);
            UInt256 balanceB = TestState.GetBalance(TestItem.AddressB);
            UInt256 balanceC = TestState.GetBalance(TestItem.AddressC);

            AssertStorage(new StorageCell(TestItem.AddressB, UInt256.Zero), balanceB);
            AssertStorage(new StorageCell(TestItem.AddressB, UInt256.One), balanceB);
            AssertStorage(new StorageCell(TestItem.AddressC, UInt256.Zero), balanceC);
        }
Exemplo n.º 5
0
        public void tload_from_different_contract()
        {
            // TLOAD and RETURN the resulting value
            byte[] contractCode = Prepare.EvmCode
                                  .PushData(1)
                                  .Op(Instruction.TLOAD)
                                  .DataOnStackToMemory(0)
                                  .PushData(32)
                                  .PushData(0)
                                  .Op(Instruction.RETURN)
                                  .Done;

            TestState.CreateAccount(TestItem.AddressD, 1.Ether());
            Keccak contractCodeHash = TestState.UpdateCode(contractCode);

            TestState.UpdateCodeHash(TestItem.AddressD, contractCodeHash, Spec);

            // Store 8 at index 1 and call contract from above
            // Return the result received from the contract
            byte[] code = Prepare.EvmCode
                          .StoreDataInTransientStorage(1, 8)
                          .Call(TestItem.AddressD, 50000)
                          .ReturnInnerCallResult()
                          .Done;

            TestAllTracerWithOutput result = Execute(MainnetSpecProvider.ShanghaiBlockNumber, 100000, code);

            // If transient state was not isolated, the return value would be 8
            Assert.AreEqual(0, (int)result.ReturnValue.ToUInt256());
        }
Exemplo n.º 6
0
        protected TestAllTracerWithOutput Execute(params byte[] code)
        {
            (var block, var transaction) = PrepareTx(BlockNumber, 100000, code);
            TestAllTracerWithOutput tracer = new TestAllTracerWithOutput();

            _processor.Execute(transaction, block.Header, tracer);
            return(tracer);
        }
Exemplo n.º 7
0
        protected TestAllTracerWithOutput Execute(long blockNumber, long gasLimit, byte[] code)
        {
            (var block, var transaction) = PrepareTx(blockNumber, gasLimit, code);
            TestAllTracerWithOutput tracer = new TestAllTracerWithOutput();

            _processor.Execute(transaction, block.Header, tracer);
            return(tracer);
        }
Exemplo n.º 8
0
        public void Mload()
        {
            TestAllTracerWithOutput receipt = Execute(
                (byte)Instruction.PUSH1,
                64, // position
                (byte)Instruction.MLOAD);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 2 + GasCostOf.Memory * 3, receipt.GasSpent, "gas");
        }
Exemplo n.º 9
0
        public void Dup1()
        {
            TestAllTracerWithOutput receipt = Execute(
                (byte)Instruction.PUSH1,
                0,
                (byte)Instruction.DUP1);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 2, receipt.GasSpent, "gas");
        }
Exemplo n.º 10
0
        public void after_istanbul()
        {
            byte[] code = Prepare.EvmCode
                          .CallWithInput(Blake2FPrecompile.Instance.Address, 1000L, new byte[InputLength])
                          .Done;
            TestAllTracerWithOutput result = Execute(code);

            Assert.AreEqual(StatusCode.Success, result.StatusCode);
        }
Exemplo n.º 11
0
        public void Sload()
        {
            TestAllTracerWithOutput receipt = Execute(
                (byte)Instruction.PUSH1,
                0, // index
                (byte)Instruction.SLOAD);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 1 + GasCostOf.SLoadEip150, receipt.GasSpent, "gas");
        }
Exemplo n.º 12
0
        public void Test_pairing_after_istanbul()
        {
            byte[] code = Prepare.EvmCode
                          .CallWithInput(Bn256PairingPrecompile.Instance.Address, 200000L, new byte[192])
                          .Done;
            TestAllTracerWithOutput result = Execute(BlockNumber, 1000000L, code);

            Assert.AreEqual(StatusCode.Success, result.StatusCode);
            AssertGas(result, 21000 + 6 * 12 + 7 * 3 + GasCostOf.CallEip150 + 45000L + 34000L);
        }
Exemplo n.º 13
0
        public void Test_mul_after_istanbul()
        {
            byte[] code = Prepare.EvmCode
                          .CallWithInput(Bn256MulPrecompile.Instance.Address, 10000L, new byte[128])
                          .Done;
            TestAllTracerWithOutput result = Execute(code);

            Assert.AreEqual(StatusCode.Success, result.StatusCode);
            AssertGas(result, 21000 + 4 * 12 + 7 * 3 + GasCostOf.CallEip150 + 6000L);
        }
Exemplo n.º 14
0
        public void after_shanghai_can_call_tstore_tload()
        {
            byte[] code = Prepare.EvmCode
                          .StoreDataInTransientStorage(1, 8)
                          .LoadDataFromTransientStorage(1)
                          .Done;

            TestAllTracerWithOutput result = Execute(MainnetSpecProvider.ShanghaiBlockNumber, 100000, code);

            Assert.AreEqual(StatusCode.Success, result.StatusCode);
        }
Exemplo n.º 15
0
        public void Mstore()
        {
            TestAllTracerWithOutput receipt = Execute(
                (byte)Instruction.PUSH1,
                96, // data
                (byte)Instruction.PUSH1,
                64, // position
                (byte)Instruction.MSTORE);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 3 + GasCostOf.Memory * 3, receipt.GasSpent, "gas");
        }
Exemplo n.º 16
0
        public void Sign_ext_underflow()
        {
            byte[] code = Prepare.EvmCode
                          .PushData(32)
                          .Op(Instruction.SIGNEXTEND)
                          .Done;

            TestAllTracerWithOutput res = Execute(code);

            res.Error.Should().Be(EvmExceptionType.StackUnderflow.ToString());
        }
Exemplo n.º 17
0
        public void Test_when_gas_just_above_stipend(string codeHex, long gasUsed, long refund, byte originalValue)
        {
            TestState.CreateAccount(Recipient, 0);
            Storage.Set(new StorageCell(Recipient, 0), new [] { originalValue });
            Storage.Commit();
            TestState.Commit(RopstenSpecProvider.Instance.GenesisSpec);

            TestAllTracerWithOutput receipt = Execute(BlockNumber, 21000 + gasUsed + (2301 - 800), Bytes.FromHexString(codeHex));

            Assert.AreEqual(1, receipt.StatusCode);
        }
Exemplo n.º 18
0
        public void tstore_does_not_result_in_gasrefund()
        {
            byte[] code = Prepare.EvmCode
                          .StoreDataInTransientStorage(1, 7)
                          .StoreDataInTransientStorage(1, 0)
                          .Done;

            TestAllTracerWithOutput receipt = Execute(MainnetSpecProvider.ShanghaiBlockNumber, 100000, code);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 4 + GasCostOf.TStore * 2, receipt.GasSpent, "gas");
        }
Exemplo n.º 19
0
        public void Swap()
        {
            TestAllTracerWithOutput receipt = Execute(
                (byte)Instruction.PUSH1,
                32, // length
                (byte)Instruction.PUSH1,
                0,  // src
                (byte)Instruction.SWAP1);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 3, receipt.GasSpent, "gas");
        }
Exemplo n.º 20
0
        public void Test(string codeHex, long gasUsed, long refund, byte originalValue)
        {
            TestState.CreateAccount(Recipient, 0);
            Storage.Set(new StorageCell(Recipient, 0), new [] { originalValue });
            Storage.Commit();
            TestState.Commit(RopstenSpecProvider.Instance.GenesisSpec);

            TestAllTracerWithOutput receipt = Execute(Bytes.FromHexString(codeHex));

            AssertGas(receipt, gasUsed + GasCostOf.Transaction - Math.Min((gasUsed + GasCostOf.Transaction) / 2, refund));
        }
Exemplo n.º 21
0
        public void Tload()
        {
            byte[] code = Prepare.EvmCode
                          .PushData(96)
                          .Op(Instruction.TLOAD)
                          .Done;

            TestAllTracerWithOutput receipt = Execute(MainnetSpecProvider.ShanghaiBlockNumber, 100000, code);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 1 + GasCostOf.TLoad, receipt.GasSpent, "gas");
        }
Exemplo n.º 22
0
        public void Test_add_before_istanbul()
        {
            _blockNumberAdjustment = -1;
            byte[] code = Prepare.EvmCode
                          .CallWithInput(Bn256AddPrecompile.Instance.Address, 1000L, new byte[128])
                          .Done;
            TestAllTracerWithOutput result = Execute(code);

            Assert.AreEqual(StatusCode.Success, result.StatusCode);
            AssertGas(result, 21000 + 4 * 12 + 7 * 3 + GasCostOf.CallEip150 + 500);
        }
Exemplo n.º 23
0
        public void Sstore_twice_0_same_storage_should_refund_only_once()
        {
            TestAllTracerWithOutput receipt = Execute(
                (byte)Instruction.PUSH1,
                0,
                (byte)Instruction.PUSH1,
                0,
                (byte)Instruction.SSTORE);

            Assert.AreEqual(GasCostOf.Transaction + GasCostOf.VeryLow * 2 + GasCostOf.SReset, receipt.GasSpent, "gas");
            Assert.AreEqual(BigInteger.Zero.ToBigEndianByteArray(), Storage.Get(new StorageCell(Recipient, 0)), "storage");
        }
Exemplo n.º 24
0
        public void Ranges()
        {
            byte[] code = Prepare.EvmCode
                          .PushData(0)
                          .PushData("0x1e4e2")
                          .PushData("0x5050600163306e2b386347355944f3636f376163636d6b")
                          .Op(Instruction.CALLDATACOPY)
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.Error.Should().BeNull();
        }
Exemplo n.º 25
0
        public void Error_on_walk_into_the_subroutine()
        {
            TestState.CreateAccount(TestItem.AddressC, 100.Ether());

            byte[] code = Prepare.EvmCode
                          .FromCode("0x5c5d00")
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.StatusCode.Should().Be(0);
            result.Error.Should().Be(EvmExceptionType.BadInstruction.ToString());
        }
Exemplo n.º 26
0
        public void Case4()
        {
            TestState.CreateAccount(TestItem.AddressC, 100.Ether());

            byte[] code = Prepare.EvmCode
                          .FromCode("0x60008080808060046000f15060008080808060ff6000f15060008080808060ff6000fa50")
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.StatusCode.Should().Be(1);
            AssertGas(result, GasCostOf.Transaction + 2869);
        }
Exemplo n.º 27
0
        public void Case3()
        {
            TestState.CreateAccount(TestItem.AddressC, 100.Ether());

            byte[] code = Prepare.EvmCode
                          .FromCode("0x60015450601160015560116002556011600255600254600154")
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.StatusCode.Should().Be(1);
            AssertGas(result, GasCostOf.Transaction + 44529);
        }
Exemplo n.º 28
0
        public void Case1()
        {
            TestState.CreateAccount(TestItem.AddressC, 100.Ether());

            byte[] code = Prepare.EvmCode
                          .FromCode("0x60013f5060023b506003315060f13f5060f23b5060f3315060f23f5060f33b5060f1315032315030315000")
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.StatusCode.Should().Be(1);
            AssertGas(result, GasCostOf.Transaction + 8653);
        }
Exemplo n.º 29
0
        public void Shallow_return_stack()
        {
            TestState.CreateAccount(TestItem.AddressC, 100.Ether());

            byte[] code = Prepare.EvmCode
                          .FromCode("0x5d5858")
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.StatusCode.Should().Be(0);
            result.Error.Should().Be(EvmExceptionType.BadInstruction.ToString());
        }
Exemplo n.º 30
0
        public void Invalid_jump()
        {
            TestState.CreateAccount(TestItem.AddressC, 100.Ether());

            byte[] code = Prepare.EvmCode
                          .FromCode("0x6801000000000000000c5e005c60115e5d5c5d")
                          .Done;

            TestAllTracerWithOutput result = Execute(code);

            result.StatusCode.Should().Be(0);
            result.Error.Should().Be(EvmExceptionType.BadInstruction.ToString());
        }