Skip to content

Commit 673007d

Browse files
cdetriofjl
authored andcommitted
core/vm: standard vm traces (#15035)
1 parent d558a59 commit 673007d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

cmd/evm/json_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cos
4040
log := vm.StructLog{
4141
Pc: pc,
4242
Op: op,
43-
Gas: gas + cost,
43+
Gas: gas,
4444
GasCost: cost,
4545
MemorySize: memory.Len(),
4646
Storage: nil,

core/vm/interpreter.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,17 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
137137
// to be uint256. Practically much less so feasible.
138138
pc = uint64(0) // program counter
139139
cost uint64
140+
// copies used by tracer
141+
stackCopy = newstack() // stackCopy needed for Tracer since stack is mutated by 63/64 gas rule
142+
pcCopy uint64 // needed for the deferred Tracer
143+
gasCopy uint64 // for Tracer to log gas remaining before execution
144+
logged bool // deferred Tracer should ignore already logged steps
140145
)
141146
contract.Input = input
142147

143148
defer func() {
144-
if err != nil && in.cfg.Debug {
145-
in.cfg.Tracer.CaptureState(in.evm, pc, op, contract.Gas, cost, mem, stack, contract, in.evm.depth, err)
149+
if err != nil && !logged && in.cfg.Debug {
150+
in.cfg.Tracer.CaptureState(in.evm, pcCopy, op, gasCopy, cost, mem, stackCopy, contract, in.evm.depth, err)
146151
}
147152
}()
148153

@@ -154,6 +159,16 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
154159
// Get the memory location of pc
155160
op = contract.GetOp(pc)
156161

162+
if in.cfg.Debug {
163+
logged = false
164+
pcCopy = uint64(pc)
165+
gasCopy = uint64(contract.Gas)
166+
stackCopy = newstack()
167+
for _, val := range stack.data {
168+
stackCopy.push(val)
169+
}
170+
}
171+
157172
// get the operation from the jump table matching the opcode
158173
operation := in.cfg.JumpTable[op]
159174
if err := in.enforceRestrictions(op, operation, stack); err != nil {
@@ -199,7 +214,8 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
199214
}
200215

201216
if in.cfg.Debug {
202-
in.cfg.Tracer.CaptureState(in.evm, pc, op, contract.Gas, cost, mem, stack, contract, in.evm.depth, err)
217+
in.cfg.Tracer.CaptureState(in.evm, pc, op, gasCopy, cost, mem, stackCopy, contract, in.evm.depth, err)
218+
logged = true
203219
}
204220

205221
// execute the operation

0 commit comments

Comments
 (0)