Skip to content

Commit d984d9d

Browse files
remagpieforiequal0
authored andcommitted
Add sync state SnapshotBody
1 parent 57d4bd0 commit d984d9d

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

codechain/run_node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use csync::snapshot::Service as SnapshotService;
3535
use csync::{BlockSyncExtension, BlockSyncSender, TransactionSyncExtension};
3636
use ctimer::TimerLoop;
3737
use ctrlc::CtrlC;
38+
use ctypes::BlockHash;
3839
use fdlimit::raise_fd_limit;
3940
use kvdb::KeyValueDB;
4041
use kvdb_rocksdb::{Database, DatabaseConfig};
@@ -303,7 +304,7 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
303304
let sync_sender = {
304305
let client = client.client();
305306
let snapshot_target = match (config.network.snapshot_hash, config.network.snapshot_number) {
306-
(Some(hash), Some(num)) => Some((hash, num)),
307+
(Some(hash), Some(num)) => Some((BlockHash::from(hash), num)),
307308
_ => None,
308309
};
309310
let snapshot_dir = config.snapshot.path.clone();

sync/src/block/extension.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ pub struct TokenInfo {
6363
#[derive(Debug)]
6464
enum State {
6565
SnapshotHeader(BlockHash, u64),
66+
SnapshotBody {
67+
block: BlockHash,
68+
prev_root: H256,
69+
},
6670
SnapshotChunk {
6771
block: BlockHash,
6872
restore: SnapshotRestore,
@@ -90,28 +94,12 @@ impl Extension {
9094
pub fn new(
9195
client: Arc<Client>,
9296
api: Box<dyn Api>,
93-
snapshot_target: Option<(H256, u64)>,
97+
snapshot_target: Option<(BlockHash, u64)>,
9498
snapshot_dir: Option<String>,
9599
) -> Extension {
96100
api.set_timer(SYNC_TIMER_TOKEN, Duration::from_millis(SYNC_TIMER_INTERVAL)).expect("Timer set succeeds");
97101

98-
let state = match snapshot_target {
99-
Some((hash, num)) => match client.block_header(&BlockId::Number(num)) {
100-
Some(ref header) if *header.hash() == hash => {
101-
let state_db = client.state_db().read();
102-
let state_root = header.state_root();
103-
match TrieFactory::readonly(state_db.as_hashdb(), &state_root) {
104-
Ok(ref trie) if trie.is_complete() => State::Full,
105-
_ => State::SnapshotChunk {
106-
block: hash.into(),
107-
restore: SnapshotRestore::new(state_root),
108-
},
109-
}
110-
}
111-
_ => State::SnapshotHeader(hash.into(), num),
112-
},
113-
None => State::Full,
114-
};
102+
let state = Extension::initial_state(client.clone(), snapshot_target);
115103
cdebug!(SYNC, "Initial state is {:?}", state);
116104
let mut header = client.best_header();
117105
let mut hollow_headers = vec![header.decode()];
@@ -149,6 +137,36 @@ impl Extension {
149137
}
150138
}
151139

140+
fn initial_state(client: Arc<Client>, snapshot_target: Option<(BlockHash, u64)>) -> State {
141+
let (hash, num) = match snapshot_target {
142+
Some(target) => target,
143+
None => return State::Full,
144+
};
145+
let header = match client.block_header(&num.into()) {
146+
Some(ref h) if h.hash() == hash => h.clone(),
147+
_ => return State::SnapshotHeader(hash, num),
148+
};
149+
if client.block_body(&hash.into()).is_none() {
150+
let parent_hash = header.parent_hash();
151+
let parent =
152+
client.block_header(&parent_hash.into()).expect("Parent header of the snapshot header must exist");
153+
return State::SnapshotBody {
154+
block: hash,
155+
prev_root: parent.state_root(),
156+
}
157+
}
158+
159+
let state_db = client.state_db().read();
160+
let state_root = header.state_root();
161+
match TrieFactory::readonly(state_db.as_hashdb(), &state_root) {
162+
Ok(ref trie) if trie.is_complete() => State::Full,
163+
_ => State::SnapshotChunk {
164+
block: hash,
165+
restore: SnapshotRestore::new(state_root),
166+
},
167+
}
168+
}
169+
152170
fn dismiss_request(&mut self, id: &NodeId, request_id: u64) {
153171
if let Some(requests) = self.requests.get_mut(id) {
154172
requests.retain(|(i, _)| *i != request_id);
@@ -395,6 +413,9 @@ impl NetworkExtension<Event> for Extension {
395413
});
396414
}
397415
}
416+
State::SnapshotBody {
417+
..
418+
} => unimplemented!(),
398419
State::SnapshotChunk {
399420
block,
400421
ref mut restore,
@@ -509,6 +530,9 @@ impl Extension {
509530
None
510531
}
511532
}
533+
State::SnapshotBody {
534+
..
535+
} => None,
512536
State::SnapshotChunk {
513537
..
514538
} => None,
@@ -562,6 +586,9 @@ impl Extension {
562586
None
563587
}
564588
}
589+
State::SnapshotBody {
590+
..
591+
} => unimplemented!(),
565592
State::SnapshotChunk {
566593
..
567594
} => None,
@@ -855,6 +882,9 @@ impl Extension {
855882
headers.len()
856883
),
857884
},
885+
State::SnapshotBody {
886+
..
887+
} => {}
858888
State::SnapshotChunk {
859889
..
860890
} => {}

0 commit comments

Comments
 (0)