solana_svm_transaction/
svm_message.rs

1use {
2    crate::{
3        instruction::SVMInstruction, message_address_table_lookup::SVMMessageAddressTableLookup,
4    },
5    core::fmt::Debug,
6    solana_hash::Hash,
7    solana_message::AccountKeys,
8    solana_pubkey::Pubkey,
9    solana_sdk_ids::{ed25519_program, secp256k1_program, secp256r1_program, system_program},
10};
11
12mod sanitized_message;
13mod sanitized_transaction;
14// inlined to avoid solana-nonce dep
15#[cfg(test)]
16static_assertions::const_assert_eq!(
17    NONCED_TX_MARKER_IX_INDEX,
18    solana_nonce::NONCED_TX_MARKER_IX_INDEX
19);
20const NONCED_TX_MARKER_IX_INDEX: u8 = 0;
21
22// - Debug to support legacy logging
23pub trait SVMMessage: Debug {
24    /// Return the number of transaction-level signatures in the message.
25    fn num_transaction_signatures(&self) -> u64;
26    /// Return the number of ed25519 precompile signatures in the message.
27    fn num_ed25519_signatures(&self) -> u64 {
28        default_precompile_signature_count(&ed25519_program::ID, self.program_instructions_iter())
29    }
30    /// Return the number of secp256k1 precompile signatures in the message.
31    fn num_secp256k1_signatures(&self) -> u64 {
32        default_precompile_signature_count(&secp256k1_program::ID, self.program_instructions_iter())
33    }
34    /// Return the number of secp256r1 precompile signatures in the message.
35    fn num_secp256r1_signatures(&self) -> u64 {
36        default_precompile_signature_count(&secp256r1_program::ID, self.program_instructions_iter())
37    }
38
39    /// Returns the number of requested write-locks in this message.
40    /// This does not consider if write-locks are demoted.
41    fn num_write_locks(&self) -> u64;
42
43    /// Return the recent blockhash.
44    fn recent_blockhash(&self) -> &Hash;
45
46    /// Return the number of instructions in the message.
47    fn num_instructions(&self) -> usize;
48
49    /// Return an iterator over the instructions in the message.
50    fn instructions_iter(&self) -> impl Iterator<Item = SVMInstruction>;
51
52    /// Return an iterator over the instructions in the message, paired with
53    /// the pubkey of the program.
54    fn program_instructions_iter(&self) -> impl Iterator<Item = (&Pubkey, SVMInstruction)> + Clone;
55
56    /// Return the list of static account keys.
57    fn static_account_keys(&self) -> &[Pubkey];
58
59    /// Return the account keys.
60    fn account_keys(&self) -> AccountKeys;
61
62    /// Return the fee-payer
63    fn fee_payer(&self) -> &Pubkey;
64
65    /// Returns `true` if the account at `index` is writable.
66    fn is_writable(&self, index: usize) -> bool;
67
68    /// Returns `true` if the account at `index` is signer.
69    fn is_signer(&self, index: usize) -> bool;
70
71    /// Returns true if the account at the specified index is invoked as a
72    /// program in top-level instructions of this message.
73    fn is_invoked(&self, key_index: usize) -> bool;
74
75    /// Returns true if the account at the specified index is an input to some
76    /// program instruction in this message.
77    fn is_instruction_account(&self, key_index: usize) -> bool {
78        if let Ok(key_index) = u8::try_from(key_index) {
79            self.instructions_iter()
80                .any(|ix| ix.accounts.contains(&key_index))
81        } else {
82            false
83        }
84    }
85
86    /// If the message uses a durable nonce, return the pubkey of the nonce account
87    fn get_durable_nonce(&self, require_static_nonce_account: bool) -> Option<&Pubkey> {
88        let account_keys = self.account_keys();
89        self.instructions_iter()
90            .nth(usize::from(NONCED_TX_MARKER_IX_INDEX))
91            .filter(
92                |ix| match account_keys.get(usize::from(ix.program_id_index)) {
93                    Some(program_id) => system_program::check_id(program_id),
94                    _ => false,
95                },
96            )
97            .filter(|ix| {
98                /// Serialized value of [`SystemInstruction::AdvanceNonceAccount`].
99                const SERIALIZED_ADVANCE_NONCE_ACCOUNT: [u8; 4] = 4u32.to_le_bytes();
100                const SERIALIZED_SIZE: usize = SERIALIZED_ADVANCE_NONCE_ACCOUNT.len();
101
102                ix.data
103                    .get(..SERIALIZED_SIZE)
104                    .map(|data| data == SERIALIZED_ADVANCE_NONCE_ACCOUNT)
105                    .unwrap_or(false)
106            })
107            .and_then(|ix| {
108                ix.accounts.first().and_then(|idx| {
109                    let index = usize::from(*idx);
110                    if (require_static_nonce_account && index >= self.static_account_keys().len())
111                        || !self.is_writable(index)
112                    {
113                        None
114                    } else {
115                        account_keys.get(index)
116                    }
117                })
118            })
119    }
120
121    /// For the instruction at `index`, return an iterator over input accounts
122    /// that are signers.
123    fn get_ix_signers(&self, index: usize) -> impl Iterator<Item = &Pubkey> {
124        self.instructions_iter()
125            .nth(index)
126            .into_iter()
127            .flat_map(|ix| {
128                ix.accounts
129                    .iter()
130                    .copied()
131                    .map(usize::from)
132                    .filter(|index| self.is_signer(*index))
133                    .filter_map(|signer_index| self.account_keys().get(signer_index))
134            })
135    }
136
137    /// Get the number of lookup tables.
138    fn num_lookup_tables(&self) -> usize;
139
140    /// Get message address table lookups used in the message
141    fn message_address_table_lookups(&self) -> impl Iterator<Item = SVMMessageAddressTableLookup>;
142}
143
144fn default_precompile_signature_count<'a>(
145    precompile: &Pubkey,
146    instructions: impl Iterator<Item = (&'a Pubkey, SVMInstruction<'a>)>,
147) -> u64 {
148    instructions
149        .filter(|(program_id, _)| *program_id == precompile)
150        .map(|(_, ix)| u64::from(ix.data.first().copied().unwrap_or(0)))
151        .sum()
152}