Skip to content

Commit

Permalink
ethclient moved to node.Node
Browse files Browse the repository at this point in the history
  • Loading branch information
mralj committed Oct 7, 2024
1 parent bd56bdc commit bdd7b7d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
11 changes: 5 additions & 6 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
cfg.Eth.OverrideVerkle = &v
}

// [rollup-geth]
// TODO: think about if there is better solution for this (eg. rollup config file)
if !ctx.IsSet(utils.L1NodeRPCEndpointFlag.Name) {
log.Crit("L1 node RPC endpoint URL not set", "flag", utils.L1NodeRPCEndpointFlag.Name)
}
backend, eth := utils.RegisterEthService(stack, &cfg.Eth, ctx.String(utils.L1NodeRPCEndpointFlag.Name))
backend, eth := utils.RegisterEthService(stack, &cfg.Eth)

// Create gauge with geth system and build information
if eth != nil { // The 'eth' backend may be nil in light mode
Expand Down Expand Up @@ -255,6 +250,10 @@ func makeFullNode(ctx *cli.Context) *node.Node {
utils.Fatalf("failed to register catalyst service: %v", err)
}
}

//[rollup-geth]
activateL1RPCEndpoint(ctx, stack)

return stack
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/geth/config_rollup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/urfave/cli/v2"
)

// TODO: when we have clearer picture of how do we want rollup "features" (EIPs/RIPs) to be activated
// make this "rule" activated (ie. if not "rule activated" then L1 client can simply be nil)
func activateL1RPCEndpoint(ctx *cli.Context, stack *node.Node) {
if !ctx.IsSet(utils.L1NodeRPCEndpointFlag.Name) {
log.Error("L1 node RPC endpoint URL not set", "flag", utils.L1NodeRPCEndpointFlag.Name)
return
}

l1RPCEndpoint := ctx.String(utils.L1NodeRPCEndpointFlag.Name)
stack.RegisterEthClient(l1RPCEndpoint)
}
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1925,8 +1925,8 @@ func SetDNSDiscoveryDefaults(cfg *ethconfig.Config, genesis common.Hash) {

// RegisterEthService adds an Ethereum client to the stack.
// The second return value is the full node instance.
func RegisterEthService(stack *node.Node, cfg *ethconfig.Config, l1RPCClientEndpoint string) (*eth.EthAPIBackend, *eth.Ethereum) {
backend, err := eth.New(stack, cfg, l1RPCClientEndpoint)
func RegisterEthService(stack *node.Node, cfg *ethconfig.Config) (*eth.EthAPIBackend, *eth.Ethereum) {
backend, err := eth.New(stack, cfg)
if err != nil {
Fatalf("Failed to register the Ethereum service: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type Ethereum struct {

// New creates a new Ethereum object (including the initialisation of the common Ethereum object),
// whose lifecycle will be managed by the provided node.
func New(stack *node.Node, config *ethconfig.Config, l1RPCEndpoint string) (*Ethereum, error) {
func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
// Ensure configuration values are compatible and sane
if !config.SyncMode.IsValid() {
return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode)
Expand Down Expand Up @@ -217,7 +217,7 @@ func New(stack *node.Node, config *ethconfig.Config, l1RPCEndpoint string) (*Eth
}

// [rollup-geth]
activateL1RPCEndpoint(l1RPCEndpoint, &vmConfig)
vmConfig.L1RpcClient = stack.EthClient()

eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, &config.TransactionHistory)
if err != nil {
Expand Down
19 changes: 0 additions & 19 deletions eth/backend_rollup.go

This file was deleted.

9 changes: 9 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/event"
Expand Down Expand Up @@ -67,6 +68,8 @@ type Node struct {
inprocHandler *rpc.Server // In-process RPC request handler to process the API requests

databases map[*closeTrackingDB]struct{} // All open databases

ethClient *ethclient.Client
}

const (
Expand Down Expand Up @@ -708,6 +711,12 @@ func (n *Node) EventMux() *event.TypeMux {
return n.eventmux
}

// [rollup-geth]
// EthClient returns instance of ETH RPC client
func (n *Node) EthClient() *ethclient.Client {
return n.ethClient
}

// OpenDatabase opens an existing database with the given name (or creates one if no
// previous can be found) from within the node's instance directory. If the node is
// ephemeral, a memory database is returned.
Expand Down
17 changes: 17 additions & 0 deletions node/node_rollup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package node

import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
)

func (n *Node) RegisterEthClient(endpoint string) {
ethClient, err := ethclient.Dial(endpoint)
if err != nil {
log.Error("Unable to connect to ETH RPC endpoint at", "URL", ethClient, "error", err)
return
}

n.ethClient = ethClient
log.Info("Initialized ETH RPC client", "endpoint", ethClient)
}

0 comments on commit bdd7b7d

Please sign in to comment.