@ -209,28 +209,6 @@ impl AccountMeta {
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for adding a signer Pubkey to an existing data structure
|
||||
pub trait WithSigner {
|
||||
/// Add a signer Pubkey
|
||||
fn with_signer(self, signer: &Pubkey) -> Self;
|
||||
}
|
||||
|
||||
impl WithSigner for Vec<AccountMeta> {
|
||||
fn with_signer(mut self, signer: &Pubkey) -> Self {
|
||||
for meta in self.iter_mut() {
|
||||
// signer might already appear in parameters
|
||||
if &meta.pubkey == signer {
|
||||
meta.is_signer = true; // found it, we're done
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
// signer wasn't in metas, append it after normal parameters
|
||||
self.push(AccountMeta::new_readonly(*signer, true));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// An instruction to execute a program
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@ -286,36 +264,6 @@ impl CompiledInstruction {
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_account_meta_list_with_signer() {
|
||||
let account_pubkey = Pubkey::new_rand();
|
||||
let signer_pubkey = Pubkey::new_rand();
|
||||
|
||||
let account_meta = AccountMeta::new(account_pubkey, false);
|
||||
let signer_account_meta = AccountMeta::new(signer_pubkey, false);
|
||||
|
||||
let metas = vec![].with_signer(&signer_pubkey);
|
||||
assert_eq!(metas.len(), 1);
|
||||
assert!(metas[0].is_signer);
|
||||
|
||||
let metas = vec![account_meta.clone()].with_signer(&signer_pubkey);
|
||||
assert_eq!(metas.len(), 2);
|
||||
assert!(!metas[0].is_signer);
|
||||
assert!(metas[1].is_signer);
|
||||
assert_eq!(metas[1].pubkey, signer_pubkey);
|
||||
|
||||
let metas = vec![signer_account_meta.clone()].with_signer(&signer_pubkey);
|
||||
assert_eq!(metas.len(), 1);
|
||||
assert!(metas[0].is_signer);
|
||||
assert_eq!(metas[0].pubkey, signer_pubkey);
|
||||
|
||||
let metas = vec![account_meta, signer_account_meta].with_signer(&signer_pubkey);
|
||||
assert_eq!(metas.len(), 2);
|
||||
assert!(!metas[0].is_signer);
|
||||
assert!(metas[1].is_signer);
|
||||
assert_eq!(metas[1].pubkey, signer_pubkey);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_visit_each_account() {
|
||||
let do_work = |accounts: &[u8]| -> (usize, usize) {
|
||||
|
@ -103,11 +103,24 @@ fn get_keys(instructions: &[Instruction], payer: Option<&Pubkey>) -> Instruction
|
||||
keys_and_signed.insert(0, &payer_account_meta);
|
||||
}
|
||||
|
||||
let mut unique_metas: Vec<AccountMeta> = vec![];
|
||||
for account_meta in keys_and_signed {
|
||||
// Promote to writable if a later AccountMeta requires it
|
||||
if let Some(x) = unique_metas
|
||||
.iter_mut()
|
||||
.find(|x| x.pubkey == account_meta.pubkey)
|
||||
{
|
||||
x.is_writable |= account_meta.is_writable;
|
||||
continue;
|
||||
}
|
||||
unique_metas.push(account_meta.clone());
|
||||
}
|
||||
|
||||
let mut signed_keys = vec![];
|
||||
let mut unsigned_keys = vec![];
|
||||
let mut num_readonly_signed_accounts = 0;
|
||||
let mut num_readonly_unsigned_accounts = 0;
|
||||
for account_meta in keys_and_signed.into_iter().unique_by(|x| x.pubkey) {
|
||||
for account_meta in unique_metas {
|
||||
if account_meta.is_signer {
|
||||
signed_keys.push(account_meta.pubkey);
|
||||
if !account_meta.is_writable {
|
||||
@ -426,6 +439,38 @@ mod tests {
|
||||
assert_eq!(keys, InstructionKeys::new(vec![id0], vec![], 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_unique_keys_one_readonly_signed() {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let keys = get_keys(
|
||||
&[
|
||||
Instruction::new(program_id, &0, vec![AccountMeta::new_readonly(id0, true)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, true)]),
|
||||
],
|
||||
None,
|
||||
);
|
||||
|
||||
// Ensure the key is no longer readonly
|
||||
assert_eq!(keys, InstructionKeys::new(vec![id0], vec![], 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_unique_keys_one_readonly_unsigned() {
|
||||
let program_id = Pubkey::default();
|
||||
let id0 = Pubkey::default();
|
||||
let keys = get_keys(
|
||||
&[
|
||||
Instruction::new(program_id, &0, vec![AccountMeta::new_readonly(id0, false)]),
|
||||
Instruction::new(program_id, &0, vec![AccountMeta::new(id0, false)]),
|
||||
],
|
||||
None,
|
||||
);
|
||||
|
||||
// Ensure the key is no longer readonly
|
||||
assert_eq!(keys, InstructionKeys::new(vec![], vec![id0], 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_unique_keys_order_preserved() {
|
||||
let program_id = Pubkey::default();
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
instruction::{AccountMeta, Instruction, WithSigner},
|
||||
instruction::{AccountMeta, Instruction},
|
||||
nonce,
|
||||
program_utils::DecodeError,
|
||||
pubkey::Pubkey,
|
||||
@ -234,8 +234,8 @@ pub fn create_account_with_seed(
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*from_pubkey, true),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
]
|
||||
.with_signer(base);
|
||||
AccountMeta::new_readonly(*base, true),
|
||||
];
|
||||
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
@ -265,7 +265,10 @@ pub fn assign_with_seed(
|
||||
seed: &str,
|
||||
owner: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*address, false)].with_signer(base);
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*address, false),
|
||||
AccountMeta::new_readonly(*base, true),
|
||||
];
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
&SystemInstruction::AssignWithSeed {
|
||||
@ -305,7 +308,10 @@ pub fn allocate_with_seed(
|
||||
space: u64,
|
||||
owner: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*address, false)].with_signer(base);
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*address, false),
|
||||
AccountMeta::new_readonly(*base, true),
|
||||
];
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
&SystemInstruction::AllocateWithSeed {
|
||||
@ -386,8 +392,8 @@ pub fn advance_nonce_account(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey)
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
]
|
||||
.with_signer(authorized_pubkey);
|
||||
AccountMeta::new_readonly(*authorized_pubkey, true),
|
||||
];
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
&SystemInstruction::AdvanceNonceAccount,
|
||||
@ -406,8 +412,8 @@ pub fn withdraw_nonce_account(
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
AccountMeta::new_readonly(recent_blockhashes::id(), false),
|
||||
AccountMeta::new_readonly(rent::id(), false),
|
||||
]
|
||||
.with_signer(authorized_pubkey);
|
||||
AccountMeta::new_readonly(*authorized_pubkey, true),
|
||||
];
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
&SystemInstruction::WithdrawNonceAccount(lamports),
|
||||
@ -420,7 +426,10 @@ pub fn authorize_nonce_account(
|
||||
authorized_pubkey: &Pubkey,
|
||||
new_authority: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![AccountMeta::new(*nonce_pubkey, false)].with_signer(authorized_pubkey);
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*nonce_pubkey, false),
|
||||
AccountMeta::new_readonly(*authorized_pubkey, true),
|
||||
];
|
||||
Instruction::new(
|
||||
system_program::id(),
|
||||
&SystemInstruction::AuthorizeNonceAccount(*new_authority),
|
||||
|
Reference in New Issue
Block a user