solana_runtime/
transaction_batch.rs1use {
2 crate::bank::Bank, core::ops::Deref, solana_svm_transaction::svm_message::SVMMessage,
3 solana_transaction_error::TransactionResult as Result,
4};
5
6pub enum OwnedOrBorrowed<'a, T> {
7 Owned(Vec<T>),
8 Borrowed(&'a [T]),
9}
10
11impl<T> Deref for OwnedOrBorrowed<'_, T> {
12 type Target = [T];
13
14 fn deref(&self) -> &Self::Target {
15 match self {
16 OwnedOrBorrowed::Owned(v) => v,
17 OwnedOrBorrowed::Borrowed(v) => v,
18 }
19 }
20}
21
22pub struct TransactionBatch<'a, 'b, Tx: SVMMessage> {
24 lock_results: Vec<Result<()>>,
25 bank: &'a Bank,
26 sanitized_txs: OwnedOrBorrowed<'b, Tx>,
27 needs_unlock: bool,
28}
29
30impl<'a, 'b, Tx: SVMMessage> TransactionBatch<'a, 'b, Tx> {
31 pub fn new(
32 lock_results: Vec<Result<()>>,
33 bank: &'a Bank,
34 sanitized_txs: OwnedOrBorrowed<'b, Tx>,
35 ) -> Self {
36 assert_eq!(lock_results.len(), sanitized_txs.len());
37 Self {
38 lock_results,
39 bank,
40 sanitized_txs,
41 needs_unlock: true,
42 }
43 }
44
45 pub fn lock_results(&self) -> &Vec<Result<()>> {
46 &self.lock_results
47 }
48
49 pub fn sanitized_transactions(&self) -> &[Tx] {
50 &self.sanitized_txs
51 }
52
53 pub fn bank(&self) -> &Bank {
54 self.bank
55 }
56
57 pub fn set_needs_unlock(&mut self, needs_unlock: bool) {
58 self.needs_unlock = needs_unlock;
59 }
60
61 pub fn needs_unlock(&self) -> bool {
62 self.needs_unlock
63 }
64
65 pub fn unlock_failures(&mut self, transaction_results: Vec<Result<()>>) {
68 assert_eq!(self.lock_results.len(), transaction_results.len());
69 if !self.needs_unlock() {
72 return;
73 }
74
75 let txs_and_results = transaction_results
76 .iter()
77 .enumerate()
78 .inspect(|(index, result)| {
79 assert!(!(result.is_ok() && self.lock_results[*index].is_err()))
84 })
85 .filter(|(index, result)| result.is_err() && self.lock_results[*index].is_ok())
86 .map(|(index, _)| (&self.sanitized_txs[index], &self.lock_results[index]));
87
88 self.bank.unlock_accounts(txs_and_results);
91
92 self.lock_results = transaction_results;
96 }
97}
98
99impl<Tx: SVMMessage> Drop for TransactionBatch<'_, '_, Tx> {
101 fn drop(&mut self) {
102 if self.needs_unlock() {
103 self.set_needs_unlock(false);
104 self.bank.unlock_accounts(
105 self.sanitized_transactions()
106 .iter()
107 .zip(self.lock_results()),
108 )
109 }
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use {
116 super::*,
117 crate::genesis_utils::{GenesisConfigInfo, create_genesis_config_with_leader},
118 solana_keypair::Keypair,
119 solana_runtime_transaction::runtime_transaction::RuntimeTransaction,
120 solana_system_transaction as system_transaction,
121 solana_transaction::sanitized::SanitizedTransaction,
122 solana_transaction_error::TransactionError,
123 };
124
125 #[test]
126 fn test_transaction_batch() {
127 let (bank, txs) = setup(false);
128
129 let batch = bank.prepare_sanitized_batch(&txs);
131
132 assert!(batch.lock_results().iter().all(|x| x.is_ok()));
134
135 let batch2 = bank.prepare_sanitized_batch(&txs);
137 assert!(batch2.lock_results().iter().all(|x| x.is_err()));
138
139 drop(batch);
141
142 let batch2 = bank.prepare_sanitized_batch(&txs);
144 assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
145 }
146
147 #[test]
148 fn test_simulation_batch() {
149 let (bank, txs) = setup(false);
150
151 let batch = bank.prepare_unlocked_batch_from_single_tx(&txs[0]);
153 assert!(batch.lock_results().iter().all(|x| x.is_ok()));
154
155 let batch2 = bank.prepare_sanitized_batch(&txs);
157 assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
158
159 let batch3 = bank.prepare_unlocked_batch_from_single_tx(&txs[0]);
161 assert!(batch3.lock_results().iter().all(|x| x.is_ok()));
162 }
163
164 #[test]
165 fn test_unlock_failures() {
166 let (bank, txs) = setup(true);
167 let expected_lock_results = vec![Ok(()), Ok(()), Ok(())];
168
169 let mut batch = bank.prepare_sanitized_batch(&txs);
171 assert_eq!(batch.lock_results, expected_lock_results,);
172
173 let qos_results = vec![
174 Ok(()),
175 Err(TransactionError::WouldExceedMaxBlockCostLimit),
176 Err(TransactionError::WouldExceedMaxBlockCostLimit),
177 ];
178 batch.unlock_failures(qos_results.clone());
179 assert_eq!(batch.lock_results, qos_results);
180
181 drop(batch);
183
184 let batch2 = bank.prepare_sanitized_batch(&txs);
186 assert_eq!(batch2.lock_results, expected_lock_results,);
187 }
188
189 fn setup(insert_conflicting_tx: bool) -> (Bank, Vec<RuntimeTransaction<SanitizedTransaction>>) {
190 let dummy_leader_pubkey = solana_pubkey::new_rand();
191 let GenesisConfigInfo {
192 genesis_config,
193 mint_keypair,
194 ..
195 } = create_genesis_config_with_leader(500, &dummy_leader_pubkey, 100);
196 let bank = Bank::new_for_tests(&genesis_config);
197
198 let pubkey = solana_pubkey::new_rand();
199 let keypair2 = Keypair::new();
200 let pubkey2 = solana_pubkey::new_rand();
201
202 let mut txs = vec![RuntimeTransaction::from_transaction_for_tests(
203 system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash()),
204 )];
205 if insert_conflicting_tx {
206 txs.push(RuntimeTransaction::from_transaction_for_tests(
207 system_transaction::transfer(&mint_keypair, &pubkey2, 1, genesis_config.hash()),
208 ));
209 }
210 txs.push(RuntimeTransaction::from_transaction_for_tests(
211 system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_config.hash()),
212 ));
213
214 (bank, txs)
215 }
216}