Skip to content

Commit a18a8df

Browse files
committed
Make queued cache to decrease importing time
1 parent d081da9 commit a18a8df

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

sync/src/block/downloader/header.rs

Lines changed: 17 additions & 4 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,15 @@ 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 => match self.downloaded.get(&self.pivot.hash) {
111+
Some(header) => header.clone(),
112+
None => self.client.block_header(&BlockId::Hash(self.pivot.hash)).unwrap(),
113+
},
109114
}
110115
}
111116

@@ -173,7 +178,7 @@ impl HeaderDownloader {
173178

174179
pub fn mark_as_imported(&mut self, hashes: Vec<H256>) {
175180
for hash in hashes {
176-
self.downloaded.remove(&hash);
181+
self.queued.remove(&hash);
177182

178183
if self.best_hash == hash {
179184
self.pivot = Pivot {
@@ -183,4 +188,12 @@ impl HeaderDownloader {
183188
}
184189
}
185190
}
191+
192+
pub fn mark_as_queued(&mut self, hashes: Vec<H256>) {
193+
for hash in hashes {
194+
if let Some(header) = self.downloaded.remove(&hash) {
195+
self.queued.insert(hash, header);
196+
}
197+
}
198+
}
186199
}

sync/src/block/extension.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,13 @@ 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),
689+
Err(BlockImportError::Import(ImportError::AlreadyQueued)) => queued.push(hash),
686690
// FIXME: handle import errors
687691
Err(err) => {
688692
cwarn!(SYNC, "Cannot import header({}): {:?}", header.hash(), err);
@@ -693,6 +697,7 @@ impl Extension {
693697
}
694698

695699
let request = self.header_downloaders.get_mut(from).and_then(|peer| {
700+
peer.mark_as_queued(queued);
696701
peer.mark_as_imported(exists);
697702
peer.create_request()
698703
});

0 commit comments

Comments
 (0)