mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 08:49:29 +00:00
Reject struct log entries that would push the buffered trace output past the configured limit. Add regression tests for oversized entries and exact boundary handling.
183 lines
6.7 KiB
Go
183 lines
6.7 KiB
Go
// Copyright 2021 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 logger
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
"github.com/holiman/uint256"
|
|
)
|
|
|
|
type dummyStatedb struct {
|
|
state.StateDB
|
|
}
|
|
|
|
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{}
|
|
}
|
|
|
|
func (*dummyStatedb) GetStateAndCommittedState(common.Address, common.Hash) (common.Hash, common.Hash) {
|
|
return common.Hash{}, common.Hash{}
|
|
}
|
|
|
|
type dummyOpContext struct{}
|
|
|
|
func (dummyOpContext) MemoryData() []byte { return nil }
|
|
func (dummyOpContext) StackData() []uint256.Int { return nil }
|
|
func (dummyOpContext) Caller() common.Address { return common.Address{} }
|
|
func (dummyOpContext) Address() common.Address { return common.Address{} }
|
|
func (dummyOpContext) CallValue() *uint256.Int { return new(uint256.Int) }
|
|
func (dummyOpContext) CallInput() []byte { return nil }
|
|
func (dummyOpContext) ContractCode() []byte { return nil }
|
|
|
|
func TestStoreCapture(t *testing.T) {
|
|
var (
|
|
logger = NewStructLogger(nil)
|
|
evm = vm.NewEVM(vm.BlockContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: logger.Hooks()})
|
|
contract = vm.NewContract(common.Address{}, common.Address{}, new(uint256.Int), 100000, nil)
|
|
)
|
|
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
|
|
var index common.Hash
|
|
logger.OnTxStart(evm.GetVMContext(), nil, common.Address{})
|
|
_, err := evm.Run(contract, []byte{}, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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()]))
|
|
}
|
|
exp := common.BigToHash(big.NewInt(1))
|
|
if logger.storage[contract.Address()][index] != exp {
|
|
t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
|
|
}
|
|
}
|
|
|
|
// 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"}`},
|
|
{"with err", &StructLog{Err: errors.New("this failed")},
|
|
`{"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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStructLogLegacyJSONSpecFormatting(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
log *StructLog
|
|
want string
|
|
}{
|
|
{
|
|
name: "omits empty error and pads memory/storage",
|
|
log: &StructLog{
|
|
Pc: 7,
|
|
Op: vm.SSTORE,
|
|
Gas: 100,
|
|
GasCost: 20,
|
|
Memory: []byte{0xaa, 0xbb},
|
|
Storage: map[common.Hash]common.Hash{common.BigToHash(big.NewInt(1)): common.BigToHash(big.NewInt(2))},
|
|
Depth: 1,
|
|
ReturnData: []byte{0x12, 0x34},
|
|
},
|
|
want: `{"pc":7,"op":"SSTORE","gas":100,"gasCost":20,"depth":1,"returnData":"0x1234","memory":["0xaabb000000000000000000000000000000000000000000000000000000000000"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000001":"0x0000000000000000000000000000000000000000000000000000000000000002"}}`,
|
|
},
|
|
{
|
|
name: "includes error only when present",
|
|
log: &StructLog{
|
|
Pc: 1,
|
|
Op: vm.STOP,
|
|
Gas: 2,
|
|
GasCost: 3,
|
|
Depth: 1,
|
|
Err: errors.New("boom"),
|
|
},
|
|
want: `{"pc":1,"op":"STOP","gas":2,"gasCost":3,"depth":1,"error":"boom"}`,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
have := string(tt.log.toLegacyJSON())
|
|
if have != tt.want {
|
|
t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStructLoggerLimitRejectsOversizedEntry(t *testing.T) {
|
|
entry := (&StructLog{Op: vm.STOP, RefundCounter: 1337}).toLegacyJSON()
|
|
logger := NewStructLogger(&Config{Limit: len(entry) - 1})
|
|
logger.env = &tracing.VMContext{StateDB: &dummyStatedb{}}
|
|
|
|
logger.OnOpcode(0, byte(vm.STOP), 0, 0, dummyOpContext{}, nil, 0, nil)
|
|
|
|
if len(logger.logs) != 0 {
|
|
t.Fatalf("expected oversized entry to be skipped, got %d logs", len(logger.logs))
|
|
}
|
|
if logger.resultSize != 0 {
|
|
t.Fatalf("expected result size to remain zero, got %d", logger.resultSize)
|
|
}
|
|
}
|
|
|
|
func TestStructLoggerLimitAllowsEntryUpToBoundary(t *testing.T) {
|
|
entry := (&StructLog{Op: vm.STOP, RefundCounter: 1337}).toLegacyJSON()
|
|
logger := NewStructLogger(&Config{Limit: len(entry)})
|
|
logger.env = &tracing.VMContext{StateDB: &dummyStatedb{}}
|
|
|
|
logger.OnOpcode(0, byte(vm.STOP), 0, 0, dummyOpContext{}, nil, 0, nil)
|
|
logger.OnOpcode(0, byte(vm.STOP), 0, 0, dummyOpContext{}, nil, 0, nil)
|
|
|
|
if len(logger.logs) != 1 {
|
|
t.Fatalf("expected exactly one log at the size boundary, got %d", len(logger.logs))
|
|
}
|
|
if logger.resultSize != len(entry) {
|
|
t.Fatalf("expected result size %d, got %d", len(entry), logger.resultSize)
|
|
}
|
|
}
|