Skip to content

Commit ad41c34

Browse files
remagpieforiequal0
authored andcommitted
Move to SnapshotBody state after downloading the snapthot header
1 parent d984d9d commit ad41c34

File tree

1 file changed

+36
-84
lines changed

1 file changed

+36
-84
lines changed

sync/src/block/extension.rs

Lines changed: 36 additions & 84 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

@@ -408,8 +408,8 @@ impl NetworkExtension<Event> for Extension {
408408
State::SnapshotHeader(_, num) => {
409409
for id in &peer_ids {
410410
self.send_header_request(id, RequestMessage::Headers {
411-
start_number: num,
412-
max_count: 1,
411+
start_number: num - 1,
412+
max_count: 2,
413413
});
414414
}
415415
}
@@ -518,89 +518,41 @@ pub enum Event {
518518

519519
impl Extension {
520520
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>, retracted: Vec<BlockHash>) {
521-
if let Some(next_state) = match self.state {
522-
State::SnapshotHeader(hash, ..) => {
523-
if imported.contains(&hash) {
524-
let header = self.client.block_header(&BlockId::Hash(hash)).expect("Imported header must exist");
525-
Some(State::SnapshotChunk {
526-
block: hash,
527-
restore: SnapshotRestore::new(header.state_root()),
528-
})
529-
} else {
530-
None
521+
if let State::Full = self.state {
522+
let peer_ids: Vec<_> = self.header_downloaders.keys().cloned().collect();
523+
for id in peer_ids {
524+
if let Some(peer) = self.header_downloaders.get_mut(&id) {
525+
peer.mark_as_imported(imported.clone());
531526
}
532527
}
533-
State::SnapshotBody {
534-
..
535-
} => None,
536-
State::SnapshotChunk {
537-
..
538-
} => None,
539-
State::Full => {
540-
let peer_ids: Vec<_> = self.header_downloaders.keys().cloned().collect();
541-
for id in peer_ids {
542-
if let Some(peer) = self.header_downloaders.get_mut(&id) {
543-
peer.mark_as_imported(imported.clone());
544-
}
545-
}
546-
let mut headers_to_download: Vec<_> = enacted
547-
.into_iter()
548-
.map(|hash| self.client.block_header(&BlockId::Hash(hash)).expect("Enacted header must exist"))
549-
.collect();
550-
headers_to_download.sort_unstable_by_key(EncodedHeader::number);
551-
#[allow(clippy::redundant_closure)]
552-
// False alarm. https://github.com/rust-lang/rust-clippy/issues/1439
553-
headers_to_download.dedup_by_key(|h| h.hash());
554-
555-
let headers: Vec<_> = headers_to_download
556-
.into_iter()
557-
.filter(|header| self.client.block_body(&BlockId::Hash(header.hash())).is_none())
558-
.collect(); // FIXME: No need to collect here if self is not borrowed.
559-
for header in headers {
560-
let parent = self
561-
.client
562-
.block_header(&BlockId::Hash(header.parent_hash()))
563-
.expect("Enacted header must have parent");
564-
let is_empty = header.transactions_root() == parent.transactions_root();
565-
self.body_downloader.add_target(&header.decode(), is_empty);
566-
}
567-
self.body_downloader.remove_target(&retracted);
568-
None
528+
let mut headers_to_download: Vec<_> = enacted
529+
.into_iter()
530+
.map(|hash| self.client.block_header(&BlockId::Hash(hash)).expect("Enacted header must exist"))
531+
.collect();
532+
headers_to_download.sort_unstable_by_key(EncodedHeader::number);
533+
#[allow(clippy::redundant_closure)]
534+
// False alarm. https://github.com/rust-lang/rust-clippy/issues/1439
535+
headers_to_download.dedup_by_key(|h| h.hash());
536+
537+
let headers: Vec<_> = headers_to_download
538+
.into_iter()
539+
.filter(|header| self.client.block_body(&BlockId::Hash(header.hash())).is_none())
540+
.collect(); // FIXME: No need to collect here if self is not borrowed.
541+
for header in headers {
542+
let parent = self
543+
.client
544+
.block_header(&BlockId::Hash(header.parent_hash()))
545+
.expect("Enacted header must have parent");
546+
let is_empty = header.transactions_root() == parent.transactions_root();
547+
self.body_downloader.add_target(&header.decode(), is_empty);
569548
}
570-
} {
571-
cdebug!(SYNC, "Transitioning state to {:?}", next_state);
572-
self.state = next_state;
549+
self.body_downloader.remove_target(&retracted);
573550
}
574551
}
575552

576553
fn new_blocks(&mut self, imported: Vec<BlockHash>, invalid: Vec<BlockHash>) {
577-
if let Some(next_state) = match self.state {
578-
State::SnapshotHeader(hash, ..) => {
579-
if imported.contains(&hash) {
580-
let header = self.client.block_header(&BlockId::Hash(hash)).expect("Imported header must exist");
581-
Some(State::SnapshotChunk {
582-
block: hash,
583-
restore: SnapshotRestore::new(header.state_root()),
584-
})
585-
} else {
586-
None
587-
}
588-
}
589-
State::SnapshotBody {
590-
..
591-
} => unimplemented!(),
592-
State::SnapshotChunk {
593-
..
594-
} => None,
595-
State::Full => {
596-
self.body_downloader.remove_target(&imported);
597-
self.body_downloader.remove_target(&invalid);
598-
None
599-
}
600-
} {
601-
cdebug!(SYNC, "Transitioning state to {:?}", next_state);
602-
self.state = next_state;
603-
}
554+
self.body_downloader.remove_target(&imported);
555+
self.body_downloader.remove_target(&invalid);
604556

605557
let chain_info = self.client.chain_info();
606558

@@ -858,20 +810,20 @@ impl Extension {
858810
ctrace!(SYNC, "Received header response from({}) with length({})", from, headers.len());
859811
match self.state {
860812
State::SnapshotHeader(hash, _) => match headers {
861-
[header] if header.hash() == hash => {
813+
[parent, header] if header.hash() == hash => {
862814
match self.client.import_bootstrap_header(&header) {
863-
Err(BlockImportError::Import(ImportError::AlreadyInChain)) => {
864-
self.state = State::SnapshotChunk {
815+
Ok(_) | Err(BlockImportError::Import(ImportError::AlreadyInChain)) => {
816+
self.state = State::SnapshotBody {
865817
block: hash,
866-
restore: SnapshotRestore::new(*header.state_root()),
818+
prev_root: *parent.transactions_root(),
867819
};
820+
cdebug!(SYNC, "Transitioning state to {:?}", self.state);
868821
}
869822
Err(BlockImportError::Import(ImportError::AlreadyQueued)) => {}
870823
// FIXME: handle import errors
871824
Err(err) => {
872825
cwarn!(SYNC, "Cannot import header({}): {:?}", header.hash(), err);
873826
}
874-
_ => {}
875827
}
876828
}
877829
_ => cdebug!(

0 commit comments

Comments
 (0)