2021-08-17 15:17:56 -07:00
|
|
|
use {
|
2021-12-29 23:42:32 -08:00
|
|
|
crate::bank::Bank,
|
2021-08-17 15:17:56 -07:00
|
|
|
solana_sdk::transaction::{Result, SanitizedTransaction},
|
|
|
|
std::borrow::Cow,
|
2021-07-15 20:51:27 -07:00
|
|
|
};
|
2019-04-02 03:55:42 -07:00
|
|
|
|
|
|
|
// Represents the results of trying to lock a set of accounts
|
2019-09-19 10:06:08 -07:00
|
|
|
pub struct TransactionBatch<'a, 'b> {
|
2021-12-29 23:42:32 -08:00
|
|
|
lock_results: Vec<Result<()>>,
|
2019-04-02 03:55:42 -07:00
|
|
|
bank: &'a Bank,
|
2021-08-17 15:17:56 -07:00
|
|
|
sanitized_txs: Cow<'b, [SanitizedTransaction]>,
|
2022-05-10 13:39:08 -07:00
|
|
|
needs_unlock: bool,
|
2019-04-02 03:55:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:06:08 -07:00
|
|
|
impl<'a, 'b> TransactionBatch<'a, 'b> {
|
2019-04-02 03:55:42 -07:00
|
|
|
pub fn new(
|
2021-12-29 23:42:32 -08:00
|
|
|
lock_results: Vec<Result<()>>,
|
2019-04-02 03:55:42 -07:00
|
|
|
bank: &'a Bank,
|
2021-08-17 15:17:56 -07:00
|
|
|
sanitized_txs: Cow<'b, [SanitizedTransaction]>,
|
2019-04-02 03:55:42 -07:00
|
|
|
) -> Self {
|
2021-07-15 20:51:27 -07:00
|
|
|
assert_eq!(lock_results.len(), sanitized_txs.len());
|
2019-04-02 03:55:42 -07:00
|
|
|
Self {
|
2019-09-19 10:06:08 -07:00
|
|
|
lock_results,
|
2019-04-02 03:55:42 -07:00
|
|
|
bank,
|
2021-07-15 20:51:27 -07:00
|
|
|
sanitized_txs,
|
2019-04-02 03:55:42 -07:00
|
|
|
needs_unlock: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:42:32 -08:00
|
|
|
pub fn lock_results(&self) -> &Vec<Result<()>> {
|
2019-09-19 10:06:08 -07:00
|
|
|
&self.lock_results
|
2019-04-02 03:55:42 -07:00
|
|
|
}
|
|
|
|
|
2021-07-15 20:51:27 -07:00
|
|
|
pub fn sanitized_transactions(&self) -> &[SanitizedTransaction] {
|
|
|
|
&self.sanitized_txs
|
2021-04-12 23:28:08 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:06:08 -07:00
|
|
|
pub fn bank(&self) -> &Bank {
|
|
|
|
self.bank
|
2019-09-16 21:45:16 -07:00
|
|
|
}
|
2022-05-10 13:39:08 -07:00
|
|
|
|
|
|
|
pub fn set_needs_unlock(&mut self, needs_unlock: bool) {
|
|
|
|
self.needs_unlock = needs_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_unlock(&self) -> bool {
|
|
|
|
self.needs_unlock
|
|
|
|
}
|
2019-04-02 03:55:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock all locked accounts in destructor.
|
2019-09-19 10:06:08 -07:00
|
|
|
impl<'a, 'b> Drop for TransactionBatch<'a, 'b> {
|
2019-04-02 03:55:42 -07:00
|
|
|
fn drop(&mut self) {
|
2019-09-19 10:06:08 -07:00
|
|
|
self.bank.unlock_accounts(self)
|
2019-04-02 03:55:42 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
crate::genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo},
|
|
|
|
solana_sdk::{signature::Keypair, system_transaction},
|
|
|
|
};
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
#[test]
|
2019-09-19 10:06:08 -07:00
|
|
|
fn test_transaction_batch() {
|
2019-05-07 15:51:35 -07:00
|
|
|
let (bank, txs) = setup();
|
|
|
|
|
|
|
|
// Test getting locked accounts
|
2021-08-17 15:17:56 -07:00
|
|
|
let batch = bank.prepare_sanitized_batch(&txs);
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
// Grab locks
|
2019-09-19 10:06:08 -07:00
|
|
|
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
// Trying to grab locks again should fail
|
2021-08-17 15:17:56 -07:00
|
|
|
let batch2 = bank.prepare_sanitized_batch(&txs);
|
2019-09-19 10:06:08 -07:00
|
|
|
assert!(batch2.lock_results().iter().all(|x| x.is_err()));
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
// Drop the first set of locks
|
2019-09-19 10:06:08 -07:00
|
|
|
drop(batch);
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
// Now grabbing locks should work again
|
2021-08-17 15:17:56 -07:00
|
|
|
let batch2 = bank.prepare_sanitized_batch(&txs);
|
2019-09-19 10:06:08 -07:00
|
|
|
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
|
2019-05-07 15:51:35 -07:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:06:01 -07:00
|
|
|
#[test]
|
|
|
|
fn test_simulation_batch() {
|
|
|
|
let (bank, txs) = setup();
|
|
|
|
|
|
|
|
// Prepare batch without locks
|
2021-07-15 20:51:27 -07:00
|
|
|
let batch = bank.prepare_simulation_batch(txs[0].clone());
|
2020-06-04 19:06:01 -07:00
|
|
|
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
|
|
|
|
|
|
|
|
// Grab locks
|
2021-07-15 20:51:27 -07:00
|
|
|
let batch2 = bank.prepare_sanitized_batch(&txs);
|
2020-06-04 19:06:01 -07:00
|
|
|
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
|
|
|
|
|
|
|
|
// Prepare another batch without locks
|
2021-07-15 20:51:27 -07:00
|
|
|
let batch3 = bank.prepare_simulation_batch(txs[0].clone());
|
2020-06-04 19:06:01 -07:00
|
|
|
assert!(batch3.lock_results().iter().all(|x| x.is_ok()));
|
|
|
|
}
|
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
fn setup() -> (Bank, Vec<SanitizedTransaction>) {
|
2020-10-19 12:12:08 -07:00
|
|
|
let dummy_leader_pubkey = solana_sdk::pubkey::new_rand();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config_with_leader(500, &dummy_leader_pubkey, 100);
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
2019-05-07 15:51:35 -07:00
|
|
|
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
2019-05-07 15:51:35 -07:00
|
|
|
let keypair2 = Keypair::new();
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey2 = solana_sdk::pubkey::new_rand();
|
2019-05-07 15:51:35 -07:00
|
|
|
|
|
|
|
let txs = vec![
|
2021-10-27 10:09:16 -07:00
|
|
|
SanitizedTransaction::from_transaction_for_tests(system_transaction::transfer(
|
|
|
|
&mint_keypair,
|
|
|
|
&pubkey,
|
|
|
|
1,
|
|
|
|
genesis_config.hash(),
|
|
|
|
)),
|
|
|
|
SanitizedTransaction::from_transaction_for_tests(system_transaction::transfer(
|
|
|
|
&keypair2,
|
|
|
|
&pubkey2,
|
|
|
|
1,
|
|
|
|
genesis_config.hash(),
|
|
|
|
)),
|
2019-05-07 15:51:35 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
(bank, txs)
|
|
|
|
}
|
|
|
|
}
|