diff --git a/crates/rooch-db/src/lib.rs b/crates/rooch-db/src/lib.rs index 428cdbfd34..c907d662d2 100644 --- a/crates/rooch-db/src/lib.rs +++ b/crates/rooch-db/src/lib.rs @@ -34,12 +34,13 @@ impl RoochDB { None, )?))?; - let rooch_store = RoochStore::new(StoreInstance::new_db_instance(RocksDB::new( - rooch_store_dir, - rooch_store::StoreMeta::get_column_family_names().to_vec(), - config.rocksdb_config(false), - None, - )?))?; + let rooch_store = + RoochStore::new_with_instance(StoreInstance::new_db_instance(RocksDB::new( + rooch_store_dir, + rooch_store::StoreMeta::get_column_family_names().to_vec(), + config.rocksdb_config(false), + None, + )?))?; let indexer_store = IndexerStore::new(indexer_store_dir.clone())?; let indexer_reader = IndexerReader::new(indexer_store_dir)?; diff --git a/crates/rooch-sequencer/src/actor/sequencer.rs b/crates/rooch-sequencer/src/actor/sequencer.rs index 304a510046..b8fa1b2450 100644 --- a/crates/rooch-sequencer/src/actor/sequencer.rs +++ b/crates/rooch-sequencer/src/actor/sequencer.rs @@ -88,10 +88,11 @@ impl SequencerActor { let sequencer_info = SequencerInfo::new(tx.sequence_info.tx_order, self.tx_accumulator.get_info()); - self.rooch_store.save_sequencer_info(sequencer_info)?; + self.rooch_store + .save_sequencer_info(sequencer_info.clone())?; self.rooch_store.save_transaction(tx.clone())?; info!("sequencer tx: {} order: {:?}", hash, tx_order); - self.last_sequencer_info.last_order = tx_order; + self.last_sequencer_info = sequencer_info; Ok(tx) } } diff --git a/crates/rooch-store/src/lib.rs b/crates/rooch-store/src/lib.rs index 5a6d2c243b..5d0e7515ef 100644 --- a/crates/rooch-store/src/lib.rs +++ b/crates/rooch-store/src/lib.rs @@ -6,16 +6,22 @@ use crate::meta_store::{MetaDBStore, MetaStore}; use crate::transaction_store::{TransactionDBStore, TransactionStore}; use accumulator::AccumulatorTreeStore; use anyhow::Result; +use moveos_config::store_config::RocksdbConfig; +use moveos_config::DataDirPath; use moveos_types::h256::H256; use once_cell::sync::Lazy; +use raw_store::rocks::RocksDB; use raw_store::{ColumnFamilyName, StoreInstance}; use rooch_types::sequencer::SequencerInfo; use rooch_types::transaction::LedgerTransaction; use std::fmt::{Debug, Display, Formatter}; +use std::path::Path; use std::sync::Arc; pub mod accumulator_store; pub mod meta_store; +#[cfg(test)] +mod tests; pub mod transaction_store; // pub const DEFAULT_PREFIX_NAME: ColumnFamilyName = "default"; @@ -52,7 +58,17 @@ pub struct RoochStore { } impl RoochStore { - pub fn new(instance: StoreInstance) -> Result { + pub fn new(db_path: &Path) -> Result { + let instance = StoreInstance::new_db_instance(RocksDB::new( + db_path, + StoreMeta::get_column_family_names().to_vec(), + RocksdbConfig::default(), + None, + )?); + Self::new_with_instance(instance) + } + + pub fn new_with_instance(instance: StoreInstance) -> Result { let store = Self { transaction_store: TransactionDBStore::new(instance.clone()), meta_store: MetaDBStore::new(instance.clone()), @@ -63,6 +79,12 @@ impl RoochStore { Ok(store) } + pub fn mock_rooch_store() -> Result<(Self, DataDirPath)> { + let tmpdir = moveos_config::temp_dir(); + //The testcases should hold the tmpdir to prevent the tmpdir from being deleted. + Ok((Self::new(tmpdir.path())?, tmpdir)) + } + pub fn get_transaction_store(&self) -> &TransactionDBStore { &self.transaction_store } diff --git a/crates/rooch-store/src/tests/mod.rs b/crates/rooch-store/src/tests/mod.rs new file mode 100644 index 0000000000..cb1dbc2af8 --- /dev/null +++ b/crates/rooch-store/src/tests/mod.rs @@ -0,0 +1,4 @@ +// Copyright (c) RoochNetwork +// SPDX-License-Identifier: Apache-2.0 + +mod test_accumulator; diff --git a/crates/rooch-store/src/tests/test_accumulator.rs b/crates/rooch-store/src/tests/test_accumulator.rs new file mode 100644 index 0000000000..224b9f2faa --- /dev/null +++ b/crates/rooch-store/src/tests/test_accumulator.rs @@ -0,0 +1,25 @@ +// Copyright (c) RoochNetwork +// SPDX-License-Identifier: Apache-2.0 + +use crate::RoochStore; +use accumulator::node_index::NodeIndex; +use accumulator::{AccumulatorNode, AccumulatorTreeStore}; +use moveos_types::h256::H256; + +#[test] +fn test_accumulator_store() { + let (rooch_store, _) = RoochStore::mock_rooch_store().unwrap(); + + let acc_node = AccumulatorNode::new_leaf(NodeIndex::from_inorder_index(1), H256::random()); + let node_hash = acc_node.hash(); + rooch_store + .transaction_accumulator_store + .save_node(acc_node.clone()) + .unwrap(); + let acc_node2 = rooch_store + .transaction_accumulator_store + .get_node(node_hash) + .unwrap() + .unwrap(); + assert_eq!(acc_node, acc_node2); +}