ust iterate over the locator...
@@ -408,41 +408,19 @@ impl NetToChainAdapter {
.expect("Failed to upgrade weak ref to our chain.")
}
- // recursively go back through the locator vector and stop when we find
- // a header that we recognize this will be a header shared in common
- // between us and the peer
+ // Find the first locator hash that refers to a known header on our main chain.
fn find_common_header(&self, locator: Vec<Hash>) -> Option<BlockHeader> {
- if locator.len() == 0 {
- return None;
+ for hash in locator {
+ if let Ok(header) = self.chain().get_block_header(&hash) {
+ if let Ok(header_at_height) = self.chain().get_header_by_height(header.height) {
+ if header.hash() == header_at_height.hash() {
+ return Some(header);
}
-
- let known = self.chain().get_block_header(&locator[0]);
-
- match known {
- Ok(header) => {
- // even if we know the block, it may not be on our winning chain
- let known_winning = self.chain().get_header_by_height(header.height);
- if let Ok(known_winning) = known_winning {
- if known_winning.hash() != header.hash() {
- self.find_common_header(locator[1..].to_vec())
- } else {
- Some(header)
- }
- } else {
- self.find_common_header(locator[1..].to_vec())
}
}
- Err(e) => match e.kind() {
- chain::ErrorKind::StoreErr(store::Error::NotFoundErr(_), _) => {
- self.find_common_header(locator[1..].to_vec())
}
- _ => {
- error!("Could not build header locator: {:?}", e);
None
}
- },
- }
- }
// pushing the new block through the chain pipeline
// remembering to reset the head if we have a bad block
