fix get_locator which loop the whole headers (#2159)

This commit is contained in:
Gary Yu 2018-12-17 07:29:49 +08:00 committed by GitHub
parent c188b60a38
commit ea1c3a9d4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -69,7 +69,7 @@ impl HeaderSync {
// Rebuild the sync MMR to match our updated sync_head. // Rebuild the sync MMR to match our updated sync_head.
self.chain.rebuild_sync_mmr(&header_head).unwrap(); self.chain.rebuild_sync_mmr(&header_head).unwrap();
self.history_locator.clear(); self.history_locator.retain(|&x| x.0 == 0);
true true
} }
_ => false, _ => false,
@ -154,7 +154,7 @@ impl HeaderSync {
// for security, clear history_locator[] in any case of header chain rollback, // for security, clear history_locator[] in any case of header chain rollback,
// the easiest way is to check whether the sync head and the header head are identical. // the easiest way is to check whether the sync head and the header head are identical.
if self.history_locator.len() > 0 && tip.hash() != self.chain.header_head()?.hash() { if self.history_locator.len() > 0 && tip.hash() != self.chain.header_head()?.hash() {
self.history_locator.clear(); self.history_locator.retain(|&x| x.0 == 0);
} }
// for each height we need, we either check if something is close enough from // for each height we need, we either check if something is close enough from
@ -168,8 +168,10 @@ impl HeaderSync {
let last_loc = locator.last().unwrap().clone(); let last_loc = locator.last().unwrap().clone();
let mut header_cursor = self.chain.get_block_header(&last_loc.1); let mut header_cursor = self.chain.get_block_header(&last_loc.1);
while let Ok(header) = header_cursor { while let Ok(header) = header_cursor {
if header.height == h && header.height != last_loc.0 { if header.height == h {
locator.push((header.height, header.hash())); if header.height != last_loc.0 {
locator.push((header.height, header.hash()));
}
break; break;
} }
header_cursor = self.chain.get_previous_header(&header); header_cursor = self.chain.get_previous_header(&header);