Skip to content

Commit

Permalink
chore(structure): Allow lazy-loading of downstream clients
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed May 5, 2022
1 parent 92664d1 commit 43b6ee6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
34 changes: 25 additions & 9 deletions pkg/exporter/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
type Node interface {
Name() string
URL() string
Bootstrapped() bool
Bootstrap(ctx context.Context) error
SyncStatus(ctx context.Context) (*SyncStatus, error)
}

Expand All @@ -25,18 +27,9 @@ type node struct {
}

func NewConsensusNode(ctx context.Context, log logrus.FieldLogger, name string, url string, metrics Metrics) (*node, error) {
client, err := http.New(ctx,
http.WithAddress(url),
http.WithLogLevel(zerolog.WarnLevel),
)
if err != nil {
log.WithError(err).Error("Failed to create consensus client")
}

return &node{
name: name,
url: url,
client: client,
log: log,
metrics: metrics,
}, nil
Expand All @@ -50,6 +43,29 @@ func (c *node) URL() string {
return c.url
}

func (c *node) Bootstrap(ctx context.Context) error {
client, err := http.New(ctx,
http.WithAddress(c.url),
http.WithLogLevel(zerolog.WarnLevel),
)
if err != nil {
return err
}

c.client = client

return nil
}

func (c *node) Bootstrapped() bool {
_, isProvider := c.client.(eth2client.NodeSyncingProvider)
if !isProvider {
return false
}

return true
}

func (c *node) refreshClient(ctx context.Context) error {
client, err := http.New(ctx,
http.WithAddress(c.url),
Expand Down
47 changes: 46 additions & 1 deletion pkg/exporter/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func (e *ethereum) Init(ctx context.Context) error {
return err
}

consensus.Bootstrap(ctx)

e.consensus = consensus
}

Expand All @@ -54,6 +56,8 @@ func (e *ethereum) Init(ctx context.Context) error {
return err
}

execution.Bootstrap(ctx)

e.execution = execution
}

Expand Down Expand Up @@ -84,11 +88,52 @@ func (e *ethereum) Serve(ctx context.Context, port int) error {
}

func (e *ethereum) Tick(ctx context.Context) {
if _, err := e.GetSyncStatus(ctx); err != nil {
if err := e.PollConsensus(ctx); err != nil {
e.log.Error(err)
}
if err := e.PollExecution(ctx); err != nil {
e.log.Error(err)
}
}

func (e *ethereum) PollConsensus(ctx context.Context) error {
if !e.config.Consensus.Enabled {
return nil
}

if !e.consensus.Bootstrapped() {
if err := e.consensus.Bootstrap(ctx); err != nil {
return err
}
}

// TODO(sam.calder-mason): Parallelize this
if _, err := e.consensus.SyncStatus(ctx); err != nil {
return err
}

return nil
}

func (e *ethereum) PollExecution(ctx context.Context) error {
if !e.config.Execution.Enabled {
return nil
}

if !e.execution.Bootstrapped() {
if err := e.execution.Bootstrap(ctx); err != nil {
return err
}
}

// TODO(sam.calder-mason): Parallelize this
if _, err := e.execution.SyncStatus(ctx); err != nil {
return err
}

return nil
}

func (e *ethereum) GetSyncStatus(ctx context.Context) (*SyncStatus, error) {
status := &SyncStatus{}
consensus, err := e.consensus.SyncStatus(ctx)
Expand Down
23 changes: 17 additions & 6 deletions pkg/exporter/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ import (
type Node interface {
Name() string
URL() string
Bootstrapped() bool
Bootstrap(ctx context.Context) error
SyncStatus(ctx context.Context) (*SyncStatus, error)
}

type node struct {
name string
url string
client ethclient.Client
client *ethclient.Client
log logrus.FieldLogger
metrics Metrics
}

func NewExecutionNode(ctx context.Context, log logrus.FieldLogger, name string, url string, metrics Metrics) (*node, error) {
// Make a dial call since its easier to create the client. We don't actually care if the
// client is alive or not at this point.
client, _ := ethclient.Dial(url)

return &node{
name: name,
url: url,
client: *client,
log: log,
metrics: metrics,
}, nil
Expand All @@ -43,6 +40,20 @@ func (e *node) URL() string {
return e.url
}

func (e *node) Bootstrapped() bool {
return e.client != nil
}

func (e *node) Bootstrap(ctx context.Context) error {
client, err := ethclient.Dial(e.url)
if err != nil {
return err
}

e.client = client
return nil
}

func (e *node) SyncStatus(ctx context.Context) (*SyncStatus, error) {
status, err := e.client.SyncProgress(ctx)
if err != nil {
Expand Down

0 comments on commit 43b6ee6

Please sign in to comment.