Integrate Message into Transaction
This commit is contained in:
@ -599,9 +599,9 @@ impl AccountsDB {
|
||||
continue;
|
||||
}
|
||||
|
||||
let tx = &txs[i];
|
||||
let message = &txs[i].message();
|
||||
let acc = raccs.as_ref().unwrap();
|
||||
for (key, account) in tx.account_keys.iter().zip(acc.0.iter()) {
|
||||
for (key, account) in message.account_keys.iter().zip(acc.0.iter()) {
|
||||
self.store(fork, key, account);
|
||||
}
|
||||
}
|
||||
@ -614,11 +614,12 @@ impl AccountsDB {
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Result<Vec<Account>> {
|
||||
// Copy all the accounts
|
||||
if tx.signatures.is_empty() && tx.fee != 0 {
|
||||
let message = tx.message();
|
||||
if tx.signatures.is_empty() && message.fee != 0 {
|
||||
Err(TransactionError::MissingSignatureForFee)
|
||||
} else {
|
||||
// Check for unique account keys
|
||||
if has_duplicates(&tx.account_keys) {
|
||||
if has_duplicates(&message.account_keys) {
|
||||
error_counters.account_loaded_twice += 1;
|
||||
return Err(TransactionError::AccountLoadedTwice);
|
||||
}
|
||||
@ -626,17 +627,17 @@ impl AccountsDB {
|
||||
// There is no way to predict what program will execute without an error
|
||||
// If a fee can pay for execution then the program will be scheduled
|
||||
let mut called_accounts: Vec<Account> = vec![];
|
||||
for key in &tx.account_keys {
|
||||
for key in &message.account_keys {
|
||||
called_accounts.push(self.load(fork, key, true).unwrap_or_default());
|
||||
}
|
||||
if called_accounts.is_empty() || called_accounts[0].lamports == 0 {
|
||||
error_counters.account_not_found += 1;
|
||||
Err(TransactionError::AccountNotFound)
|
||||
} else if called_accounts[0].lamports < tx.fee {
|
||||
} else if called_accounts[0].lamports < message.fee {
|
||||
error_counters.insufficient_funds += 1;
|
||||
Err(TransactionError::InsufficientFundsForFee)
|
||||
} else {
|
||||
called_accounts[0].lamports -= tx.fee;
|
||||
called_accounts[0].lamports -= message.fee;
|
||||
Ok(called_accounts)
|
||||
}
|
||||
}
|
||||
@ -690,14 +691,16 @@ impl AccountsDB {
|
||||
tx: &Transaction,
|
||||
error_counters: &mut ErrorCounters,
|
||||
) -> Result<Vec<Vec<(Pubkey, Account)>>> {
|
||||
tx.instructions
|
||||
let message = tx.message();
|
||||
message
|
||||
.instructions
|
||||
.iter()
|
||||
.map(|ix| {
|
||||
if tx.program_ids.len() <= ix.program_ids_index as usize {
|
||||
if message.program_ids.len() <= ix.program_ids_index as usize {
|
||||
error_counters.account_not_found += 1;
|
||||
return Err(TransactionError::AccountNotFound);
|
||||
}
|
||||
let program_id = tx.program_ids[ix.program_ids_index as usize];
|
||||
let program_id = message.program_ids[ix.program_ids_index as usize];
|
||||
self.load_executable_accounts(fork, &program_id, error_counters)
|
||||
})
|
||||
.collect()
|
||||
@ -881,7 +884,7 @@ impl Accounts {
|
||||
Err(TransactionError::AccountInUse) => (),
|
||||
_ => {
|
||||
if let Some(locks) = account_locks.get_mut(&fork) {
|
||||
for k in &tx.account_keys {
|
||||
for k in &tx.message().account_keys {
|
||||
locks.remove(k);
|
||||
}
|
||||
if locks.is_empty() {
|
||||
@ -908,7 +911,7 @@ impl Accounts {
|
||||
Self::lock_account(
|
||||
fork,
|
||||
&mut account_locks,
|
||||
&tx.account_keys,
|
||||
&tx.message().account_keys,
|
||||
&mut error_counters,
|
||||
)
|
||||
})
|
||||
|
@ -497,7 +497,9 @@ impl Bank {
|
||||
txs.iter()
|
||||
.zip(lock_results.into_iter())
|
||||
.map(|(tx, lock_res)| {
|
||||
if lock_res.is_ok() && !hash_queue.check_hash_age(tx.recent_blockhash, max_age) {
|
||||
if lock_res.is_ok()
|
||||
&& !hash_queue.check_hash_age(tx.message().recent_blockhash, max_age)
|
||||
{
|
||||
error_counters.reserve_blockhash += 1;
|
||||
Err(TransactionError::BlockhashNotFound)
|
||||
} else {
|
||||
@ -566,7 +568,7 @@ impl Bank {
|
||||
Err(e) => Err(e.clone()),
|
||||
Ok((ref mut accounts, ref mut loaders)) => {
|
||||
self.runtime
|
||||
.execute_transaction(tx, loaders, accounts, tick_height)
|
||||
.process_message(tx.message(), loaders, accounts, tick_height)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@ -652,18 +654,21 @@ impl Bank {
|
||||
let results = txs
|
||||
.iter()
|
||||
.zip(executed.iter())
|
||||
.map(|(tx, res)| match *res {
|
||||
Err(TransactionError::InstructionError(_, _)) => {
|
||||
// Charge the transaction fee even in case of InstructionError
|
||||
self.withdraw(&tx.account_keys[0], tx.fee)?;
|
||||
fees += tx.fee;
|
||||
Ok(())
|
||||
.map(|(tx, res)| {
|
||||
let message = tx.message();
|
||||
match *res {
|
||||
Err(TransactionError::InstructionError(_, _)) => {
|
||||
// Charge the transaction fee even in case of InstructionError
|
||||
self.withdraw(&message.account_keys[0], message.fee)?;
|
||||
fees += message.fee;
|
||||
Ok(())
|
||||
}
|
||||
Ok(()) => {
|
||||
fees += message.fee;
|
||||
Ok(())
|
||||
}
|
||||
_ => res.clone(),
|
||||
}
|
||||
Ok(()) => {
|
||||
fees += tx.fee;
|
||||
Ok(())
|
||||
}
|
||||
_ => res.clone(),
|
||||
})
|
||||
.collect();
|
||||
self.deposit(&self.collector_id, fees);
|
||||
@ -1308,14 +1313,14 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut tx_invalid_program_index = tx.clone();
|
||||
tx_invalid_program_index.instructions[0].program_ids_index = 42;
|
||||
tx_invalid_program_index.message.instructions[0].program_ids_index = 42;
|
||||
assert_eq!(
|
||||
bank.process_transaction(&tx_invalid_program_index),
|
||||
Err(TransactionError::InvalidAccountIndex)
|
||||
);
|
||||
|
||||
let mut tx_invalid_account_index = tx.clone();
|
||||
tx_invalid_account_index.instructions[0].accounts[0] = 42;
|
||||
tx_invalid_account_index.message.instructions[0].accounts[0] = 42;
|
||||
assert_eq!(
|
||||
bank.process_transaction(&tx_invalid_account_index),
|
||||
Err(TransactionError::InvalidAccountIndex)
|
||||
@ -1598,7 +1603,7 @@ mod tests {
|
||||
|
||||
// Set the fee to 0, this should give an InstructionError
|
||||
// but since no signature we cannot look up the error.
|
||||
tx.fee = 0;
|
||||
tx.message.fee = 0;
|
||||
|
||||
assert_eq!(bank.process_transaction(&tx), Ok(()));
|
||||
assert_eq!(bank.get_balance(&key.pubkey()), 0);
|
||||
|
@ -1,9 +1,10 @@
|
||||
use crate::native_loader;
|
||||
use solana_sdk::account::{create_keyed_accounts, Account, KeyedAccount};
|
||||
use solana_sdk::instruction::InstructionError;
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::transaction::{Transaction, TransactionError};
|
||||
use solana_sdk::transaction::TransactionError;
|
||||
|
||||
/// Return true if the slice has any duplicate elements
|
||||
pub fn has_duplicates<T: PartialEq>(xs: &[T]) -> bool {
|
||||
@ -113,22 +114,22 @@ impl Runtime {
|
||||
/// This method calls the instruction's program entrypoint method
|
||||
fn process_instruction(
|
||||
&self,
|
||||
tx: &Transaction,
|
||||
message: &Message,
|
||||
instruction_index: usize,
|
||||
executable_accounts: &mut [(Pubkey, Account)],
|
||||
program_accounts: &mut [&mut Account],
|
||||
tick_height: u64,
|
||||
) -> Result<(), InstructionError> {
|
||||
let program_id = tx.program_id(instruction_index);
|
||||
let program_id = message.program_id(instruction_index);
|
||||
|
||||
let mut keyed_accounts = create_keyed_accounts(executable_accounts);
|
||||
let mut keyed_accounts2: Vec<_> = tx.instructions[instruction_index]
|
||||
let mut keyed_accounts2: Vec<_> = message.instructions[instruction_index]
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|&index| {
|
||||
let index = index as usize;
|
||||
let key = &tx.account_keys[index];
|
||||
(key, index < tx.signatures.len())
|
||||
let key = &message.account_keys[index];
|
||||
(key, index < message.num_signatures as usize)
|
||||
})
|
||||
.zip(program_accounts.iter_mut())
|
||||
.map(|((key, is_signer), account)| KeyedAccount::new(key, is_signer, account))
|
||||
@ -140,7 +141,7 @@ impl Runtime {
|
||||
return process_instruction(
|
||||
&program_id,
|
||||
&mut keyed_accounts[1..],
|
||||
&tx.instructions[instruction_index].data,
|
||||
&message.instructions[instruction_index].data,
|
||||
tick_height,
|
||||
);
|
||||
}
|
||||
@ -149,7 +150,7 @@ impl Runtime {
|
||||
native_loader::entrypoint(
|
||||
&program_id,
|
||||
&mut keyed_accounts,
|
||||
&tx.instructions[instruction_index].data,
|
||||
&message.instructions[instruction_index].data,
|
||||
tick_height,
|
||||
)
|
||||
}
|
||||
@ -160,13 +161,13 @@ impl Runtime {
|
||||
/// The accounts are committed back to the bank only if this function returns Ok(_).
|
||||
fn execute_instruction(
|
||||
&self,
|
||||
tx: &Transaction,
|
||||
message: &Message,
|
||||
instruction_index: usize,
|
||||
executable_accounts: &mut [(Pubkey, Account)],
|
||||
program_accounts: &mut [&mut Account],
|
||||
tick_height: u64,
|
||||
) -> Result<(), InstructionError> {
|
||||
let program_id = tx.program_id(instruction_index);
|
||||
let program_id = message.program_id(instruction_index);
|
||||
// TODO: the runtime should be checking read/write access to memory
|
||||
// we are trusting the hard-coded programs not to clobber or allocate
|
||||
let pre_total: u64 = program_accounts.iter().map(|a| a.lamports).sum();
|
||||
@ -176,7 +177,7 @@ impl Runtime {
|
||||
.collect();
|
||||
|
||||
self.process_instruction(
|
||||
tx,
|
||||
message,
|
||||
instruction_index,
|
||||
executable_accounts,
|
||||
program_accounts,
|
||||
@ -204,22 +205,22 @@ impl Runtime {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Execute a transaction.
|
||||
/// This method calls each instruction in the transaction over the set of loaded Accounts
|
||||
/// Process a message.
|
||||
/// This method calls each instruction in the message over the set of loaded Accounts
|
||||
/// The accounts are committed back to the bank only if every instruction succeeds
|
||||
pub fn execute_transaction(
|
||||
pub fn process_message(
|
||||
&self,
|
||||
tx: &Transaction,
|
||||
message: &Message,
|
||||
loaders: &mut [Vec<(Pubkey, Account)>],
|
||||
tx_accounts: &mut [Account],
|
||||
accounts: &mut [Account],
|
||||
tick_height: u64,
|
||||
) -> Result<(), TransactionError> {
|
||||
for (instruction_index, instruction) in tx.instructions.iter().enumerate() {
|
||||
for (instruction_index, instruction) in message.instructions.iter().enumerate() {
|
||||
let executable_accounts = &mut loaders[instruction.program_ids_index as usize];
|
||||
let mut program_accounts = get_subset_unchecked_mut(tx_accounts, &instruction.accounts)
|
||||
let mut program_accounts = get_subset_unchecked_mut(accounts, &instruction.accounts)
|
||||
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?;
|
||||
self.execute_instruction(
|
||||
tx,
|
||||
message,
|
||||
instruction_index,
|
||||
executable_accounts,
|
||||
&mut program_accounts,
|
||||
|
Reference in New Issue
Block a user