solana_runtime/
transaction_batch.rs1use {
2 crate::bank::Bank, core::ops::Deref, solana_sdk::transaction::Result,
3 solana_svm_transaction::svm_message::SVMMessage,
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<'a, 'b, Tx: SVMMessage> Drop for TransactionBatch<'a, 'b, 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::{create_genesis_config_with_leader, GenesisConfigInfo},
118 solana_sdk::{
119 signature::Keypair,
120 system_transaction,
121 transaction::{SanitizedTransaction, TransactionError},
122 },
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
168 let mut batch = bank.prepare_sanitized_batch(&txs);
170 assert_eq!(
171 batch.lock_results,
172 vec![Ok(()), Err(TransactionError::AccountInUse), Ok(())]
173 );
174
175 let qos_results = vec![
176 Ok(()),
177 Err(TransactionError::AccountInUse),
178 Err(TransactionError::WouldExceedMaxBlockCostLimit),
179 ];
180 batch.unlock_failures(qos_results.clone());
181 assert_eq!(batch.lock_results, qos_results);
182
183 drop(batch);
185
186 let batch2 = bank.prepare_sanitized_batch(&txs);
188 assert_eq!(
189 batch2.lock_results,
190 vec![Ok(()), Err(TransactionError::AccountInUse), Ok(())]
191 );
192 }
193
194 fn setup(insert_conflicting_tx: bool) -> (Bank, Vec<SanitizedTransaction>) {
195 let dummy_leader_pubkey = solana_sdk::pubkey::new_rand();
196 let GenesisConfigInfo {
197 genesis_config,
198 mint_keypair,
199 ..
200 } = create_genesis_config_with_leader(500, &dummy_leader_pubkey, 100);
201 let bank = Bank::new_for_tests(&genesis_config);
202
203 let pubkey = solana_sdk::pubkey::new_rand();
204 let keypair2 = Keypair::new();
205 let pubkey2 = solana_sdk::pubkey::new_rand();
206
207 let mut txs = vec![SanitizedTransaction::from_transaction_for_tests(
208 system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash()),
209 )];
210 if insert_conflicting_tx {
211 txs.push(SanitizedTransaction::from_transaction_for_tests(
212 system_transaction::transfer(&mint_keypair, &pubkey2, 1, genesis_config.hash()),
213 ));
214 }
215 txs.push(SanitizedTransaction::from_transaction_for_tests(
216 system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_config.hash()),
217 ));
218
219 (bank, txs)
220 }
221}