From 9f046a023e72c703529ad6f4ee9703c8def26c4c Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Fri, 5 Apr 2019 10:42:25 -0700 Subject: [PATCH] move transaction_count up (#3618) * move transaction_count up * fixup --- runtime/src/accounts.rs | 45 --------------------------------- runtime/src/bank.rs | 56 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index a71becb6af..f2c879af7e 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -183,9 +183,6 @@ type AccountStorage = Vec; #[derive(Default, Debug)] struct ForkInfo { - /// The number of transactions processed without error - transaction_count: u64, - /// List of all parents of this fork parents: Vec, @@ -284,7 +281,6 @@ impl AccountsDB { if let Some(parent) = parent { fork_info.parents.push(parent); if let Some(parent_fork_info) = fork_infos.get(&parent) { - fork_info.transaction_count = parent_fork_info.transaction_count; fork_info .parents .extend_from_slice(&parent_fork_info.parents); @@ -739,20 +735,6 @@ impl AccountsDB { .collect() } - pub fn increment_transaction_count(&self, fork: Fork, tx_count: usize) { - let mut fork_infos = self.fork_infos.write().unwrap(); - let fork_info = fork_infos.entry(fork).or_insert(ForkInfo::default()); - fork_info.transaction_count += tx_count as u64; - } - - pub fn transaction_count(&self, fork: Fork) -> u64 { - self.fork_infos - .read() - .unwrap() - .get(&fork) - .map_or(0, |fork_info| fork_info.transaction_count) - } - fn remove_parents(&self, fork: Fork) -> Vec { let mut squashed_vote_accounts = self.get_vote_accounts(fork); squashed_vote_accounts.retain(|_, account| account.lamports != 0); @@ -995,14 +977,6 @@ impl Accounts { self.accounts_db.store_accounts(fork, txs, res, loaded) } - pub fn increment_transaction_count(&self, fork: Fork, tx_count: usize) { - self.accounts_db.increment_transaction_count(fork, tx_count) - } - - pub fn transaction_count(&self, fork: Fork) -> u64 { - self.accounts_db.transaction_count(fork) - } - /// accounts starts with an empty data structure for every child/fork /// this function squashes all the parents into this instance pub fn squash(&self, fork: Fork) { @@ -1915,25 +1889,6 @@ mod tests { assert_eq!(error_counters.account_not_found, 1); } - #[test] - fn test_accountsdb_inherit_tx_count() { - let paths = get_tmp_accounts_path!(); - let accounts = AccountsDB::new(0, &paths.paths); - assert_eq!(accounts.transaction_count(0), 0); - accounts.increment_transaction_count(0, 1); - assert_eq!(accounts.transaction_count(0), 1); - // fork and check that tx count is inherited - accounts.add_fork(1, Some(0)); - assert_eq!(accounts.transaction_count(1), 1); - // Parent fork shouldn't change - accounts.increment_transaction_count(1, 1); - assert_eq!(accounts.transaction_count(1), 2); - assert_eq!(accounts.transaction_count(0), 1); - // Squash shouldn't effect tx count - accounts.squash(1); - assert_eq!(accounts.transaction_count(1), 2); - } - #[test] fn test_load_by_program() { let paths = get_tmp_accounts_path!(); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 35483bea0c..b9eee6c1d9 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -131,6 +131,9 @@ pub struct Bank { /// Hash of this Bank's parent's state parent_hash: Hash, + /// The number of transactions processed without error + transaction_count: AtomicUsize, // TODO: Use AtomicU64 if/when available + /// Bank tick height tick_height: AtomicUsize, // TODO: Use AtomicU64 if/when available @@ -194,6 +197,9 @@ impl Bank { bank.blockhash_queue = RwLock::new(parent.blockhash_queue.read().unwrap().clone()); bank.status_cache = parent.status_cache.clone(); + bank.transaction_count + .store(parent.transaction_count() as usize, Ordering::Relaxed); + bank.tick_height .store(parent.tick_height.load(Ordering::SeqCst), Ordering::SeqCst); bank.ticks_per_slot = parent.ticks_per_slot; @@ -647,8 +653,7 @@ impl Bank { inc_new_counter_info!("bank-process_transactions-error_count", err_count); } - self.accounts - .increment_transaction_count(self.accounts_id, tx_count); + self.increment_transaction_count(tx_count); inc_new_counter_info!("bank-process_transactions-txs", tx_count); if 0 != error_counters.blockhash_not_found { @@ -839,7 +844,11 @@ impl Bank { } pub fn transaction_count(&self) -> u64 { - self.accounts.transaction_count(self.accounts_id) + self.transaction_count.load(Ordering::Relaxed) as u64 + } + fn increment_transaction_count(&self, tx_count: usize) { + self.transaction_count + .fetch_add(tx_count, Ordering::Relaxed); } pub fn get_signature_confirmation_status( @@ -1758,4 +1767,45 @@ mod tests { assert!(!bank5.is_in_subtree_of(2)); assert!(!bank5.is_in_subtree_of(4)); } + + #[test] + fn test_bank_inherit_tx_count() { + let (genesis_block, mint_keypair) = GenesisBlock::new(500); + let bank0 = Arc::new(Bank::new(&genesis_block)); + + // Bank 1 + let bank1 = Arc::new(new_from_parent(&bank0)); + // Bank 2 + let bank2 = new_from_parent(&bank0); + + // transfer a token + assert_eq!( + bank1.process_transaction(&system_transaction::transfer( + &mint_keypair, + &Keypair::new().pubkey(), + 1, + genesis_block.hash(), + 0 + )), + Ok(()) + ); + + assert_eq!(bank0.transaction_count(), 0); + assert_eq!(bank2.transaction_count(), 0); + assert_eq!(bank1.transaction_count(), 1); + + bank1.squash(); + + assert_eq!(bank0.transaction_count(), 0); + assert_eq!(bank2.transaction_count(), 0); + assert_eq!(bank1.transaction_count(), 1); + + let bank6 = new_from_parent(&bank1); + assert_eq!(bank1.transaction_count(), 1); + assert_eq!(bank6.transaction_count(), 1); + + bank6.squash(); + assert_eq!(bank6.transaction_count(), 1); + } + }