solana/runtime/src/transaction_batch.rs

121 lines
3.4 KiB
Rust
Raw Normal View History

use {
crate::bank::Bank,
solana_sdk::transaction::{Result, SanitizedTransaction},
std::borrow::Cow,
};
// Represents the results of trying to lock a set of accounts
pub struct TransactionBatch<'a, 'b> {
lock_results: Vec<Result<()>>,
bank: &'a Bank,
sanitized_txs: Cow<'b, [SanitizedTransaction]>,
pub(crate) needs_unlock: bool,
}
impl<'a, 'b> TransactionBatch<'a, 'b> {
pub fn new(
lock_results: Vec<Result<()>>,
bank: &'a Bank,
sanitized_txs: Cow<'b, [SanitizedTransaction]>,
) -> Self {
assert_eq!(lock_results.len(), sanitized_txs.len());
Self {
lock_results,
bank,
sanitized_txs,
needs_unlock: true,
}
}
pub fn lock_results(&self) -> &Vec<Result<()>> {
&self.lock_results
}
pub fn sanitized_transactions(&self) -> &[SanitizedTransaction] {
&self.sanitized_txs
}
pub fn bank(&self) -> &Bank {
self.bank
}
}
// Unlock all locked accounts in destructor.
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};
2020-10-19 12:23:14 -07:00
use solana_sdk::{signature::Keypair, system_transaction};
use std::convert::TryInto;
#[test]
fn test_transaction_batch() {
let (bank, txs) = setup();
// Test getting locked accounts
let batch = bank.prepare_sanitized_batch(&txs);
// Grab locks
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
// Trying to grab locks again should fail
let batch2 = bank.prepare_sanitized_batch(&txs);
assert!(batch2.lock_results().iter().all(|x| x.is_err()));
// Drop the first set of locks
drop(batch);
// Now grabbing locks should work again
let batch2 = bank.prepare_sanitized_batch(&txs);
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
}
#[test]
fn test_simulation_batch() {
let (bank, txs) = setup();
// Prepare batch without locks
let batch = bank.prepare_simulation_batch(txs[0].clone());
assert!(batch.lock_results().iter().all(|x| x.is_ok()));
// Grab locks
let batch2 = bank.prepare_sanitized_batch(&txs);
assert!(batch2.lock_results().iter().all(|x| x.is_ok()));
// Prepare another batch without locks
let batch3 = bank.prepare_simulation_batch(txs[0].clone());
assert!(batch3.lock_results().iter().all(|x| x.is_ok()));
}
fn setup() -> (Bank, Vec<SanitizedTransaction>) {
let dummy_leader_pubkey = solana_sdk::pubkey::new_rand();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config_with_leader(500, &dummy_leader_pubkey, 100);
let bank = Bank::new_for_tests(&genesis_config);
let pubkey = solana_sdk::pubkey::new_rand();
let keypair2 = Keypair::new();
let pubkey2 = solana_sdk::pubkey::new_rand();
let txs = vec![
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash())
.try_into()
.unwrap(),
system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_config.hash())
.try_into()
.unwrap(),
];
(bank, txs)
}
}