Rename StaticEntrypoint to ProcessInstruction

This commit is contained in:
Greg Fitzgerald 2019-03-16 17:20:09 -06:00 committed by Grimes
parent ae4d14a2ad
commit c09accb685
4 changed files with 26 additions and 45 deletions

View File

@ -154,7 +154,7 @@ mod test {
fn create_bank(lamports: u64) -> (Bank, Keypair) { fn create_bank(lamports: u64) -> (Bank, Keypair) {
let (genesis_block, mint_keypair) = GenesisBlock::new(lamports); let (genesis_block, mint_keypair) = GenesisBlock::new(lamports);
let mut bank = Bank::new(&genesis_block); let mut bank = Bank::new(&genesis_block);
bank.add_entrypoint(id(), process_instruction); bank.add_instruction_processor(id(), process_instruction);
(bank, mint_keypair) (bank, mint_keypair)
} }

View File

@ -1,32 +0,0 @@
use solana_budget_api::budget_transaction::BudgetTransaction;
use solana_runtime::bank::{Bank, Result};
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
struct BudgetBank<'a> {
bank: &'a Bank,
}
impl<'a> BudgetBank<'a> {
fn new(bank: &'a Bank) -> Self {
bank.add_native_program("solana_budget_program", &solana_budget_api::id());
Self { bank }
}
fn pay(&self, from_keypair: &Keypair, to_id: &Pubkey, lamports: u64) -> Result<()> {
let blockhash = self.bank.last_blockhash();
let tx = BudgetTransaction::new_payment(from_keypair, to_id, lamports, blockhash, 0);
self.bank.process_transaction(&tx)
}
}
#[test]
fn test_budget_payment_via_bank() {
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
let bank = Bank::new(&genesis_block);
let budget_bank = BudgetBank::new(&bank);
let to_id = Keypair::new().pubkey();
budget_bank.pay(&from_keypair, &to_id, 100).unwrap();
assert_eq!(bank.get_balance(&to_id), 100);
}

View File

@ -5,7 +5,7 @@
use crate::accounts::{Accounts, ErrorCounters, InstructionAccounts, InstructionLoaders}; use crate::accounts::{Accounts, ErrorCounters, InstructionAccounts, InstructionLoaders};
use crate::blockhash_queue::BlockhashQueue; use crate::blockhash_queue::BlockhashQueue;
use crate::runtime::{Runtime, StaticEntrypoint}; use crate::runtime::{ProcessInstruction, Runtime};
use crate::status_cache::StatusCache; use crate::status_cache::StatusCache;
use bincode::serialize; use bincode::serialize;
use hashbrown::HashMap; use hashbrown::HashMap;
@ -840,9 +840,14 @@ impl Bank {
self.is_delta.load(Ordering::Relaxed) && self.tick_height() == max_tick_height self.is_delta.load(Ordering::Relaxed) && self.tick_height() == max_tick_height
} }
/// Add a static entrypoint to intercept intructions before the dynamic loader. /// Add an instruction processor to intercept intructions before the dynamic loader.
pub fn add_entrypoint(&mut self, program_id: Pubkey, entrypoint: StaticEntrypoint) { pub fn add_instruction_processor(
self.runtime.add_entrypoint(program_id, entrypoint); &mut self,
program_id: Pubkey,
process_instruction: ProcessInstruction,
) {
self.runtime
.add_instruction_processor(program_id, process_instruction);
// Add a bogus executable account to load. // Add a bogus executable account to load.
let bogus_account = Account { let bogus_account = Account {

View File

@ -80,25 +80,33 @@ fn verify_error(err: ProgramError) -> ProgramError {
} }
} }
pub type StaticEntrypoint = pub type ProcessInstruction =
fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), ProgramError>; fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), ProgramError>;
pub struct Runtime { pub struct Runtime {
static_entrypoints: Vec<(Pubkey, StaticEntrypoint)>, instruction_processors: Vec<(Pubkey, ProcessInstruction)>,
} }
impl Default for Runtime { impl Default for Runtime {
fn default() -> Self { fn default() -> Self {
let static_entrypoints: Vec<(Pubkey, StaticEntrypoint)> = let instruction_processors: Vec<(Pubkey, ProcessInstruction)> =
vec![(system_program::id(), crate::system_program::entrypoint)]; vec![(system_program::id(), crate::system_program::entrypoint)];
Self { static_entrypoints }
Self {
instruction_processors,
}
} }
} }
impl Runtime { impl Runtime {
/// Add a static entrypoint to intercept intructions before the dynamic loader. /// Add a static entrypoint to intercept intructions before the dynamic loader.
pub fn add_entrypoint(&mut self, program_id: Pubkey, entrypoint: StaticEntrypoint) { pub fn add_instruction_processor(
self.static_entrypoints.push((program_id, entrypoint)); &mut self,
program_id: Pubkey,
process_instruction: ProcessInstruction,
) {
self.instruction_processors
.push((program_id, process_instruction));
} }
/// Process an instruction /// Process an instruction
@ -127,9 +135,9 @@ impl Runtime {
.collect(); .collect();
keyed_accounts.append(&mut keyed_accounts2); keyed_accounts.append(&mut keyed_accounts2);
for (id, entrypoint) in &self.static_entrypoints { for (id, process_instruction) in &self.instruction_processors {
if id == program_id { if id == program_id {
return entrypoint( return process_instruction(
&program_id, &program_id,
&mut keyed_accounts[1..], &mut keyed_accounts[1..],
&tx.instructions[instruction_index].data, &tx.instructions[instruction_index].data,