Skip to content

Commit

Permalink
refactor(store): get_reverted_block_hashes func return sig
Browse files Browse the repository at this point in the history
New `SMTRevertedBlockHashes` struct to properly represent db value
  • Loading branch information
zeroqn authored and jjyr committed Jul 13, 2022
1 parent b214f74 commit 69cebad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
6 changes: 4 additions & 2 deletions crates/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ impl Chain {

let reverted_block_hashes = db
.get_reverted_block_hashes_by_root(&current_reverted_block_root)?
.expect("reverted block hashes should exists");
.expect("reverted block hashes should exists")
.block_hashes;

db.rewind_reverted_block_smt(reverted_block_hashes)?;
current_reverted_block_root = db.get_reverted_block_smt_root()?;
Expand Down Expand Up @@ -770,7 +771,8 @@ impl Chain {

let reverted_block_hashes = db
.get_reverted_block_hashes_by_root(&current_reverted_block_root)?
.expect("reverted block hashes should exists");
.expect("reverted block hashes should exists")
.block_hashes;

db.rewind_reverted_block_smt(reverted_block_hashes)?;
current_reverted_block_root = db.get_reverted_block_smt_root()?;
Expand Down
13 changes: 9 additions & 4 deletions crates/store/src/traits/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use gw_db::schema::{
META_CHAIN_ID_KEY, META_LAST_VALID_TIP_BLOCK_HASH_KEY, META_REVERTED_BLOCK_SMT_ROOT_KEY,
META_TIP_BLOCK_HASH_KEY,
};
use gw_types::offchain::global_state_from_slice;
use gw_types::offchain::{global_state_from_slice, SMTRevertedBlockHashes};
use gw_types::packed::{Script, WithdrawalKey};
use gw_types::{
from_box_should_be_ok,
Expand Down Expand Up @@ -252,7 +252,7 @@ pub trait ChainStore: KVStoreRead {
fn get_reverted_block_hashes_by_root(
&self,
reverted_block_smt_root: &H256,
) -> Result<Option<Vec<H256>>, Error> {
) -> Result<Option<SMTRevertedBlockHashes>, Error> {
match self.get(
COLUMN_REVERTED_BLOCK_SMT_ROOT,
reverted_block_smt_root.as_slice(),
Expand All @@ -264,9 +264,14 @@ pub trait ChainStore: KVStoreRead {
// Remove prev smt root at 0 idx
let last_hash_idx = block_hashes.len().saturating_sub(1);
block_hashes.swap(0, last_hash_idx);
block_hashes.pop();
let prev_smt_root = block_hashes.pop().expect("prev root");

Ok(Some(block_hashes))
let root_hashes = SMTRevertedBlockHashes {
prev_smt_root,
block_hashes,
};

Ok(Some(root_hashes))
}
None => Ok(None),
}
Expand Down
2 changes: 2 additions & 0 deletions crates/types/src/offchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod pool;
mod rollup_context;
mod rpc;
mod run_result;
mod store;

pub use error_receipt::*;
pub use exported_block::*;
Expand All @@ -15,3 +16,4 @@ pub use pool::*;
pub use rollup_context::*;
pub use rpc::*;
pub use run_result::*;
pub use store::*;
6 changes: 6 additions & 0 deletions crates/types/src/offchain/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use sparse_merkle_tree::H256;

pub struct SMTRevertedBlockHashes {
pub prev_smt_root: H256,
pub block_hashes: Vec<H256>,
}

0 comments on commit 69cebad

Please sign in to comment.