From 69cebadb46fe296acc72cf33670e7bd60cd83ee0 Mon Sep 17 00:00:00 2001 From: "zero.qn" Date: Tue, 12 Jul 2022 18:05:37 +0800 Subject: [PATCH] refactor(store): get_reverted_block_hashes func return sig New `SMTRevertedBlockHashes` struct to properly represent db value --- crates/chain/src/chain.rs | 6 ++++-- crates/store/src/traits/chain_store.rs | 13 +++++++++---- crates/types/src/offchain/mod.rs | 2 ++ crates/types/src/offchain/store.rs | 6 ++++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 crates/types/src/offchain/store.rs diff --git a/crates/chain/src/chain.rs b/crates/chain/src/chain.rs index 58bb53721..766a599e1 100644 --- a/crates/chain/src/chain.rs +++ b/crates/chain/src/chain.rs @@ -705,7 +705,8 @@ impl Chain { let reverted_block_hashes = db .get_reverted_block_hashes_by_root(¤t_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()?; @@ -770,7 +771,8 @@ impl Chain { let reverted_block_hashes = db .get_reverted_block_hashes_by_root(¤t_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()?; diff --git a/crates/store/src/traits/chain_store.rs b/crates/store/src/traits/chain_store.rs index b1e2d96c2..a7a807a54 100644 --- a/crates/store/src/traits/chain_store.rs +++ b/crates/store/src/traits/chain_store.rs @@ -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, @@ -252,7 +252,7 @@ pub trait ChainStore: KVStoreRead { fn get_reverted_block_hashes_by_root( &self, reverted_block_smt_root: &H256, - ) -> Result>, Error> { + ) -> Result, Error> { match self.get( COLUMN_REVERTED_BLOCK_SMT_ROOT, reverted_block_smt_root.as_slice(), @@ -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), } diff --git a/crates/types/src/offchain/mod.rs b/crates/types/src/offchain/mod.rs index 24640db6e..60891b3bf 100644 --- a/crates/types/src/offchain/mod.rs +++ b/crates/types/src/offchain/mod.rs @@ -6,6 +6,7 @@ mod pool; mod rollup_context; mod rpc; mod run_result; +mod store; pub use error_receipt::*; pub use exported_block::*; @@ -15,3 +16,4 @@ pub use pool::*; pub use rollup_context::*; pub use rpc::*; pub use run_result::*; +pub use store::*; diff --git a/crates/types/src/offchain/store.rs b/crates/types/src/offchain/store.rs new file mode 100644 index 000000000..748b61f0b --- /dev/null +++ b/crates/types/src/offchain/store.rs @@ -0,0 +1,6 @@ +use sparse_merkle_tree::H256; + +pub struct SMTRevertedBlockHashes { + pub prev_smt_root: H256, + pub block_hashes: Vec, +}