Skip to content

Commit 0df530a

Browse files
remagpieforiequal0
authored andcommitted
Add sync state SnapshotBody
1 parent 39f3ae0 commit 0df530a

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()];
@@ -148,6 +136,36 @@ impl Extension {
148136
}
149137
}
150138

139+
fn initial_state(client: Arc<Client>, snapshot_target: Option<(BlockHash, u64)>) -> State {
140+
let (hash, num) = match snapshot_target {
141+
Some(target) => target,
142+
None => return State::Full,
143+
};
144+
let header = match client.block_header(&num.into()) {
145+
Some(ref h) if h.hash() == hash => h.clone(),
146+
_ => return State::SnapshotHeader(hash, num),
147+
};
148+
if client.block_body(&hash.into()).is_none() {
149+
let parent_hash = header.parent_hash();
150+
let parent =
151+
client.block_header(&parent_hash.into()).expect("Parent header of the snapshot header must exist");
152+
return State::SnapshotBody {
153+
block: hash,
154+
prev_root: parent.state_root(),
155+
}
156+
}
157+
158+
let state_db = client.state_db().read();
159+
let state_root = header.state_root();
160+
match TrieFactory::readonly(state_db.as_hashdb(), &state_root) {
161+
Ok(ref trie) if trie.is_complete() => State::Full,
162+
_ => State::SnapshotChunk {
163+
block: hash,
164+
restore: SnapshotRestore::new(state_root),
165+
},
166+
}
167+
}
168+
151169
fn dismiss_request(&mut self, id: &NodeId, request_id: u64) {
152170
if let Some(requests) = self.requests.get_mut(id) {
153171
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,
@@ -561,6 +585,9 @@ impl Extension {
561585
None
562586
}
563587
}
588+
State::SnapshotBody {
589+
..
590+
} => unimplemented!(),
564591
State::SnapshotChunk {
565592
..
566593
} => 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)