Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 61 additions & 11 deletions l1/l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"errors"
"fmt"
"math/big"
"time"
Expand Down Expand Up @@ -131,20 +132,35 @@
}
}

// checkChainID checks that the client is connected to the right L1 client
func (c *Client) checkChainID(ctx context.Context) error {
gotChainID, err := c.l1.ChainID(ctx)
const chainIDCheckTimeout = 30 * time.Second
Comment thread
rodrodros marked this conversation as resolved.
ctx, cancel := context.WithTimeout(ctx, chainIDCheckTimeout)
defer cancel()

l1ChainID, err := c.l1.ChainID(ctx)
if err != nil {
return fmt.Errorf("retrieve Ethereum chain ID: %w", err)
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf(
"eth_chainId did not respond within %s; is --eth-node a valid Ethereum L1 RPC URL?",
chainIDCheckTimeout,
)
}
return fmt.Errorf("retrieving Ethereum chain ID: %w", err)
}

wantChainID := c.network.L1ChainID
if gotChainID.Cmp(wantChainID) == 0 {
expectedL1ChainID := c.network.L1ChainID
if l1ChainID.Cmp(expectedL1ChainID) == 0 {
return nil
}

// NOTE: for now we return an error. If we want to support users who fork
// Starknet to create a "custom" Starknet network, we will need to log a warning instead.
return fmt.Errorf("mismatched L1 and L2 networks: L2 network %s; is the L1 node on the correct network?", c.network.String())
return fmt.Errorf(
"mismatched network id between L1 and L2. L2 network is %s; "+
"is --eth-node pointing to the right network?",
c.network.String(),
)
}

func (c *Client) Run(ctx context.Context) error {
Expand Down Expand Up @@ -261,13 +277,36 @@
// caller (Run) treats the error as best-effort and proceeds to the live
// subscription, so the partial state is an additive head-start, not a leak.
func (c *Client) catchUpL1HeadUpdates(ctx context.Context) error {
latest, err := c.l1.LatestHeight(ctx)
const (
heightCallTimeout = 30 * time.Second
filterCallTimeout = 60 * time.Second
)

latestCtx, cancelLatest := context.WithTimeout(ctx, heightCallTimeout)
latest, err := c.l1.LatestHeight(latestCtx)
cancelLatest()
if err != nil {
return fmt.Errorf("L1 catch-up: failed to get latest height: %w", err)
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf(
"eth_blockNumber did not respond within %s; is --eth-node responsive?",
heightCallTimeout,
)
}
return fmt.Errorf("failed to get latest height: %w", err)

Check warning on line 295 in l1/l1.go

View check run for this annotation

Codecov / codecov/patch

l1/l1.go#L295

Added line #L295 was not covered by tests
}
finalised, err := c.l1.FinalisedHeight(ctx)

finalisedCtx, cancelFinalised := context.WithTimeout(ctx, heightCallTimeout)
finalised, err := c.l1.FinalisedHeight(finalisedCtx)
cancelFinalised()
if err != nil {
return fmt.Errorf("L1 catch-up: failed to get finalised height: %w", err)
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf(
`eth_getBlockByNumber("finalized") did not respond within %s; `+
`does --eth-node support the finalized block tag?`,
heightCallTimeout,
)
}
return fmt.Errorf("failed to get finalised height: %w", err)

Check warning on line 309 in l1/l1.go

View check run for this annotation

Codecov / codecov/patch

l1/l1.go#L309

Added line #L309 was not covered by tests
}

c.logger.Info("L1 catch-up starting",
Expand All @@ -287,8 +326,16 @@
if to+1 > c.catchUpChunkSize {
from = to + 1 - c.catchUpChunkSize
}
events, err := c.l1.FilterLogStateUpdate(ctx, from, to)
filterCtx, cancelFilter := context.WithTimeout(ctx, filterCallTimeout)
events, err := c.l1.FilterLogStateUpdate(filterCtx, from, to)
cancelFilter()
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf(
"eth_getLogs did not respond within %s; is --eth-node responsive?",
filterCallTimeout,
)
}
return err
}
chunks++
Expand All @@ -315,12 +362,15 @@
}

func (c *Client) finalisedHeight(ctx context.Context) uint64 {
const finalisedHeightTimeout = 30 * time.Second
for {
select {
case <-ctx.Done():
return 0
default:
finalisedHeight, err := c.l1.FinalisedHeight(ctx)
callCtx, cancel := context.WithTimeout(ctx, finalisedHeightTimeout)
finalisedHeight, err := c.l1.FinalisedHeight(callCtx)
cancel()
if err == nil {
return finalisedHeight
}
Expand Down
Loading
Loading