correctly trim and download tips (#531)
* also download tips and filter tips * dispatch all block downloads together * tweek to match henry's changes * switch to more intuitive match Co-authored-by: Jane Lusby <jane@zfnd.org>
This commit is contained in:
parent
2abf4b93a8
commit
e102bd5e34
|
@ -30,7 +30,7 @@ use zebra_chain::block::BlockHeaderHash;
|
||||||
mod sync;
|
mod sync;
|
||||||
|
|
||||||
// genesis
|
// genesis
|
||||||
static GENESIS: BlockHeaderHash = BlockHeaderHash([
|
const GENESIS: BlockHeaderHash = BlockHeaderHash([
|
||||||
8, 206, 61, 151, 49, 176, 0, 192, 131, 56, 69, 92, 138, 74, 107, 208, 93, 161, 110, 38, 177,
|
8, 206, 61, 151, 49, 176, 0, 192, 131, 56, 69, 92, 138, 74, 107, 208, 93, 161, 110, 38, 177,
|
||||||
29, 170, 27, 145, 113, 132, 236, 232, 15, 4, 0,
|
29, 170, 27, 145, 113, 132, 236, 232, 15, 4, 0,
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -161,11 +161,13 @@ where
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
match res.map_err::<Report, _>(|e| eyre!(e)) {
|
match res.map_err::<Report, _>(|e| eyre!(e)) {
|
||||||
Ok(zn::Response::BlockHeaderHashes(hashes)) => {
|
Ok(zn::Response::BlockHeaderHashes(mut hashes)) => {
|
||||||
if hashes.is_empty() {
|
let new_tip = if let Some(tip) = hashes.pop() {
|
||||||
|
tip
|
||||||
|
} else {
|
||||||
tracing::debug!("skipping empty response");
|
tracing::debug!("skipping empty response");
|
||||||
continue;
|
continue;
|
||||||
}
|
};
|
||||||
|
|
||||||
// ExtendTips Step 3
|
// ExtendTips Step 3
|
||||||
//
|
//
|
||||||
|
@ -174,22 +176,20 @@ where
|
||||||
// It indicates that the remote peer does not have any blocks
|
// It indicates that the remote peer does not have any blocks
|
||||||
// following the prospective tip.
|
// following the prospective tip.
|
||||||
// TODO(jlusby): reject both main and test net genesis blocks
|
// TODO(jlusby): reject both main and test net genesis blocks
|
||||||
if hashes[0] == super::GENESIS {
|
match hashes.first() {
|
||||||
|
Some(&super::GENESIS) => {
|
||||||
tracing::debug!("skipping response that does not extend the tip");
|
tracing::debug!("skipping response that does not extend the tip");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Some(_) | None => {}
|
||||||
|
}
|
||||||
|
|
||||||
// ExtendTips Step 4
|
// ExtendTips Step 4
|
||||||
//
|
//
|
||||||
// Combine the last elements of the remaining responses into
|
// Combine the last elements of the remaining responses into
|
||||||
// a set, and add this set to the set of prospective tips.
|
// a set, and add this set to the set of prospective tips.
|
||||||
let new_tip = *hashes.last().expect("already checked is_empty");
|
|
||||||
let _ = self.prospective_tips.insert(new_tip);
|
let _ = self.prospective_tips.insert(new_tip);
|
||||||
|
|
||||||
// ExtendTips Step 5
|
|
||||||
//
|
|
||||||
// Combine all elements of the remaining responses into a
|
|
||||||
// set, and queue download and verification of those blocks
|
|
||||||
download_set.extend(hashes);
|
download_set.extend(hashes);
|
||||||
}
|
}
|
||||||
Ok(r) => tracing::error!("unexpected response {:?}", r),
|
Ok(r) => tracing::error!("unexpected response {:?}", r),
|
||||||
|
@ -198,7 +198,19 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.request_blocks(download_set.into_iter().collect())
|
self.prospective_tips
|
||||||
|
.retain(|tip| !download_set.contains(tip));
|
||||||
|
|
||||||
|
// ExtendTips Step 5
|
||||||
|
//
|
||||||
|
// Combine all elements of the remaining responses into a
|
||||||
|
// set, and queue download and verification of those blocks
|
||||||
|
self.request_blocks(
|
||||||
|
download_set
|
||||||
|
.into_iter()
|
||||||
|
.chain(self.prospective_tips.iter().cloned())
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in New Issue