Less copy pasta
This commit is contained in:
@ -68,34 +68,35 @@ impl ActiveStakers {
|
|||||||
|
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use solana_runtime::bank::Bank;
|
use solana_runtime::bank::Bank;
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::vote_transaction::VoteTransaction;
|
use solana_sdk::vote_transaction::VoteTransaction;
|
||||||
|
|
||||||
pub fn new_vote_account<T: KeypairUtil>(
|
pub fn new_vote_account(
|
||||||
|
from_keypair: &Keypair,
|
||||||
|
voting_pubkey: &Pubkey,
|
||||||
|
bank: &Bank,
|
||||||
|
num_tokens: u64,
|
||||||
|
) {
|
||||||
|
let last_id = bank.last_id();
|
||||||
|
let tx = VoteTransaction::new_account(from_keypair, *voting_pubkey, last_id, num_tokens, 0);
|
||||||
|
bank.process_transaction(&tx).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_vote<T: KeypairUtil>(voting_keypair: &T, bank: &Bank, tick_height: u64) {
|
||||||
|
let last_id = bank.last_id();
|
||||||
|
let tx = VoteTransaction::new_vote(voting_keypair, tick_height, last_id, 0);
|
||||||
|
bank.process_transaction(&tx).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_vote_account_with_vote<T: KeypairUtil>(
|
||||||
from_keypair: &Keypair,
|
from_keypair: &Keypair,
|
||||||
voting_keypair: &T,
|
voting_keypair: &T,
|
||||||
bank: &Bank,
|
bank: &Bank,
|
||||||
num_tokens: u64,
|
num_tokens: u64,
|
||||||
last_id: Hash,
|
|
||||||
) {
|
|
||||||
let tx = VoteTransaction::new_account(
|
|
||||||
from_keypair,
|
|
||||||
voting_keypair.pubkey(),
|
|
||||||
last_id,
|
|
||||||
num_tokens,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
bank.process_transaction(&tx).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push_vote<T: KeypairUtil>(
|
|
||||||
voting_keypair: &T,
|
|
||||||
bank: &Bank,
|
|
||||||
tick_height: u64,
|
tick_height: u64,
|
||||||
last_id: Hash,
|
|
||||||
) {
|
) {
|
||||||
let new_vote_tx = VoteTransaction::new_vote(voting_keypair, tick_height, last_id, 0);
|
new_vote_account(from_keypair, &voting_keypair.pubkey(), bank, num_tokens);
|
||||||
bank.process_transaction(&new_vote_tx).unwrap();
|
push_vote(voting_keypair, bank, tick_height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,7 +133,7 @@ impl Service for LeaderConfirmationService {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::active_stakers::tests::new_vote_account;
|
use crate::active_stakers::tests::{new_vote_account, push_vote};
|
||||||
use crate::voting_keypair::VotingKeypair;
|
use crate::voting_keypair::VotingKeypair;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use solana_sdk::genesis_block::GenesisBlock;
|
use solana_sdk::genesis_block::GenesisBlock;
|
||||||
@ -169,16 +169,15 @@ pub mod tests {
|
|||||||
let validator_keypair = Arc::new(Keypair::new());
|
let validator_keypair = Arc::new(Keypair::new());
|
||||||
let last_id = ids[i];
|
let last_id = ids[i];
|
||||||
let voting_keypair = VotingKeypair::new_local(&validator_keypair);
|
let voting_keypair = VotingKeypair::new_local(&validator_keypair);
|
||||||
|
let voting_pubkey = voting_keypair.pubkey();
|
||||||
|
|
||||||
// Give the validator some tokens
|
// Give the validator some tokens
|
||||||
bank.transfer(2, &mint_keypair, validator_keypair.pubkey(), last_id)
|
bank.transfer(2, &mint_keypair, validator_keypair.pubkey(), last_id)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
new_vote_account(&validator_keypair, &voting_keypair, &bank, 1, last_id);
|
new_vote_account(&validator_keypair, &voting_pubkey, &bank, 1);
|
||||||
|
|
||||||
if i < 6 {
|
if i < 6 {
|
||||||
let vote_tx =
|
push_vote(&voting_keypair, &bank, (i + 1) as u64);
|
||||||
VoteTransaction::new_vote(&voting_keypair, (i + 1) as u64, last_id, 0);
|
|
||||||
bank.process_transaction(&vote_tx).unwrap();
|
|
||||||
}
|
}
|
||||||
(voting_keypair, validator_keypair)
|
(voting_keypair, validator_keypair)
|
||||||
})
|
})
|
||||||
|
@ -428,7 +428,7 @@ pub fn make_active_set_entries(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::active_stakers::tests::{new_vote_account, push_vote};
|
use crate::active_stakers::tests::{new_vote_account_with_vote, push_vote};
|
||||||
use hashbrown::HashSet;
|
use hashbrown::HashSet;
|
||||||
use solana_sdk::genesis_block::{GenesisBlock, BOOTSTRAP_LEADER_TOKENS};
|
use solana_sdk::genesis_block::{GenesisBlock, BOOTSTRAP_LEADER_TOKENS};
|
||||||
use std::hash::Hash as StdHash;
|
use std::hash::Hash as StdHash;
|
||||||
@ -477,22 +477,14 @@ pub mod tests {
|
|||||||
bank.transfer(stake, &mint_keypair, new_pubkey, last_id)
|
bank.transfer(stake, &mint_keypair, new_pubkey, last_id)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Create a vote account
|
// Vote to make the validator part of the active set for the entire test
|
||||||
new_vote_account(
|
// (we made the active_window_tick_length large enough at the beginning of the test)
|
||||||
|
new_vote_account_with_vote(
|
||||||
&new_validator,
|
&new_validator,
|
||||||
&voting_keypair,
|
&voting_keypair,
|
||||||
&bank,
|
&bank,
|
||||||
num_vote_account_tokens as u64,
|
num_vote_account_tokens as u64,
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Vote to make the validator part of the active set for the entire test
|
|
||||||
// (we made the active_window_tick_length large enough at the beginning of the test)
|
|
||||||
push_vote(
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
ticks_per_epoch,
|
ticks_per_epoch,
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -636,23 +628,8 @@ pub mod tests {
|
|||||||
bank.transfer(5, &mint_keypair, pk, genesis_block.last_id())
|
bank.transfer(5, &mint_keypair, pk, genesis_block.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Create a vote account
|
// Create a vote account and push a vote
|
||||||
let voting_keypair = Keypair::new();
|
new_vote_account_with_vote(&new_keypair, &Keypair::new(), &bank, 1, start_height);
|
||||||
new_vote_account(
|
|
||||||
&new_keypair,
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
1,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Push a vote for the account
|
|
||||||
push_vote(
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
start_height,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert a bunch of votes at height "start_height + active_window_tick_length"
|
// Insert a bunch of votes at height "start_height + active_window_tick_length"
|
||||||
@ -666,22 +643,9 @@ pub mod tests {
|
|||||||
bank.transfer(5, &mint_keypair, pk, genesis_block.last_id())
|
bank.transfer(5, &mint_keypair, pk, genesis_block.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Create a vote account
|
// Create a vote account and push a vote
|
||||||
let voting_keypair = Keypair::new();
|
let tick_height = start_height + active_window_tick_length + 1;
|
||||||
new_vote_account(
|
new_vote_account_with_vote(&new_keypair, &Keypair::new(), &bank, 1, tick_height);
|
||||||
&new_keypair,
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
1,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
push_vote(
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
start_height + active_window_tick_length + 1,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query for the active set at various heights
|
// Query for the active set at various heights
|
||||||
@ -932,21 +896,9 @@ pub mod tests {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Create a vote account
|
// Create a vote account and push a vote
|
||||||
new_vote_account(
|
let tick_height = (i + 2) * active_window_tick_length - 1;
|
||||||
&new_validator,
|
new_vote_account_with_vote(&new_validator, &voting_keypair, &bank, 1, tick_height);
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
num_vote_account_tokens as u64,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
push_vote(
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
(i + 2) * active_window_tick_length - 1,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate schedule every active_window_tick_length entries and check that
|
// Generate schedule every active_window_tick_length entries and check that
|
||||||
@ -1006,18 +958,9 @@ pub mod tests {
|
|||||||
// Check that a node that votes twice in a row will get included in the active
|
// Check that a node that votes twice in a row will get included in the active
|
||||||
// window
|
// window
|
||||||
|
|
||||||
let voting_keypair = Keypair::new();
|
|
||||||
// Create a vote account
|
// Create a vote account
|
||||||
new_vote_account(
|
let voting_keypair = Keypair::new();
|
||||||
&leader_keypair,
|
new_vote_account_with_vote(&leader_keypair, &voting_keypair, &bank, 1, 1);
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
1,
|
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Vote at tick_height 1
|
|
||||||
push_vote(&voting_keypair, &bank, 1, genesis_block.last_id());
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut leader_scheduler = leader_scheduler.write().unwrap();
|
let mut leader_scheduler = leader_scheduler.write().unwrap();
|
||||||
@ -1029,7 +972,7 @@ pub mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Vote at tick_height 2
|
// Vote at tick_height 2
|
||||||
push_vote(&voting_keypair, &bank, 2, genesis_block.last_id());
|
push_vote(&voting_keypair, &bank, 2);
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut leader_scheduler = leader_scheduler.write().unwrap();
|
let mut leader_scheduler = leader_scheduler.write().unwrap();
|
||||||
@ -1203,21 +1146,14 @@ pub mod tests {
|
|||||||
if add_validator {
|
if add_validator {
|
||||||
bank.transfer(5, &mint_keypair, validator_id, last_id)
|
bank.transfer(5, &mint_keypair, validator_id, last_id)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Create a vote account
|
|
||||||
let voting_keypair = VotingKeypair::new_local(&validator_keypair);
|
// Create a vote account and push a vote
|
||||||
new_vote_account(
|
new_vote_account_with_vote(
|
||||||
&validator_keypair,
|
&validator_keypair,
|
||||||
&voting_keypair,
|
&Keypair::new(),
|
||||||
&bank,
|
&bank,
|
||||||
1,
|
1,
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
push_vote(
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
initial_vote_height,
|
initial_vote_height,
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1238,22 +1174,14 @@ pub mod tests {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Create a vote account
|
// Create a vote account and push a vote to add the leader to the active set
|
||||||
let voting_keypair = VotingKeypair::new_local(&bootstrap_leader_keypair);
|
let voting_keypair = Keypair::new();
|
||||||
new_vote_account(
|
new_vote_account_with_vote(
|
||||||
&bootstrap_leader_keypair,
|
&bootstrap_leader_keypair,
|
||||||
&voting_keypair,
|
&voting_keypair,
|
||||||
&bank,
|
&bank,
|
||||||
vote_account_tokens as u64,
|
vote_account_tokens as u64,
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add leader to the active set
|
|
||||||
push_vote(
|
|
||||||
&voting_keypair,
|
|
||||||
&bank,
|
|
||||||
initial_vote_height,
|
initial_vote_height,
|
||||||
genesis_block.last_id(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let leader_scheduler_config =
|
let leader_scheduler_config =
|
||||||
|
Reference in New Issue
Block a user