Skip to content

Commit

Permalink
Emulated blocks: fix potentially skipping blocks on a crash
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Sep 19, 2023
1 parent ba39e5f commit e4f609d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions engine/paima-funnel/src/funnels/emulated/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@ export class EmulatedBlocksFunnel extends BaseFunnel {
// 1) Check if time interval is complete. If yes, return the emulated block
// Do this in batch to speed up the initial sync
{
const completedBlocks = await this.getNextBlockBatch(blockHeight);
if (completedBlocks.length > 0) {
this.logRange(completedBlocks);
return completedBlocks;
const latestAvailableBlockNumber = this.sharedData.cacheManager.cacheEntries[
RpcCacheEntry.SYMBOL
]?.getState(ENV.CHAIN_ID);

// only check if time intervals are complete if we've made at least one RPC call
// otherwise, if we clear the cache due to an error being thrown,
// we might accidentally close an interval even though there is a block didn't include
if (latestAvailableBlockNumber?.state === RpcRequestState.HasResult) {
const completedBlocks = await this.getNextBlockBatch(blockHeight);
if (completedBlocks.length > 0) {
this.logRange(completedBlocks);
return completedBlocks;
}
}
}

Expand All @@ -98,10 +107,7 @@ export class EmulatedBlocksFunnel extends BaseFunnel {
const latestAvailableBlockNumber = this.sharedData.cacheManager.cacheEntries[
RpcCacheEntry.SYMBOL
]?.getState(ENV.CHAIN_ID);
if (
latestAvailableBlockNumber == null ||
latestAvailableBlockNumber.state === RpcRequestState.NotRequested
)
if (latestAvailableBlockNumber?.state !== RpcRequestState.HasResult)
throw new Error(`latestAvailableBlockNumber missing from cache for ${ENV.CHAIN_ID}`);

// check if the chunk we read matches the latest block known by the RPC endpoint
Expand Down

0 comments on commit e4f609d

Please sign in to comment.