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

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

View File

@ -39,6 +39,10 @@ impl SystemInstruction {
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
@ -46,15 +50,19 @@ impl SystemInstruction {
space,
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 {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&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
/// requires a Transaction signature corresponding to this key.
pub struct AccountMeta(pub Pubkey, pub bool);
/// Account metadata used to define Instructions
pub struct AccountMeta {
/// 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 CompiledInstruction = GenericInstruction<u8, u8>;
@ -213,11 +223,11 @@ impl Transaction {
recent_blockhash: Hash,
fee: u64,
) -> 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 {
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]);
transaction.fee = fee;
transaction.recent_blockhash = recent_blockhash;
@ -709,7 +719,7 @@ mod tests {
let program_id = Pubkey::default();
let keypair0 = Keypair::new();
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());
}
@ -719,7 +729,7 @@ mod tests {
let program_id = Pubkey::default();
let keypair0 = Keypair::new();
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());
}
@ -728,7 +738,7 @@ mod tests {
let program_id = Pubkey::default();
let keypair0 = Keypair::new();
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]);
tx.sign(&[&keypair0], Hash::default());
assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![0]));