Merge pull request #92 from openrelayxyz/feature/etc-plugin

Feature/etc plugin
This commit is contained in:
AusIV
2023-10-30 12:53:00 -05:00
committed by GitHub
23 changed files with 1568 additions and 301 deletions
+48 -2
View File
@@ -979,7 +979,13 @@ var (
// if none (or the empty string) is specified. If the node is starting a testnet,
// then a subdirectory of the specified datadir will be used.
func MakeDataDir(ctx *cli.Context) string {
if path := ctx.String(DataDirFlag.Name); path != "" {
if path := ctx.String(DataDirFlag.Name); path == "" {
// begin PluGeth injection
if pluginPath := pluginDefaultDataDir(path); pluginPath != "" {
log.Error("Inside datdir injection number one")
return pluginPath
}
// end PluGeth injection
if ctx.Bool(GoerliFlag.Name) {
return filepath.Join(path, "goerli")
}
@@ -1038,7 +1044,12 @@ func setNodeUserIdent(ctx *cli.Context, cfg *node.Config) {
// 4. default to mainnet nodes
func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
urls := params.MainnetBootnodes
if ctx.IsSet(BootnodesFlag.Name) {
// begin PluGeth injection
if pluginUrls := pluginSetBootstrapNodes(); pluginUrls != nil {
urls = pluginUrls
}
// end PluGeth injection
if ctx.IsSet(BootnodesFlag.Name) {
urls = SplitAndTrim(ctx.String(BootnodesFlag.Name))
} else {
if cfg.BootstrapNodes != nil {
@@ -1490,7 +1501,13 @@ func setSmartCard(ctx *cli.Context, cfg *node.Config) {
}
func SetDataDir(ctx *cli.Context, cfg *node.Config) {
// begin PluGeth injection
pluginPath := pluginDefaultDataDir(node.DefaultDataDir())
switch {
case pluginPath != "" && ctx.String(DataDirFlag.Name) == node.DefaultDataDir():
log.Error("Inside datdir injection number two")
cfg.DataDir = pluginPath
// end PluGeth injection
case ctx.IsSet(DataDirFlag.Name):
cfg.DataDir = ctx.String(DataDirFlag.Name)
case ctx.Bool(DeveloperFlag.Name):
@@ -1669,6 +1686,20 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
setRequiredBlocks(ctx, cfg)
setLes(ctx, cfg)
// beginPluGethInjection
if pluginNetworkId := pluginNetworkId(); pluginNetworkId != nil {
cfg.NetworkId = *pluginNetworkId
}
if cfg.EthDiscoveryURLs == nil {
var lightMode bool
if cfg.SyncMode == downloader.LightSync {
lightMode = true
}
cfg.EthDiscoveryURLs = pluginETHDiscoveryURLs(lightMode)
cfg.SnapDiscoveryURLs = pluginSnapDiscoveryURLs()
}
//end PluGeth injection
// Cap the cache allowance and tune the garbage collector
mem, err := gopsutil.VirtualMemory()
if err == nil {
@@ -1889,6 +1920,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
}
}
//begin plugeth injection
if genesis := pluginGenesisBlock(); genesis != nil {
chaindb := MakeChainDatabase(ctx, stack, false)
cfg.Genesis = genesis
rawdb.WriteChainConfig(chaindb, genesis.ToBlock().Hash(), genesis.Config)
chaindb.Close()
}
//end plugeth injection
// Set any dangling config values
if ctx.String(CryptoKZGFlag.Name) != "gokzg" && ctx.String(CryptoKZGFlag.Name) != "ckzg" {
Fatalf("--%s flag must be 'gokzg' or 'ckzg'", CryptoKZGFlag.Name)
@@ -2141,6 +2182,11 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
case ctx.Bool(DeveloperFlag.Name):
Fatalf("Developer chains are ephemeral")
}
//begin plugeth injection
if genesis == nil {
genesis = pluginGenesisBlock()
}
//end plugeth injection
return genesis
}
+139
View File
@@ -0,0 +1,139 @@
package utils
import (
"encoding/json"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/plugins"
)
func DefaultDataDir(pl *plugins.PluginLoader, path string) string {
dataDirPath := ""
fnList := pl.Lookup("SetDefaultDataDir", func(item interface{}) bool {
_, ok := item.(func(string) string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(string) string); ok {
dataDirPath = fn(path)
}
}
return dataDirPath
}
func pluginDefaultDataDir(path string) string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting DefaultDataDir, but default PluginLoader has not been initialized")
return ""
}
return DefaultDataDir(plugins.DefaultPluginLoader, path)
}
func PluginSetBootStrapNodes(pl *plugins.PluginLoader) []string {
var urls []string
fnList := pl.Lookup("SetBootstrapNodes", func(item interface{}) bool {
_, ok := item.(func() []string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() []string); ok {
urls = fn()
}
}
return urls
}
func pluginSetBootstrapNodes() []string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting pluginSetBootStrapNodes, but default PluginLoader has not been initialized")
return nil
}
return PluginSetBootStrapNodes(plugins.DefaultPluginLoader)
}
func PluginNetworkId(pl *plugins.PluginLoader) *uint64 {
var networkId *uint64
fnList := pl.Lookup("SetNetworkId", func(item interface{}) bool {
_, ok := item.(func() *uint64)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() *uint64); ok {
networkId = fn()
}
}
return networkId
}
func pluginNetworkId() *uint64 {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting pluginNetworkID, but default PluginLoader has not been initialized")
return nil
}
return PluginNetworkId(plugins.DefaultPluginLoader)
}
func PluginETHDiscoveryURLs(pl *plugins.PluginLoader, mode bool) []string {
var ethDiscoveryURLs []string
fnList := pl.Lookup("SetETHDiscoveryURLs", func(item interface{}) bool {
_, ok := item.(func(bool) []string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(bool) []string); ok {
ethDiscoveryURLs = fn(mode)
}
}
return ethDiscoveryURLs
}
func pluginETHDiscoveryURLs(mode bool) []string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting pluginETHDiscoveryURLs, but default PluginLoader has not been initialized")
return nil
}
return PluginETHDiscoveryURLs(plugins.DefaultPluginLoader, mode)
}
func PluginSnapDiscoveryURLs(pl *plugins.PluginLoader) []string {
var snapDiscoveryURLs []string
fnList := pl.Lookup("SetSnapDiscoveryURLs", func(item interface{}) bool {
_, ok := item.(func() []string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() []string); ok {
snapDiscoveryURLs = fn()
}
}
return snapDiscoveryURLs
}
func pluginSnapDiscoveryURLs() []string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PluginSnapDiscoveryURLs, but default PluginLoader has not been initialized")
return nil
}
return PluginSnapDiscoveryURLs(plugins.DefaultPluginLoader)
}
func PluginGenesisBlock(pl *plugins.PluginLoader) *core.Genesis {
genesisJSON, ok := plugins.LookupOne[func() []byte](pl, "GenesisBlock")
if !ok {
return nil
}
var genesis core.Genesis
if err := json.Unmarshal(genesisJSON(), &genesis); err != nil {
log.Warn("Error unmarshalling genesis", "err", err)
return nil
}
return &genesis
}
func pluginGenesisBlock() *core.Genesis {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PluginGenesisBlock, but default PluginLoader has not been initialized")
return nil
}
return PluginGenesisBlock(plugins.DefaultPluginLoader)
}