From 4cfa8704a9aa0cee22317461f618275fba3ff91f Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Fri, 24 Mar 2023 12:46:45 -0700 Subject: [PATCH] Functional hook for consensus engine plugin --- eth/ethconfig/config.go | 1 + eth/ethconfig/plugin_hooks.go | 26 +++++++++----------- plugins/wrappers/backendwrapper/dbwrapper.go | 4 +++ plugins/wrappers/engine/enginewrapper.go | 23 ++++++++++++----- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 28358fa34..f4c65876f 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -213,6 +213,7 @@ type Config struct { func CreateConsensusEngine(stack *node.Node, ethashConfig *ethash.Config, cliqueConfig *params.CliqueConfig, notify []string, noverify bool, db ethdb.Database) consensus.Engine { // If proof-of-authority is requested, set it up if engine := pluginGetEngine(stack, notify, noverify, db); engine != nil { + log.Error("returning plugin consensus engine") return engine } var engine consensus.Engine diff --git a/eth/ethconfig/plugin_hooks.go b/eth/ethconfig/plugin_hooks.go index c4b9c0b26..75b28c986 100644 --- a/eth/ethconfig/plugin_hooks.go +++ b/eth/ethconfig/plugin_hooks.go @@ -4,7 +4,9 @@ import ( "github.com/ethereum/go-ethereum/log" // "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/plugins" - // "github.com/ethereum/go-ethereum/plugins/wrappers" + "github.com/ethereum/go-ethereum/plugins/wrappers" + wengine "github.com/ethereum/go-ethereum/plugins/wrappers/engine" + "github.com/ethereum/go-ethereum/plugins/wrappers/backendwrapper" // "github.com/ethereum/go-ethereum/rpc" "github.com/openrelayxyz/plugeth-utils/core" "github.com/openrelayxyz/plugeth-utils/restricted" @@ -25,30 +27,26 @@ import ( // "github.com/ethereum/go-ethereum/params" pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" - pparams "github.com/openrelayxyz/plugeth-utils/restricted/params" + // pparams "github.com/openrelayxyz/plugeth-utils/restricted/params" ) -// stack *node.Node, ethashConfig *ethash.Config, cliqueConfig *params.CliqueConfig, notify []string, noverify bool, db ethdb.Database) consensus.Engine -func engineTranslate(engine pconsensus.Engine) consensus.Engine { - result consensus.Engine{ - - } - - return result -} func PluginGetEngine(pl *plugins.PluginLoader, stack *node.Node, notify []string, noverify bool, db ethdb.Database) consensus.Engine { fnList := pl.Lookup("CreateEngine", func(item interface{}) bool { - _, ok := item.(func(*core.Node, []string, bool, restricted.Database) pconsensus.Engine) + _, ok := item.(func(core.Node, []string, bool, restricted.Database) pconsensus.Engine) return ok }) for _, fni := range fnList { - if fn, ok := fni.(func(*core.Node, []string, bool, restircted.Database)); ok { - engine := fn(wrappers.NewNode(stack), notify, noverify, db) // modify + if fn, ok := fni.(func(core.Node, []string, bool, restricted.Database) pconsensus.Engine); ok { + if engine := fn(wrappers.NewNode(stack), notify, noverify, backendwrapper.NewDB(db)); engine != nil { + wrappedEngine := wengine.NewWrappedEngine(engine) + return wrappedEngine + } + } } - return engineTranslate(engine) + return nil } func pluginGetEngine(stack *node.Node, notify []string, noverify bool, db ethdb.Database) consensus.Engine { diff --git a/plugins/wrappers/backendwrapper/dbwrapper.go b/plugins/wrappers/backendwrapper/dbwrapper.go index 30b0d7aa5..8af502034 100644 --- a/plugins/wrappers/backendwrapper/dbwrapper.go +++ b/plugins/wrappers/backendwrapper/dbwrapper.go @@ -11,6 +11,10 @@ type dbWrapper struct { db ethdb.Database } +func NewDB(d ethdb.Database) restricted.Database { + return &dbWrapper{d} +} + func (d *dbWrapper) Has(key []byte) (bool, error) { return d.db.Has(key) } func (d *dbWrapper) Get(key []byte) ([]byte, error) { return d.db.Get(key) } func (d *dbWrapper) Put(key []byte, value []byte) error { return d.db.Put(key, value) } diff --git a/plugins/wrappers/engine/enginewrapper.go b/plugins/wrappers/engine/enginewrapper.go index dada9112d..40bc297b4 100644 --- a/plugins/wrappers/engine/enginewrapper.go +++ b/plugins/wrappers/engine/enginewrapper.go @@ -17,8 +17,6 @@ import ( ptypes "github.com/openrelayxyz/plugeth-utils/restricted/types" pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" pparams "github.com/openrelayxyz/plugeth-utils/restricted/params" - - philip "github.com/philip-morlier/butttruck" ) @@ -212,7 +210,7 @@ func gethToUtilsBlockChan(ch chan<- *types.Block) chan<- *ptypes.Block { for block := range pchan { ch <- utilsToGethBlock(block) } - close(ch) + // close(ch) }() return pchan } @@ -404,9 +402,15 @@ type engineWrapper struct { engine pconsensus.Engine } -func (ew *engineWrapper) Author(header *types.Header) (core.Address, error) { +func NewWrappedEngine(e pconsensus.Engine) consensus.Engine { + return &engineWrapper { + engine: e, + } +} + +func (ew *engineWrapper) Author(header *types.Header) (common.Address, error) { addr, err := ew.engine.Author(gethToUtilsHeader(header)) - return core.Address(addr), err + return common.Address(addr), err } func (ew *engineWrapper) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error { return ew.engine.VerifyHeader(&WrappedHeaderReader{chain, nil}, gethToUtilsHeader(header), seal) @@ -422,7 +426,14 @@ func (ew *engineWrapper) VerifyUncles(chain consensus.ChainReader, block *types. return ew.engine.VerifyUncles(&WrappedChainReader{chain, nil}, gethToUtilsBlock(block)) } func (ew *engineWrapper) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { - return ew.engine.Prepare(&WrappedHeaderReader{chain, nil}, gethToUtilsHeader(header)) + uHeader := gethToUtilsHeader(header) + if err := ew.engine.Prepare(&WrappedHeaderReader{chain, nil}, uHeader); err != nil { + return err + } + // header.Difficulty = uHeader.Difficulty + *header = *utilsToGethHeader(uHeader) + log.Error("header logs", "header D", header.Difficulty, "U head D", uHeader.Difficulty) + return nil } func (ew *engineWrapper) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal) { ew.engine.Finalize(&WrappedHeaderReader{chain, nil}, gethToUtilsHeader(header), wrappers.NewWrappedStateDB(state), gethToUtilsTransactions(txs), gethToUtilsHeaders(uncles), gethToUtilsWithdrawals(withdrawals))