1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::bank::Bank;
use crate::hashed_transaction::HashedTransaction;
use safecoin_sdk::transaction::{Result, Transaction};
use std::borrow::Cow;
pub struct TransactionBatch<'a, 'b> {
lock_results: Vec<Result<()>>,
bank: &'a Bank,
hashed_txs: Cow<'b, [HashedTransaction<'b>]>,
pub(crate) needs_unlock: bool,
}
impl<'a, 'b> TransactionBatch<'a, 'b> {
pub fn new(
lock_results: Vec<Result<()>>,
bank: &'a Bank,
hashed_txs: Cow<'b, [HashedTransaction<'b>]>,
) -> Self {
assert_eq!(lock_results.len(), hashed_txs.len());
Self {
lock_results,
bank,
hashed_txs,
needs_unlock: true,
}
}
pub fn lock_results(&self) -> &Vec<Result<()>> {
&self.lock_results
}
pub fn hashed_transactions(&self) -> &[HashedTransaction] {
&self.hashed_txs
}
pub fn transactions_iter(&self) -> impl Iterator<Item = &Transaction> {
self.hashed_txs.iter().map(|h| h.transaction())
}
pub fn bank(&self) -> &Bank {
self.bank
}
}
impl<'a, 'b> Drop for TransactionBatch<'a, 'b> {
fn drop(&mut self) {
self.bank.unlock_accounts(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo};
use safecoin_sdk::{signature::Keypair, system_transaction};
#[test]
fn test_transaction_batch() {
let (bank, txs) = setup();
let batch = bank.prepare_batch(txs.iter());
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
let batch2 = bank.prepare_batch(txs.iter());
assert!(batch2.lock_results().iter().all(|x| x.is_err()));
drop(batch);
let batch2 = bank.prepare_batch(txs.iter());
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
}
#[test]
fn test_simulation_batch() {
let (bank, txs) = setup();
let batch = bank.prepare_simulation_batch(&txs[0]);
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
let batch2 = bank.prepare_batch(txs.iter());
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
let batch3 = bank.prepare_simulation_batch(&txs[0]);
assert!(batch3.lock_results().iter().all(|x| x.is_ok()));
}
fn setup() -> (Bank, Vec<Transaction>) {
let dummy_leader_pubkey = safecoin_sdk::pubkey::new_rand();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config_with_leader(500, &dummy_leader_pubkey, 100);
let bank = Bank::new(&genesis_config);
let pubkey = safecoin_sdk::pubkey::new_rand();
let keypair2 = Keypair::new();
let pubkey2 = safecoin_sdk::pubkey::new_rand();
let txs = vec![
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash()),
system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_config.hash()),
];
(bank, txs)
}
}