Skip to content

Commit

Permalink
Initial commit of xplugeth injection and hooks in internal/cli/server/
Browse files Browse the repository at this point in the history
  • Loading branch information
philip-morlier committed Sep 10, 2024
1 parent 2a10448 commit 061ff1c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/naoina/toml v0.1.1
github.com/olekukonko/tablewriter v0.0.5
github.com/openrelayxyz/xplugeth v0.0.0-build-test2
github.com/pelletier/go-toml v1.9.5
github.com/peterh/liner v1.2.2
github.com/protolambda/bls12-381-util v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,8 @@ github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJK
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/openrelayxyz/xplugeth v0.0.0-build-test2 h1:zq75jb0VNgUTiVvyM+O7fe/89bgeLxgUNXvycu09rMw=
github.com/openrelayxyz/xplugeth v0.0.0-build-test2/go.mod h1:JcModBzVLMJN2oaIk4kO+rN9ia5Ve3r3QhbA6bjrlfs=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
Expand Down
23 changes: 23 additions & 0 deletions internal/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import (
"github.com/ethereum/go-ethereum/metrics/prometheus"
"github.com/ethereum/go-ethereum/node"

"github.com/openrelayxyz/xplugeth"
xtypes "github.com/openrelayxyz/xplugeth/types"

// Force-load the tracer engines to trigger registration
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
Expand Down Expand Up @@ -131,6 +134,10 @@ func NewServer(config *Config, opts ...serverOption) (*Server, error) {
}
}

//begin xplugeth injection
xplugeth.Initialize()
//end xplugeth injection

// load the chain genesis
if err = config.loadChain(); err != nil {
return nil, err
Expand Down Expand Up @@ -283,11 +290,23 @@ func NewServer(config *Config, opts ...serverOption) (*Server, error) {
// Set the node instance
srv.node = stack

// begin xplugeth injection
xplugeth.StoreSingleton[*node.Node](stack)
xplugeth.StoreSingleton[xtypes.Backend](srv.backend.APIBackend)
pluginInitializeNode()
stack.RegisterAPIs(pluginGetAPIs())
// end xplugeth injection

// start the node
if err := srv.node.Start(); err != nil {
return nil, err
}

//begin xplugeth injection
pluginBlockchain()
//end xplugeth injection


return srv, nil
}

Expand All @@ -306,6 +325,10 @@ func (s *Server) Stop() {
log.Error("Failed to shutdown open telemetry tracer")
}
}

//begin xplugeth injection
defer pluginOnShutdown()
//end xplugeth injection
}

func (s *Server) setupMetrics(config *TelemetryConfig, serviceName string) error {
Expand Down
74 changes: 74 additions & 0 deletions internal/cli/server/xplugeth_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package server

import (
"github.com/openrelayxyz/xplugeth"
"github.com/openrelayxyz/xplugeth/types"

"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
)

type initializer interface {
InitializeNode(*node.Node, types.Backend)
}
type shutdown interface {
Shutdown()
}
type blockchain interface {
Blockchain()
}
type getAPIs interface {
GetAPIs(*node.Node, types.Backend) []rpc.API
}

func init() {
xplugeth.RegisterHook[initializer]()
xplugeth.RegisterHook[shutdown]()
xplugeth.RegisterHook[blockchain]()
xplugeth.RegisterHook[getAPIs]()
}

func pluginInitializeNode() {
stack, ok := xplugeth.GetSingleton[*node.Node]()
if !ok {
panic("*node.Node singleton not set, xplugeth InitializeNode")
}
backend, ok := xplugeth.GetSingleton[types.Backend]()
if !ok {
panic("types.Backend singleton not set, xplugeth Initializenode")
}
for _, init := range xplugeth.GetModules[initializer]() {
init.InitializeNode(stack, backend)
}
}

func pluginGetAPIs() []rpc.API {
result := []rpc.API{}

stack, ok := xplugeth.GetSingleton[*node.Node]()
if !ok {
panic("*node.Node singleton not set, xplugeth GetAPIs")
}
backend, ok := xplugeth.GetSingleton[types.Backend]()
if !ok {
panic("types.Backend singleton not set xplugeth GetAPIs")
}

for _, a := range xplugeth.GetModules[getAPIs]() {
result = append(result, a.GetAPIs(stack, backend)...)
}

return result
}

func pluginOnShutdown() {
for _, shutdown := range xplugeth.GetModules[shutdown]() {
shutdown.Shutdown()
}
}

func pluginBlockchain() {
for _, b := range xplugeth.GetModules[blockchain]() {
b.Blockchain()
}
}
7 changes: 7 additions & 0 deletions internal/cli/server/xplugeth_imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package server

// +build example_plugin

import (
_ "github.com/openrelayxyz/xplugeth/plugins/example"
)

0 comments on commit 061ff1c

Please sign in to comment.