Skip to content

Commit

Permalink
record state root hash to resolve statedb smt rebuild after server re…
Browse files Browse the repository at this point in the history
…start (#539)

* record state root hash to resolve statedb smt rebuild after server restart

* simple option call by map lambda

* cleanup code
  • Loading branch information
baichuan3 authored Jul 27, 2023
1 parent efe2be0 commit 5cc3f5f
Show file tree
Hide file tree
Showing 20 changed files with 569 additions and 425 deletions.
3 changes: 0 additions & 3 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ use moveos_types::transaction::FunctionCall;
use moveos_types::transaction::TransactionExecutionInfo;
use moveos_types::transaction::VerifiedMoveOSTransaction;
use moveos_types::tx_context::TxContext;
// use raw_store::rocks::RocksDB;
// use raw_store::StoreInstance;
// use rooch_config::store_config::StoreConfig;
use rooch_framework::bindings::address_mapping::AddressMapping;
use rooch_framework::bindings::auth_validator::AuthValidatorCaller;
use rooch_framework::bindings::transaction_validator::TransactionValidator;
Expand Down
3 changes: 1 addition & 2 deletions crates/rooch-framework-tests/src/binding_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ pub struct RustBindingTest {

impl RustBindingTest {
pub fn new() -> Result<Self> {
// let moveos_store = MoveOSDB::new_with_memory_store();
let moveos_store = MoveOSStore::mock().unwrap();
let moveos_store = MoveOSStore::mock_moveos_store().unwrap();
let genesis: &RoochGenesis = &rooch_genesis::ROOCH_GENESIS;

let mut moveos = MoveOS::new(moveos_store, genesis.all_natives(), genesis.config.clone())?;
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ mod tests {
fn test_genesis_init() {
let genesis = super::RoochGenesis::build().expect("build rooch framework failed");
// let db = moveos_store::MoveOSStore::new_with_memory_store();
let moveos_store = MoveOSStore::mock().unwrap();
let moveos_store = MoveOSStore::mock_moveos_store().unwrap();
let mut moveos = MoveOS::new(
moveos_store,
all_natives(genesis.gas_params),
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-integration-test-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a> MoveOSTestAdapter<'a> for MoveOSTestRunner<'a> {
None => BTreeMap::new(),
};

let moveos_store = MoveOSStore::mock().unwrap();
let moveos_store = MoveOSStore::mock_moveos_store().unwrap();

let genesis: &RoochGenesis = &rooch_genesis::ROOCH_GENESIS;
let mut moveos = MoveOS::new(
Expand Down
83 changes: 47 additions & 36 deletions crates/rooch-rpc-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use jsonrpsee::server::ServerBuilder;
use jsonrpsee::RpcModule;
use moveos_config::store_config::RocksdbConfig;
use moveos_store::MoveOSStore;
use moveos_store::config_store::ConfigStore;
use moveos_store::{MoveOSDB, MoveOSStore};
use raw_store::rocks::RocksDB;
use raw_store::StoreInstance;
use rooch_config::rpc::server_config::ServerConfig;
Expand Down Expand Up @@ -72,8 +73,8 @@ impl Service {
Self { handle: None }
}

pub async fn start(&mut self, is_mock: bool) -> Result<()> {
self.handle = Some(start_server(is_mock).await?);
pub async fn start(&mut self, is_mock_storage: bool) -> Result<()> {
self.handle = Some(start_server(is_mock_storage).await?);
Ok(())
}

Expand Down Expand Up @@ -108,47 +109,16 @@ impl RpcModuleBuilder {
}

// Start json-rpc server
pub async fn start_server(is_mock: bool) -> Result<ServerHandle> {
pub async fn start_server(is_mock_storage: bool) -> Result<ServerHandle> {
tracing_subscriber::fmt::init();

let config = ServerConfig::default();

let addr: SocketAddr = format!("{}:{}", config.host, config.port).parse()?;
let actor_system = ActorSystem::global_system();

let (rooch_db_path, moveos_db_path) = if !is_mock {
(
StoreConfig::get_rooch_store_dir(),
StoreConfig::get_moveos_store_dir(),
)
} else {
(
moveos_config::temp_dir().path().to_path_buf(),
moveos_config::temp_dir().path().to_path_buf(),
)
};

//Init store
let moveos_store = MoveOSStore::new(StoreInstance::new_db_instance(
RocksDB::new(
moveos_db_path,
moveos_store::StoreMeta::get_column_family_names().to_vec(),
RocksdbConfig::default(),
None,
)
.unwrap(),
))
.unwrap();
let rooch_store = RoochStore::new(StoreInstance::new_db_instance(
RocksDB::new(
rooch_db_path,
rooch_store::StoreMeta::get_column_family_names().to_vec(),
RocksdbConfig::default(),
None,
)
.unwrap(),
))
.unwrap();
let (moveos_store, rooch_store) = init_stroage(is_mock_storage)?;

// Init executor
let executor = ExecutorActor::new(moveos_store, rooch_store.clone())?
Expand Down Expand Up @@ -220,3 +190,44 @@ fn _build_rpc_api<M: Send + Sync + 'static>(mut rpc_module: RpcModule<M>) -> Rpc

rpc_module
}

fn init_stroage(is_mock_storage: bool) -> Result<(MoveOSStore, RoochStore)> {
let (rooch_db_path, moveos_db_path) = if !is_mock_storage {
(
StoreConfig::get_rooch_store_dir(),
StoreConfig::get_moveos_store_dir(),
)
} else {
(
moveos_config::temp_dir().path().to_path_buf(),
moveos_config::temp_dir().path().to_path_buf(),
)
};

//Init store
let moveosdb = MoveOSDB::new(StoreInstance::new_db_instance(
RocksDB::new(
moveos_db_path,
moveos_store::StoreMeta::get_column_family_names().to_vec(),
RocksdbConfig::default(),
None,
)
.unwrap(),
))?;
let lastest_state_root = moveosdb
.config_store
.get_startup_info()?
.map(|info| info.state_root_hash);
let moveos_store = MoveOSStore::new_with_root(moveosdb, lastest_state_root).unwrap();

let rooch_store = RoochStore::new(StoreInstance::new_db_instance(
RocksDB::new(
rooch_db_path,
rooch_store::StoreMeta::get_column_family_names().to_vec(),
RocksdbConfig::default(),
None,
)
.unwrap(),
))?;
Ok((moveos_store, rooch_store))
}
4 changes: 2 additions & 2 deletions crates/rooch/src/commands/move_cli/commands/unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use move_package::BuildConfig;
use move_unit_test::extensions::set_extension_hook;
use move_vm_runtime::native_extensions::NativeContextExtensions;
use moveos_stdlib::natives::moveos_stdlib::raw_table::NativeTableContext;
use moveos_store::state_store::StateDBStore;
use moveos_store::state_store::statedb::StateDBStore;
use moveos_store::MoveOSStore;
use moveos_verifier::build::build_model_with_test_attr;
use moveos_verifier::metadata::run_extended_checks;
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Test {
}

static STATEDBSTORE: Lazy<Box<StateDBStore>> =
Lazy::new(|| Box::new(MoveOSStore::mock().unwrap().state_store));
Lazy::new(|| Box::new(MoveOSStore::mock_moveos_store().unwrap().statedb));

fn new_moveos_natives_runtime(ext: &mut NativeContextExtensions) {
let statedb_store = Lazy::force(&STATEDBSTORE).as_ref();
Expand Down
3 changes: 0 additions & 3 deletions moveos/moveos-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ repository = { workspace = true }
rust-version = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
#[lib]
#proc-macro = true

[dependencies]
anyhow = { workspace = true }
Expand All @@ -23,7 +21,6 @@ serde = { workspace = true }
serde_bytes = { workspace = true }
hex = { workspace = true }
parking_lot = { workspace = true }
#proc-macro2 = { workspace = true }
quote = { workspace = true }
num_enum = { workspace = true }
once_cell = { workspace = true }
Expand Down
28 changes: 28 additions & 0 deletions moveos/moveos-store/src/config_store/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::CONFIG_PREFIX_NAME;
use anyhow::Result;
use moveos_types::startup_info::StartupInfo;
use raw_store::{derive_store, CodecKVStore};
use std::string::ToString;

pub const STARTUP_INFO_KEY: &str = "startup_info";

derive_store!(StartupInfoDBStore, String, StartupInfo, CONFIG_PREFIX_NAME);

pub trait ConfigStore {
fn get_startup_info(&self) -> Result<Option<StartupInfo>>;

fn save_startup_info(&self, startup_info: StartupInfo) -> Result<()>;
}

impl ConfigStore for StartupInfoDBStore {
fn get_startup_info(&self) -> Result<Option<StartupInfo>> {
self.kv_get(STARTUP_INFO_KEY.to_string())
}

fn save_startup_info(&self, startup_info: StartupInfo) -> Result<()> {
self.put_sync(STARTUP_INFO_KEY.to_string(), startup_info)
}
}
Loading

0 comments on commit 5cc3f5f

Please sign in to comment.