Skip to content

Commit

Permalink
program: map pubkey err to customs (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Nov 13, 2024
1 parent 654736b commit 82af5f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ use {
decode_error::DecodeError,
msg,
program_error::{PrintProgramError, ProgramError},
pubkey::PubkeyError,
},
thiserror::Error,
};

/// Errors that can be returned by the Config program.
#[derive(Error, Clone, Debug, Eq, PartialEq, FromPrimitive)]
pub enum AddressLookupTableError {
// Reimplementations of `PubkeyError` variants.
//
// Required for the BPF version since the Agave SDK only maps `PubkeyError`
// to `ProgramError`, not `InstructionError`. Therefore, the builtin
// version throws unknown custom error codes (0x0 - 0x2).
/// Length of the seed is too long for address generation
#[error("Length of the seed is too long for address generation")]
PubkeyErrorMaxSeedLengthExceeded = 0,
#[error("Provided seeds do not result in a valid address")]
PubkeyErrorInvalidSeeds,
#[error("Provided owner is not allowed")]
PubkeyErrorIllegalOwner,
/// Instruction modified data of a read-only account.
#[error("Instruction modified data of a read-only account")]
ReadonlyDataModified = 10, // Avoid collisions with System.
Expand All @@ -35,3 +48,15 @@ impl<T> DecodeError<T> for AddressLookupTableError {
"AddressLookupTableError"
}
}

impl From<PubkeyError> for AddressLookupTableError {
fn from(e: PubkeyError) -> Self {
match e {
PubkeyError::MaxSeedLengthExceeded => {
AddressLookupTableError::PubkeyErrorMaxSeedLengthExceeded
}
PubkeyError::InvalidSeeds => AddressLookupTableError::PubkeyErrorInvalidSeeds,
PubkeyError::IllegalOwner => AddressLookupTableError::PubkeyErrorIllegalOwner,
}
}
}
3 changes: 2 additions & 1 deletion program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ fn process_create_lookup_table(
&derivation_slot.to_le_bytes(),
&[bump_seed],
];
let derived_table_key = Pubkey::create_program_address(derived_table_seeds, program_id)?;
let derived_table_key = Pubkey::create_program_address(derived_table_seeds, program_id)
.map_err(AddressLookupTableError::from)?;

if lookup_table_info.key != &derived_table_key {
msg!(
Expand Down

0 comments on commit 82af5f8

Please sign in to comment.