github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/vm/logger_test.go (about) 1 // Copyright 2016 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 vm 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/holiman/uint256" 24 25 "github.com/scroll-tech/go-ethereum/common" 26 "github.com/scroll-tech/go-ethereum/core/rawdb" 27 "github.com/scroll-tech/go-ethereum/core/state" 28 "github.com/scroll-tech/go-ethereum/params" 29 ) 30 31 type dummyContractRef struct { 32 calledForEach bool 33 } 34 35 func (dummyContractRef) Address() common.Address { return common.Address{} } 36 func (dummyContractRef) Value() *big.Int { return new(big.Int) } 37 func (dummyContractRef) SetCode(common.Hash, []byte) {} 38 func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) { 39 d.calledForEach = true 40 } 41 func (d *dummyContractRef) SubBalance(amount *big.Int) {} 42 func (d *dummyContractRef) AddBalance(amount *big.Int) {} 43 func (d *dummyContractRef) SetBalance(*big.Int) {} 44 func (d *dummyContractRef) SetNonce(uint64) {} 45 func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) } 46 47 // makeTestState create a sample test state to test node-wise reconstruction. 48 func makeTestState() *state.StateDB { 49 // Create an empty state 50 db := state.NewDatabase(rawdb.NewMemoryDatabase()) 51 stateDb, _ := state.New(common.Hash{}, db, nil) 52 return stateDb 53 } 54 55 type dummyStatedb struct { 56 state.StateDB 57 } 58 59 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 60 61 func TestStoreCapture(t *testing.T) { 62 var ( 63 env = NewEVM(BlockContext{}, TxContext{}, makeTestState(), params.TestChainConfig, Config{}) 64 logger = NewStructLogger(nil) 65 contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0) 66 scope = &ScopeContext{ 67 Memory: NewMemory(), 68 Stack: newstack(), 69 Contract: contract, 70 } 71 ) 72 scope.Stack.push(uint256.NewInt(1)) 73 scope.Stack.push(new(uint256.Int)) 74 var index common.Hash 75 logger.CaptureStart(env, common.Address{}, contract.Address(), false, nil, 0, nil) 76 logger.CaptureState(0, SSTORE, 0, 0, scope, nil, 0, nil) 77 if len(logger.storage[contract.Address()]) == 0 { 78 t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), 79 len(logger.storage[contract.Address()])) 80 } 81 exp := common.BigToHash(big.NewInt(1)) 82 if logger.storage[contract.Address()][index] != exp { 83 t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index]) 84 } 85 }