Label tuple with AccountMeta
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
|
||||
use crate::hash::Hash;
|
||||
use crate::pubkey::Pubkey;
|
||||
use crate::transaction::{CompiledInstruction, Instruction, Transaction};
|
||||
use crate::transaction::{AccountMeta, CompiledInstruction, Instruction, Transaction};
|
||||
use itertools::Itertools;
|
||||
|
||||
fn position(keys: &[Pubkey], key: &Pubkey) -> u8 {
|
||||
@ -14,7 +14,12 @@ fn compile_instruction(
|
||||
keys: &[Pubkey],
|
||||
program_ids: &[Pubkey],
|
||||
) -> 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 {
|
||||
program_ids_index: position(program_ids, &ix.program_ids_index),
|
||||
data: ix.data.clone(),
|
||||
@ -55,7 +60,7 @@ impl Script {
|
||||
|
||||
let mut signed_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 {
|
||||
signed_keys.push(*key);
|
||||
} else {
|
||||
@ -139,8 +144,8 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
|
||||
])
|
||||
.keys();
|
||||
assert_eq!(keys, (vec![id0], vec![]));
|
||||
@ -151,8 +156,8 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]),
|
||||
])
|
||||
.keys();
|
||||
assert_eq!(keys, (vec![id0], vec![]));
|
||||
@ -164,8 +169,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![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id1, false)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id1, false)]),
|
||||
])
|
||||
.keys();
|
||||
assert_eq!(keys, (vec![], vec![id0, id1]));
|
||||
@ -177,9 +182,9 @@ mod tests {
|
||||
let id0 = Pubkey::default();
|
||||
let id1 = Keypair::new().pubkey();
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id1, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||
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)]),
|
||||
])
|
||||
.keys();
|
||||
assert_eq!(keys, (vec![id0], vec![id1]));
|
||||
@ -191,8 +196,8 @@ mod tests {
|
||||
let id0 = Pubkey::default();
|
||||
let id1 = Keypair::new().pubkey();
|
||||
let keys = Script::new(vec![
|
||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![(id1, true)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta(id1, true)]),
|
||||
])
|
||||
.keys();
|
||||
assert_eq!(keys, (vec![id1], vec![id0]));
|
||||
@ -203,10 +208,12 @@ mod tests {
|
||||
fn test_transaction_builder_signed_keys_len() {
|
||||
let program_id = 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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -218,9 +225,9 @@ mod tests {
|
||||
let keypair1 = Keypair::new();
|
||||
let id1 = keypair1.pubkey();
|
||||
let tx = Script::new(vec![
|
||||
Instruction::new(program_id0, &0, vec![(id0, false)]),
|
||||
Instruction::new(program_id1, &0, vec![(id1, true)]),
|
||||
Instruction::new(program_id0, &0, vec![(id1, false)]),
|
||||
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)]),
|
||||
])
|
||||
.compile();
|
||||
assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![1]));
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::pubkey::Pubkey;
|
||||
use crate::system_program;
|
||||
use crate::transaction::Instruction;
|
||||
use crate::transaction::{AccountMeta, Instruction};
|
||||
|
||||
#[derive(Serialize, Debug, Clone, PartialEq)]
|
||||
pub enum SystemError {
|
||||
@ -46,7 +46,7 @@ impl SystemInstruction {
|
||||
space,
|
||||
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(
|
||||
system_program::id(),
|
||||
&SystemInstruction::Move { lamports },
|
||||
vec![(*from_id, true), (*to_id, false)],
|
||||
vec![AccountMeta(*from_id, true), AccountMeta(*to_id, false)],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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>;
|
||||
|
||||
impl CompiledInstruction {
|
||||
@ -209,9 +213,9 @@ impl Transaction {
|
||||
recent_blockhash: Hash,
|
||||
fee: u64,
|
||||
) -> Self {
|
||||
let mut account_keys = vec![(*from_pubkey, true)];
|
||||
let mut account_keys = vec![AccountMeta(*from_pubkey, true)];
|
||||
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 mut transaction = Self::new(vec![instruction]);
|
||||
@ -705,8 +709,8 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let keypair0 = Keypair::new();
|
||||
let id0 = keypair0.pubkey();
|
||||
Transaction::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])])
|
||||
.sign(&Vec::<&Keypair>::new(), Hash::default());
|
||||
let ix = Instruction::new(program_id, &0, vec![AccountMeta(id0, true)]);
|
||||
Transaction::new(vec![ix]).sign(&Vec::<&Keypair>::new(), Hash::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -715,12 +719,8 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let keypair0 = Keypair::new();
|
||||
let wrong_id = Pubkey::default();
|
||||
Transaction::new(vec![Instruction::new(
|
||||
program_id,
|
||||
&0,
|
||||
vec![(wrong_id, true)],
|
||||
)])
|
||||
.sign(&[&keypair0], Hash::default());
|
||||
let ix = Instruction::new(program_id, &0, vec![AccountMeta(wrong_id, true)]);
|
||||
Transaction::new(vec![ix]).sign(&[&keypair0], Hash::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -728,7 +728,8 @@ mod tests {
|
||||
let program_id = Pubkey::default();
|
||||
let keypair0 = Keypair::new();
|
||||
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());
|
||||
assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![0]));
|
||||
}
|
||||
|
Reference in New Issue
Block a user