update plugeth-utils version

This commit is contained in:
Austin Roberts
2021-08-31 15:29:09 -05:00
parent 40377543bf
commit d9d51dd345
5 changed files with 446 additions and 23 deletions
+4 -2
View File
@@ -41,6 +41,7 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/plugins/wrappers"
"gopkg.in/urfave/cli.v1"
)
@@ -314,7 +315,8 @@ func geth(ctx *cli.Context) error {
if err := plugins.Initialize(path.Join(ctx.GlobalString(utils.DataDirFlag.Name), "plugins"), ctx); err != nil { return err }
prepare(ctx)
stack, backend := makeFullNode(ctx)
pluginsInitializeNode(stack, backend)
wrapperBackend := wrappers.NewBackend(backend)
pluginsInitializeNode(stack, wrapperBackend)
if ok, err := plugins.RunSubcommand(ctx); ok {
stack.Close()
return err
@@ -325,7 +327,7 @@ func geth(ctx *cli.Context) error {
return fmt.Errorf("invalid command: %q", args[0])
}
}
stack.RegisterAPIs(pluginGetAPIs(stack, backend))
stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend))
startNode(ctx, stack, backend)
stack.Wait()
+52 -19
View File
@@ -3,28 +3,51 @@ package main
import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/plugins/interfaces"
"github.com/ethereum/go-ethereum/plugins/wrappers"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/log"
"github.com/openrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/restricted"
)
type APILoader func(*node.Node, interfaces.Backend) []rpc.API
func GetAPIsFromLoader(pl *plugins.PluginLoader, stack *node.Node, backend interfaces.Backend) []rpc.API {
result := []rpc.API{}
fnList := pl.Lookup("GetAPIs", func(item interface{}) bool {
_, ok := item.(func(*node.Node, interfaces.Backend) []rpc.API)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(*node.Node, interfaces.Backend) []rpc.API); ok {
result = append(result, fn(stack, backend)...)
func apiTranslate(apis []core.API) []rpc.API {
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
}
func pluginGetAPIs(stack *node.Node, backend interfaces.Backend) []rpc.API {
func GetAPIsFromLoader(pl *plugins.PluginLoader, stack *node.Node, backend restricted.Backend) []rpc.API {
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)
}
func pluginGetAPIs(stack *node.Node, backend restricted.Backend) []rpc.API {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting GetAPIs, but default PluginLoader has not been initialized")
return []rpc.API{}
@@ -32,19 +55,29 @@ func pluginGetAPIs(stack *node.Node, backend interfaces.Backend) []rpc.API {
return GetAPIsFromLoader(plugins.DefaultPluginLoader, stack, backend)
}
func InitializeNode(pl *plugins.PluginLoader, stack *node.Node, backend interfaces.Backend) {
func InitializeNode(pl *plugins.PluginLoader, stack *node.Node, backend restricted.Backend) {
fnList := pl.Lookup("InitializeNode", func(item interface{}) bool {
_, ok := item.(func(*node.Node, interfaces.Backend))
return ok
switch item.(type) {
case func(core.Logger, core.Node, restricted.Backend):
return true
case func(core.Logger, core.Node, core.Backend):
return true
default:
return false
}
})
for _, fni := range fnList {
if fn, ok := fni.(func(*node.Node, interfaces.Backend)); ok {
fn(stack, backend)
switch fn := fni.(type) {
case func(core.Logger, core.Node, restricted.Backend):
fn(log.Root(), wrappers.NewNode(stack), backend)
case func(core.Logger, core.Node, core.Backend):
fn(log.Root(), wrappers.NewNode(stack), backend)
default:
}
}
}
func pluginsInitializeNode(stack *node.Node, backend interfaces.Backend) {
func pluginsInitializeNode(stack *node.Node, backend restricted.Backend) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting InitializeNode, but default PluginLoader has not been initialized")
return