github.com/klaytn/klaytn@v1.12.1/blockchain/vm/runtime/runtime_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/runtime/runtime_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package runtime
    22  
    23  import (
    24  	"fmt"
    25  	"math/big"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/klaytn/klaytn/accounts/abi"
    30  	"github.com/klaytn/klaytn/blockchain"
    31  	"github.com/klaytn/klaytn/blockchain/asm"
    32  	"github.com/klaytn/klaytn/blockchain/state"
    33  	"github.com/klaytn/klaytn/blockchain/types"
    34  	"github.com/klaytn/klaytn/blockchain/vm"
    35  	"github.com/klaytn/klaytn/common"
    36  	"github.com/klaytn/klaytn/consensus"
    37  	"github.com/klaytn/klaytn/params"
    38  	"github.com/klaytn/klaytn/storage/database"
    39  )
    40  
    41  func TestDefaults(t *testing.T) {
    42  	cfg := new(Config)
    43  	setDefaults(cfg)
    44  
    45  	if cfg.BlockScore == nil {
    46  		t.Error("expected blockscore to be non nil")
    47  	}
    48  
    49  	if cfg.Time == nil {
    50  		t.Error("expected time to be non nil")
    51  	}
    52  	if cfg.GasPrice == nil {
    53  		t.Error("expected time to be non nil")
    54  	}
    55  	if cfg.Value == nil {
    56  		t.Error("expected time to be non nil")
    57  	}
    58  	if cfg.GetHashFn == nil {
    59  		t.Error("expected time to be non nil")
    60  	}
    61  	if cfg.BlockNumber == nil {
    62  		t.Error("expected block number to be non nil")
    63  	}
    64  }
    65  
    66  func TestEVM(t *testing.T) {
    67  	defer func() {
    68  		if r := recover(); r != nil {
    69  			t.Fatalf("crashed with: %v", r)
    70  		}
    71  	}()
    72  
    73  	Execute([]byte{
    74  		byte(vm.DIFFICULTY),
    75  		byte(vm.TIMESTAMP),
    76  		byte(vm.GASLIMIT),
    77  		byte(vm.PUSH1),
    78  		byte(vm.ORIGIN),
    79  		byte(vm.BLOCKHASH),
    80  		byte(vm.COINBASE),
    81  		byte(vm.PREVRANDAO),
    82  	}, nil, nil)
    83  }
    84  
    85  func TestExecute(t *testing.T) {
    86  	ret, _, err := Execute([]byte{
    87  		byte(vm.PUSH1), 10,
    88  		byte(vm.PUSH1), 0,
    89  		byte(vm.MSTORE),
    90  		byte(vm.PUSH1), 32,
    91  		byte(vm.PUSH1), 0,
    92  		byte(vm.RETURN),
    93  	}, nil, nil)
    94  	if err != nil {
    95  		t.Fatal("didn't expect error", err)
    96  	}
    97  
    98  	num := new(big.Int).SetBytes(ret)
    99  	if num.Cmp(big.NewInt(10)) != 0 {
   100  		t.Error("Expected 10, got", num)
   101  	}
   102  }
   103  
   104  func TestCall(t *testing.T) {
   105  	state, _ := state.New(common.Hash{}, state.NewDatabase(database.NewMemoryDBManager()), nil, nil)
   106  	address := common.HexToAddress("0x0a00")
   107  	state.SetCode(address, []byte{
   108  		byte(vm.PUSH1), 10,
   109  		byte(vm.PUSH1), 0,
   110  		byte(vm.MSTORE),
   111  		byte(vm.PUSH1), 32,
   112  		byte(vm.PUSH1), 0,
   113  		byte(vm.RETURN),
   114  	})
   115  
   116  	ret, _, err := Call(address, nil, &Config{State: state})
   117  	if err != nil {
   118  		t.Fatal("didn't expect error", err)
   119  	}
   120  
   121  	num := new(big.Int).SetBytes(ret)
   122  	if num.Cmp(big.NewInt(10)) != 0 {
   123  		t.Error("Expected 10, got", num)
   124  	}
   125  }
   126  
   127  func BenchmarkCall(b *testing.B) {
   128  	definition := `[{"constant":true,"inputs":[],"name":"seller","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"abort","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmReceived","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"state","outputs":[{"name":"","type":"uint8"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmPurchase","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[],"name":"Aborted","type":"event"},{"anonymous":false,"inputs":[],"name":"PurchaseConfirmed","type":"event"},{"anonymous":false,"inputs":[],"name":"ItemReceived","type":"event"},{"anonymous":false,"inputs":[],"name":"Refunded","type":"event"}]`
   129  
   130  	code := common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056")
   131  
   132  	abi, err := abi.JSON(strings.NewReader(definition))
   133  	if err != nil {
   134  		b.Fatal(err)
   135  	}
   136  
   137  	cpurchase, err := abi.Pack("confirmPurchase")
   138  	if err != nil {
   139  		b.Fatal(err)
   140  	}
   141  	creceived, err := abi.Pack("confirmReceived")
   142  	if err != nil {
   143  		b.Fatal(err)
   144  	}
   145  	refund, err := abi.Pack("refund")
   146  	if err != nil {
   147  		b.Fatal(err)
   148  	}
   149  
   150  	b.ResetTimer()
   151  	for i := 0; i < b.N; i++ {
   152  		for j := 0; j < 400; j++ {
   153  			Execute(code, cpurchase, nil)
   154  			Execute(code, creceived, nil)
   155  			Execute(code, refund, nil)
   156  		}
   157  	}
   158  }
   159  
   160  func benchmarkEVM_Create(bench *testing.B, code string) {
   161  	var (
   162  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(database.NewMemoryDBManager()), nil, nil)
   163  		sender     = common.BytesToAddress([]byte("sender"))
   164  		receiver   = common.BytesToAddress([]byte("receiver"))
   165  	)
   166  
   167  	statedb.CreateAccount(sender)
   168  	statedb.SetCode(receiver, common.FromHex(code))
   169  	runtimeConfig := Config{
   170  		Origin:      sender,
   171  		State:       statedb,
   172  		GasLimit:    10000000,
   173  		Time:        new(big.Int).SetUint64(0),
   174  		Coinbase:    common.Address{},
   175  		Rewardbase:  common.Address{},
   176  		BlockNumber: new(big.Int).SetUint64(1),
   177  		ChainConfig: &params.ChainConfig{Istanbul: params.GetDefaultIstanbulConfig(), Governance: params.GetDefaultGovernanceConfig()},
   178  		EVMConfig:   vm.Config{},
   179  	}
   180  	// Warm up the intpools and stuff
   181  	bench.ResetTimer()
   182  	for i := 0; i < bench.N; i++ {
   183  		Call(receiver, []byte{}, &runtimeConfig)
   184  	}
   185  	bench.StopTimer()
   186  }
   187  
   188  func BenchmarkEVM_CREATE_500(bench *testing.B) {
   189  	// initcode size 500K, repeatedly calls CREATE and then modifies the mem contents
   190  	benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056")
   191  }
   192  
   193  func BenchmarkEVM_CREATE2_500(bench *testing.B) {
   194  	// initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents
   195  	benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056")
   196  }
   197  
   198  func BenchmarkEVM_CREATE_1200(bench *testing.B) {
   199  	// initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents
   200  	benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056")
   201  }
   202  
   203  func BenchmarkEVM_CREATE2_1200(bench *testing.B) {
   204  	// initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents
   205  	benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056")
   206  }
   207  
   208  func fakeHeader(n uint64, parentHash common.Hash) *types.Header {
   209  	header := types.Header{
   210  		Rewardbase: common.HexToAddress("0x00000000000000000000000000000000deadbeef"),
   211  		Number:     big.NewInt(int64(n)),
   212  		ParentHash: parentHash,
   213  		Time:       big.NewInt(1000),
   214  		BlockScore: big.NewInt(0),
   215  		Extra:      []byte{},
   216  	}
   217  	return &header
   218  }
   219  
   220  type dummyChain struct {
   221  	counter int
   222  }
   223  
   224  // Engine retrieves the chain's consensus engine.
   225  func (d *dummyChain) Engine() consensus.Engine {
   226  	return nil
   227  }
   228  
   229  // GetHeader returns the hash corresponding to their hash.
   230  func (d *dummyChain) GetHeader(h common.Hash, n uint64) *types.Header {
   231  	d.counter++
   232  	parentHash := common.Hash{}
   233  	s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32)
   234  	copy(parentHash[:], s)
   235  
   236  	// parentHash := common.Hash{byte(n - 1)}
   237  	// fmt.Printf("GetHeader(%x, %d) => header with parent %x\n", h, n, parentHash)
   238  	return fakeHeader(n, parentHash)
   239  }
   240  
   241  // TestBlockhash tests the blockhash operation. It's a bit special, since it internally
   242  // requires access to a chain reader.
   243  func TestBlockhash(t *testing.T) {
   244  	// Current head
   245  	n := uint64(1000)
   246  	parentHash := common.Hash{}
   247  	s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32)
   248  	copy(parentHash[:], s)
   249  	header := fakeHeader(n, parentHash)
   250  
   251  	// This is the contract we're using. It requests the blockhash for current num (should be all zeroes),
   252  	// then iteratively fetches all blockhashes back to n-260.
   253  	// It returns
   254  	// 1. the first (should be zero)
   255  	// 2. the second (should be the parent hash)
   256  	// 3. the last non-zero hash
   257  	// By making the chain reader return hashes which correlate to the number, we can
   258  	// verify that it obtained the right hashes where it should
   259  
   260  	/*
   261  		pragma solidity ^0.5.3;
   262  		contract Hasher{
   263  			function test() public view returns (bytes32, bytes32, bytes32){
   264  				uint256 x = block.number;
   265  				bytes32 first;
   266  				bytes32 last;
   267  				bytes32 zero;
   268  				zero = blockhash(x); // Should be zeroes
   269  				first = blockhash(x-1);
   270  				for(uint256 i = 2 ; i < 260; i++){
   271  					bytes32 hash = blockhash(x - i);
   272  					if (uint256(hash) != 0){
   273  						last = hash;
   274  					}
   275  				}
   276  				return (zero, first, last);
   277  			}
   278  		}
   279  	*/
   280  	// The contract above
   281  	data := common.Hex2Bytes("6080604052348015600f57600080fd5b50600436106045576000357c010000000000000000000000000000000000000000000000000000000090048063f8a8fd6d14604a575b600080fd5b60506074565b60405180848152602001838152602001828152602001935050505060405180910390f35b600080600080439050600080600083409050600184034092506000600290505b61010481101560c35760008186034090506000816001900414151560b6578093505b5080806001019150506094565b508083839650965096505050505090919256fea165627a7a72305820462d71b510c1725ff35946c20b415b0d50b468ea157c8c77dff9466c9cb85f560029")
   282  	// The method call to 'test()'
   283  	input := common.Hex2Bytes("f8a8fd6d")
   284  	chain := &dummyChain{}
   285  	ret, _, err := Execute(data, input, &Config{
   286  		GetHashFn:   blockchain.GetHashFn(header, chain),
   287  		BlockNumber: new(big.Int).Set(header.Number),
   288  	})
   289  	if err != nil {
   290  		t.Fatalf("expected no error, got %v", err)
   291  	}
   292  	if len(ret) != 96 {
   293  		t.Fatalf("expected returndata to be 96 bytes, got %d", len(ret))
   294  	}
   295  
   296  	zero := new(big.Int).SetBytes(ret[0:32])
   297  	first := new(big.Int).SetBytes(ret[32:64])
   298  	last := new(big.Int).SetBytes(ret[64:96])
   299  	if zero.BitLen() != 0 {
   300  		t.Fatalf("expected zeroes, got %x", ret[0:32])
   301  	}
   302  	if first.Uint64() != 999 {
   303  		t.Fatalf("second block should be 999, got %d (%x)", first, ret[32:64])
   304  	}
   305  	if last.Uint64() != 744 {
   306  		t.Fatalf("last block should be 744, got %d (%x)", last, ret[64:96])
   307  	}
   308  	if exp, got := 255, chain.counter; exp != got {
   309  		t.Errorf("suboptimal; too much chain iteration, expected %d, got %d", exp, got)
   310  	}
   311  }
   312  
   313  // TestEip2929Cases contains various testcases that are used for
   314  // EIP-2929 about gas repricings
   315  func TestEip2929Cases(t *testing.T) {
   316  	id := 1
   317  	prettyPrint := func(comment string, code []byte) {
   318  		instrs := make([]string, 0)
   319  		it := asm.NewInstructionIterator(code)
   320  		for it.Next() {
   321  			if it.Arg() != nil && 0 < len(it.Arg()) {
   322  				instrs = append(instrs, fmt.Sprintf("%v 0x%x", it.Op(), it.Arg()))
   323  			} else {
   324  				instrs = append(instrs, fmt.Sprintf("%v", it.Op()))
   325  			}
   326  		}
   327  		ops := strings.Join(instrs, ", ")
   328  		fmt.Printf("### Case %d\n\n", id)
   329  		id++
   330  		fmt.Printf("%v\n\nBytecode: \n```\n0x%x\n```\nOperations: \n```\n%v\n```\n\n",
   331  			comment,
   332  			code, ops)
   333  		Execute(code, nil, &Config{
   334  			EVMConfig: vm.Config{
   335  				Debug:     true,
   336  				Tracer:    vm.NewStructLogger(nil),
   337  				ExtraEips: []int{2929},
   338  			},
   339  		})
   340  	}
   341  	{ // First eip testcase
   342  		code := []byte{
   343  			// Three checks against a precompile
   344  			byte(vm.PUSH1), 1, byte(vm.EXTCODEHASH), byte(vm.POP),
   345  			byte(vm.PUSH1), 2, byte(vm.EXTCODESIZE), byte(vm.POP),
   346  			byte(vm.PUSH1), 3, byte(vm.BALANCE), byte(vm.POP),
   347  			// Three checks against a non-precompile
   348  			byte(vm.PUSH1), 0xf1, byte(vm.EXTCODEHASH), byte(vm.POP),
   349  			byte(vm.PUSH1), 0xf2, byte(vm.EXTCODESIZE), byte(vm.POP),
   350  			byte(vm.PUSH1), 0xf3, byte(vm.BALANCE), byte(vm.POP),
   351  			// Same three checks (should be cheaper)
   352  			byte(vm.PUSH1), 0xf2, byte(vm.EXTCODEHASH), byte(vm.POP),
   353  			byte(vm.PUSH1), 0xf3, byte(vm.EXTCODESIZE), byte(vm.POP),
   354  			byte(vm.PUSH1), 0xf1, byte(vm.BALANCE), byte(vm.POP),
   355  			// Check the origin, and the 'this'
   356  			byte(vm.ORIGIN), byte(vm.BALANCE), byte(vm.POP),
   357  			byte(vm.ADDRESS), byte(vm.BALANCE), byte(vm.POP),
   358  			byte(vm.STOP),
   359  		}
   360  		prettyPrint("This checks `EXT`(codehash,codesize,balance) of precompiles, which should be `100`, "+
   361  			"and later checks the same operations twice against some non-precompiles. "+
   362  			"Those are cheaper second time they are accessed. Lastly, it checks the `BALANCE` of `origin` and `this`.", code)
   363  	}
   364  	{ // EXTCODECOPY
   365  		code := []byte{
   366  			// extcodecopy( 0xff,0,0,0,0)
   367  			byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
   368  			byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY),
   369  			// extcodecopy( 0xff,0,0,0,0)
   370  			byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
   371  			byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY),
   372  			// extcodecopy( this,0,0,0,0)
   373  			byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
   374  			byte(vm.ADDRESS), byte(vm.EXTCODECOPY),
   375  			byte(vm.STOP),
   376  		}
   377  		prettyPrint("This checks `extcodecopy( 0xff,0,0,0,0)` twice, (should be expensive first time), "+
   378  			"and then does `extcodecopy( this,0,0,0,0)`.", code)
   379  	}
   380  	{ // SLOAD + SSTORE
   381  		code := []byte{
   382  			// Add slot `0x1` to access list
   383  			byte(vm.PUSH1), 0x01, byte(vm.SLOAD), byte(vm.POP), // SLOAD( 0x1) (add to access list)
   384  			// Write to `0x1` which is already in access list
   385  			byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x01, byte(vm.SSTORE), // SSTORE( loc: 0x01, val: 0x11)
   386  			// Write to `0x2` which is not in access list
   387  			byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11)
   388  			// Write again to `0x2`
   389  			byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11)
   390  			// Read slot in access list (0x2)
   391  			byte(vm.PUSH1), 0x02, byte(vm.SLOAD), // SLOAD( 0x2)
   392  			// Read slot in access list (0x1)
   393  			byte(vm.PUSH1), 0x01, byte(vm.SLOAD), // SLOAD( 0x1)
   394  		}
   395  		prettyPrint("This checks `sload( 0x1)` followed by `sstore(loc: 0x01, val:0x11)`, then 'naked' sstore:"+
   396  			"`sstore(loc: 0x02, val:0x11)` twice, and `sload(0x2)`, `sload(0x1)`. ", code)
   397  	}
   398  	{ // Call variants
   399  		code := []byte{
   400  			// identity precompile
   401  			byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   402  			byte(vm.PUSH1), 0x04, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP),
   403  			// random account - call 1
   404  			byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   405  			byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP),
   406  			// random account - call 2
   407  			byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   408  			byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.STATICCALL), byte(vm.POP),
   409  		}
   410  		prettyPrint("This calls the `identity`-precompile (cheap), then calls an account (expensive) and `staticcall`s the same"+
   411  			"account (cheap)", code)
   412  	}
   413  }
   414  
   415  // TestColdAccountAccessCost test that the cold account access cost is reported
   416  // correctly
   417  // see: https://github.com/ethereum/go-ethereum/issues/22649
   418  func TestColdAccountAccessCost(t *testing.T) {
   419  	for i, tc := range []struct {
   420  		code []byte
   421  		step int
   422  		want uint64
   423  	}{
   424  		{ // EXTCODEHASH(0xff)
   425  			code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.EXTCODEHASH), byte(vm.POP)},
   426  			step: 1,
   427  			want: 2600,
   428  		},
   429  		{ // BALANCE(0xff)
   430  			code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.BALANCE), byte(vm.POP)},
   431  			step: 1,
   432  			want: 2600,
   433  		},
   434  		{ // CALL(0xff)
   435  			code: []byte{
   436  				byte(vm.PUSH1), 0x0,
   437  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   438  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALL), byte(vm.POP),
   439  			},
   440  			step: 7,
   441  			want: 2855,
   442  		},
   443  		{ // CALLCODE(0xff)
   444  			code: []byte{
   445  				byte(vm.PUSH1), 0x0,
   446  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   447  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALLCODE), byte(vm.POP),
   448  			},
   449  			step: 7,
   450  			want: 2855,
   451  		},
   452  		{ // DELEGATECALL(0xff)
   453  			code: []byte{
   454  				byte(vm.PUSH1), 0x0,
   455  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   456  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.DELEGATECALL), byte(vm.POP),
   457  			},
   458  			step: 6,
   459  			want: 2855,
   460  		},
   461  		{ // STATICCALL(0xff)
   462  			code: []byte{
   463  				byte(vm.PUSH1), 0x0,
   464  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   465  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.STATICCALL), byte(vm.POP),
   466  			},
   467  			step: 6,
   468  			want: 2855,
   469  		},
   470  		{ // SELFDESTRUCT(0xff)
   471  			code: []byte{
   472  				byte(vm.PUSH1), 0xff, byte(vm.SELFDESTRUCT),
   473  			},
   474  			step: 1,
   475  			want: 7600,
   476  		},
   477  	} {
   478  		tracer := vm.NewStructLogger(nil)
   479  		Execute(tc.code, nil, &Config{
   480  			EVMConfig: vm.Config{
   481  				Debug:  true,
   482  				Tracer: tracer,
   483  			},
   484  		})
   485  		have := tracer.StructLogs()[tc.step].GasCost
   486  		if want := tc.want; have != want {
   487  			for ii, op := range tracer.StructLogs() {
   488  				t.Logf("%d: %v %d", ii, op.OpName(), op.GasCost)
   489  			}
   490  			t.Fatalf("tescase %d, gas report wrong, step %d, have %d want %d", i, tc.step, have, want)
   491  		}
   492  	}
   493  }