Don't call instructions transactions

This commit is contained in:
Greg Fitzgerald 2018-11-11 20:50:03 -07:00 committed by Grimes
parent a00284c727
commit 5df4754579
2 changed files with 23 additions and 21 deletions

View File

@ -78,8 +78,9 @@ pub enum BankError {
/// Proof of History verification failed. /// Proof of History verification failed.
LedgerVerificationFailed, LedgerVerificationFailed,
/// Contract's transaction token balance does not equal the balance after the transaction
UnbalancedTransaction(u8), /// Contract's instruction token balance does not equal the balance after the instruction
UnbalancedInstruction(u8),
/// Contract's transactions resulted in an account with a negative balance /// Contract's transactions resulted in an account with a negative balance
/// The difference from InsufficientFundsForFee is that the transaction was executed by the /// The difference from InsufficientFundsForFee is that the transaction was executed by the
@ -715,9 +716,9 @@ impl Bank {
}).collect() }).collect()
} }
pub fn verify_transaction( pub fn verify_instruction(
instruction_index: usize, instruction_index: usize,
tx_program_id: &Pubkey, program_id: &Pubkey,
pre_program_id: &Pubkey, pre_program_id: &Pubkey,
pre_tokens: u64, pre_tokens: u64,
account: &Account, account: &Account,
@ -725,11 +726,11 @@ impl Bank {
// Verify the transaction // Verify the transaction
// Make sure that program_id is still the same or this was just assigned by the system call contract // Make sure that program_id is still the same or this was just assigned by the system call contract
if *pre_program_id != account.program_id && !SystemProgram::check_id(&tx_program_id) { if *pre_program_id != account.program_id && !SystemProgram::check_id(&program_id) {
return Err(BankError::ModifiedContractId(instruction_index as u8)); return Err(BankError::ModifiedContractId(instruction_index as u8));
} }
// For accounts unassigned to the contract, the individual balance of each accounts cannot decrease. // For accounts unassigned to the contract, the individual balance of each accounts cannot decrease.
if *tx_program_id != account.program_id && pre_tokens > account.tokens { if *program_id != account.program_id && pre_tokens > account.tokens {
return Err(BankError::ExternalAccountTokenSpend( return Err(BankError::ExternalAccountTokenSpend(
instruction_index as u8, instruction_index as u8,
)); ));
@ -764,7 +765,7 @@ impl Bank {
instruction_index: usize, instruction_index: usize,
program_accounts: &mut [&mut Account], program_accounts: &mut [&mut Account],
) -> Result<()> { ) -> Result<()> {
let tx_program_id = tx.program_id(instruction_index); let program_id = tx.program_id(instruction_index);
// TODO: the runtime should be checking read/write access to memory // TODO: the runtime should be checking read/write access to memory
// we are trusting the hard coded contracts not to clobber or allocate // we are trusting the hard coded contracts not to clobber or allocate
let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum(); let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum();
@ -775,7 +776,7 @@ impl Bank {
// Call the contract method // Call the contract method
// It's up to the contract to implement its own rules on moving funds // It's up to the contract to implement its own rules on moving funds
if SystemProgram::check_id(&tx_program_id) { if SystemProgram::check_id(&program_id) {
if let Err(err) = if let Err(err) =
SystemProgram::process_transaction(&tx, instruction_index, program_accounts) SystemProgram::process_transaction(&tx, instruction_index, program_accounts)
{ {
@ -785,17 +786,17 @@ impl Bank {
}; };
return Err(err); return Err(err);
} }
} else if BudgetState::check_id(&tx_program_id) { } else if BudgetState::check_id(&program_id) {
if BudgetState::process_transaction(&tx, instruction_index, program_accounts).is_err() { if BudgetState::process_transaction(&tx, instruction_index, program_accounts).is_err() {
return Err(BankError::ProgramRuntimeError(instruction_index as u8)); return Err(BankError::ProgramRuntimeError(instruction_index as u8));
} }
} else if StorageProgram::check_id(&tx_program_id) { } else if StorageProgram::check_id(&program_id) {
if StorageProgram::process_transaction(&tx, instruction_index, program_accounts) if StorageProgram::process_transaction(&tx, instruction_index, program_accounts)
.is_err() .is_err()
{ {
return Err(BankError::ProgramRuntimeError(instruction_index as u8)); return Err(BankError::ProgramRuntimeError(instruction_index as u8));
} }
} else if VoteProgram::check_id(&tx_program_id) { } else if VoteProgram::check_id(&program_id) {
VoteProgram::process_transaction(&tx, instruction_index, program_accounts).is_err(); VoteProgram::process_transaction(&tx, instruction_index, program_accounts).is_err();
} else { } else {
let mut depth = 0; let mut depth = 0;
@ -843,7 +844,7 @@ impl Bank {
}).collect(); }).collect();
keyed_accounts.append(&mut keyed_accounts2); keyed_accounts.append(&mut keyed_accounts2);
if !native_loader::process_transaction( if !native_loader::process_instruction(
&mut keyed_accounts, &mut keyed_accounts,
&tx.instructions[instruction_index].userdata, &tx.instructions[instruction_index].userdata,
) { ) {
@ -851,26 +852,27 @@ impl Bank {
} }
} }
// Verify the transaction // Verify the instruction
for ((pre_program_id, pre_tokens), post_account) in for ((pre_program_id, pre_tokens), post_account) in
pre_data.iter().zip(program_accounts.iter()) pre_data.iter().zip(program_accounts.iter())
{ {
Self::verify_transaction( Self::verify_instruction(
instruction_index, instruction_index,
&tx_program_id, &program_id,
pre_program_id, pre_program_id,
*pre_tokens, *pre_tokens,
post_account, post_account,
)?; )?;
} }
// The total sum of all the tokens in all the pages cannot change. // The total sum of all the tokens in all the accounts cannot change.
let post_total: u64 = program_accounts.iter().map(|a| a.tokens).sum(); let post_total: u64 = program_accounts.iter().map(|a| a.tokens).sum();
if pre_total != post_total { if pre_total != post_total {
Err(BankError::UnbalancedTransaction(instruction_index as u8)) Err(BankError::UnbalancedInstruction(instruction_index as u8))
} else { } else {
Ok(()) Ok(())
} }
} }
/// Execute a transaction. /// Execute a transaction.
/// This method calls each instruction in the transaction over the set of loaded Accounts /// This method calls each instruction in the transaction over the set of loaded Accounts
/// The accounts are committed back to the bank only if every instruction succeeds /// The accounts are committed back to the bank only if every instruction succeeds

View File

@ -56,7 +56,7 @@ pub fn id() -> Pubkey {
Pubkey::new(&NATIVE_LOADER_PROGRAM_ID) Pubkey::new(&NATIVE_LOADER_PROGRAM_ID)
} }
pub fn process_transaction(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8]) -> bool { pub fn process_instruction(keyed_accounts: &mut [KeyedAccount], ix_userdata: &[u8]) -> bool {
if keyed_accounts[0].account.executable { if keyed_accounts[0].account.executable {
// dispatch it // dispatch it
let name = keyed_accounts[0].account.userdata.clone(); let name = keyed_accounts[0].account.userdata.clone();
@ -79,14 +79,14 @@ pub fn process_transaction(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8])
return false; return false;
} }
}; };
return entrypoint(&mut keyed_accounts[1..], tx_data); return entrypoint(&mut keyed_accounts[1..], ix_userdata);
}, },
Err(e) => { Err(e) => {
warn!("Unable to load: {:?}", e); warn!("Unable to load: {:?}", e);
return false; return false;
} }
} }
} else if let Ok(instruction) = deserialize(tx_data) { } else if let Ok(instruction) = deserialize(ix_userdata) {
match instruction { match instruction {
LoaderInstruction::Write { offset, bytes } => { LoaderInstruction::Write { offset, bytes } => {
trace!("NativeLoader::Write offset {} bytes {:?}", offset, bytes); trace!("NativeLoader::Write offset {} bytes {:?}", offset, bytes);
@ -109,7 +109,7 @@ pub fn process_transaction(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8])
} }
} }
} else { } else {
warn!("Invalid program transaction: {:?}", tx_data); warn!("Invalid userdata in instruction: {:?}", ix_userdata);
} }
true true
} }