Skip to content

Commit cc236fd

Browse files
remagpieforiequal0
authored andcommitted
Add sync state SnapshotBody
1 parent a96282d commit cc236fd

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};
@@ -299,7 +300,7 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
299300
let sync_sender = {
300301
let client = client.client();
301302
let snapshot_target = match (config.network.snapshot_hash, config.network.snapshot_number) {
302-
(Some(hash), Some(num)) => Some((hash, num)),
303+
(Some(hash), Some(num)) => Some((BlockHash::from(hash), num)),
303304
_ => None,
304305
};
305306
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);
@@ -418,6 +436,9 @@ impl NetworkExtension<Event> for Extension {
418436
});
419437
}
420438
}
439+
State::SnapshotBody {
440+
..
441+
} => unimplemented!(),
421442
State::SnapshotChunk {
422443
block,
423444
ref mut restore,
@@ -532,6 +553,9 @@ impl Extension {
532553
None
533554
}
534555
}
556+
State::SnapshotBody {
557+
..
558+
} => None,
535559
State::SnapshotChunk {
536560
..
537561
} => None,
@@ -582,6 +606,9 @@ impl Extension {
582606
None
583607
}
584608
}
609+
State::SnapshotBody {
610+
..
611+
} => unimplemented!(),
585612
State::SnapshotChunk {
586613
..
587614
} => None,
@@ -859,6 +886,9 @@ impl Extension {
859886
headers.len()
860887
),
861888
},
889+
State::SnapshotBody {
890+
..
891+
} => {}
862892
State::SnapshotChunk {
863893
..
864894
} => {}

0 commit comments

Comments
 (0)