From 82af5f893ac573d7b603f2a72a00a81c6f3d476b Mon Sep 17 00:00:00 2001 From: Joe C Date: Thu, 14 Nov 2024 03:44:27 +0800 Subject: [PATCH] program: map pubkey err to customs (#45) --- program/src/error.rs | 25 +++++++++++++++++++++++++ program/src/processor.rs | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/program/src/error.rs b/program/src/error.rs index b9511f6..1d804c9 100644 --- a/program/src/error.rs +++ b/program/src/error.rs @@ -6,6 +6,7 @@ use { decode_error::DecodeError, msg, program_error::{PrintProgramError, ProgramError}, + pubkey::PubkeyError, }, thiserror::Error, }; @@ -13,6 +14,18 @@ use { /// 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. @@ -35,3 +48,15 @@ impl DecodeError for AddressLookupTableError { "AddressLookupTableError" } } + +impl From for AddressLookupTableError { + fn from(e: PubkeyError) -> Self { + match e { + PubkeyError::MaxSeedLengthExceeded => { + AddressLookupTableError::PubkeyErrorMaxSeedLengthExceeded + } + PubkeyError::InvalidSeeds => AddressLookupTableError::PubkeyErrorInvalidSeeds, + PubkeyError::IllegalOwner => AddressLookupTableError::PubkeyErrorIllegalOwner, + } + } +} diff --git a/program/src/processor.rs b/program/src/processor.rs index 32e289f..83bf5ea 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -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!(