Allow tests to add instruction processors
Make runtime a private module again.
This commit is contained in:
parent
ee39f31d81
commit
55cdbedb52
|
@ -145,62 +145,45 @@ mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use solana_budget_api::budget_transaction::BudgetTransaction;
|
use solana_budget_api::budget_transaction::BudgetTransaction;
|
||||||
use solana_budget_api::id;
|
use solana_budget_api::id;
|
||||||
use solana_runtime::runtime::Runtime;
|
use solana_runtime::bank::Bank;
|
||||||
use solana_sdk::account::Account;
|
use solana_sdk::genesis_block::GenesisBlock;
|
||||||
use solana_sdk::hash::Hash;
|
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::system_program;
|
|
||||||
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};
|
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};
|
||||||
|
|
||||||
fn process_transaction(
|
fn create_bank(lamports: u64) -> (Bank, Keypair) {
|
||||||
tx: &Transaction,
|
let (genesis_block, mint_keypair) = GenesisBlock::new(lamports);
|
||||||
tx_accounts: &mut Vec<Account>,
|
let mut bank = Bank::new(&genesis_block);
|
||||||
) -> Result<(), TransactionError> {
|
bank.add_entrypoint(id(), process_instruction);
|
||||||
let mut runtime = Runtime::default();
|
(bank, mint_keypair)
|
||||||
runtime.add_entrypoint(id(), process_instruction);
|
|
||||||
runtime.process_transaction(tx, tx_accounts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_instruction() {
|
fn test_invalid_instruction() {
|
||||||
let mut accounts = vec![Account::new(1, 0, &id()), Account::new(0, 512, &id())];
|
let (bank, from) = create_bank(10_000);
|
||||||
let from = Keypair::new();
|
let blockhash = bank.last_blockhash();
|
||||||
let contract = Keypair::new();
|
let contract = Keypair::new();
|
||||||
let data = (1u8, 2u8, 3u8);
|
let data = (1u8, 2u8, 3u8);
|
||||||
let tx = Transaction::new_signed(
|
let tx = Transaction::new_signed(&from, &[contract.pubkey()], &id(), &data, blockhash, 0);
|
||||||
&from,
|
assert!(bank.process_transaction(&tx).is_err());
|
||||||
&[contract.pubkey()],
|
|
||||||
&id(),
|
|
||||||
&data,
|
|
||||||
Hash::default(),
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
assert!(process_transaction(&tx, &mut accounts).is_err());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unsigned_witness_key() {
|
fn test_unsigned_witness_key() {
|
||||||
let mut accounts = vec![Account::new(1, 0, &system_program::id())];
|
let (bank, from) = create_bank(10_000);
|
||||||
|
let blockhash = bank.last_blockhash();
|
||||||
|
|
||||||
// Initialize BudgetState
|
// Initialize BudgetState
|
||||||
let from = Keypair::new();
|
|
||||||
let contract = Keypair::new().pubkey();
|
let contract = Keypair::new().pubkey();
|
||||||
let to = Keypair::new().pubkey();
|
let to = Keypair::new().pubkey();
|
||||||
let witness = Keypair::new().pubkey();
|
let witness = Keypair::new().pubkey();
|
||||||
let tx = BudgetTransaction::new_when_signed(
|
let tx =
|
||||||
&from,
|
BudgetTransaction::new_when_signed(&from, &to, &contract, &witness, None, 1, blockhash);
|
||||||
&to,
|
bank.process_transaction(&tx).unwrap();
|
||||||
&contract,
|
|
||||||
&witness,
|
|
||||||
None,
|
|
||||||
1,
|
|
||||||
Hash::default(),
|
|
||||||
);
|
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
|
||||||
|
|
||||||
// Attack! Part 1: Sign a witness transaction with a random key.
|
// Attack! Part 1: Sign a witness transaction with a random key.
|
||||||
let rando = Keypair::new();
|
let rando = Keypair::new();
|
||||||
let mut tx = BudgetTransaction::new_signature(&rando, &contract, &to, Hash::default());
|
bank.transfer(1, &from, &rando.pubkey(), blockhash).unwrap();
|
||||||
|
let mut tx = BudgetTransaction::new_signature(&rando, &contract, &to, blockhash);
|
||||||
|
|
||||||
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
||||||
tx.account_keys.push(from.pubkey());
|
tx.account_keys.push(from.pubkey());
|
||||||
|
@ -208,7 +191,7 @@ mod test {
|
||||||
|
|
||||||
// Ensure the transaction fails because of the unsigned key.
|
// Ensure the transaction fails because of the unsigned key.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
process_transaction(&tx, &mut accounts),
|
bank.process_transaction(&tx),
|
||||||
Err(TransactionError::InstructionError(
|
Err(TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
||||||
|
@ -218,10 +201,10 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unsigned_timestamp() {
|
fn test_unsigned_timestamp() {
|
||||||
let mut accounts = vec![Account::new(1, 0, &system_program::id())];
|
let (bank, from) = create_bank(10_000);
|
||||||
|
let blockhash = bank.last_blockhash();
|
||||||
|
|
||||||
// Initialize BudgetState
|
// Initialize BudgetState
|
||||||
let from = Keypair::new();
|
|
||||||
let contract = Keypair::new().pubkey();
|
let contract = Keypair::new().pubkey();
|
||||||
let to = Keypair::new().pubkey();
|
let to = Keypair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
|
@ -233,13 +216,14 @@ mod test {
|
||||||
&from.pubkey(),
|
&from.pubkey(),
|
||||||
None,
|
None,
|
||||||
1,
|
1,
|
||||||
Hash::default(),
|
blockhash,
|
||||||
);
|
);
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
bank.process_transaction(&tx).unwrap();
|
||||||
|
|
||||||
// Attack! Part 1: Sign a timestamp transaction with a random key.
|
// Attack! Part 1: Sign a timestamp transaction with a random key.
|
||||||
let rando = Keypair::new();
|
let rando = Keypair::new();
|
||||||
let mut tx = BudgetTransaction::new_timestamp(&rando, &contract, &to, dt, Hash::default());
|
bank.transfer(1, &from, &rando.pubkey(), blockhash).unwrap();
|
||||||
|
let mut tx = BudgetTransaction::new_timestamp(&rando, &contract, &to, dt, blockhash);
|
||||||
|
|
||||||
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
||||||
tx.account_keys.push(from.pubkey());
|
tx.account_keys.push(from.pubkey());
|
||||||
|
@ -247,7 +231,7 @@ mod test {
|
||||||
|
|
||||||
// Ensure the transaction fails because of the unsigned key.
|
// Ensure the transaction fails because of the unsigned key.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
process_transaction(&tx, &mut accounts),
|
bank.process_transaction(&tx),
|
||||||
Err(TransactionError::InstructionError(
|
Err(TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
||||||
|
@ -257,11 +241,8 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_on_date() {
|
fn test_transfer_on_date() {
|
||||||
let mut accounts = vec![Account::new(1, 0, &system_program::id())];
|
let (bank, from) = create_bank(2);
|
||||||
let from_account = 0;
|
let blockhash = bank.last_blockhash();
|
||||||
let contract_account = 1;
|
|
||||||
let to_account = 2;
|
|
||||||
let from = Keypair::new();
|
|
||||||
let contract = Keypair::new();
|
let contract = Keypair::new();
|
||||||
let to = Keypair::new();
|
let to = Keypair::new();
|
||||||
let rando = Keypair::new();
|
let rando = Keypair::new();
|
||||||
|
@ -274,12 +255,14 @@ mod test {
|
||||||
&from.pubkey(),
|
&from.pubkey(),
|
||||||
None,
|
None,
|
||||||
1,
|
1,
|
||||||
Hash::default(),
|
blockhash,
|
||||||
);
|
);
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
bank.process_transaction(&tx).unwrap();
|
||||||
assert_eq!(accounts[from_account].lamports, 0);
|
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
||||||
assert_eq!(accounts[contract_account].lamports, 1);
|
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
||||||
let budget_state = BudgetState::deserialize(&accounts[contract_account].data).unwrap();
|
|
||||||
|
let contract_account = bank.get_account(&contract.pubkey()).unwrap();
|
||||||
|
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Attack! Try to payout to a rando key
|
// Attack! Try to payout to a rando key
|
||||||
|
@ -288,10 +271,10 @@ mod test {
|
||||||
&contract.pubkey(),
|
&contract.pubkey(),
|
||||||
&rando.pubkey(),
|
&rando.pubkey(),
|
||||||
dt,
|
dt,
|
||||||
Hash::default(),
|
blockhash,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
process_transaction(&tx, &mut accounts).unwrap_err(),
|
bank.process_transaction(&tx).unwrap_err(),
|
||||||
TransactionError::InstructionError(
|
TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::CustomError(
|
InstructionError::ProgramError(ProgramError::CustomError(
|
||||||
|
@ -299,11 +282,12 @@ mod test {
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(accounts[from_account].lamports, 0);
|
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
||||||
assert_eq!(accounts[contract_account].lamports, 1);
|
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
||||||
assert_eq!(accounts[to_account].lamports, 0);
|
assert_eq!(bank.get_balance(&to.pubkey()), 0);
|
||||||
|
|
||||||
let budget_state = BudgetState::deserialize(&accounts[contract_account].data).unwrap();
|
let contract_account = bank.get_account(&contract.pubkey()).unwrap();
|
||||||
|
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Now, acknowledge the time in the condition occurred and
|
// Now, acknowledge the time in the condition occurred and
|
||||||
|
@ -313,32 +297,23 @@ mod test {
|
||||||
&contract.pubkey(),
|
&contract.pubkey(),
|
||||||
&to.pubkey(),
|
&to.pubkey(),
|
||||||
dt,
|
dt,
|
||||||
Hash::default(),
|
blockhash,
|
||||||
);
|
);
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
bank.process_transaction(&tx).unwrap();
|
||||||
assert_eq!(accounts[from_account].lamports, 0);
|
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
||||||
assert_eq!(accounts[contract_account].lamports, 0);
|
assert_eq!(bank.get_balance(&contract.pubkey()), 0);
|
||||||
assert_eq!(accounts[to_account].lamports, 1);
|
assert_eq!(bank.get_balance(&to.pubkey()), 1);
|
||||||
|
assert_eq!(bank.get_account(&contract.pubkey()), None);
|
||||||
let budget_state = BudgetState::deserialize(&accounts[contract_account].data).unwrap();
|
|
||||||
assert!(!budget_state.is_pending());
|
|
||||||
|
|
||||||
// try to replay the timestamp contract
|
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
|
||||||
assert_eq!(accounts[from_account].lamports, 0);
|
|
||||||
assert_eq!(accounts[contract_account].lamports, 0);
|
|
||||||
assert_eq!(accounts[to_account].lamports, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cancel_transfer() {
|
fn test_cancel_transfer() {
|
||||||
let mut accounts = vec![Account::new(1, 0, &system_program::id())];
|
let (bank, from) = create_bank(3);
|
||||||
let from_account = 0;
|
let blockhash = bank.last_blockhash();
|
||||||
let contract_account = 1;
|
|
||||||
let pay_account = 2;
|
|
||||||
let from = Keypair::new();
|
|
||||||
let contract = Keypair::new();
|
let contract = Keypair::new();
|
||||||
let to = Keypair::new();
|
let to = Keypair::new();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
|
|
||||||
let tx = BudgetTransaction::new_on_date(
|
let tx = BudgetTransaction::new_on_date(
|
||||||
&from,
|
&from,
|
||||||
&to.pubkey(),
|
&to.pubkey(),
|
||||||
|
@ -347,51 +322,37 @@ mod test {
|
||||||
&from.pubkey(),
|
&from.pubkey(),
|
||||||
Some(from.pubkey()),
|
Some(from.pubkey()),
|
||||||
1,
|
1,
|
||||||
Hash::default(),
|
blockhash,
|
||||||
);
|
);
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
bank.process_transaction(&tx).unwrap();
|
||||||
assert_eq!(accounts[from_account].lamports, 0);
|
assert_eq!(bank.get_balance(&from.pubkey()), 2);
|
||||||
assert_eq!(accounts[contract_account].lamports, 1);
|
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
||||||
let budget_state = BudgetState::deserialize(&accounts[contract_account].data).unwrap();
|
|
||||||
|
let contract_account = bank.get_account(&contract.pubkey()).unwrap();
|
||||||
|
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Attack! try to put the lamports into the wrong account with cancel
|
// Attack! try to put the lamports into the wrong account with cancel
|
||||||
let tx = BudgetTransaction::new_signature(
|
let rando = Keypair::new();
|
||||||
&to,
|
bank.transfer(1, &from, &rando.pubkey(), blockhash).unwrap();
|
||||||
&contract.pubkey(),
|
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
||||||
&to.pubkey(),
|
|
||||||
Hash::default(),
|
let tx =
|
||||||
);
|
BudgetTransaction::new_signature(&rando, &contract.pubkey(), &to.pubkey(), blockhash);
|
||||||
// unit test hack, the `from account` is passed instead of the `to` account to avoid
|
// unit test hack, the `from account` is passed instead of the `to` account to avoid
|
||||||
// creating more account vectors
|
// creating more account vectors
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
bank.process_transaction(&tx).unwrap();
|
||||||
// nothing should be changed because apply witness didn't finalize a payment
|
// nothing should be changed because apply witness didn't finalize a payment
|
||||||
assert_eq!(accounts[from_account].lamports, 0);
|
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
||||||
assert_eq!(accounts[contract_account].lamports, 1);
|
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
||||||
assert_eq!(accounts.get(pay_account), None);
|
assert_eq!(bank.get_account(&to.pubkey()), None);
|
||||||
|
|
||||||
// Now, cancel the transaction. from gets her funds back
|
// Now, cancel the transaction. from gets her funds back
|
||||||
let tx = BudgetTransaction::new_signature(
|
let tx =
|
||||||
&from,
|
BudgetTransaction::new_signature(&from, &contract.pubkey(), &from.pubkey(), blockhash);
|
||||||
&contract.pubkey(),
|
bank.process_transaction(&tx).unwrap();
|
||||||
&from.pubkey(),
|
assert_eq!(bank.get_balance(&from.pubkey()), 2);
|
||||||
Hash::default(),
|
assert_eq!(bank.get_account(&contract.pubkey()), None);
|
||||||
);
|
assert_eq!(bank.get_account(&to.pubkey()), None);
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
|
||||||
assert_eq!(accounts[from_account].lamports, 1);
|
|
||||||
assert_eq!(accounts[contract_account].lamports, 0);
|
|
||||||
assert_eq!(accounts.get(pay_account), None);
|
|
||||||
|
|
||||||
// try to replay the cancel contract
|
|
||||||
let tx = BudgetTransaction::new_signature(
|
|
||||||
&from,
|
|
||||||
&contract.pubkey(),
|
|
||||||
&from.pubkey(),
|
|
||||||
Hash::default(),
|
|
||||||
);
|
|
||||||
process_transaction(&tx, &mut accounts).unwrap();
|
|
||||||
assert_eq!(accounts[from_account].lamports, 1);
|
|
||||||
assert_eq!(accounts[contract_account].lamports, 0);
|
|
||||||
assert_eq!(accounts.get(pay_account), None);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
use crate::runtime::{Runtime, StaticEntrypoint};
|
||||||
use crate::status_cache::StatusCache;
|
use crate::status_cache::StatusCache;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
|
@ -839,6 +839,21 @@ impl Bank {
|
||||||
let max_tick_height = (self.slot + 1) * self.ticks_per_slot - 1;
|
let max_tick_height = (self.slot + 1) * self.ticks_per_slot - 1;
|
||||||
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.
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -6,7 +6,7 @@ mod blockhash_queue;
|
||||||
pub mod bloom;
|
pub mod bloom;
|
||||||
pub mod loader_utils;
|
pub mod loader_utils;
|
||||||
mod native_loader;
|
mod native_loader;
|
||||||
pub mod runtime;
|
mod runtime;
|
||||||
mod status_cache;
|
mod status_cache;
|
||||||
mod system_program;
|
mod system_program;
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
pub struct Runtime {
|
||||||
static_entrypoints: Vec<(Pubkey, StaticEntrypoint)>,
|
static_entrypoints: Vec<(Pubkey, StaticEntrypoint)>,
|
||||||
|
@ -221,30 +222,6 @@ impl Runtime {
|
||||||
}
|
}
|
||||||
Ok(())
|
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)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue