Skip to content

Commit 0ba5323

Browse files
remagpieforiequal0
authored andcommitted
Move to SnapshotBody state after downloading the snapthot header
1 parent cc236fd commit 0ba5323

File tree

1 file changed

+34
-82
lines changed

1 file changed

+34
-82
lines changed

sync/src/block/extension.rs

Lines changed: 34 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Extension {
152152
client.block_header(&parent_hash.into()).expect("Parent header of the snapshot header must exist");
153153
return State::SnapshotBody {
154154
block: hash,
155-
prev_root: parent.state_root(),
155+
prev_root: parent.transactions_root(),
156156
}
157157
}
158158

@@ -431,8 +431,8 @@ impl NetworkExtension<Event> for Extension {
431431
State::SnapshotHeader(_, num) => {
432432
for id in &peer_ids {
433433
self.send_header_request(id, RequestMessage::Headers {
434-
start_number: num,
435-
max_count: 1,
434+
start_number: num - 1,
435+
max_count: 2,
436436
});
437437
}
438438
}
@@ -541,86 +541,38 @@ pub enum Event {
541541

542542
impl Extension {
543543
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>, retracted: Vec<BlockHash>) {
544-
if let Some(next_state) = match self.state {
545-
State::SnapshotHeader(hash, ..) => {
546-
if imported.contains(&hash) {
547-
let header = self.client.block_header(&BlockId::Hash(hash)).expect("Imported header must exist");
548-
Some(State::SnapshotChunk {
549-
block: hash,
550-
restore: SnapshotRestore::new(header.state_root()),
551-
})
552-
} else {
553-
None
554-
}
544+
if let State::Full = self.state {
545+
for peer in self.header_downloaders.values_mut() {
546+
peer.mark_as_imported(imported.clone());
555547
}
556-
State::SnapshotBody {
557-
..
558-
} => None,
559-
State::SnapshotChunk {
560-
..
561-
} => None,
562-
State::Full => {
563-
for peer in self.header_downloaders.values_mut() {
564-
peer.mark_as_imported(imported.clone());
565-
}
566-
let mut headers_to_download: Vec<_> = enacted
567-
.into_iter()
568-
.map(|hash| self.client.block_header(&BlockId::Hash(hash)).expect("Enacted header must exist"))
569-
.collect();
570-
headers_to_download.sort_unstable_by_key(EncodedHeader::number);
571-
#[allow(clippy::redundant_closure)]
572-
// False alarm. https://github.com/rust-lang/rust-clippy/issues/1439
573-
headers_to_download.dedup_by_key(|h| h.hash());
574-
575-
let headers: Vec<_> = headers_to_download
576-
.into_iter()
577-
.filter(|header| self.client.block_body(&BlockId::Hash(header.hash())).is_none())
578-
.collect(); // FIXME: No need to collect here if self is not borrowed.
579-
for header in headers {
580-
let parent = self
581-
.client
582-
.block_header(&BlockId::Hash(header.parent_hash()))
583-
.expect("Enacted header must have parent");
584-
let is_empty = header.transactions_root() == parent.transactions_root();
585-
self.body_downloader.add_target(&header.decode(), is_empty);
586-
}
587-
self.body_downloader.remove_target(&retracted);
588-
None
548+
let mut headers_to_download: Vec<_> = enacted
549+
.into_iter()
550+
.map(|hash| self.client.block_header(&BlockId::Hash(hash)).expect("Enacted header must exist"))
551+
.collect();
552+
headers_to_download.sort_unstable_by_key(EncodedHeader::number);
553+
#[allow(clippy::redundant_closure)]
554+
// False alarm. https://github.com/rust-lang/rust-clippy/issues/1439
555+
headers_to_download.dedup_by_key(|h| h.hash());
556+
557+
let headers: Vec<_> = headers_to_download
558+
.into_iter()
559+
.filter(|header| self.client.block_body(&BlockId::Hash(header.hash())).is_none())
560+
.collect(); // FIXME: No need to collect here if self is not borrowed.
561+
for header in headers {
562+
let parent = self
563+
.client
564+
.block_header(&BlockId::Hash(header.parent_hash()))
565+
.expect("Enacted header must have parent");
566+
let is_empty = header.transactions_root() == parent.transactions_root();
567+
self.body_downloader.add_target(&header.decode(), is_empty);
589568
}
590-
} {
591-
cdebug!(SYNC, "Transitioning state to {:?}", next_state);
592-
self.state = next_state;
569+
self.body_downloader.remove_target(&retracted);
593570
}
594571
}
595572

596573
fn new_blocks(&mut self, imported: Vec<BlockHash>, invalid: Vec<BlockHash>) {
597-
if let Some(next_state) = match self.state {
598-
State::SnapshotHeader(hash, ..) => {
599-
if imported.contains(&hash) {
600-
let header = self.client.block_header(&BlockId::Hash(hash)).expect("Imported header must exist");
601-
Some(State::SnapshotChunk {
602-
block: hash,
603-
restore: SnapshotRestore::new(header.state_root()),
604-
})
605-
} else {
606-
None
607-
}
608-
}
609-
State::SnapshotBody {
610-
..
611-
} => unimplemented!(),
612-
State::SnapshotChunk {
613-
..
614-
} => None,
615-
State::Full => {
616-
self.body_downloader.remove_target(&imported);
617-
self.body_downloader.remove_target(&invalid);
618-
None
619-
}
620-
} {
621-
cdebug!(SYNC, "Transitioning state to {:?}", next_state);
622-
self.state = next_state;
623-
}
574+
self.body_downloader.remove_target(&imported);
575+
self.body_downloader.remove_target(&invalid);
624576
self.send_status_broadcast();
625577
}
626578
}
@@ -862,20 +814,20 @@ impl Extension {
862814
ctrace!(SYNC, "Received header response from({}) with length({})", from, headers.len());
863815
match self.state {
864816
State::SnapshotHeader(hash, _) => match headers {
865-
[header] if header.hash() == hash => {
817+
[parent, header] if header.hash() == hash => {
866818
match self.client.import_bootstrap_header(&header) {
867-
Err(BlockImportError::Import(ImportError::AlreadyInChain)) => {
868-
self.state = State::SnapshotChunk {
819+
Ok(_) | Err(BlockImportError::Import(ImportError::AlreadyInChain)) => {
820+
self.state = State::SnapshotBody {
869821
block: hash,
870-
restore: SnapshotRestore::new(*header.state_root()),
822+
prev_root: *parent.transactions_root(),
871823
};
824+
cdebug!(SYNC, "Transitioning state to {:?}", self.state);
872825
}
873826
Err(BlockImportError::Import(ImportError::AlreadyQueued)) => {}
874827
// FIXME: handle import errors
875828
Err(err) => {
876829
cwarn!(SYNC, "Cannot import header({}): {:?}", header.hash(), err);
877830
}
878-
_ => {}
879831
}
880832
}
881833
_ => cdebug!(

0 commit comments

Comments
 (0)