CustomError from Vec->u32

This commit is contained in:
Tyera Eulberg
2019-04-11 11:41:12 -07:00
committed by Tyera Eulberg
parent f669ae5868
commit d31989f878
5 changed files with 12 additions and 42 deletions

View File

@ -2,7 +2,7 @@
use crate::budget_expr::Witness; use crate::budget_expr::Witness;
use crate::budget_instruction::BudgetInstruction; use crate::budget_instruction::BudgetInstruction;
use crate::budget_state::{BudgetError, BudgetState}; use crate::budget_state::{BudgetError, BudgetState};
use bincode::{deserialize, serialize}; use bincode::deserialize;
use chrono::prelude::{DateTime, Utc}; use chrono::prelude::{DateTime, Utc};
use log::*; use log::*;
use solana_sdk::account::KeyedAccount; use solana_sdk::account::KeyedAccount;
@ -115,7 +115,7 @@ pub fn process_instruction(
} }
trace!("apply timestamp"); trace!("apply timestamp");
apply_timestamp(&mut budget_state, keyed_accounts, dt) apply_timestamp(&mut budget_state, keyed_accounts, dt)
.map_err(|e| InstructionError::CustomError(serialize(&e).unwrap()))?; .map_err(|e| InstructionError::CustomError(e as u32))?;
trace!("apply timestamp committed"); trace!("apply timestamp committed");
budget_state.serialize(&mut keyed_accounts[1].account.data) budget_state.serialize(&mut keyed_accounts[1].account.data)
} }
@ -133,7 +133,7 @@ pub fn process_instruction(
} }
trace!("apply signature"); trace!("apply signature");
apply_signature(&mut budget_state, keyed_accounts) apply_signature(&mut budget_state, keyed_accounts)
.map_err(|e| InstructionError::CustomError(serialize(&e).unwrap()))?; .map_err(|e| InstructionError::CustomError(e as u32))?;
trace!("apply signature committed"); trace!("apply signature committed");
budget_state.serialize(&mut keyed_accounts[1].account.data) budget_state.serialize(&mut keyed_accounts[1].account.data)
} }
@ -312,7 +312,7 @@ mod tests {
.unwrap(), .unwrap(),
TransactionError::InstructionError( TransactionError::InstructionError(
0, 0,
InstructionError::CustomError(serialize(&BudgetError::DestinationMissing).unwrap()) InstructionError::CustomError(BudgetError::DestinationMissing as u32)
) )
); );
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 1); assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 1);

View File

@ -1,5 +1,4 @@
use crate::token_state::TokenState; use crate::token_state::TokenState;
use bincode::serialize;
use log::*; use log::*;
use solana_sdk::account::KeyedAccount; use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError; use solana_sdk::instruction::InstructionError;
@ -15,6 +14,6 @@ pub fn process_instruction(
TokenState::process(program_id, info, input).map_err(|e| { TokenState::process(program_id, info, input).map_err(|e| {
error!("error: {:?}", e); error!("error: {:?}", e);
InstructionError::CustomError(serialize(&e).unwrap()) InstructionError::CustomError(e as u32)
}) })
} }

View File

@ -72,16 +72,6 @@ fn verify_instruction(
Ok(()) Ok(())
} }
fn verify_error(err: InstructionError) -> InstructionError {
match err {
InstructionError::CustomError(mut error) => {
error.truncate(32);
InstructionError::CustomError(error)
}
e => e,
}
}
pub type ProcessInstruction = pub type ProcessInstruction =
fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), InstructionError>; fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), InstructionError>;
@ -185,8 +175,7 @@ impl MessageProcessor {
executable_accounts, executable_accounts,
program_accounts, program_accounts,
tick_height, tick_height,
) )?;
.map_err(verify_error)?;
// Verify the instruction // Verify the instruction
for ((pre_program_id, pre_lamports, pre_data), post_account) in for ((pre_program_id, pre_lamports, pre_data), post_account) in
@ -321,19 +310,4 @@ mod tests {
"malicious Mallory should not be able to change the account data" "malicious Mallory should not be able to change the account data"
); );
} }
#[test]
fn test_verify_error() {
let short_error = InstructionError::CustomError(vec![1, 2, 3]);
let expected_short_error = short_error.clone(); // short CustomError errors should be untouched
assert_eq!(verify_error(short_error), expected_short_error);
let long_error = InstructionError::CustomError(vec![8; 40]);
let expected_long_error = InstructionError::CustomError(vec![8; 32]); // long CustomError errors should be truncated
assert_eq!(verify_error(long_error), expected_long_error);
let other_error = InstructionError::GenericError;
let expected_other_error = other_error.clone(); // non-CustomError errors should be untouched
assert_eq!(verify_error(other_error), expected_other_error);
}
} }

View File

@ -1,4 +1,3 @@
use bincode::serialize;
use log::*; use log::*;
use solana_sdk::account::KeyedAccount; use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError; use solana_sdk::instruction::InstructionError;
@ -94,7 +93,7 @@ pub fn process_instruction(
} }
SystemInstruction::Transfer { lamports } => move_lamports(keyed_accounts, lamports), SystemInstruction::Transfer { lamports } => move_lamports(keyed_accounts, lamports),
} }
.map_err(|e| InstructionError::CustomError(serialize(&e).unwrap())) .map_err(|e| InstructionError::CustomError(e as u32))
} else { } else {
debug!("Invalid instruction data: {:?}", data); debug!("Invalid instruction data: {:?}", data);
Err(InstructionError::InvalidInstructionData) Err(InstructionError::InvalidInstructionData)
@ -106,6 +105,7 @@ mod tests {
use super::*; use super::*;
use crate::bank::Bank; use crate::bank::Bank;
use crate::bank_client::BankClient; use crate::bank_client::BankClient;
use bincode::serialize;
use solana_sdk::account::Account; use solana_sdk::account::Account;
use solana_sdk::client::SyncClient; use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::genesis_block::GenesisBlock;

View File

@ -53,17 +53,14 @@ pub enum InstructionError {
DuplicateAccountIndex, DuplicateAccountIndex,
/// CustomError allows on-chain programs to implement program-specific error types and see /// CustomError allows on-chain programs to implement program-specific error types and see
/// them returned by the Solana runtime. A CustomError may be any type that is serialized /// them returned by the Solana runtime. A CustomError may be any type that is represented
/// to a Vec of bytes, max length 32 bytes. Any CustomError Vec greater than this length will /// as or serialized to a u32 integer.
/// be truncated by the runtime. CustomError(u32),
CustomError(Vec<u8>),
} }
impl InstructionError { impl InstructionError {
pub fn new_result_with_negative_lamports() -> Self { pub fn new_result_with_negative_lamports() -> Self {
let serialized_error = InstructionError::CustomError(SystemError::ResultWithNegativeLamports as u32)
bincode::serialize(&SystemError::ResultWithNegativeLamports).unwrap();
InstructionError::CustomError(serialized_error)
} }
} }