Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Fetch missing blocks #5

Merged
merged 6 commits into from
Dec 13, 2023
Merged

Conversation

0xKitsune
Copy link
Contributor

This PR introduces logic to fetch missing blocks due to any potential interruptions of the index_chain task. Rather than spinning up a new task, this approach backfills blocks before listening to the blocks_stream. Since the WsClient's subscribe_blocks implementation in ethers-rs uses an UnboundedReciever, new blocks are buffered as backfilling occurs.

pub async fn index_chain(app: Arc<App>, chain_id: u64) -> eyre::Result<()> {
    loop {
        let ws_rpc = app.ws_provider(chain_id).await?;
        let rpc = app.http_provider(chain_id).await?;

        // Subscribe to new block with the WS client which uses an unbounded receiver, buffering the stream
        let mut blocks_stream = ws_rpc.subscribe_blocks().await?;

        // Get the first block from the stream, backfilling any missing blocks from the latest block in the db to the chain head
        if let Some(latest_block) = blocks_stream.next().await {
            backfill_to_block(app.clone(), chain_id, &rpc, latest_block)
                .await?;
        }

        // Index incoming blocks from the stream
        while let Some(block) = blocks_stream.next().await {
            index_block(app.clone(), chain_id, &rpc, block).await?;
        }
    }
}

@0xKitsune 0xKitsune changed the base branch from main to dev December 13, 2023 03:16
@0xKitsune 0xKitsune merged commit b612dc8 into dev Dec 13, 2023
4 checks passed
@0xKitsune 0xKitsune deleted the 0xkitsune/fetch-missing-blocks branch December 13, 2023 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants