Skip to content

Commit 2995012

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

File tree

1 file changed

+35
-83
lines changed

1 file changed

+35
-83
lines changed

sync/src/block/extension.rs

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

@@ -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,88 +518,40 @@ 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-
self.body_downloader.add_target(&header.decode(), &parent.decode());
565-
}
566-
self.body_downloader.remove_target(&retracted);
567-
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+
self.body_downloader.add_target(&header.decode(), &parent.decode());
568547
}
569-
} {
570-
cdebug!(SYNC, "Transitioning state to {:?}", next_state);
571-
self.state = next_state;
548+
self.body_downloader.remove_target(&retracted);
572549
}
573550
}
574551

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

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

@@ -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)