gitlab.com/flarenetwork/coreth@v0.1.1/core/vm/logger_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 2016 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/big" 31 "testing" 32 33 "github.com/ethereum/go-ethereum/common" 34 "github.com/holiman/uint256" 35 "gitlab.com/flarenetwork/coreth/core/state" 36 "gitlab.com/flarenetwork/coreth/params" 37 ) 38 39 type dummyContractRef struct { 40 calledForEach bool 41 } 42 43 func (dummyContractRef) Address() common.Address { return common.Address{} } 44 func (dummyContractRef) Value() *big.Int { return new(big.Int) } 45 func (dummyContractRef) SetCode(common.Hash, []byte) {} 46 func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) { 47 d.calledForEach = true 48 } 49 func (d *dummyContractRef) SubBalance(amount *big.Int) {} 50 func (d *dummyContractRef) AddBalance(amount *big.Int) {} 51 func (d *dummyContractRef) SetBalance(*big.Int) {} 52 func (d *dummyContractRef) SetNonce(uint64) {} 53 func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) } 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{}, &dummyStatedb{}, 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.CaptureState(env, 0, SSTORE, 0, 0, scope, nil, 0, nil) 76 if len(logger.storage[contract.Address()]) == 0 { 77 t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), 78 len(logger.storage[contract.Address()])) 79 } 80 exp := common.BigToHash(big.NewInt(1)) 81 if logger.storage[contract.Address()][index] != exp { 82 t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index]) 83 } 84 }