Skip to content

Commit

Permalink
refactor: improve SharedBuilder interface
Browse files Browse the repository at this point in the history
Before this refactor, SharedBuilder user needs to specify db type
(rocksdb or memory) in two places, one in type instance another in
method name, e.g. `SharedBuilder<ChainKVStore<MemoryKeyValueDB>>::new_memory()`,
which is both unnecessary and may cause inconsistent invocation.
  • Loading branch information
Jan Xie committed Jan 29, 2019
1 parent 568e436 commit 840a72e
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub mod test {
fn start_chain(
consensus: Option<Consensus>,
) -> (ChainController, Shared<ChainKVStore<MemoryKeyValueDB>>) {
let builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory();
let builder = SharedBuilder::<MemoryKeyValueDB>::new();
let shared = builder
.consensus(consensus.unwrap_or_else(|| Consensus::default().set_verification(false)))
.build();
Expand Down
2 changes: 1 addition & 1 deletion miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ mod tests {
Shared<ChainKVStore<MemoryKeyValueDB>>,
NotifyController,
) {
let mut builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory();
let mut builder = SharedBuilder::<MemoryKeyValueDB>::new();
if let Some(consensus) = consensus {
builder = builder.consensus(consensus);
}
Expand Down
2 changes: 1 addition & 1 deletion pool/src/tests/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ impl<CI: ChainIndex + 'static> TestPool<CI> {
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::<ChainKVStore<MemoryKeyValueDB>>::new_memory()
let shared = SharedBuilder::<MemoryKeyValueDB>::new()
.consensus(Consensus::default().set_verification(false))
.build();

Expand Down
53 changes: 32 additions & 21 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,41 +439,52 @@ impl<CI: ChainIndex> BlockMedianTimeContext for Shared<CI> {
}
}

pub struct SharedBuilder<CI> {
store: CI,
pub struct SharedBuilder<DB: KeyValueDB> {
db: Option<DB>,
consensus: Option<Consensus>,
}

impl<CI: ChainIndex> SharedBuilder<CI> {
pub fn new_memory() -> SharedBuilder<ChainKVStore<MemoryKeyValueDB>> {
let db = MemoryKeyValueDB::open(COLUMNS as usize);
SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_simple(db)
}

pub fn new_rocks(db: &DBConfig) -> SharedBuilder<ChainKVStore<CacheDB<RocksDB>>> {
let db = CacheDB::new(
RocksDB::open(db, COLUMNS),
&[(COLUMN_BLOCK_HEADER.unwrap(), 4096)],
);
SharedBuilder::<ChainKVStore<CacheDB<RocksDB>>>::new_simple(db)
impl<DB: KeyValueDB> Default for SharedBuilder<DB> {
fn default() -> Self {
SharedBuilder {
db: None,
consensus: None,
}
}
}

pub fn new_simple<T: 'static + KeyValueDB>(db: T) -> SharedBuilder<ChainKVStore<T>> {
let mut consensus = Consensus::default();
consensus.initial_block_reward = 50;
impl SharedBuilder<MemoryKeyValueDB> {
pub fn new() -> Self {
SharedBuilder {
store: ChainKVStore::new(db),
consensus: Some(consensus),
db: Some(MemoryKeyValueDB::open(COLUMNS as usize)),
consensus: None,
}
}
}

impl SharedBuilder<CacheDB<RocksDB>> {
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<DB: 'static + KeyValueDB> SharedBuilder<DB> {
pub fn consensus(mut self, value: Consensus) -> Self {
self.consensus = Some(value);
self
}

pub fn build(self) -> Shared<CI> {
pub fn build(self) -> Shared<ChainKVStore<DB>> {
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)
}
}
2 changes: 1 addition & 1 deletion shared/src/tests/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ckb_db::{kvdb::KeyValueDB, memorydb::MemoryKeyValueDB};
use numext_fixed_hash::H256;

fn new_shared() -> Shared<ChainKVStore<MemoryKeyValueDB>> {
SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory().build()
SharedBuilder::<MemoryKeyValueDB>::new().build()
}

fn insert_block_timestamps<T>(store: &ChainKVStore<T>, timestamps: &[u64]) -> Vec<H256>
Expand Down
4 changes: 2 additions & 2 deletions src/cli/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ChainKVStore<CacheDB<RocksDB>>>::new_rocks(&setup.configs.db)
let shared = SharedBuilder::<CacheDB<RocksDB>>::default()
.consensus(setup.chain_spec.to_consensus().unwrap())
.db(&setup.configs.db)
.build();
Export::new(shared, format, target.into())
.execute()
Expand Down
4 changes: 2 additions & 2 deletions src/cli/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ChainKVStore<CacheDB<RocksDB>>>::new_rocks(&setup.configs.db)
let shared = SharedBuilder::<CacheDB<RocksDB>>::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();
Expand Down
4 changes: 2 additions & 2 deletions src/cli/run_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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::<ChainKVStore<CacheDB<RocksDB>>>::new_rocks(&setup.configs.db)
let shared = SharedBuilder::<CacheDB<RocksDB>>::default()
.consensus(consensus)
.db(&setup.configs.db)
.build();

let (_handle, notify) = NotifyService::default().start(Some("notify"));
Expand Down
2 changes: 1 addition & 1 deletion sync/src/synchronizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ mod tests {
Shared<ChainKVStore<MemoryKeyValueDB>>,
NotifyController,
) {
let mut builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory();
let mut builder = SharedBuilder::<MemoryKeyValueDB>::new();
if let Some(consensus) = consensus {
builder = builder.consensus(consensus);
}
Expand Down
2 changes: 1 addition & 1 deletion sync/src/tests/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ fn setup_node(
);
let consensus = Consensus::default().set_genesis_block(block.clone());

let shared = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory()
let shared = SharedBuilder::<MemoryKeyValueDB>::new()
.consensus(consensus)
.build();
let (chain_controller, chain_receivers) = ChainController::build();
Expand Down
2 changes: 1 addition & 1 deletion sync/src/tests/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn setup_node(
);

let consensus = Consensus::default().set_genesis_block(block.clone());
let shared = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory()
let shared = SharedBuilder::<MemoryKeyValueDB>::new()
.consensus(consensus)
.build();
let (chain_controller, chain_receivers) = ChainController::build();
Expand Down
2 changes: 1 addition & 1 deletion verification/src/tests/commit_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn create_transaction(parent: &H256) -> Transaction {
fn start_chain(
consensus: Option<Consensus>,
) -> (ChainController, Shared<ChainKVStore<MemoryKeyValueDB>>) {
let mut builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory();
let mut builder = SharedBuilder::<MemoryKeyValueDB>::new();
if let Some(consensus) = consensus {
builder = builder.consensus(consensus);
}
Expand Down
2 changes: 1 addition & 1 deletion verification/src/tests/uncle_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn gen_block(parent_header: &Header, nonce: u64, difficulty: U256) -> Block {
fn start_chain(
consensus: Option<Consensus>,
) -> (ChainController, Shared<ChainKVStore<MemoryKeyValueDB>>) {
let mut builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory();
let mut builder = SharedBuilder::<MemoryKeyValueDB>::new();
if let Some(consensus) = consensus {
builder = builder.consensus(consensus);
}
Expand Down

0 comments on commit 840a72e

Please sign in to comment.