From d799c68159c30996f94fa7886b267c041e832478 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 10 Jul 2023 17:53:19 +0200 Subject: [PATCH] nicer printer output --- eth/tracers/printer.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/eth/tracers/printer.go b/eth/tracers/printer.go index 42855de9bd..4c481cce3a 100644 --- a/eth/tracers/printer.go +++ b/eth/tracers/printer.go @@ -1,10 +1,12 @@ package tracers import ( + "encoding/json" "fmt" "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" ) @@ -17,12 +19,12 @@ func NewPrinter() *Printer { // CaptureStart implements the EVMLogger interface to initialize the tracing operation. func (p *Printer) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { - fmt.Printf("CaptureStart: from=%v, to=%v, create=%v, input=%v, gas=%v, value=%v\n", from, to, create, input, gas, value) + fmt.Printf("CaptureStart: from=%v, to=%v, create=%v, input=%s, gas=%v, value=%v\n", from, to, create, hexutil.Bytes(input), gas, value) } // CaptureEnd is called after the call finishes to finalize the tracing. func (p *Printer) CaptureEnd(output []byte, gasUsed uint64, err error) { - fmt.Printf("CaptureEnd: output=%v, gasUsed=%v, err=%v\n", output, gasUsed, err) + fmt.Printf("CaptureEnd: output=%s, gasUsed=%v, err=%v\n", hexutil.Bytes(output), gasUsed, err) } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. @@ -40,22 +42,32 @@ func (p *Printer) CaptureKeccakPreimage(hash common.Hash, data []byte) {} // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct). func (p *Printer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { - fmt.Printf("CaptureEnter: typ=%v, from=%v, to=%v, input=%v, gas=%v, value=%v\n", typ, from, to, input, gas, value) + fmt.Printf("CaptureEnter: typ=%v, from=%v, to=%v, input=%s, gas=%v, value=%v\n", typ, from, to, hexutil.Bytes(input), gas, value) } // CaptureExit is called when EVM exits a scope, even if the scope didn't // execute any code. func (p *Printer) CaptureExit(output []byte, gasUsed uint64, err error) { - fmt.Printf("CaptureExit: output=%v, gasUsed=%v, err=%v\n", output, gasUsed, err) + fmt.Printf("CaptureExit: output=%s, gasUsed=%v, err=%v\n", hexutil.Bytes(output), gasUsed, err) } func (p *Printer) CaptureTxStart(env *vm.EVM, tx *types.Transaction) { - fmt.Printf("CaptureTxStart: tx=%v\n", tx) + buf, err := json.Marshal(tx) + if err != nil { + fmt.Printf("err: %v\n", err) + return + } + fmt.Printf("CaptureTxStart: tx=%s\n", buf) } func (p *Printer) CaptureTxEnd(receipt *types.Receipt) { - fmt.Printf("CaptureTxEnd: receipt=%v\n", receipt) + buf, err := json.Marshal(receipt) + if err != nil { + fmt.Printf("err: %v\n", err) + return + } + fmt.Printf("CaptureTxEnd: receipt=%s\n", buf) } func (p *Printer) OnBlockStart(b *types.Block) { @@ -83,7 +95,7 @@ func (p *Printer) OnNonceChange(a common.Address, prev, new uint64) { } func (p *Printer) OnCodeChange(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte) { - fmt.Printf("OnCodeChange: a=%v, prevCodeHash=%v, prev=%v, codeHash=%v, code=%v\n", a, prevCodeHash, prev, codeHash, code) + fmt.Printf("OnCodeChange: a=%v, prevCodeHash=%v, prev=%s, codeHash=%v, code=%s\n", a, prevCodeHash, hexutil.Bytes(prev), codeHash, hexutil.Bytes(code)) } func (p *Printer) OnStorageChange(a common.Address, k, prev, new common.Hash) { @@ -91,7 +103,12 @@ func (p *Printer) OnStorageChange(a common.Address, k, prev, new common.Hash) { } func (p *Printer) OnLog(l *types.Log) { - fmt.Printf("OnLog: l=%v\n", l) + buf, err := json.Marshal(l) + if err != nil { + fmt.Printf("err: %v\n", err) + return + } + fmt.Printf("OnLog: l=%s\n", buf) } func (p *Printer) OnNewAccount(a common.Address) {