From 62040cef560331a4f02a8d0a034652056458179e Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 30 Mar 2020 11:29:30 -0600 Subject: [PATCH] Store BlockCommitmentCache slot and root metadata (#9154) automerge --- core/src/commitment.rs | 62 ++++++++++++++++++++++++++++++++++------ core/src/replay_stage.rs | 20 +++++++++++-- core/src/rpc.rs | 40 ++++++++++++++++---------- 3 files changed, 96 insertions(+), 26 deletions(-) diff --git a/core/src/commitment.rs b/core/src/commitment.rs index c73b95ee80..2496c2fd27 100644 --- a/core/src/commitment.rs +++ b/core/src/commitment.rs @@ -31,17 +31,40 @@ impl BlockCommitment { } } -#[derive(Debug, Default)] +#[derive(Default)] pub struct BlockCommitmentCache { block_commitment: HashMap, total_stake: u64, + bank: Arc, + root: Slot, +} + +impl std::fmt::Debug for BlockCommitmentCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("BlockCommitmentCache") + .field("block_commitment", &self.block_commitment) + .field("total_stake", &self.total_stake) + .field( + "bank", + &format_args!("Bank({{current_slot: {:?}}})", self.bank.slot()), + ) + .field("root", &self.root) + .finish() + } } impl BlockCommitmentCache { - pub fn new(block_commitment: HashMap, total_stake: u64) -> Self { + pub fn new( + block_commitment: HashMap, + total_stake: u64, + bank: Arc, + root: Slot, + ) -> Self { Self { block_commitment, total_stake, + bank, + root, } } @@ -53,6 +76,18 @@ impl BlockCommitmentCache { self.total_stake } + pub fn bank(&self) -> Arc { + self.bank.clone() + } + + pub fn slot(&self) -> Slot { + self.bank.slot() + } + + pub fn root(&self) -> Slot { + self.root + } + pub fn get_block_with_depth_commitment( &self, minimum_depth: usize, @@ -79,12 +114,17 @@ impl BlockCommitmentCache { pub struct CommitmentAggregationData { bank: Arc, + root: Slot, total_staked: u64, } impl CommitmentAggregationData { - pub fn new(bank: Arc, total_staked: u64) -> Self { - Self { bank, total_staked } + pub fn new(bank: Arc, root: Slot, total_staked: u64) -> Self { + Self { + bank, + root, + total_staked, + } } } @@ -146,8 +186,12 @@ impl AggregateCommitmentService { let block_commitment = Self::aggregate_commitment(&ancestors, &aggregation_data.bank); - let mut new_block_commitment = - BlockCommitmentCache::new(block_commitment, aggregation_data.total_staked); + let mut new_block_commitment = BlockCommitmentCache::new( + block_commitment, + aggregation_data.total_staked, + aggregation_data.bank, + aggregation_data.root, + ); let mut w_block_commitment_cache = block_commitment_cache.write().unwrap(); @@ -247,6 +291,7 @@ mod tests { #[test] fn test_get_block_with_depth_commitment() { + let bank = Arc::new(Bank::default()); // Build BlockCommitmentCache with votes at depths 0 and 1 for 2 slots let mut cache0 = BlockCommitment::default(); cache0.increase_confirmation_stake(1, 15); @@ -259,7 +304,7 @@ mod tests { let mut block_commitment = HashMap::new(); block_commitment.entry(0).or_insert(cache0.clone()); block_commitment.entry(1).or_insert(cache1.clone()); - let block_commitment_cache = BlockCommitmentCache::new(block_commitment, 50); + let block_commitment_cache = BlockCommitmentCache::new(block_commitment, 50, bank, 0); // Neither slot has rooted votes assert_eq!( @@ -295,6 +340,7 @@ mod tests { #[test] fn test_get_rooted_block_with_commitment() { + let bank = Arc::new(Bank::default()); // Build BlockCommitmentCache with rooted votes let mut cache0 = BlockCommitment::new([0; MAX_LOCKOUT_HISTORY]); cache0.increase_confirmation_stake(MAX_LOCKOUT_HISTORY, 40); @@ -307,7 +353,7 @@ mod tests { let mut block_commitment = HashMap::new(); block_commitment.entry(0).or_insert(cache0.clone()); block_commitment.entry(1).or_insert(cache1.clone()); - let block_commitment_cache = BlockCommitmentCache::new(block_commitment, 50); + let block_commitment_cache = BlockCommitmentCache::new(block_commitment, 50, bank, 0); // Only slot 0 meets the minimum level of commitment 0.66 at root assert_eq!( diff --git a/core/src/replay_stage.rs b/core/src/replay_stage.rs index f89b1dbde4..501272004a 100644 --- a/core/src/replay_stage.rs +++ b/core/src/replay_stage.rs @@ -762,6 +762,7 @@ impl ReplayStage { Self::update_commitment_cache( bank.clone(), + bank_forks.read().unwrap().root(), progress.get_fork_stats(bank.slot()).unwrap().total_staked, lockouts_sender, ); @@ -792,10 +793,13 @@ impl ReplayStage { fn update_commitment_cache( bank: Arc, + root: Slot, total_staked: u64, lockouts_sender: &Sender, ) { - if let Err(e) = lockouts_sender.send(CommitmentAggregationData::new(bank, total_staked)) { + if let Err(e) = + lockouts_sender.send(CommitmentAggregationData::new(bank, root, total_staked)) + { trace!("lockouts_sender failed: {:?}", e); } } @@ -2350,7 +2354,12 @@ pub(crate) mod tests { bank_forks.write().unwrap().insert(bank1); let arc_bank1 = bank_forks.read().unwrap().get(1).unwrap().clone(); leader_vote(&arc_bank1, &leader_voting_pubkey); - ReplayStage::update_commitment_cache(arc_bank1.clone(), leader_lamports, &lockouts_sender); + ReplayStage::update_commitment_cache( + arc_bank1.clone(), + 0, + leader_lamports, + &lockouts_sender, + ); let bank2 = Bank::new_from_parent(&arc_bank1, &Pubkey::default(), arc_bank1.slot() + 1); let _res = bank2.transfer(10, &genesis_config_info.mint_keypair, &Pubkey::new_rand()); @@ -2361,7 +2370,12 @@ pub(crate) mod tests { bank_forks.write().unwrap().insert(bank2); let arc_bank2 = bank_forks.read().unwrap().get(2).unwrap().clone(); leader_vote(&arc_bank2, &leader_voting_pubkey); - ReplayStage::update_commitment_cache(arc_bank2.clone(), leader_lamports, &lockouts_sender); + ReplayStage::update_commitment_cache( + arc_bank2.clone(), + 0, + leader_lamports, + &lockouts_sender, + ); thread::sleep(Duration::from_millis(200)); let mut expected0 = BlockCommitment::default(); diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 84f2ca4b7e..5c5de70b2d 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -1222,18 +1222,6 @@ pub mod tests { ) -> RpcHandler { let (bank_forks, alice, leader_vote_keypair) = new_bank_forks(); let bank = bank_forks.read().unwrap().working_bank(); - - let commitment_slot0 = BlockCommitment::new([8; MAX_LOCKOUT_HISTORY]); - let commitment_slot1 = BlockCommitment::new([9; MAX_LOCKOUT_HISTORY]); - let mut block_commitment: HashMap = HashMap::new(); - block_commitment - .entry(0) - .or_insert(commitment_slot0.clone()); - block_commitment - .entry(1) - .or_insert(commitment_slot1.clone()); - let block_commitment_cache = - Arc::new(RwLock::new(BlockCommitmentCache::new(block_commitment, 42))); let ledger_path = get_tmp_ledger_path!(); let blockstore = Blockstore::open(&ledger_path).unwrap(); let blockstore = Arc::new(blockstore); @@ -1249,6 +1237,22 @@ pub mod tests { blockstore.clone(), ); + let commitment_slot0 = BlockCommitment::new([8; MAX_LOCKOUT_HISTORY]); + let commitment_slot1 = BlockCommitment::new([9; MAX_LOCKOUT_HISTORY]); + let mut block_commitment: HashMap = HashMap::new(); + block_commitment + .entry(0) + .or_insert(commitment_slot0.clone()); + block_commitment + .entry(1) + .or_insert(commitment_slot1.clone()); + let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new( + block_commitment, + 42, + bank.clone(), + 0, + ))); + // Add timestamp vote to blockstore let vote = Vote { slots: vec![1], @@ -2119,6 +2123,8 @@ pub mod tests { fn test_rpc_processor_get_block_commitment() { let exit = Arc::new(AtomicBool::new(false)); let validator_exit = create_validator_exit(&exit); + let bank_forks = new_bank_forks().0; + let commitment_slot0 = BlockCommitment::new([8; MAX_LOCKOUT_HISTORY]); let commitment_slot1 = BlockCommitment::new([9; MAX_LOCKOUT_HISTORY]); let mut block_commitment: HashMap = HashMap::new(); @@ -2128,8 +2134,12 @@ pub mod tests { block_commitment .entry(1) .or_insert(commitment_slot1.clone()); - let block_commitment_cache = - Arc::new(RwLock::new(BlockCommitmentCache::new(block_commitment, 42))); + let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new( + block_commitment, + 42, + bank_forks.read().unwrap().working_bank(), + 0, + ))); let ledger_path = get_tmp_ledger_path!(); let blockstore = Blockstore::open(&ledger_path).unwrap(); @@ -2137,7 +2147,7 @@ pub mod tests { config.enable_validator_exit = true; let request_processor = JsonRpcRequestProcessor::new( config, - new_bank_forks().0, + bank_forks, block_commitment_cache, Arc::new(blockstore), StorageState::default(),