Skip to content

Commit

Permalink
Merge pull request ethereum#3064 from pirapira/limit_struct_logs
Browse files Browse the repository at this point in the history
core/vm: add limit option to LogConfig
  • Loading branch information
obscuren authored Oct 31, 2016
2 parents b8dec94 + bb6115b commit 4dc1fb9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ import (
var OutOfGasError = errors.New("Out of gas")
var CodeStoreOutOfGasError = errors.New("Contract creation code storage out of gas")
var DepthError = fmt.Errorf("Max call depth exceeded (%d)", params.CallCreateDepth)
var TraceLimitReachedError = errors.New("The number of logs reached the specified limit")
11 changes: 9 additions & 2 deletions core/vm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type LogConfig struct {
DisableStack bool // disable stack capture
DisableStorage bool // disable storage capture
FullStorage bool // show full storage (slow)
Limit int // maximum length of output, but zero means unlimited
}

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

// StructLogger is an EVM state logger and implements Tracer.
Expand Down Expand Up @@ -93,7 +94,12 @@ func NewStructLogger(cfg *LogConfig) *StructLogger {
// captureState logs a new structured log message and pushes it out to the environment
//
// captureState also tracks SSTORE ops to track dirty values.
func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error) {
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 {
// check if already accumulated the specified number of logs
if l.cfg.Limit != 0 && l.cfg.Limit <= len(l.logs) {
return TraceLimitReachedError
}

// initialise new changed values storage container for this contract
// if not present.
if l.changedValues[contract.Address()] == nil {
Expand Down Expand Up @@ -152,6 +158,7 @@ func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas,
log := StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, env.Depth(), err}

l.logs = append(l.logs, log)
return nil
}

// StructLogs returns a list of captured log entries
Expand Down
5 changes: 4 additions & 1 deletion core/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
mem.Resize(newMemSize.Uint64())
// Add a log message
if evm.cfg.Debug {
evm.cfg.Tracer.CaptureState(evm.env, pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), nil)
err = evm.cfg.Tracer.CaptureState(evm.env, pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), nil)
if err != nil {
return nil, err
}
}

if opPtr := evm.jumpTable[op]; opPtr.valid {
Expand Down
3 changes: 2 additions & 1 deletion internal/ethapi/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func wrapError(context string, err error) error {
}

// CaptureState implements the Tracer interface to trace a single step of VM execution
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) {
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 {
if jst.err == nil {
jst.memory.memory = memory
jst.stack.stack = stack
Expand All @@ -301,6 +301,7 @@ func (jst *JavascriptTracer) CaptureState(env vm.Environment, pc uint64, op vm.O
jst.err = wrapError("step", err)
}
}
return nil
}

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

0 comments on commit 4dc1fb9

Please sign in to comment.