github.com/klaytn/klaytn@v1.10.2/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)
   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)
   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  		BlockNumber: new(big.Int).SetUint64(1),
   176  		ChainConfig: &params.ChainConfig{Istanbul: params.GetDefaultIstanbulConfig(), Governance: params.GetDefaultGovernanceConfig()},
   177  		EVMConfig:   vm.Config{},
   178  	}
   179  	// Warm up the intpools and stuff
   180  	bench.ResetTimer()
   181  	for i := 0; i < bench.N; i++ {
   182  		Call(receiver, []byte{}, &runtimeConfig)
   183  	}
   184  	bench.StopTimer()
   185  }
   186  
   187  func BenchmarkEVM_CREATE_500(bench *testing.B) {
   188  	// initcode size 500K, repeatedly calls CREATE and then modifies the mem contents
   189  	benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056")
   190  }
   191  
   192  func BenchmarkEVM_CREATE2_500(bench *testing.B) {
   193  	// initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents
   194  	benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056")
   195  }
   196  
   197  func BenchmarkEVM_CREATE_1200(bench *testing.B) {
   198  	// initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents
   199  	benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056")
   200  }
   201  
   202  func BenchmarkEVM_CREATE2_1200(bench *testing.B) {
   203  	// initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents
   204  	benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056")
   205  }
   206  
   207  func fakeHeader(n uint64, parentHash common.Hash) *types.Header {
   208  	header := types.Header{
   209  		Rewardbase: common.HexToAddress("0x00000000000000000000000000000000deadbeef"),
   210  		Number:     big.NewInt(int64(n)),
   211  		ParentHash: parentHash,
   212  		Time:       big.NewInt(1000),
   213  		BlockScore: big.NewInt(0),
   214  		Extra:      []byte{},
   215  	}
   216  	return &header
   217  }
   218  
   219  type dummyChain struct {
   220  	counter int
   221  }
   222  
   223  // Engine retrieves the chain's consensus engine.
   224  func (d *dummyChain) Engine() consensus.Engine {
   225  	return nil
   226  }
   227  
   228  // GetHeader returns the hash corresponding to their hash.
   229  func (d *dummyChain) GetHeader(h common.Hash, n uint64) *types.Header {
   230  	d.counter++
   231  	parentHash := common.Hash{}
   232  	s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32)
   233  	copy(parentHash[:], s)
   234  
   235  	// parentHash := common.Hash{byte(n - 1)}
   236  	// fmt.Printf("GetHeader(%x, %d) => header with parent %x\n", h, n, parentHash)
   237  	return fakeHeader(n, parentHash)
   238  }
   239  
   240  // TestBlockhash tests the blockhash operation. It's a bit special, since it internally
   241  // requires access to a chain reader.
   242  func TestBlockhash(t *testing.T) {
   243  	// Current head
   244  	n := uint64(1000)
   245  	parentHash := common.Hash{}
   246  	s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32)
   247  	copy(parentHash[:], s)
   248  	header := fakeHeader(n, parentHash)
   249  
   250  	// This is the contract we're using. It requests the blockhash for current num (should be all zeroes),
   251  	// then iteratively fetches all blockhashes back to n-260.
   252  	// It returns
   253  	// 1. the first (should be zero)
   254  	// 2. the second (should be the parent hash)
   255  	// 3. the last non-zero hash
   256  	// By making the chain reader return hashes which correlate to the number, we can
   257  	// verify that it obtained the right hashes where it should
   258  
   259  	/*
   260  		pragma solidity ^0.5.3;
   261  		contract Hasher{
   262  			function test() public view returns (bytes32, bytes32, bytes32){
   263  				uint256 x = block.number;
   264  				bytes32 first;
   265  				bytes32 last;
   266  				bytes32 zero;
   267  				zero = blockhash(x); // Should be zeroes
   268  				first = blockhash(x-1);
   269  				for(uint256 i = 2 ; i < 260; i++){
   270  					bytes32 hash = blockhash(x - i);
   271  					if (uint256(hash) != 0){
   272  						last = hash;
   273  					}
   274  				}
   275  				return (zero, first, last);
   276  			}
   277  		}
   278  	*/
   279  	// The contract above
   280  	data := common.Hex2Bytes("6080604052348015600f57600080fd5b50600436106045576000357c010000000000000000000000000000000000000000000000000000000090048063f8a8fd6d14604a575b600080fd5b60506074565b60405180848152602001838152602001828152602001935050505060405180910390f35b600080600080439050600080600083409050600184034092506000600290505b61010481101560c35760008186034090506000816001900414151560b6578093505b5080806001019150506094565b508083839650965096505050505090919256fea165627a7a72305820462d71b510c1725ff35946c20b415b0d50b468ea157c8c77dff9466c9cb85f560029")
   281  	// The method call to 'test()'
   282  	input := common.Hex2Bytes("f8a8fd6d")
   283  	chain := &dummyChain{}
   284  	ret, _, err := Execute(data, input, &Config{
   285  		GetHashFn:   blockchain.GetHashFn(header, chain),
   286  		BlockNumber: new(big.Int).Set(header.Number),
   287  	})
   288  	if err != nil {
   289  		t.Fatalf("expected no error, got %v", err)
   290  	}
   291  	if len(ret) != 96 {
   292  		t.Fatalf("expected returndata to be 96 bytes, got %d", len(ret))
   293  	}
   294  
   295  	zero := new(big.Int).SetBytes(ret[0:32])
   296  	first := new(big.Int).SetBytes(ret[32:64])
   297  	last := new(big.Int).SetBytes(ret[64:96])
   298  	if zero.BitLen() != 0 {
   299  		t.Fatalf("expected zeroes, got %x", ret[0:32])
   300  	}
   301  	if first.Uint64() != 999 {
   302  		t.Fatalf("second block should be 999, got %d (%x)", first, ret[32:64])
   303  	}
   304  	if last.Uint64() != 744 {
   305  		t.Fatalf("last block should be 744, got %d (%x)", last, ret[64:96])
   306  	}
   307  	if exp, got := 255, chain.counter; exp != got {
   308  		t.Errorf("suboptimal; too much chain iteration, expected %d, got %d", exp, got)
   309  	}
   310  }
   311  
   312  // TestEip2929Cases contains various testcases that are used for
   313  // EIP-2929 about gas repricings
   314  func TestEip2929Cases(t *testing.T) {
   315  	id := 1
   316  	prettyPrint := func(comment string, code []byte) {
   317  		instrs := make([]string, 0)
   318  		it := asm.NewInstructionIterator(code)
   319  		for it.Next() {
   320  			if it.Arg() != nil && 0 < len(it.Arg()) {
   321  				instrs = append(instrs, fmt.Sprintf("%v 0x%x", it.Op(), it.Arg()))
   322  			} else {
   323  				instrs = append(instrs, fmt.Sprintf("%v", it.Op()))
   324  			}
   325  		}
   326  		ops := strings.Join(instrs, ", ")
   327  		fmt.Printf("### Case %d\n\n", id)
   328  		id++
   329  		fmt.Printf("%v\n\nBytecode: \n```\n0x%x\n```\nOperations: \n```\n%v\n```\n\n",
   330  			comment,
   331  			code, ops)
   332  		Execute(code, nil, &Config{
   333  			EVMConfig: vm.Config{
   334  				Debug:     true,
   335  				Tracer:    vm.NewStructLogger(nil),
   336  				ExtraEips: []int{2929},
   337  			},
   338  		})
   339  	}
   340  	{ // First eip testcase
   341  		code := []byte{
   342  			// Three checks against a precompile
   343  			byte(vm.PUSH1), 1, byte(vm.EXTCODEHASH), byte(vm.POP),
   344  			byte(vm.PUSH1), 2, byte(vm.EXTCODESIZE), byte(vm.POP),
   345  			byte(vm.PUSH1), 3, byte(vm.BALANCE), byte(vm.POP),
   346  			// Three checks against a non-precompile
   347  			byte(vm.PUSH1), 0xf1, byte(vm.EXTCODEHASH), byte(vm.POP),
   348  			byte(vm.PUSH1), 0xf2, byte(vm.EXTCODESIZE), byte(vm.POP),
   349  			byte(vm.PUSH1), 0xf3, byte(vm.BALANCE), byte(vm.POP),
   350  			// Same three checks (should be cheaper)
   351  			byte(vm.PUSH1), 0xf2, byte(vm.EXTCODEHASH), byte(vm.POP),
   352  			byte(vm.PUSH1), 0xf3, byte(vm.EXTCODESIZE), byte(vm.POP),
   353  			byte(vm.PUSH1), 0xf1, byte(vm.BALANCE), byte(vm.POP),
   354  			// Check the origin, and the 'this'
   355  			byte(vm.ORIGIN), byte(vm.BALANCE), byte(vm.POP),
   356  			byte(vm.ADDRESS), byte(vm.BALANCE), byte(vm.POP),
   357  			byte(vm.STOP),
   358  		}
   359  		prettyPrint("This checks `EXT`(codehash,codesize,balance) of precompiles, which should be `100`, "+
   360  			"and later checks the same operations twice against some non-precompiles. "+
   361  			"Those are cheaper second time they are accessed. Lastly, it checks the `BALANCE` of `origin` and `this`.", code)
   362  	}
   363  	{ // EXTCODECOPY
   364  		code := []byte{
   365  			// extcodecopy( 0xff,0,0,0,0)
   366  			byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
   367  			byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY),
   368  			// extcodecopy( 0xff,0,0,0,0)
   369  			byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
   370  			byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY),
   371  			// extcodecopy( this,0,0,0,0)
   372  			byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
   373  			byte(vm.ADDRESS), byte(vm.EXTCODECOPY),
   374  			byte(vm.STOP),
   375  		}
   376  		prettyPrint("This checks `extcodecopy( 0xff,0,0,0,0)` twice, (should be expensive first time), "+
   377  			"and then does `extcodecopy( this,0,0,0,0)`.", code)
   378  	}
   379  	{ // SLOAD + SSTORE
   380  		code := []byte{
   381  			// Add slot `0x1` to access list
   382  			byte(vm.PUSH1), 0x01, byte(vm.SLOAD), byte(vm.POP), // SLOAD( 0x1) (add to access list)
   383  			// Write to `0x1` which is already in access list
   384  			byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x01, byte(vm.SSTORE), // SSTORE( loc: 0x01, val: 0x11)
   385  			// Write to `0x2` which is not in access list
   386  			byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11)
   387  			// Write again to `0x2`
   388  			byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11)
   389  			// Read slot in access list (0x2)
   390  			byte(vm.PUSH1), 0x02, byte(vm.SLOAD), // SLOAD( 0x2)
   391  			// Read slot in access list (0x1)
   392  			byte(vm.PUSH1), 0x01, byte(vm.SLOAD), // SLOAD( 0x1)
   393  		}
   394  		prettyPrint("This checks `sload( 0x1)` followed by `sstore(loc: 0x01, val:0x11)`, then 'naked' sstore:"+
   395  			"`sstore(loc: 0x02, val:0x11)` twice, and `sload(0x2)`, `sload(0x1)`. ", code)
   396  	}
   397  	{ // Call variants
   398  		code := []byte{
   399  			// identity precompile
   400  			byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   401  			byte(vm.PUSH1), 0x04, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP),
   402  			// random account - call 1
   403  			byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   404  			byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP),
   405  			// random account - call 2
   406  			byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   407  			byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.STATICCALL), byte(vm.POP),
   408  		}
   409  		prettyPrint("This calls the `identity`-precompile (cheap), then calls an account (expensive) and `staticcall`s the same"+
   410  			"account (cheap)", code)
   411  	}
   412  }
   413  
   414  // TestColdAccountAccessCost test that the cold account access cost is reported
   415  // correctly
   416  // see: https://github.com/ethereum/go-ethereum/issues/22649
   417  func TestColdAccountAccessCost(t *testing.T) {
   418  	for i, tc := range []struct {
   419  		code []byte
   420  		step int
   421  		want uint64
   422  	}{
   423  		{ // EXTCODEHASH(0xff)
   424  			code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.EXTCODEHASH), byte(vm.POP)},
   425  			step: 1,
   426  			want: 2600,
   427  		},
   428  		{ // BALANCE(0xff)
   429  			code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.BALANCE), byte(vm.POP)},
   430  			step: 1,
   431  			want: 2600,
   432  		},
   433  		{ // CALL(0xff)
   434  			code: []byte{
   435  				byte(vm.PUSH1), 0x0,
   436  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   437  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALL), byte(vm.POP),
   438  			},
   439  			step: 7,
   440  			want: 2855,
   441  		},
   442  		{ // CALLCODE(0xff)
   443  			code: []byte{
   444  				byte(vm.PUSH1), 0x0,
   445  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   446  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALLCODE), byte(vm.POP),
   447  			},
   448  			step: 7,
   449  			want: 2855,
   450  		},
   451  		{ // DELEGATECALL(0xff)
   452  			code: []byte{
   453  				byte(vm.PUSH1), 0x0,
   454  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   455  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.DELEGATECALL), byte(vm.POP),
   456  			},
   457  			step: 6,
   458  			want: 2855,
   459  		},
   460  		{ // STATICCALL(0xff)
   461  			code: []byte{
   462  				byte(vm.PUSH1), 0x0,
   463  				byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
   464  				byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.STATICCALL), byte(vm.POP),
   465  			},
   466  			step: 6,
   467  			want: 2855,
   468  		},
   469  		{ // SELFDESTRUCT(0xff)
   470  			code: []byte{
   471  				byte(vm.PUSH1), 0xff, byte(vm.SELFDESTRUCT),
   472  			},
   473  			step: 1,
   474  			want: 7600,
   475  		},
   476  	} {
   477  		tracer := vm.NewStructLogger(nil)
   478  		Execute(tc.code, nil, &Config{
   479  			EVMConfig: vm.Config{
   480  				Debug:  true,
   481  				Tracer: tracer,
   482  			},
   483  		})
   484  		have := tracer.StructLogs()[tc.step].GasCost
   485  		if want := tc.want; have != want {
   486  			for ii, op := range tracer.StructLogs() {
   487  				t.Logf("%d: %v %d", ii, op.OpName(), op.GasCost)
   488  			}
   489  			t.Fatalf("tescase %d, gas report wrong, step %d, have %d want %d", i, tc.step, have, want)
   490  		}
   491  	}
   492  }