Functional hook for consensus engine plugin

This commit is contained in:
philip-morlier 2023-03-24 12:46:45 -07:00
parent 9ee93f1e26
commit 4cfa8704a9
4 changed files with 34 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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