Skip to content

Commit

Permalink
refactor: txs verify cache required
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Mar 8, 2019
1 parent f334752 commit 79cec0a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 59 deletions.
24 changes: 0 additions & 24 deletions shared/src/block_median_time_context.rs

This file was deleted.

1 change: 0 additions & 1 deletion shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! - [Chain](chain::chain::Chain) represent a struct which
//! implement `ChainProvider`
pub mod block_median_time_context;
pub mod cachedb;
pub mod chain_state;
pub mod error;
Expand Down
47 changes: 21 additions & 26 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::sync::Arc;
pub struct Shared<CI> {
store: Arc<CI>,
chain_state: Arc<RwLock<ChainState>>,
txs_verify_cache: Arc<RwLock<Option<LruCache<H256, Cycle>>>>,
txs_verify_cache: Arc<RwLock<LruCache<H256, Cycle>>>,
consensus: Arc<Consensus>,
tx_pool: Arc<RwLock<TxPool>>,
}
Expand All @@ -52,7 +52,7 @@ impl<CI: ChainIndex> Shared<CI> {
pub fn new(
store: CI,
consensus: Consensus,
txs_verify_cache: Arc<RwLock<Option<LruCache<H256, Cycle>>>>,
txs_verify_cache_size: usize,
tx_pool_config: TxPoolConfig,
) -> Self {
let chain_state = {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<CI: ChainIndex> Shared<CI> {
Shared {
store: Arc::new(store),
chain_state,
txs_verify_cache,
txs_verify_cache: Arc::new(RwLock::new(LruCache::new(txs_verify_cache_size))),
consensus: Arc::new(consensus),
tx_pool: Arc::new(RwLock::new(TxPool::new(tx_pool_config))),
}
Expand All @@ -104,7 +104,7 @@ impl<CI: ChainIndex> Shared<CI> {
&self.store
}

pub fn txs_verify_cache(&self) -> &RwLock<Option<LruCache<H256, Cycle>>> {
pub fn txs_verify_cache(&self) -> &RwLock<LruCache<H256, Cycle>> {
&self.txs_verify_cache
}

Expand Down Expand Up @@ -202,21 +202,16 @@ impl<CI: ChainIndex> Shared<CI> {
fn verify_rtx(
&self,
rtx: &ResolvedTransaction,
txs_cache: &mut Option<LruCache<H256, Cycle>>,
txs_cache: &mut LruCache<H256, Cycle>,
) -> Result<Cycle, TransactionError> {
let tx_hash = rtx.transaction.hash();
match txs_cache
.as_ref()
.and_then(|cache| cache.get(&tx_hash).cloned())
{
Some(cycles) => Ok(cycles),
match txs_cache.get(&tx_hash) {
Some(cycles) => Ok(*cycles),
None => {
let cycles =
TransactionVerifier::new(&rtx).verify(self.consensus.max_block_cycles())?;
// write cache
txs_cache
.as_mut()
.and_then(|cache| cache.insert(tx_hash, cycles));
txs_cache.insert(tx_hash, cycles);
Ok(cycles)
}
}
Expand Down Expand Up @@ -251,7 +246,7 @@ impl<CI: ChainIndex> Shared<CI> {
&self,
chain_state: &ChainState,
tx_pool: &mut TxPool,
txs_cache: &mut Option<LruCache<H256, Cycle>>,
txs_cache: &mut LruCache<H256, Cycle>,
mut entry: PoolEntry,
) -> Result<PromoteTxResult, PoolError> {
let tx = &entry.transaction;
Expand Down Expand Up @@ -330,7 +325,7 @@ impl<CI: ChainIndex> Shared<CI> {
&self,
chain_state: &ChainState,
tx_pool: &mut TxPool,
txs_cache: &mut Option<LruCache<H256, Cycle>>,
txs_cache: &mut LruCache<H256, Cycle>,
tx: &Transaction,
) {
let entries = tx_pool.orphan.reconcile_tx(tx);
Expand Down Expand Up @@ -692,19 +687,21 @@ impl SharedBuilder<CacheDB<RocksDB>> {
));
self
}

pub fn tx_pool_config(mut self, config: TxPoolConfig) -> Self {
self.tx_pool_config = Some(config);
self
}
}

pub const MIN_TXS_VERIFY_CACHE_SIZE: Option<usize> = Some(100);

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

pub fn tx_pool_config(mut self, config: TxPoolConfig) -> Self {
self.tx_pool_config = Some(config);
self
}

pub fn txs_verify_cache_size(mut self, value: usize) -> Self {
self.txs_verify_cache_size = Some(value);
self
Expand All @@ -714,11 +711,9 @@ impl<DB: 'static + KeyValueDB> SharedBuilder<DB> {
let store = ChainKVStore::new(self.db.unwrap());
let consensus = self.consensus.unwrap_or_else(Consensus::default);
let tx_pool_config = self.tx_pool_config.unwrap_or_else(Default::default);
Shared::new(
store,
consensus,
Arc::new(RwLock::new(self.txs_verify_cache_size.map(LruCache::new))),
tx_pool_config,
)
let txs_verify_cache_size =
std::cmp::max(MIN_TXS_VERIFY_CACHE_SIZE, self.txs_verify_cache_size)
.expect("txs_verify_cache_size MUST not be none");
Shared::new(store, consensus, txs_verify_cache_size, tx_pool_config)
}
}
11 changes: 3 additions & 8 deletions verification/src/block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl TransactionsVerifier {

pub fn verify<F: Fn(&OutPoint) -> CellStatus>(
&self,
txs_verify_cache: &mut Option<LruCache<H256, Cycle>>,
txs_verify_cache: &mut LruCache<H256, Cycle>,
block: &Block,
cell_resolver: F,
) -> Result<(), Error> {
Expand Down Expand Up @@ -442,10 +442,7 @@ impl TransactionsVerifier {
.par_iter()
.enumerate()
.map(|(index, tx)| {
if let Some(cycles) = txs_verify_cache
.as_ref()
.and_then(|cache| cache.get(&tx.transaction.hash()))
{
if let Some(cycles) = txs_verify_cache.get(&tx.transaction.hash()) {
InputVerifier::new(&tx)
.verify()
.map_err(|e| Error::Transactions((index, e)))
Expand All @@ -463,9 +460,7 @@ impl TransactionsVerifier {

for (hash, cycles) in cycles_set {
if let Some(h) = hash {
txs_verify_cache
.as_mut()
.map(|cache| cache.insert(h, cycles));
txs_verify_cache.insert(h, cycles);
}
}

Expand Down

0 comments on commit 79cec0a

Please sign in to comment.