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_instruction::BudgetInstruction;
use crate::budget_state::{BudgetError, BudgetState};
use bincode::{deserialize, serialize};
use bincode::deserialize;
use chrono::prelude::{DateTime, Utc};
use log::*;
use solana_sdk::account::KeyedAccount;
@ -115,7 +115,7 @@ pub fn process_instruction(
}
trace!("apply timestamp");
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");
budget_state.serialize(&mut keyed_accounts[1].account.data)
}
@ -133,7 +133,7 @@ pub fn process_instruction(
}
trace!("apply signature");
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");
budget_state.serialize(&mut keyed_accounts[1].account.data)
}
@ -312,7 +312,7 @@ mod tests {
.unwrap(),
TransactionError::InstructionError(
0,
InstructionError::CustomError(serialize(&BudgetError::DestinationMissing).unwrap())
InstructionError::CustomError(BudgetError::DestinationMissing as u32)
)
);
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 1);

View File

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

View File

@ -72,16 +72,6 @@ fn verify_instruction(
Ok(())
}
fn verify_error(err: InstructionError) -> InstructionError {
match err {
InstructionError::CustomError(mut error) => {
error.truncate(32);
InstructionError::CustomError(error)
}
e => e,
}
}
pub type ProcessInstruction =
fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), InstructionError>;
@ -185,8 +175,7 @@ impl MessageProcessor {
executable_accounts,
program_accounts,
tick_height,
)
.map_err(verify_error)?;
)?;
// Verify the instruction
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"
);
}
#[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 solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError;
@ -94,7 +93,7 @@ pub fn process_instruction(
}
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 {
debug!("Invalid instruction data: {:?}", data);
Err(InstructionError::InvalidInstructionData)
@ -106,6 +105,7 @@ mod tests {
use super::*;
use crate::bank::Bank;
use crate::bank_client::BankClient;
use bincode::serialize;
use solana_sdk::account::Account;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::GenesisBlock;

View File

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