From bb43b2cbc44710eb3effc352153b338d8a606e47 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Mon, 31 Jul 2023 12:43:06 -0700 Subject: [PATCH] Changes made to support unity across all plugeth projects with resepct to consensus engine. --- eth/ethconfig/config.go | 4 +- eth/ethconfig/plugin_hooks.go | 29 ++++---- plugins/test-plugin/test/results.txt | 1 - plugins/test-plugin/test/test.sh | 70 ------------------- .../wrappers/backendwrapper/backendwrapper.go | 23 ++++++ plugins/wrappers/backendwrapper/dbwrapper.go | 2 +- 6 files changed, 41 insertions(+), 88 deletions(-) delete mode 100644 plugins/test-plugin/test/results.txt delete mode 100644 plugins/test-plugin/test/test.sh diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 074dc5436..63c3305a4 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -171,8 +171,8 @@ type Config struct { func CreateConsensusEngine(config *params.ChainConfig, db ethdb.Database) (consensus.Engine, error) { // If proof-of-authority is requested, set it up //begin PluGeth code injection - if engine := pluginGetEngine(); engine != nil { - log.Error("returning plugin consensus engine") + if engine := pluginGetEngine(config, db); engine != nil { + log.Info("returning plugin consensus engine") return engine, nil } //end PluGeth code injection diff --git a/eth/ethconfig/plugin_hooks.go b/eth/ethconfig/plugin_hooks.go index 2d4abe455..0e9ad889f 100644 --- a/eth/ethconfig/plugin_hooks.go +++ b/eth/ethconfig/plugin_hooks.go @@ -3,29 +3,30 @@ 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/params" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/consensus" - // "github.com/ethereum/go-ethereum/ethdb" - // "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/plugins/wrappers/backendwrapper" + wengine "github.com/ethereum/go-ethereum/plugins/wrappers/engine" + + "github.com/openrelayxyz/plugeth-utils/restricted" + pparams "github.com/openrelayxyz/plugeth-utils/restricted/params" pconsensus "github.com/openrelayxyz/plugeth-utils/restricted/consensus" ) -func PluginGetEngine(pl *plugins.PluginLoader) consensus.Engine { +func PluginGetEngine(pl *plugins.PluginLoader, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine { fnList := pl.Lookup("CreateEngine", func(item interface{}) bool { - _, ok := item.(func() pconsensus.Engine) + _, ok := item.(func(*pparams.ChainConfig, restricted.Database) pconsensus.Engine) return ok }) for _, fni := range fnList { - if fn, ok := fni.(func() pconsensus.Engine); ok { - if engine := fn(); engine != nil { + if fn, ok := fni.(func(*pparams.ChainConfig, restricted.Database) pconsensus.Engine); ok { + clonedConfig := backendwrapper.CloneChainConfig(chainConfig) + wrappedDb := backendwrapper.NewDb(db) + if engine := fn(clonedConfig, wrappedDb); engine != nil { wrappedEngine := wengine.NewWrappedEngine(engine) return wrappedEngine } @@ -35,10 +36,10 @@ func PluginGetEngine(pl *plugins.PluginLoader) consensus.Engine { return nil } -func pluginGetEngine() consensus.Engine { +func pluginGetEngine(chainConfig *params.ChainConfig, 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) + return PluginGetEngine(plugins.DefaultPluginLoader, chainConfig, db) } \ No newline at end of file diff --git a/plugins/test-plugin/test/results.txt b/plugins/test-plugin/test/results.txt deleted file mode 100644 index 4864d07fd..000000000 --- a/plugins/test-plugin/test/results.txt +++ /dev/null @@ -1 +0,0 @@ -map[LiveCaptureEnd:{} LiveCaptureStart:{} LiveCaptureState:{} LivePostProcessBlock:{} PostProcessBlock:{}] diff --git a/plugins/test-plugin/test/test.sh b/plugins/test-plugin/test/test.sh deleted file mode 100644 index 6dc9c0764..000000000 --- a/plugins/test-plugin/test/test.sh +++ /dev/null @@ -1,70 +0,0 @@ -[ -f "passwordfile" ] && rm -f passwordfile -[ -d "00/" ] && rm -rf 00/ -[ -d "test00/" ] && rm -rf test00/ -[ -d "01/" ] && rm -rf 01/ -[ -d "test01/" ] && rm -rf test01/ -[ -d "02/" ] && rm -rf 02/ -[ -d "test02/" ] && rm -rf test02/ - -mkdir -p test00 test01 test02 00/keystore 01/keystore 02/keystore 00/geth 01/geth 02/geth 00/plugins 01/plugins 02/plugins - - -cp ../engine.go test00/ -cp ../engine.go ../main.go ../hooks.go ../tracer.go ../live_tracer.go test01/ -cp ../engine.go ../shutdown.go test02/ -cd test00/ -go build -buildmode=plugin -o ../00/plugins -cd ../ -cd test01/ -go build -buildmode=plugin -o ../01/plugins -cd ../ -cd test02/ -go build -buildmode=plugin -o ../02/plugins -cd ../ - -cp UTC--2021-03-02T16-47-49.510918858Z--f2c207111cb6ef761e439e56b25c7c99ac026a01 00/keystore -cp UTC--2021-03-02T16-47-39.492920333Z--4204477bf7fce868e761caaba991ffc607717dbf 01/keystore -cp UTC--2021-03-02T16-47-59.816632526Z--2cb2e3bdb066a83a7f1191eef1697da51793f631 02/keystore - -cp nodekey00 00/geth/nodekey -cp nodekey01 01/geth/nodekey -cp nodekey02 02/geth/nodekey - -echo -n "supersecretpassword" > passwordfile - -$GETH init --datadir=./00 genesis.json -$GETH init --datadir=./01 genesis.json -$GETH init --datadir=./02 genesis.json - -# miner node -$GETH --cache.preimages --config config00.toml --authrpc.port 8552 --port 64480 --verbosity=0 --nodiscover --networkid=6448 --datadir=./00/ --mine --miner.etherbase f2c207111cb6ef761e439e56b25c7c99ac026a01 --unlock f2c207111cb6ef761e439e56b25c7c99ac026a01 --http --http.api eth,debug,net --http.port 9545 --password passwordfile --allow-insecure-unlock & -pid0=$! - -sleep 1 -# passive node -$GETH --cache.preimages --config config01.toml --authrpc.port 8553 --port 64481 --verbosity=3 --syncmode=full --nodiscover --networkid=6448 --datadir=./01/ --unlock 4204477bf7fce868e761caaba991ffc607717dbf --miner.etherbase 4204477bf7fce868e761caaba991ffc607717dbf --password passwordfile --ws --ws.port 8546 --ws.api eth,admin --http --http.api eth,debug,net --http.port 9546 --allow-insecure-unlock & - -sleep 1 - -# shutdown node -$GETH --config config02.toml --authrpc.port 8556 --port 64484 --verbosity=0 --syncmode=full --nodiscover --networkid=6448 --datadir=./02/ --unlock 2cb2e3bdb066a83a7f1191eef1697da51793f631 --miner.etherbase 2cb2e3bdb066a83a7f1191eef1697da51793f631 --password passwordfile --ws --ws.port 8548 --ws.api eth,admin --http --http.api eth,debug,net --http.port 9547 --allow-insecure-unlock & -pid1=$! - -sleep 5 - -if ps -p $pid1 > /dev/null; then - kill $pid1 -fi - -sleep 255 - -if ps -p $pid0 > /dev/null; then - kill $pid0 -fi - -wait - -rm -f passwordfile -rm -rf 00/ 01/ 02/ test00/ test01/ test02/ - - diff --git a/plugins/wrappers/backendwrapper/backendwrapper.go b/plugins/wrappers/backendwrapper/backendwrapper.go index c2421ddc9..6716d4182 100644 --- a/plugins/wrappers/backendwrapper/backendwrapper.go +++ b/plugins/wrappers/backendwrapper/backendwrapper.go @@ -14,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" + gparams "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/trie" @@ -463,6 +464,28 @@ func (b *Backend) ChainConfig() *params.ChainConfig { return b.chainConfig } +func CloneChainConfig(cf *gparams.ChainConfig) *params.ChainConfig { + result := ¶ms.ChainConfig{} + nval := reflect.ValueOf(result) + ntype := nval.Elem().Type() + lval := reflect.ValueOf(cf) + 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 result +} + func (b *Backend) GetTrie(h core.Hash) (core.Trie, error) { tr, err := trie.NewStateTrie(trie.TrieID(common.Hash(h)), trie.NewDatabase(b.b.ChainDb())) if err != nil { diff --git a/plugins/wrappers/backendwrapper/dbwrapper.go b/plugins/wrappers/backendwrapper/dbwrapper.go index 8af502034..8fa0fa891 100644 --- a/plugins/wrappers/backendwrapper/dbwrapper.go +++ b/plugins/wrappers/backendwrapper/dbwrapper.go @@ -11,7 +11,7 @@ type dbWrapper struct { db ethdb.Database } -func NewDB(d ethdb.Database) restricted.Database { +func NewDb(d ethdb.Database) restricted.Database { return &dbWrapper{d} }