subspace_service/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use futures::StreamExt;
use sc_client_api::BlockchainEvents;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, Header, NumberFor};
use tracing::{debug, trace};

pub async fn wait_for_block_import<Block, Client>(
    client: &Client,
    waiting_block_number: NumberFor<Block>,
) where
    Block: BlockT,
    Client: HeaderBackend<Block> + BlockchainEvents<Block>,
{
    let mut blocks_stream = client.every_import_notification_stream();

    let info = client.info();
    debug!(
        %waiting_block_number,
        "Waiting client info: {:?}", info
    );

    if info.best_number >= waiting_block_number {
        return;
    }

    while let Some(block) = blocks_stream.next().await {
        let current_block_number = *block.header.number();
        trace!(%current_block_number, %waiting_block_number, "Waiting for the target block");

        if current_block_number >= waiting_block_number {
            return;
        }
    }
}