2021-06-26 03:38:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-13 19:57:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
|
|
gcore "github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
2023-09-14 23:34:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
2021-09-09 19:51:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2023-09-13 19:57:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
|
|
|
|
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
|
|
|
|
|
2021-09-09 19:51:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/plugins"
|
|
|
|
"github.com/ethereum/go-ethereum/plugins/wrappers"
|
2023-09-13 19:57:48 +00:00
|
|
|
|
2021-09-09 19:51:13 +00:00
|
|
|
"github.com/openrelayxyz/plugeth-utils/core"
|
|
|
|
"github.com/openrelayxyz/plugeth-utils/restricted"
|
2023-09-13 19:57:48 +00:00
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-06-26 03:38:04 +00:00
|
|
|
)
|
|
|
|
|
2021-08-31 20:29:09 +00:00
|
|
|
func apiTranslate(apis []core.API) []rpc.API {
|
2021-09-09 19:51:13 +00:00
|
|
|
result := make([]rpc.API, len(apis))
|
|
|
|
for i, api := range apis {
|
|
|
|
result[i] = rpc.API{
|
|
|
|
Namespace: api.Namespace,
|
|
|
|
Version: api.Version,
|
|
|
|
Service: api.Service,
|
|
|
|
Public: api.Public,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
2021-08-31 20:29:09 +00:00
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
|
2021-08-31 20:29:09 +00:00
|
|
|
func GetAPIsFromLoader(pl *plugins.PluginLoader, stack *node.Node, backend restricted.Backend) []rpc.API {
|
2021-09-09 19:51:13 +00:00
|
|
|
result := []core.API{}
|
|
|
|
fnList := pl.Lookup("GetAPIs", func(item interface{}) bool {
|
|
|
|
switch item.(type) {
|
|
|
|
case func(core.Node, restricted.Backend) []core.API:
|
|
|
|
return true
|
|
|
|
case func(core.Node, core.Backend) []core.API:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
switch fn := fni.(type) {
|
|
|
|
case func(core.Node, restricted.Backend) []core.API:
|
|
|
|
result = append(result, fn(wrappers.NewNode(stack), backend)...)
|
|
|
|
case func(core.Node, core.Backend) []core.API:
|
|
|
|
result = append(result, fn(wrappers.NewNode(stack), backend)...)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiTranslate(result)
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:29:09 +00:00
|
|
|
func pluginGetAPIs(stack *node.Node, backend restricted.Backend) []rpc.API {
|
2021-06-26 03:38:04 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting GetAPIs, but default PluginLoader has not been initialized")
|
|
|
|
return []rpc.API{}
|
2021-09-09 19:51:13 +00:00
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
return GetAPIsFromLoader(plugins.DefaultPluginLoader, stack, backend)
|
|
|
|
}
|
2021-07-12 19:45:42 +00:00
|
|
|
|
2021-08-31 20:29:09 +00:00
|
|
|
func InitializeNode(pl *plugins.PluginLoader, stack *node.Node, backend restricted.Backend) {
|
2021-09-09 19:51:13 +00:00
|
|
|
fnList := pl.Lookup("InitializeNode", func(item interface{}) bool {
|
|
|
|
switch item.(type) {
|
|
|
|
case func(core.Node, restricted.Backend):
|
|
|
|
return true
|
|
|
|
case func(core.Node, core.Backend):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
switch fn := fni.(type) {
|
|
|
|
case func(core.Node, restricted.Backend):
|
|
|
|
fn(wrappers.NewNode(stack), backend)
|
|
|
|
case func(core.Node, core.Backend):
|
|
|
|
fn(wrappers.NewNode(stack), backend)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 19:45:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:29:09 +00:00
|
|
|
func pluginsInitializeNode(stack *node.Node, backend restricted.Backend) {
|
2021-07-12 19:45:42 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting InitializeNode, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
InitializeNode(plugins.DefaultPluginLoader, stack, backend)
|
|
|
|
}
|
2022-07-29 20:34:42 +00:00
|
|
|
|
|
|
|
func OnShutdown(pl *plugins.PluginLoader) {
|
|
|
|
fnList := pl.Lookup("OnShutdown", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func())
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
fni.(func())()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pluginsOnShutdown() {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting OnShutdown, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
OnShutdown(plugins.DefaultPluginLoader)
|
|
|
|
}
|
2023-06-12 16:52:46 +00:00
|
|
|
|
2023-06-12 20:37:29 +00:00
|
|
|
func BlockChain(pl *plugins.PluginLoader) {
|
|
|
|
fnList := pl.Lookup("BlockChain", func(item interface{}) bool {
|
2023-06-12 16:52:46 +00:00
|
|
|
_, ok := item.(func())
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
fni.(func())()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 20:37:29 +00:00
|
|
|
func pluginBlockChain() {
|
2023-06-12 16:52:46 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2023-06-12 20:37:29 +00:00
|
|
|
log.Warn("Attempting BlockChain, but default PluginLoader has not been initialized")
|
2023-06-12 16:52:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-12 20:37:29 +00:00
|
|
|
BlockChain(plugins.DefaultPluginLoader)
|
2023-09-13 19:57:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 23:34:35 +00:00
|
|
|
func plugethCaptureTrieConfig(ctx *cli.Context, stack *node.Node, backend ethapi.Backend) *trie.Config {
|
2023-09-13 19:57:48 +00:00
|
|
|
|
2023-09-14 23:34:35 +00:00
|
|
|
ethCfg := new(ethconfig.Config)
|
2023-09-13 19:57:48 +00:00
|
|
|
|
2023-09-14 23:34:35 +00:00
|
|
|
if ctx.IsSet(utils.CacheFlag.Name) || ctx.IsSet(utils.CacheTrieFlag.Name) {
|
|
|
|
ethCfg.TrieCleanCache = ctx.Int(utils.CacheFlag.Name) * ctx.Int(utils.CacheTrieFlag.Name) / 100
|
|
|
|
}
|
|
|
|
if ctx.IsSet(utils.CacheNoPrefetchFlag.Name) {
|
|
|
|
ethCfg.NoPrefetch = ctx.Bool(utils.CacheNoPrefetchFlag.Name)
|
|
|
|
}
|
|
|
|
if ctx.IsSet(utils.CacheFlag.Name) || ctx.IsSet(utils.CacheGCFlag.Name) {
|
|
|
|
ethCfg.TrieDirtyCache = ctx.Int(utils.CacheFlag.Name) * ctx.Int(utils.CacheGCFlag.Name) / 100
|
|
|
|
}
|
|
|
|
if ctx.IsSet(utils.GCModeFlag.Name) {
|
|
|
|
ethCfg.NoPruning = ctx.String(utils.GCModeFlag.Name) == "archive"
|
|
|
|
}
|
|
|
|
if ctx.IsSet(utils.CacheFlag.Name) || ctx.IsSet(utils.CacheSnapshotFlag.Name) {
|
|
|
|
ethCfg.SnapshotCache = ctx.Int(utils.CacheFlag.Name) * ctx.Int(utils.CacheSnapshotFlag.Name) / 100
|
|
|
|
}
|
|
|
|
ethCfg.Preimages = ctx.Bool(utils.CachePreimagesFlag.Name)
|
|
|
|
if ethCfg.NoPruning && !ethCfg.Preimages {
|
|
|
|
ethCfg.Preimages = true
|
|
|
|
log.Info("Enabling recording of key preimages since archive mode is used")
|
|
|
|
}
|
|
|
|
if ctx.IsSet(utils.StateHistoryFlag.Name) {
|
|
|
|
ethCfg.StateHistory = ctx.Uint64(utils.StateHistoryFlag.Name)
|
2023-09-13 19:57:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 23:34:35 +00:00
|
|
|
chaindb := backend.ChainDb()
|
|
|
|
|
|
|
|
scheme, err := utils.ParseStateScheme(ctx, chaindb)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ethCfg.StateScheme = scheme
|
|
|
|
|
|
|
|
cacheCfg := &gcore.CacheConfig{
|
|
|
|
TrieCleanLimit: ethCfg.TrieCleanCache,
|
|
|
|
TrieCleanNoPrefetch: ethCfg.NoPrefetch,
|
|
|
|
TrieDirtyLimit: ethCfg.TrieDirtyCache,
|
|
|
|
TrieDirtyDisabled: ethCfg.NoPruning,
|
|
|
|
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
|
|
|
|
SnapshotLimit: ethCfg.SnapshotCache,
|
|
|
|
Preimages: ethCfg.Preimages,
|
|
|
|
StateHistory: ethCfg.StateHistory,
|
|
|
|
StateScheme: ethCfg.StateScheme,
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &trie.Config{Preimages: cacheCfg.Preimages}
|
|
|
|
if cacheCfg.StateScheme == rawdb.HashScheme {
|
2023-09-13 19:57:48 +00:00
|
|
|
config.HashDB = &hashdb.Config{
|
2023-09-14 23:34:35 +00:00
|
|
|
CleanCacheSize: cacheCfg.TrieCleanLimit * 1024 * 1024,
|
2023-09-13 19:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-14 23:34:35 +00:00
|
|
|
if cacheCfg.StateScheme == rawdb.PathScheme {
|
2023-09-13 19:57:48 +00:00
|
|
|
config.PathDB = &pathdb.Config{
|
2023-09-14 23:34:35 +00:00
|
|
|
StateHistory: cacheCfg.StateHistory,
|
|
|
|
CleanCacheSize: cacheCfg.TrieCleanLimit * 1024 * 1024,
|
|
|
|
DirtyCacheSize: cacheCfg.TrieDirtyLimit * 1024 * 1024,
|
2023-09-13 19:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
2023-09-14 23:34:35 +00:00
|
|
|
}
|