Skip to content

Commit

Permalink
Merge pull request #6 from samcm/total-difficulty
Browse files Browse the repository at this point in the history
feat(merge): Add total difficulty from exe
  • Loading branch information
samcm committed May 10, 2022
2 parents 1cafe3c + 3ff0467 commit 27b18bc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/exporter/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package execution

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/sirupsen/logrus"
Expand All @@ -17,6 +18,7 @@ type Node interface {
ChainID(ctx context.Context) (int64, error)
MostRecentBlockNumber(ctx context.Context) (uint64, error)
EstimatedGasPrice(ctx context.Context) (float64, error)
TotalDifficulty(ctx context.Context) (uint64, error)
}

type node struct {
Expand Down Expand Up @@ -127,3 +129,19 @@ func (e *node) ChainID(ctx context.Context) (int64, error) {

return id.Int64(), nil
}

func (e *node) TotalDifficulty(ctx context.Context) (uint64, error) {
blockNumber, err := e.MostRecentBlockNumber(ctx)
if err != nil {
return 0, err
}

block, err := e.client.BlockByNumber(ctx, big.NewInt(int64(blockNumber)))
if err != nil {
return 0, err
}

e.metrics.ObserveTotalDifficulty(block.Difficulty().Uint64())

return block.Difficulty().Uint64(), nil
}
13 changes: 13 additions & 0 deletions pkg/exporter/execution/jobs/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type GeneralMetrics struct {
GasPrice prometheus.Gauge
NetworkID prometheus.Gauge
ChainID prometheus.Gauge
TotalDifficulty prometheus.Gauge
}

func NewGeneralMetrics(namespace string, constLabels map[string]string) GeneralMetrics {
Expand Down Expand Up @@ -45,6 +46,14 @@ func NewGeneralMetrics(namespace string, constLabels map[string]string) GeneralM
ConstLabels: constLabels,
},
),
TotalDifficulty: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "total_difficulty",
Help: "The total difficulty of the chain.",
ConstLabels: constLabels,
},
),
}
}

Expand All @@ -63,3 +72,7 @@ func (g *GeneralMetrics) ObserveNetworkID(networkID int64) {
func (g *GeneralMetrics) ObserveChainID(id int64) {
g.ChainID.Set(float64(id))
}

func (g *GeneralMetrics) ObserveTotalDifficulty(difficulty uint64) {
g.TotalDifficulty.Set(float64(difficulty))
}
6 changes: 6 additions & 0 deletions pkg/exporter/execution/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Metrics interface {
ObserveChainID(chainID int64)
ObserveMostRecentBlock(block int64)
ObserveGasPrice(gasPrice float64)
ObserveTotalDifficulty(difficulty uint64)
}

type metrics struct {
Expand Down Expand Up @@ -38,6 +39,7 @@ func NewMetrics(nodeName, namespace string) Metrics {
prometheus.MustRegister(m.generalMetrics.GasPrice)
prometheus.MustRegister(m.generalMetrics.MostRecentBlockNumber)
prometheus.MustRegister(m.generalMetrics.ChainID)
prometheus.MustRegister(m.generalMetrics.TotalDifficulty)

return m
}
Expand Down Expand Up @@ -65,3 +67,7 @@ func (m *metrics) ObserveGasPrice(gasPrice float64) {
func (m *metrics) ObserveChainID(chainID int64) {
m.generalMetrics.ObserveChainID(chainID)
}

func (m *metrics) ObserveTotalDifficulty(difficulty uint64) {
m.generalMetrics.ObserveTotalDifficulty(difficulty)
}
4 changes: 4 additions & 0 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ func (e *exporter) PollExecution(ctx context.Context) error {
e.log.WithError(err).Error("failed to get chain id")
}

if _, err := e.execution.TotalDifficulty(ctx); err != nil {
e.log.WithError(err).Error("failed to get total difficulty")
}

return nil
}

Expand Down

0 comments on commit 27b18bc

Please sign in to comment.