Skip to content

Commit 2892b26

Browse files
authored
Add epoch_node_id_to_stake() to get total stake belonging to given node_id in given epoch. (#2478)
* Add epoch_node_id_stake to get total stake belonging to given node_id in the given epoch. * Fix typos to test epoch 1. * Small fix.
1 parent 4bc8d5d commit 2892b26

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

runtime/src/bank.rs

+7
Original file line numberDiff line numberDiff line change
@@ -5913,6 +5913,13 @@ impl Bank {
59135913
.get(node_id)
59145914
}
59155915

5916+
/// Get the total stake belonging to vote accounts associated with the given node id for the
5917+
/// given epoch.
5918+
pub fn epoch_node_id_to_stake(&self, epoch: Epoch, node_id: &Pubkey) -> Option<u64> {
5919+
self.epoch_stakes(epoch)
5920+
.and_then(|epoch_stakes| epoch_stakes.node_id_to_stake(node_id))
5921+
}
5922+
59165923
/// Get the fixed total stake of all vote accounts for current epoch
59175924
pub fn total_epoch_stake(&self) -> u64 {
59185925
self.epoch_stakes

runtime/src/bank/tests.rs

+43
Original file line numberDiff line numberDiff line change
@@ -12984,3 +12984,46 @@ fn test_blockhash_last_valid_block_height() {
1298412984
);
1298512985
assert!(!bank.is_blockhash_valid(&last_blockhash));
1298612986
}
12987+
12988+
#[test]
12989+
fn test_bank_epoch_stakes() {
12990+
solana_logger::setup();
12991+
let num_of_nodes: u64 = 30;
12992+
let stakes = (1..num_of_nodes.checked_add(1).expect("Shouldn't be big")).collect::<Vec<_>>();
12993+
let voting_keypairs = stakes
12994+
.iter()
12995+
.map(|_| ValidatorVoteKeypairs::new_rand())
12996+
.collect::<Vec<_>>();
12997+
let total_stake = stakes.iter().sum();
12998+
let GenesisConfigInfo { genesis_config, .. } =
12999+
create_genesis_config_with_vote_accounts(1_000_000_000, &voting_keypairs, stakes.clone());
13000+
13001+
let bank0 = Arc::new(Bank::new_for_tests(&genesis_config));
13002+
let bank1 = Bank::new_from_parent(
13003+
bank0.clone(),
13004+
&Pubkey::default(),
13005+
bank0.get_slots_in_epoch(0) + 1,
13006+
);
13007+
13008+
let initial_epochs = bank0.epoch_stake_keys();
13009+
assert_eq!(initial_epochs, vec![0, 1]);
13010+
13011+
assert_eq!(bank0.epoch(), 0);
13012+
assert_eq!(bank0.epoch_total_stake(0), Some(total_stake));
13013+
assert_eq!(bank0.epoch_node_id_to_stake(0, &Pubkey::new_unique()), None);
13014+
for (i, keypair) in voting_keypairs.iter().enumerate() {
13015+
assert_eq!(
13016+
bank0.epoch_node_id_to_stake(0, &keypair.node_keypair.pubkey()),
13017+
Some(stakes[i])
13018+
);
13019+
}
13020+
assert_eq!(bank1.epoch(), 1);
13021+
assert_eq!(bank1.epoch_total_stake(1), Some(total_stake));
13022+
assert_eq!(bank1.epoch_node_id_to_stake(1, &Pubkey::new_unique()), None);
13023+
for (i, keypair) in voting_keypairs.iter().enumerate() {
13024+
assert_eq!(
13025+
bank1.epoch_node_id_to_stake(1, &keypair.node_keypair.pubkey()),
13026+
Some(stakes[i])
13027+
);
13028+
}
13029+
}

0 commit comments

Comments
 (0)