Skip to content

Commit

Permalink
fix import snapshot bug (#3425)
Browse files Browse the repository at this point in the history
* add export_snapshot special block number

* 1.add export_snapshot special block number for debug
2.fix apply_snapshot on barnard

* remove unused code
  • Loading branch information
nkysg authored May 27, 2022
1 parent 5f7a786 commit 2d2325e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
19 changes: 17 additions & 2 deletions cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ pub struct ExportSnapshotOptions {
#[clap(long, short = 't')]
/// enable increment export snapshot
pub increment: Option<bool>,
#[clap(long, short = 'b')]
/// special block_num for debug usage
pub special_block_num: Option<BlockNumber>,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -479,7 +482,13 @@ fn main() -> anyhow::Result<()> {
}

if let Cmd::ExportSnapshot(option) = cmd {
let result = export_snapshot(option.db_path, option.output, option.net, option.increment);
let result = export_snapshot(
option.db_path,
option.output,
option.net,
option.increment,
option.special_block_num,
);
return result;
}

Expand Down Expand Up @@ -1038,6 +1047,7 @@ pub fn export_snapshot(
output: PathBuf,
network: BuiltinNetworkID,
increment: Option<bool>,
special_block_num: Option<BlockNumber>,
) -> anyhow::Result<()> {
let start_time = SystemTime::now();
let net = ChainNetwork::new_builtin(network);
Expand All @@ -1064,11 +1074,16 @@ pub fn export_snapshot(
)
.expect("create block chain should success.");
let block_num = chain.status().head().number();
let cur_num = if block_num <= SNAP_GAP {
let mut cur_num = if block_num <= SNAP_GAP {
block_num
} else {
block_num - SNAP_GAP
};
if let Some(special_num) = special_block_num {
if special_num <= cur_num {
cur_num = special_num;
}
}
let cur_block = chain
.get_block_by_number(cur_num)?
.ok_or_else(|| format_err!("get block by number {} error", cur_num))?;
Expand Down
52 changes: 14 additions & 38 deletions state/statedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,45 +458,21 @@ impl ChainStateWriter for ChainStateDB {

fn apply(&self, chain_state_set: ChainStateSet) -> Result<()> {
for (address, account_state_set) in chain_state_set.state_sets() {
let (code_root, resource_root) = match self.get_account_state(address)? {
Some(account_state) => (
account_state.code_root(),
Some(account_state.resource_root()),
),
None => (None, None),
let code_root = if let Some(state_set) = account_state_set.code_set() {
let state_tree = StateTree::<ModuleName>::new(self.store.clone(), None);
state_tree.apply(state_set.clone())?;
state_tree.flush()?;
Some(state_tree.root_hash())
} else {
None
};
let code_root = match (code_root, account_state_set.code_set()) {
(Some(storage_root), Some(state_set)) => {
let state_tree = self.new_state_tree::<ModuleName>(storage_root);
state_tree.apply(state_set.clone())?;
state_tree.flush()?;
Some(state_tree.root_hash())
}
(Some(storage_root), None) => Some(storage_root),
(None, Some(state_set)) => {
let state_tree = StateTree::<ModuleName>::new(self.store.clone(), None);
state_tree.apply(state_set.clone())?;
state_tree.flush()?;
Some(state_tree.root_hash())
}
(None, None) => None,
};

let resource_root = match (resource_root, account_state_set.resource_set()) {
(Some(storage_root), Some(state_set)) => {
let state_tree = self.new_state_tree::<StructTag>(storage_root);
state_tree.apply(state_set.clone())?;
state_tree.flush()?;
state_tree.root_hash()
}
(Some(storage_root), None) => storage_root,
(None, Some(state_set)) => {
let state_tree = StateTree::<StructTag>::new(self.store.clone(), None);
state_tree.apply(state_set.clone())?;
state_tree.flush()?;
state_tree.root_hash()
}
(None, None) => unreachable!("this should never happened"),
let resource_root = if let Some(state_set) = account_state_set.resource_set() {
let state_tree = StateTree::<StructTag>::new(self.store.clone(), None);
state_tree.apply(state_set.clone())?;
state_tree.flush()?;
state_tree.root_hash()
} else {
unreachable!("this should never happened")
};
let new_account_state = AccountState::new(code_root, resource_root);
self.state_tree.put(*address, new_account_state.try_into()?);
Expand Down

0 comments on commit 2d2325e

Please sign in to comment.