add message when syncing is done

This commit is contained in:
Boog900 2025-01-05 01:35:02 +00:00
parent 764c4663a0
commit a133490b31
No known key found for this signature in database
GPG key ID: 42AB1287CB0041C2

View file

@ -10,6 +10,7 @@ use tower::{Service, ServiceExt};
use tracing::instrument;
use cuprate_consensus::{BlockChainContext, BlockChainContextRequest, BlockChainContextResponse};
use cuprate_consensus_context::RawBlockChainContext;
use cuprate_p2p::{
block_downloader::{BlockBatch, BlockDownloaderConfig, ChainSvcRequest, ChainSvcResponse},
NetworkInterface, PeerSetRequest, PeerSetResponse,
@ -73,20 +74,7 @@ where
check_update_blockchain_context(&mut context_svc, &mut blockchain_ctx).await?;
let raw_blockchain_context = blockchain_ctx.unchecked_blockchain_context();
let PeerSetResponse::MostPoWSeen {
cumulative_difficulty,
..
} = clearnet_interface
.peer_set()
.ready()
.await?
.call(PeerSetRequest::MostPoWSeen)
.await?
else {
unreachable!();
};
if cumulative_difficulty <= raw_blockchain_context.cumulative_difficulty {
if !check_behind_peers(raw_blockchain_context, &mut clearnet_interface).await? {
continue;
}
@ -99,11 +87,18 @@ where
loop {
tokio::select! {
() = stop_current_block_downloader.notified() => {
tracing::info!("Stopping block downloader");
tracing::info!("Received stop signal, stopping block downloader");
break;
}
batch = block_batch_stream.next() => {
let Some(batch) = batch else {
check_update_blockchain_context(&mut context_svc, &mut blockchain_ctx).await?;
let raw_blockchain_context = blockchain_ctx.unchecked_blockchain_context();
if check_behind_peers(raw_blockchain_context, &mut clearnet_interface).await? {
tracing::info!("Synchronised with the network.");
}
break;
};
@ -117,6 +112,31 @@ where
}
}
/// Returns if we are behind the current connected network peers.
async fn check_behind_peers(
raw_blockchain_context: &RawBlockChainContext,
mut clearnet_interface: &mut NetworkInterface<ClearNet>,
) -> Result<bool, tower::BoxError> {
let PeerSetResponse::MostPoWSeen {
cumulative_difficulty,
..
} = clearnet_interface
.peer_set()
.ready()
.await?
.call(PeerSetRequest::MostPoWSeen)
.await?
else {
unreachable!();
};
if cumulative_difficulty <= raw_blockchain_context.cumulative_difficulty {
return Ok(false);
}
Ok(true)
}
/// Checks if we should update the given [`BlockChainContext`] and updates it if needed.
async fn check_update_blockchain_context<C>(
context_svc: C,