switch vote program to use slot height instead of tick height, change confirmation computation to use slots

This commit is contained in:
Carl
2019-02-21 00:44:37 -08:00
committed by Greg Fitzgerald
parent 20fffd8abf
commit 9e1c5e1ab0
5 changed files with 46 additions and 27 deletions

View File

@ -27,13 +27,13 @@ pub const MAX_VOTE_HISTORY: usize = 32;
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Vote { pub struct Vote {
// TODO: add signature of the state here as well // TODO: add signature of the state here as well
/// A vote for height tick_height /// A vote for height slot_height
pub tick_height: u64, pub slot_height: u64,
} }
impl Vote { impl Vote {
pub fn new(tick_height: u64) -> Self { pub fn new(slot_height: u64) -> Self {
Self { tick_height } Self { slot_height }
} }
} }

View File

@ -14,11 +14,11 @@ pub struct VoteTransaction {}
impl VoteTransaction { impl VoteTransaction {
pub fn new_vote<T: KeypairUtil>( pub fn new_vote<T: KeypairUtil>(
voting_keypair: &T, voting_keypair: &T,
tick_height: u64, slot_height: u64,
last_id: Hash, last_id: Hash,
fee: u64, fee: u64,
) -> Transaction { ) -> Transaction {
let vote = Vote { tick_height }; let vote = Vote { slot_height };
let instruction = VoteInstruction::Vote(vote); let instruction = VoteInstruction::Vote(vote);
Transaction::new( Transaction::new(
voting_keypair, voting_keypair,
@ -81,12 +81,12 @@ mod tests {
#[test] #[test]
fn test_get_votes() { fn test_get_votes() {
let keypair = Keypair::new(); let keypair = Keypair::new();
let tick_height = 1; let slot_height = 1;
let last_id = Hash::default(); let last_id = Hash::default();
let transaction = VoteTransaction::new_vote(&keypair, tick_height, last_id, 0); let transaction = VoteTransaction::new_vote(&keypair, slot_height, last_id, 0);
assert_eq!( assert_eq!(
VoteTransaction::get_votes(&transaction), VoteTransaction::get_votes(&transaction),
vec![(keypair.pubkey(), Vote::new(tick_height), last_id)] vec![(keypair.pubkey(), Vote::new(slot_height), last_id)]
); );
} }
} }

View File

@ -10,7 +10,7 @@ fn is_active_staker(vote_state: &VoteState, lower_bound: u64, upper_bound: u64)
vote_state vote_state
.votes .votes
.back() .back()
.filter(|vote| vote.tick_height >= lower_bound && vote.tick_height <= upper_bound) .filter(|vote| vote.slot_height >= lower_bound && vote.slot_height <= upper_bound)
.is_some() .is_some()
} }
@ -36,8 +36,8 @@ pub struct ActiveStakers {
} }
impl ActiveStakers { impl ActiveStakers {
pub fn new_with_bounds(bank: &Bank, active_window_tick_length: u64, upper_bound: u64) -> Self { pub fn new_with_bounds(bank: &Bank, active_window_num_slots: u64, upper_bound: u64) -> Self {
let lower_bound = upper_bound.saturating_sub(active_window_tick_length); let lower_bound = upper_bound.saturating_sub(active_window_num_slots);
let mut stakes: Vec<_> = bank let mut stakes: Vec<_> = bank
.vote_states(|vote_state| is_active_staker(vote_state, lower_bound, upper_bound)) .vote_states(|vote_state| is_active_staker(vote_state, lower_bound, upper_bound))
.iter() .iter()
@ -96,9 +96,9 @@ pub mod tests {
bank.process_transaction(&tx).unwrap(); bank.process_transaction(&tx).unwrap();
} }
pub fn push_vote<T: KeypairUtil>(voting_keypair: &T, bank: &Bank, tick_height: u64) { pub fn push_vote<T: KeypairUtil>(voting_keypair: &T, bank: &Bank, slot_height: u64) {
let last_id = bank.last_id(); let last_id = bank.last_id();
let tx = VoteTransaction::new_vote(voting_keypair, tick_height, last_id, 0); let tx = VoteTransaction::new_vote(voting_keypair, slot_height, last_id, 0);
bank.process_transaction(&tx).unwrap(); bank.process_transaction(&tx).unwrap();
} }
@ -107,10 +107,10 @@ pub mod tests {
voting_keypair: &T, voting_keypair: &T,
bank: &Bank, bank: &Bank,
num_tokens: u64, num_tokens: u64,
tick_height: u64, slot_height: u64,
) { ) {
new_vote_account(from_keypair, &voting_keypair.pubkey(), bank, num_tokens); new_vote_account(from_keypair, &voting_keypair.pubkey(), bank, num_tokens);
push_vote(voting_keypair, bank, tick_height); push_vote(voting_keypair, bank, slot_height);
} }
#[test] #[test]
@ -154,8 +154,8 @@ pub mod tests {
.unwrap(); .unwrap();
// Create a vote account and push a vote // Create a vote account and push a vote
let tick_height = start_height + active_window_tick_length + 1; let slot_height = start_height + active_window_tick_length + 1;
new_vote_account_with_vote(&new_keypair, &Keypair::new(), &bank, 1, tick_height); new_vote_account_with_vote(&new_keypair, &Keypair::new(), &bank, 1, slot_height);
} }
new_ids.sort(); new_ids.sort();
@ -273,7 +273,7 @@ pub mod tests {
assert_eq!(result.len(), 0); assert_eq!(result.len(), 0);
} }
// Vote at tick_height 2 // Vote at slot_height 2
push_vote(&voting_keypair, &bank, 2); push_vote(&voting_keypair, &bank, 2);
{ {

View File

@ -59,6 +59,7 @@ impl BankingStage {
max_tick_height, max_tick_height,
}; };
let ticks_per_slot = max_tick_height - bank.tick_height();
let poh_recorder = PohRecorder::new(bank.tick_height(), *last_entry_id); let poh_recorder = PohRecorder::new(bank.tick_height(), *last_entry_id);
// Single thread to generate entries from many banks. // Single thread to generate entries from many banks.
@ -74,8 +75,12 @@ impl BankingStage {
.expect("failed to send leader to poh_service"); .expect("failed to send leader to poh_service");
// Single thread to compute confirmation // Single thread to compute confirmation
let leader_confirmation_service = let leader_confirmation_service = LeaderConfirmationService::new(
LeaderConfirmationService::new(bank.clone(), leader_id, poh_exit.clone()); bank.clone(),
leader_id,
poh_exit.clone(),
ticks_per_slot,
);
// Many banks that process transactions in parallel. // Many banks that process transactions in parallel.
let bank_thread_hdls: Vec<JoinHandle<UnprocessedPackets>> = (0..Self::num_threads()) let bank_thread_hdls: Vec<JoinHandle<UnprocessedPackets>> = (0..Self::num_threads())

View File

@ -30,6 +30,7 @@ impl LeaderConfirmationService {
bank: &Arc<Bank>, bank: &Arc<Bank>,
leader_id: Pubkey, leader_id: Pubkey,
last_valid_validator_timestamp: u64, last_valid_validator_timestamp: u64,
ticks_per_slot: u64,
) -> result::Result<u64, ConfirmationError> { ) -> result::Result<u64, ConfirmationError> {
let mut total_stake = 0; let mut total_stake = 0;
@ -47,7 +48,8 @@ impl LeaderConfirmationService {
vote_state vote_state
.votes .votes
.back() .back()
.map(|vote| (vote.tick_height, validator_stake)) // A vote for a slot is like a vote for the last tick in that slot
.map(|vote| ((vote.slot_height + 1) * ticks_per_slot - 1, validator_stake))
}) })
.collect(); .collect();
@ -78,10 +80,14 @@ impl LeaderConfirmationService {
bank: &Arc<Bank>, bank: &Arc<Bank>,
leader_id: Pubkey, leader_id: Pubkey,
last_valid_validator_timestamp: &mut u64, last_valid_validator_timestamp: &mut u64,
ticks_per_slot: u64,
) { ) {
if let Ok(super_majority_timestamp) = if let Ok(super_majority_timestamp) = Self::get_last_supermajority_timestamp(
Self::get_last_supermajority_timestamp(bank, leader_id, *last_valid_validator_timestamp) bank,
{ leader_id,
*last_valid_validator_timestamp,
ticks_per_slot,
) {
let now = timing::timestamp(); let now = timing::timestamp();
let confirmation_ms = now - super_majority_timestamp; let confirmation_ms = now - super_majority_timestamp;
@ -99,9 +105,14 @@ impl LeaderConfirmationService {
} }
/// Create a new LeaderConfirmationService for computing confirmation. /// Create a new LeaderConfirmationService for computing confirmation.
pub fn new(bank: Arc<Bank>, leader_id: Pubkey, exit: Arc<AtomicBool>) -> Self { pub fn new(
bank: Arc<Bank>,
leader_id: Pubkey,
exit: Arc<AtomicBool>,
ticks_per_slot: u64,
) -> Self {
let thread_hdl = Builder::new() let thread_hdl = Builder::new()
.name("solana-leader-confirmation-stage".to_string()) .name("solana-leader-confirmation-service".to_string())
.spawn(move || { .spawn(move || {
let mut last_valid_validator_timestamp = 0; let mut last_valid_validator_timestamp = 0;
loop { loop {
@ -112,6 +123,7 @@ impl LeaderConfirmationService {
&bank, &bank,
leader_id, leader_id,
&mut last_valid_validator_timestamp, &mut last_valid_validator_timestamp,
ticks_per_slot,
); );
sleep(Duration::from_millis(COMPUTE_CONFIRMATION_MS)); sleep(Duration::from_millis(COMPUTE_CONFIRMATION_MS));
} }
@ -189,6 +201,7 @@ pub mod tests {
&bank, &bank,
genesis_block.bootstrap_leader_id, genesis_block.bootstrap_leader_id,
&mut last_confirmation_time, &mut last_confirmation_time,
1,
); );
// Get another validator to vote, so we now have 2/3 consensus // Get another validator to vote, so we now have 2/3 consensus
@ -200,6 +213,7 @@ pub mod tests {
&bank, &bank,
genesis_block.bootstrap_leader_id, genesis_block.bootstrap_leader_id,
&mut last_confirmation_time, &mut last_confirmation_time,
1,
); );
assert!(last_confirmation_time > 0); assert!(last_confirmation_time > 0);
} }