Add hook for getting vote transactions on replay (#11264)
* Add hook for getting vote transactions on replay Co-authored-by: Carl <carl@solana.com>
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
use crate::{
|
||||
cluster_info::{ClusterInfo, GOSSIP_SLEEP_MILLIS},
|
||||
consensus::PubkeyVotes,
|
||||
crds_value::CrdsValueLabel,
|
||||
optimistic_confirmation_verifier::OptimisticConfirmationVerifier,
|
||||
poh_recorder::PohRecorder,
|
||||
pubkey_references::LockedPubkeyReferences,
|
||||
replay_stage::ReplayVotesReceiver,
|
||||
result::{Error, Result},
|
||||
rpc_subscriptions::RpcSubscriptions,
|
||||
sigverify,
|
||||
@ -17,7 +15,10 @@ use crossbeam_channel::{
|
||||
};
|
||||
use itertools::izip;
|
||||
use log::*;
|
||||
use solana_ledger::blockstore::Blockstore;
|
||||
use solana_ledger::{
|
||||
blockstore::Blockstore,
|
||||
blockstore_processor::{ReplayVotesReceiver, ReplayedVote},
|
||||
};
|
||||
use solana_metrics::inc_new_counter_debug;
|
||||
use solana_perf::packet::{self, Packets};
|
||||
use solana_runtime::{
|
||||
@ -34,9 +35,9 @@ use solana_sdk::{
|
||||
pubkey::Pubkey,
|
||||
transaction::Transaction,
|
||||
};
|
||||
use solana_vote_program::{self, vote_transaction};
|
||||
use solana_vote_program::{self, vote_state::Vote, vote_transaction};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
collections::HashMap,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
{Arc, Mutex, RwLock},
|
||||
@ -417,7 +418,7 @@ impl ClusterInfoVoteListener {
|
||||
|
||||
fn process_votes_loop(
|
||||
exit: Arc<AtomicBool>,
|
||||
vote_txs_receiver: VerifiedVoteTransactionsReceiver,
|
||||
gossip_vote_txs_receiver: VerifiedVoteTransactionsReceiver,
|
||||
vote_tracker: Arc<VoteTracker>,
|
||||
bank_forks: Arc<RwLock<BankForks>>,
|
||||
subscriptions: Arc<RpcSubscriptions>,
|
||||
@ -449,7 +450,7 @@ impl ClusterInfoVoteListener {
|
||||
last_process_root = Instant::now();
|
||||
}
|
||||
let optimistic_confirmed_slots = Self::get_and_process_votes(
|
||||
&vote_txs_receiver,
|
||||
&gossip_vote_txs_receiver,
|
||||
&vote_tracker,
|
||||
&root_bank,
|
||||
&subscriptions,
|
||||
@ -475,7 +476,7 @@ impl ClusterInfoVoteListener {
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn get_and_process_votes_for_tests(
|
||||
vote_txs_receiver: &VerifiedVoteTransactionsReceiver,
|
||||
gossip_vote_txs_receiver: &VerifiedVoteTransactionsReceiver,
|
||||
vote_tracker: &VoteTracker,
|
||||
root_bank: &Bank,
|
||||
subscriptions: &RpcSubscriptions,
|
||||
@ -483,7 +484,7 @@ impl ClusterInfoVoteListener {
|
||||
replay_votes_receiver: &ReplayVotesReceiver,
|
||||
) -> Result<Vec<(Slot, Hash)>> {
|
||||
Self::get_and_process_votes(
|
||||
vote_txs_receiver,
|
||||
gossip_vote_txs_receiver,
|
||||
vote_tracker,
|
||||
root_bank,
|
||||
subscriptions,
|
||||
@ -493,7 +494,7 @@ impl ClusterInfoVoteListener {
|
||||
}
|
||||
|
||||
fn get_and_process_votes(
|
||||
vote_txs_receiver: &VerifiedVoteTransactionsReceiver,
|
||||
gossip_vote_txs_receiver: &VerifiedVoteTransactionsReceiver,
|
||||
vote_tracker: &VoteTracker,
|
||||
root_bank: &Bank,
|
||||
subscriptions: &RpcSubscriptions,
|
||||
@ -501,7 +502,7 @@ impl ClusterInfoVoteListener {
|
||||
replay_votes_receiver: &ReplayVotesReceiver,
|
||||
) -> Result<Vec<(Slot, Hash)>> {
|
||||
let mut sel = Select::new();
|
||||
sel.recv(vote_txs_receiver);
|
||||
sel.recv(gossip_vote_txs_receiver);
|
||||
sel.recv(replay_votes_receiver);
|
||||
let mut remaining_wait_time = 200;
|
||||
loop {
|
||||
@ -517,16 +518,16 @@ impl ClusterInfoVoteListener {
|
||||
|
||||
// Should not early return from this point onwards until `process_votes()`
|
||||
// returns below to avoid missing any potential `optimistic_confirmed_slots`
|
||||
let vote_txs: Vec<_> = vote_txs_receiver.try_iter().flatten().collect();
|
||||
let gossip_vote_txs: Vec<_> = gossip_vote_txs_receiver.try_iter().flatten().collect();
|
||||
let replay_votes: Vec<_> = replay_votes_receiver.try_iter().collect();
|
||||
if !vote_txs.is_empty() || !replay_votes.is_empty() {
|
||||
if !gossip_vote_txs.is_empty() || !replay_votes.is_empty() {
|
||||
return Ok(Self::process_votes(
|
||||
vote_tracker,
|
||||
vote_txs,
|
||||
gossip_vote_txs,
|
||||
replay_votes,
|
||||
root_bank,
|
||||
subscriptions,
|
||||
verified_vote_sender,
|
||||
&replay_votes,
|
||||
));
|
||||
} else {
|
||||
remaining_wait_time = remaining_wait_time
|
||||
@ -536,120 +537,161 @@ impl ClusterInfoVoteListener {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn process_votes(
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn update_new_votes(
|
||||
vote: Vote,
|
||||
vote_pubkey: &Pubkey,
|
||||
vote_tracker: &VoteTracker,
|
||||
vote_txs: Vec<Transaction>,
|
||||
root_bank: &Bank,
|
||||
subscriptions: &RpcSubscriptions,
|
||||
verified_vote_sender: &VerifiedVoteSender,
|
||||
replay_votes: &[Arc<PubkeyVotes>],
|
||||
) -> Vec<(Slot, Hash)> {
|
||||
let mut optimistic_confirmation_counted: HashSet<(Slot, Pubkey)> = HashSet::new();
|
||||
let mut diff: HashMap<Slot, HashMap<Arc<Pubkey>, bool>> = HashMap::new();
|
||||
let mut new_optimistic_confirmed_slots = vec![];
|
||||
let root = root_bank.slot();
|
||||
{
|
||||
for tx in vote_txs {
|
||||
if let Some((vote_pubkey, vote, _)) = vote_transaction::parse_vote_transaction(&tx)
|
||||
{
|
||||
if vote.slots.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let last_vote_slot = vote.slots.last().unwrap();
|
||||
|
||||
// Determine the authorized voter based on the last vote slot. This will
|
||||
// drop votes from authorized voters trying to make votes for slots
|
||||
// earlier than the epoch for which they are authorized
|
||||
let actual_authorized_voter =
|
||||
vote_tracker.get_authorized_voter(&vote_pubkey, *last_vote_slot);
|
||||
|
||||
if actual_authorized_voter.is_none() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Voting without the correct authorized pubkey, dump the vote
|
||||
if !VoteTracker::vote_contains_authorized_voter(
|
||||
&tx,
|
||||
&actual_authorized_voter.unwrap(),
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let root = root_bank.slot();
|
||||
let last_vote_hash = vote.hash;
|
||||
for slot in &vote.slots {
|
||||
// If slot is before the root, or so far ahead we don't have
|
||||
// stake information, then ignore it
|
||||
let epoch = root_bank.epoch_schedule().get_epoch(*slot);
|
||||
let epoch_stakes = root_bank.epoch_stakes(epoch);
|
||||
if *slot <= root || epoch_stakes.is_none() {
|
||||
continue;
|
||||
}
|
||||
let epoch_stakes = epoch_stakes.unwrap();
|
||||
let epoch_vote_accounts = Stakes::vote_accounts(epoch_stakes.stakes());
|
||||
let total_epoch_stake = epoch_stakes.total_stake();
|
||||
|
||||
let unduplicated_pubkey = vote_tracker.keys.get_or_insert(&vote_pubkey);
|
||||
|
||||
// The last vote slot , which is the greatest slot in the stack
|
||||
// of votes in a vote transaction, qualifies for optimistic confirmation.
|
||||
let update_optimistic_confirmation_info = if slot == last_vote_slot {
|
||||
let stake = epoch_vote_accounts
|
||||
.get(&vote_pubkey)
|
||||
.map(|(stake, _)| *stake)
|
||||
.unwrap_or(0);
|
||||
Some((stake, last_vote_hash))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// If this vote for this slot qualifies for optimistic confirmation
|
||||
if let Some((stake, hash)) = update_optimistic_confirmation_info {
|
||||
// Fast track processing of the last slot in a vote transactions
|
||||
// so that notifications for optimistic confirmation can be sent
|
||||
// as soon as possible.
|
||||
if !optimistic_confirmation_counted
|
||||
.contains(&(*slot, *unduplicated_pubkey))
|
||||
&& Self::add_optimistic_confirmation_vote(
|
||||
vote_tracker,
|
||||
*slot,
|
||||
hash,
|
||||
unduplicated_pubkey.clone(),
|
||||
stake,
|
||||
total_epoch_stake,
|
||||
)
|
||||
{
|
||||
optimistic_confirmation_counted
|
||||
.insert((*slot, *unduplicated_pubkey));
|
||||
new_optimistic_confirmed_slots.push((*slot, last_vote_hash));
|
||||
// TODO: Notify subscribers about new optimistic confirmation
|
||||
}
|
||||
}
|
||||
|
||||
diff.entry(*slot)
|
||||
.or_default()
|
||||
.insert(unduplicated_pubkey, true);
|
||||
}
|
||||
|
||||
subscriptions.notify_vote(&vote);
|
||||
let _ = verified_vote_sender.send((vote_pubkey, vote.slots));
|
||||
}
|
||||
}
|
||||
diff: &mut HashMap<Slot, HashMap<Arc<Pubkey>, bool>>,
|
||||
new_optimistic_confirmed_slots: &mut Vec<(Slot, Hash)>,
|
||||
is_gossip_vote: bool,
|
||||
) {
|
||||
if vote.slots.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Process the replay votes
|
||||
for votes in replay_votes {
|
||||
for (pubkey, slot) in votes.iter() {
|
||||
if *slot <= root {
|
||||
continue;
|
||||
}
|
||||
let unduplicated_pubkey = vote_tracker.keys.get_or_insert(pubkey);
|
||||
diff.entry(*slot)
|
||||
.or_default()
|
||||
.entry(unduplicated_pubkey)
|
||||
.or_default();
|
||||
let last_vote_slot = vote.slots.last().unwrap();
|
||||
|
||||
let root = root_bank.slot();
|
||||
let last_vote_hash = vote.hash;
|
||||
let mut is_new_vote = false;
|
||||
for slot in vote.slots.iter().rev() {
|
||||
// If slot is before the root, or so far ahead we don't have
|
||||
// stake information, then ignore it
|
||||
let epoch = root_bank.epoch_schedule().get_epoch(*slot);
|
||||
let epoch_stakes = root_bank.epoch_stakes(epoch);
|
||||
if *slot <= root || epoch_stakes.is_none() {
|
||||
continue;
|
||||
}
|
||||
let epoch_stakes = epoch_stakes.unwrap();
|
||||
let epoch_vote_accounts = Stakes::vote_accounts(epoch_stakes.stakes());
|
||||
let total_epoch_stake = epoch_stakes.total_stake();
|
||||
let unduplicated_pubkey = vote_tracker.keys.get_or_insert(&vote_pubkey);
|
||||
|
||||
// The last vote slot, which is the greatest slot in the stack
|
||||
// of votes in a vote transaction, qualifies for optimistic confirmation.
|
||||
let update_optimistic_confirmation_info = if slot == last_vote_slot {
|
||||
let stake = epoch_vote_accounts
|
||||
.get(&vote_pubkey)
|
||||
.map(|(stake, _)| *stake)
|
||||
.unwrap_or(0);
|
||||
Some((stake, last_vote_hash))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// If this vote for this slot qualifies for optimistic confirmation
|
||||
if let Some((stake, hash)) = update_optimistic_confirmation_info {
|
||||
// Fast track processing of the last slot in a vote transactions
|
||||
// so that notifications for optimistic confirmation can be sent
|
||||
// as soon as possible.
|
||||
let (is_confirmed, is_new) = Self::add_optimistic_confirmation_vote(
|
||||
vote_tracker,
|
||||
*slot,
|
||||
hash,
|
||||
unduplicated_pubkey.clone(),
|
||||
stake,
|
||||
total_epoch_stake,
|
||||
);
|
||||
|
||||
if is_confirmed {
|
||||
new_optimistic_confirmed_slots.push((*slot, last_vote_hash));
|
||||
// TODO: Notify subscribers about new optimistic confirmation
|
||||
}
|
||||
|
||||
if !is_new && !is_gossip_vote {
|
||||
// By now:
|
||||
// 1) The vote must have come from ReplayStage,
|
||||
// 2) We've seen this vote from replay for this hash before
|
||||
// (`add_optimistic_confirmation_vote()` will not set `is_new == true`
|
||||
// for same slot different hash), so short circuit because this vote
|
||||
// has no new information
|
||||
|
||||
// Note gossip votes will always be processed because those should be unique
|
||||
// and we need to update the gossip-only stake in the `VoteTracker`.
|
||||
return;
|
||||
}
|
||||
|
||||
is_new_vote = is_new;
|
||||
}
|
||||
|
||||
diff.entry(*slot)
|
||||
.or_default()
|
||||
.entry(unduplicated_pubkey)
|
||||
.and_modify(|seen_in_gossip_previously| {
|
||||
*seen_in_gossip_previously = *seen_in_gossip_previously || is_gossip_vote
|
||||
})
|
||||
.or_insert(is_gossip_vote);
|
||||
}
|
||||
|
||||
if is_new_vote {
|
||||
subscriptions.notify_vote(&vote);
|
||||
let _ = verified_vote_sender.send((*vote_pubkey, vote.slots));
|
||||
}
|
||||
}
|
||||
|
||||
fn process_votes(
|
||||
vote_tracker: &VoteTracker,
|
||||
gossip_vote_txs: Vec<Transaction>,
|
||||
replayed_votes: Vec<ReplayedVote>,
|
||||
root_bank: &Bank,
|
||||
subscriptions: &RpcSubscriptions,
|
||||
verified_vote_sender: &VerifiedVoteSender,
|
||||
) -> Vec<(Slot, Hash)> {
|
||||
let mut diff: HashMap<Slot, HashMap<Arc<Pubkey>, bool>> = HashMap::new();
|
||||
let mut new_optimistic_confirmed_slots = vec![];
|
||||
|
||||
// Process votes from gossip and ReplayStage
|
||||
for (i, (vote_pubkey, vote, _)) in gossip_vote_txs
|
||||
.iter()
|
||||
.filter_map(|gossip_tx| {
|
||||
vote_transaction::parse_vote_transaction(gossip_tx).filter(
|
||||
|(vote_pubkey, vote, _)| {
|
||||
if vote.slots.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let last_vote_slot = vote.slots.last().unwrap();
|
||||
// Votes from gossip need to be verified as they have not been
|
||||
// verified by the replay pipeline. Determine the authorized voter
|
||||
// based on the last vote slot. This will drop votes from authorized
|
||||
// voters trying to make votes for slots earlier than the epoch for
|
||||
// which they are authorized
|
||||
let actual_authorized_voter =
|
||||
vote_tracker.get_authorized_voter(&vote_pubkey, *last_vote_slot);
|
||||
|
||||
if actual_authorized_voter.is_none() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Voting without the correct authorized pubkey, dump the vote
|
||||
if !VoteTracker::vote_contains_authorized_voter(
|
||||
&gossip_tx,
|
||||
&actual_authorized_voter.unwrap(),
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
},
|
||||
)
|
||||
})
|
||||
.chain(replayed_votes)
|
||||
.enumerate()
|
||||
{
|
||||
Self::update_new_votes(
|
||||
vote,
|
||||
&vote_pubkey,
|
||||
&vote_tracker,
|
||||
root_bank,
|
||||
subscriptions,
|
||||
verified_vote_sender,
|
||||
&mut diff,
|
||||
&mut new_optimistic_confirmed_slots,
|
||||
i < gossip_vote_txs.len(),
|
||||
);
|
||||
}
|
||||
|
||||
// Process all the slots accumulated from replay and gossip.
|
||||
@ -661,13 +703,6 @@ impl ClusterInfoVoteListener {
|
||||
slot_diff.retain(|pubkey, seen_in_gossip_above| {
|
||||
let seen_in_gossip_previously = r_slot_tracker.voted.get(pubkey);
|
||||
let is_new = seen_in_gossip_previously.is_none();
|
||||
if is_new && !*seen_in_gossip_above {
|
||||
// If this vote wasn't seen in gossip, then it must be a
|
||||
// replay vote, and we haven't sent a notification for
|
||||
// those yet
|
||||
let _ = verified_vote_sender.send((**pubkey, vec![slot]));
|
||||
}
|
||||
|
||||
// `is_new_from_gossip` means we observed a vote for this slot
|
||||
// for the first time in gossip
|
||||
let is_new_from_gossip = !seen_in_gossip_previously.cloned().unwrap_or(false)
|
||||
@ -721,7 +756,8 @@ impl ClusterInfoVoteListener {
|
||||
new_optimistic_confirmed_slots
|
||||
}
|
||||
|
||||
// Returns if the slot was optimistically confirmed
|
||||
// Returns if the slot was optimistically confirmed, and whether
|
||||
// the slot was new
|
||||
fn add_optimistic_confirmation_vote(
|
||||
vote_tracker: &VoteTracker,
|
||||
slot: Slot,
|
||||
@ -729,7 +765,7 @@ impl ClusterInfoVoteListener {
|
||||
pubkey: Arc<Pubkey>,
|
||||
stake: u64,
|
||||
total_epoch_stake: u64,
|
||||
) -> bool {
|
||||
) -> (bool, bool) {
|
||||
let slot_tracker = vote_tracker.get_or_insert_slot_tracker(slot);
|
||||
// Insert vote and check for optimistic confirmation
|
||||
let mut w_slot_tracker = slot_tracker.write().unwrap();
|
||||
@ -784,16 +820,18 @@ impl ClusterInfoVoteListener {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::replay_stage::ReplayVotesSender;
|
||||
use solana_ledger::blockstore_processor::ReplayVotesSender;
|
||||
use solana_perf::packet;
|
||||
use solana_runtime::{
|
||||
bank::Bank,
|
||||
commitment::BlockCommitmentCache,
|
||||
genesis_utils::{self, GenesisConfigInfo, ValidatorVoteKeypairs},
|
||||
};
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::signature::Signature;
|
||||
use solana_sdk::signature::{Keypair, Signer};
|
||||
use solana_sdk::{
|
||||
hash::Hash,
|
||||
signature::{Keypair, Signature, Signer},
|
||||
};
|
||||
use solana_vote_program::vote_state::Vote;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
#[test]
|
||||
@ -1048,7 +1086,7 @@ mod tests {
|
||||
gossip_vote_slots: Vec<Slot>,
|
||||
replay_vote_slots: Vec<Slot>,
|
||||
validator_voting_keypairs: &[ValidatorVoteKeypairs],
|
||||
hash: Option<Hash>,
|
||||
switch_proof_hash: Option<Hash>,
|
||||
votes_sender: &VerifiedVoteTransactionsSender,
|
||||
replay_votes_sender: &ReplayVotesSender,
|
||||
) {
|
||||
@ -1062,16 +1100,18 @@ mod tests {
|
||||
node_keypair,
|
||||
vote_keypair,
|
||||
vote_keypair,
|
||||
hash,
|
||||
switch_proof_hash,
|
||||
);
|
||||
votes_sender.send(vec![vote_tx]).unwrap();
|
||||
for vote_slot in &replay_vote_slots {
|
||||
// Send twice, should only expect to be notified once later
|
||||
let replay_vote = Vote::new(replay_vote_slots.clone(), Hash::default());
|
||||
// Send same vote twice, but should only notify once
|
||||
for _ in 0..2 {
|
||||
replay_votes_sender
|
||||
.send(Arc::new(vec![(vote_keypair.pubkey(), *vote_slot)]))
|
||||
.unwrap();
|
||||
replay_votes_sender
|
||||
.send(Arc::new(vec![(vote_keypair.pubkey(), *vote_slot)]))
|
||||
.send((
|
||||
vote_keypair.pubkey(),
|
||||
replay_vote.clone(),
|
||||
switch_proof_hash,
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
@ -1155,7 +1195,7 @@ mod tests {
|
||||
// the `optimistic` vote set.
|
||||
let optimistic_votes_tracker =
|
||||
r_slot_vote_tracker.optimistic_votes_tracker(&Hash::default());
|
||||
if vote_slot == 2 {
|
||||
if vote_slot == 2 || vote_slot == 4 {
|
||||
let optimistic_votes_tracker = optimistic_votes_tracker.unwrap();
|
||||
assert!(optimistic_votes_tracker.voted().contains(&pubkey));
|
||||
assert_eq!(
|
||||
@ -1269,7 +1309,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_test_process_votes3(hash: Option<Hash>) {
|
||||
fn run_test_process_votes3(switch_proof_hash: Option<Hash>) {
|
||||
let (votes_sender, votes_receiver) = unbounded();
|
||||
let (verified_vote_sender, _verified_vote_receiver) = unbounded();
|
||||
let (replay_votes_sender, replay_votes_receiver) = unbounded();
|
||||
@ -1288,6 +1328,7 @@ mod tests {
|
||||
vec![2],
|
||||
vec![0, 1, 2],
|
||||
vec![1, 0, 2],
|
||||
vec![0, 1, 2, 0, 1, 2],
|
||||
];
|
||||
for events in ordered_events {
|
||||
let (vote_tracker, bank, validator_voting_keypairs, subscriptions) = setup();
|
||||
@ -1303,13 +1344,17 @@ mod tests {
|
||||
node_keypair,
|
||||
vote_keypair,
|
||||
vote_keypair,
|
||||
hash,
|
||||
switch_proof_hash,
|
||||
);
|
||||
votes_sender.send(vec![vote_tx.clone()]).unwrap();
|
||||
}
|
||||
if e == 1 || e == 2 {
|
||||
replay_votes_sender
|
||||
.send(Arc::new(vec![(vote_keypair.pubkey(), vote_slot)]))
|
||||
.send((
|
||||
vote_keypair.pubkey(),
|
||||
Vote::new(vec![vote_slot], Hash::default()),
|
||||
switch_proof_hash,
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
let _ = ClusterInfoVoteListener::get_and_process_votes(
|
||||
@ -1403,9 +1448,6 @@ mod tests {
|
||||
// SlotVoteTracker.voted, one in SlotVoteTracker.updates, one in
|
||||
// SlotVoteTracker.optimistic_votes_tracker
|
||||
let ref_count_per_vote = 3;
|
||||
// Replay votes don't get added to `SlotVoteTracker.optimistic_votes_tracker`,
|
||||
// so there's one less
|
||||
let ref_count_per_replay_vote = ref_count_per_vote - 1;
|
||||
let ref_count_per_new_key = 1;
|
||||
|
||||
// Create some voters at genesis
|
||||
@ -1448,14 +1490,15 @@ mod tests {
|
||||
ClusterInfoVoteListener::process_votes(
|
||||
&vote_tracker,
|
||||
vote_tx,
|
||||
// Add gossip vote for same slot, should not affect outcome
|
||||
vec![(
|
||||
validator0_keypairs.vote_keypair.pubkey(),
|
||||
Vote::new(vec![voted_slot], Hash::default()),
|
||||
None,
|
||||
)],
|
||||
&bank,
|
||||
&subscriptions,
|
||||
&verified_vote_sender,
|
||||
// Add vote for same slot, should not affect outcome
|
||||
&[Arc::new(vec![(
|
||||
validator0_keypairs.vote_keypair.pubkey(),
|
||||
voted_slot,
|
||||
)])],
|
||||
);
|
||||
let ref_count = Arc::strong_count(
|
||||
&vote_tracker
|
||||
@ -1517,13 +1560,14 @@ mod tests {
|
||||
ClusterInfoVoteListener::process_votes(
|
||||
&vote_tracker,
|
||||
vote_txs,
|
||||
vec![(
|
||||
validator_keypairs[1].vote_keypair.pubkey(),
|
||||
Vote::new(vec![first_slot_in_new_epoch], Hash::default()),
|
||||
None,
|
||||
)],
|
||||
&new_root_bank,
|
||||
&subscriptions,
|
||||
&verified_vote_sender,
|
||||
&[Arc::new(vec![(
|
||||
validator_keypairs[1].vote_keypair.pubkey(),
|
||||
first_slot_in_new_epoch,
|
||||
)])],
|
||||
);
|
||||
|
||||
// Check new replay vote pubkey first
|
||||
@ -1540,7 +1584,7 @@ mod tests {
|
||||
// `ref_count_per_optimistic_vote + ref_count_per_new_key`.
|
||||
// +ref_count_per_new_key for the new pubkey in `vote_tracker.keys` and
|
||||
// +ref_count_per_optimistic_vote for the one new vote
|
||||
assert_eq!(ref_count, ref_count_per_replay_vote + ref_count_per_new_key);
|
||||
assert_eq!(ref_count, ref_count_per_vote + ref_count_per_new_key);
|
||||
|
||||
// Check the existing pubkey
|
||||
let ref_count = Arc::strong_count(
|
||||
|
@ -679,7 +679,6 @@ pub mod test {
|
||||
progress_map::ForkProgress,
|
||||
replay_stage::{HeaviestForkFailures, ReplayStage},
|
||||
};
|
||||
use crossbeam_channel::unbounded;
|
||||
use solana_runtime::{
|
||||
bank::Bank,
|
||||
bank_forks::BankForks,
|
||||
@ -795,7 +794,6 @@ pub mod test {
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let (replay_slot_sender, _replay_slot_receiver) = unbounded();
|
||||
let _ = ReplayStage::compute_bank_stats(
|
||||
&my_pubkey,
|
||||
&ancestors,
|
||||
@ -808,7 +806,6 @@ pub mod test {
|
||||
&mut PubkeyReferences::default(),
|
||||
&mut self.heaviest_subtree_fork_choice,
|
||||
&mut BankWeightForkChoice::default(),
|
||||
&replay_slot_sender,
|
||||
);
|
||||
|
||||
let vote_bank = self
|
||||
|
@ -7,7 +7,7 @@ use crate::{
|
||||
cluster_info_vote_listener::VoteTracker,
|
||||
cluster_slots::ClusterSlots,
|
||||
commitment_service::{AggregateCommitmentService, CommitmentAggregationData},
|
||||
consensus::{ComputedBankState, PubkeyVotes, Stake, SwitchForkDecision, Tower, VotedStakes},
|
||||
consensus::{ComputedBankState, Stake, SwitchForkDecision, Tower, VotedStakes},
|
||||
fork_choice::{ForkChoice, SelectVoteAndResetForkResult},
|
||||
heaviest_subtree_fork_choice::HeaviestSubtreeForkChoice,
|
||||
poh_recorder::{PohRecorder, GRACE_TICKS_FACTOR, MAX_GRACE_SLOTS},
|
||||
@ -18,11 +18,12 @@ use crate::{
|
||||
rewards_recorder_service::RewardsRecorderSender,
|
||||
rpc_subscriptions::RpcSubscriptions,
|
||||
};
|
||||
use crossbeam_channel::{Receiver as CrossbeamReceiver, Sender as CrossbeamSender};
|
||||
use solana_ledger::{
|
||||
block_error::BlockError,
|
||||
blockstore::Blockstore,
|
||||
blockstore_processor::{self, BlockstoreProcessorError, TransactionStatusSender},
|
||||
blockstore_processor::{
|
||||
self, BlockstoreProcessorError, ReplayVotesSender, TransactionStatusSender,
|
||||
},
|
||||
entry::VerifyRecyclers,
|
||||
leader_schedule_cache::LeaderScheduleCache,
|
||||
};
|
||||
@ -62,9 +63,6 @@ pub const MAX_ENTRY_RECV_PER_ITER: usize = 512;
|
||||
pub const SUPERMINORITY_THRESHOLD: f64 = 1f64 / 3f64;
|
||||
pub const MAX_UNCONFIRMED_SLOTS: usize = 5;
|
||||
|
||||
pub type ReplayVotesSender = CrossbeamSender<Arc<PubkeyVotes>>;
|
||||
pub type ReplayVotesReceiver = CrossbeamReceiver<Arc<PubkeyVotes>>;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub(crate) enum HeaviestForkFailures {
|
||||
LockedOut(u64),
|
||||
@ -346,6 +344,7 @@ impl ReplayStage {
|
||||
&verify_recyclers,
|
||||
&mut heaviest_subtree_fork_choice,
|
||||
&subscriptions,
|
||||
&replay_votes_sender,
|
||||
);
|
||||
replay_active_banks_time.stop();
|
||||
Self::report_memory(&allocated, "replay_active_banks", start);
|
||||
@ -392,7 +391,6 @@ impl ReplayStage {
|
||||
&mut all_pubkeys,
|
||||
&mut heaviest_subtree_fork_choice,
|
||||
&mut bank_weight_fork_choice,
|
||||
&replay_votes_sender,
|
||||
);
|
||||
compute_bank_stats_time.stop();
|
||||
|
||||
@ -942,6 +940,7 @@ impl ReplayStage {
|
||||
blockstore: &Blockstore,
|
||||
bank_progress: &mut ForkProgress,
|
||||
transaction_status_sender: Option<TransactionStatusSender>,
|
||||
replay_votes_sender: &ReplayVotesSender,
|
||||
verify_recyclers: &VerifyRecyclers,
|
||||
) -> result::Result<usize, BlockstoreProcessorError> {
|
||||
let tx_count_before = bank_progress.replay_progress.num_txs;
|
||||
@ -952,6 +951,7 @@ impl ReplayStage {
|
||||
&mut bank_progress.replay_progress,
|
||||
false,
|
||||
transaction_status_sender,
|
||||
Some(replay_votes_sender),
|
||||
None,
|
||||
verify_recyclers,
|
||||
);
|
||||
@ -1203,6 +1203,7 @@ impl ReplayStage {
|
||||
);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn replay_active_banks(
|
||||
blockstore: &Arc<Blockstore>,
|
||||
bank_forks: &Arc<RwLock<BankForks>>,
|
||||
@ -1213,6 +1214,7 @@ impl ReplayStage {
|
||||
verify_recyclers: &VerifyRecyclers,
|
||||
heaviest_subtree_fork_choice: &mut HeaviestSubtreeForkChoice,
|
||||
subscriptions: &Arc<RpcSubscriptions>,
|
||||
replay_votes_sender: &ReplayVotesSender,
|
||||
) -> bool {
|
||||
let mut did_complete_bank = false;
|
||||
let mut tx_count = 0;
|
||||
@ -1258,6 +1260,7 @@ impl ReplayStage {
|
||||
&blockstore,
|
||||
bank_progress,
|
||||
transaction_status_sender.clone(),
|
||||
replay_votes_sender,
|
||||
verify_recyclers,
|
||||
);
|
||||
match replay_result {
|
||||
@ -1309,7 +1312,6 @@ impl ReplayStage {
|
||||
all_pubkeys: &mut PubkeyReferences,
|
||||
heaviest_subtree_fork_choice: &mut dyn ForkChoice,
|
||||
bank_weight_fork_choice: &mut dyn ForkChoice,
|
||||
replay_votes_sender: &ReplayVotesSender,
|
||||
) -> Vec<Slot> {
|
||||
frozen_banks.sort_by_key(|bank| bank.slot());
|
||||
let mut new_stats = vec![];
|
||||
@ -1333,7 +1335,6 @@ impl ReplayStage {
|
||||
);
|
||||
// Notify any listeners of the votes found in this newly computed
|
||||
// bank
|
||||
let _ = replay_votes_sender.send(computed_bank_state.pubkey_votes.clone());
|
||||
heaviest_subtree_fork_choice.compute_bank_stats(
|
||||
&bank,
|
||||
tower,
|
||||
@ -2414,6 +2415,7 @@ pub(crate) mod tests {
|
||||
F: Fn(&Keypair, Arc<Bank>) -> Vec<Shred>,
|
||||
{
|
||||
let ledger_path = get_tmp_ledger_path!();
|
||||
let (replay_votes_sender, _replay_votes_receiver) = unbounded();
|
||||
let res = {
|
||||
let blockstore = Arc::new(
|
||||
Blockstore::open(&ledger_path)
|
||||
@ -2438,6 +2440,7 @@ pub(crate) mod tests {
|
||||
&blockstore,
|
||||
&mut bank0_progress,
|
||||
None,
|
||||
&replay_votes_sender,
|
||||
&VerifyRecyclers::default(),
|
||||
);
|
||||
|
||||
@ -2601,6 +2604,7 @@ pub(crate) mod tests {
|
||||
blockstore.set_roots(&[slot]).unwrap();
|
||||
|
||||
let (transaction_status_sender, transaction_status_receiver) = unbounded();
|
||||
let (replay_votes_sender, _replay_votes_receiver) = unbounded();
|
||||
let transaction_status_service = TransactionStatusService::new(
|
||||
transaction_status_receiver,
|
||||
blockstore,
|
||||
@ -2614,6 +2618,7 @@ pub(crate) mod tests {
|
||||
&entries,
|
||||
true,
|
||||
Some(transaction_status_sender),
|
||||
Some(&replay_votes_sender),
|
||||
);
|
||||
|
||||
transaction_status_service.join().unwrap();
|
||||
@ -2724,7 +2729,6 @@ pub(crate) mod tests {
|
||||
.cloned()
|
||||
.collect();
|
||||
let tower = Tower::new_for_tests(0, 0.67);
|
||||
let (replay_votes_sender, replay_votes_receiver) = unbounded();
|
||||
let newly_computed = ReplayStage::compute_bank_stats(
|
||||
&node_pubkey,
|
||||
&ancestors,
|
||||
@ -2737,11 +2741,9 @@ pub(crate) mod tests {
|
||||
&mut PubkeyReferences::default(),
|
||||
&mut heaviest_subtree_fork_choice,
|
||||
&mut BankWeightForkChoice::default(),
|
||||
&replay_votes_sender,
|
||||
);
|
||||
|
||||
// bank 0 has no votes, should not send any votes on the channel
|
||||
assert_eq!(replay_votes_receiver.try_recv().unwrap(), Arc::new(vec![]));
|
||||
assert_eq!(newly_computed, vec![0]);
|
||||
|
||||
// The only vote is in bank 1, and bank_forks does not currently contain
|
||||
@ -2785,15 +2787,9 @@ pub(crate) mod tests {
|
||||
&mut PubkeyReferences::default(),
|
||||
&mut heaviest_subtree_fork_choice,
|
||||
&mut BankWeightForkChoice::default(),
|
||||
&replay_votes_sender,
|
||||
);
|
||||
|
||||
// Bank 1 had one vote, ensure that `compute_bank_stats` notifies listeners
|
||||
// via `replay_votes_receiver`.
|
||||
assert_eq!(
|
||||
replay_votes_receiver.try_recv().unwrap(),
|
||||
Arc::new(vec![(my_keypairs.vote_keypair.pubkey(), 0)])
|
||||
);
|
||||
// Bank 1 had one vote
|
||||
assert_eq!(newly_computed, vec![1]);
|
||||
{
|
||||
let fork_progress = progress.get(&1).unwrap();
|
||||
@ -2827,10 +2823,8 @@ pub(crate) mod tests {
|
||||
&mut PubkeyReferences::default(),
|
||||
&mut heaviest_subtree_fork_choice,
|
||||
&mut BankWeightForkChoice::default(),
|
||||
&replay_votes_sender,
|
||||
);
|
||||
// No new stats should have been computed
|
||||
assert!(replay_votes_receiver.try_iter().next().is_none());
|
||||
assert!(newly_computed.is_empty());
|
||||
}
|
||||
|
||||
@ -2855,7 +2849,6 @@ pub(crate) mod tests {
|
||||
.collect();
|
||||
|
||||
let ancestors = vote_simulator.bank_forks.read().unwrap().ancestors();
|
||||
let (replay_votes_sender, _replay_votes_receiver) = unbounded();
|
||||
ReplayStage::compute_bank_stats(
|
||||
&node_pubkey,
|
||||
&ancestors,
|
||||
@ -2868,7 +2861,6 @@ pub(crate) mod tests {
|
||||
&mut PubkeyReferences::default(),
|
||||
&mut heaviest_subtree_fork_choice,
|
||||
&mut BankWeightForkChoice::default(),
|
||||
&replay_votes_sender,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
@ -2931,7 +2923,6 @@ pub(crate) mod tests {
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let (replay_votes_sender, _replay_votes_receiver) = unbounded();
|
||||
ReplayStage::compute_bank_stats(
|
||||
&node_pubkey,
|
||||
&vote_simulator.bank_forks.read().unwrap().ancestors(),
|
||||
@ -2944,7 +2935,6 @@ pub(crate) mod tests {
|
||||
&mut PubkeyReferences::default(),
|
||||
&mut vote_simulator.heaviest_subtree_fork_choice,
|
||||
&mut BankWeightForkChoice::default(),
|
||||
&replay_votes_sender,
|
||||
);
|
||||
|
||||
frozen_banks.sort_by_key(|bank| bank.slot());
|
||||
|
@ -8,13 +8,15 @@ use crate::{
|
||||
cluster_info_vote_listener::{ClusterInfoVoteListener, VerifiedVoteSender, VoteTracker},
|
||||
fetch_stage::FetchStage,
|
||||
poh_recorder::{PohRecorder, WorkingBankEntry},
|
||||
replay_stage::ReplayVotesReceiver,
|
||||
rpc_subscriptions::RpcSubscriptions,
|
||||
sigverify::TransactionSigVerifier,
|
||||
sigverify_stage::SigVerifyStage,
|
||||
};
|
||||
use crossbeam_channel::unbounded;
|
||||
use solana_ledger::{blockstore::Blockstore, blockstore_processor::TransactionStatusSender};
|
||||
use solana_ledger::{
|
||||
blockstore::Blockstore,
|
||||
blockstore_processor::{ReplayVotesReceiver, TransactionStatusSender},
|
||||
};
|
||||
use solana_runtime::bank_forks::BankForks;
|
||||
use std::{
|
||||
net::UdpSocket,
|
||||
|
@ -10,7 +10,7 @@ use crate::{
|
||||
cluster_slots::ClusterSlots,
|
||||
ledger_cleanup_service::LedgerCleanupService,
|
||||
poh_recorder::PohRecorder,
|
||||
replay_stage::{ReplayStage, ReplayStageConfig, ReplayVotesSender},
|
||||
replay_stage::{ReplayStage, ReplayStageConfig},
|
||||
retransmit_stage::RetransmitStage,
|
||||
rewards_recorder_service::RewardsRecorderSender,
|
||||
rpc_subscriptions::RpcSubscriptions,
|
||||
@ -21,7 +21,7 @@ use crate::{
|
||||
use crossbeam_channel::unbounded;
|
||||
use solana_ledger::{
|
||||
blockstore::{Blockstore, CompletedSlotsReceiver},
|
||||
blockstore_processor::TransactionStatusSender,
|
||||
blockstore_processor::{ReplayVotesSender, TransactionStatusSender},
|
||||
leader_schedule_cache::LeaderScheduleCache,
|
||||
};
|
||||
use solana_runtime::{
|
||||
|
@ -27,7 +27,7 @@ use solana_ledger::{
|
||||
bank_forks_utils,
|
||||
blockstore::{Blockstore, CompletedSlotsReceiver, PurgeType},
|
||||
blockstore_db::BlockstoreRecoveryMode,
|
||||
blockstore_processor::{self, TransactionStatusSender},
|
||||
blockstore_processor::{self, ReplayVotesSender, TransactionStatusSender},
|
||||
create_new_tmp_ledger,
|
||||
leader_schedule::FixedSchedule,
|
||||
leader_schedule_cache::LeaderScheduleCache,
|
||||
@ -223,6 +223,7 @@ impl Validator {
|
||||
validator_exit.register_exit(Box::new(move || exit_.store(true, Ordering::Relaxed)));
|
||||
let validator_exit = Arc::new(RwLock::new(Some(validator_exit)));
|
||||
|
||||
let (replay_votes_sender, replay_votes_receiver) = unbounded();
|
||||
let (
|
||||
genesis_config,
|
||||
bank_forks,
|
||||
@ -237,7 +238,7 @@ impl Validator {
|
||||
rewards_recorder_sender,
|
||||
rewards_recorder_service,
|
||||
},
|
||||
) = new_banks_from_ledger(config, ledger_path, poh_verify, &exit);
|
||||
) = new_banks_from_ledger(config, ledger_path, poh_verify, &exit, &replay_votes_sender);
|
||||
|
||||
let leader_schedule_cache = Arc::new(leader_schedule_cache);
|
||||
let bank = bank_forks.working_bank();
|
||||
@ -407,7 +408,6 @@ impl Validator {
|
||||
|
||||
let (retransmit_slots_sender, retransmit_slots_receiver) = unbounded();
|
||||
let (verified_vote_sender, verified_vote_receiver) = unbounded();
|
||||
let (replay_votes_sender, replay_votes_receiver) = unbounded();
|
||||
let tvu = Tvu::new(
|
||||
vote_account,
|
||||
authorized_voter_keypairs,
|
||||
@ -574,6 +574,7 @@ fn new_banks_from_ledger(
|
||||
ledger_path: &Path,
|
||||
poh_verify: bool,
|
||||
exit: &Arc<AtomicBool>,
|
||||
replay_votes_sender: &ReplayVotesSender,
|
||||
) -> (
|
||||
GenesisConfig,
|
||||
BankForks,
|
||||
@ -635,6 +636,7 @@ fn new_banks_from_ledger(
|
||||
transaction_history_services
|
||||
.transaction_status_sender
|
||||
.clone(),
|
||||
Some(&replay_votes_sender),
|
||||
)
|
||||
.unwrap_or_else(|err| {
|
||||
error!("Failed to load ledger: {:?}", err);
|
||||
|
@ -9,23 +9,29 @@ pub struct VoteStakeTracker {
|
||||
}
|
||||
|
||||
impl VoteStakeTracker {
|
||||
// Returns true if the stake that has voted has just crosssed the supermajority
|
||||
// Returns tuple (is_confirmed, is_new) where
|
||||
// `is_confirmed` is true if the stake that has voted has just crosssed the supermajority
|
||||
// of stake
|
||||
// `is_new` is true if the vote has not been seen before
|
||||
pub fn add_vote_pubkey(
|
||||
&mut self,
|
||||
vote_pubkey: Arc<Pubkey>,
|
||||
stake: u64,
|
||||
total_epoch_stake: u64,
|
||||
) -> bool {
|
||||
if !self.voted.contains(&vote_pubkey) {
|
||||
) -> (bool, bool) {
|
||||
let is_new = !self.voted.contains(&vote_pubkey);
|
||||
if is_new {
|
||||
self.voted.insert(vote_pubkey);
|
||||
let ratio_before = self.stake as f64 / total_epoch_stake as f64;
|
||||
self.stake += stake;
|
||||
let ratio_now = self.stake as f64 / total_epoch_stake as f64;
|
||||
|
||||
ratio_before <= VOTE_THRESHOLD_SIZE && ratio_now > VOTE_THRESHOLD_SIZE
|
||||
(
|
||||
ratio_before <= VOTE_THRESHOLD_SIZE && ratio_now > VOTE_THRESHOLD_SIZE,
|
||||
is_new,
|
||||
)
|
||||
} else {
|
||||
false
|
||||
(false, is_new)
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,21 +54,26 @@ mod test {
|
||||
let mut vote_stake_tracker = VoteStakeTracker::default();
|
||||
for i in 0..10 {
|
||||
let pubkey = Arc::new(Pubkey::new_rand());
|
||||
let res = vote_stake_tracker.add_vote_pubkey(pubkey.clone(), 1, total_epoch_stake);
|
||||
let (is_confirmed, is_new) =
|
||||
vote_stake_tracker.add_vote_pubkey(pubkey.clone(), 1, total_epoch_stake);
|
||||
let stake = vote_stake_tracker.stake();
|
||||
vote_stake_tracker.add_vote_pubkey(pubkey.clone(), 1, total_epoch_stake);
|
||||
let (is_confirmed2, is_new2) =
|
||||
vote_stake_tracker.add_vote_pubkey(pubkey.clone(), 1, total_epoch_stake);
|
||||
let stake2 = vote_stake_tracker.stake();
|
||||
|
||||
// Stake should not change from adding same pubkey twice
|
||||
assert_eq!(stake, stake2);
|
||||
assert!(!is_confirmed2);
|
||||
assert!(!is_new2);
|
||||
|
||||
// at i == 7, the voted stake is 70%, which is the first time crossing
|
||||
// the supermajority threshold
|
||||
if i == 6 {
|
||||
assert!(res);
|
||||
assert!(is_confirmed);
|
||||
} else {
|
||||
assert!(!res);
|
||||
assert!(!is_confirmed);
|
||||
}
|
||||
assert!(is_new);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user