Make AccountMeta a traditional struct instead of a tuple struct

This commit is contained in:
Greg Fitzgerald 2019-03-19 15:25:48 -06:00
parent a4652a9aaf
commit 94b5835738
10 changed files with 82 additions and 54 deletions

View File

@ -31,9 +31,9 @@ impl BudgetInstruction {
pub fn new_initialize_account(contract: &Pubkey, expr: BudgetExpr) -> Instruction { pub fn new_initialize_account(contract: &Pubkey, expr: BudgetExpr) -> Instruction {
let mut keys = vec![]; let mut keys = vec![];
if let BudgetExpr::Pay(payment) = &expr { if let BudgetExpr::Pay(payment) = &expr {
keys.push(AccountMeta(payment.to, false)); keys.push(AccountMeta::new(payment.to, false));
} }
keys.push(AccountMeta(*contract, false)); keys.push(AccountMeta::new(*contract, false));
Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys) Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys)
} }
@ -43,18 +43,24 @@ impl BudgetInstruction {
to: &Pubkey, to: &Pubkey,
dt: DateTime<Utc>, dt: DateTime<Utc>,
) -> Instruction { ) -> Instruction {
let mut keys = vec![AccountMeta(*from, true), AccountMeta(*contract, false)]; let mut account_metas = vec![
AccountMeta::new(*from, true),
AccountMeta::new(*contract, false),
];
if from != to { if from != to {
keys.push(AccountMeta(*to, false)); account_metas.push(AccountMeta::new(*to, false));
} }
Instruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), keys) Instruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), account_metas)
} }
pub fn new_apply_signature(from: &Pubkey, contract: &Pubkey, to: &Pubkey) -> Instruction { pub fn new_apply_signature(from: &Pubkey, contract: &Pubkey, to: &Pubkey) -> Instruction {
let mut keys = vec![AccountMeta(*from, true), AccountMeta(*contract, false)]; let mut account_metas = vec![
AccountMeta::new(*from, true),
AccountMeta::new(*contract, false),
];
if from != to { if from != to {
keys.push(AccountMeta(*to, false)); account_metas.push(AccountMeta::new(*to, false));
} }
Instruction::new(id(), &BudgetInstruction::ApplySignature, keys) Instruction::new(id(), &BudgetInstruction::ApplySignature, account_metas)
} }
} }

View File

@ -29,8 +29,8 @@ impl ConfigInstruction {
data: &T, data: &T,
) -> Instruction { ) -> Instruction {
let account_metas = vec![ let account_metas = vec![
AccountMeta(*from_account_pubkey, true), AccountMeta::new(*from_account_pubkey, true),
AccountMeta(*config_account_pubkey, true), AccountMeta::new(*config_account_pubkey, true),
]; ];
Instruction::new(id(), data, account_metas) Instruction::new(id(), data, account_metas)
} }

View File

@ -10,7 +10,10 @@ pub enum RewardsInstruction {
impl RewardsInstruction { impl RewardsInstruction {
pub fn new_redeem_vote_credits(vote_id: &Pubkey, rewards_id: &Pubkey) -> Instruction { pub fn new_redeem_vote_credits(vote_id: &Pubkey, rewards_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta(*vote_id, true), AccountMeta(*rewards_id, false)]; let account_metas = vec![
AccountMeta::new(*vote_id, true),
AccountMeta::new(*rewards_id, false),
];
Instruction::new(id(), &RewardsInstruction::RedeemVoteCredits, account_metas) Instruction::new(id(), &RewardsInstruction::RedeemVoteCredits, account_metas)
} }
} }

View File

@ -114,7 +114,7 @@ fn test_vote_via_bank_with_no_signature() {
let vote_ix = Instruction::new( let vote_ix = Instruction::new(
solana_vote_api::id(), solana_vote_api::id(),
&VoteInstruction::Vote(Vote::new(0)), &VoteInstruction::Vote(Vote::new(0)),
vec![AccountMeta(vote_id, false)], // <--- attack!! No signature. vec![AccountMeta::new(vote_id, false)], // <--- attack!! No signer required.
); );
// Sneak in an instruction so that the transaction is signed but // Sneak in an instruction so that the transaction is signed but

View File

@ -33,11 +33,11 @@ pub enum VoteInstruction {
impl VoteInstruction { impl VoteInstruction {
pub fn new_clear_credits(vote_id: &Pubkey) -> Instruction { pub fn new_clear_credits(vote_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta(*vote_id, true)]; let account_metas = vec![AccountMeta::new(*vote_id, true)];
Instruction::new(id(), &VoteInstruction::ClearCredits, account_metas) Instruction::new(id(), &VoteInstruction::ClearCredits, account_metas)
} }
pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> Instruction { pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta(*vote_id, true)]; let account_metas = vec![AccountMeta::new(*vote_id, true)];
Instruction::new( Instruction::new(
id(), id(),
&VoteInstruction::DelegateStake(*delegate_id), &VoteInstruction::DelegateStake(*delegate_id),
@ -45,7 +45,7 @@ impl VoteInstruction {
) )
} }
pub fn new_authorize_voter(vote_id: &Pubkey, authorized_voter_id: &Pubkey) -> Instruction { pub fn new_authorize_voter(vote_id: &Pubkey, authorized_voter_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta(*vote_id, true)]; let account_metas = vec![AccountMeta::new(*vote_id, true)];
Instruction::new( Instruction::new(
id(), id(),
&VoteInstruction::AuthorizeVoter(*authorized_voter_id), &VoteInstruction::AuthorizeVoter(*authorized_voter_id),
@ -53,11 +53,11 @@ impl VoteInstruction {
) )
} }
pub fn new_initialize_account(vote_id: &Pubkey) -> Instruction { pub fn new_initialize_account(vote_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta(*vote_id, false)]; let account_metas = vec![AccountMeta::new(*vote_id, false)];
Instruction::new(id(), &VoteInstruction::InitializeAccount, account_metas) Instruction::new(id(), &VoteInstruction::InitializeAccount, account_metas)
} }
pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction { pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction {
let account_metas = vec![AccountMeta(*vote_id, true)]; let account_metas = vec![AccountMeta::new(*vote_id, true)];
Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas) Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas)
} }
} }

View File

@ -84,7 +84,7 @@ mod tests {
SystemInstruction::new_move(&doe_client.pubkey(), &bob_pubkey, 42); SystemInstruction::new_move(&doe_client.pubkey(), &bob_pubkey, 42);
move_instruction move_instruction
.accounts .accounts
.push(AccountMeta(jane_pubkey, true)); .push(AccountMeta::new(jane_pubkey, true));
doe_client.process_instruction(move_instruction).unwrap(); doe_client.process_instruction(move_instruction).unwrap();
assert_eq!(bank.get_balance(&bob_pubkey), 42); assert_eq!(bank.get_balance(&bob_pubkey), 42);

View File

@ -292,8 +292,8 @@ mod tests {
// Erroneously sign transaction with recipient account key // Erroneously sign transaction with recipient account key
// No signature case is tested by bank `test_zero_signatures()` // No signature case is tested by bank `test_zero_signatures()`
let account_metas = vec![ let account_metas = vec![
AccountMeta(alice_pubkey, false), AccountMeta::new(alice_pubkey, false),
AccountMeta(mallory_pubkey, true), AccountMeta::new(mallory_pubkey, true),
]; ];
let malicious_script = Script::new(vec![Instruction::new( let malicious_script = Script::new(vec![Instruction::new(
system_program::id(), system_program::id(),

View File

@ -2,7 +2,7 @@
use crate::hash::Hash; use crate::hash::Hash;
use crate::pubkey::Pubkey; use crate::pubkey::Pubkey;
use crate::transaction::{AccountMeta, CompiledInstruction, Instruction, Transaction}; use crate::transaction::{CompiledInstruction, Instruction, Transaction};
use itertools::Itertools; use itertools::Itertools;
fn position(keys: &[Pubkey], key: &Pubkey) -> u8 { fn position(keys: &[Pubkey], key: &Pubkey) -> u8 {
@ -17,7 +17,7 @@ fn compile_instruction(
let accounts: Vec<_> = ix let accounts: Vec<_> = ix
.accounts .accounts
.iter() .iter()
.map(|AccountMeta(k, _)| position(keys, k)) .map(|account_meta| position(keys, &account_meta.pubkey))
.collect(); .collect();
CompiledInstruction { CompiledInstruction {
@ -56,15 +56,15 @@ impl Script {
.iter() .iter()
.flat_map(|ix| ix.accounts.iter()) .flat_map(|ix| ix.accounts.iter())
.collect(); .collect();
keys_and_signed.sort_by(|x, y| y.1.cmp(&x.1)); keys_and_signed.sort_by(|x, y| y.is_signer.cmp(&x.is_signer));
let mut signed_keys = vec![]; let mut signed_keys = vec![];
let mut unsigned_keys = vec![]; let mut unsigned_keys = vec![];
for AccountMeta(key, signed) in keys_and_signed.into_iter().unique_by(|x| x.0) { for account_meta in keys_and_signed.into_iter().unique_by(|x| x.pubkey) {
if *signed { if account_meta.is_signer {
signed_keys.push(*key); signed_keys.push(account_meta.pubkey);
} else { } else {
unsigned_keys.push(*key); unsigned_keys.push(account_meta.pubkey);
} }
} }
(signed_keys, unsigned_keys) (signed_keys, unsigned_keys)
@ -101,6 +101,7 @@ impl Script {
mod tests { mod tests {
use super::*; use super::*;
use crate::signature::{Keypair, KeypairUtil}; use crate::signature::{Keypair, KeypairUtil};
use crate::transaction::AccountMeta;
#[test] #[test]
fn test_transaction_builder_unique_program_ids() { fn test_transaction_builder_unique_program_ids() {
@ -144,8 +145,8 @@ mod tests {
let program_id = Pubkey::default(); let program_id = Pubkey::default();
let id0 = Pubkey::default(); let id0 = Pubkey::default();
let keys = Script::new(vec![ let keys = Script::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id0], vec![])); assert_eq!(keys, (vec![id0], vec![]));
@ -156,8 +157,8 @@ mod tests {
let program_id = Pubkey::default(); let program_id = Pubkey::default();
let id0 = Pubkey::default(); let id0 = Pubkey::default();
let keys = Script::new(vec![ let keys = Script::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id0], vec![])); assert_eq!(keys, (vec![id0], vec![]));
@ -169,8 +170,8 @@ mod tests {
let id0 = Keypair::new().pubkey(); let id0 = Keypair::new().pubkey();
let id1 = Pubkey::default(); // Key less than id0 let id1 = Pubkey::default(); // Key less than id0
let keys = Script::new(vec![ let keys = Script::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta(id1, false)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id1, false)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![], vec![id0, id1])); assert_eq!(keys, (vec![], vec![id0, id1]));
@ -182,9 +183,9 @@ mod tests {
let id0 = Pubkey::default(); let id0 = Pubkey::default();
let id1 = Keypair::new().pubkey(); let id1 = Keypair::new().pubkey();
let keys = Script::new(vec![ let keys = Script::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta(id1, false)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id1, false)]),
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id0], vec![id1])); assert_eq!(keys, (vec![id0], vec![id1]));
@ -196,8 +197,8 @@ mod tests {
let id0 = Pubkey::default(); let id0 = Pubkey::default();
let id1 = Keypair::new().pubkey(); let id1 = Keypair::new().pubkey();
let keys = Script::new(vec![ let keys = Script::new(vec![
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id, &0, vec![AccountMeta(id1, true)]), Instruction::new(program_id, &0, vec![AccountMeta::new(id1, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id1], vec![id0])); assert_eq!(keys, (vec![id1], vec![id0]));
@ -208,11 +209,11 @@ mod tests {
fn test_transaction_builder_signed_keys_len() { fn test_transaction_builder_signed_keys_len() {
let program_id = Pubkey::default(); let program_id = Pubkey::default();
let id0 = Pubkey::default(); let id0 = Pubkey::default();
let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]); let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]);
let tx = Script::new(vec![ix]).compile(); let tx = Script::new(vec![ix]).compile();
assert_eq!(tx.signatures.capacity(), 0); assert_eq!(tx.signatures.capacity(), 0);
let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]); let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]);
let tx = Script::new(vec![ix]).compile(); let tx = Script::new(vec![ix]).compile();
assert_eq!(tx.signatures.capacity(), 1); assert_eq!(tx.signatures.capacity(), 1);
} }
@ -225,9 +226,9 @@ mod tests {
let keypair1 = Keypair::new(); let keypair1 = Keypair::new();
let id1 = keypair1.pubkey(); let id1 = keypair1.pubkey();
let tx = Script::new(vec![ let tx = Script::new(vec![
Instruction::new(program_id0, &0, vec![AccountMeta(id0, false)]), Instruction::new(program_id0, &0, vec![AccountMeta::new(id0, false)]),
Instruction::new(program_id1, &0, vec![AccountMeta(id1, true)]), Instruction::new(program_id1, &0, vec![AccountMeta::new(id1, true)]),
Instruction::new(program_id0, &0, vec![AccountMeta(id1, false)]), Instruction::new(program_id0, &0, vec![AccountMeta::new(id1, false)]),
]) ])
.compile(); .compile();
assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![1])); assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![1]));

View File

@ -39,6 +39,10 @@ impl SystemInstruction {
space: u64, space: u64,
program_id: &Pubkey, program_id: &Pubkey,
) -> Instruction { ) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new( Instruction::new(
system_program::id(), system_program::id(),
&SystemInstruction::CreateAccount { &SystemInstruction::CreateAccount {
@ -46,15 +50,19 @@ impl SystemInstruction {
space, space,
program_id: *program_id, program_id: *program_id,
}, },
vec![AccountMeta(*from_id, true), AccountMeta(*to_id, false)], account_metas,
) )
} }
pub fn new_move(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction { pub fn new_move(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new( Instruction::new(
system_program::id(), system_program::id(),
&SystemInstruction::Move { lamports }, &SystemInstruction::Move { lamports },
vec![AccountMeta(*from_id, true), AccountMeta(*to_id, false)], account_metas,
) )
} }
} }

View File

@ -100,9 +100,19 @@ impl<P, Q> GenericInstruction<P, Q> {
} }
} }
/// An account's pubkey and a Boolean that is true if an Instruciton /// Account metadata used to define Instructions
/// requires a Transaction signature corresponding to this key. pub struct AccountMeta {
pub struct AccountMeta(pub Pubkey, pub bool); /// An account's public key
pub pubkey: Pubkey,
/// True if an Instruciton requires a Transaction signature matching `pubkey`.
pub is_signer: bool,
}
impl AccountMeta {
pub fn new(pubkey: Pubkey, is_signer: bool) -> Self {
Self { pubkey, is_signer }
}
}
pub type Instruction = GenericInstruction<Pubkey, AccountMeta>; pub type Instruction = GenericInstruction<Pubkey, AccountMeta>;
pub type CompiledInstruction = GenericInstruction<u8, u8>; pub type CompiledInstruction = GenericInstruction<u8, u8>;
@ -213,11 +223,11 @@ impl Transaction {
recent_blockhash: Hash, recent_blockhash: Hash,
fee: u64, fee: u64,
) -> Self { ) -> Self {
let mut account_keys = vec![AccountMeta(*from_pubkey, true)]; let mut account_metas = vec![AccountMeta::new(*from_pubkey, true)];
for pubkey in transaction_keys { for pubkey in transaction_keys {
account_keys.push(AccountMeta(*pubkey, false)); account_metas.push(AccountMeta::new(*pubkey, false));
} }
let instruction = Instruction::new(*program_id, data, account_keys); let instruction = Instruction::new(*program_id, data, account_metas);
let mut transaction = Self::new(vec![instruction]); let mut transaction = Self::new(vec![instruction]);
transaction.fee = fee; transaction.fee = fee;
transaction.recent_blockhash = recent_blockhash; transaction.recent_blockhash = recent_blockhash;
@ -709,7 +719,7 @@ mod tests {
let program_id = Pubkey::default(); let program_id = Pubkey::default();
let keypair0 = Keypair::new(); let keypair0 = Keypair::new();
let id0 = keypair0.pubkey(); let id0 = keypair0.pubkey();
let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]); let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]);
Transaction::new(vec![ix]).sign(&Vec::<&Keypair>::new(), Hash::default()); Transaction::new(vec![ix]).sign(&Vec::<&Keypair>::new(), Hash::default());
} }
@ -719,7 +729,7 @@ mod tests {
let program_id = Pubkey::default(); let program_id = Pubkey::default();
let keypair0 = Keypair::new(); let keypair0 = Keypair::new();
let wrong_id = Pubkey::default(); let wrong_id = Pubkey::default();
let ix = Instruction::new(program_id, &0, vec![AccountMeta(wrong_id, true)]); let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(wrong_id, true)]);
Transaction::new(vec![ix]).sign(&[&keypair0], Hash::default()); Transaction::new(vec![ix]).sign(&[&keypair0], Hash::default());
} }
@ -728,7 +738,7 @@ mod tests {
let program_id = Pubkey::default(); let program_id = Pubkey::default();
let keypair0 = Keypair::new(); let keypair0 = Keypair::new();
let id0 = keypair0.pubkey(); let id0 = keypair0.pubkey();
let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]); let ix = Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]);
let mut tx = Transaction::new(vec![ix]); let mut tx = Transaction::new(vec![ix]);
tx.sign(&[&keypair0], Hash::default()); tx.sign(&[&keypair0], Hash::default());
assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![0])); assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![0]));