2016-02-03 22:46:27 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-10-23 14:28:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2016-12-06 01:16:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-06-08 12:24:40 +00:00
|
|
|
"github.com/holiman/uint256"
|
2016-02-03 22:46:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type dummyContractRef struct {
|
|
|
|
calledForEach bool
|
|
|
|
}
|
|
|
|
|
2016-12-06 01:16:03 +00:00
|
|
|
func (dummyContractRef) ReturnGas(*big.Int) {}
|
|
|
|
func (dummyContractRef) Address() common.Address { return common.Address{} }
|
|
|
|
func (dummyContractRef) Value() *big.Int { return new(big.Int) }
|
|
|
|
func (dummyContractRef) SetCode(common.Hash, []byte) {}
|
2016-02-03 22:46:27 +00:00
|
|
|
func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
|
|
|
|
d.calledForEach = true
|
|
|
|
}
|
|
|
|
func (d *dummyContractRef) SubBalance(amount *big.Int) {}
|
|
|
|
func (d *dummyContractRef) AddBalance(amount *big.Int) {}
|
|
|
|
func (d *dummyContractRef) SetBalance(*big.Int) {}
|
|
|
|
func (d *dummyContractRef) SetNonce(uint64) {}
|
|
|
|
func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
|
|
|
|
|
2018-10-23 14:28:18 +00:00
|
|
|
type dummyStatedb struct {
|
|
|
|
state.StateDB
|
|
|
|
}
|
|
|
|
|
2018-11-08 12:07:15 +00:00
|
|
|
func (*dummyStatedb) GetRefund() uint64 { return 1337 }
|
2018-10-23 14:28:18 +00:00
|
|
|
|
2016-02-03 22:46:27 +00:00
|
|
|
func TestStoreCapture(t *testing.T) {
|
|
|
|
var (
|
2020-11-13 12:42:19 +00:00
|
|
|
env = NewEVM(BlockContext{}, TxContext{}, &dummyStatedb{}, params.TestChainConfig, Config{})
|
2016-08-19 14:19:51 +00:00
|
|
|
logger = NewStructLogger(nil)
|
2016-02-03 22:46:27 +00:00
|
|
|
mem = NewMemory()
|
|
|
|
stack = newstack()
|
2020-06-02 10:30:16 +00:00
|
|
|
rstack = newReturnStack()
|
2017-01-04 19:17:24 +00:00
|
|
|
contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
|
2016-02-03 22:46:27 +00:00
|
|
|
)
|
2020-06-08 12:24:40 +00:00
|
|
|
stack.push(uint256.NewInt().SetUint64(1))
|
|
|
|
stack.push(uint256.NewInt())
|
2016-02-03 22:46:27 +00:00
|
|
|
var index common.Hash
|
2020-07-16 12:06:19 +00:00
|
|
|
logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, rstack, nil, contract, 0, nil)
|
2020-06-10 09:46:13 +00:00
|
|
|
if len(logger.storage[contract.Address()]) == 0 {
|
|
|
|
t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.storage[contract.Address()]))
|
2016-02-03 22:46:27 +00:00
|
|
|
}
|
|
|
|
exp := common.BigToHash(big.NewInt(1))
|
2020-06-10 09:46:13 +00:00
|
|
|
if logger.storage[contract.Address()][index] != exp {
|
|
|
|
t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
|
2016-02-03 22:46:27 +00:00
|
|
|
}
|
|
|
|
}
|