Allow tests to add instruction processors

Make runtime a private module again.
This commit is contained in:
Greg Fitzgerald
2019-03-16 05:50:44 -06:00
committed by Grimes
parent ee39f31d81
commit 55cdbedb52
4 changed files with 94 additions and 141 deletions

View File

@@ -5,7 +5,7 @@
use crate::accounts::{Accounts, ErrorCounters, InstructionAccounts, InstructionLoaders};
use crate::blockhash_queue::BlockhashQueue;
use crate::runtime::Runtime;
use crate::runtime::{Runtime, StaticEntrypoint};
use crate::status_cache::StatusCache;
use bincode::serialize;
use hashbrown::HashMap;
@@ -839,6 +839,21 @@ impl Bank {
let max_tick_height = (self.slot + 1) * self.ticks_per_slot - 1;
self.is_delta.load(Ordering::Relaxed) && self.tick_height() == max_tick_height
}
/// Add a static entrypoint to intercept intructions before the dynamic loader.
pub fn add_entrypoint(&mut self, program_id: Pubkey, entrypoint: StaticEntrypoint) {
self.runtime.add_entrypoint(program_id, entrypoint);
// Add a bogus executable account to load.
let bogus_account = Account {
lamports: 1,
data: vec![],
owner: native_loader::id(),
executable: true,
};
self.accounts
.store_slow(self.accounts_id, &program_id, &bogus_account);
}
}
#[cfg(test)]

View File

@@ -6,7 +6,7 @@ mod blockhash_queue;
pub mod bloom;
pub mod loader_utils;
mod native_loader;
pub mod runtime;
mod runtime;
mod status_cache;
mod system_program;

View File

@@ -80,7 +80,8 @@ fn verify_error(err: ProgramError) -> ProgramError {
}
}
type StaticEntrypoint = fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), ProgramError>;
pub type StaticEntrypoint =
fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), ProgramError>;
pub struct Runtime {
static_entrypoints: Vec<(Pubkey, StaticEntrypoint)>,
@@ -221,30 +222,6 @@ impl Runtime {
}
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(
&self,
tx: &Transaction,
tx_accounts: &mut Vec<Account>,
) -> Result<(), TransactionError> {
// Simulate how the Bank automatically creates empty accounts as needed.
for _ in tx_accounts.len()..tx.account_keys.len() {
tx_accounts.push(Account::new(0, 0, &system_program::id()));
}
// Add a bogus loader accounts for each program id
let mut loaders = vec![];
for _ in 0..tx.program_ids.len() {
loaders.push(vec![(
Pubkey::default(),
Account::new(0, 0, &system_program::id()),
)]);
}
self.execute_transaction(tx, &mut loaders, tx_accounts, 0)
}
}
#[cfg(test)]