Move a more generic process_transaction to runtime.rs
This commit is contained in:
parent
5b672f8921
commit
505f77b108
|
@ -177,24 +177,9 @@ mod test {
|
|||
tx: &Transaction,
|
||||
tx_accounts: &mut [Account],
|
||||
) -> Result<(), BudgetError> {
|
||||
for ix in &tx.instructions {
|
||||
let mut ix_accounts = runtime::get_subset_unchecked_mut(tx_accounts, &ix.accounts);
|
||||
let mut keyed_accounts: Vec<_> = ix
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|&index| {
|
||||
let index = index as usize;
|
||||
let key = &tx.account_keys[index];
|
||||
(key, index < tx.signatures.len())
|
||||
})
|
||||
.zip(ix_accounts.iter_mut())
|
||||
.map(|((key, is_signer), account)| KeyedAccount::new(key, is_signer, account))
|
||||
.collect();
|
||||
runtime::process_transaction(tx, tx_accounts, process_instruction)
|
||||
}
|
||||
|
||||
process_instruction(&mut keyed_accounts, &ix.userdata)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn test_invalid_instruction() {
|
||||
let mut accounts = vec![Account::new(1, 0, id()), Account::new(0, 512, id())];
|
||||
|
|
|
@ -143,7 +143,7 @@ pub fn has_duplicates<T: PartialEq>(xs: &[T]) -> bool {
|
|||
}
|
||||
|
||||
/// Get mut references to a subset of elements.
|
||||
pub fn get_subset_unchecked_mut<'a, T>(xs: &'a mut [T], indexes: &[u8]) -> Vec<&'a mut T> {
|
||||
fn get_subset_unchecked_mut<'a, T>(xs: &'a mut [T], indexes: &[u8]) -> Vec<&'a mut T> {
|
||||
// Since the compiler doesn't know the indexes are unique, dereferencing
|
||||
// multiple mut elements is assumed to be unsafe. If, however, all
|
||||
// indexes are unique, it's perfectly safe. The returned elements will share
|
||||
|
@ -188,6 +188,35 @@ pub fn execute_transaction(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// A utility function for unit-tests. Same as execute_transaction(), but bypasses the loaders
|
||||
/// for easier usage and better stack traces.
|
||||
pub fn process_transaction<F, E>(
|
||||
tx: &Transaction,
|
||||
tx_accounts: &mut [Account],
|
||||
process_instruction: F,
|
||||
) -> Result<(), E>
|
||||
where
|
||||
F: Fn(&mut [KeyedAccount], &[u8]) -> Result<(), E>,
|
||||
{
|
||||
for ix in &tx.instructions {
|
||||
let mut ix_accounts = get_subset_unchecked_mut(tx_accounts, &ix.accounts);
|
||||
let mut keyed_accounts: Vec<_> = ix
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|&index| {
|
||||
let index = index as usize;
|
||||
let key = &tx.account_keys[index];
|
||||
(key, index < tx.signatures.len())
|
||||
})
|
||||
.zip(ix_accounts.iter_mut())
|
||||
.map(|((key, is_signer), account)| KeyedAccount::new(key, is_signer, account))
|
||||
.collect();
|
||||
|
||||
process_instruction(&mut keyed_accounts, &ix.userdata)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Reference in New Issue