2022-05-24 21:39:40 +03:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
2016-02-04 00:46:27 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2021-11-25 15:17:09 +03:00
|
|
|
package logger
|
2016-02-04 00:46:27 +02:00
|
|
|
|
|
|
|
import (
|
2022-03-29 23:36:55 +03:00
|
|
|
"encoding/json"
|
2023-05-25 09:57:34 +03:00
|
|
|
"errors"
|
2016-02-04 00:46:27 +02:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-10-23 17:28:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2021-11-25 15:17:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-12-06 04:16:03 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2024-01-23 16:51:58 +03:00
|
|
|
"github.com/holiman/uint256"
|
2016-02-04 00:46:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type dummyContractRef struct {
|
|
|
|
calledForEach bool
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:16:03 +03:00
|
|
|
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-04 00:46:27 +02: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 17:28:18 +03:00
|
|
|
type dummyStatedb struct {
|
|
|
|
state.StateDB
|
|
|
|
}
|
|
|
|
|
core/state: move state log mechanism to a separate layer (#30569)
This PR moves the logging/tracing-facilities out of `*state.StateDB`,
in to a wrapping struct which implements `vm.StateDB` instead.
In most places, it is a pretty straight-forward change:
- First, hoisting the invocations from state objects up to the statedb.
- Then making the mutation-methods simply return the previous value, so
that the external logging layer could log everything.
Some internal code uses the direct object-accessors to mutate the state,
particularly in testing and in setting up state overrides, which means
that these changes are unobservable for the hooked layer. Thus, configuring
the overrides are not necessarily part of the API we want to publish.
The trickiest part about the layering is that when the selfdestructs are
finally deleted during `Finalise`, there's the possibility that someone
sent some ether to it, which is burnt at that point, and thus needs to
be logged. The hooked layer reaches into the inner layer to figure out
these events.
In package `vm`, the conversion from `state.StateDB + hooks` into a
hooked `vm.StateDB` is performed where needed.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2024-10-23 09:03:36 +03:00
|
|
|
func (*dummyStatedb) GetRefund() uint64 { return 1337 }
|
|
|
|
func (*dummyStatedb) GetState(_ common.Address, _ common.Hash) common.Hash { return common.Hash{} }
|
|
|
|
func (*dummyStatedb) SetState(_ common.Address, _ common.Hash, _ common.Hash) common.Hash {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
2018-10-23 17:28:18 +03:00
|
|
|
|
2016-02-04 00:46:27 +02:00
|
|
|
func TestStoreCapture(t *testing.T) {
|
|
|
|
var (
|
2016-08-19 17:19:51 +03:00
|
|
|
logger = NewStructLogger(nil)
|
2024-03-22 20:53:53 +03:00
|
|
|
env = vm.NewEVM(vm.BlockContext{}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: logger.Hooks()})
|
2024-01-23 16:51:58 +03:00
|
|
|
contract = vm.NewContract(&dummyContractRef{}, &dummyContractRef{}, new(uint256.Int), 100000)
|
2016-02-04 00:46:27 +02:00
|
|
|
)
|
2021-11-25 15:17:09 +03:00
|
|
|
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
|
2016-02-04 00:46:27 +02:00
|
|
|
var index common.Hash
|
2024-03-22 20:53:53 +03:00
|
|
|
logger.OnTxStart(env.GetVMContext(), nil, common.Address{})
|
2021-11-25 15:17:09 +03:00
|
|
|
_, err := env.Interpreter().Run(contract, []byte{}, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-10 12:46:13 +03:00
|
|
|
if len(logger.storage[contract.Address()]) == 0 {
|
2021-03-25 12:13:14 +03:00
|
|
|
t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(),
|
|
|
|
len(logger.storage[contract.Address()]))
|
2016-02-04 00:46:27 +02:00
|
|
|
}
|
|
|
|
exp := common.BigToHash(big.NewInt(1))
|
2020-06-10 12:46:13 +03:00
|
|
|
if logger.storage[contract.Address()][index] != exp {
|
|
|
|
t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
|
2016-02-04 00:46:27 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 23:36:55 +03:00
|
|
|
|
|
|
|
// Tests that blank fields don't appear in logs when JSON marshalled, to reduce
|
|
|
|
// logs bloat and confusion. See https://github.com/ethereum/go-ethereum/issues/24487
|
|
|
|
func TestStructLogMarshalingOmitEmpty(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
log *StructLog
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"empty err and no fields", &StructLog{},
|
|
|
|
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
|
2023-05-25 09:57:34 +03:00
|
|
|
{"with err", &StructLog{Err: errors.New("this failed")},
|
2022-03-29 23:36:55 +03:00
|
|
|
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`},
|
|
|
|
{"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2},
|
|
|
|
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
|
|
|
|
{"with 0-size mem", &StructLog{Memory: make([]byte, 0)},
|
|
|
|
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
blob, err := json.Marshal(tt.log)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if have, want := string(blob), tt.want; have != want {
|
|
|
|
t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|