diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index 0820b20fb1..47e0211b70 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -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 +} diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index e7c69fdeee..59ff28742c 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -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 } diff --git a/eth/tracers/logger/logger.go b/eth/tracers/logger/logger.go index 2bf7ac0694..b36029a85a 100644 --- a/eth/tracers/logger/logger.go +++ b/eth/tracers/logger/logger.go @@ -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 {