2019-05-16 08:23:31 -07:00
|
|
|
//! Stakes serve as a cache of stake and vote accounts to derive
|
|
|
|
//! node stakes
|
|
|
|
use hashbrown::HashMap;
|
|
|
|
use solana_sdk::account::Account;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use solana_stake_api::stake_state::StakeState;
|
|
|
|
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct Stakes {
|
|
|
|
/// vote accounts
|
|
|
|
vote_accounts: HashMap<Pubkey, (u64, Account)>,
|
|
|
|
|
|
|
|
/// stake_accounts
|
|
|
|
stake_accounts: HashMap<Pubkey, Account>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stakes {
|
2019-05-23 23:20:04 -07:00
|
|
|
// sum the stakes that point to the given voter_pubkey
|
|
|
|
fn calculate_stake(&self, voter_pubkey: &Pubkey) -> u64 {
|
2019-05-16 08:23:31 -07:00
|
|
|
self.stake_accounts
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, stake_account)| {
|
2019-05-23 23:20:04 -07:00
|
|
|
Some(*voter_pubkey) == StakeState::voter_pubkey_from(stake_account)
|
2019-05-16 08:23:31 -07:00
|
|
|
})
|
|
|
|
.map(|(_, stake_account)| stake_account.lamports)
|
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_stake(account: &Account) -> bool {
|
|
|
|
solana_vote_api::check_id(&account.owner) || solana_stake_api::check_id(&account.owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&mut self, pubkey: &Pubkey, account: &Account) {
|
|
|
|
if solana_vote_api::check_id(&account.owner) {
|
|
|
|
if account.lamports == 0 {
|
|
|
|
self.vote_accounts.remove(pubkey);
|
|
|
|
} else {
|
|
|
|
// update the stake of this entry
|
|
|
|
let stake = self
|
|
|
|
.vote_accounts
|
|
|
|
.get(pubkey)
|
|
|
|
.map_or_else(|| self.calculate_stake(pubkey), |v| v.0);
|
|
|
|
|
|
|
|
self.vote_accounts.insert(*pubkey, (stake, account.clone()));
|
|
|
|
}
|
|
|
|
} else if solana_stake_api::check_id(&account.owner) {
|
2019-05-23 23:20:04 -07:00
|
|
|
// old_stake is stake lamports and voter_pubkey from the pre-store() version
|
2019-05-16 08:23:31 -07:00
|
|
|
let old_stake = self.stake_accounts.get(pubkey).and_then(|old_account| {
|
2019-05-23 23:20:04 -07:00
|
|
|
StakeState::voter_pubkey_from(old_account)
|
|
|
|
.map(|old_voter_pubkey| (old_account.lamports, old_voter_pubkey))
|
2019-05-16 08:23:31 -07:00
|
|
|
});
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let stake = StakeState::voter_pubkey_from(account)
|
|
|
|
.map(|voter_pubkey| (account.lamports, voter_pubkey));
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
// if adjustments need to be made...
|
|
|
|
if stake != old_stake {
|
2019-05-23 23:20:04 -07:00
|
|
|
if let Some((old_stake, old_voter_pubkey)) = old_stake {
|
2019-05-16 08:23:31 -07:00
|
|
|
self.vote_accounts
|
2019-05-23 23:20:04 -07:00
|
|
|
.entry(old_voter_pubkey)
|
2019-05-16 08:23:31 -07:00
|
|
|
.and_modify(|e| e.0 -= old_stake);
|
|
|
|
}
|
2019-05-23 23:20:04 -07:00
|
|
|
if let Some((stake, voter_pubkey)) = stake {
|
2019-05-16 08:23:31 -07:00
|
|
|
self.vote_accounts
|
2019-05-23 23:20:04 -07:00
|
|
|
.entry(voter_pubkey)
|
2019-05-16 08:23:31 -07:00
|
|
|
.and_modify(|e| e.0 += stake);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.lamports == 0 {
|
|
|
|
self.stake_accounts.remove(pubkey);
|
|
|
|
} else {
|
|
|
|
self.stake_accounts.insert(*pubkey, account.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn vote_accounts(&self) -> &HashMap<Pubkey, (u64, Account)> {
|
|
|
|
&self.vote_accounts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use solana_stake_api::stake_state;
|
|
|
|
use solana_vote_api::vote_state::{self, VoteState};
|
|
|
|
|
|
|
|
// set up some dummies for a staked node (( vote ) ( stake ))
|
|
|
|
fn create_staked_node_accounts(stake: u64) -> ((Pubkey, Account), (Pubkey, Account)) {
|
2019-05-23 23:20:04 -07:00
|
|
|
let vote_pubkey = Pubkey::new_rand();
|
|
|
|
let vote_account = vote_state::create_account(&vote_pubkey, &Pubkey::new_rand(), 0, 1);
|
2019-05-16 08:23:31 -07:00
|
|
|
(
|
2019-05-23 23:20:04 -07:00
|
|
|
(vote_pubkey, vote_account),
|
|
|
|
create_stake_account(stake, &vote_pubkey),
|
2019-05-16 08:23:31 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
// add stake to a vote_pubkey ( stake )
|
|
|
|
fn create_stake_account(stake: u64, vote_pubkey: &Pubkey) -> (Pubkey, Account) {
|
2019-05-16 08:23:31 -07:00
|
|
|
(
|
|
|
|
Pubkey::new_rand(),
|
2019-05-23 23:20:04 -07:00
|
|
|
stake_state::create_delegate_stake_account(&vote_pubkey, &VoteState::default(), stake),
|
2019-05-16 08:23:31 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stakes_basic() {
|
|
|
|
let mut stakes = Stakes::default();
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let ((vote_pubkey, vote_account), (stake_pubkey, mut stake_account)) =
|
2019-05-16 08:23:31 -07:00
|
|
|
create_staked_node_accounts(10);
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
stake_account.lamports = 42;
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 42);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
stake_account.lamports = 0;
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 0);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stakes_vote_account_disappear_reappear() {
|
|
|
|
let mut stakes = Stakes::default();
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let ((vote_pubkey, mut vote_account), (stake_pubkey, stake_account)) =
|
2019-05-16 08:23:31 -07:00
|
|
|
create_staked_node_accounts(10);
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vote_account.lamports = 0;
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_none());
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
vote_account.lamports = 1;
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stakes_change_delegate() {
|
|
|
|
let mut stakes = Stakes::default();
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
|
|
|
|
create_staked_node_accounts(10);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let ((vote_pubkey2, vote_account2), (_stake_pubkey2, stake_account2)) =
|
2019-05-16 08:23:31 -07:00
|
|
|
create_staked_node_accounts(10);
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
|
|
|
stakes.store(&vote_pubkey2, &vote_account2);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
// delegates to vote_pubkey
|
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
|
|
|
assert!(vote_accounts.get(&vote_pubkey2).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey2).unwrap().0, 0);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
// delegates to vote_pubkey2
|
|
|
|
stakes.store(&stake_pubkey, &stake_account2);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 0);
|
|
|
|
assert!(vote_accounts.get(&vote_pubkey2).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey2).unwrap().0, 10);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_stakes_multiple_stakers() {
|
|
|
|
let mut stakes = Stakes::default();
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
|
|
|
|
create_staked_node_accounts(10);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let (stake_pubkey2, stake_account2) = create_stake_account(10, &vote_pubkey);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
// delegates to vote_pubkey
|
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
|
|
|
stakes.store(&stake_pubkey2, &stake_account2);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 20);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stakes_not_delegate() {
|
|
|
|
let mut stakes = Stakes::default();
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
|
|
|
|
create_staked_node_accounts(10);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&vote_pubkey, &vote_account);
|
|
|
|
stakes.store(&stake_pubkey, &stake_account);
|
2019-05-16 08:23:31 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// not a stake account, and whacks above entry
|
2019-05-23 23:20:04 -07:00
|
|
|
stakes.store(&stake_pubkey, &Account::new(1, 0, &solana_stake_api::id()));
|
2019-05-16 08:23:31 -07:00
|
|
|
{
|
|
|
|
let vote_accounts = stakes.vote_accounts();
|
2019-05-23 23:20:04 -07:00
|
|
|
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
|
|
|
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 0);
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|