From f1bd4151c18b9f8f404013255ff1fd46d717a9e9 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 10 Jul 2023 17:32:54 +0200 Subject: [PATCH 1/3] rm extra CaptureTxEnd --- core/state_processor.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/state_processor.go b/core/state_processor.go index 5e1bceb9f7..7a4fd35e16 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -99,9 +99,6 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg var receipt *types.Receipt receipt, err = applyTransaction(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv) - if vmenv.Config.Tracer != nil { - vmenv.Config.Tracer.CaptureTxEnd(receipt) - } if err != nil { return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } From d799c68159c30996f94fa7886b267c041e832478 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 10 Jul 2023 17:53:19 +0200 Subject: [PATCH 2/3] 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) { From add825e9c94dd17ecad5cc523e1834fa7a826f65 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 10 Jul 2023 19:01:55 +0200 Subject: [PATCH 3/3] fix BlockEnd in case of err --- core/blockchain.go | 19 +++++++++++++++++-- core/state_processor.go | 12 +----------- eth/tracers/printer.go | 4 ---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index a3c2fa6484..42e35b3fa6 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -160,7 +160,6 @@ type BlockchainLogger interface { state.StateLogger OnBlockStart(*types.Block) OnBlockEnd(td *big.Int, err error) - OnBlockValidationError(block *types.Block, err error) OnGenesisBlock(*types.Block) } @@ -1774,10 +1773,17 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) // Process block using the parent state as reference point pstart := time.Now() + + if bc.logger != nil { + bc.logger.OnBlockStart(block) + } receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig) if err != nil { bc.reportBlock(block, receipts, err) followupInterrupt.Store(true) + if bc.logger != nil { + bc.logger.OnBlockEnd(new(big.Int), err) + } return it.index, err } ptime := time.Since(pstart) @@ -1787,7 +1793,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) bc.reportBlock(block, receipts, err) followupInterrupt.Store(true) if bc.logger != nil { - bc.logger.OnBlockValidationError(block, err) + bc.logger.OnBlockEnd(new(big.Int), err) } return it.index, err } @@ -1823,6 +1829,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) } followupInterrupt.Store(true) if err != nil { + if bc.logger != nil { + bc.logger.OnBlockEnd(new(big.Int), err) + } return it.index, err } // Update the metrics touched during block commit @@ -1841,6 +1850,12 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) dirty, _ := bc.triedb.Size() stats.report(chain, it.index, dirty, setHead) + if bc.logger != nil { + td := bc.GetTd(block.ParentHash(), block.NumberU64()-1) + td.Add(td, block.Difficulty()) + bc.logger.OnBlockEnd(td, nil) + } + if !setHead { // After merge we expect few side chains. Simply count // all blocks the CL gives us for GC processing time diff --git a/core/state_processor.go b/core/state_processor.go index 7a4fd35e16..a5972f371b 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -68,17 +68,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg gp = new(GasPool).AddGas(block.GasLimit()) err error ) - if p.bc.logger != nil { - p.bc.logger.OnBlockStart(block) - defer func() { - var td *big.Int - if err == nil { - td = p.bc.GetTd(block.ParentHash(), block.NumberU64()-1) - td.Add(td, block.Difficulty()) - } - p.bc.logger.OnBlockEnd(td, err) - }() - } + // Mutate the block and state according to any hard-fork specs if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { misc.ApplyDAOHardFork(statedb) diff --git a/eth/tracers/printer.go b/eth/tracers/printer.go index 4c481cce3a..98c7417831 100644 --- a/eth/tracers/printer.go +++ b/eth/tracers/printer.go @@ -78,10 +78,6 @@ func (p *Printer) OnBlockEnd(td *big.Int, err error) { fmt.Printf("OnBlockEnd: td=%v, err=%v\n", td, err) } -func (p *Printer) OnBlockValidationError(block *types.Block, err error) { - fmt.Printf("OnBlockValidationError: b=%v, err=%v\n", block.NumberU64(), err) -} - func (p *Printer) OnGenesisBlock(b *types.Block) { fmt.Printf("OnGenesisBlock: b=%v\n", b.NumberU64()) }