From 77ed71abbe486e6caaa12b52d178111b6bc1c480 Mon Sep 17 00:00:00 2001 From: Softcloud Date: Sat, 3 Feb 2024 02:10:21 +0800 Subject: [PATCH] FIX: using readOnly rocksdb in olavm. --- core/src/storage/db.rs | 25 +++++++++++++++++++++++++ mini-ola/src/subcommands/invoke.rs | 2 +- zk-vm/src/lib.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/core/src/storage/db.rs b/core/src/storage/db.rs index b062b09f..50239f96 100644 --- a/core/src/storage/db.rs +++ b/core/src/storage/db.rs @@ -140,6 +140,31 @@ impl RocksDB { } } + pub fn new_read_only>(database: Database, path: P, tune_options: bool) -> Self { + let options = Self::rocksdb_options(tune_options); + let db = match database { + Database::MerkleTree => { + let cfs = MerkleTreeColumnFamily::all().iter().map(|cf| { + ColumnFamilyDescriptor::new(cf.to_string(), Self::rocksdb_options(tune_options)) + }); + DB::open_cf_descriptors_read_only(&options, path, cfs, false) + .expect("failed to init rocksdb") + } + Database::Sequencer => { + let cfs = SequencerColumnFamily::all().iter().map(|cf| { + ColumnFamilyDescriptor::new(cf.to_string(), Self::rocksdb_options(tune_options)) + }); + DB::open_cf_descriptors_read_only(&options, path, cfs, false) + .expect("failed to init rocksdb") + } + }; + + Self { + db, + _registry_entry: RegistryEntry::new(), + } + } + fn rocksdb_options(tune_options: bool) -> Options { let mut options = Options::default(); options.create_missing_column_families(true); diff --git a/mini-ola/src/subcommands/invoke.rs b/mini-ola/src/subcommands/invoke.rs index 90f5a00f..c5be00d1 100644 --- a/mini-ola/src/subcommands/invoke.rs +++ b/mini-ola/src/subcommands/invoke.rs @@ -121,7 +121,7 @@ impl Invoke { tx_hash: [0; 4].map(|n| GoldilocksField::from_canonical_u64(n)), }; - let mut vm = OlaVM::new( + let mut vm = OlaVM::new_local( tree_db_path_buf.as_path(), state_db_path_buf.as_path(), tx_init_info, diff --git a/zk-vm/src/lib.rs b/zk-vm/src/lib.rs index ee4ac2b2..d4b2526d 100644 --- a/zk-vm/src/lib.rs +++ b/zk-vm/src/lib.rs @@ -50,7 +50,7 @@ pub struct OlaVM { } impl OlaVM { - pub fn new(tree_db_path: &Path, state_db_path: &Path, ctx_info: TxCtxInfo) -> Self { + pub fn new_local(tree_db_path: &Path, state_db_path: &Path, ctx_info: TxCtxInfo) -> Self { // let acc_db = RocksDB::new(Database::MerkleTree, tree_db_path, false); // let account_tree = AccountTree::new(acc_db); let state_db = RocksDB::new(Database::Sequencer, state_db_path, false); @@ -72,10 +72,32 @@ impl OlaVM { } } + pub fn new(tree_db_path: &Path, state_db_path: &Path, ctx_info: TxCtxInfo) -> Self { + // let acc_db = RocksDB::new(Database::MerkleTree, tree_db_path, false); + // let account_tree = AccountTree::new(acc_db); + let state_db = RocksDB::new_read_only(Database::Sequencer, state_db_path, false); + + let ola_state = NodeState::new( + Contracts { + contracts: HashMap::new(), + }, + StateStorage { db: state_db }, + ZkHasher::default(), + ); + + OlaVM { + ola_state, + // account_tree, + process_ctx: Vec::new(), + ctx_info, + is_call: false, + } + } + pub fn new_call(tree_db_path: &Path, state_db_path: &Path, ctx_info: TxCtxInfo) -> Self { // let acc_db = RocksDB::new(Database::MerkleTree, tree_db_path, false); // let account_tree = AccountTree::new(acc_db); - let state_db = RocksDB::new(Database::Sequencer, state_db_path, false); + let state_db = RocksDB::new_read_only(Database::Sequencer, state_db_path, false); let ola_state = NodeState::new( Contracts { contracts: HashMap::new(),