Skip to content

Commit f192340

Browse files
committed
Generate snapshot chunks for shard level state
1 parent 275a671 commit f192340

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

sync/src/snapshot/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::thread::{spawn, JoinHandle};
2424
use ccore::snapshot_notify::{NotifyReceiverSource, ReceiverCanceller};
2525
use ccore::{BlockChainClient, BlockChainTrait, BlockId, Client};
2626
use cmerkle::snapshot::{ChunkCompressor, Error as SnapshotError, Snapshot};
27+
use cstate::{StateDB, TopLevelState, TopStateView};
2728
use ctypes::BlockHash;
2829
use hashdb::{AsHashDB, HashDB};
2930
use primitives::H256;
@@ -66,7 +67,7 @@ impl Service {
6667
};
6768
{
6869
let db_lock = client.state_db().read();
69-
if let Err(err) = snapshot(db_lock.as_hashdb(), block_hash, state_root, &root_dir) {
70+
if let Err(err) = snapshot(&db_lock, block_hash, state_root, &root_dir) {
7071
cerror!(
7172
SYNC,
7273
"Snapshot request failed for block: {}, chunk_root: {}, err: {}",
@@ -94,12 +95,26 @@ impl Service {
9495
}
9596
}
9697
}
98+
fn snapshot(db: &StateDB, block_hash: BlockHash, root: H256, dir: &str) -> Result<(), SnapshotError> {
99+
snapshot_trie(db.as_hashdb(), block_hash, root, dir)?;
100+
101+
let top_state = TopLevelState::from_existing(db.clone(&root), root)?;
102+
let shard_roots = {
103+
let metadata = top_state.metadata()?.expect("Metadata must exist for snapshot block");
104+
let shard_num = *metadata.number_of_shards();
105+
(0..shard_num).map(|n| top_state.shard_root(n))
106+
};
107+
for sr in shard_roots {
108+
snapshot_trie(db.as_hashdb(), block_hash, sr?.expect("Shard root must exist"), dir)?;
109+
}
110+
Ok(())
111+
}
97112

98-
fn snapshot(db: &dyn HashDB, block_hash: BlockHash, chunk_root: H256, root_dir: &str) -> Result<(), SnapshotError> {
113+
fn snapshot_trie(db: &dyn HashDB, block_hash: BlockHash, root: H256, root_dir: &str) -> Result<(), SnapshotError> {
99114
let snapshot_dir = snapshot_dir(root_dir, &block_hash);
100115
fs::create_dir_all(snapshot_dir)?;
101116

102-
for chunk in Snapshot::from_hashdb(db, chunk_root) {
117+
for chunk in Snapshot::from_hashdb(db, root) {
103118
let chunk_path = snapshot_path(root_dir, &block_hash, &chunk.root);
104119
let chunk_file = fs::File::create(chunk_path)?;
105120
let compressor = ChunkCompressor::new(chunk_file);

0 commit comments

Comments
 (0)