github.com/klaytn/klaytn@v1.12.1/blockchain/vm/gas_table_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 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/gas_table_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package vm 22 23 import ( 24 "bytes" 25 "math" 26 "math/big" 27 "sort" 28 "testing" 29 30 "github.com/klaytn/klaytn/blockchain/state" 31 "github.com/klaytn/klaytn/common" 32 "github.com/klaytn/klaytn/common/hexutil" 33 "github.com/klaytn/klaytn/kerrors" 34 "github.com/klaytn/klaytn/params" 35 "github.com/klaytn/klaytn/storage/database" 36 ) 37 38 func TestMemoryGasCost(t *testing.T) { 39 tests := []struct { 40 size uint64 41 cost uint64 42 overflow bool 43 }{ 44 {0x1fffffffe0, 36028809887088637, false}, 45 {0x1fffffffe1, 0, true}, 46 } 47 for i, tt := range tests { 48 v, err := memoryGasCost(&Memory{}, tt.size) 49 if (err == errGasUintOverflow) != tt.overflow { 50 t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == errGasUintOverflow, tt.overflow) 51 } 52 if v != tt.cost { 53 t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost) 54 } 55 } 56 } 57 58 var eip2200Tests = []struct { 59 original byte 60 gaspool uint64 61 input string 62 used uint64 63 refund uint64 64 failure error 65 }{ 66 // before istanbul compatible hard fork 67 {0, math.MaxUint64, "0x60006000556000600055", 10012, 0, nil}, // 0 -> 0 -> 0 68 {0, math.MaxUint64, "0x60006000556001600055", 25012, 0, nil}, // 0 -> 0 -> 1 69 {0, math.MaxUint64, "0x60016000556000600055", 25012, 15000, nil}, // 0 -> 1 -> 0 70 {0, math.MaxUint64, "0x60016000556002600055", 25012, 0, nil}, // 0 -> 1 -> 2 71 {0, math.MaxUint64, "0x60016000556001600055", 25012, 0, nil}, // 0 -> 1 -> 1 72 {1, math.MaxUint64, "0x60006000556000600055", 10012, 15000, nil}, // 1 -> 0 -> 0 73 {1, math.MaxUint64, "0x60006000556001600055", 25012, 15000, nil}, // 1 -> 0 -> 1 74 {1, math.MaxUint64, "0x60006000556002600055", 25012, 15000, nil}, // 1 -> 0 -> 2 75 {1, math.MaxUint64, "0x60026000556000600055", 10012, 15000, nil}, // 1 -> 2 -> 0 76 {1, math.MaxUint64, "0x60026000556003600055", 10012, 0, nil}, // 1 -> 2 -> 3 77 {1, math.MaxUint64, "0x60026000556001600055", 10012, 0, nil}, // 1 -> 2 -> 1 78 {1, math.MaxUint64, "0x60026000556002600055", 10012, 0, nil}, // 1 -> 2 -> 2 79 {1, math.MaxUint64, "0x60016000556000600055", 10012, 15000, nil}, // 1 -> 1 -> 0 80 {1, math.MaxUint64, "0x60016000556002600055", 10012, 0, nil}, // 1 -> 1 -> 2 81 {1, math.MaxUint64, "0x60016000556001600055", 10012, 0, nil}, // 1 -> 1 -> 1 82 {0, math.MaxUint64, "0x600160005560006000556001600055", 45018, 15000, nil}, // 0 -> 1 -> 0 -> 1 83 {1, math.MaxUint64, "0x600060005560016000556000600055", 30018, 30000, nil}, // 1 -> 0 -> 1 -> 0 84 {1, 2306, "0x6001600055", 2306, 0, kerrors.ErrOutOfGas}, // 1 -> 1 (2300 sentry + 2xPUSH) 85 {1, 2307, "0x6001600055", 2307, 0, kerrors.ErrOutOfGas}, // 1 -> 1 (2301 sentry + 2xPUSH) 86 // after istanbul compatible hard fork 87 {0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil}, // 0 -> 0 -> 0 88 {0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil}, // 0 -> 0 -> 1 89 {0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil}, // 0 -> 1 -> 0 90 {0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil}, // 0 -> 1 -> 2 91 {0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil}, // 0 -> 1 -> 1 92 {1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil}, // 1 -> 0 -> 0 93 {1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil}, // 1 -> 0 -> 1 94 {1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil}, // 1 -> 0 -> 2 95 {1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil}, // 1 -> 2 -> 0 96 {1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil}, // 1 -> 2 -> 3 97 {1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil}, // 1 -> 2 -> 1 98 {1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil}, // 1 -> 2 -> 2 99 {1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil}, // 1 -> 1 -> 0 100 {1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil}, // 1 -> 1 -> 2 101 {1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil}, // 1 -> 1 -> 1 102 {0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1 103 {1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0 104 {1, 2306, "0x6001600055", 2306, 0, kerrors.ErrOutOfGas}, // 1 -> 1 (2300 sentry + 2xPUSH) 105 {1, 2307, "0x6001600055", 806, 0, nil}, // 1 -> 1 (2301 sentry + 2xPUSH) 106 } 107 108 func TestEIP2200(t *testing.T) { 109 for i, tt := range eip2200Tests { 110 address := common.BytesToAddress([]byte("contract")) 111 112 statedb, _ := state.New(common.Hash{}, state.NewDatabase(database.NewMemoryDBManager()), nil, nil) 113 statedb.CreateSmartContractAccount(address, params.CodeFormatEVM, params.Rules{IsIstanbul: true}) 114 statedb.SetCode(address, hexutil.MustDecode(tt.input)) 115 statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original})) 116 statedb.Finalise(true, false) // Push the state into the "original" slot 117 118 vmctx := BlockContext{ 119 CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true }, 120 Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, 121 } 122 var vmenv *EVM 123 if i <= 18 { 124 vmenv = NewEVM(vmctx, TxContext{}, statedb, params.AllGxhashProtocolChanges, &Config{}) 125 } else { 126 vmenv = NewEVM(vmctx, TxContext{}, statedb, params.AllGxhashProtocolChanges, &Config{ExtraEips: []int{2200}}) 127 } 128 129 _, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int)) 130 if err != tt.failure { 131 t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure) 132 } 133 if used := tt.gaspool - gas; used != tt.used { 134 t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used) 135 } 136 if refund := vmenv.StateDB.GetRefund(); refund != tt.refund { 137 t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund) 138 } 139 } 140 } 141 142 var createGasTests = []struct { 143 code string 144 eip3860 bool 145 gasUsed uint64 146 minimumGas uint64 147 }{ 148 // legacy create(0, 0, 0xc000) without eip3860 149 {"0x61C00060006000f0" + "600052" + "60206000F3", false, 41237, 41237}, 150 // create2(0, 0, 0xc001, 0) without eip3860 151 {"0x600061C00160006000f5" + "600052" + "60206000F3", false, 50471, 50471}, 152 // legacy create(0, 0, 0xc000), with eip3860 153 {"0x61C00060006000f0" + "600052" + "60206000F3", true, 44309, 44309}, 154 // create2(0, 0, 0xc001, 0) (too large), with eip3860 155 {"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32012, 100_000}, 156 // create2(0, 0, 0xc000, 0) 157 // This case is trying to deploy code at (within) the limit 158 {"0x600061C00060006000f5" + "600052" + "60206000F3", true, 53528, 53528}, 159 // create2(0, 0, 0xc001, 0) 160 // This case is trying to deploy code exceeding the limit 161 {"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32024, 100000}, 162 } 163 164 func TestCreateGas(t *testing.T) { 165 for i, tt := range createGasTests { 166 gasUsed := uint64(0) 167 doCheck := func(testGas int) bool { 168 address := common.BytesToAddress([]byte("contract")) 169 statedb, _ := state.New(common.Hash{}, state.NewDatabase(database.NewMemoryDBManager()), nil, nil) 170 statedb.CreateSmartContractAccount(address, params.CodeFormatEVM, params.Rules{IsIstanbul: true}) 171 statedb.SetCode(address, hexutil.MustDecode(tt.code)) 172 statedb.Finalise(true, false) 173 174 vmctx := BlockContext{ 175 CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true }, 176 Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, 177 } 178 config := Config{} 179 if tt.eip3860 { 180 config.ExtraEips = []int{3860} 181 } 182 183 vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllGxhashProtocolChanges, &config) 184 startGas := uint64(testGas) 185 ret, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, startGas, new(big.Int)) 186 if err != nil { 187 return false 188 } 189 gasUsed = startGas - gas 190 191 if len(ret) != 32 { 192 t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret)) 193 } 194 if bytes.Equal(ret, make([]byte, 32)) { 195 // Failure 196 return false 197 } 198 return true 199 } 200 minGas := sort.Search(100_000, doCheck) 201 if uint64(minGas) != tt.minimumGas { 202 t.Fatalf("test %d: min gas error, want %d, have %d", i, tt.minimumGas, minGas) 203 } 204 // If the deployment succeeded, we also check the gas used 205 if minGas < 100_000 { 206 if gasUsed != tt.gasUsed { 207 t.Errorf("test %d: gas used mismatch: have %v, want %v", i, gasUsed, tt.gasUsed) 208 } 209 } 210 } 211 }