diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 3edf0d41a1..6223ebd8ec 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -413,7 +413,7 @@ pub mod test { fn start_chain( consensus: Option, ) -> (ChainController, Shared>) { - let builder = SharedBuilder::>::new_memory(); + let builder = SharedBuilder::::new(); let shared = builder .consensus(consensus.unwrap_or_else(|| Consensus::default().set_verification(false))) .build(); diff --git a/miner/src/block_assembler.rs b/miner/src/block_assembler.rs index c313ab4f71..6501064c2e 100644 --- a/miner/src/block_assembler.rs +++ b/miner/src/block_assembler.rs @@ -423,7 +423,7 @@ mod tests { Shared>, NotifyController, ) { - let mut builder = SharedBuilder::>::new_memory(); + let mut builder = SharedBuilder::::new(); if let Some(consensus) = consensus { builder = builder.consensus(consensus); } diff --git a/pool/src/tests/pool.rs b/pool/src/tests/pool.rs index 9a4cd5fd9d..89785444ba 100644 --- a/pool/src/tests/pool.rs +++ b/pool/src/tests/pool.rs @@ -580,7 +580,7 @@ impl TestPool { let (_handle, notify) = NotifyService::default().start::<&str>(None); let new_tip_receiver = notify.subscribe_new_tip("txs_pool"); let switch_fork_receiver = notify.subscribe_switch_fork("txs_pool"); - let shared = SharedBuilder::>::new_memory() + let shared = SharedBuilder::::new() .consensus(Consensus::default().set_verification(false)) .build(); diff --git a/shared/src/shared.rs b/shared/src/shared.rs index 3ef3231135..d4683267bf 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -439,41 +439,52 @@ impl BlockMedianTimeContext for Shared { } } -pub struct SharedBuilder { - store: CI, +pub struct SharedBuilder { + db: Option, consensus: Option, } -impl SharedBuilder { - pub fn new_memory() -> SharedBuilder> { - let db = MemoryKeyValueDB::open(COLUMNS as usize); - SharedBuilder::>::new_simple(db) - } - - pub fn new_rocks(db: &DBConfig) -> SharedBuilder>> { - let db = CacheDB::new( - RocksDB::open(db, COLUMNS), - &[(COLUMN_BLOCK_HEADER.unwrap(), 4096)], - ); - SharedBuilder::>>::new_simple(db) +impl Default for SharedBuilder { + fn default() -> Self { + SharedBuilder { + db: None, + consensus: None, + } } +} - pub fn new_simple(db: T) -> SharedBuilder> { - let mut consensus = Consensus::default(); - consensus.initial_block_reward = 50; +impl SharedBuilder { + pub fn new() -> Self { SharedBuilder { - store: ChainKVStore::new(db), - consensus: Some(consensus), + db: Some(MemoryKeyValueDB::open(COLUMNS as usize)), + consensus: None, } } +} + +impl SharedBuilder> { + pub fn new() -> Self { + Default::default() + } + + pub fn db(mut self, config: &DBConfig) -> Self { + self.db = Some(CacheDB::new( + RocksDB::open(config, COLUMNS), + &[(COLUMN_BLOCK_HEADER.unwrap(), 4096)], + )); + self + } +} +impl SharedBuilder { pub fn consensus(mut self, value: Consensus) -> Self { self.consensus = Some(value); self } - pub fn build(self) -> Shared { + pub fn build(self) -> Shared> { + let store = ChainKVStore::new(self.db.unwrap()); let consensus = self.consensus.unwrap_or_else(Consensus::default); - Shared::new(self.store, consensus) + Shared::new(store, consensus) } } diff --git a/shared/src/tests/shared.rs b/shared/src/tests/shared.rs index d4016c1a79..3309e3e45e 100644 --- a/shared/src/tests/shared.rs +++ b/shared/src/tests/shared.rs @@ -8,7 +8,7 @@ use ckb_db::{kvdb::KeyValueDB, memorydb::MemoryKeyValueDB}; use numext_fixed_hash::H256; fn new_shared() -> Shared> { - SharedBuilder::>::new_memory().build() + SharedBuilder::::new().build() } fn insert_block_timestamps(store: &ChainKVStore, timestamps: &[u64]) -> Vec diff --git a/src/cli/export.rs b/src/cli/export.rs index 1e6568cd07..effd7d6b3e 100644 --- a/src/cli/export.rs +++ b/src/cli/export.rs @@ -3,15 +3,15 @@ use ckb_db::diskdb::RocksDB; use ckb_instrument::{Export, Format}; use ckb_shared::cachedb::CacheDB; use ckb_shared::shared::SharedBuilder; -use ckb_shared::store::ChainKVStore; use clap::{value_t, ArgMatches}; pub fn export(setup: &Setup, matches: &ArgMatches) { let format = value_t!(matches.value_of("format"), Format).unwrap_or_else(|e| e.exit()); let target = value_t!(matches.value_of("target"), String).unwrap_or_else(|e| e.exit()); - let shared = SharedBuilder::>>::new_rocks(&setup.configs.db) + let shared = SharedBuilder::>::default() .consensus(setup.chain_spec.to_consensus().unwrap()) + .db(&setup.configs.db) .build(); Export::new(shared, format, target.into()) .execute() diff --git a/src/cli/import.rs b/src/cli/import.rs index b243ae8218..c3f429650b 100644 --- a/src/cli/import.rs +++ b/src/cli/import.rs @@ -4,15 +4,15 @@ use ckb_db::diskdb::RocksDB; use ckb_instrument::{Format, Import}; use ckb_shared::cachedb::CacheDB; use ckb_shared::shared::SharedBuilder; -use ckb_shared::store::ChainKVStore; use clap::{value_t, ArgMatches}; pub fn import(setup: &Setup, matches: &ArgMatches) { let format = value_t!(matches.value_of("format"), Format).unwrap_or_else(|e| e.exit()); let source = value_t!(matches.value_of("source"), String).unwrap_or_else(|e| e.exit()); - let shared = SharedBuilder::>>::new_rocks(&setup.configs.db) + let shared = SharedBuilder::>::default() .consensus(setup.chain_spec.to_consensus().unwrap()) + .db(&setup.configs.db) .build(); let (chain_controller, chain_receivers) = ChainController::build(); let chain_service = ChainBuilder::new(shared).build(); diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 2a7d363013..28fc58a9c7 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -14,7 +14,6 @@ use ckb_rpc::RpcServer; use ckb_shared::cachedb::CacheDB; use ckb_shared::index::ChainIndex; use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; -use ckb_shared::store::ChainKVStore; use ckb_sync::{ NetTimeProtocol, Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID, TIME_PROTOCOL_ID, }; @@ -28,8 +27,9 @@ pub fn run(setup: Setup) { let consensus = setup.chain_spec.to_consensus().unwrap(); let pow_engine = setup.chain_spec.pow_engine(); - let shared = SharedBuilder::>>::new_rocks(&setup.configs.db) + let shared = SharedBuilder::>::default() .consensus(consensus) + .db(&setup.configs.db) .build(); let (_handle, notify) = NotifyService::default().start(Some("notify")); diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index f2c811fdd8..14a8b73c6a 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -753,7 +753,7 @@ mod tests { Shared>, NotifyController, ) { - let mut builder = SharedBuilder::>::new_memory(); + let mut builder = SharedBuilder::::new(); if let Some(consensus) = consensus { builder = builder.consensus(consensus); } diff --git a/sync/src/tests/relayer.rs b/sync/src/tests/relayer.rs index 30ba45c342..1fd8ad91ce 100644 --- a/sync/src/tests/relayer.rs +++ b/sync/src/tests/relayer.rs @@ -311,7 +311,7 @@ fn setup_node( ); let consensus = Consensus::default().set_genesis_block(block.clone()); - let shared = SharedBuilder::>::new_memory() + let shared = SharedBuilder::::new() .consensus(consensus) .build(); let (chain_controller, chain_receivers) = ChainController::build(); diff --git a/sync/src/tests/synchronizer.rs b/sync/src/tests/synchronizer.rs index e8e0d40f92..f734788581 100644 --- a/sync/src/tests/synchronizer.rs +++ b/sync/src/tests/synchronizer.rs @@ -72,7 +72,7 @@ fn setup_node( ); let consensus = Consensus::default().set_genesis_block(block.clone()); - let shared = SharedBuilder::>::new_memory() + let shared = SharedBuilder::::new() .consensus(consensus) .build(); let (chain_controller, chain_receivers) = ChainController::build(); diff --git a/verification/src/tests/commit_verifier.rs b/verification/src/tests/commit_verifier.rs index c326c3af12..d799a55d88 100644 --- a/verification/src/tests/commit_verifier.rs +++ b/verification/src/tests/commit_verifier.rs @@ -80,7 +80,7 @@ fn create_transaction(parent: &H256) -> Transaction { fn start_chain( consensus: Option, ) -> (ChainController, Shared>) { - let mut builder = SharedBuilder::>::new_memory(); + let mut builder = SharedBuilder::::new(); if let Some(consensus) = consensus { builder = builder.consensus(consensus); } diff --git a/verification/src/tests/uncle_verifier.rs b/verification/src/tests/uncle_verifier.rs index 6981dc51e1..d29c9513e2 100644 --- a/verification/src/tests/uncle_verifier.rs +++ b/verification/src/tests/uncle_verifier.rs @@ -38,7 +38,7 @@ fn gen_block(parent_header: &Header, nonce: u64, difficulty: U256) -> Block { fn start_chain( consensus: Option, ) -> (ChainController, Shared>) { - let mut builder = SharedBuilder::>::new_memory(); + let mut builder = SharedBuilder::::new(); if let Some(consensus) = consensus { builder = builder.consensus(consensus); }