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

@ -40,11 +40,11 @@ impl fmt::Debug for Account {
impl Account {
// TODO do we want to add executable and leader_owner even though they should always be false/default?
pub fn new(lamports: u64, space: usize, owner: Pubkey) -> Account {
pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Account {
Account {
lamports,
userdata: vec![0u8; space],
owner,
owner: *owner,
executable: false,
}
}

View File

@ -32,19 +32,23 @@ impl GenesisBlock {
let lamports = lamports
.checked_add(BOOTSTRAP_LEADER_LAMPORTS)
.unwrap_or(lamports);
Self::new_with_leader(lamports, Keypair::new().pubkey(), BOOTSTRAP_LEADER_LAMPORTS)
Self::new_with_leader(
lamports,
&Keypair::new().pubkey(),
BOOTSTRAP_LEADER_LAMPORTS,
)
}
pub fn new_with_leader(
lamports: u64,
bootstrap_leader_id: Pubkey,
bootstrap_leader_id: &Pubkey,
bootstrap_leader_lamports: u64,
) -> (Self, Keypair) {
let mint_keypair = Keypair::new();
let bootstrap_leader_vote_account_keypair = Keypair::new();
(
Self {
bootstrap_leader_id,
bootstrap_leader_id: *bootstrap_leader_id,
bootstrap_leader_lamports,
bootstrap_leader_vote_account_id: bootstrap_leader_vote_account_keypair.pubkey(),
mint_id: mint_keypair.pubkey(),
@ -97,7 +101,7 @@ mod tests {
fn test_genesis_block_new_with_leader() {
let leader_keypair = Keypair::new();
let (genesis_block, mint) =
GenesisBlock::new_with_leader(20_000, leader_keypair.pubkey(), 123);
GenesisBlock::new_with_leader(20_000, &leader_keypair.pubkey(), 123);
assert_eq!(genesis_block.lamports, 20_000);
assert_eq!(genesis_block.mint_id, mint.pubkey());

View File

@ -11,7 +11,7 @@ pub struct LoaderTransaction {}
impl LoaderTransaction {
pub fn new_write(
from_keypair: &Keypair,
loader: Pubkey,
loader: &Pubkey,
offset: u32,
bytes: Vec<u8>,
recent_blockhash: Hash,
@ -30,7 +30,7 @@ impl LoaderTransaction {
pub fn new_finalize(
from_keypair: &Keypair,
loader: Pubkey,
loader: &Pubkey,
recent_blockhash: Hash,
fee: u64,
) -> Transaction {

View File

@ -26,28 +26,28 @@ pub enum SystemInstruction {
impl SystemInstruction {
pub fn new_program_account(
from_id: Pubkey,
to_id: Pubkey,
from_id: &Pubkey,
to_id: &Pubkey,
lamports: u64,
space: u64,
program_id: Pubkey,
program_id: &Pubkey,
) -> BuilderInstruction {
BuilderInstruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
lamports,
space,
program_id,
program_id: *program_id,
},
vec![(from_id, true), (to_id, false)],
vec![(*from_id, true), (*to_id, false)],
)
}
pub fn new_move(from_id: Pubkey, to_id: Pubkey, lamports: u64) -> BuilderInstruction {
pub fn new_move(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> BuilderInstruction {
BuilderInstruction::new(
system_program::id(),
&SystemInstruction::Move { lamports },
vec![(from_id, true), (to_id, false)],
vec![(*from_id, true), (*to_id, false)],
)
}
}

View File

@ -13,22 +13,22 @@ impl SystemTransaction {
/// Create and sign new SystemInstruction::CreateAccount transaction
pub fn new_program_account(
from_keypair: &Keypair,
to: Pubkey,
to: &Pubkey,
recent_blockhash: Hash,
lamports: u64,
space: u64,
program_id: Pubkey,
program_id: &Pubkey,
fee: u64,
) -> Transaction {
let create = SystemInstruction::CreateAccount {
lamports, //TODO, the lamports to allocate might need to be higher then 0 in the future
space,
program_id,
program_id: *program_id,
};
Transaction::new(
from_keypair,
&[to],
system_program::id(),
&[*to],
&system_program::id(),
&create,
recent_blockhash,
fee,
@ -38,7 +38,7 @@ impl SystemTransaction {
/// Create and sign a transaction to create a system account
pub fn new_account(
from_keypair: &Keypair,
to: Pubkey,
to: &Pubkey,
lamports: u64,
recent_blockhash: Hash,
fee: u64,
@ -50,7 +50,7 @@ impl SystemTransaction {
recent_blockhash,
lamports,
0,
program_id,
&program_id,
fee,
)
}
@ -58,14 +58,16 @@ impl SystemTransaction {
pub fn new_assign(
from_keypair: &Keypair,
recent_blockhash: Hash,
program_id: Pubkey,
program_id: &Pubkey,
fee: u64,
) -> Transaction {
let assign = SystemInstruction::Assign { program_id };
let assign = SystemInstruction::Assign {
program_id: *program_id,
};
Transaction::new(
from_keypair,
&[],
system_program::id(),
&system_program::id(),
&assign,
recent_blockhash,
fee,
@ -74,7 +76,7 @@ impl SystemTransaction {
/// Create and sign new SystemInstruction::Move transaction
pub fn new_move(
from_keypair: &Keypair,
to: Pubkey,
to: &Pubkey,
lamports: u64,
recent_blockhash: Hash,
fee: u64,
@ -82,8 +84,8 @@ impl SystemTransaction {
let move_lamports = SystemInstruction::Move { lamports };
Transaction::new(
from_keypair,
&[to],
system_program::id(),
&[*to],
&system_program::id(),
&move_lamports,
recent_blockhash,
fee,

View File

@ -99,12 +99,12 @@ impl Transaction {
pub fn new<S: Serialize, T: KeypairUtil>(
from_keypair: &T,
transaction_keys: &[Pubkey],
program_id: Pubkey,
program_id: &Pubkey,
userdata: &S,
recent_blockhash: Hash,
fee: u64,
) -> Self {
let program_ids = vec![program_id];
let program_ids = vec![*program_id];
let accounts = (0..=transaction_keys.len() as u8).collect();
let instructions = vec![Instruction::new(0, userdata, accounts)];
Self::new_with_instructions(
@ -119,12 +119,12 @@ impl Transaction {
pub fn new_unsigned<T: Serialize>(
from_pubkey: &Pubkey,
transaction_keys: &[Pubkey],
program_id: Pubkey,
program_id: &Pubkey,
userdata: &T,
recent_blockhash: Hash,
fee: u64,
) -> Self {
let program_ids = vec![program_id];
let program_ids = vec![*program_id];
let accounts = (0..=transaction_keys.len() as u8).collect();
let instructions = vec![Instruction::new(0, userdata, accounts)];
let mut keys = vec![*from_pubkey];
@ -492,7 +492,7 @@ mod tests {
let tx = Transaction::new(
&keypair,
&[keypair.pubkey(), to],
program_id,
&program_id,
&(1u8, 2u8, 3u8),
Hash::default(),
99,
@ -511,7 +511,7 @@ mod tests {
let tx = Transaction::new(
&keypair,
&[keypair.pubkey(), to],
program_id,
&program_id,
&(1u8, 2u8, 3u8),
Hash::default(),
99,
@ -557,7 +557,7 @@ mod tests {
let tx = Transaction::new(
&keypair,
&[keypair.pubkey(), to],
program_id,
&program_id,
&(1u8, 2u8, 3u8),
Hash::default(),
99,

View File

@ -8,8 +8,8 @@ use itertools::Itertools;
pub type BuilderInstruction = Instruction<Pubkey, (Pubkey, bool)>;
fn position(keys: &[Pubkey], key: Pubkey) -> u8 {
keys.iter().position(|&k| k == key).unwrap() as u8
fn position(keys: &[Pubkey], key: &Pubkey) -> u8 {
keys.iter().position(|k| k == key).unwrap() as u8
}
fn compile_instruction(
@ -17,13 +17,9 @@ fn compile_instruction(
keys: &[Pubkey],
program_ids: &[Pubkey],
) -> Instruction<u8, u8> {
let accounts: Vec<_> = ix
.accounts
.iter()
.map(|&(k, _)| position(keys, k))
.collect();
let accounts: Vec<_> = ix.accounts.iter().map(|(k, _)| position(keys, k)).collect();
Instruction {
program_ids_index: position(program_ids, ix.program_ids_index),
program_ids_index: position(program_ids, &ix.program_ids_index),
userdata: ix.userdata.clone(),
accounts,
}