Skip to content

Commit 8bfc071

Browse files
committed
Generate snapshot chunks for shard level state
1 parent 764bf32 commit 8bfc071

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
@@ -25,6 +25,7 @@ use ccore::snapshot_notify::{NotifyReceiverSource, ReceiverCanceller};
2525
use ccore::{BlockChainClient, BlockChainTrait, BlockId, Client};
2626
use cdb::{AsHashDB, HashDB};
2727
use cmerkle::snapshot::{ChunkCompressor, Error as SnapshotError, Snapshot};
28+
use cstate::{StateDB, TopLevelState, TopStateView};
2829
use ctypes::BlockHash;
2930
use primitives::H256;
3031

@@ -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: {}",
@@ -95,12 +96,26 @@ impl Service {
9596
}
9697
}
9798
}
99+
fn snapshot(db: &StateDB, block_hash: BlockHash, root: H256, dir: &str) -> Result<(), SnapshotError> {
100+
snapshot_trie(db.as_hashdb(), block_hash, root, dir)?;
101+
102+
let top_state = TopLevelState::from_existing(db.clone(&root), root)?;
103+
let shard_roots = {
104+
let metadata = top_state.metadata()?.expect("Metadata must exist for snapshot block");
105+
let shard_num = *metadata.number_of_shards();
106+
(0..shard_num).map(|n| top_state.shard_root(n))
107+
};
108+
for sr in shard_roots {
109+
snapshot_trie(db.as_hashdb(), block_hash, sr?.expect("Shard root must exist"), dir)?;
110+
}
111+
Ok(())
112+
}
98113

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

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

0 commit comments

Comments
 (0)