github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/core/vm/gas_table_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"bytes"
    21  	"math"
    22  	"math/big"
    23  	"sort"
    24  	"testing"
    25  
    26  	"github.com/tacshi/go-ethereum/common"
    27  	"github.com/tacshi/go-ethereum/common/hexutil"
    28  	"github.com/tacshi/go-ethereum/core/rawdb"
    29  	"github.com/tacshi/go-ethereum/core/state"
    30  	"github.com/tacshi/go-ethereum/params"
    31  )
    32  
    33  func TestMemoryGasCost(t *testing.T) {
    34  	tests := []struct {
    35  		size     uint64
    36  		cost     uint64
    37  		overflow bool
    38  	}{
    39  		{0x1fffffffe0, 36028809887088637, false},
    40  		{0x1fffffffe1, 0, true},
    41  	}
    42  	for i, tt := range tests {
    43  		v, err := memoryGasCost(&Memory{}, tt.size)
    44  		if (err == ErrGasUintOverflow) != tt.overflow {
    45  			t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
    46  		}
    47  		if v != tt.cost {
    48  			t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
    49  		}
    50  	}
    51  }
    52  
    53  var eip2200Tests = []struct {
    54  	original byte
    55  	gaspool  uint64
    56  	input    string
    57  	used     uint64
    58  	refund   uint64
    59  	failure  error
    60  }{
    61  	{0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil},                // 0 -> 0 -> 0
    62  	{0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil},               // 0 -> 0 -> 1
    63  	{0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil},           // 0 -> 1 -> 0
    64  	{0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil},               // 0 -> 1 -> 2
    65  	{0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil},               // 0 -> 1 -> 1
    66  	{1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil},            // 1 -> 0 -> 0
    67  	{1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil},             // 1 -> 0 -> 1
    68  	{1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil},                // 1 -> 0 -> 2
    69  	{1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil},            // 1 -> 2 -> 0
    70  	{1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil},                // 1 -> 2 -> 3
    71  	{1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil},             // 1 -> 2 -> 1
    72  	{1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil},                // 1 -> 2 -> 2
    73  	{1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil},            // 1 -> 1 -> 0
    74  	{1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil},                // 1 -> 1 -> 2
    75  	{1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil},                // 1 -> 1 -> 1
    76  	{0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1
    77  	{1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0
    78  	{1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas},                            // 1 -> 1 (2300 sentry + 2xPUSH)
    79  	{1, 2307, "0x6001600055", 806, 0, nil},                                     // 1 -> 1 (2301 sentry + 2xPUSH)
    80  }
    81  
    82  func TestEIP2200(t *testing.T) {
    83  	for i, tt := range eip2200Tests {
    84  		address := common.BytesToAddress([]byte("contract"))
    85  
    86  		statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    87  		statedb.CreateAccount(address)
    88  		statedb.SetCode(address, hexutil.MustDecode(tt.input))
    89  		statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original}))
    90  		statedb.Finalise(true) // Push the state into the "original" slot
    91  
    92  		vmctx := BlockContext{
    93  			CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
    94  			Transfer:    func(StateDB, common.Address, common.Address, *big.Int) {},
    95  		}
    96  		vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
    97  
    98  		_, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int))
    99  		if err != tt.failure {
   100  			t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
   101  		}
   102  		if used := tt.gaspool - gas; used != tt.used {
   103  			t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used)
   104  		}
   105  		if refund := vmenv.StateDB.GetRefund(); refund != tt.refund {
   106  			t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund)
   107  		}
   108  	}
   109  }
   110  
   111  var createGasTests = []struct {
   112  	code       string
   113  	eip3860    bool
   114  	gasUsed    uint64
   115  	minimumGas uint64
   116  }{
   117  	// legacy create(0, 0, 0xc000) without 3860 used
   118  	{"0x61C00060006000f0" + "600052" + "60206000F3", false, 41237, 41237},
   119  	// legacy create(0, 0, 0xc000) _with_ 3860
   120  	{"0x61C00060006000f0" + "600052" + "60206000F3", true, 44309, 44309},
   121  	// create2(0, 0, 0xc001, 0) without 3860
   122  	{"0x600061C00160006000f5" + "600052" + "60206000F3", false, 50471, 50471},
   123  	// create2(0, 0, 0xc001, 0) (too large), with 3860
   124  	{"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32012, 100_000},
   125  	// create2(0, 0, 0xc000, 0)
   126  	// This case is trying to deploy code at (within) the limit
   127  	{"0x600061C00060006000f5" + "600052" + "60206000F3", true, 53528, 53528},
   128  	// create2(0, 0, 0xc001, 0)
   129  	// This case is trying to deploy code exceeding the limit
   130  	{"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32024, 100000},
   131  }
   132  
   133  func TestCreateGas(t *testing.T) {
   134  	for i, tt := range createGasTests {
   135  		var gasUsed = uint64(0)
   136  		doCheck := func(testGas int) bool {
   137  			address := common.BytesToAddress([]byte("contract"))
   138  			statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   139  			statedb.CreateAccount(address)
   140  			statedb.SetCode(address, hexutil.MustDecode(tt.code))
   141  			statedb.Finalise(true)
   142  			vmctx := BlockContext{
   143  				CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
   144  				Transfer:    func(StateDB, common.Address, common.Address, *big.Int) {},
   145  				BlockNumber: big.NewInt(0),
   146  			}
   147  			config := Config{}
   148  			if tt.eip3860 {
   149  				config.ExtraEips = []int{3860}
   150  			}
   151  
   152  			vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, config)
   153  			var startGas = uint64(testGas)
   154  			ret, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, startGas, new(big.Int))
   155  			if err != nil {
   156  				return false
   157  			}
   158  			gasUsed = startGas - gas
   159  			if len(ret) != 32 {
   160  				t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret))
   161  			}
   162  			if bytes.Equal(ret, make([]byte, 32)) {
   163  				// Failure
   164  				return false
   165  			}
   166  			return true
   167  		}
   168  		minGas := sort.Search(100_000, doCheck)
   169  		if uint64(minGas) != tt.minimumGas {
   170  			t.Fatalf("test %d: min gas error, want %d, have %d", i, tt.minimumGas, minGas)
   171  		}
   172  		// If the deployment succeeded, we also check the gas used
   173  		if minGas < 100_000 {
   174  			if gasUsed != tt.gasUsed {
   175  				t.Errorf("test %d: gas used mismatch: have %v, want %v", i, gasUsed, tt.gasUsed)
   176  			}
   177  		}
   178  	}
   179  }