Label tuple with AccountMeta

This commit is contained in:
Greg Fitzgerald
2019-03-19 13:03:20 -06:00
parent 7246d72f03
commit a4652a9aaf
10 changed files with 83 additions and 68 deletions

View File

@ -3,7 +3,7 @@ use crate::id;
use chrono::prelude::{DateTime, Utc}; use chrono::prelude::{DateTime, Utc};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction::Instruction; use solana_sdk::transaction::{AccountMeta, Instruction};
/// A smart contract. /// A smart contract.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
@ -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((payment.to, false)); keys.push(AccountMeta(payment.to, false));
} }
keys.push((*contract, false)); keys.push(AccountMeta(*contract, false));
Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys) Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys)
} }
@ -43,17 +43,17 @@ impl BudgetInstruction {
to: &Pubkey, to: &Pubkey,
dt: DateTime<Utc>, dt: DateTime<Utc>,
) -> Instruction { ) -> Instruction {
let mut keys = vec![(*from, true), (*contract, false)]; let mut keys = vec![AccountMeta(*from, true), AccountMeta(*contract, false)];
if from != to { if from != to {
keys.push((*to, false)); keys.push(AccountMeta(*to, false));
} }
Instruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), keys) Instruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), keys)
} }
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![(*from, true), (*contract, false)]; let mut keys = vec![AccountMeta(*from, true), AccountMeta(*contract, false)];
if from != to { if from != to {
keys.push((*to, false)); keys.push(AccountMeta(*to, false));
} }
Instruction::new(id(), &BudgetInstruction::ApplySignature, keys) Instruction::new(id(), &BudgetInstruction::ApplySignature, keys)
} }

View File

@ -2,7 +2,7 @@ use crate::id;
use crate::ConfigState; use crate::ConfigState;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::Instruction; use solana_sdk::transaction::{AccountMeta, Instruction};
pub struct ConfigInstruction {} pub struct ConfigInstruction {}
@ -28,10 +28,10 @@ impl ConfigInstruction {
config_account_pubkey: &Pubkey, config_account_pubkey: &Pubkey,
data: &T, data: &T,
) -> Instruction { ) -> Instruction {
Instruction::new( let account_metas = vec![
id(), AccountMeta(*from_account_pubkey, true),
data, AccountMeta(*config_account_pubkey, true),
vec![(*from_account_pubkey, true), (*config_account_pubkey, true)], ];
) Instruction::new(id(), data, account_metas)
} }
} }

View File

@ -1,7 +1,7 @@
use crate::id; use crate::id;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction::Instruction; use solana_sdk::transaction::{AccountMeta, Instruction};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum RewardsInstruction { pub enum RewardsInstruction {
@ -10,10 +10,7 @@ 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 {
Instruction::new( let account_metas = vec![AccountMeta(*vote_id, true), AccountMeta(*rewards_id, false)];
id(), Instruction::new(id(), &RewardsInstruction::RedeemVoteCredits, account_metas)
&RewardsInstruction::RedeemVoteCredits,
vec![(*vote_id, true), (*rewards_id, false)],
)
} }
} }

View File

@ -4,7 +4,9 @@ use solana_sdk::hash::hash;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::{Instruction, InstructionError, Transaction, TransactionError}; use solana_sdk::transaction::{
AccountMeta, Instruction, InstructionError, Transaction, TransactionError,
};
use solana_vote_api::vote_instruction::{Vote, VoteInstruction}; use solana_vote_api::vote_instruction::{Vote, VoteInstruction};
use solana_vote_api::vote_state::VoteState; use solana_vote_api::vote_state::VoteState;
use solana_vote_api::vote_transaction::VoteTransaction; use solana_vote_api::vote_transaction::VoteTransaction;
@ -112,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![(vote_id, false)], // <--- attack!! No signature. vec![AccountMeta(vote_id, false)], // <--- attack!! No signature.
); );
// Sneak in an instruction so that the transaction is signed but // Sneak in an instruction so that the transaction is signed but

View File

@ -1,7 +1,7 @@
use crate::id; use crate::id;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction::Instruction; use solana_sdk::transaction::{AccountMeta, Instruction};
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Vote { pub struct Vote {
@ -33,30 +33,31 @@ 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 {
Instruction::new(id(), &VoteInstruction::ClearCredits, vec![(*vote_id, true)]) let account_metas = vec![AccountMeta(*vote_id, true)];
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)];
Instruction::new( Instruction::new(
id(), id(),
&VoteInstruction::DelegateStake(*delegate_id), &VoteInstruction::DelegateStake(*delegate_id),
vec![(*vote_id, true)], account_metas,
) )
} }
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)];
Instruction::new( Instruction::new(
id(), id(),
&VoteInstruction::AuthorizeVoter(*authorized_voter_id), &VoteInstruction::AuthorizeVoter(*authorized_voter_id),
vec![(*vote_id, true)], account_metas,
) )
} }
pub fn new_initialize_account(vote_id: &Pubkey) -> Instruction { pub fn new_initialize_account(vote_id: &Pubkey) -> Instruction {
Instruction::new( let account_metas = vec![AccountMeta(*vote_id, false)];
id(), Instruction::new(id(), &VoteInstruction::InitializeAccount, account_metas)
&VoteInstruction::InitializeAccount,
vec![(*vote_id, false)],
)
} }
pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction { pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction {
Instruction::new(id(), &VoteInstruction::Vote(vote), vec![(*vote_id, true)]) let account_metas = vec![AccountMeta(*vote_id, true)];
Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas)
} }
} }

View File

@ -67,6 +67,7 @@ impl<'a> BankClient<'a> {
mod tests { mod tests {
use super::*; use super::*;
use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::transaction::AccountMeta;
#[test] #[test]
fn test_bank_client_new_with_keypairs() { fn test_bank_client_new_with_keypairs() {
@ -81,7 +82,9 @@ mod tests {
let bob_pubkey = Keypair::new().pubkey(); let bob_pubkey = Keypair::new().pubkey();
let mut move_instruction = let mut move_instruction =
SystemInstruction::new_move(&doe_client.pubkey(), &bob_pubkey, 42); SystemInstruction::new_move(&doe_client.pubkey(), &bob_pubkey, 42);
move_instruction.accounts.push((jane_pubkey, true)); move_instruction
.accounts
.push(AccountMeta(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

@ -112,7 +112,7 @@ mod tests {
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_program; use solana_sdk::system_program;
use solana_sdk::transaction::{Instruction, InstructionError, TransactionError}; use solana_sdk::transaction::{AccountMeta, Instruction, InstructionError, TransactionError};
#[test] #[test]
fn test_create_system_account() { fn test_create_system_account() {
@ -291,10 +291,14 @@ 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![
AccountMeta(alice_pubkey, false),
AccountMeta(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(),
&SystemInstruction::Move { lamports: 10 }, &SystemInstruction::Move { lamports: 10 },
vec![(alice_pubkey, false), (mallory_pubkey, true)], account_metas,
)]); )]);
assert_eq!( assert_eq!(
mallory_client.process_script(malicious_script), mallory_client.process_script(malicious_script),

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::{CompiledInstruction, Instruction, Transaction}; use crate::transaction::{AccountMeta, CompiledInstruction, Instruction, Transaction};
use itertools::Itertools; use itertools::Itertools;
fn position(keys: &[Pubkey], key: &Pubkey) -> u8 { fn position(keys: &[Pubkey], key: &Pubkey) -> u8 {
@ -14,7 +14,12 @@ fn compile_instruction(
keys: &[Pubkey], keys: &[Pubkey],
program_ids: &[Pubkey], program_ids: &[Pubkey],
) -> CompiledInstruction { ) -> CompiledInstruction {
let accounts: Vec<_> = ix.accounts.iter().map(|(k, _)| position(keys, k)).collect(); let accounts: Vec<_> = ix
.accounts
.iter()
.map(|AccountMeta(k, _)| position(keys, k))
.collect();
CompiledInstruction { CompiledInstruction {
program_ids_index: position(program_ids, &ix.program_ids_index), program_ids_index: position(program_ids, &ix.program_ids_index),
data: ix.data.clone(), data: ix.data.clone(),
@ -55,7 +60,7 @@ impl Script {
let mut signed_keys = vec![]; let mut signed_keys = vec![];
let mut unsigned_keys = vec![]; let mut unsigned_keys = vec![];
for (key, signed) in keys_and_signed.into_iter().unique_by(|x| x.0) { for AccountMeta(key, signed) in keys_and_signed.into_iter().unique_by(|x| x.0) {
if *signed { if *signed {
signed_keys.push(*key); signed_keys.push(*key);
} else { } else {
@ -139,8 +144,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![(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
Instruction::new(program_id, &0, vec![(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id0], vec![])); assert_eq!(keys, (vec![id0], vec![]));
@ -151,8 +156,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![(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
Instruction::new(program_id, &0, vec![(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id0], vec![])); assert_eq!(keys, (vec![id0], vec![]));
@ -164,8 +169,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![(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
Instruction::new(program_id, &0, vec![(id1, false)]), Instruction::new(program_id, &0, vec![AccountMeta(id1, false)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![], vec![id0, id1])); assert_eq!(keys, (vec![], vec![id0, id1]));
@ -177,9 +182,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![(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
Instruction::new(program_id, &0, vec![(id1, false)]), Instruction::new(program_id, &0, vec![AccountMeta(id1, false)]),
Instruction::new(program_id, &0, vec![(id0, true)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id0], vec![id1])); assert_eq!(keys, (vec![id0], vec![id1]));
@ -191,8 +196,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![(id0, false)]), Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
Instruction::new(program_id, &0, vec![(id1, true)]), Instruction::new(program_id, &0, vec![AccountMeta(id1, true)]),
]) ])
.keys(); .keys();
assert_eq!(keys, (vec![id1], vec![id0])); assert_eq!(keys, (vec![id1], vec![id0]));
@ -203,10 +208,12 @@ 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 tx = Script::new(vec![Instruction::new(program_id, &0, vec![(id0, false)])]).compile(); let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]);
let tx = Script::new(vec![ix]).compile();
assert_eq!(tx.signatures.capacity(), 0); assert_eq!(tx.signatures.capacity(), 0);
let tx = Script::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])]).compile(); let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]);
let tx = Script::new(vec![ix]).compile();
assert_eq!(tx.signatures.capacity(), 1); assert_eq!(tx.signatures.capacity(), 1);
} }
@ -218,9 +225,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![(id0, false)]), Instruction::new(program_id0, &0, vec![AccountMeta(id0, false)]),
Instruction::new(program_id1, &0, vec![(id1, true)]), Instruction::new(program_id1, &0, vec![AccountMeta(id1, true)]),
Instruction::new(program_id0, &0, vec![(id1, false)]), Instruction::new(program_id0, &0, vec![AccountMeta(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

@ -1,6 +1,6 @@
use crate::pubkey::Pubkey; use crate::pubkey::Pubkey;
use crate::system_program; use crate::system_program;
use crate::transaction::Instruction; use crate::transaction::{AccountMeta, Instruction};
#[derive(Serialize, Debug, Clone, PartialEq)] #[derive(Serialize, Debug, Clone, PartialEq)]
pub enum SystemError { pub enum SystemError {
@ -46,7 +46,7 @@ impl SystemInstruction {
space, space,
program_id: *program_id, program_id: *program_id,
}, },
vec![(*from_id, true), (*to_id, false)], vec![AccountMeta(*from_id, true), AccountMeta(*to_id, false)],
) )
} }
@ -54,7 +54,7 @@ impl SystemInstruction {
Instruction::new( Instruction::new(
system_program::id(), system_program::id(),
&SystemInstruction::Move { lamports }, &SystemInstruction::Move { lamports },
vec![(*from_id, true), (*to_id, false)], vec![AccountMeta(*from_id, true), AccountMeta(*to_id, false)],
) )
} }
} }

View File

@ -100,7 +100,11 @@ impl<P, Q> GenericInstruction<P, Q> {
} }
} }
pub type Instruction = GenericInstruction<Pubkey, (Pubkey, bool)>; /// An account's pubkey and a Boolean that is true if an Instruciton
/// requires a Transaction signature corresponding to this key.
pub struct AccountMeta(pub Pubkey, pub bool);
pub type Instruction = GenericInstruction<Pubkey, AccountMeta>;
pub type CompiledInstruction = GenericInstruction<u8, u8>; pub type CompiledInstruction = GenericInstruction<u8, u8>;
impl CompiledInstruction { impl CompiledInstruction {
@ -209,9 +213,9 @@ impl Transaction {
recent_blockhash: Hash, recent_blockhash: Hash,
fee: u64, fee: u64,
) -> Self { ) -> Self {
let mut account_keys = vec![(*from_pubkey, true)]; let mut account_keys = vec![AccountMeta(*from_pubkey, true)];
for pubkey in transaction_keys { for pubkey in transaction_keys {
account_keys.push((*pubkey, false)); account_keys.push(AccountMeta(*pubkey, false));
} }
let instruction = Instruction::new(*program_id, data, account_keys); let instruction = Instruction::new(*program_id, data, account_keys);
let mut transaction = Self::new(vec![instruction]); let mut transaction = Self::new(vec![instruction]);
@ -705,8 +709,8 @@ 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();
Transaction::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])]) let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]);
.sign(&Vec::<&Keypair>::new(), Hash::default()); Transaction::new(vec![ix]).sign(&Vec::<&Keypair>::new(), Hash::default());
} }
#[test] #[test]
@ -715,12 +719,8 @@ 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();
Transaction::new(vec![Instruction::new( let ix = Instruction::new(program_id, &0, vec![AccountMeta(wrong_id, true)]);
program_id, Transaction::new(vec![ix]).sign(&[&keypair0], Hash::default());
&0,
vec![(wrong_id, true)],
)])
.sign(&[&keypair0], Hash::default());
} }
#[test] #[test]
@ -728,7 +728,8 @@ 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 mut tx = Transaction::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])]); let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]);
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]));
} }