|
| 1 | +//! Instruction <-> Transaction key deduplication and privilege handling. |
| 2 | +//! |
| 3 | +//! Solana instructions and transactions are designed to be intentionally |
| 4 | +//! verbosely declarative, to provide the runtime with granular directives |
| 5 | +//! for manipulating chain state. |
| 6 | +//! |
| 7 | +//! As a result, when a transaction is _compiled_, many steps occur: |
| 8 | +//! * Ensuring there is a fee payer. |
| 9 | +//! * Ensuring there is a signature. |
| 10 | +//! * Deduplicating account keys. |
| 11 | +//! * Configuring the highest role awarded to each account key. |
| 12 | +//! * ... |
| 13 | +//! |
| 14 | +//! Since Mollusk does not use transactions or fee payers, the deduplication |
| 15 | +//! of account keys and handling of roles are the only two steps necessary |
| 16 | +//! to perform under the hood within the harness. |
| 17 | +//! |
| 18 | +//! This implementation closely follows the implementation in the Anza SDK |
| 19 | +//! for `Message::new_with_blockhash`. For more information, see: |
| 20 | +//! <https://github.com/anza-xyz/agave/blob/c6e8239843af8e6301cd198e39d0a44add427bef/sdk/program/src/message/legacy.rs#L357>. |
| 21 | +
|
| 22 | +use { |
| 23 | + solana_sdk::{ |
| 24 | + account::{AccountSharedData, WritableAccount}, |
| 25 | + instruction::Instruction, |
| 26 | + pubkey::Pubkey, |
| 27 | + transaction_context::{IndexOfAccount, InstructionAccount, TransactionAccount}, |
| 28 | + }, |
| 29 | + std::collections::HashMap, |
| 30 | +}; |
| 31 | + |
| 32 | +struct KeyMap(HashMap<Pubkey, (bool, bool)>); |
| 33 | + |
| 34 | +impl KeyMap { |
| 35 | + fn compile(instruction: &Instruction) -> Self { |
| 36 | + let mut map: HashMap<Pubkey, (bool, bool)> = HashMap::new(); |
| 37 | + map.entry(instruction.program_id).or_default(); |
| 38 | + for meta in instruction.accounts.iter() { |
| 39 | + let entry = map.entry(meta.pubkey).or_default(); |
| 40 | + entry.0 |= meta.is_signer; |
| 41 | + entry.1 |= meta.is_writable; |
| 42 | + } |
| 43 | + Self(map) |
| 44 | + } |
| 45 | + |
| 46 | + fn is_signer(&self, key: &Pubkey) -> bool { |
| 47 | + self.0.get(key).map(|(s, _)| *s).unwrap_or(false) |
| 48 | + } |
| 49 | + |
| 50 | + fn is_writable(&self, key: &Pubkey) -> bool { |
| 51 | + self.0.get(key).map(|(_, w)| *w).unwrap_or(false) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +struct Keys<'a> { |
| 56 | + keys: Vec<&'a Pubkey>, |
| 57 | + key_map: &'a KeyMap, |
| 58 | +} |
| 59 | + |
| 60 | +impl<'a> Keys<'a> { |
| 61 | + fn new(key_map: &'a KeyMap) -> Self { |
| 62 | + Self { |
| 63 | + keys: key_map.0.keys().collect(), |
| 64 | + key_map, |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn position(&self, key: &Pubkey) -> u8 { |
| 69 | + self.keys.iter().position(|k| *k == key).unwrap() as u8 |
| 70 | + } |
| 71 | + |
| 72 | + fn is_signer(&self, index: usize) -> bool { |
| 73 | + self.key_map.is_signer(self.keys[index]) |
| 74 | + } |
| 75 | + |
| 76 | + fn is_writable(&self, index: usize) -> bool { |
| 77 | + self.key_map.is_writable(self.keys[index]) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Helper struct so Mollusk doesn't have to clone instruction data. |
| 82 | +struct CompiledInstructionWithoutData { |
| 83 | + program_id_index: u8, |
| 84 | + accounts: Vec<u8>, |
| 85 | +} |
| 86 | + |
| 87 | +pub struct CompiledAccounts { |
| 88 | + pub program_id_index: u16, |
| 89 | + pub instruction_accounts: Vec<InstructionAccount>, |
| 90 | + pub transaction_accounts: Vec<TransactionAccount>, |
| 91 | +} |
| 92 | + |
| 93 | +pub fn compile_accounts( |
| 94 | + instruction: &Instruction, |
| 95 | + accounts: &[(Pubkey, AccountSharedData)], |
| 96 | + loader_key: Pubkey, |
| 97 | +) -> CompiledAccounts { |
| 98 | + let key_map = KeyMap::compile(instruction); |
| 99 | + let keys = Keys::new(&key_map); |
| 100 | + |
| 101 | + let compiled_instruction = CompiledInstructionWithoutData { |
| 102 | + program_id_index: keys.position(&instruction.program_id), |
| 103 | + accounts: instruction |
| 104 | + .accounts |
| 105 | + .iter() |
| 106 | + .map(|account_meta| keys.position(&account_meta.pubkey)) |
| 107 | + .collect(), |
| 108 | + }; |
| 109 | + |
| 110 | + let instruction_accounts: Vec<InstructionAccount> = compiled_instruction |
| 111 | + .accounts |
| 112 | + .iter() |
| 113 | + .enumerate() |
| 114 | + .map(|(ix_account_index, &index_in_transaction)| { |
| 115 | + let index_in_callee = compiled_instruction |
| 116 | + .accounts |
| 117 | + .get(0..ix_account_index) |
| 118 | + .unwrap() |
| 119 | + .iter() |
| 120 | + .position(|&account_index| account_index == index_in_transaction) |
| 121 | + .unwrap_or(ix_account_index) as IndexOfAccount; |
| 122 | + let index_in_transaction = index_in_transaction as usize; |
| 123 | + InstructionAccount { |
| 124 | + index_in_transaction: index_in_transaction as IndexOfAccount, |
| 125 | + index_in_caller: index_in_transaction as IndexOfAccount, |
| 126 | + index_in_callee, |
| 127 | + is_signer: keys.is_signer(index_in_transaction), |
| 128 | + is_writable: keys.is_writable(index_in_transaction), |
| 129 | + } |
| 130 | + }) |
| 131 | + .collect(); |
| 132 | + |
| 133 | + let transaction_accounts: Vec<TransactionAccount> = keys |
| 134 | + .keys |
| 135 | + .iter() |
| 136 | + .map(|key| { |
| 137 | + if *key == &instruction.program_id { |
| 138 | + (**key, stub_out_program_account(loader_key)) |
| 139 | + } else { |
| 140 | + let account = accounts |
| 141 | + .iter() |
| 142 | + .find(|(k, _)| k == *key) |
| 143 | + .map(|(_, account)| account.clone()) |
| 144 | + .unwrap_or_else(|| { |
| 145 | + panic!( |
| 146 | + " [mollusk]: An account required by the instruction was not \ |
| 147 | + provided: {:?}", |
| 148 | + key, |
| 149 | + ) |
| 150 | + }); |
| 151 | + (**key, account) |
| 152 | + } |
| 153 | + }) |
| 154 | + .collect(); |
| 155 | + |
| 156 | + CompiledAccounts { |
| 157 | + program_id_index: compiled_instruction.program_id_index as u16, |
| 158 | + instruction_accounts, |
| 159 | + transaction_accounts, |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +fn stub_out_program_account(loader_key: Pubkey) -> AccountSharedData { |
| 164 | + let mut program_account = AccountSharedData::default(); |
| 165 | + program_account.set_owner(loader_key); |
| 166 | + program_account.set_executable(true); |
| 167 | + program_account |
| 168 | +} |
0 commit comments