github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/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  	"errors"
    22  	"math"
    23  	"math/big"
    24  	"sort"
    25  	"testing"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/common/hexutil"
    29  	"github.com/ethereum/go-ethereum/core/rawdb"
    30  	"github.com/ethereum/go-ethereum/core/state"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/params"
    33  	"github.com/holiman/uint256"
    34  )
    35  
    36  func TestMemoryGasCost(t *testing.T) {
    37  	tests := []struct {
    38  		size     uint64
    39  		cost     uint64
    40  		overflow bool
    41  	}{
    42  		{0x1fffffffe0, 36028809887088637, false},
    43  		{0x1fffffffe1, 0, true},
    44  	}
    45  	for i, tt := range tests {
    46  		v, err := memoryGasCost(&Memory{}, tt.size)
    47  		if (err == ErrGasUintOverflow) != tt.overflow {
    48  			t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
    49  		}
    50  		if v != tt.cost {
    51  			t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
    52  		}
    53  	}
    54  }
    55  
    56  var eip2200Tests = []struct {
    57  	original byte
    58  	gaspool  uint64
    59  	input    string
    60  	used     uint64
    61  	refund   uint64
    62  	failure  error
    63  }{
    64  	{0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil},                // 0 -> 0 -> 0
    65  	{0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil},               // 0 -> 0 -> 1
    66  	{0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil},           // 0 -> 1 -> 0
    67  	{0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil},               // 0 -> 1 -> 2
    68  	{0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil},               // 0 -> 1 -> 1
    69  	{1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil},            // 1 -> 0 -> 0
    70  	{1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil},             // 1 -> 0 -> 1
    71  	{1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil},                // 1 -> 0 -> 2
    72  	{1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil},            // 1 -> 2 -> 0
    73  	{1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil},                // 1 -> 2 -> 3
    74  	{1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil},             // 1 -> 2 -> 1
    75  	{1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil},                // 1 -> 2 -> 2
    76  	{1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil},            // 1 -> 1 -> 0
    77  	{1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil},                // 1 -> 1 -> 2
    78  	{1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil},                // 1 -> 1 -> 1
    79  	{0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1
    80  	{1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0
    81  	{1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas},                            // 1 -> 1 (2300 sentry + 2xPUSH)
    82  	{1, 2307, "0x6001600055", 806, 0, nil},                                     // 1 -> 1 (2301 sentry + 2xPUSH)
    83  }
    84  
    85  func TestEIP2200(t *testing.T) {
    86  	for i, tt := range eip2200Tests {
    87  		address := common.BytesToAddress([]byte("contract"))
    88  
    89  		statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    90  		statedb.CreateAccount(address)
    91  		statedb.SetCode(address, hexutil.MustDecode(tt.input))
    92  		statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original}))
    93  		statedb.Finalise(true) // Push the state into the "original" slot
    94  
    95  		vmctx := BlockContext{
    96  			CanTransfer: func(StateDB, common.Address, *uint256.Int) bool { return true },
    97  			Transfer:    func(StateDB, common.Address, common.Address, *uint256.Int) {},
    98  		}
    99  		vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
   100  
   101  		_, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(uint256.Int))
   102  		if !errors.Is(err, tt.failure) {
   103  			t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
   104  		}
   105  		if used := tt.gaspool - gas; used != tt.used {
   106  			t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used)
   107  		}
   108  		if refund := vmenv.StateDB.GetRefund(); refund != tt.refund {
   109  			t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund)
   110  		}
   111  	}
   112  }
   113  
   114  var createGasTests = []struct {
   115  	code       string
   116  	eip3860    bool
   117  	gasUsed    uint64
   118  	minimumGas uint64
   119  }{
   120  	// legacy create(0, 0, 0xc000) without 3860 used
   121  	{"0x61C00060006000f0" + "600052" + "60206000F3", false, 41237, 41237},
   122  	// legacy create(0, 0, 0xc000) _with_ 3860
   123  	{"0x61C00060006000f0" + "600052" + "60206000F3", true, 44309, 44309},
   124  	// create2(0, 0, 0xc001, 0) without 3860
   125  	{"0x600061C00160006000f5" + "600052" + "60206000F3", false, 50471, 50471},
   126  	// create2(0, 0, 0xc001, 0) (too large), with 3860
   127  	{"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32012, 100_000},
   128  	// create2(0, 0, 0xc000, 0)
   129  	// This case is trying to deploy code at (within) the limit
   130  	{"0x600061C00060006000f5" + "600052" + "60206000F3", true, 53528, 53528},
   131  	// create2(0, 0, 0xc001, 0)
   132  	// This case is trying to deploy code exceeding the limit
   133  	{"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32024, 100000},
   134  }
   135  
   136  func TestCreateGas(t *testing.T) {
   137  	for i, tt := range createGasTests {
   138  		var gasUsed = uint64(0)
   139  		doCheck := func(testGas int) bool {
   140  			address := common.BytesToAddress([]byte("contract"))
   141  			statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   142  			statedb.CreateAccount(address)
   143  			statedb.SetCode(address, hexutil.MustDecode(tt.code))
   144  			statedb.Finalise(true)
   145  			vmctx := BlockContext{
   146  				CanTransfer: func(StateDB, common.Address, *uint256.Int) bool { return true },
   147  				Transfer:    func(StateDB, common.Address, common.Address, *uint256.Int) {},
   148  				BlockNumber: big.NewInt(0),
   149  			}
   150  			config := Config{}
   151  			if tt.eip3860 {
   152  				config.ExtraEips = []int{3860}
   153  			}
   154  
   155  			vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, config)
   156  			var startGas = uint64(testGas)
   157  			ret, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, startGas, new(uint256.Int))
   158  			if err != nil {
   159  				return false
   160  			}
   161  			gasUsed = startGas - gas
   162  			if len(ret) != 32 {
   163  				t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret))
   164  			}
   165  			if bytes.Equal(ret, make([]byte, 32)) {
   166  				// Failure
   167  				return false
   168  			}
   169  			return true
   170  		}
   171  		minGas := sort.Search(100_000, doCheck)
   172  		if uint64(minGas) != tt.minimumGas {
   173  			t.Fatalf("test %d: min gas error, want %d, have %d", i, tt.minimumGas, minGas)
   174  		}
   175  		// If the deployment succeeded, we also check the gas used
   176  		if minGas < 100_000 {
   177  			if gasUsed != tt.gasUsed {
   178  				t.Errorf("test %d: gas used mismatch: have %v, want %v", i, gasUsed, tt.gasUsed)
   179  			}
   180  		}
   181  	}
   182  }