|
2 | 2 |
|
3 | 3 | use { |
4 | 4 | crate::keys::KeyMap, |
5 | | - mollusk_svm_error::error::MolluskError, |
| 5 | + mollusk_svm_error::error::{MolluskError, MolluskPanic}, |
6 | 6 | solana_account::{Account, AccountSharedData}, |
7 | 7 | solana_instruction::Instruction, |
8 | 8 | solana_pubkey::Pubkey, |
9 | 9 | solana_transaction_context::{IndexOfAccount, InstructionAccount, TransactionAccount}, |
| 10 | + std::collections::HashMap, |
10 | 11 | }; |
11 | 12 |
|
12 | 13 | // Helper struct to avoid cloning instruction data. |
@@ -47,60 +48,64 @@ pub fn compile_instruction_accounts( |
47 | 48 | .collect() |
48 | 49 | } |
49 | 50 |
|
50 | | -pub fn compile_transaction_accounts_for_instruction( |
| 51 | +pub fn compile_transaction_accounts_for_instruction<'a>( |
51 | 52 | key_map: &KeyMap, |
52 | 53 | instruction: &Instruction, |
53 | | - accounts: &[(Pubkey, Account)], |
| 54 | + accounts: impl Iterator<Item = &'a (Pubkey, Account)>, |
54 | 55 | stub_out_program_account: Option<Box<dyn Fn() -> Account>>, |
55 | 56 | ) -> Vec<TransactionAccount> { |
| 57 | + let len = key_map.len(); |
| 58 | + let mut by_key: HashMap<Pubkey, AccountSharedData> = HashMap::with_capacity(len); |
| 59 | + |
| 60 | + for (key, account) in accounts { |
| 61 | + if key_map.contains_key(key) { |
| 62 | + by_key.insert(*key, AccountSharedData::from(account.clone())); |
| 63 | + } |
| 64 | + } |
| 65 | + |
56 | 66 | key_map |
57 | 67 | .keys() |
58 | 68 | .map(|key| { |
59 | | - let account = accounts |
60 | | - .iter() |
61 | | - .find(|(k, _)| k == key) |
62 | | - .map(|(_, account)| AccountSharedData::from(account.clone())); |
63 | | - |
64 | | - if let Some(account) = account { |
65 | | - (*key, account) |
66 | | - } else if let Some(stub_out_program_account) = &stub_out_program_account { |
| 69 | + if let Some(stub_out_program_account) = &stub_out_program_account { |
67 | 70 | if instruction.program_id == *key { |
68 | | - (*key, stub_out_program_account().into()) |
69 | | - } else { |
70 | | - panic!("{}", MolluskError::AccountMissing(key)) |
| 71 | + return (*key, stub_out_program_account().into()); |
71 | 72 | } |
72 | | - } else { |
73 | | - panic!("{}", MolluskError::AccountMissing(key)) |
74 | 73 | } |
| 74 | + let account = by_key |
| 75 | + .remove(key) |
| 76 | + .or_panic_with(MolluskError::AccountMissing(key)); |
| 77 | + (*key, account) |
75 | 78 | }) |
76 | 79 | .collect() |
77 | 80 | } |
78 | 81 |
|
79 | | -pub fn compile_transaction_accounts( |
| 82 | +pub fn compile_transaction_accounts<'a>( |
80 | 83 | key_map: &KeyMap, |
81 | 84 | instructions: &[Instruction], |
82 | | - accounts: &[(Pubkey, Account)], |
| 85 | + accounts: impl Iterator<Item = &'a (Pubkey, Account)>, |
83 | 86 | stub_out_program_account: Option<Box<dyn Fn() -> Account>>, |
84 | 87 | ) -> Vec<TransactionAccount> { |
| 88 | + let len = key_map.len(); |
| 89 | + let mut by_key: HashMap<Pubkey, AccountSharedData> = HashMap::with_capacity(len); |
| 90 | + |
| 91 | + for (key, account) in accounts { |
| 92 | + if key_map.contains_key(key) { |
| 93 | + by_key.insert(*key, AccountSharedData::from(account.clone())); |
| 94 | + } |
| 95 | + } |
| 96 | + |
85 | 97 | key_map |
86 | 98 | .keys() |
87 | 99 | .map(|key| { |
88 | | - let account = accounts |
89 | | - .iter() |
90 | | - .find(|(k, _)| k == key) |
91 | | - .map(|(_, account)| AccountSharedData::from(account.clone())); |
92 | | - |
93 | | - if let Some(account) = account { |
94 | | - (*key, account) |
95 | | - } else if let Some(stub_out_program_account) = &stub_out_program_account { |
| 100 | + if let Some(stub_out_program_account) = &stub_out_program_account { |
96 | 101 | if instructions.iter().any(|ix| ix.program_id == *key) { |
97 | | - (*key, stub_out_program_account().into()) |
98 | | - } else { |
99 | | - panic!("{}", MolluskError::AccountMissing(key)) |
| 102 | + return (*key, stub_out_program_account().into()); |
100 | 103 | } |
101 | | - } else { |
102 | | - panic!("{}", MolluskError::AccountMissing(key)) |
103 | 104 | } |
| 105 | + let account = by_key |
| 106 | + .remove(key) |
| 107 | + .or_panic_with(MolluskError::AccountMissing(key)); |
| 108 | + (*key, account) |
104 | 109 | }) |
105 | 110 | .collect() |
106 | 111 | } |
0 commit comments