From 7434ccb8c861e3f25e94a9fe2ebb1393633a7a66 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 12 Jun 2023 09:52:46 -0700 Subject: [PATCH 1/3] Initial commit of stand alone consensus engine work. --- cmd/geth/main.go | 3 +- cmd/geth/plugin_hooks.go | 18 + eth/ethconfig/config.go | 6 + eth/ethconfig/plugin_hooks.go | 44 ++ plugins/wrappers/backendwrapper/dbwrapper.go | 4 + plugins/wrappers/engine/enginewrapper.go | 468 +++++++++++++++++++ plugins/wrappers/wrappers.go | 6 + 7 files changed, 548 insertions(+), 1 deletion(-) create mode 100644 eth/ethconfig/plugin_hooks.go create mode 100644 plugins/wrappers/engine/enginewrapper.go diff --git a/cmd/geth/main.go b/cmd/geth/main.go index b750c55b2..40e6b6cfe 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -355,8 +355,9 @@ func geth(ctx *cli.Context) error { defer stack.Close() defer pluginsOnShutdown() stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend)) - //end PluGeth code injection startNode(ctx, stack, backend, false) + pluginHookTester() + //end PluGeth code injection stack.Wait() return nil } diff --git a/cmd/geth/plugin_hooks.go b/cmd/geth/plugin_hooks.go index 80187a028..73ff14884 100644 --- a/cmd/geth/plugin_hooks.go +++ b/cmd/geth/plugin_hooks.go @@ -102,3 +102,21 @@ func pluginsOnShutdown() { } OnShutdown(plugins.DefaultPluginLoader) } + +func HookTester(pl *plugins.PluginLoader) { + fnList := pl.Lookup("HookTester", func(item interface{}) bool { + _, ok := item.(func()) + return ok + }) + for _, fni := range fnList { + fni.(func())() + } +} + +func pluginHookTester() { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting HookTester, but default PluginLoader has not been initialized") + return + } + HookTester(plugins.DefaultPluginLoader) +} \ No newline at end of file diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index db686c5d0..5f421825d 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -212,6 +212,12 @@ type Config struct { // CreateConsensusEngine creates a consensus engine for the given chain configuration. 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 + //begin PluGeth code injection + if engine := pluginGetEngine(stack, notify, noverify, db); engine != nil { + log.Debug("returning plugin consensus engine") + return engine + } + //end PluGeth code injection var engine consensus.Engine if cliqueConfig != nil { engine = clique.New(cliqueConfig, db) diff --git a/eth/ethconfig/plugin_hooks.go b/eth/ethconfig/plugin_hooks.go new file mode 100644 index 000000000..8f09f6069 --- /dev/null +++ b/eth/ethconfig/plugin_hooks.go @@ -0,0 +1,44 @@ +package ethconfig + +import ( + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/plugins" + "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/openrelayxyz/plugeth-utils/core" + "github.com/openrelayxyz/plugeth-utils/restricted" + + "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/node" + + pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" +) + + + +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) + return ok + }) + for _, fni := range fnList { + 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 nil +} + +func pluginGetEngine(stack *node.Node, notify []string, noverify bool, db ethdb.Database) consensus.Engine { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting GetEngine, but default PluginLoader has not been initialized") + return nil + } + return PluginGetEngine(plugins.DefaultPluginLoader, stack, notify, noverify, db) +} \ No newline at end of file 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 new file mode 100644 index 000000000..f8aacc60d --- /dev/null +++ b/plugins/wrappers/engine/enginewrapper.go @@ -0,0 +1,468 @@ +package engine + +import ( + "fmt" + "math/big" + "reflect" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" + // "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/plugins/wrappers" + "github.com/openrelayxyz/plugeth-utils/core" + ptypes "github.com/openrelayxyz/plugeth-utils/restricted/types" + pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" + pparams "github.com/openrelayxyz/plugeth-utils/restricted/params" +) + + +func gethToUtilsHeader(header *types.Header) *ptypes.Header { + if header == nil { return nil } + return &ptypes.Header{ + ParentHash: core.Hash(header.ParentHash), + UncleHash: core.Hash(header.UncleHash), + Coinbase: core.Address(header.Coinbase), + Root: core.Hash(header.Root), + TxHash: core.Hash(header.TxHash), + ReceiptHash: core.Hash(header.ReceiptHash), + Bloom: ptypes.Bloom(header.Bloom), + Difficulty: header.Difficulty, + Number: header.Number, + GasLimit: header.GasLimit, + GasUsed: header.GasUsed, + Time: header.Time, + Extra: header.Extra, + MixDigest: core.Hash(header.MixDigest), + Nonce: ptypes.BlockNonce(header.Nonce), + BaseFee: header.BaseFee, + WithdrawalsHash: (*core.Hash)(header.WithdrawalsHash), + } +} +func utilsToGethHeader(header *ptypes.Header) *types.Header { + if header == nil { return nil } + return &types.Header{ + ParentHash: common.Hash(header.ParentHash), + UncleHash: common.Hash(header.UncleHash), + Coinbase: common.Address(header.Coinbase), + Root: common.Hash(header.Root), + TxHash: common.Hash(header.TxHash), + ReceiptHash: common.Hash(header.ReceiptHash), + Bloom: types.Bloom(header.Bloom), + Difficulty: header.Difficulty, + Number: header.Number, + GasLimit: header.GasLimit, + GasUsed: header.GasUsed, + Time: header.Time, + Extra: header.Extra, + MixDigest: common.Hash(header.MixDigest), + Nonce: types.BlockNonce(header.Nonce), + BaseFee: header.BaseFee, + WithdrawalsHash: (*common.Hash)(header.WithdrawalsHash), + } +} + +func gethToUtilsTransactions(transactions []*types.Transaction) []*ptypes.Transaction { + if transactions == nil { return nil } + txs := make([]*ptypes.Transaction, len(transactions)) + for i, tx := range transactions { + bin, err := tx.MarshalBinary() + if err != nil { panic (err) } + txs[i] = &ptypes.Transaction{} + txs[i].UnmarshalBinary(bin) + } + return txs +} + +func gethToUtilsHeaders(headers []*types.Header) []*ptypes.Header { + if headers == nil { return nil } + pheaders := make([]*ptypes.Header, len(headers)) + for i, header := range headers { + pheaders[i] = gethToUtilsHeader(header) + } + return pheaders +} + +func gethToUtilsReceipts(receipts []*types.Receipt) []*ptypes.Receipt { + if receipts == nil { return nil } + preceipts := make([]*ptypes.Receipt, len(receipts)) + for i, receipt := range receipts { + preceipts[i] = &ptypes.Receipt{ + Type: receipt.Type, + PostState: receipt.PostState, + Status: receipt.Status, + CumulativeGasUsed: receipt.CumulativeGasUsed, + Bloom: ptypes.Bloom(receipt.Bloom), + Logs: gethToUtilsLogs(receipt.Logs), + TxHash: core.Hash(receipt.TxHash), + ContractAddress: core.Address(receipt.ContractAddress), + GasUsed: receipt.GasUsed, + BlockHash: core.Hash(receipt.BlockHash), + BlockNumber: receipt.BlockNumber, + TransactionIndex: receipt.TransactionIndex, + } + } + return preceipts +} + +func gethToUtilsWithdrawals(withdrawals []*types.Withdrawal) []*ptypes.Withdrawal { + if withdrawals == nil { return nil } + pwithdrawals := make([]*ptypes.Withdrawal, len(withdrawals)) + for i, withdrawal := range withdrawals { + pwithdrawals[i] = &ptypes.Withdrawal{ + Index: withdrawal.Index, + Validator: withdrawal.Validator, + Address: core.Address(withdrawal.Address), + Amount: withdrawal.Amount, + } + } + return pwithdrawals +} + +func gethToUtilsBlock(block *types.Block) *ptypes.Block { + if block == nil { return nil } + return ptypes.NewBlockWithHeader(gethToUtilsHeader(block.Header())).WithBody(gethToUtilsTransactions(block.Transactions()), gethToUtilsHeaders(block.Uncles())).WithWithdrawals(gethToUtilsWithdrawals(block.Withdrawals())) + + // NewBlockWithWithdrawals( + // gethToUtilsHeader(block.Header()), + // gethToUtilsTransactions(block.Transactions()), + // gethToUtilsHeaders(block.Uncles()), + // gethToUtilsReceipts(block.Receipts()), + // gethToUtilsWithdrawals(block.Withdrawals()), + // &hasherWrapper{trie.NewStackTrie(nil)}, + // ) +} + +func utilsToGethBlock(block *ptypes.Block) *types.Block { + if block == nil { return nil } + return types.NewBlockWithHeader(utilsToGethHeader(block.Header())).WithBody(utilsToGethTransactions(block.Transactions()), utilsToGethHeaders(block.Uncles())).WithWithdrawals(utilsToGethWithdrawals(block.Withdrawals())) + // return types.NewBlockWithWithdrawals( + // utilsToGethHeader(block.Header()), + // utilsToGethTransactions(block.Transactions()), + // utilsToGethHeaders(block.Uncles()), + // utilsToGethReceipts(block.Receipts()), + // utilsToGethWithdrawals(block.Withdrawals()), + // trie.NewStackTrie(nil), + // ) +} + +func utilsToGethHeaders(headers []*ptypes.Header) []*types.Header { + if headers == nil { return nil } + pheaders := make([]*types.Header, len(headers)) + for i, header := range headers { + pheaders[i] = utilsToGethHeader(header) + } + return pheaders +} + +func utilsToGethTransactions(transactions []*ptypes.Transaction) []*types.Transaction { + if transactions == nil { return nil } + txs := make([]*types.Transaction, len(transactions)) + for i, tx := range transactions { + bin, err := tx.MarshalBinary() + if err != nil { panic (err) } + txs[i] = &types.Transaction{} + txs[i].UnmarshalBinary(bin) + } + return txs +} +// func utilsToGethReceipts(receipts []*ptypes.Receipt) []*types.Receipt { +// if receipts == nil { return nil } +// preceipts := make([]*types.Receipt, len(receipts)) +// for i, receipt := range receipts { +// preceipts[i] = &types.Receipt{ +// Type: receipt.Type, +// PostState: receipt.PostState, +// Status: receipt.Status, +// CumulativeGasUsed: receipt.CumulativeGasUsed, +// Bloom: types.Bloom(receipt.Bloom), +// Logs: utilsToGethLogs(receipt.Logs), +// TxHash: common.Hash(receipt.TxHash), +// ContractAddress: common.Address(receipt.ContractAddress), +// GasUsed: receipt.GasUsed, +// BlockHash: common.Hash(receipt.BlockHash), +// BlockNumber: receipt.BlockNumber, +// TransactionIndex: receipt.TransactionIndex, +// } +// } +// return preceipts +// } +func utilsToGethWithdrawals(withdrawals []*ptypes.Withdrawal) []*types.Withdrawal { + if withdrawals == nil { return nil } + pwithdrawals := make([]*types.Withdrawal, len(withdrawals)) + for i, withdrawal := range withdrawals { + pwithdrawals[i] = &types.Withdrawal{ + Index: withdrawal.Index, + Validator: withdrawal.Validator, + Address: common.Address(withdrawal.Address), + Amount: withdrawal.Amount, + } + } + return pwithdrawals +} + +func gethToUtilsBlockChan(ch chan<- *types.Block) chan<- *ptypes.Block { + pchan := make(chan *ptypes.Block) + go func() { + for block := range pchan { + ch <- utilsToGethBlock(block) + } + // close(ch) + }() + return pchan +} + +func gethToUtilsLog(logRecord *types.Log) *ptypes.Log { + if logRecord == nil { return nil } + topics := make([]core.Hash, len(logRecord.Topics)) + for i, t := range logRecord.Topics { + topics[i] = core.Hash(t) + } + return &ptypes.Log{ + Address: core.Address(logRecord.Address), + Topics: topics, + Data: logRecord.Data, + BlockNumber: logRecord.BlockNumber, + TxHash: core.Hash(logRecord.TxHash), + TxIndex: logRecord.TxIndex, + BlockHash: core.Hash(logRecord.BlockHash), + Index: logRecord.Index, + Removed: logRecord.Removed, + } +} + +func utilsToGethLog(logRecord *ptypes.Log) *types.Log { + if logRecord == nil { return nil } + topics := make([]common.Hash, len(logRecord.Topics)) + for i, t := range logRecord.Topics { + topics[i] = common.Hash(t) + } + return &types.Log{ + Address: common.Address(logRecord.Address), + Topics: topics, + Data: logRecord.Data, + BlockNumber: logRecord.BlockNumber, + TxHash: common.Hash(logRecord.TxHash), + TxIndex: logRecord.TxIndex, + BlockHash: common.Hash(logRecord.BlockHash), + Index: logRecord.Index, + Removed: logRecord.Removed, + } +} + +func gethToUtilsLogs(logs []*types.Log) []*ptypes.Log { + result := make([]*ptypes.Log, len(logs)) + for i, logRecord := range logs { + result[i] = gethToUtilsLog(logRecord) + } + return result +} + +func utilsToGethLogs(logs []*ptypes.Log) []*types.Log { + result := make([]*types.Log, len(logs)) + for i, logRecord := range logs { + result[i] = utilsToGethLog(logRecord) + } + return result +} + + +func convertAndSet(a, b reflect.Value) (err error) { + defer func() { + if recover() != nil { + fmt.Errorf("error converting: %v", err.Error()) + } + }() + a.Set(b.Convert(a.Type())) + return nil +} + +func gethToUtilsConfig(gcfg *params.ChainConfig) *pparams.ChainConfig { + cfg := &pparams.ChainConfig{} + nval := reflect.ValueOf(gcfg) + ntype := nval.Elem().Type() + lval := reflect.ValueOf(cfg) + for i := 0; i < nval.Elem().NumField(); i++ { + field := ntype.Field(i) + v := nval.Elem().FieldByName(field.Name) + lv := lval.Elem().FieldByName(field.Name) + log.Info("Checking value for", "field", field.Name) + if lv.Kind() != reflect.Invalid { + // If core.ChainConfig doesn't have this field, skip it. + if v.Type() == lv.Type() && lv.CanSet() { + lv.Set(v) + } else { + convertAndSet(lv, v) + } + } + } + return cfg +} + +type WrappedHeaderReader struct { + chr consensus.ChainHeaderReader + cfg *pparams.ChainConfig +} + +func (whr *WrappedHeaderReader) Config() *pparams.ChainConfig { + if whr.cfg == nil { + whr.cfg = gethToUtilsConfig(whr.chr.Config()) + } + return whr.cfg +} + +// CurrentHeader retrieves the current header from the local chain. +func (whr *WrappedHeaderReader) CurrentHeader() *ptypes.Header { + return gethToUtilsHeader(whr.chr.CurrentHeader()) +} + +// GetHeader retrieves a block header from the database by hash and number. +func (whr *WrappedHeaderReader) GetHeader(hash core.Hash, number uint64) *ptypes.Header { + return gethToUtilsHeader(whr.chr.GetHeader(common.Hash(hash), number)) +} + +// GetHeaderByNumber retrieves a block header from the database by number. +func (whr *WrappedHeaderReader) GetHeaderByNumber(number uint64) *ptypes.Header { + return gethToUtilsHeader(whr.chr.GetHeaderByNumber(number)) +} + +// GetHeaderByHash retrieves a block header from the database by its hash. +func (whr *WrappedHeaderReader) GetHeaderByHash(hash core.Hash) *ptypes.Header { + return gethToUtilsHeader(whr.chr.GetHeaderByHash(common.Hash(hash))) +} + +// GetTd retrieves the total difficulty from the database by hash and number. +func (whr *WrappedHeaderReader) GetTd(hash core.Hash, number uint64) *big.Int { + return whr.chr.GetTd(common.Hash(hash), number) +} + + +type WrappedChainReader struct { + chr consensus.ChainReader + cfg *pparams.ChainConfig +} + +func (whr *WrappedChainReader) Config() *pparams.ChainConfig { + // We're using the reflect library to copy data from params.ChainConfig to + // pparams.ChainConfig, so this function shouldn't need to be touched for + // simple changes to ChainConfig (though pparams.ChainConfig may need to be + // updated). Note that this probably won't carry over consensus engine data. + if whr.cfg == nil { + whr.cfg = gethToUtilsConfig(whr.chr.Config()) + } + return whr.cfg +} + +// CurrentHeader retrieves the current header from the local chain. +func (whr *WrappedChainReader) CurrentHeader() *ptypes.Header { + return gethToUtilsHeader(whr.chr.CurrentHeader()) +} + +// GetHeader retrieves a block header from the database by hash and number. +func (whr *WrappedChainReader) GetHeader(hash core.Hash, number uint64) *ptypes.Header { + return gethToUtilsHeader(whr.chr.GetHeader(common.Hash(hash), number)) +} + +// GetHeaderByNumber retrieves a block header from the database by number. +func (whr *WrappedChainReader) GetHeaderByNumber(number uint64) *ptypes.Header { + return gethToUtilsHeader(whr.chr.GetHeaderByNumber(number)) +} + +// GetHeaderByHash retrieves a block header from the database by its hash. +func (whr *WrappedChainReader) GetHeaderByHash(hash core.Hash) *ptypes.Header { + return gethToUtilsHeader(whr.chr.GetHeaderByHash(common.Hash(hash))) +} + +// GetTd retrieves the total difficulty from the database by hash and number. +func (whr *WrappedChainReader) GetTd(hash core.Hash, number uint64) *big.Int { + return whr.chr.GetTd(common.Hash(hash), number) +} + +func (whr *WrappedChainReader) GetBlock(hash core.Hash, number uint64) *ptypes.Block { + return gethToUtilsBlock(whr.chr.GetBlock(common.Hash(hash), number)) +} + +type hasherWrapper struct { + th types.TrieHasher +} +func (hw *hasherWrapper) Reset() { + hw.th.Reset() +} +func (hw *hasherWrapper) Update(a, b []byte) { + hw.th.Update(a, b) +} +func (hw *hasherWrapper) Hash() core.Hash { + return core.Hash(hw.th.Hash()) +} + +type engineWrapper struct { + engine pconsensus.Engine +} + +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 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) +} +func (ew *engineWrapper) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { + pheaders := make([]*ptypes.Header, len(headers)) + for i, header := range headers { + pheaders[i] = gethToUtilsHeader(header) + } + return ew.engine.VerifyHeaders(&WrappedHeaderReader{chain, nil}, pheaders, seals) +} +func (ew *engineWrapper) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { + return ew.engine.VerifyUncles(&WrappedChainReader{chain, nil}, gethToUtilsBlock(block)) +} +func (ew *engineWrapper) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { + uHeader := gethToUtilsHeader(header) + if err := ew.engine.Prepare(&WrappedHeaderReader{chain, nil}, uHeader); err != nil { + return err + } + // header.Difficulty = uHeader.Difficulty + *header = *utilsToGethHeader(uHeader) + 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)) +} +func (ew *engineWrapper) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) { + block, err := ew.engine.FinalizeAndAssemble(&WrappedHeaderReader{chain, nil}, gethToUtilsHeader(header), wrappers.NewWrappedStateDB(state), gethToUtilsTransactions(txs), gethToUtilsHeaders(uncles), gethToUtilsReceipts(receipts), gethToUtilsWithdrawals(withdrawals)) + return utilsToGethBlock(block), err +} +func (ew *engineWrapper) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { + return ew.engine.Seal(&WrappedHeaderReader{chain, nil}, gethToUtilsBlock(block), gethToUtilsBlockChan(results), stop) +} +func (ew *engineWrapper) SealHash(header *types.Header) common.Hash { + return common.Hash(ew.engine.SealHash(gethToUtilsHeader(header))) +} +func (ew *engineWrapper) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { + return ew.engine.CalcDifficulty(&WrappedHeaderReader{chain, nil}, time, gethToUtilsHeader(parent)) +} +func (ew *engineWrapper) APIs(chain consensus.ChainHeaderReader) []rpc.API { + papis := ew.engine.APIs(&WrappedHeaderReader{chain, nil}) + apis := make([]rpc.API, len(papis)) + for i, api := range papis { + apis[i] = rpc.API{ + Namespace: api.Namespace, + Version: api.Version, + Service: api.Service, + Public: api.Public, + } + } + return apis +} +func (ew *engineWrapper) Close() error { + return ew.engine.Close() +} diff --git a/plugins/wrappers/wrappers.go b/plugins/wrappers/wrappers.go index 5458cd4a5..e2ca48684 100644 --- a/plugins/wrappers/wrappers.go +++ b/plugins/wrappers/wrappers.go @@ -194,6 +194,12 @@ func (w *WrappedStateDB) SlotInAccessList(addr core.Address, slot core.Hash) (ad return w.s.SlotInAccessList(common.Address(addr), common.Hash(slot)) } +// IntermediateRoot(deleteEmptyObjects bool) common.Hash +func (w *WrappedStateDB) IntermediateRoot(deleteEmptyObjects bool) core.Hash { + return core.Hash(w.s.IntermediateRoot(deleteEmptyObjects)) +} + + type Node struct { n *node.Node } From d8d864768ad645ef72c89fa57b4e89ccc11b581b Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 12 Jun 2023 13:37:29 -0700 Subject: [PATCH 2/3] Updated mod and sum to reflect v1.2.0 Also changed the injection and hook from hooktester to blockchain(). --- cmd/geth/main.go | 2 +- cmd/geth/plugin_hooks.go | 10 +++++----- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 40e6b6cfe..71e88af2c 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -356,7 +356,7 @@ func geth(ctx *cli.Context) error { defer pluginsOnShutdown() stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend)) startNode(ctx, stack, backend, false) - pluginHookTester() + pluginBlockChain() //end PluGeth code injection stack.Wait() return nil diff --git a/cmd/geth/plugin_hooks.go b/cmd/geth/plugin_hooks.go index 73ff14884..2a1053c3a 100644 --- a/cmd/geth/plugin_hooks.go +++ b/cmd/geth/plugin_hooks.go @@ -103,8 +103,8 @@ func pluginsOnShutdown() { OnShutdown(plugins.DefaultPluginLoader) } -func HookTester(pl *plugins.PluginLoader) { - fnList := pl.Lookup("HookTester", func(item interface{}) bool { +func BlockChain(pl *plugins.PluginLoader) { + fnList := pl.Lookup("BlockChain", func(item interface{}) bool { _, ok := item.(func()) return ok }) @@ -113,10 +113,10 @@ func HookTester(pl *plugins.PluginLoader) { } } -func pluginHookTester() { +func pluginBlockChain() { if plugins.DefaultPluginLoader == nil { - log.Warn("Attempting HookTester, but default PluginLoader has not been initialized") + log.Warn("Attempting BlockChain, but default PluginLoader has not been initialized") return } - HookTester(plugins.DefaultPluginLoader) + BlockChain(plugins.DefaultPluginLoader) } \ No newline at end of file diff --git a/go.mod b/go.mod index 71c374bd0..d02b0b15a 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,7 @@ require ( github.com/mattn/go-isatty v0.0.16 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/olekukonko/tablewriter v0.0.5 - github.com/openrelayxyz/plugeth-utils v1.1.0 + github.com/openrelayxyz/plugeth-utils v1.2.0 github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 github.com/rs/cors v1.7.0 github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible diff --git a/go.sum b/go.sum index 225d0091a..5b2df071a 100644 --- a/go.sum +++ b/go.sum @@ -349,8 +349,8 @@ github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= -github.com/openrelayxyz/plugeth-utils v1.1.0 h1:pK02TCFT32G/o/GNgAF+i3CihWfa/G71i0/vB9PHG3k= -github.com/openrelayxyz/plugeth-utils v1.1.0/go.mod h1:W1Hwwhv04MCuNCcI6a62/kL6eWbH7OO+KvpBEyQTgIs= +github.com/openrelayxyz/plugeth-utils v1.2.0 h1:H8r4P9XER1dRvFzCBK3AUjfjN6hXQkxGGpIlhc6nXFo= +github.com/openrelayxyz/plugeth-utils v1.2.0/go.mod h1:W1Hwwhv04MCuNCcI6a62/kL6eWbH7OO+KvpBEyQTgIs= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= From 80956ac72322a7e2ee048df4fc49bbf40f38322b Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 12 Jun 2023 15:08:37 -0700 Subject: [PATCH 3/3] removed unnecessary comments from wrappers/engine.go --- plugins/wrappers/engine/enginewrapper.go | 42 +----------------------- 1 file changed, 1 insertion(+), 41 deletions(-) diff --git a/plugins/wrappers/engine/enginewrapper.go b/plugins/wrappers/engine/enginewrapper.go index f8aacc60d..2b95d2da5 100644 --- a/plugins/wrappers/engine/enginewrapper.go +++ b/plugins/wrappers/engine/enginewrapper.go @@ -9,7 +9,6 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" - // "github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/plugins/wrappers" @@ -125,28 +124,11 @@ func gethToUtilsWithdrawals(withdrawals []*types.Withdrawal) []*ptypes.Withdrawa func gethToUtilsBlock(block *types.Block) *ptypes.Block { if block == nil { return nil } return ptypes.NewBlockWithHeader(gethToUtilsHeader(block.Header())).WithBody(gethToUtilsTransactions(block.Transactions()), gethToUtilsHeaders(block.Uncles())).WithWithdrawals(gethToUtilsWithdrawals(block.Withdrawals())) - - // NewBlockWithWithdrawals( - // gethToUtilsHeader(block.Header()), - // gethToUtilsTransactions(block.Transactions()), - // gethToUtilsHeaders(block.Uncles()), - // gethToUtilsReceipts(block.Receipts()), - // gethToUtilsWithdrawals(block.Withdrawals()), - // &hasherWrapper{trie.NewStackTrie(nil)}, - // ) } func utilsToGethBlock(block *ptypes.Block) *types.Block { if block == nil { return nil } return types.NewBlockWithHeader(utilsToGethHeader(block.Header())).WithBody(utilsToGethTransactions(block.Transactions()), utilsToGethHeaders(block.Uncles())).WithWithdrawals(utilsToGethWithdrawals(block.Withdrawals())) - // return types.NewBlockWithWithdrawals( - // utilsToGethHeader(block.Header()), - // utilsToGethTransactions(block.Transactions()), - // utilsToGethHeaders(block.Uncles()), - // utilsToGethReceipts(block.Receipts()), - // utilsToGethWithdrawals(block.Withdrawals()), - // trie.NewStackTrie(nil), - // ) } func utilsToGethHeaders(headers []*ptypes.Header) []*types.Header { @@ -169,27 +151,7 @@ func utilsToGethTransactions(transactions []*ptypes.Transaction) []*types.Transa } return txs } -// func utilsToGethReceipts(receipts []*ptypes.Receipt) []*types.Receipt { -// if receipts == nil { return nil } -// preceipts := make([]*types.Receipt, len(receipts)) -// for i, receipt := range receipts { -// preceipts[i] = &types.Receipt{ -// Type: receipt.Type, -// PostState: receipt.PostState, -// Status: receipt.Status, -// CumulativeGasUsed: receipt.CumulativeGasUsed, -// Bloom: types.Bloom(receipt.Bloom), -// Logs: utilsToGethLogs(receipt.Logs), -// TxHash: common.Hash(receipt.TxHash), -// ContractAddress: common.Address(receipt.ContractAddress), -// GasUsed: receipt.GasUsed, -// BlockHash: common.Hash(receipt.BlockHash), -// BlockNumber: receipt.BlockNumber, -// TransactionIndex: receipt.TransactionIndex, -// } -// } -// return preceipts -// } + func utilsToGethWithdrawals(withdrawals []*ptypes.Withdrawal) []*types.Withdrawal { if withdrawals == nil { return nil } pwithdrawals := make([]*types.Withdrawal, len(withdrawals)) @@ -210,7 +172,6 @@ func gethToUtilsBlockChan(ch chan<- *types.Block) chan<- *ptypes.Block { for block := range pchan { ch <- utilsToGethBlock(block) } - // close(ch) }() return pchan } @@ -430,7 +391,6 @@ func (ew *engineWrapper) Prepare(chain consensus.ChainHeaderReader, header *type if err := ew.engine.Prepare(&WrappedHeaderReader{chain, nil}, uHeader); err != nil { return err } - // header.Difficulty = uHeader.Difficulty *header = *utilsToGethHeader(uHeader) return nil }