2019-04-05 09:42:54 -07:00
|
|
|
use crate::bank::Bank;
|
2021-04-12 23:28:08 -07:00
|
|
|
use crate::hashed_transaction::HashedTransaction;
|
2019-04-05 09:42:54 -07:00
|
|
|
use solana_sdk::transaction::{Result, Transaction};
|
2021-04-12 23:28:08 -07:00
|
|
|
use std::borrow::Cow;
|
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> {
|
|
|
|
lock_results: Vec<Result<()>>,
|
2019-04-02 03:55:42 -07:00
|
|
|
bank: &'a Bank,
|
2021-04-12 23:28:08 -07:00
|
|
|
hashed_txs: Cow<'b, [HashedTransaction<'b>]>,
|
2019-04-02 03:55:42 -07:00
|
|
|
pub(crate) needs_unlock: bool,
|
|
|
|
}
|
|
|
|
|
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(
|
2019-09-19 10:06:08 -07:00
|
|
|
lock_results: Vec<Result<()>>,
|
2019-04-02 03:55:42 -07:00
|
|
|
bank: &'a Bank,
|
2021-04-12 23:28:08 -07:00
|
|
|
hashed_txs: Cow<'b, [HashedTransaction<'b>]>,
|
2019-04-02 03:55:42 -07:00
|
|
|
) -> Self {
|
2021-04-12 23:28:08 -07:00
|
|
|
assert_eq!(lock_results.len(), hashed_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-04-12 23:28:08 -07:00
|
|
|
hashed_txs,
|
2019-04-02 03:55:42 -07:00
|
|
|
needs_unlock: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:06:08 -07:00
|
|
|
pub fn lock_results(&self) -> &Vec<Result<()>> {
|
|
|
|
&self.lock_results
|
2019-04-02 03:55:42 -07:00
|
|
|
}
|
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
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())
|
2019-04-02 03:55:42 -07:00
|
|
|
}
|
2019-09-16 21:45:16 -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
|
|
|
}
|
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 {
|
|
|
|
use super::*;
|
2019-11-08 20:56:57 -08:00
|
|
|
use crate::genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo};
|
2020-10-19 12:23:14 -07:00
|
|
|
use 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-04-12 23:28:08 -07:00
|
|
|
let batch = bank.prepare_batch(txs.iter());
|
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-04-12 23:28:08 -07:00
|
|
|
let batch2 = bank.prepare_batch(txs.iter());
|
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-04-12 23:28:08 -07:00
|
|
|
let batch2 = bank.prepare_batch(txs.iter());
|
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-05-25 16:44:18 -07:00
|
|
|
let batch = bank.prepare_simulation_batch(&txs[0]);
|
2020-06-04 19:06:01 -07:00
|
|
|
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
|
|
|
|
|
|
|
|
// Grab locks
|
2021-04-12 23:28:08 -07:00
|
|
|
let batch2 = bank.prepare_batch(txs.iter());
|
2020-06-04 19:06:01 -07:00
|
|
|
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
|
|
|
|
|
|
|
|
// Prepare another batch without locks
|
2021-05-25 16:44:18 -07:00
|
|
|
let batch3 = bank.prepare_simulation_batch(&txs[0]);
|
2020-06-04 19:06:01 -07:00
|
|
|
assert!(batch3.lock_results().iter().all(|x| x.is_ok()));
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:51:35 -07:00
|
|
|
fn setup() -> (Bank, Vec<Transaction>) {
|
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);
|
|
|
|
let bank = Bank::new(&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![
|
2019-11-08 20:56:57 -08:00
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash()),
|
|
|
|
system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_config.hash()),
|
2019-05-07 15:51:35 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
(bank, txs)
|
|
|
|
}
|
|
|
|
}
|