solana-validator now supports multiple --authorized-voter arguments (#9174)
* Use Epoch type * Vote account's authorized voter is now supported without a validator restart
This commit is contained in:
@ -1,26 +1,26 @@
|
||||
use log::*;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::{clock::Epoch, pubkey::Pubkey};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||
pub struct AuthorizedVoters {
|
||||
authorized_voters: BTreeMap<u64, Pubkey>,
|
||||
authorized_voters: BTreeMap<Epoch, Pubkey>,
|
||||
}
|
||||
|
||||
impl AuthorizedVoters {
|
||||
pub fn new(epoch: u64, pubkey: Pubkey) -> Self {
|
||||
pub fn new(epoch: Epoch, pubkey: Pubkey) -> Self {
|
||||
let mut authorized_voters = BTreeMap::new();
|
||||
authorized_voters.insert(epoch, pubkey);
|
||||
Self { authorized_voters }
|
||||
}
|
||||
|
||||
pub fn get_authorized_voter(&self, epoch: u64) -> Option<Pubkey> {
|
||||
pub fn get_authorized_voter(&self, epoch: Epoch) -> Option<Pubkey> {
|
||||
self.get_or_calculate_authorized_voter_for_epoch(epoch)
|
||||
.map(|(pubkey, _)| pubkey)
|
||||
}
|
||||
|
||||
pub fn get_and_cache_authorized_voter_for_epoch(&mut self, epoch: u64) -> Option<Pubkey> {
|
||||
pub fn get_and_cache_authorized_voter_for_epoch(&mut self, epoch: Epoch) -> Option<Pubkey> {
|
||||
let res = self.get_or_calculate_authorized_voter_for_epoch(epoch);
|
||||
|
||||
res.map(|(pubkey, existed)| {
|
||||
@ -31,11 +31,11 @@ impl AuthorizedVoters {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, epoch: u64, authorized_voter: Pubkey) {
|
||||
pub fn insert(&mut self, epoch: Epoch, authorized_voter: Pubkey) {
|
||||
self.authorized_voters.insert(epoch, authorized_voter);
|
||||
}
|
||||
|
||||
pub fn purge_authorized_voters(&mut self, current_epoch: u64) -> bool {
|
||||
pub fn purge_authorized_voters(&mut self, current_epoch: Epoch) -> bool {
|
||||
// Iterate through the keys in order, filtering out the ones
|
||||
// less than the current epoch
|
||||
let expired_keys: Vec<_> = self
|
||||
@ -72,14 +72,18 @@ impl AuthorizedVoters {
|
||||
self.authorized_voters.len()
|
||||
}
|
||||
|
||||
pub fn contains(&self, epoch: u64) -> bool {
|
||||
pub fn contains(&self, epoch: Epoch) -> bool {
|
||||
self.authorized_voters.get(&epoch).is_some()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::collections::btree_map::Iter<Epoch, Pubkey> {
|
||||
self.authorized_voters.iter()
|
||||
}
|
||||
|
||||
// Returns the authorized voter at the given epoch if the epoch is >= the
|
||||
// current epoch, and a bool indicating whether the entry for this epoch
|
||||
// exists in the self.authorized_voter map
|
||||
fn get_or_calculate_authorized_voter_for_epoch(&self, epoch: u64) -> Option<(Pubkey, bool)> {
|
||||
fn get_or_calculate_authorized_voter_for_epoch(&self, epoch: Epoch) -> Option<(Pubkey, bool)> {
|
||||
let res = self.authorized_voters.get(&epoch);
|
||||
if res.is_none() {
|
||||
// If no authorized voter has been set yet for this epoch,
|
||||
|
@ -161,7 +161,7 @@ pub struct VoteState {
|
||||
pub commission: u8,
|
||||
|
||||
pub votes: VecDeque<Lockout>,
|
||||
pub root_slot: Option<u64>,
|
||||
pub root_slot: Option<Slot>,
|
||||
|
||||
/// the signer for vote transactions
|
||||
authorized_voters: AuthorizedVoters,
|
||||
@ -190,7 +190,7 @@ impl VoteState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_authorized_voter(&self, epoch: u64) -> Option<Pubkey> {
|
||||
pub fn get_authorized_voter(&self, epoch: Epoch) -> Option<Pubkey> {
|
||||
self.authorized_voters.get_authorized_voter(epoch)
|
||||
}
|
||||
|
||||
@ -436,8 +436,8 @@ impl VoteState {
|
||||
fn set_new_authorized_voter<F>(
|
||||
&mut self,
|
||||
authorized_pubkey: &Pubkey,
|
||||
current_epoch: u64,
|
||||
target_epoch: u64,
|
||||
current_epoch: Epoch,
|
||||
target_epoch: Epoch,
|
||||
verify: F,
|
||||
) -> Result<(), InstructionError>
|
||||
where
|
||||
@ -494,7 +494,7 @@ impl VoteState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_and_update_authorized_voter(&mut self, current_epoch: u64) -> Option<Pubkey> {
|
||||
fn get_and_update_authorized_voter(&mut self, current_epoch: Epoch) -> Option<Pubkey> {
|
||||
let pubkey = self
|
||||
.authorized_voters
|
||||
.get_and_cache_authorized_voter_for_epoch(current_epoch)
|
||||
|
Reference in New Issue
Block a user