Skip to content

Commit

Permalink
bootstrap from peer until reaching the current tip
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed Nov 27, 2020
1 parent 57bc4e7 commit ed2da0d
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions jormungandr/src/network/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,49 @@ pub async fn bootstrap_from_peer(
logger: Logger,
) -> Result<(), Error> {
use crate::network::convert::Decode;
use chain_impl_mockchain::header::Header;
use chain_network::data::BlockId;
use std::convert::TryFrom;

debug!(logger, "connecting to bootstrap peer {}", peer.connection);

let mut client = grpc::connect(&peer).await.map_err(Error::Connect)?;

let checkpoints = blockchain.get_checkpoints(tip.branch()).await;
let checkpoints = net_data::block::try_ids_from_iter(checkpoints).unwrap();
loop {
let remote_tip = client
.tip()
.await
.and_then(|header| header.decode())
.map_err(Error::TipFailed)?
.id();

let remote_tip: Header = client
.tip()
.await
.and_then(|header| header.decode())
.map_err(Error::TipFailed)?;
let remote_tip = BlockId::try_from(remote_tip.id().as_ref()).unwrap();
if remote_tip == tip.get_ref().await.hash() {
break Ok(());
}

info!(
logger,
"pulling blocks starting from checkpoints: {:?}; to tip {:?}", checkpoints, remote_tip,
);
let checkpoints = blockchain.get_checkpoints(tip.branch()).await;
let checkpoints = net_data::block::try_ids_from_iter(checkpoints).unwrap();

let stream = client
.pull_blocks(checkpoints, remote_tip)
.await
.map_err(Error::PullRequestFailed)?;
let remote_tip = BlockId::try_from(remote_tip.as_ref()).unwrap();

bootstrap_from_stream(blockchain, tip, stream, cancellation_token, logger).await
info!(
logger,
"pulling blocks starting from checkpoints: {:?}; to tip {:?}", checkpoints, remote_tip,
);

let stream = client
.pull_blocks(checkpoints, remote_tip)
.await
.map_err(Error::PullRequestFailed)?;

bootstrap_from_stream(
blockchain.clone(),
tip.clone(),
stream,
cancellation_token.clone(),
logger.clone(),
)
.await?;
}
}

struct BootstrapInfo {
Expand Down

0 comments on commit ed2da0d

Please sign in to comment.