Skip to content

Commit 4dc1fb9

Browse files
authored
Merge pull request bnb-chain#3064 from pirapira/limit_struct_logs
core/vm: add limit option to LogConfig
2 parents b8dec94 + bb6115b commit 4dc1fb9

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎core/vm/errors.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ import (
2626
var OutOfGasError = errors.New("Out of gas")
2727
var CodeStoreOutOfGasError = errors.New("Contract creation code storage out of gas")
2828
var DepthError = fmt.Errorf("Max call depth exceeded (%d)", params.CallCreateDepth)
29+
var TraceLimitReachedError = errors.New("The number of logs reached the specified limit")

‎core/vm/logger.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type LogConfig struct {
4242
DisableStack bool // disable stack capture
4343
DisableStorage bool // disable storage capture
4444
FullStorage bool // show full storage (slow)
45+
Limit int // maximum length of output, but zero means unlimited
4546
}
4647

4748
// StructLog is emitted to the Environment each cycle and lists information about the current internal state
@@ -64,7 +65,7 @@ type StructLog struct {
6465
// Note that reference types are actual VM data structures; make copies
6566
// if you need to retain them beyond the current call.
6667
type Tracer interface {
67-
CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error)
68+
CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error
6869
}
6970

7071
// StructLogger is an EVM state logger and implements Tracer.
@@ -93,7 +94,12 @@ func NewStructLogger(cfg *LogConfig) *StructLogger {
9394
// captureState logs a new structured log message and pushes it out to the environment
9495
//
9596
// captureState also tracks SSTORE ops to track dirty values.
96-
func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error) {
97+
func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
98+
// check if already accumulated the specified number of logs
99+
if l.cfg.Limit != 0 && l.cfg.Limit <= len(l.logs) {
100+
return TraceLimitReachedError
101+
}
102+
97103
// initialise new changed values storage container for this contract
98104
// if not present.
99105
if l.changedValues[contract.Address()] == nil {
@@ -152,6 +158,7 @@ func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas,
152158
log := StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, env.Depth(), err}
153159

154160
l.logs = append(l.logs, log)
161+
return nil
155162
}
156163

157164
// StructLogs returns a list of captured log entries

‎core/vm/vm.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
188188
mem.Resize(newMemSize.Uint64())
189189
// Add a log message
190190
if evm.cfg.Debug {
191-
evm.cfg.Tracer.CaptureState(evm.env, pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), nil)
191+
err = evm.cfg.Tracer.CaptureState(evm.env, pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), nil)
192+
if err != nil {
193+
return nil, err
194+
}
192195
}
193196

194197
if opPtr := evm.jumpTable[op]; opPtr.valid {

‎internal/ethapi/tracer.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func wrapError(context string, err error) error {
278278
}
279279

280280
// CaptureState implements the Tracer interface to trace a single step of VM execution
281-
func (jst *JavascriptTracer) CaptureState(env vm.Environment, pc uint64, op vm.OpCode, gas, cost *big.Int, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) {
281+
func (jst *JavascriptTracer) CaptureState(env vm.Environment, pc uint64, op vm.OpCode, gas, cost *big.Int, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
282282
if jst.err == nil {
283283
jst.memory.memory = memory
284284
jst.stack.stack = stack
@@ -301,6 +301,7 @@ func (jst *JavascriptTracer) CaptureState(env vm.Environment, pc uint64, op vm.O
301301
jst.err = wrapError("step", err)
302302
}
303303
}
304+
return nil
304305
}
305306

306307
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error

0 commit comments

Comments
 (0)