solana_svm_transaction/
svm_message.rs1use {
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#[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
22pub trait SVMMessage: Debug {
24 fn num_transaction_signatures(&self) -> u64;
26 fn num_ed25519_signatures(&self) -> u64 {
28 default_precompile_signature_count(&ed25519_program::ID, self.program_instructions_iter())
29 }
30 fn num_secp256k1_signatures(&self) -> u64 {
32 default_precompile_signature_count(&secp256k1_program::ID, self.program_instructions_iter())
33 }
34 fn num_secp256r1_signatures(&self) -> u64 {
36 default_precompile_signature_count(&secp256r1_program::ID, self.program_instructions_iter())
37 }
38
39 fn num_write_locks(&self) -> u64;
42
43 fn recent_blockhash(&self) -> &Hash;
45
46 fn num_instructions(&self) -> usize;
48
49 fn instructions_iter(&self) -> impl Iterator<Item = SVMInstruction>;
51
52 fn program_instructions_iter(&self) -> impl Iterator<Item = (&Pubkey, SVMInstruction)> + Clone;
55
56 fn account_keys(&self) -> AccountKeys;
58
59 fn fee_payer(&self) -> &Pubkey;
61
62 fn is_writable(&self, index: usize) -> bool;
64
65 fn is_signer(&self, index: usize) -> bool;
67
68 fn is_invoked(&self, key_index: usize) -> bool;
71
72 fn is_instruction_account(&self, key_index: usize) -> bool {
75 if let Ok(key_index) = u8::try_from(key_index) {
76 self.instructions_iter()
77 .any(|ix| ix.accounts.contains(&key_index))
78 } else {
79 false
80 }
81 }
82
83 fn get_durable_nonce(&self) -> Option<&Pubkey> {
85 let account_keys = self.account_keys();
86 self.instructions_iter()
87 .nth(usize::from(NONCED_TX_MARKER_IX_INDEX))
88 .filter(
89 |ix| match account_keys.get(usize::from(ix.program_id_index)) {
90 Some(program_id) => system_program::check_id(program_id),
91 _ => false,
92 },
93 )
94 .filter(|ix| {
95 const SERIALIZED_ADVANCE_NONCE_ACCOUNT: [u8; 4] = 4u32.to_le_bytes();
97 const SERIALIZED_SIZE: usize = SERIALIZED_ADVANCE_NONCE_ACCOUNT.len();
98
99 ix.data
100 .get(..SERIALIZED_SIZE)
101 .map(|data| data == SERIALIZED_ADVANCE_NONCE_ACCOUNT)
102 .unwrap_or(false)
103 })
104 .and_then(|ix| {
105 ix.accounts.first().and_then(|idx| {
106 let index = usize::from(*idx);
107 if !self.is_writable(index) {
108 None
109 } else {
110 account_keys.get(index)
111 }
112 })
113 })
114 }
115
116 fn get_ix_signers(&self, index: usize) -> impl Iterator<Item = &Pubkey> {
119 self.instructions_iter()
120 .nth(index)
121 .into_iter()
122 .flat_map(|ix| {
123 ix.accounts
124 .iter()
125 .copied()
126 .map(usize::from)
127 .filter(|index| self.is_signer(*index))
128 .filter_map(|signer_index| self.account_keys().get(signer_index))
129 })
130 }
131
132 fn num_lookup_tables(&self) -> usize;
134
135 fn message_address_table_lookups(&self) -> impl Iterator<Item = SVMMessageAddressTableLookup>;
137}
138
139fn default_precompile_signature_count<'a>(
140 precompile: &Pubkey,
141 instructions: impl Iterator<Item = (&'a Pubkey, SVMInstruction<'a>)>,
142) -> u64 {
143 instructions
144 .filter(|(program_id, _)| *program_id == precompile)
145 .map(|(_, ix)| u64::from(ix.data.first().copied().unwrap_or(0)))
146 .sum()
147}