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
119
120
121
122
123
124
125
126
127
128
129
use crate::accounts::AccountLockType;
use crate::bank::Bank;
use solana_sdk::transaction::{Result, Transaction};
use std::borrow::Borrow;
pub struct LockedAccountsResults<'a, 'b, I: Borrow<Transaction>> {
locked_accounts_results: Vec<Result<()>>,
bank: &'a Bank,
transactions: &'b [I],
lock_type: AccountLockType,
pub(crate) needs_unlock: bool,
}
impl<'a, 'b, I: Borrow<Transaction>> LockedAccountsResults<'a, 'b, I> {
pub fn new(
locked_accounts_results: Vec<Result<()>>,
bank: &'a Bank,
transactions: &'b [I],
lock_type: AccountLockType,
) -> Self {
Self {
locked_accounts_results,
bank,
transactions,
needs_unlock: true,
lock_type,
}
}
pub fn lock_type(&self) -> AccountLockType {
self.lock_type.clone()
}
pub fn locked_accounts_results(&self) -> &Vec<Result<()>> {
&self.locked_accounts_results
}
pub fn transactions(&self) -> &[I] {
self.transactions
}
}
impl<'a, 'b, I: Borrow<Transaction>> Drop for LockedAccountsResults<'a, 'b, I> {
fn drop(&mut self) {
if self.needs_unlock {
self.bank.unlock_accounts(self)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::genesis_utils::{create_genesis_block_with_leader, GenesisBlockInfo};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction;
#[test]
fn test_account_locks() {
let (bank, txs) = setup();
let lock_results = bank.lock_accounts(&txs);
assert!(lock_results
.locked_accounts_results()
.iter()
.all(|x| x.is_ok()));
let lock_results2 = bank.lock_accounts(&txs);
assert!(lock_results2
.locked_accounts_results()
.iter()
.all(|x| x.is_err()));
drop(lock_results);
let lock_results2 = bank.lock_accounts(&txs);
assert!(lock_results2
.locked_accounts_results()
.iter()
.all(|x| x.is_ok()));
}
#[test]
fn test_record_locks() {
let (bank, txs) = setup();
let lock_results = bank.lock_record_accounts(&txs);
assert!(lock_results.locked_accounts_results().is_empty());
drop(lock_results);
let lock_results2 = bank.lock_record_accounts(&txs);
assert!(lock_results2.locked_accounts_results().is_empty());
}
fn setup() -> (Bank, Vec<Transaction>) {
let dummy_leader_pubkey = Pubkey::new_rand();
let GenesisBlockInfo {
genesis_block,
mint_keypair,
..
} = create_genesis_block_with_leader(500, &dummy_leader_pubkey, 100);
let bank = Bank::new(&genesis_block);
let pubkey = Pubkey::new_rand();
let keypair2 = Keypair::new();
let pubkey2 = Pubkey::new_rand();
let txs = vec![
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_block.hash()),
system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_block.hash()),
];
(bank, txs)
}
}