add hooks for beacon block root processing

This commit is contained in:
Sina Mahmoodi 2023-11-24 19:08:42 +03:30
parent cd8c4c589f
commit 8610f8f68c
5 changed files with 15 additions and 4 deletions

View File

@ -195,6 +195,8 @@ type BlockchainLogger interface {
OnBlockStart(block *types.Block, td *big.Int, finalized *types.Header, safe *types.Header)
OnBlockEnd(err error)
OnGenesisBlock(genesis *types.Block, alloc GenesisAlloc)
OnBeaconBlockRootStart(root common.Hash)
OnBeaconBlockRootEnd()
}
// BlockChain represents the canonical chain given a database with a genesis

View File

@ -100,7 +100,7 @@ func (b *BlockGen) SetParentBeaconRoot(root common.Hash) {
blockContext = NewEVMBlockContext(b.header, b.cm, &b.header.Coinbase)
vmenv = vm.NewEVM(blockContext, vm.TxContext{}, b.statedb, b.cm.config, vm.Config{})
)
ProcessBeaconBlockRoot(root, vmenv, b.statedb)
ProcessBeaconBlockRoot(root, vmenv, b.statedb, nil)
}
// addTx adds a transaction to the generated block. If no coinbase has

View File

@ -79,7 +79,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
signer = types.MakeSigner(p.config, header.Number, header.Time)
)
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb)
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb, p.bc.logger)
}
statedb.PrepareBlock(vm.ActivePrecompiles(rules))
// Iterate over and process the individual transactions
@ -185,7 +185,13 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
// ProcessBeaconBlockRoot applies the EIP-4788 system call to the beacon block root
// contract. This method is exported to be used in tests.
func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *state.StateDB) {
func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *state.StateDB, logger BlockchainLogger) {
if logger != nil {
logger.OnBeaconBlockRootStart(beaconRoot)
defer func() {
logger.OnBeaconBlockRootEnd()
}()
}
// If EIP-4788 is enabled, we need to invoke the beaconroot storage contract with
// the new root
msg := &Message{

View File

@ -58,6 +58,9 @@ func (p *Printer) CaptureExit(output []byte, gasUsed uint64, err error) {
fmt.Printf("CaptureExit: output=%s, gasUsed=%v, err=%v\n", hexutil.Bytes(output), gasUsed, err)
}
func (p *Printer) OnBeaconBlockRootStart(root common.Hash) {}
func (p *Printer) OnBeaconBlockRootEnd() {}
func (p *Printer) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Address) {
buf, err := json.Marshal(tx)
if err != nil {

View File

@ -976,7 +976,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
if header.ParentBeaconRoot != nil {
context := core.NewEVMBlockContext(header, w.chain, nil)
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, w.chainConfig, vm.Config{})
core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state)
core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state, nil)
}
return env, nil
}