Review comments
This commit is contained in:
parent
1a9ef37251
commit
fe1676bc3a
|
@ -1,11 +1,12 @@
|
||||||
mod budget_program;
|
mod budget_program;
|
||||||
|
|
||||||
use crate::budget_program::process_instruction;
|
use crate::budget_program::process_instruction;
|
||||||
|
use bincode::serialize;
|
||||||
use log::*;
|
use log::*;
|
||||||
use solana_sdk::account::KeyedAccount;
|
use solana_sdk::account::KeyedAccount;
|
||||||
use solana_sdk::native_program::ProgramError;
|
use solana_sdk::native_program::ProgramError;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::{custom_error, solana_entrypoint};
|
use solana_sdk::solana_entrypoint;
|
||||||
|
|
||||||
solana_entrypoint!(entrypoint);
|
solana_entrypoint!(entrypoint);
|
||||||
fn entrypoint(
|
fn entrypoint(
|
||||||
|
@ -19,5 +20,5 @@ fn entrypoint(
|
||||||
trace!("process_instruction: {:?}", data);
|
trace!("process_instruction: {:?}", data);
|
||||||
trace!("keyed_accounts: {:?}", keyed_accounts);
|
trace!("keyed_accounts: {:?}", keyed_accounts);
|
||||||
process_instruction(program_id, keyed_accounts, data)
|
process_instruction(program_id, keyed_accounts, data)
|
||||||
.map_err(|e| ProgramError::CustomError(custom_error!(e)))
|
.map_err(|e| ProgramError::CustomError(serialize(&e).unwrap()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
use bincode::serialize;
|
||||||
use log::*;
|
use log::*;
|
||||||
use serde_derive::Serialize;
|
use serde_derive::Serialize;
|
||||||
use solana_sdk::account::KeyedAccount;
|
use solana_sdk::account::KeyedAccount;
|
||||||
use solana_sdk::custom_error;
|
|
||||||
use solana_sdk::native_program::ProgramError;
|
use solana_sdk::native_program::ProgramError;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::system_instruction::SystemInstruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
|
@ -101,7 +101,7 @@ pub fn entrypoint(
|
||||||
SystemError::ResultWithNegativeLamports => {
|
SystemError::ResultWithNegativeLamports => {
|
||||||
ProgramError::ResultWithNegativeLamports
|
ProgramError::ResultWithNegativeLamports
|
||||||
}
|
}
|
||||||
e => ProgramError::CustomError(custom_error!(e)),
|
e => ProgramError::CustomError(serialize(&e).unwrap()),
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
SystemInstruction::Assign { program_id } => {
|
SystemInstruction::Assign { program_id } => {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
use bincode::serialize;
|
||||||
use log::*;
|
use log::*;
|
||||||
use solana_sdk::account::KeyedAccount;
|
use solana_sdk::account::KeyedAccount;
|
||||||
use solana_sdk::native_program::ProgramError;
|
use solana_sdk::native_program::ProgramError;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::{custom_error, solana_entrypoint};
|
use solana_sdk::solana_entrypoint;
|
||||||
|
|
||||||
mod token_program;
|
mod token_program;
|
||||||
|
|
||||||
|
@ -15,8 +16,8 @@ fn entrypoint(
|
||||||
) -> Result<(), ProgramError> {
|
) -> Result<(), ProgramError> {
|
||||||
solana_logger::setup();
|
solana_logger::setup();
|
||||||
|
|
||||||
token_program::TokenProgram::process(program_id, info, input).map_err(|err| {
|
token_program::TokenProgram::process(program_id, info, input).map_err(|e| {
|
||||||
error!("error: {:?}", err);
|
error!("error: {:?}", e);
|
||||||
ProgramError::CustomError(custom_error!(err))
|
ProgramError::CustomError(serialize(&e).unwrap())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,13 +108,7 @@ fn execute_instruction(
|
||||||
program_accounts,
|
program_accounts,
|
||||||
tick_height,
|
tick_height,
|
||||||
)
|
)
|
||||||
.map_err(|err| match err {
|
.map_err(verify_error)?;
|
||||||
ProgramError::CustomError(mut error) => {
|
|
||||||
error.truncate(32);
|
|
||||||
ProgramError::CustomError(error)
|
|
||||||
}
|
|
||||||
e => e,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// Verify the instruction
|
// Verify the instruction
|
||||||
for ((pre_program_id, pre_lamports, pre_userdata), post_account) in
|
for ((pre_program_id, pre_lamports, pre_userdata), post_account) in
|
||||||
|
@ -233,6 +227,16 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn verify_error(err: ProgramError) -> ProgramError {
|
||||||
|
match err {
|
||||||
|
ProgramError::CustomError(mut error) => {
|
||||||
|
error.truncate(32);
|
||||||
|
ProgramError::CustomError(error)
|
||||||
|
}
|
||||||
|
e => e,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -312,4 +316,18 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_error() {
|
||||||
|
let short_error = ProgramError::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 = ProgramError::CustomError(vec![8; 40]);
|
||||||
|
let expected_long_error = ProgramError::CustomError(vec![8; 32]); // long CustomError errors should be truncated
|
||||||
|
assert_eq!(verify_error(long_error), expected_long_error);
|
||||||
|
|
||||||
|
let other_error = ProgramError::GenericError;
|
||||||
|
let expected_other_error = other_error.clone(); // non-CustomError errors should be untouched
|
||||||
|
assert_eq!(verify_error(other_error), expected_other_error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,20 +51,6 @@ impl std::fmt::Display for ProgramError {
|
||||||
}
|
}
|
||||||
impl std::error::Error for ProgramError {}
|
impl std::error::Error for ProgramError {}
|
||||||
|
|
||||||
// Convenience macro to serialize (and potentially truncate) a program-specific error to pass in
|
|
||||||
// ProgramError::CustomError
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! custom_error(
|
|
||||||
($program_error:expr) => ({
|
|
||||||
use bincode::serialize;
|
|
||||||
let mut error = serialize(&$program_error).expect("failed to serialize program error");
|
|
||||||
if error.len() > 32 {
|
|
||||||
error.truncate(32);
|
|
||||||
}
|
|
||||||
error
|
|
||||||
});
|
|
||||||
);
|
|
||||||
|
|
||||||
// All native programs export a symbol named process()
|
// All native programs export a symbol named process()
|
||||||
pub const ENTRYPOINT: &str = "process";
|
pub const ENTRYPOINT: &str = "process";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue