Skip to content

Commit 1892b3c

Browse files
Juhyung Parkmajecty
authored andcommitted
Do not request older headers if re-organization is not occurred
Header downloader has "best_hash," which is peer's best hash, and "pivot," which is the best hash of received headers from a peer. When header downloader receives headers from a peer, it checks the first header's hash and the pivot's hash. If they are not the same, the downloader concludes that there is a reorganization and request older headers. However, if the pivot is changed between header request and response, the header downloader requests older hashes even though there is no re-organization. This commit changes the header downloader check re-organization more concisely.
1 parent 9775908 commit 1892b3c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

sync/src/block/downloader/header.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ impl HeaderDownloader {
142142
/// Imports headers and mark success
143143
/// Expects importing headers matches requested header
144144
pub fn import_headers(&mut self, headers: &[Header]) {
145-
let first_header_hash = headers.first().expect("First header must exist").hash();
145+
let first_header = headers.first().expect("First header must exist");
146+
let first_header_hash = first_header.hash();
147+
let first_header_number = first_header.number();
148+
let pivot_header = self.pivot_header();
146149

147150
// This happens when best_hash is imported by other peer.
148151
if self.best_hash == self.pivot.hash {
@@ -158,14 +161,26 @@ impl HeaderDownloader {
158161
hash: headers.last().expect("Last downloaded header must exist").hash(),
159162
total_score: self.pivot.total_score + new_scores,
160163
}
161-
} else {
162-
let pivot_header = self.pivot_header();
164+
} else if first_header_number < pivot_header.number() {
165+
ctrace!(
166+
SYNC,
167+
"Ignore received headers, pivot is already updated since headers are imported by other peers"
168+
);
169+
} else if first_header_number == pivot_header.number() {
163170
if pivot_header.number() != 0 {
164171
self.pivot = Pivot {
165172
hash: pivot_header.parent_hash(),
166173
total_score: self.pivot.total_score - pivot_header.score(),
167174
}
168175
}
176+
} else {
177+
cerror!(
178+
SYNC,
179+
"Invalid header update state. best_hash: {}, self.pivot.hash: {}, first_header_hash: {}",
180+
self.best_hash,
181+
self.pivot.hash,
182+
first_header_hash
183+
);
169184
}
170185

171186
self.request_time = None;

0 commit comments

Comments
 (0)