vote_api cleanup (#3710)
* vote_api cleanup * fixups * fixup * remove unused code * revert removal of serialize and deserialize * ... * increase coverage, bootstrap staking * Sagar's STAKE to my VOTE
This commit is contained in:
@ -451,7 +451,8 @@ mod tests {
|
||||
use solana_sdk::hash::hash;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_transaction;
|
||||
use solana_vote_api::vote_instruction::{self, Vote};
|
||||
use solana_vote_api::vote_instruction;
|
||||
use solana_vote_api::vote_state::Vote;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
|
||||
fn create_sample_payment(keypair: &Keypair, hash: Hash) -> Transaction {
|
||||
|
@ -28,7 +28,8 @@ use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_transaction;
|
||||
use solana_sdk::timing::timestamp;
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use solana_vote_api::vote_instruction::{self, Vote};
|
||||
use solana_vote_api::vote_instruction;
|
||||
use solana_vote_api::vote_state::Vote;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{channel, Receiver};
|
||||
@ -334,6 +335,8 @@ pub fn make_active_set_entries(
|
||||
let new_vote_account_ixs = vote_instruction::create_account(
|
||||
&active_keypair.pubkey(),
|
||||
&vote_account_id,
|
||||
&active_keypair.pubkey(),
|
||||
0,
|
||||
stake.saturating_sub(2),
|
||||
);
|
||||
let new_vote_account_tx = Transaction::new_signed_instructions(
|
||||
|
@ -100,7 +100,7 @@ mod tests {
|
||||
use crate::blocktree::get_tmp_ledger_path;
|
||||
use crate::blocktree::tests::make_slot_entries;
|
||||
use crate::staking_utils;
|
||||
use crate::voting_keypair::tests::new_vote_account_with_delegate;
|
||||
use crate::voting_keypair::tests::new_vote_account;
|
||||
use solana_sdk::genesis_block::{GenesisBlock, BOOTSTRAP_LEADER_LAMPORTS};
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use std::sync::Arc;
|
||||
@ -224,7 +224,7 @@ mod tests {
|
||||
|
||||
// Create new vote account
|
||||
let new_voting_keypair = Keypair::new();
|
||||
new_vote_account_with_delegate(
|
||||
new_vote_account(
|
||||
&mint_keypair,
|
||||
&new_voting_keypair,
|
||||
&delegate_id,
|
||||
|
@ -334,13 +334,15 @@ impl LocalCluster {
|
||||
amount: u64,
|
||||
) -> Result<()> {
|
||||
let vote_account_pubkey = vote_account.pubkey();
|
||||
let delegate_id = from_account.pubkey();
|
||||
let node_id = from_account.pubkey();
|
||||
// Create the vote account if necessary
|
||||
if client.poll_get_balance(&vote_account_pubkey).unwrap_or(0) == 0 {
|
||||
// 1) Create vote account
|
||||
let instructions = vote_instruction::create_account(
|
||||
&from_account.pubkey(),
|
||||
&vote_account_pubkey,
|
||||
&node_id,
|
||||
0,
|
||||
amount,
|
||||
);
|
||||
let mut transaction = Transaction::new_signed_instructions(
|
||||
@ -355,27 +357,12 @@ impl LocalCluster {
|
||||
client
|
||||
.wait_for_balance(&vote_account_pubkey, Some(amount))
|
||||
.expect("get balance");
|
||||
|
||||
// 2) Set delegate for new vote account
|
||||
let vote_instruction =
|
||||
vote_instruction::delegate_stake(&vote_account_pubkey, &delegate_id);
|
||||
|
||||
let mut transaction = Transaction::new_signed_instructions(
|
||||
&[vote_account],
|
||||
vec![vote_instruction],
|
||||
client.get_recent_blockhash().unwrap(),
|
||||
);
|
||||
|
||||
client
|
||||
.retry_transfer(&vote_account, &mut transaction, 5)
|
||||
.expect("client transfer 2");
|
||||
}
|
||||
|
||||
info!("Checking for vote account registration");
|
||||
let vote_account_user_data = client.get_account_data(&vote_account_pubkey);
|
||||
if let Ok(Some(vote_account_user_data)) = vote_account_user_data {
|
||||
if let Ok(vote_state) = VoteState::deserialize(&vote_account_user_data) {
|
||||
if vote_state.delegate_id == delegate_id {
|
||||
if vote_state.node_id == node_id {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,7 @@ use solana_metrics::influxdb;
|
||||
use solana_runtime::bank::Bank;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_vote_api::vote_instruction::Vote;
|
||||
use solana_vote_api::vote_state::{Lockout, VoteState, MAX_LOCKOUT_HISTORY};
|
||||
use solana_vote_api::vote_state::{Lockout, Vote, VoteState, MAX_LOCKOUT_HISTORY};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub const VOTE_THRESHOLD_DEPTH: usize = 8;
|
||||
@ -117,7 +116,7 @@ impl Locktower {
|
||||
.expect("bank should always have valid VoteState data");
|
||||
|
||||
if key == self.epoch_stakes.delegate_id
|
||||
|| vote_state.delegate_id == self.epoch_stakes.delegate_id
|
||||
|| vote_state.node_id == self.epoch_stakes.delegate_id
|
||||
{
|
||||
debug!("vote state {:?}", vote_state);
|
||||
debug!(
|
||||
@ -141,7 +140,7 @@ impl Locktower {
|
||||
);
|
||||
}
|
||||
let start_root = vote_state.root_slot;
|
||||
vote_state.process_vote(Vote { slot: bank_slot });
|
||||
vote_state.process_vote(&Vote { slot: bank_slot });
|
||||
for vote in &vote_state.votes {
|
||||
Self::update_ancestor_lockouts(&mut stake_lockouts, &vote, ancestors);
|
||||
}
|
||||
@ -237,7 +236,7 @@ impl Locktower {
|
||||
|
||||
pub fn record_vote(&mut self, slot: u64) -> Option<u64> {
|
||||
let root_slot = self.lockouts.root_slot;
|
||||
self.lockouts.process_vote(Vote { slot });
|
||||
self.lockouts.process_vote(&Vote { slot });
|
||||
solana_metrics::submit(
|
||||
influxdb::Point::new("counter-locktower-vote")
|
||||
.add_field("latest", influxdb::Value::Integer(slot as i64))
|
||||
@ -281,7 +280,7 @@ impl Locktower {
|
||||
|
||||
pub fn is_locked_out(&self, slot: u64, descendants: &HashMap<u64, HashSet<u64>>) -> bool {
|
||||
let mut lockouts = self.lockouts.clone();
|
||||
lockouts.process_vote(Vote { slot });
|
||||
lockouts.process_vote(&Vote { slot });
|
||||
for vote in &lockouts.votes {
|
||||
if vote.slot == slot {
|
||||
continue;
|
||||
@ -303,7 +302,7 @@ impl Locktower {
|
||||
stake_lockouts: &HashMap<u64, StakeLockout>,
|
||||
) -> bool {
|
||||
let mut lockouts = self.lockouts.clone();
|
||||
lockouts.process_vote(Vote { slot });
|
||||
lockouts.process_vote(&Vote { slot });
|
||||
let vote = lockouts.nth_recent_vote(self.threshold_depth);
|
||||
if let Some(vote) = vote {
|
||||
if let Some(fork_stake) = stake_lockouts.get(&vote.slot) {
|
||||
@ -398,7 +397,7 @@ mod test {
|
||||
account.lamports = *lamports;
|
||||
let mut vote_state = VoteState::default();
|
||||
for slot in *votes {
|
||||
vote_state.process_vote(Vote { slot: *slot });
|
||||
vote_state.process_vote(&Vote { slot: *slot });
|
||||
}
|
||||
vote_state
|
||||
.serialize(&mut account.data)
|
||||
|
@ -21,7 +21,8 @@ use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::KeypairUtil;
|
||||
use solana_sdk::timing::{self, duration_as_ms};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use solana_vote_api::vote_instruction::{self, Vote};
|
||||
use solana_vote_api::vote_instruction;
|
||||
use solana_vote_api::vote_state::Vote;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
|
@ -76,7 +76,7 @@ pub fn node_staked_accounts_at_epoch(
|
||||
|
||||
fn filter_no_delegate(account_id: &Pubkey, account: &Account) -> bool {
|
||||
VoteState::deserialize(&account.data)
|
||||
.map(|vote_state| vote_state.delegate_id != *account_id)
|
||||
.map(|vote_state| vote_state.node_id != *account_id)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
@ -104,7 +104,7 @@ fn to_delegated_stakes(
|
||||
) -> HashMap<Pubkey, u64> {
|
||||
let mut map: HashMap<Pubkey, u64> = HashMap::new();
|
||||
node_staked_accounts.for_each(|(stake, state)| {
|
||||
let delegate = &state.delegate_id;
|
||||
let delegate = &state.node_id;
|
||||
map.entry(*delegate)
|
||||
.and_modify(|s| *s += stake)
|
||||
.or_insert(stake);
|
||||
@ -200,7 +200,7 @@ mod tests {
|
||||
|
||||
// Make a mint vote account. Because the mint has nonzero stake, this
|
||||
// should show up in the active set
|
||||
voting_keypair_tests::new_vote_account_with_delegate(
|
||||
voting_keypair_tests::new_vote_account(
|
||||
&mint_keypair,
|
||||
&bank_voter,
|
||||
&mint_keypair.pubkey(),
|
||||
@ -283,11 +283,11 @@ mod tests {
|
||||
|
||||
// Delegate 1 has stake of 3
|
||||
for i in 0..3 {
|
||||
stakes.push((i, VoteState::new(&delegate1)));
|
||||
stakes.push((i, VoteState::new(&Pubkey::new_rand(), &delegate1, 0)));
|
||||
}
|
||||
|
||||
// Delegate 1 has stake of 5
|
||||
stakes.push((5, VoteState::new(&delegate2)));
|
||||
stakes.push((5, VoteState::new(&Pubkey::new_rand(), &delegate2, 0)));
|
||||
|
||||
let result = to_delegated_stakes(stakes.into_iter());
|
||||
assert_eq!(result.len(), 2);
|
||||
|
@ -110,7 +110,8 @@ pub mod tests {
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use solana_vote_api::vote_instruction::{self, Vote};
|
||||
use solana_vote_api::vote_instruction;
|
||||
use solana_vote_api::vote_state::Vote;
|
||||
|
||||
fn process_instructions<T: KeypairUtil>(bank: &Bank, keypairs: &[&T], ixs: Vec<Instruction>) {
|
||||
let blockhash = bank.last_blockhash();
|
||||
@ -119,27 +120,21 @@ pub mod tests {
|
||||
}
|
||||
|
||||
pub fn new_vote_account(
|
||||
from_keypair: &Keypair,
|
||||
voting_pubkey: &Pubkey,
|
||||
bank: &Bank,
|
||||
lamports: u64,
|
||||
) {
|
||||
let ixs = vote_instruction::create_account(&from_keypair.pubkey(), voting_pubkey, lamports);
|
||||
process_instructions(bank, &[from_keypair], ixs);
|
||||
}
|
||||
|
||||
pub fn new_vote_account_with_delegate(
|
||||
from_keypair: &Keypair,
|
||||
voting_keypair: &Keypair,
|
||||
delegate: &Pubkey,
|
||||
node_id: &Pubkey,
|
||||
bank: &Bank,
|
||||
lamports: u64,
|
||||
) {
|
||||
let voting_pubkey = voting_keypair.pubkey();
|
||||
let mut ixs =
|
||||
vote_instruction::create_account(&from_keypair.pubkey(), &voting_pubkey, lamports);
|
||||
ixs.push(vote_instruction::delegate_stake(&voting_pubkey, delegate));
|
||||
process_instructions(bank, &[from_keypair, voting_keypair], ixs);
|
||||
let ixs = vote_instruction::create_account(
|
||||
&from_keypair.pubkey(),
|
||||
&voting_pubkey,
|
||||
node_id,
|
||||
0,
|
||||
lamports,
|
||||
);
|
||||
process_instructions(bank, &[from_keypair], ixs);
|
||||
}
|
||||
|
||||
pub fn push_vote<T: KeypairUtil>(voting_keypair: &T, bank: &Bank, slot: u64) {
|
||||
@ -150,13 +145,19 @@ pub mod tests {
|
||||
pub fn new_vote_account_with_vote<T: KeypairUtil>(
|
||||
from_keypair: &T,
|
||||
voting_keypair: &T,
|
||||
node_id: &Pubkey,
|
||||
bank: &Bank,
|
||||
lamports: u64,
|
||||
slot: u64,
|
||||
) {
|
||||
let voting_pubkey = voting_keypair.pubkey();
|
||||
let mut ixs =
|
||||
vote_instruction::create_account(&from_keypair.pubkey(), &voting_pubkey, lamports);
|
||||
let mut ixs = vote_instruction::create_account(
|
||||
&from_keypair.pubkey(),
|
||||
&voting_pubkey,
|
||||
node_id,
|
||||
0,
|
||||
lamports,
|
||||
);
|
||||
ixs.push(vote_instruction::vote(&voting_pubkey, Vote::new(slot)));
|
||||
process_instructions(bank, &[from_keypair, voting_keypair], ixs);
|
||||
}
|
||||
|
Reference in New Issue
Block a user