pass Pubkeys as refs, copy only where values needed (#3213)

* pass Pubkeys as refs, copy only where values needed

* Pubkey is pervasive

* fixup
This commit is contained in:
Rob Walker
2019-03-09 19:28:43 -08:00
committed by GitHub
parent ac226c3e14
commit 195a880576
89 changed files with 864 additions and 828 deletions

View File

@ -32,31 +32,34 @@ pub enum VoteInstruction {
}
impl VoteInstruction {
pub fn new_clear_credits(vote_id: Pubkey) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::ClearCredits, vec![(vote_id, true)])
pub fn new_clear_credits(vote_id: &Pubkey) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::ClearCredits, vec![(*vote_id, true)])
}
pub fn new_delegate_stake(vote_id: Pubkey, delegate_id: Pubkey) -> BuilderInstruction {
pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> BuilderInstruction {
BuilderInstruction::new(
id(),
&VoteInstruction::DelegateStake(delegate_id),
vec![(vote_id, true)],
&VoteInstruction::DelegateStake(*delegate_id),
vec![(*vote_id, true)],
)
}
pub fn new_authorize_voter(vote_id: Pubkey, authorized_voter_id: Pubkey) -> BuilderInstruction {
pub fn new_authorize_voter(
vote_id: &Pubkey,
authorized_voter_id: &Pubkey,
) -> BuilderInstruction {
BuilderInstruction::new(
id(),
&VoteInstruction::AuthorizeVoter(authorized_voter_id),
vec![(vote_id, true)],
&VoteInstruction::AuthorizeVoter(*authorized_voter_id),
vec![(*vote_id, true)],
)
}
pub fn new_initialize_account(vote_id: Pubkey) -> BuilderInstruction {
pub fn new_initialize_account(vote_id: &Pubkey) -> BuilderInstruction {
BuilderInstruction::new(
id(),
&VoteInstruction::InitializeAccount,
vec![(vote_id, false)],
vec![(*vote_id, false)],
)
}
pub fn new_vote(vote_id: Pubkey, vote: Vote) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::Vote(vote), vec![(vote_id, true)])
pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::Vote(vote), vec![(*vote_id, true)])
}
}

View File

@ -51,14 +51,14 @@ pub struct VoteState {
}
impl VoteState {
pub fn new(staker_id: Pubkey) -> Self {
pub fn new(staker_id: &Pubkey) -> Self {
let votes = VecDeque::new();
let credits = 0;
let root_slot = None;
Self {
votes,
delegate_id: staker_id,
authorized_voter_id: staker_id,
delegate_id: *staker_id,
authorized_voter_id: *staker_id,
credits,
root_slot,
}
@ -151,7 +151,7 @@ impl VoteState {
pub fn delegate_stake(
keyed_accounts: &mut [KeyedAccount],
node_id: Pubkey,
node_id: &Pubkey,
) -> Result<(), ProgramError> {
if !check_id(&keyed_accounts[0].account.owner) {
error!("account[0] is not assigned to the VOTE_PROGRAM");
@ -165,7 +165,7 @@ pub fn delegate_stake(
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata);
if let Ok(mut vote_state) = vote_state {
vote_state.delegate_id = node_id;
vote_state.delegate_id = *node_id;
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
} else {
error!("account[0] does not valid userdata");
@ -180,7 +180,7 @@ pub fn delegate_stake(
/// voter. The default voter is the owner of the vote account's pubkey.
pub fn authorize_voter(
keyed_accounts: &mut [KeyedAccount],
voter_id: Pubkey,
voter_id: &Pubkey,
) -> Result<(), ProgramError> {
if !check_id(&keyed_accounts[0].account.owner) {
error!("account[0] is not assigned to the VOTE_PROGRAM");
@ -194,7 +194,7 @@ pub fn authorize_voter(
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata);
if let Ok(mut vote_state) = vote_state {
vote_state.authorized_voter_id = voter_id;
vote_state.authorized_voter_id = *voter_id;
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
} else {
error!("account[0] does not valid userdata");
@ -217,7 +217,7 @@ pub fn initialize_account(keyed_accounts: &mut [KeyedAccount]) -> Result<(), Pro
let vote_state = VoteState::deserialize(&keyed_accounts[0].account.userdata);
if let Ok(vote_state) = vote_state {
if vote_state.delegate_id == Pubkey::default() {
let vote_state = VoteState::new(*staker_id);
let vote_state = VoteState::new(staker_id);
vote_state.serialize(&mut keyed_accounts[0].account.userdata)?;
} else {
error!("account[0] userdata already initialized");
@ -274,7 +274,7 @@ pub fn clear_credits(keyed_accounts: &mut [KeyedAccount]) -> Result<(), ProgramE
pub fn create_vote_account(lamports: u64) -> Account {
let space = VoteState::max_size();
Account::new(lamports, space, id())
Account::new(lamports, space, &id())
}
pub fn initialize_and_deserialize(
@ -309,7 +309,7 @@ mod tests {
let mut vote_account = create_vote_account(100);
let bogus_account_id = Keypair::new().pubkey();
let mut bogus_account = Account::new(100, 0, id());
let mut bogus_account = Account::new(100, 0, &id());
let mut keyed_accounts = [KeyedAccount::new(
&bogus_account_id,
@ -385,7 +385,7 @@ mod tests {
#[test]
fn test_vote_lockout() {
let voter_id = Keypair::new().pubkey();
let mut vote_state = VoteState::new(voter_id);
let mut vote_state = VoteState::new(&voter_id);
for i in 0..(MAX_LOCKOUT_HISTORY + 1) {
vote_state.process_vote(Vote::new((INITIAL_LOCKOUT as usize * i) as u64));
@ -415,7 +415,7 @@ mod tests {
#[test]
fn test_vote_double_lockout_after_expiration() {
let voter_id = Keypair::new().pubkey();
let mut vote_state = VoteState::new(voter_id);
let mut vote_state = VoteState::new(&voter_id);
for i in 0..3 {
let vote = Vote::new(i as u64);
@ -441,7 +441,7 @@ mod tests {
#[test]
fn test_vote_credits() {
let voter_id = Keypair::new().pubkey();
let mut vote_state = VoteState::new(voter_id);
let mut vote_state = VoteState::new(&voter_id);
for i in 0..MAX_LOCKOUT_HISTORY {
vote_state.process_vote(Vote::new(i as u64));

View File

@ -15,7 +15,7 @@ pub struct VoteTransaction {}
impl VoteTransaction {
pub fn new_vote<T: KeypairUtil>(
staking_account: Pubkey,
staking_account: &Pubkey,
authorized_voter_keypair: &T,
slot: u64,
recent_blockhash: Hash,
@ -30,7 +30,7 @@ impl VoteTransaction {
/// Fund or create the staking account with lamports
pub fn new_account(
from_keypair: &Keypair,
staker_id: Pubkey,
staker_id: &Pubkey,
recent_blockhash: Hash,
lamports: u64,
fee: u64,
@ -39,11 +39,11 @@ impl VoteTransaction {
let space = VoteState::max_size() as u64;
TransactionBuilder::new(fee)
.push(SystemInstruction::new_program_account(
from_id,
&from_id,
staker_id,
lamports,
space,
id(),
&id(),
))
.push(VoteInstruction::new_initialize_account(staker_id))
.sign(&[from_keypair], recent_blockhash)
@ -53,7 +53,7 @@ impl VoteTransaction {
pub fn new_account_with_delegate(
from_keypair: &Keypair,
voter_keypair: &Keypair,
delegate_id: Pubkey,
delegate_id: &Pubkey,
recent_blockhash: Hash,
lamports: u64,
fee: u64,
@ -63,14 +63,14 @@ impl VoteTransaction {
let space = VoteState::max_size() as u64;
TransactionBuilder::new(fee)
.push(SystemInstruction::new_program_account(
from_id,
voter_id,
&from_id,
&voter_id,
lamports,
space,
id(),
&id(),
))
.push(VoteInstruction::new_initialize_account(voter_id))
.push(VoteInstruction::new_delegate_stake(voter_id, delegate_id))
.push(VoteInstruction::new_initialize_account(&voter_id))
.push(VoteInstruction::new_delegate_stake(&voter_id, &delegate_id))
.sign(&[from_keypair, voter_keypair], recent_blockhash)
}
@ -78,12 +78,12 @@ impl VoteTransaction {
pub fn new_authorize_voter(
vote_keypair: &Keypair,
recent_blockhash: Hash,
authorized_voter_id: Pubkey,
authorized_voter_id: &Pubkey,
fee: u64,
) -> Transaction {
TransactionBuilder::new(fee)
.push(VoteInstruction::new_authorize_voter(
vote_keypair.pubkey(),
&vote_keypair.pubkey(),
authorized_voter_id,
))
.sign(&[vote_keypair], recent_blockhash)
@ -93,12 +93,12 @@ impl VoteTransaction {
pub fn delegate_vote_account<T: KeypairUtil>(
vote_keypair: &T,
recent_blockhash: Hash,
node_id: Pubkey,
node_id: &Pubkey,
fee: u64,
) -> Transaction {
TransactionBuilder::new(fee)
.push(VoteInstruction::new_delegate_stake(
vote_keypair.pubkey(),
&vote_keypair.pubkey(),
node_id,
))
.sign(&[vote_keypair], recent_blockhash)
@ -133,7 +133,7 @@ mod tests {
let slot = 1;
let recent_blockhash = Hash::default();
let transaction =
VoteTransaction::new_vote(keypair.pubkey(), &keypair, slot, recent_blockhash, 0);
VoteTransaction::new_vote(&keypair.pubkey(), &keypair, slot, recent_blockhash, 0);
assert_eq!(
VoteTransaction::get_votes(&transaction),
vec![(keypair.pubkey(), Vote::new(slot), recent_blockhash)]