eth/tracers, core/vm: fix tracers output new opcodes for pre-cancun blocks

This commit is contained in:
Cedrick AHOUANGANSI 2025-02-06 02:44:28 +01:00
parent fbb8b85aee
commit 9489a46982
3 changed files with 34 additions and 6 deletions

View file

@ -634,3 +634,11 @@ var stringToOp = map[string]OpCode{
func StringToOp(str string) OpCode {
return stringToOp[str]
}
// IsCancunOpcode specifies if an opcode is added at Cancun fork.
func (op OpCode) IsCancunOpcode() bool {
if op == BLOBHASH || op == BLOBBASEFEE || op == TLOAD || op == TSTORE || op == MCOPY {
return true
}
return false
}

View file

@ -682,7 +682,12 @@ func TestColdAccountAccessCost(t *testing.T) {
Tracer: &tracing.Hooks{
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, isCancun bool, err error) {
// Uncomment to investigate failures:
//t.Logf("%d: %v %d", step, vm.OpCode(op).String(), cost)
// opString := vm.OpCode(op).String()
// Use hex string if Cancun is inactive but the opcode is invalid before Cancun fork
// if !isCancun && vm.OpCode(op).IsCancunOpcode() {
// opString = fmt.Sprintf("0x%x", byte(vm.OpCode(op)))
// }
//t.Logf("%d: %v %d", step, opString, cost)
if step == tc.step {
have = cost
}
@ -936,7 +941,12 @@ func TestDelegatedAccountAccessCost(t *testing.T) {
Tracer: &tracing.Hooks{
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, isCancun bool, err error) {
// Uncomment to investigate failures:
t.Logf("%d: %v %d", step, vm.OpCode(op).String(), cost)
opString := vm.OpCode(op).String()
// Use hex string if Cancun is inactive but the opcode is invalid before Cancun fork
if !isCancun && vm.OpCode(op).IsCancunOpcode() {
opString = fmt.Sprintf("0x%x", byte(vm.OpCode(op)))
}
t.Logf("%d: %v %d", step, opString, cost)
if step == tc.step {
have = cost
}

View file

@ -157,10 +157,15 @@ type structLogLegacy struct {
}
// toLegacyJSON converts the structLog to legacy json-encoded legacy form.
func (s *StructLog) toLegacyJSON() json.RawMessage {
func (s *StructLog) toLegacyJSON(isCancun bool) json.RawMessage {
opString := s.OpName()
// Use hex string if Cancun is inactive but the opcode is invalid before Cancun fork
if !isCancun && s.Op.IsCancunOpcode() {
opString = fmt.Sprintf("0x%x", byte(s.Op))
}
msg := structLogLegacy{
Pc: s.Pc,
Op: s.Op.String(),
Op: opString,
Gas: s.Gas,
GasCost: s.GasCost,
Depth: s.Depth,
@ -315,7 +320,7 @@ func (l *StructLogger) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scope
// create a log
if l.writer == nil {
entry := log.toLegacyJSON()
entry := log.toLegacyJSON(isCancun)
l.resultSize += len(entry)
l.logs = append(l.logs, entry)
return
@ -496,7 +501,12 @@ func (t *mdLogger) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.
return
}
stack := scope.StackData()
fmt.Fprintf(t.out, "| %4d | %10v | %3d |%10v |", pc, vm.OpCode(op).String(),
opString := vm.OpCode(op).String()
// Use hex string if Cancun is inactive but the opcode is invalid before Cancun fork
if !isCancun && vm.OpCode(op).IsCancunOpcode() {
opString = fmt.Sprintf("0x%x", byte(vm.OpCode(op)))
}
fmt.Fprintf(t.out, "| %4d | %10v | %3d |%10v |", pc, opString,
cost, t.env.StateDB.GetRefund())
if !t.cfg.DisableStack {