Skip to content

Commit 8a428cb

Browse files
committed
Refactor on_chunk_response
1 parent f192340 commit 8a428cb

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

sync/src/block/extension.rs

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -999,59 +999,60 @@ impl Extension {
999999
}
10001000

10011001
fn on_chunk_response(&mut self, from: &NodeId, roots: &[H256], chunks: &[Vec<u8>]) {
1002-
if let State::SnapshotChunk {
1003-
block,
1004-
ref mut restore,
1005-
} = self.state
1006-
{
1007-
for (r, c) in roots.iter().zip(chunks) {
1008-
if c.is_empty() {
1009-
cdebug!(SYNC, "Peer {} sent empty response for chunk request {}", from, r);
1002+
let (block, restore) = match self.state {
1003+
State::SnapshotChunk {
1004+
block,
1005+
ref mut restore,
1006+
} => (block, restore),
1007+
_ => return,
1008+
};
1009+
for (r, c) in roots.iter().zip(chunks) {
1010+
if c.is_empty() {
1011+
cdebug!(SYNC, "Peer {} sent empty response for chunk request {}", from, r);
1012+
continue
1013+
}
1014+
let decompressor = ChunkDecompressor::from_slice(c);
1015+
let raw_chunk = match decompressor.decompress() {
1016+
Ok(chunk) => chunk,
1017+
Err(e) => {
1018+
cwarn!(SYNC, "Decode failed for chunk response from peer {}: {}", from, e);
10101019
continue
10111020
}
1012-
let decompressor = ChunkDecompressor::from_slice(c);
1013-
let raw_chunk = match decompressor.decompress() {
1014-
Ok(chunk) => chunk,
1015-
Err(e) => {
1016-
cwarn!(SYNC, "Decode failed for chunk response from peer {}: {}", from, e);
1017-
continue
1018-
}
1019-
};
1020-
let recovered = match raw_chunk.recover(*r) {
1021-
Ok(chunk) => chunk,
1021+
};
1022+
let recovered = match raw_chunk.recover(*r) {
1023+
Ok(chunk) => chunk,
1024+
Err(e) => {
1025+
cwarn!(SYNC, "Invalid chunk response from peer {}: {}", from, e);
1026+
continue
1027+
}
1028+
};
1029+
1030+
let batch = {
1031+
let mut state_db = self.client.state_db().write();
1032+
let hash_db = state_db.as_hashdb_mut();
1033+
restore.feed(hash_db, recovered);
1034+
1035+
let mut batch = DBTransaction::new();
1036+
match state_db.journal_under(&mut batch, 0, H256::zero()) {
1037+
Ok(_) => batch,
10221038
Err(e) => {
1023-
cwarn!(SYNC, "Invalid chunk response from peer {}: {}", from, e);
1039+
cwarn!(SYNC, "Failed to write state chunk to database: {}", e);
10241040
continue
10251041
}
1026-
};
1027-
1028-
let batch = {
1029-
let mut state_db = self.client.state_db().write();
1030-
let hash_db = state_db.as_hashdb_mut();
1031-
restore.feed(hash_db, recovered);
1032-
1033-
let mut batch = DBTransaction::new();
1034-
match state_db.journal_under(&mut batch, 0, H256::zero()) {
1035-
Ok(_) => batch,
1036-
Err(e) => {
1037-
cwarn!(SYNC, "Failed to write state chunk to database: {}", e);
1038-
continue
1039-
}
1040-
}
1041-
};
1042-
self.client.db().write_buffered(batch);
1043-
match self.client.db().flush() {
1044-
Ok(_) => cdebug!(SYNC, "Wrote state chunk to database: {}", r),
1045-
Err(e) => cwarn!(SYNC, "Failed to flush database: {}", e),
10461042
}
1043+
};
1044+
self.client.db().write_buffered(batch);
1045+
match self.client.db().flush() {
1046+
Ok(_) => cdebug!(SYNC, "Wrote state chunk to database: {}", r),
1047+
Err(e) => cwarn!(SYNC, "Failed to flush database: {}", e),
10471048
}
1049+
}
10481050

1049-
if let Some(root) = restore.next_to_feed() {
1050-
self.send_chunk_request(&block, &root);
1051-
} else {
1052-
self.client.force_update_best_block(&block);
1053-
self.transition_to_full();
1054-
}
1051+
if let Some(root) = restore.next_to_feed() {
1052+
self.send_chunk_request(&block, &root);
1053+
} else {
1054+
self.client.force_update_best_block(&block);
1055+
self.transition_to_full();
10551056
}
10561057
}
10571058

0 commit comments

Comments
 (0)