From 9aba661554aa3d26f529e7184619a614b4afb82f Mon Sep 17 00:00:00 2001 From: Austin Roberts Date: Thu, 23 Mar 2023 16:37:16 -0500 Subject: [PATCH 01/19] Updates for consensus engine injection --- eth/ethconfig/config.go | 4 + plugins/wrappers/engine/enginewrapper.go | 456 +++++++++++++++++++++++ 2 files changed, 460 insertions(+) create mode 100644 plugins/wrappers/engine/enginewrapper.go diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index db686c5d0..28358fa34 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -212,7 +212,11 @@ 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 + if engine := pluginGetEngine(stack, notify, noverify, db); engine != nil { + return engine + } var engine consensus.Engine + if cliqueConfig != nil { engine = clique.New(cliqueConfig, db) } else { diff --git a/plugins/wrappers/engine/enginewrapper.go b/plugins/wrappers/engine/enginewrapper.go new file mode 100644 index 000000000..61de2c8cf --- /dev/null +++ b/plugins/wrappers/engine/enginewrapper.go @@ -0,0 +1,456 @@ +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 (ew *engineWrapper) Author(header *types.Header) (core.Address, error) { + addr, err := ew.engine.Author(gethToUtilsHeader(header)) + return core.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 { + return ew.engine.Prepare(&WrappedHeaderReader{chain, nil}, gethToUtilsHeader(header)) +} +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() +} From d2d3490abcdad4f264e868844149eb6b436ed4c6 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 23 Mar 2023 17:03:08 -0700 Subject: [PATCH 02/19] initial work on pluginGetEngine hook --- eth/ethconfig/plugin_hooks.go | 52 +++++++++++++++++++++++++++++++++++ go.mod | 2 ++ 2 files changed, 54 insertions(+) create mode 100644 eth/ethconfig/plugin_hooks.go diff --git a/eth/ethconfig/plugin_hooks.go b/eth/ethconfig/plugin_hooks.go new file mode 100644 index 000000000..f766d5f09 --- /dev/null +++ b/eth/ethconfig/plugin_hooks.go @@ -0,0 +1,52 @@ +package ethconfig + +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/rpc" + // "github.com/openrelayxyz/plugeth-utils/core" + // "github.com/openrelayxyz/plugeth-utils/restricted" + + // "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus" + // "github.com/ethereum/go-ethereum/consensus/beacon" + // "github.com/ethereum/go-ethereum/consensus/clique" + // "github.com/ethereum/go-ethereum/consensus/ethash" + // "github.com/ethereum/go-ethereum/core" + // "github.com/ethereum/go-ethereum/core/txpool" + // "github.com/ethereum/go-ethereum/eth/downloader" + // "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/ethereum/go-ethereum/ethdb" + // "github.com/ethereum/go-ethereum/log" + // "github.com/ethereum/go-ethereum/miner" + "github.com/ethereum/go-ethereum/node" + // "github.com/ethereum/go-ethereum/params" + + pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" + 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 PluginGetEngine(pl *plugins.PluginLoader, stack *node.Node, notify []string, noverify bool, db ethdb.Database) consensus.Engine { + fnList := pl.Lookup("GetEngine", func(item interface{}) bool { + _, ok := item.(func(*node.Node, []string, bool, ethdb.Database) consensus.Engine) + return ok + }) + for _, fni := range fnList { + if fn, ok := fni.(func(*node.Node, []string, bool, ethdb.Database)); ok { // modify + return fn(stack, notify, noverify, db) // modify + } + } + 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/go.mod b/go.mod index 6faf3a773..a86c21011 100644 --- a/go.mod +++ b/go.mod @@ -125,3 +125,5 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect ) + +replace github.com/openrelayxyz/plugeth-utils => /home/philip/src/rivet/plugeth_superspace/plugeth-utils From 9ee93f1e26ec9a7a610a3a10bc39d522f64aa839 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 23 Mar 2023 21:59:14 -0700 Subject: [PATCH 03/19] further progress on plugin hook --- eth/ethconfig/plugin_hooks.go | 22 +++++++++++++++------- plugins/wrappers/engine/enginewrapper.go | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/eth/ethconfig/plugin_hooks.go b/eth/ethconfig/plugin_hooks.go index f766d5f09..c4b9c0b26 100644 --- a/eth/ethconfig/plugin_hooks.go +++ b/eth/ethconfig/plugin_hooks.go @@ -6,8 +6,8 @@ import ( "github.com/ethereum/go-ethereum/plugins" // "github.com/ethereum/go-ethereum/plugins/wrappers" // "github.com/ethereum/go-ethereum/rpc" - // "github.com/openrelayxyz/plugeth-utils/core" - // "github.com/openrelayxyz/plugeth-utils/restricted" + "github.com/openrelayxyz/plugeth-utils/core" + "github.com/openrelayxyz/plugeth-utils/restricted" // "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" @@ -30,17 +30,25 @@ import ( // 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("GetEngine", func(item interface{}) bool { - _, ok := item.(func(*node.Node, []string, bool, 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(*node.Node, []string, bool, ethdb.Database)); ok { // modify - return fn(stack, notify, noverify, db) // modify + if fn, ok := fni.(func(*core.Node, []string, bool, restircted.Database)); ok { + engine := fn(wrappers.NewNode(stack), notify, noverify, db) // modify } } - return nil + return engineTranslate(engine) } func pluginGetEngine(stack *node.Node, notify []string, noverify bool, db ethdb.Database) consensus.Engine { diff --git a/plugins/wrappers/engine/enginewrapper.go b/plugins/wrappers/engine/enginewrapper.go index 61de2c8cf..dada9112d 100644 --- a/plugins/wrappers/engine/enginewrapper.go +++ b/plugins/wrappers/engine/enginewrapper.go @@ -17,6 +17,8 @@ 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" ) From 4cfa8704a9aa0cee22317461f618275fba3ff91f Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Fri, 24 Mar 2023 12:46:45 -0700 Subject: [PATCH 04/19] 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)) From 7f1b5c5f9b36a50be6e4529787342f12c17c11d1 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Fri, 24 Mar 2023 16:05:39 -0700 Subject: [PATCH 05/19] Progress made on 3/24/23 --- cmd/geth/plugin_hooks.go | 19 +++++++++++++++++++ eth/ethconfig/plugin_hooks.go | 14 -------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/geth/plugin_hooks.go b/cmd/geth/plugin_hooks.go index 80187a028..854975809 100644 --- a/cmd/geth/plugin_hooks.go +++ b/cmd/geth/plugin_hooks.go @@ -102,3 +102,22 @@ 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) +} + diff --git a/eth/ethconfig/plugin_hooks.go b/eth/ethconfig/plugin_hooks.go index 75b28c986..8f09f6069 100644 --- a/eth/ethconfig/plugin_hooks.go +++ b/eth/ethconfig/plugin_hooks.go @@ -2,32 +2,18 @@ package ethconfig 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" 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" - // "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" - // "github.com/ethereum/go-ethereum/consensus/beacon" - // "github.com/ethereum/go-ethereum/consensus/clique" - // "github.com/ethereum/go-ethereum/consensus/ethash" - // "github.com/ethereum/go-ethereum/core" - // "github.com/ethereum/go-ethereum/core/txpool" - // "github.com/ethereum/go-ethereum/eth/downloader" - // "github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/ethdb" - // "github.com/ethereum/go-ethereum/log" - // "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" - // "github.com/ethereum/go-ethereum/params" pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" - // pparams "github.com/openrelayxyz/plugeth-utils/restricted/params" ) From b1dd4f79f34d9bf964398dd4955930919e34e511 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 27 Mar 2023 16:24:56 -0700 Subject: [PATCH 06/19] Channel production and consumption set up. Ready to fully implement. --- cmd/geth/main.go | 1 + core/plugin_hooks.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index f3a0c3d91..a62b43845 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -357,6 +357,7 @@ func geth(ctx *cli.Context) error { stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend)) //end PluGeth code injection startNode(ctx, stack, backend, false) + pluginHookTester() stack.Wait() return nil } diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index f00730bdd..6e62e8b86 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -98,6 +98,7 @@ func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) { _, ok := item.(func(core.Hash)) return ok }) + log.Error("inside post pppp()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))") for _, fni := range fnList { if fn, ok := fni.(func(core.Hash)); ok { fn(core.Hash(block.Hash())) @@ -116,6 +117,7 @@ func PluginNewHead(pl *plugins.PluginLoader, block *types.Block, hash common.Has _, ok := item.(func([]byte, core.Hash, [][]byte, *big.Int)) return ok }) + log.Error("inside of pluginNewHead()", "len fnList", len(fnList)) blockBytes, _ := rlp.EncodeToBytes(block) logBytes := make([][]byte, len(logs)) for i, l := range logs { From c3924a529e80a9f048cfeb4851b56922e0ec12b5 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 6 Apr 2023 10:36:43 -0700 Subject: [PATCH 07/19] new consensus engine commit 4/6/23 --- plugins/wrappers/engine/enginewrapper.go | 1 - plugins/wrappers/wrappers.go | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/wrappers/engine/enginewrapper.go b/plugins/wrappers/engine/enginewrapper.go index 40bc297b4..f8aacc60d 100644 --- a/plugins/wrappers/engine/enginewrapper.go +++ b/plugins/wrappers/engine/enginewrapper.go @@ -432,7 +432,6 @@ func (ew *engineWrapper) Prepare(chain consensus.ChainHeaderReader, header *type } // 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) { diff --git a/plugins/wrappers/wrappers.go b/plugins/wrappers/wrappers.go index 5458cd4a5..33fe44d07 100644 --- a/plugins/wrappers/wrappers.go +++ b/plugins/wrappers/wrappers.go @@ -194,6 +194,11 @@ 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 86b30777b8d254d632f90357e35095c4117d9aee Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Wed, 19 Apr 2023 10:32:26 -0700 Subject: [PATCH 08/19] working commit 4/19 --- eth/tracers/api.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 54c32449a..704abe932 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -957,14 +957,17 @@ func (api *API) traceTx(ctx context.Context, message *core.Message, txctx *Conte if config == nil { config = &TraceConfig{} } + log.Error("outside of geth condition", "config", config.Tracer) // Default tracer is the struct logger tracer = logger.NewStructLogger(config.Config) if config.Tracer != nil { // Get the tracer from the plugin loader //begin PluGeth code injection if tr, ok := getPluginTracer(*config.Tracer); ok { + log.Error("inside geth", "tracer config", *config.Tracer) tracer = tr(statedb, vmctx) } else { + log.Error("default geth condition", "config", config.Tracer) tracer, err = DefaultDirectory.New(*config.Tracer, txctx, config.TracerConfig) if err != nil { return nil, err From 6e8c38b994cb13fb63447dbb7bf6d8bad727b2a5 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Fri, 28 Apr 2023 12:09:04 -0700 Subject: [PATCH 09/19] removed unnecessary comments --- core/plugin_hooks.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index 6e62e8b86..f2c5def39 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -98,7 +98,6 @@ func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) { _, ok := item.(func(core.Hash)) return ok }) - log.Error("inside post pppp()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))") for _, fni := range fnList { if fn, ok := fni.(func(core.Hash)); ok { fn(core.Hash(block.Hash())) From 679d02848440c87fd2e27a75c71ababef1a86cfa Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 1 Jun 2023 13:14:08 -0700 Subject: [PATCH 10/19] Functional test for injections in core/rawdb --- core/rawdb/freezer_batch.go | 6 ++++ core/rawdb/plugeth_hook_test.go | 50 +++++++++++++++++++++++++++++++++ core/rawdb/plugin_hooks.go | 13 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 core/rawdb/plugeth_hook_test.go diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index 99d226e9a..8189cecff 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -46,6 +46,9 @@ func newFreezerBatch(f *Freezer) *freezerBatch { func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error { // begin PluGeth injection PluginTrackUpdate(num, kind, item) + if injectionCalled != nil { + return nil + } // end PluGeth injection return batch.tables[kind].Append(num, item) } @@ -54,6 +57,9 @@ func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) err func (batch *freezerBatch) AppendRaw(kind string, num uint64, item []byte) error { // being PluGeth injection PluginTrackUpdate(num, kind, item) + if injectionCalled != nil { + return nil + } // end PluGeth injection return batch.tables[kind].AppendRaw(num, item) } diff --git a/core/rawdb/plugeth_hook_test.go b/core/rawdb/plugeth_hook_test.go new file mode 100644 index 000000000..4fb6d1560 --- /dev/null +++ b/core/rawdb/plugeth_hook_test.go @@ -0,0 +1,50 @@ +package rawdb + + +import ( + "fmt" + "os" + "testing" + "github.com/ethereum/go-ethereum/ethdb" +) + + + + +func TestAncientsInjections(t *testing.T) { + + test_dir_path := "./injection_test_dir" + f, _ := NewFreezer(test_dir_path, "plugeth hook test", false, uint32(0), map[string]bool{"test": false}) + + t.Run(fmt.Sprintf("test ModifyAncients"), func(t *testing.T) { + called := false + injectionCalled = &called + _, _ = f.ModifyAncients(func (ethdb.AncientWriteOp) error {return nil}) + if *injectionCalled != true { + t.Fatalf("pluginCommitUpdate injection in ModifyAncients not called") + } + }) + + os.RemoveAll(test_dir_path) + + fb := newFreezerBatch(f) + + t.Run(fmt.Sprintf("test Append"), func(t *testing.T) { + var item interface{} + called := false + injectionCalled = &called + _ = fb.Append("kind", uint64(0), item) + if *injectionCalled != true { + t.Fatalf("PluginTrackUpdate injection in Append not called") + } + }) + + t.Run(fmt.Sprintf("test AppendRaw"), func(t *testing.T) { + called := false + injectionCalled = &called + _ = fb.AppendRaw("kind", uint64(100), []byte{}) + if *injectionCalled != true { + t.Fatalf("PluginTrackUpdate injection in AppendRaw not called") + } + }) +} \ No newline at end of file diff --git a/core/rawdb/plugin_hooks.go b/core/rawdb/plugin_hooks.go index 98fd27a2e..83a7aa9a6 100644 --- a/core/rawdb/plugin_hooks.go +++ b/core/rawdb/plugin_hooks.go @@ -11,9 +11,16 @@ import ( var ( freezerUpdates map[uint64]map[string]interface{} lock sync.Mutex + injectionCalled *bool ) func PluginTrackUpdate(num uint64, kind string, value interface{}) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + lock.Lock() defer lock.Unlock() if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) } @@ -26,6 +33,12 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) { } func pluginCommitUpdate(num uint64) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + if plugins.DefaultPluginLoader == nil { log.Warn("Attempting CommitUpdate, but default PluginLoader has not been initialized") return From 5dc7541135f701b1f0490c396cdd41c8ab775fb2 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Fri, 2 Jun 2023 09:45:48 -0700 Subject: [PATCH 11/19] Functional stand alone test for pluginBlockProcessingError --- core/plugeth_injection_test.go | 93 +++++++++++++++++++ core/plugin_hooks.go | 8 ++ ...hook_test.go => plugeth_injection_test.go} | 0 3 files changed, 101 insertions(+) create mode 100644 core/plugeth_injection_test.go rename core/rawdb/{plugeth_hook_test.go => plugeth_injection_test.go} (100%) diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go new file mode 100644 index 000000000..faa7c2399 --- /dev/null +++ b/core/plugeth_injection_test.go @@ -0,0 +1,93 @@ +package core + +import ( + "fmt" + "testing" + "math/big" + "crypto/ecdsa" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/consensus/ethash" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/common/math" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/state" +) + +var ( + config = ¶ms.ChainConfig{ + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + Ethash: new(params.EthashConfig), + } + signer = types.LatestSigner(config) + key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + key2, _ = crypto.HexToECDSA("0202020202020202020202020202020202020202020202020202002020202020") +) + +var makeTx = func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction { + tx, _ := types.SignTx(types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data), signer, key) + return tx +} + +var ( + db = rawdb.NewMemoryDatabase() + gspec = &Genesis{ + Config: config, + Alloc: GenesisAlloc{ + common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ + Balance: big.NewInt(1000000000000000000), // 1 ether + Nonce: 0, + }, + common.HexToAddress("0xfd0810DD14796680f72adf1a371963d0745BCc64"): GenesisAccount{ + Balance: big.NewInt(1000000000000000000), // 1 ether + Nonce: math.MaxUint64, + }, + }, + } +) + + +func TestBlockProcessingInjections(t *testing.T) { + + + blockchain, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + engine := ethash.NewFaker() + + sp := NewStateProcessor(config, blockchain, engine) + + txns := []*types.Transaction{ + makeTx(key1, 0, common.Address{}, big.NewInt(1000), params.TxGas-1000, big.NewInt(875000000), nil), + } + + block := GenerateBadBlock(gspec.ToBlock(), engine, txns, gspec.Config) + + statedb, _ := state.New(blockchain.GetBlockByHash(block.ParentHash()).Root(), blockchain.stateCache, nil) + + t.Run(fmt.Sprintf("test BlockProcessingError"), func(t *testing.T) { + called := false + injectionCalled = &called + + _, _, _, _ = sp.Process(block, statedb, vm.Config{}) + + if *injectionCalled != true { + t.Fatalf("pluginBlockProcessingError injection in stateProcessor.Process() not called") + } + }) + + +} \ No newline at end of file diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index f2c5def39..9abbfa723 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -18,6 +18,8 @@ import ( "github.com/openrelayxyz/plugeth-utils/core" ) +var injectionCalled *bool + func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) { fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool { _, ok := item.(func(core.Hash, uint64, []byte)) @@ -68,6 +70,12 @@ func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, } } func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err error) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + if plugins.DefaultPluginLoader == nil { log.Warn("Attempting BlockProcessingError, but default PluginLoader has not been initialized") return diff --git a/core/rawdb/plugeth_hook_test.go b/core/rawdb/plugeth_injection_test.go similarity index 100% rename from core/rawdb/plugeth_hook_test.go rename to core/rawdb/plugeth_injection_test.go From 02d22a5aada54e97915036bd97f478e04450b5e3 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 5 Jun 2023 11:04:07 -0700 Subject: [PATCH 12/19] pluginReorg() test functional --- core/plugeth_injection_test.go | 25 ++++++++++++++++++++++--- core/plugin_hooks.go | 7 +++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go index faa7c2399..3068cb68e 100644 --- a/core/plugeth_injection_test.go +++ b/core/plugeth_injection_test.go @@ -1,6 +1,7 @@ package core import ( + // "reflect" "fmt" "testing" "math/big" @@ -61,7 +62,7 @@ var ( ) -func TestBlockProcessingInjections(t *testing.T) { +func TestPlugethInjections(t *testing.T) { blockchain, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) @@ -89,5 +90,23 @@ func TestBlockProcessingInjections(t *testing.T) { } }) - -} \ No newline at end of file + t.Run(fmt.Sprintf("test Reorg"), func(t *testing.T) { + called := false + injectionCalled = &called + + // the transaction has to be initialized with a different gas price than the previous tx in order to trigger a reorg + txns2 := []*types.Transaction{ + makeTx(key1, 0, common.Address{}, big.NewInt(1000), params.TxGas-1000, big.NewInt(875000001), nil), + } + block2 := GenerateBadBlock(gspec.ToBlock(), engine, txns2, gspec.Config) + + + _, _ = blockchain.writeBlockAndSetHead(block, []*types.Receipt{}, []*types.Log{}, statedb, false) + + _ = blockchain.reorg(block.Header(), block2) + + if *injectionCalled != true { + t.Fatalf("pluginReorg injection in blockChain.Reorg() not called") + } + }) +} \ No newline at end of file diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index 9abbfa723..06d2e89ee 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -169,6 +169,7 @@ func pluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log) } func PluginReorg(pl *plugins.PluginLoader, commonBlock *types.Block, oldChain, newChain types.Blocks) { + fnList := pl.Lookup("Reorg", func(item interface{}) bool { _, ok := item.(func(core.Hash, []core.Hash, []core.Hash)) return ok @@ -188,6 +189,12 @@ func PluginReorg(pl *plugins.PluginLoader, commonBlock *types.Block, oldChain, n } } func pluginReorg(commonBlock *types.Block, oldChain, newChain types.Blocks) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + if plugins.DefaultPluginLoader == nil { log.Warn("Attempting Reorg, but default PluginLoader has not been initialized") return From 12336c753f70eb7ef1f28f76888ba5d86a7ef044 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Tue, 6 Jun 2023 11:32:13 -0700 Subject: [PATCH 13/19] pluginNewSideBlock covered by stand alone test. --- core/plugeth_injection_test.go | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go index 3068cb68e..06c10f51b 100644 --- a/core/plugeth_injection_test.go +++ b/core/plugeth_injection_test.go @@ -2,6 +2,8 @@ package core import ( // "reflect" + // "sync/atomic" + "hash" "fmt" "testing" "math/big" @@ -16,6 +18,7 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "golang.org/x/crypto/sha3" ) var ( @@ -61,6 +64,27 @@ var ( } ) +type testHasher struct { + hasher hash.Hash +} + +func newHasher() *testHasher { + return &testHasher{hasher: sha3.NewLegacyKeccak256()} +} + +func (h *testHasher) Reset() { + h.hasher.Reset() +} + +func (h *testHasher) Update(key, val []byte) { + h.hasher.Write(key) + h.hasher.Write(val) +} + +func (h *testHasher) Hash() common.Hash { + return common.BytesToHash(h.hasher.Sum(nil)) +} + func TestPlugethInjections(t *testing.T) { @@ -109,4 +133,15 @@ func TestPlugethInjections(t *testing.T) { t.Fatalf("pluginReorg injection in blockChain.Reorg() not called") } }) + + t.Run(fmt.Sprintf("test NewSideBlock"), func(t *testing.T) { + called := false + injectionCalled = &called + + TestReorgToShorterRemovesCanonMapping(t) + + if *injectionCalled != true { + t.Fatalf("pluginNewSideBlock injection in blockChain.writeBlockAndSetHead() not called") + } + }) } \ No newline at end of file From 23c0ca58af42b38c20085fe0eee6553689778410 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Tue, 13 Jun 2023 13:13:13 -0700 Subject: [PATCH 14/19] trieFlushClone test experiment --- core/blockchain.go | 1 + core/plugeth_injection_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index b31f915bc..0c5836bb9 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1334,6 +1334,7 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error { // writeBlockWithState writes block, metadata and corresponding state data to the // database. func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) error { + log.Error("inside of the target function") // Calculate the total difficulty of the block ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) if ptd == nil { diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go index 06c10f51b..08108f242 100644 --- a/core/plugeth_injection_test.go +++ b/core/plugeth_injection_test.go @@ -134,6 +134,17 @@ func TestPlugethInjections(t *testing.T) { } }) + t.Run(fmt.Sprintf("test treiIntervarFlushClone"), func(t *testing.T) { + called := false + injectionCalled = &called + + _ = blockchain.writeBlockWithState(block, []*types.Receipt{}, statedb) + + if *injectionCalled != true { + t.Fatalf("pluginNewSideBlock injection in blockChain.writeBlockAndSetHead() not called") + } + }) + t.Run(fmt.Sprintf("test NewSideBlock"), func(t *testing.T) { called := false injectionCalled = &called @@ -144,4 +155,5 @@ func TestPlugethInjections(t *testing.T) { t.Fatalf("pluginNewSideBlock injection in blockChain.writeBlockAndSetHead() not called") } }) + } \ No newline at end of file From fee25089a92037643a01fd82b4ad0519bb525fbd Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Wed, 14 Jun 2023 22:24:08 -0700 Subject: [PATCH 15/19] Added HookTester back into main. --- cmd/geth/plugin_hooks.go | 19 +++++++++++++++++++ go.mod | 2 -- rpc/service.go | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/geth/plugin_hooks.go b/cmd/geth/plugin_hooks.go index ec44501f2..3f1dca401 100644 --- a/cmd/geth/plugin_hooks.go +++ b/cmd/geth/plugin_hooks.go @@ -86,6 +86,7 @@ func pluginsInitializeNode(stack *node.Node, backend restricted.Backend) { } func OnShutdown(pl *plugins.PluginLoader) { + log.Error("inside of on shutdown") fnList := pl.Lookup("OnShutdown", func(item interface{}) bool { _, ok := item.(func()) return ok @@ -120,3 +121,21 @@ func pluginBlockChain() { } BlockChain(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/go.mod b/go.mod index 95749d81a..d02b0b15a 100644 --- a/go.mod +++ b/go.mod @@ -126,5 +126,3 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect ) - -replace github.com/openrelayxyz/plugeth-utils => /home/philip/src/rivet/plugeth_superspace/plugeth-utils diff --git a/rpc/service.go b/rpc/service.go index cfdfba023..c71586a6f 100644 --- a/rpc/service.go +++ b/rpc/service.go @@ -63,6 +63,9 @@ func (r *serviceRegistry) registerName(name string, rcvr interface{}) error { return fmt.Errorf("no service name for type %s", rcvrVal.Type().String()) } callbacks := suitableCallbacks(rcvrVal) + // begin PluGeth code injection + pluginExtendedCallbacks(callbacks, rcvrVal) + // end PluGeth code injection if len(callbacks) == 0 { return fmt.Errorf("service %T doesn't have any suitable methods/subscriptions to expose", rcvr) } From 8a33d8e5069688b8bc7947552ede83d1362391a7 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 19 Jun 2023 20:59:18 -0700 Subject: [PATCH 16/19] added meta blockProcessingError to test --- core/plugeth_injection_test.go | 15 ++++----------- core/plugin_hooks.go | 7 +++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go index 08108f242..3672be1b6 100644 --- a/core/plugeth_injection_test.go +++ b/core/plugeth_injection_test.go @@ -106,12 +106,16 @@ func TestPlugethInjections(t *testing.T) { t.Run(fmt.Sprintf("test BlockProcessingError"), func(t *testing.T) { called := false injectionCalled = &called + metaInjectionCalled = &called _, _, _, _ = sp.Process(block, statedb, vm.Config{}) if *injectionCalled != true { t.Fatalf("pluginBlockProcessingError injection in stateProcessor.Process() not called") } + if *metaInjectionCalled != true { + t.Fatalf("metaTracer.BlockProcessingError injection in stateProcessor.Process() not called") + } }) t.Run(fmt.Sprintf("test Reorg"), func(t *testing.T) { @@ -134,17 +138,6 @@ func TestPlugethInjections(t *testing.T) { } }) - t.Run(fmt.Sprintf("test treiIntervarFlushClone"), func(t *testing.T) { - called := false - injectionCalled = &called - - _ = blockchain.writeBlockWithState(block, []*types.Receipt{}, statedb) - - if *injectionCalled != true { - t.Fatalf("pluginNewSideBlock injection in blockChain.writeBlockAndSetHead() not called") - } - }) - t.Run(fmt.Sprintf("test NewSideBlock"), func(t *testing.T) { called := false injectionCalled = &called diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index 06d2e89ee..5d95af640 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -19,6 +19,7 @@ import ( ) var injectionCalled *bool +var metaInjectionCalled *bool func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) { fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool { @@ -229,6 +230,12 @@ func (mt *metaTracer) PreProcessTransaction(tx *types.Transaction, block *types. } } func (mt *metaTracer) BlockProcessingError(tx *types.Transaction, block *types.Block, err error) { + + if metaInjectionCalled != nil { + called := true + metaInjectionCalled = &called + } + if len(mt.tracers) == 0 { return } blockHash := core.Hash(block.Hash()) transactionHash := core.Hash(tx.Hash()) From b3f1d35171ebb9d0e4f0e8b9e9fb5b64c205900c Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 22 Jun 2023 01:05:10 -0700 Subject: [PATCH 17/19] functional test plugin. This PR will also include the stand alone tests. --- cmd/geth/main.go | 1 - cmd/geth/plugin_hooks.go | 19 ------------------- core/blockchain.go | 1 - core/plugin_hooks.go | 1 - eth/tracers/api.go | 3 --- rpc/plugin_subscriptions.go | 18 ++++++++++++++++++ 6 files changed, 18 insertions(+), 25 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 40d1b25e9..71e88af2c 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -357,7 +357,6 @@ func geth(ctx *cli.Context) error { stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend)) startNode(ctx, stack, backend, false) pluginBlockChain() - pluginHookTester() //end PluGeth code injection stack.Wait() return nil diff --git a/cmd/geth/plugin_hooks.go b/cmd/geth/plugin_hooks.go index 3f1dca401..2a1053c3a 100644 --- a/cmd/geth/plugin_hooks.go +++ b/cmd/geth/plugin_hooks.go @@ -86,7 +86,6 @@ func pluginsInitializeNode(stack *node.Node, backend restricted.Backend) { } func OnShutdown(pl *plugins.PluginLoader) { - log.Error("inside of on shutdown") fnList := pl.Lookup("OnShutdown", func(item interface{}) bool { _, ok := item.(func()) return ok @@ -120,22 +119,4 @@ func pluginBlockChain() { return } BlockChain(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/core/blockchain.go b/core/blockchain.go index d79a64d32..aa723598b 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1333,7 +1333,6 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error { // writeBlockWithState writes block, metadata and corresponding state data to the // database. func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) error { - log.Error("inside of the target function") // Calculate the total difficulty of the block ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) if ptd == nil { diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index 5d95af640..3052b1b44 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -125,7 +125,6 @@ func PluginNewHead(pl *plugins.PluginLoader, block *types.Block, hash common.Has _, ok := item.(func([]byte, core.Hash, [][]byte, *big.Int)) return ok }) - log.Error("inside of pluginNewHead()", "len fnList", len(fnList)) blockBytes, _ := rlp.EncodeToBytes(block) logBytes := make([][]byte, len(logs)) for i, l := range logs { diff --git a/eth/tracers/api.go b/eth/tracers/api.go index f22effa2b..b0c1de365 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -956,17 +956,14 @@ func (api *API) traceTx(ctx context.Context, message *core.Message, txctx *Conte if config == nil { config = &TraceConfig{} } - log.Error("outside of geth condition", "config", config.Tracer) // Default tracer is the struct logger tracer = logger.NewStructLogger(config.Config) if config.Tracer != nil { // Get the tracer from the plugin loader //begin PluGeth code injection if tr, ok := getPluginTracer(*config.Tracer); ok { - log.Error("inside geth", "tracer config", *config.Tracer) tracer = tr(statedb, vmctx) } else { - log.Error("default geth condition", "config", config.Tracer) tracer, err = DefaultDirectory.New(*config.Tracer, txctx, config.TracerConfig) if err != nil { return nil, err diff --git a/rpc/plugin_subscriptions.go b/rpc/plugin_subscriptions.go index ac8282925..2c960fa0b 100644 --- a/rpc/plugin_subscriptions.go +++ b/rpc/plugin_subscriptions.go @@ -4,6 +4,7 @@ import ( "context" "reflect" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/plugins" ) @@ -121,7 +122,24 @@ func callbackifyChanPubSub(receiver, fn reflect.Value) *callback { return c } +func RPCSubscription(pl *plugins.PluginLoader) { + fnList := pl.Lookup("RPCSubscriptionTest", func(item interface{}) bool { + _, ok := item.(func()) + return ok + }) + for _, fni := range fnList { + if fn, ok := fni.(func()); ok { + fn() + } + } +} + func pluginExtendedCallbacks(callbacks map[string]*callback, receiver reflect.Value) { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting RPCSubscriptionTest, but default PluginLoader has not been initialized") + return + } + RPCSubscription(plugins.DefaultPluginLoader) typ := receiver.Type() for m := 0; m < typ.NumMethod(); m++ { method := typ.Method(m) From 4ca9210d59299bf96ae7a1684c0ad5a128cd1478 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 22 Jun 2023 13:00:05 -0700 Subject: [PATCH 18/19] Modified injection test in /core/rawdb/ --- core/rawdb/freezer_batch.go | 6 --- core/rawdb/plugeth_injection_test.go | 68 ++++++++++++++-------------- core/rawdb/plugin_hooks.go | 23 +++++++--- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index bb80da8e6..c5dac62e2 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -45,9 +45,6 @@ func newFreezerBatch(f *Freezer) *freezerBatch { func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) error { // begin PluGeth injection PluginTrackUpdate(num, kind, item) - if injectionCalled != nil { - return nil - } // end PluGeth injection return batch.tables[kind].Append(num, item) } @@ -56,9 +53,6 @@ func (batch *freezerBatch) Append(kind string, num uint64, item interface{}) err func (batch *freezerBatch) AppendRaw(kind string, num uint64, item []byte) error { // being PluGeth injection PluginTrackUpdate(num, kind, item) - if injectionCalled != nil { - return nil - } // end PluGeth injection return batch.tables[kind].AppendRaw(num, item) } diff --git a/core/rawdb/plugeth_injection_test.go b/core/rawdb/plugeth_injection_test.go index 4fb6d1560..7ab47f9eb 100644 --- a/core/rawdb/plugeth_injection_test.go +++ b/core/rawdb/plugeth_injection_test.go @@ -3,48 +3,48 @@ package rawdb import ( "fmt" - "os" "testing" + "math/big" + "github.com/ethereum/go-ethereum/ethdb" ) +func TestPlugethInjections(t *testing.T) { + var valuesRaw [][]byte + var valuesRLP []*big.Int + for x := 0; x < 100; x++ { + v := getChunk(256, x) + valuesRaw = append(valuesRaw, v) + iv := big.NewInt(int64(x)) + iv = iv.Exp(iv, iv, nil) + valuesRLP = append(valuesRLP, iv) + } + tables := map[string]bool{"raw": true, "rlp": false} + f, _ := newFreezerForTesting(t, tables) - -func TestAncientsInjections(t *testing.T) { - - test_dir_path := "./injection_test_dir" - f, _ := NewFreezer(test_dir_path, "plugeth hook test", false, uint32(0), map[string]bool{"test": false}) - - t.Run(fmt.Sprintf("test ModifyAncients"), func(t *testing.T) { + t.Run(fmt.Sprintf("test plugeth injections"), func(t *testing.T) { called := false - injectionCalled = &called - _, _ = f.ModifyAncients(func (ethdb.AncientWriteOp) error {return nil}) - if *injectionCalled != true { + modifyAncientsInjection = &called + + _, _ = f.ModifyAncients(func(op ethdb.AncientWriteOp) error { + + appendRawInjection = &called + _ = op.AppendRaw("raw", uint64(0), valuesRaw[0]) + if *appendRawInjection != true { + t.Fatalf("pluginTrackUpdate injection in AppendRaw not called") + } + + appendInjection = &called + _ = op.Append("rlp", uint64(0), valuesRaw[0]) + if *appendInjection != true { + t.Fatalf("pluginTrackUpdate injection in Append not called") + } + + return nil + }) + if *modifyAncientsInjection != true { t.Fatalf("pluginCommitUpdate injection in ModifyAncients not called") } }) - - os.RemoveAll(test_dir_path) - - fb := newFreezerBatch(f) - - t.Run(fmt.Sprintf("test Append"), func(t *testing.T) { - var item interface{} - called := false - injectionCalled = &called - _ = fb.Append("kind", uint64(0), item) - if *injectionCalled != true { - t.Fatalf("PluginTrackUpdate injection in Append not called") - } - }) - - t.Run(fmt.Sprintf("test AppendRaw"), func(t *testing.T) { - called := false - injectionCalled = &called - _ = fb.AppendRaw("kind", uint64(100), []byte{}) - if *injectionCalled != true { - t.Fatalf("PluginTrackUpdate injection in AppendRaw not called") - } - }) } \ No newline at end of file diff --git a/core/rawdb/plugin_hooks.go b/core/rawdb/plugin_hooks.go index 83a7aa9a6..e4635e4a6 100644 --- a/core/rawdb/plugin_hooks.go +++ b/core/rawdb/plugin_hooks.go @@ -2,23 +2,31 @@ package rawdb import ( + "sync" + "github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" - "sync" ) var ( freezerUpdates map[uint64]map[string]interface{} lock sync.Mutex - injectionCalled *bool + modifyAncientsInjection *bool + appendRawInjection *bool + appendInjection *bool ) func PluginTrackUpdate(num uint64, kind string, value interface{}) { - if injectionCalled != nil { + if appendRawInjection != nil { called := true - injectionCalled = &called + appendRawInjection = &called + } + + if appendInjection != nil { + called := true + appendInjection = &called } lock.Lock() @@ -33,10 +41,10 @@ func PluginTrackUpdate(num uint64, kind string, value interface{}) { } func pluginCommitUpdate(num uint64) { - - if injectionCalled != nil { + + if modifyAncientsInjection != nil { called := true - injectionCalled = &called + modifyAncientsInjection = &called } if plugins.DefaultPluginLoader == nil { @@ -47,6 +55,7 @@ func pluginCommitUpdate(num uint64) { } func PluginCommitUpdate(pl *plugins.PluginLoader, num uint64) { + lock.Lock() defer lock.Unlock() if freezerUpdates == nil { freezerUpdates = make(map[uint64]map[string]interface{}) } From 54899440d202a6136cfe3447abbe2effb4c39201 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 22 Jun 2023 13:28:50 -0700 Subject: [PATCH 19/19] Removed comments --- core/plugeth_injection_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go index 3672be1b6..a2dc60d54 100644 --- a/core/plugeth_injection_test.go +++ b/core/plugeth_injection_test.go @@ -1,8 +1,6 @@ package core import ( - // "reflect" - // "sync/atomic" "hash" "fmt" "testing"