gitlab.com/flarenetwork/coreth@v0.1.1/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/ethereum/go-ethereum/common" 35 "github.com/ethereum/go-ethereum/common/hexutil" 36 "gitlab.com/flarenetwork/coreth/core/rawdb" 37 "gitlab.com/flarenetwork/coreth/core/state" 38 "gitlab.com/flarenetwork/coreth/params" 39 ) 40 41 func TestMemoryGasCost(t *testing.T) { 42 tests := []struct { 43 size uint64 44 cost uint64 45 overflow bool 46 }{ 47 {0x1fffffffe0, 36028809887088637, false}, 48 {0x1fffffffe1, 0, true}, 49 } 50 for i, tt := range tests { 51 v, err := memoryGasCost(&Memory{}, tt.size) 52 if (err == ErrGasUintOverflow) != tt.overflow { 53 t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow) 54 } 55 if v != tt.cost { 56 t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost) 57 } 58 } 59 } 60 61 var eip2200Tests = []struct { 62 original byte 63 gaspool uint64 64 input string 65 used uint64 66 refund uint64 67 failure error 68 }{ 69 {0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil}, // 0 -> 0 -> 0 70 {0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil}, // 0 -> 0 -> 1 71 {0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil}, // 0 -> 1 -> 0 72 {0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil}, // 0 -> 1 -> 2 73 {0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil}, // 0 -> 1 -> 1 74 {1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil}, // 1 -> 0 -> 0 75 {1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil}, // 1 -> 0 -> 1 76 {1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil}, // 1 -> 0 -> 2 77 {1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil}, // 1 -> 2 -> 0 78 {1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil}, // 1 -> 2 -> 3 79 {1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil}, // 1 -> 2 -> 1 80 {1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil}, // 1 -> 2 -> 2 81 {1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil}, // 1 -> 1 -> 0 82 {1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil}, // 1 -> 1 -> 2 83 {1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil}, // 1 -> 1 -> 1 84 {0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1 85 {1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0 86 {1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas}, // 1 -> 1 (2300 sentry + 2xPUSH) 87 {1, 2307, "0x6001600055", 806, 0, nil}, // 1 -> 1 (2301 sentry + 2xPUSH) 88 } 89 90 func TestEIP2200(t *testing.T) { 91 for i, tt := range eip2200Tests { 92 address := common.BytesToAddress([]byte("contract")) 93 94 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 95 statedb.CreateAccount(address) 96 statedb.SetCode(address, hexutil.MustDecode(tt.input)) 97 statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original})) 98 statedb.Finalise(true) // Push the state into the "original" slot 99 100 vmctx := BlockContext{ 101 CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true }, 102 Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, 103 } 104 vmenv := NewEVM(vmctx, TxContext{}, statedb, params.TestChainConfig, Config{ExtraEips: []int{2200}}) 105 106 _, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int)) 107 if err != tt.failure { 108 t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure) 109 } 110 if used := tt.gaspool - gas; used != tt.used { 111 t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used) 112 } 113 if refund := vmenv.StateDB.GetRefund(); refund != tt.refund { 114 t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund) 115 } 116 } 117 }