Skip to content

Commit

Permalink
Add l1_height metric
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored and omerfirmak committed Nov 22, 2023
1 parent 2696a86 commit a62fb6a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
19 changes: 19 additions & 0 deletions l1/event_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package l1

import (
"github.com/NethermindEth/juno/core"
)

type EventListener interface {
OnNewL1Head(head *core.L1Head)
}

type SelectiveListener struct {
OnNewL1HeadCb func(head *core.L1Head)
}

func (l SelectiveListener) OnNewL1Head(head *core.L1Head) {
if l.OnNewL1HeadCb != nil {
l.OnNewL1HeadCb(head)
}
}
8 changes: 8 additions & 0 deletions l1/l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Client struct {
resubscribeDelay time.Duration
pollFinalisedInterval time.Duration
nonFinalisedLogs map[uint64]*contract.StarknetLogStateUpdate
listener EventListener
}

var _ service.Service = (*Client)(nil)
Expand All @@ -45,9 +46,15 @@ func NewClient(l1 Subscriber, chain *blockchain.Blockchain, log utils.SimpleLogg
resubscribeDelay: 10 * time.Second,
pollFinalisedInterval: time.Minute,
nonFinalisedLogs: make(map[uint64]*contract.StarknetLogStateUpdate, 0),
listener: SelectiveListener{},
}
}

func (c *Client) WithEventListener(l EventListener) *Client {
c.listener = l
return c
}

func (c *Client) WithResubscribeDelay(delay time.Duration) *Client {
c.resubscribeDelay = delay
return c
Expand Down Expand Up @@ -204,6 +211,7 @@ func (c *Client) setL1Head(ctx context.Context) error {
if err := c.l2Chain.SetL1Head(head); err != nil {
return fmt.Errorf("l1 head for block %d and state root %s: %w", head.BlockNumber, head.StateRoot.String(), err)
}
c.listener.OnNewL1Head(head)
c.log.Infow("Updated l1 head",
"blockNumber", head.BlockNumber,
"blockHash", head.BlockHash.ShortString(),
Expand Down
59 changes: 59 additions & 0 deletions l1/l1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"time"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/l1"
"github.com/NethermindEth/juno/l1/contract"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -95,3 +98,59 @@ func TestMismatchedChainID(t *testing.T) {
err := client.Run(ctx)
require.ErrorContains(t, err, "mismatched L1 and L2 networks")
}

func TestEventListener(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
nopLog := utils.NewNopZapLogger()
network := utils.Mainnet
chain := blockchain.New(pebble.NewMemTest(t), network, nopLog)

subscriber := mocks.NewMockSubscriber(ctrl)
subscriber.
EXPECT().
WatchLogStateUpdate(gomock.Any(), gomock.Any()).
Do(func(_ context.Context, sink chan<- *contract.StarknetLogStateUpdate) {
sink <- &contract.StarknetLogStateUpdate{
GlobalRoot: new(big.Int),
BlockNumber: new(big.Int),
BlockHash: new(big.Int),
}
}).
Return(newFakeSubscription(), nil).
Times(1)

subscriber.
EXPECT().
FinalisedHeight(gomock.Any()).
Return(uint64(0), nil).
AnyTimes()

subscriber.
EXPECT().
ChainID(gomock.Any()).
Return(network.DefaultL1ChainID(), nil).
Times(1)

subscriber.EXPECT().Close().Times(1)

var got *core.L1Head
client := l1.NewClient(subscriber, chain, nopLog).
WithResubscribeDelay(0).
WithPollFinalisedInterval(time.Nanosecond).
WithEventListener(l1.SelectiveListener{
OnNewL1HeadCb: func(head *core.L1Head) {
got = head
},
})

ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
require.NoError(t, client.Run(ctx))
cancel()

require.Equal(t, &core.L1Head{
BlockHash: new(felt.Felt),
StateRoot: new(felt.Felt),
}, got)
}
16 changes: 16 additions & 0 deletions node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"time"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/jsonrpc"
"github.com/NethermindEth/juno/l1"
"github.com/NethermindEth/juno/sync"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -212,3 +214,17 @@ func makeBlockchainMetrics() blockchain.EventListener {
},
}
}

func makeL1Metrics() l1.EventListener {
l1Height := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "l1",
Name: "height",
})
prometheus.MustRegister(l1Height)

return l1.SelectiveListener{
OnNewL1HeadCb: func(head *core.L1Head) {
l1Height.Set(float64(head.BlockNumber))
},
}
}
4 changes: 3 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
if err != nil {
return nil, fmt.Errorf("create L1 client: %w", err)
}

if cfg.Metrics {
l1Client.WithEventListener(makeL1Metrics())
}
n.services = append(n.services, l1Client)
}

Expand Down

0 comments on commit a62fb6a

Please sign in to comment.