github.com/ethereum/go-ethereum@v1.16.1/internal/ethapi/override/override_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 override 18 19 import ( 20 "maps" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/hexutil" 25 "github.com/ethereum/go-ethereum/core/rawdb" 26 "github.com/ethereum/go-ethereum/core/state" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/core/vm" 29 "github.com/ethereum/go-ethereum/triedb" 30 ) 31 32 type precompileContract struct{} 33 34 func (p *precompileContract) RequiredGas(input []byte) uint64 { return 0 } 35 36 func (p *precompileContract) Run(input []byte) ([]byte, error) { return nil, nil } 37 38 func TestStateOverrideMovePrecompile(t *testing.T) { 39 db := state.NewDatabase(triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil), nil) 40 statedb, err := state.New(types.EmptyRootHash, db) 41 if err != nil { 42 t.Fatalf("failed to create statedb: %v", err) 43 } 44 precompiles := map[common.Address]vm.PrecompiledContract{ 45 common.BytesToAddress([]byte{0x1}): &precompileContract{}, 46 common.BytesToAddress([]byte{0x2}): &precompileContract{}, 47 } 48 bytes2Addr := func(b []byte) *common.Address { 49 a := common.BytesToAddress(b) 50 return &a 51 } 52 var testSuite = []struct { 53 overrides StateOverride 54 expectedPrecompiles map[common.Address]struct{} 55 fail bool 56 }{ 57 { 58 overrides: StateOverride{ 59 common.BytesToAddress([]byte{0x1}): { 60 Code: hex2Bytes("0xff"), 61 MovePrecompileTo: bytes2Addr([]byte{0x2}), 62 }, 63 common.BytesToAddress([]byte{0x2}): { 64 Code: hex2Bytes("0x00"), 65 }, 66 }, 67 // 0x2 has already been touched by the moveTo. 68 fail: true, 69 }, { 70 overrides: StateOverride{ 71 common.BytesToAddress([]byte{0x1}): { 72 Code: hex2Bytes("0xff"), 73 MovePrecompileTo: bytes2Addr([]byte{0xff}), 74 }, 75 common.BytesToAddress([]byte{0x3}): { 76 Code: hex2Bytes("0x00"), 77 MovePrecompileTo: bytes2Addr([]byte{0xfe}), 78 }, 79 }, 80 // 0x3 is not a precompile. 81 fail: true, 82 }, { 83 overrides: StateOverride{ 84 common.BytesToAddress([]byte{0x1}): { 85 Code: hex2Bytes("0xff"), 86 MovePrecompileTo: bytes2Addr([]byte{0xff}), 87 }, 88 common.BytesToAddress([]byte{0x2}): { 89 Code: hex2Bytes("0x00"), 90 MovePrecompileTo: bytes2Addr([]byte{0xfe}), 91 }, 92 }, 93 expectedPrecompiles: map[common.Address]struct{}{common.BytesToAddress([]byte{0xfe}): {}, common.BytesToAddress([]byte{0xff}): {}}, 94 }, 95 } 96 97 for i, tt := range testSuite { 98 cpy := maps.Clone(precompiles) 99 // Apply overrides 100 err := tt.overrides.Apply(statedb, cpy) 101 if tt.fail { 102 if err == nil { 103 t.Errorf("test %d: want error, have nothing", i) 104 } 105 continue 106 } 107 if err != nil { 108 t.Errorf("test %d: want no error, have %v", i, err) 109 continue 110 } 111 // Precompile keys 112 if len(cpy) != len(tt.expectedPrecompiles) { 113 t.Errorf("test %d: precompile mismatch, want %d, have %d", i, len(tt.expectedPrecompiles), len(cpy)) 114 } 115 for k := range tt.expectedPrecompiles { 116 if _, ok := cpy[k]; !ok { 117 t.Errorf("test %d: precompile not found: %s", i, k.String()) 118 } 119 } 120 } 121 } 122 123 func hex2Bytes(str string) *hexutil.Bytes { 124 rpcBytes := hexutil.Bytes(common.FromHex(str)) 125 return &rpcBytes 126 }