Skip to content

Commit

Permalink
op-batcher/op-proposer: Only pass lifecycle contexts into RollupProvi…
Browse files Browse the repository at this point in the history
…der.RollupClient (#10705)

* Only pass lifecycle contexts into RollupProvider.RollupClient

* Ensure that L2EndpointProvider.EthClient is called without a timeout
  • Loading branch information
BrianBland authored Jun 4, 2024
1 parent af1d939 commit 70912c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
35 changes: 20 additions & 15 deletions op-batcher/batcher/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ func (l *BatchSubmitter) loadBlocksIntoState(ctx context.Context) error {

// loadBlockIntoState fetches & stores a single block into `state`. It returns the block it loaded.
func (l *BatchSubmitter) loadBlockIntoState(ctx context.Context, blockNumber uint64) (*types.Block, error) {
ctx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()
l2Client, err := l.EndpointProvider.EthClient(ctx)
if err != nil {
return nil, fmt.Errorf("getting L2 client: %w", err)
}
block, err := l2Client.BlockByNumber(ctx, new(big.Int).SetUint64(blockNumber))

cCtx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()

block, err := l2Client.BlockByNumber(cCtx, new(big.Int).SetUint64(blockNumber))
if err != nil {
return nil, fmt.Errorf("getting L2 block: %w", err)
}
Expand All @@ -208,13 +210,15 @@ func (l *BatchSubmitter) loadBlockIntoState(ctx context.Context, blockNumber uin
// calculateL2BlockRangeToStore determines the range (start,end] that should be loaded into the local state.
// It also takes care of initializing some local state (i.e. will modify l.lastStoredBlock in certain conditions)
func (l *BatchSubmitter) calculateL2BlockRangeToStore(ctx context.Context) (eth.BlockID, eth.BlockID, error) {
ctx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()
rollupClient, err := l.EndpointProvider.RollupClient(ctx)
if err != nil {
return eth.BlockID{}, eth.BlockID{}, fmt.Errorf("getting rollup client: %w", err)
}
syncStatus, err := rollupClient.SyncStatus(ctx)

cCtx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()

syncStatus, err := rollupClient.SyncStatus(cCtx)
// Ensure that we have the sync status
if err != nil {
return eth.BlockID{}, eth.BlockID{}, fmt.Errorf("failed to get sync status: %w", err)
Expand Down Expand Up @@ -337,23 +341,24 @@ func (l *BatchSubmitter) loop() {
// waitNodeSync Check to see if there was a batcher tx sent recently that
// still needs more block confirmations before being considered finalized
func (l *BatchSubmitter) waitNodeSync() error {
ctx, cancel := context.WithTimeout(l.shutdownCtx, l.Config.NetworkTimeout)
defer cancel()

ctx := l.shutdownCtx
rollupClient, err := l.EndpointProvider.RollupClient(ctx)
if err != nil {
return fmt.Errorf("failed to get rollup client: %w", err)
}

l1Tip, err := l.l1Tip(ctx)
cCtx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()

l1Tip, err := l.l1Tip(cCtx)
if err != nil {
return fmt.Errorf("failed to retrieve l1 tip: %w", err)
}

l1TargetBlock := l1Tip.Number
if l.Config.CheckRecentTxsDepth != 0 {
l.Log.Info("Checking for recently submitted batcher transactions on L1")
recentBlock, found, err := eth.CheckRecentTxs(ctx, l.L1Client, l.Config.CheckRecentTxsDepth, l.Txmgr.From())
recentBlock, found, err := eth.CheckRecentTxs(cCtx, l.L1Client, l.Config.CheckRecentTxsDepth, l.Txmgr.From())
if err != nil {
return fmt.Errorf("failed checking recent batcher txs: %w", err)
}
Expand Down Expand Up @@ -451,16 +456,16 @@ func (l *BatchSubmitter) publishTxToL1(ctx context.Context, queue *txmgr.Queue[t
}

func (l *BatchSubmitter) safeL1Origin(ctx context.Context) (eth.BlockID, error) {
ctx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()

c, err := l.EndpointProvider.RollupClient(ctx)
if err != nil {
log.Error("Failed to get rollup client", "err", err)
return eth.BlockID{}, fmt.Errorf("safe l1 origin: error getting rollup client: %w", err)
}

status, err := c.SyncStatus(ctx)
cCtx, cancel := context.WithTimeout(ctx, l.Config.NetworkTimeout)
defer cancel()

status, err := c.SyncStatus(cCtx)
if err != nil {
log.Error("Failed to get sync status", "err", err)
return eth.BlockID{}, fmt.Errorf("safe l1 origin: error getting sync status: %w", err)
Expand Down
23 changes: 13 additions & 10 deletions op-proposer/proposer/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,15 @@ func (l *L2OutputSubmitter) FetchNextOutputInfo(ctx context.Context) (*eth.Outpu
// FetchCurrentBlockNumber gets the current block number from the [L2OutputSubmitter]'s [RollupClient]. If the `AllowNonFinalized` configuration
// option is set, it will return the safe head block number, and if not, it will return the finalized head block number.
func (l *L2OutputSubmitter) FetchCurrentBlockNumber(ctx context.Context) (*big.Int, error) {
cCtx, cancel := context.WithTimeout(ctx, l.Cfg.NetworkTimeout)
defer cancel()
rollupClient, err := l.RollupProvider.RollupClient(cCtx)
rollupClient, err := l.RollupProvider.RollupClient(ctx)
if err != nil {
l.Log.Error("proposer unable to get rollup client", "err", err)
return nil, err
}

cCtx, cancel := context.WithTimeout(ctx, l.Cfg.NetworkTimeout)
defer cancel()

status, err := rollupClient.SyncStatus(cCtx)
if err != nil {
l.Log.Error("proposer unable to get sync status", "err", err)
Expand All @@ -268,15 +270,16 @@ func (l *L2OutputSubmitter) FetchCurrentBlockNumber(ctx context.Context) (*big.I
}

func (l *L2OutputSubmitter) FetchOutput(ctx context.Context, block *big.Int) (*eth.OutputResponse, bool, error) {
ctx, cancel := context.WithTimeout(ctx, l.Cfg.NetworkTimeout)
defer cancel()

rollupClient, err := l.RollupProvider.RollupClient(ctx)
if err != nil {
l.Log.Error("proposer unable to get rollup client", "err", err)
return nil, false, err
}
output, err := rollupClient.OutputAtBlock(ctx, block.Uint64())

cCtx, cancel := context.WithTimeout(ctx, l.Cfg.NetworkTimeout)
defer cancel()

output, err := rollupClient.OutputAtBlock(cCtx, block.Uint64())
if err != nil {
l.Log.Error("failed to fetch output at block", "block", block, "err", err)
return nil, false, err
Expand Down Expand Up @@ -431,15 +434,15 @@ func (l *L2OutputSubmitter) loop() {
}

func (l *L2OutputSubmitter) waitNodeSync() error {
ctx, cancel := context.WithTimeout(l.ctx, l.Cfg.NetworkTimeout)
cCtx, cancel := context.WithTimeout(l.ctx, l.Cfg.NetworkTimeout)
defer cancel()

l1head, err := l.Txmgr.BlockNumber(ctx)
l1head, err := l.Txmgr.BlockNumber(cCtx)
if err != nil {
return fmt.Errorf("failed to retrieve current L1 block number: %w", err)
}

rollupClient, err := l.RollupProvider.RollupClient(ctx)
rollupClient, err := l.RollupProvider.RollupClient(l.ctx)
if err != nil {
return fmt.Errorf("failed to get rollup client: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion op-service/dial/static_l2_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
// It does this by extending the RollupProvider interface to add the ability to get an EthClient
type L2EndpointProvider interface {
RollupProvider
// EthClient(ctx) returns the underlying ethclient pointing to the L2 execution node
// EthClient(ctx) returns the underlying ethclient pointing to the L2 execution node.
// Note: ctx should be a lifecycle context without an attached timeout as client selection may involve
// multiple network operations, specifically in the case of failover.
EthClient(ctx context.Context) (EthClientInterface, error)
}

Expand Down
4 changes: 3 additions & 1 deletion op-service/dial/static_rollup_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
// RollupProvider is an interface for providing a RollupClient
// It manages the lifecycle of the RollupClient for callers
type RollupProvider interface {
// RollupClient(ctx) returns the underlying sources.RollupClient pointing to the L2 rollup consensus node
// RollupClient(ctx) returns the underlying sources.RollupClient pointing to the L2 rollup consensus node.
// Note: ctx should be a lifecycle context without an attached timeout as client selection may involve
// multiple network operations, specifically in the case of failover.
RollupClient(ctx context.Context) (RollupClientInterface, error)
// Close() closes the underlying client or clients
Close()
Expand Down

0 comments on commit 70912c0

Please sign in to comment.