Skip to content

Commit 4c48d0e

Browse files
committed
Make queued chache to decrease importing time
1 parent 3128b13 commit 4c48d0e

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

sync/src/block/downloader/header.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct HeaderDownloader {
4545
pivot: Pivot,
4646
request_time: Option<Instant>,
4747
downloaded: HashMap<H256, Header>,
48+
queued: HashMap<H256, Header>,
4849
trial: usize,
4950
}
5051

@@ -69,6 +70,7 @@ impl HeaderDownloader {
6970
},
7071
request_time: None,
7172
downloaded: HashMap::new(),
73+
queued: HashMap::new(),
7274
trial: 0,
7375
}
7476
}
@@ -100,12 +102,17 @@ impl HeaderDownloader {
100102
self.request_time.map_or(false, |time| (Instant::now() - time).as_secs() > MAX_WAIT)
101103
}
102104

103-
/// Find header from download cache, and then from blockchain
105+
/// Find header from queued headers, downloaded cache and then from blockchain
104106
/// Panics if header dosn't exist
105107
fn pivot_header(&self) -> Header {
106-
match self.downloaded.get(&self.pivot.hash) {
108+
match self.queued.get(&self.pivot.hash) {
107109
Some(header) => header.clone(),
108-
None => self.client.block_header(&BlockId::Hash(self.pivot.hash)).unwrap(),
110+
None => self
111+
.downloaded
112+
.get(&self.pivot.hash)
113+
.map(Clone::clone)
114+
.or_else(|| self.client.block_header(&BlockId::Hash(self.pivot.hash)))
115+
.unwrap(),
109116
}
110117
}
111118

@@ -143,7 +150,7 @@ impl HeaderDownloader {
143150
if self.best_hash == self.pivot.hash {
144151
ctrace!(SYNC, "Ignore received headers, pivot already reached the best hash");
145152
} else if first_header_hash == self.pivot.hash {
146-
for header in headers.iter() {
153+
for header in headers[1..].iter() {
147154
self.downloaded.insert(header.hash(), header.clone());
148155
}
149156

@@ -173,7 +180,7 @@ impl HeaderDownloader {
173180

174181
pub fn mark_as_imported(&mut self, hashes: Vec<H256>) {
175182
for hash in hashes {
176-
self.downloaded.remove(&hash);
183+
self.queued.remove(&hash);
177184

178185
if self.best_hash == hash {
179186
self.pivot = Pivot {
@@ -183,4 +190,12 @@ impl HeaderDownloader {
183190
}
184191
}
185192
}
193+
194+
pub fn mark_as_queued(&mut self, hashes: Vec<H256>) {
195+
for hash in hashes {
196+
if let Some(header) = self.downloaded.remove(&hash) {
197+
self.queued.insert(hash, header);
198+
}
199+
}
200+
}
186201
}

sync/src/block/extension.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,19 +680,23 @@ impl Extension {
680680
completed.sort_unstable_by_key(EncodedHeader::number);
681681

682682
let mut exists = Vec::new();
683+
let mut queued = Vec::new();
684+
683685
for header in completed {
686+
let hash = header.hash();
684687
match self.client.import_header(header.clone().into_inner()) {
685-
Err(BlockImportError::Import(ImportError::AlreadyInChain)) => exists.push(header.hash()),
688+
Err(BlockImportError::Import(ImportError::AlreadyInChain)) => exists.push(hash),
686689
// FIXME: handle import errors
687690
Err(err) => {
688691
cwarn!(SYNC, "Cannot import header({}): {:?}", header.hash(), err);
689692
break
690693
}
691-
_ => {}
694+
_ => queued.push(hash),
692695
}
693696
}
694697

695698
let request = self.header_downloaders.get_mut(from).and_then(|peer| {
699+
peer.mark_as_queued(queued);
696700
peer.mark_as_imported(exists);
697701
peer.create_request()
698702
});

0 commit comments

Comments
 (0)