Add process_message() to BankClient

This commit is contained in:
Greg Fitzgerald 2019-03-27 05:36:01 -06:00
parent 16ff4ac1a8
commit 55115d0eeb
3 changed files with 25 additions and 26 deletions

View File

@ -149,8 +149,9 @@ mod tests {
use solana_runtime::bank_client::BankClient; use solana_runtime::bank_client::BankClient;
use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::instruction::InstructionError; use solana_sdk::instruction::InstructionError;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::transaction::{Transaction, TransactionError}; use solana_sdk::transaction::TransactionError;
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);
@ -196,15 +197,15 @@ mod tests {
alice_client.transfer(1, &mallory_pubkey).unwrap(); alice_client.transfer(1, &mallory_pubkey).unwrap();
let instruction = let instruction =
BudgetInstruction::new_apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey); BudgetInstruction::new_apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
let mut transaction = Transaction::new_unsigned_instructions(vec![instruction]); let mut message = Message::new(vec![instruction]);
// Attack! Part 2: Point the instruction to the expected, but unsigned, key. // Attack! Part 2: Point the instruction to the expected, but unsigned, key.
transaction.account_keys.push(alice_pubkey); message.account_keys.push(alice_pubkey);
transaction.instructions[0].accounts[0] = 3; message.instructions[0].accounts[0] = 3;
// Ensure the transaction fails because of the unsigned key. // Ensure the transaction fails because of the unsigned key.
assert_eq!( assert_eq!(
mallory_client.process_transaction(transaction), mallory_client.process_message(message),
Err(TransactionError::InstructionError( Err(TransactionError::InstructionError(
0, 0,
InstructionError::MissingRequiredSignature InstructionError::MissingRequiredSignature
@ -243,15 +244,15 @@ mod tests {
&bob_pubkey, &bob_pubkey,
dt, dt,
); );
let mut transaction = Transaction::new_unsigned_instructions(vec![instruction]); let mut message = Message::new(vec![instruction]);
// Attack! Part 2: Point the instruction to the expected, but unsigned, key. // Attack! Part 2: Point the instruction to the expected, but unsigned, key.
transaction.account_keys.push(alice_pubkey); message.account_keys.push(alice_pubkey);
transaction.instructions[0].accounts[0] = 3; message.instructions[0].accounts[0] = 3;
// Ensure the transaction fails because of the unsigned key. // Ensure the transaction fails because of the unsigned key.
assert_eq!( assert_eq!(
mallory_client.process_transaction(transaction), mallory_client.process_message(message),
Err(TransactionError::InstructionError( Err(TransactionError::InstructionError(
0, 0,
InstructionError::MissingRequiredSignature InstructionError::MissingRequiredSignature

View File

@ -34,9 +34,9 @@ mod tests {
use solana_runtime::bank::Bank; use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient; use solana_runtime::bank_client::BankClient;
use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::Transaction;
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)] #[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
struct MyConfig { struct MyConfig {
@ -131,9 +131,9 @@ mod tests {
let instruction = ConfigInstruction::new_store(&from_pubkey, &config_pubkey, &my_config); let instruction = ConfigInstruction::new_store(&from_pubkey, &config_pubkey, &my_config);
// Replace instruction data with a vector that's too large // Replace instruction data with a vector that's too large
let mut transaction = Transaction::new_unsigned_instructions(vec![instruction]); let mut message = Message::new(vec![instruction]);
transaction.instructions[0].data = vec![0; 123]; message.instructions[0].data = vec![0; 123];
config_client.process_transaction(transaction).unwrap_err(); config_client.process_message(message).unwrap_err();
} }
#[test] #[test]
@ -149,14 +149,14 @@ mod tests {
let move_instruction = SystemInstruction::new_move(&system_pubkey, &Pubkey::default(), 42); let move_instruction = SystemInstruction::new_move(&system_pubkey, &Pubkey::default(), 42);
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let store_instruction = let mut store_instruction =
ConfigInstruction::new_store(&from_pubkey, &config_pubkey, &my_config); ConfigInstruction::new_store(&from_pubkey, &config_pubkey, &my_config);
store_instruction.accounts[0].is_signer = false;
store_instruction.accounts[1].is_signer = false;
// Don't sign the transaction with `config_client` // Don't sign the transaction with `config_client`
let mut transaction = let message = Message::new(vec![move_instruction, store_instruction]);
Transaction::new_unsigned_instructions(vec![move_instruction, store_instruction]);
transaction.sign_unchecked(&[&system_keypair], bank.last_blockhash());
let system_client = BankClient::new(&bank, system_keypair); let system_client = BankClient::new(&bank, system_keypair);
system_client.process_transaction(transaction).unwrap_err(); system_client.process_message(message).unwrap_err();
} }
} }

View File

@ -1,5 +1,6 @@
use crate::bank::Bank; use crate::bank::Bank;
use solana_sdk::instruction::Instruction; use solana_sdk::instruction::Instruction;
use solana_sdk::message::Message;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_instruction::SystemInstruction;
@ -28,14 +29,11 @@ impl<'a> BankClient<'a> {
self.keypairs.iter().map(|x| x.pubkey()).collect() self.keypairs.iter().map(|x| x.pubkey()).collect()
} }
fn sign(&self, tx: &mut Transaction) { pub fn process_message(&self, message: Message) -> Result<(), TransactionError> {
let keypairs: Vec<_> = self.keypairs.iter().collect(); let keypairs: Vec<_> = self.keypairs.iter().collect();
tx.sign(&keypairs, self.bank.last_blockhash()); let blockhash = self.bank.last_blockhash();
} let transaction = Transaction::new(&keypairs, message, blockhash);
self.bank.process_transaction(&transaction)
pub fn process_transaction(&self, mut tx: Transaction) -> Result<(), TransactionError> {
self.sign(&mut tx);
self.bank.process_transaction(&tx)
} }
/// Create and process a transaction from a list of instructions. /// Create and process a transaction from a list of instructions.
@ -43,7 +41,7 @@ impl<'a> BankClient<'a> {
&self, &self,
instructions: Vec<Instruction>, instructions: Vec<Instruction>,
) -> Result<(), TransactionError> { ) -> Result<(), TransactionError> {
self.process_transaction(Transaction::new_unsigned_instructions(instructions)) self.process_message(Message::new(instructions))
} }
/// Create and process a transaction from a single instruction. /// Create and process a transaction from a single instruction.