diff --git a/zebrad/src/commands/start.rs b/zebrad/src/commands/start.rs index 401558be5..ad8f43d31 100644 --- a/zebrad/src/commands/start.rs +++ b/zebrad/src/commands/start.rs @@ -30,7 +30,7 @@ use zebra_chain::block::BlockHeaderHash; mod sync; // 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, 29, 170, 27, 145, 113, 132, 236, 232, 15, 4, 0, ]); diff --git a/zebrad/src/commands/start/sync.rs b/zebrad/src/commands/start/sync.rs index 1ead8f390..a28bf5c35 100644 --- a/zebrad/src/commands/start/sync.rs +++ b/zebrad/src/commands/start/sync.rs @@ -161,11 +161,13 @@ where }) .await; match res.map_err::(|e| eyre!(e)) { - Ok(zn::Response::BlockHeaderHashes(hashes)) => { - if hashes.is_empty() { + Ok(zn::Response::BlockHeaderHashes(mut hashes)) => { + let new_tip = if let Some(tip) = hashes.pop() { + tip + } else { tracing::debug!("skipping empty response"); continue; - } + }; // ExtendTips Step 3 // @@ -174,22 +176,20 @@ where // It indicates that the remote peer does not have any blocks // following the prospective tip. // TODO(jlusby): reject both main and test net genesis blocks - if hashes[0] == super::GENESIS { - tracing::debug!("skipping response that does not extend the tip"); - continue; + match hashes.first() { + Some(&super::GENESIS) => { + tracing::debug!("skipping response that does not extend the tip"); + continue; + } + Some(_) | None => {} } // ExtendTips Step 4 // // Combine the last elements of the remaining responses into // 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); - // 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); } Ok(r) => tracing::error!("unexpected response {:?}", r), @@ -198,8 +198,20 @@ where } } - self.request_blocks(download_set.into_iter().collect()) - .await?; + 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?; Ok(()) }