Add CommitmentSlots::new_from_slot() (#11600)

This commit is contained in:
Greg Fitzgerald 2020-08-12 21:51:15 -06:00 committed by GitHub
parent 6c887c2b87
commit 574c356863
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 85 deletions

View File

@ -88,12 +88,7 @@ impl BanksServer {
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new( let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new(
HashMap::default(), HashMap::default(),
0, 0,
CommitmentSlots { CommitmentSlots::new_from_slot(slot),
slot,
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
))); )));
Builder::new() Builder::new()
.name("solana-bank-forks-client".to_string()) .name("solana-bank-forks-client".to_string())

View File

@ -218,12 +218,7 @@ impl JsonRpcRequestProcessor {
block_commitment_cache: Arc::new(RwLock::new(BlockCommitmentCache::new( block_commitment_cache: Arc::new(RwLock::new(BlockCommitmentCache::new(
HashMap::new(), HashMap::new(),
0, 0,
CommitmentSlots { CommitmentSlots::new_from_slot(bank.slot()),
slot: bank.slot(),
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
))), ))),
blockstore, blockstore,
validator_exit: create_validator_exit(&exit), validator_exit: create_validator_exit(&exit),
@ -2547,12 +2542,7 @@ pub mod tests {
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new( let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new(
block_commitment, block_commitment,
10, 10,
CommitmentSlots { CommitmentSlots::new_from_slot(bank.slot()),
slot: bank.slot(),
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
))); )));
// Add timestamp vote to blockstore // Add timestamp vote to blockstore
@ -4153,12 +4143,7 @@ pub mod tests {
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new( let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::new(
block_commitment, block_commitment,
42, 42,
CommitmentSlots { CommitmentSlots::new_from_slot(bank_forks.read().unwrap().highest_slot()),
slot: bank_forks.read().unwrap().highest_slot(),
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
))); )));
let mut config = JsonRpcConfig::default(); let mut config = JsonRpcConfig::default();
@ -4502,12 +4487,7 @@ pub mod tests {
let mut new_block_commitment = BlockCommitmentCache::new( let mut new_block_commitment = BlockCommitmentCache::new(
HashMap::new(), HashMap::new(),
0, 0,
CommitmentSlots { CommitmentSlots::new_from_slot(bank_forks.read().unwrap().highest_slot()),
root: 0,
slot: bank_forks.read().unwrap().highest_slot(),
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
); );
let mut w_block_commitment_cache = block_commitment_cache.write().unwrap(); let mut w_block_commitment_cache = block_commitment_cache.write().unwrap();
std::mem::swap(&mut *w_block_commitment_cache, &mut new_block_commitment); std::mem::swap(&mut *w_block_commitment_cache, &mut new_block_commitment);
@ -4707,9 +4687,8 @@ pub mod tests {
50, 50,
CommitmentSlots { CommitmentSlots {
slot: bank.slot(), slot: bank.slot(),
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root, highest_confirmed_root,
..CommitmentSlots::default()
}, },
); );

View File

@ -1228,9 +1228,7 @@ pub(crate) mod tests {
10, 10,
CommitmentSlots { CommitmentSlots {
slot: bank1.slot(), slot: bank1.slot(),
root: 0, ..CommitmentSlots::default()
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
}, },
); );

View File

@ -206,6 +206,15 @@ pub struct CommitmentSlots {
pub highest_confirmed_root: Slot, pub highest_confirmed_root: Slot,
} }
impl CommitmentSlots {
pub fn new_from_slot(slot: Slot) -> Self {
Self {
slot,
..Self::default()
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -275,16 +284,9 @@ mod tests {
block_commitment.entry(1).or_insert_with(|| cache0.clone()); // Slot 1, conf 2 block_commitment.entry(1).or_insert_with(|| cache0.clone()); // Slot 1, conf 2
block_commitment.entry(2).or_insert_with(|| cache1.clone()); // Slot 2, conf 1 block_commitment.entry(2).or_insert_with(|| cache1.clone()); // Slot 2, conf 1
block_commitment.entry(3).or_insert_with(|| cache2.clone()); // Slot 3, conf 0 block_commitment.entry(3).or_insert_with(|| cache2.clone()); // Slot 3, conf 0
let block_commitment_cache = BlockCommitmentCache::new( let commitment_slots = CommitmentSlots::new_from_slot(bank_slot_5);
block_commitment, let block_commitment_cache =
total_stake, BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
CommitmentSlots {
slot: bank_slot_5,
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
);
assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 2); assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 2);
@ -293,16 +295,8 @@ mod tests {
block_commitment.entry(1).or_insert_with(|| cache1.clone()); // Slot 1, conf 1 block_commitment.entry(1).or_insert_with(|| cache1.clone()); // Slot 1, conf 1
block_commitment.entry(2).or_insert_with(|| cache1.clone()); // Slot 2, conf 1 block_commitment.entry(2).or_insert_with(|| cache1.clone()); // Slot 2, conf 1
block_commitment.entry(3).or_insert_with(|| cache2.clone()); // Slot 3, conf 0 block_commitment.entry(3).or_insert_with(|| cache2.clone()); // Slot 3, conf 0
let block_commitment_cache = BlockCommitmentCache::new( let block_commitment_cache =
block_commitment, BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
total_stake,
CommitmentSlots {
slot: bank_slot_5,
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
);
assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 2); assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 2);
@ -311,16 +305,8 @@ mod tests {
block_commitment.entry(1).or_insert_with(|| cache1.clone()); // Slot 1, conf 1 block_commitment.entry(1).or_insert_with(|| cache1.clone()); // Slot 1, conf 1
block_commitment.entry(3).or_insert(cache1); // Slot 3, conf 1 block_commitment.entry(3).or_insert(cache1); // Slot 3, conf 1
block_commitment.entry(5).or_insert_with(|| cache2.clone()); // Slot 5, conf 0 block_commitment.entry(5).or_insert_with(|| cache2.clone()); // Slot 5, conf 0
let block_commitment_cache = BlockCommitmentCache::new( let block_commitment_cache =
block_commitment, BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
total_stake,
CommitmentSlots {
slot: bank_slot_5,
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
);
assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 3); assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 3);
@ -329,16 +315,8 @@ mod tests {
block_commitment.entry(1).or_insert(cache0); // Slot 1, conf 2 block_commitment.entry(1).or_insert(cache0); // Slot 1, conf 2
block_commitment.entry(2).or_insert_with(|| cache2.clone()); // Slot 2, conf 0 block_commitment.entry(2).or_insert_with(|| cache2.clone()); // Slot 2, conf 0
block_commitment.entry(3).or_insert_with(|| cache2.clone()); // Slot 3, conf 0 block_commitment.entry(3).or_insert_with(|| cache2.clone()); // Slot 3, conf 0
let block_commitment_cache = BlockCommitmentCache::new( let block_commitment_cache =
block_commitment, BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
total_stake,
CommitmentSlots {
slot: bank_slot_5,
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
);
assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 1); assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 1);
@ -347,16 +325,8 @@ mod tests {
block_commitment.entry(1).or_insert_with(|| cache2.clone()); // Slot 1, conf 0 block_commitment.entry(1).or_insert_with(|| cache2.clone()); // Slot 1, conf 0
block_commitment.entry(2).or_insert_with(|| cache2.clone()); // Slot 2, conf 0 block_commitment.entry(2).or_insert_with(|| cache2.clone()); // Slot 2, conf 0
block_commitment.entry(3).or_insert(cache2); // Slot 3, conf 0 block_commitment.entry(3).or_insert(cache2); // Slot 3, conf 0
let block_commitment_cache = BlockCommitmentCache::new( let block_commitment_cache =
block_commitment, BlockCommitmentCache::new(block_commitment, total_stake, commitment_slots);
total_stake,
CommitmentSlots {
slot: bank_slot_5,
root: 0,
highest_confirmed_slot: 0,
highest_confirmed_root: 0,
},
);
assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 0); assert_eq!(block_commitment_cache.calculate_highest_confirmed_slot(), 0);
} }