Skip to content

Commit

Permalink
fetch lib only once in 5 min
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Jul 4, 2024
1 parent f15a004 commit a80dbdd
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions rpc/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type Fetcher struct {
logger *zap.Logger
latestBlockNum uint64

ethCallLIBParams ethRPC.CallParams
ethCallLIBParams ethRPC.CallParams
lastL1AcceptedBlock uint64
lastL1AcceptedBlockFetchTime time.Time
}

func NewFetcher(
Expand Down Expand Up @@ -81,10 +83,14 @@ func (f *Fetcher) Fetch(ctx context.Context, requestBlockNum uint64) (b *pbbstre
}
f.logger.Debug("block fetched successfully", zap.Uint64("block_num", requestBlockNum))

//todo: call only once every 5 min
lastL1AcceptedBlock, err := f.fetchLastL1AcceptBlock(ctx)
if err != nil {
return nil, false, fmt.Errorf("fetching LIB: %w", err)
if f.lastL1AcceptedBlock == 0 || time.Since(f.lastL1AcceptedBlockFetchTime) > time.Minute*5 {
f.logger.Info("fetching last L1 accepted block")
lastL1AcceptedBlock, err := f.fetchLastL1AcceptBlock(ctx)
if err != nil {
return nil, false, fmt.Errorf("fetching LIB: %w", err)
}
f.lastL1AcceptedBlock = lastL1AcceptedBlock
f.lastL1AcceptedBlockFetchTime = time.Now()
}

stateUpdate, err := FetchStateUpdate(ctx, f.starknetClients, requestBlockNum)
Expand All @@ -93,7 +99,7 @@ func (f *Fetcher) Fetch(ctx context.Context, requestBlockNum uint64) (b *pbbstre
}

f.logger.Info("converting block", zap.Uint64("block_num", requestBlockNum))
bstreamBlock, err := convertBlock(blockWithReceipts, stateUpdate, lastL1AcceptedBlock)
bstreamBlock, err := convertBlock(blockWithReceipts, stateUpdate, f.lastL1AcceptedBlock)
if err != nil {
return nil, false, fmt.Errorf("converting block %d from rpc response: %w", requestBlockNum, err)
}
Expand Down

0 comments on commit a80dbdd

Please sign in to comment.