github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/gas_table_test.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2017 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package vm 28 29 import ( 30 "math" 31 "math/big" 32 "testing" 33 34 "github.com/MetalBlockchain/subnet-evm/core/rawdb" 35 "github.com/MetalBlockchain/subnet-evm/core/state" 36 "github.com/MetalBlockchain/subnet-evm/params" 37 "github.com/MetalBlockchain/subnet-evm/vmerrs" 38 "github.com/ethereum/go-ethereum/common" 39 "github.com/ethereum/go-ethereum/common/hexutil" 40 ) 41 42 func TestMemoryGasCost(t *testing.T) { 43 tests := []struct { 44 size uint64 45 cost uint64 46 overflow bool 47 }{ 48 {0x1fffffffe0, 36028809887088637, false}, 49 {0x1fffffffe1, 0, true}, 50 } 51 for i, tt := range tests { 52 v, err := memoryGasCost(&Memory{}, tt.size) 53 if (err == vmerrs.ErrGasUintOverflow) != tt.overflow { 54 t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == vmerrs.ErrGasUintOverflow, tt.overflow) 55 } 56 if v != tt.cost { 57 t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost) 58 } 59 } 60 } 61 62 var eip2200Tests = []struct { 63 original byte 64 gaspool uint64 65 input string 66 used uint64 67 refund uint64 68 failure error 69 }{ 70 {0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil}, // 0 -> 0 -> 0 71 {0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil}, // 0 -> 0 -> 1 72 {0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil}, // 0 -> 1 -> 0 73 {0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil}, // 0 -> 1 -> 2 74 {0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil}, // 0 -> 1 -> 1 75 {1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil}, // 1 -> 0 -> 0 76 {1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil}, // 1 -> 0 -> 1 77 {1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil}, // 1 -> 0 -> 2 78 {1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil}, // 1 -> 2 -> 0 79 {1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil}, // 1 -> 2 -> 3 80 {1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil}, // 1 -> 2 -> 1 81 {1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil}, // 1 -> 2 -> 2 82 {1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil}, // 1 -> 1 -> 0 83 {1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil}, // 1 -> 1 -> 2 84 {1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil}, // 1 -> 1 -> 1 85 {0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1 86 {1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0 87 {1, 2306, "0x6001600055", 2306, 0, vmerrs.ErrOutOfGas}, // 1 -> 1 (2300 sentry + 2xPUSH) 88 {1, 2307, "0x6001600055", 806, 0, nil}, // 1 -> 1 (2301 sentry + 2xPUSH) 89 } 90 91 func TestEIP2200(t *testing.T) { 92 for i, tt := range eip2200Tests { 93 address := common.BytesToAddress([]byte("contract")) 94 95 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 96 statedb.CreateAccount(address) 97 statedb.SetCode(address, hexutil.MustDecode(tt.input)) 98 statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original})) 99 statedb.Finalise(true) // Push the state into the "original" slot 100 101 vmctx := BlockContext{ 102 CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true }, 103 Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, 104 } 105 vmenv := NewEVM(vmctx, TxContext{}, statedb, params.TestChainConfig, Config{ExtraEips: []int{2200}}) 106 107 _, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int)) 108 if err != tt.failure { 109 t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure) 110 } 111 if used := tt.gaspool - gas; used != tt.used { 112 t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used) 113 } 114 if refund := vmenv.StateDB.GetRefund(); refund != tt.refund { 115 t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund) 116 } 117 } 118 }