github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/ethclient/simulated/options_test.go (about) 1 // Copyright 2024 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 simulated 18 19 import ( 20 "context" 21 "math/big" 22 "strings" 23 "testing" 24 25 "github.com/ethereum/go-ethereum" 26 "github.com/ethereum/go-ethereum/core" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/params" 29 ) 30 31 // Tests that the simulator starts with the initial gas limit in the genesis block, 32 // and that it keeps the same target value. 33 func TestWithBlockGasLimitOption(t *testing.T) { 34 // Construct a simulator, targeting a different gas limit 35 sim := NewBackend(types.GenesisAlloc{}, WithBlockGasLimit(12_345_678)) 36 defer sim.Close() 37 38 client := sim.Client() 39 genesis, err := client.BlockByNumber(context.Background(), big.NewInt(0)) 40 if err != nil { 41 t.Fatalf("failed to retrieve genesis block: %v", err) 42 } 43 if genesis.GasLimit() != 12_345_678 { 44 t.Errorf("genesis gas limit mismatch: have %v, want %v", genesis.GasLimit(), 12_345_678) 45 } 46 // Produce a number of blocks and verify the locked in gas target 47 sim.Commit() 48 head, err := client.BlockByNumber(context.Background(), big.NewInt(1)) 49 if err != nil { 50 t.Fatalf("failed to retrieve head block: %v", err) 51 } 52 if head.GasLimit() != 12_345_678 { 53 t.Errorf("head gas limit mismatch: have %v, want %v", head.GasLimit(), 12_345_678) 54 } 55 } 56 57 // Tests that the simulator honors the RPC call caps set by the options. 58 func TestWithCallGasLimitOption(t *testing.T) { 59 // Construct a simulator, targeting a different gas limit 60 sim := NewBackend(types.GenesisAlloc{ 61 testAddr: {Balance: big.NewInt(10000000000000000)}, 62 }, WithCallGasLimit(params.TxGas-1)) 63 defer sim.Close() 64 65 client := sim.Client() 66 _, err := client.CallContract(context.Background(), ethereum.CallMsg{ 67 From: testAddr, 68 To: &testAddr, 69 Gas: 21000, 70 }, nil) 71 if !strings.Contains(err.Error(), core.ErrIntrinsicGas.Error()) { 72 t.Fatalf("error mismatch: have %v, want %v", err, core.ErrIntrinsicGas) 73 } 74 }