_id => _pubkey variable renaming (#4419)
* wallet: rename *_account_id to *_account_pubkey * s/from_id/from_pubkey/g * s/node_id/node_pubkey/g * s/stake_id/stake_pubkey/g * s/voter_id/voter_pubkey/g * s/vote_id/vote_pubkey/g * s/delegate_id/delegate_pubkey/g * s/account_id/account_pubkey/g * s/to_id/to_pubkey/g * s/my_id/my_pubkey/g * cargo fmt * s/staker_id/staker_pubkey/g * s/mining_pool_id/mining_pool_pubkey/g * s/leader_id/leader_pubkey/g * cargo fmt * s/funding_id/funding_pubkey/g
This commit is contained in:
@@ -15,12 +15,12 @@ pub struct Stakes {
|
||||
}
|
||||
|
||||
impl Stakes {
|
||||
// sum the stakes that point to the given voter_id
|
||||
fn calculate_stake(&self, voter_id: &Pubkey) -> u64 {
|
||||
// sum the stakes that point to the given voter_pubkey
|
||||
fn calculate_stake(&self, voter_pubkey: &Pubkey) -> u64 {
|
||||
self.stake_accounts
|
||||
.iter()
|
||||
.filter(|(_, stake_account)| {
|
||||
Some(*voter_id) == StakeState::voter_id_from(stake_account)
|
||||
Some(*voter_pubkey) == StakeState::voter_pubkey_from(stake_account)
|
||||
})
|
||||
.map(|(_, stake_account)| stake_account.lamports)
|
||||
.sum()
|
||||
@@ -44,25 +44,25 @@ impl Stakes {
|
||||
self.vote_accounts.insert(*pubkey, (stake, account.clone()));
|
||||
}
|
||||
} else if solana_stake_api::check_id(&account.owner) {
|
||||
// old_stake is stake lamports and voter_id from the pre-store() version
|
||||
// old_stake is stake lamports and voter_pubkey from the pre-store() version
|
||||
let old_stake = self.stake_accounts.get(pubkey).and_then(|old_account| {
|
||||
StakeState::voter_id_from(old_account)
|
||||
.map(|old_voter_id| (old_account.lamports, old_voter_id))
|
||||
StakeState::voter_pubkey_from(old_account)
|
||||
.map(|old_voter_pubkey| (old_account.lamports, old_voter_pubkey))
|
||||
});
|
||||
|
||||
let stake =
|
||||
StakeState::voter_id_from(account).map(|voter_id| (account.lamports, voter_id));
|
||||
let stake = StakeState::voter_pubkey_from(account)
|
||||
.map(|voter_pubkey| (account.lamports, voter_pubkey));
|
||||
|
||||
// if adjustments need to be made...
|
||||
if stake != old_stake {
|
||||
if let Some((old_stake, old_voter_id)) = old_stake {
|
||||
if let Some((old_stake, old_voter_pubkey)) = old_stake {
|
||||
self.vote_accounts
|
||||
.entry(old_voter_id)
|
||||
.entry(old_voter_pubkey)
|
||||
.and_modify(|e| e.0 -= old_stake);
|
||||
}
|
||||
if let Some((stake, voter_id)) = stake {
|
||||
if let Some((stake, voter_pubkey)) = stake {
|
||||
self.vote_accounts
|
||||
.entry(voter_id)
|
||||
.entry(voter_pubkey)
|
||||
.and_modify(|e| e.0 += stake);
|
||||
}
|
||||
}
|
||||
@@ -88,19 +88,19 @@ mod tests {
|
||||
|
||||
// set up some dummies for a staked node (( vote ) ( stake ))
|
||||
fn create_staked_node_accounts(stake: u64) -> ((Pubkey, Account), (Pubkey, Account)) {
|
||||
let vote_id = Pubkey::new_rand();
|
||||
let vote_account = vote_state::create_account(&vote_id, &Pubkey::new_rand(), 0, 1);
|
||||
let vote_pubkey = Pubkey::new_rand();
|
||||
let vote_account = vote_state::create_account(&vote_pubkey, &Pubkey::new_rand(), 0, 1);
|
||||
(
|
||||
(vote_id, vote_account),
|
||||
create_stake_account(stake, &vote_id),
|
||||
(vote_pubkey, vote_account),
|
||||
create_stake_account(stake, &vote_pubkey),
|
||||
)
|
||||
}
|
||||
|
||||
// add stake to a vote_id ( stake )
|
||||
fn create_stake_account(stake: u64, vote_id: &Pubkey) -> (Pubkey, Account) {
|
||||
// add stake to a vote_pubkey ( stake )
|
||||
fn create_stake_account(stake: u64, vote_pubkey: &Pubkey) -> (Pubkey, Account) {
|
||||
(
|
||||
Pubkey::new_rand(),
|
||||
stake_state::create_delegate_stake_account(&vote_id, &VoteState::default(), stake),
|
||||
stake_state::create_delegate_stake_account(&vote_pubkey, &VoteState::default(), stake),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -108,32 +108,32 @@ mod tests {
|
||||
fn test_stakes_basic() {
|
||||
let mut stakes = Stakes::default();
|
||||
|
||||
let ((vote_id, vote_account), (stake_id, mut stake_account)) =
|
||||
let ((vote_pubkey, vote_account), (stake_pubkey, mut stake_account)) =
|
||||
create_staked_node_accounts(10);
|
||||
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 10);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
||||
}
|
||||
|
||||
stake_account.lamports = 42;
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 42);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 42);
|
||||
}
|
||||
|
||||
stake_account.lamports = 0;
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 0);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,32 +141,32 @@ mod tests {
|
||||
fn test_stakes_vote_account_disappear_reappear() {
|
||||
let mut stakes = Stakes::default();
|
||||
|
||||
let ((vote_id, mut vote_account), (stake_id, stake_account)) =
|
||||
let ((vote_pubkey, mut vote_account), (stake_pubkey, stake_account)) =
|
||||
create_staked_node_accounts(10);
|
||||
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 10);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
||||
}
|
||||
|
||||
vote_account.lamports = 0;
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_none());
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_none());
|
||||
}
|
||||
vote_account.lamports = 1;
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 10);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,54 +174,56 @@ mod tests {
|
||||
fn test_stakes_change_delegate() {
|
||||
let mut stakes = Stakes::default();
|
||||
|
||||
let ((vote_id, vote_account), (stake_id, stake_account)) = create_staked_node_accounts(10);
|
||||
|
||||
let ((vote_id2, vote_account2), (_stake_id2, stake_account2)) =
|
||||
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
|
||||
create_staked_node_accounts(10);
|
||||
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&vote_id2, &vote_account2);
|
||||
let ((vote_pubkey2, vote_account2), (_stake_pubkey2, stake_account2)) =
|
||||
create_staked_node_accounts(10);
|
||||
|
||||
// delegates to vote_id
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
stakes.store(&vote_pubkey2, &vote_account2);
|
||||
|
||||
// delegates to vote_pubkey
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 10);
|
||||
assert!(vote_accounts.get(&vote_id2).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id2).unwrap().0, 0);
|
||||
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);
|
||||
}
|
||||
|
||||
// delegates to vote_id2
|
||||
stakes.store(&stake_id, &stake_account2);
|
||||
// delegates to vote_pubkey2
|
||||
stakes.store(&stake_pubkey, &stake_account2);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 0);
|
||||
assert!(vote_accounts.get(&vote_id2).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id2).unwrap().0, 10);
|
||||
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);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_stakes_multiple_stakers() {
|
||||
let mut stakes = Stakes::default();
|
||||
|
||||
let ((vote_id, vote_account), (stake_id, stake_account)) = create_staked_node_accounts(10);
|
||||
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
|
||||
create_staked_node_accounts(10);
|
||||
|
||||
let (stake_id2, stake_account2) = create_stake_account(10, &vote_id);
|
||||
let (stake_pubkey2, stake_account2) = create_stake_account(10, &vote_pubkey);
|
||||
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
|
||||
// delegates to vote_id
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&stake_id2, &stake_account2);
|
||||
// delegates to vote_pubkey
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
stakes.store(&stake_pubkey2, &stake_account2);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 20);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 20);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,23 +231,24 @@ mod tests {
|
||||
fn test_stakes_not_delegate() {
|
||||
let mut stakes = Stakes::default();
|
||||
|
||||
let ((vote_id, vote_account), (stake_id, stake_account)) = create_staked_node_accounts(10);
|
||||
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
|
||||
create_staked_node_accounts(10);
|
||||
|
||||
stakes.store(&vote_id, &vote_account);
|
||||
stakes.store(&stake_id, &stake_account);
|
||||
stakes.store(&vote_pubkey, &vote_account);
|
||||
stakes.store(&stake_pubkey, &stake_account);
|
||||
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 10);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 10);
|
||||
}
|
||||
|
||||
// not a stake account, and whacks above entry
|
||||
stakes.store(&stake_id, &Account::new(1, 0, &solana_stake_api::id()));
|
||||
stakes.store(&stake_pubkey, &Account::new(1, 0, &solana_stake_api::id()));
|
||||
{
|
||||
let vote_accounts = stakes.vote_accounts();
|
||||
assert!(vote_accounts.get(&vote_id).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_id).unwrap().0, 0);
|
||||
assert!(vote_accounts.get(&vote_pubkey).is_some());
|
||||
assert_eq!(vote_accounts.get(&vote_pubkey).unwrap().0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user