Merge remote-tracking branch 's1na/extended-tracer' into feature/firehose-extended-tracer

This commit is contained in:
Matthieu Vachon 2023-07-11 21:08:34 -04:00
commit 7cc206e494
3 changed files with 43 additions and 28 deletions

View file

@ -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

View file

@ -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)
@ -99,9 +89,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)
}

View file

@ -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) {
@ -66,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())
}
@ -83,7 +91,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 +99,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) {