From 4c0a2b4248335addf6ed7b1cb5bb3dc219a4ed62 Mon Sep 17 00:00:00 2001 From: Jon C Date: Mon, 21 Jul 2025 18:33:02 +0200 Subject: [PATCH] program-error!: Upgrade to v2.2.2 crates #### Problem The repo is still on v2.2.1 sdk crates, but a few versions have been updated since then. Most importantly, `DecodeError` and `PrintProgramError` were deprecated in favor of `ToStr`, and `spl-program-error` implements the old traits. #### Summary of changes Update to v2.2.2 crates, and update spl-program-error to stop implementing `DecodeError` and `PrintProgramError` in favor of `ToStr`. --- Cargo.lock | 11 ++- discriminator/Cargo.toml | 2 +- pod/Cargo.toml | 5 +- pod/src/error.rs | 40 +++----- pod/src/lib.rs | 4 +- program-error/Cargo.toml | 3 +- program-error/README.md | 82 +++------------- program-error/derive/src/lib.rs | 20 ++-- program-error/derive/src/macro_impl.rs | 65 +++---------- program-error/derive/src/parser.rs | 67 +------------ program-error/src/lib.rs | 6 +- program-error/tests/bench.rs | 21 +---- program-error/tests/mod.rs | 99 ++++---------------- program-error/tests/print.rs | 30 ------ program-error/tests/spl.rs | 17 ---- program-error/tests/{decode.rs => to_str.rs} | 6 +- tlv-account-resolution/Cargo.toml | 4 +- tlv-account-resolution/src/error.rs | 64 ++++++------- tlv-account-resolution/src/lib.rs | 2 +- type-length-value/Cargo.toml | 3 +- type-length-value/src/error.rs | 30 +++--- 21 files changed, 141 insertions(+), 440 deletions(-) delete mode 100644 program-error/tests/print.rs rename program-error/tests/{decode.rs => to_str.rs} (87%) diff --git a/Cargo.lock b/Cargo.lock index 83fc27ed..495965be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5063,9 +5063,9 @@ dependencies = [ [[package]] name = "solana-program-error" -version = "2.2.1" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8ae2c1a8d0d4ae865882d5770a7ebca92bab9c685e43f0461682c6c05a35bfa" +checksum = "9ee2e0217d642e2ea4bee237f37bd61bb02aec60da3647c48ff88f6556ade775" dependencies = [ "borsh 1.5.7", "num-traits", @@ -6715,10 +6715,9 @@ dependencies = [ "bytemuck_derive", "num-derive", "num-traits", + "num_enum", "serde", "serde_json", - "solana-decode-error", - "solana-msg", "solana-program-error", "solana-program-option", "solana-pubkey", @@ -6753,6 +6752,7 @@ dependencies = [ "lazy_static", "num-derive", "num-traits", + "num_enum", "serial_test", "solana-decode-error", "solana-msg", @@ -6809,12 +6809,12 @@ dependencies = [ "futures-util", "num-derive", "num-traits", + "num_enum", "serde", "solana-account-info", "solana-client", "solana-decode-error", "solana-instruction", - "solana-msg", "solana-program-error", "solana-program-test", "solana-pubkey", @@ -7035,6 +7035,7 @@ dependencies = [ "bytemuck", "num-derive", "num-traits", + "num_enum", "solana-account-info", "solana-decode-error", "solana-msg", diff --git a/discriminator/Cargo.toml b/discriminator/Cargo.toml index fa2545eb..1d94cab0 100644 --- a/discriminator/Cargo.toml +++ b/discriminator/Cargo.toml @@ -13,7 +13,7 @@ borsh = ["dep:borsh"] [dependencies] borsh = { version = "1", optional = true, features = ["derive"] } bytemuck = { version = "1.23.1", features = ["derive"] } -solana-program-error = "2.2.1" +solana-program-error = "2.2.2" solana-sha256-hasher = "2.2.1" spl-discriminator-derive = { version = "0.2.0", path = "./derive" } diff --git a/pod/Cargo.toml b/pod/Cargo.toml index 5fdf70aa..4139d768 100644 --- a/pod/Cargo.toml +++ b/pod/Cargo.toml @@ -16,11 +16,10 @@ borsh = { version = "1.5.7", features = ["derive", "unstable__schema"], optional bytemuck = { version = "1.23.1" } bytemuck_derive = { version = "1.9.3" } num-derive = "0.4" +num_enum = "0.7" num-traits = "0.2" serde = { version = "1.0.219", optional = true } -solana-decode-error = "2.2.1" -solana-msg = "2.2.1" -solana-program-error = "2.2.1" +solana-program-error = "2.2.2" solana-program-option = "2.2.1" solana-pubkey = "2.2.1" solana-zk-sdk = "2.2.0" diff --git a/pod/src/error.rs b/pod/src/error.rs index c8325f4b..2d83a231 100644 --- a/pod/src/error.rs +++ b/pod/src/error.rs @@ -1,12 +1,17 @@ //! Error types -use { - solana_msg::msg, - solana_program_error::{PrintProgramError, ProgramError}, -}; +use solana_program_error::{ProgramError, ToStr}; /// Errors that may be returned by the spl-pod library. #[repr(u32)] -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, num_derive::FromPrimitive)] +#[derive( + Debug, + Clone, + PartialEq, + Eq, + thiserror::Error, + num_enum::TryFromPrimitive, + num_derive::FromPrimitive, +)] pub enum PodSliceError { /// Error in checked math operation #[error("Error in checked math operation")] @@ -25,27 +30,12 @@ impl From for ProgramError { } } -impl solana_decode_error::DecodeError for PodSliceError { - fn type_of() -> &'static str { - "PodSliceError" - } -} - -impl PrintProgramError for PodSliceError { - fn print(&self) - where - E: 'static + std::error::Error + PrintProgramError + num_traits::FromPrimitive, - { +impl ToStr for PodSliceError { + fn to_str(&self) -> &'static str { match self { - PodSliceError::CalculationFailure => { - msg!("Error in checked math operation") - } - PodSliceError::BufferTooSmall => { - msg!("Provided byte buffer too small for expected type") - } - PodSliceError::BufferTooLarge => { - msg!("Provided byte buffer too large for expected type") - } + PodSliceError::CalculationFailure => "Error in checked math operation", + PodSliceError::BufferTooSmall => "Provided byte buffer too small for expected type", + PodSliceError::BufferTooLarge => "Provided byte buffer too large for expected type", } } } diff --git a/pod/src/lib.rs b/pod/src/lib.rs index 292f1c09..e1aa65fd 100644 --- a/pod/src/lib.rs +++ b/pod/src/lib.rs @@ -9,6 +9,4 @@ pub mod slice; // Export current sdk types for downstream users building with a different sdk // version -pub use { - solana_decode_error, solana_msg, solana_program_error, solana_program_option, solana_pubkey, -}; +pub use {solana_program_error, solana_program_option, solana_pubkey}; diff --git a/program-error/Cargo.toml b/program-error/Cargo.toml index 57089486..a48ad539 100644 --- a/program-error/Cargo.toml +++ b/program-error/Cargo.toml @@ -9,10 +9,11 @@ edition = "2021" [dependencies] num-derive = "0.4" +num_enum = "0.7" num-traits = "0.2" solana-decode-error = "2.2.1" solana-msg = "2.2.1" -solana-program-error = "2.2.1" +solana-program-error = "2.2.2" spl-program-error-derive = { version = "0.5.0", path = "./derive" } thiserror = "2.0" diff --git a/program-error/README.md b/program-error/README.md index 7cf74d2d..79b1c54e 100644 --- a/program-error/README.md +++ b/program-error/README.md @@ -3,17 +3,16 @@ Macros for implementing error-based traits on enums. - `#[derive(IntoProgramError)]`: automatically derives the trait `From for solana_program_error::ProgramError`. -- `#[derive(DecodeError)]`: automatically derives the trait `solana_decode_error::DecodeError`. -- `#[derive(PrintProgramError)]`: automatically derives the trait `solana_program_error::PrintProgramError`. +- `#[derive(ToStr)]`: automatically derives the trait `solana_program_error::ToStr`. - `#[spl_program_error]`: Automatically derives all below traits: - `Clone` - `Debug` - `Eq` - - `DecodeError` - `IntoProgramError` - - `PrintProgramError` + - `ToStr` - `thiserror::Error` - `num_derive::FromPrimitive` + - `num_enum::TryFromPrimitive` - `PartialEq` ### `#[derive(IntoProgramError)]` @@ -27,6 +26,7 @@ Your enum must implement the following traits in order for this macro to work: - `Eq` - `thiserror::Error` - `num_derive::FromPrimitive` +- `num_enum::TryFromPrimitive` - `PartialEq` Sample code: @@ -46,9 +46,9 @@ pub enum ExampleError { } ``` -### `#[derive(DecodeError)]` +### `#[derive(ToStr)]` -This derive macro automatically derives the trait `solana_decode_error::DecodeError`. +This derive macro automatically derives the trait `solana_program_error::ToStr`. Your enum must implement the following traits in order for this macro to work: @@ -56,8 +56,7 @@ Your enum must implement the following traits in order for this macro to work: - `Debug` - `Eq` - `IntoProgramError` (above) -- `thiserror::Error` -- `num_derive::FromPrimitive` +- `num_enum::TryFromPrimitive` - `PartialEq` Sample code: @@ -67,50 +66,11 @@ Sample code: #[derive( Clone, Debug, - DecodeError, - Eq, - IntoProgramError, - thiserror::Error, - num_derive::FromPrimitive, - PartialEq, -)] -pub enum ExampleError { - /// Mint has no mint authority - #[error("Mint has no mint authority")] - MintHasNoMintAuthority, - /// Incorrect mint authority has signed the instruction - #[error("Incorrect mint authority has signed the instruction")] - IncorrectMintAuthority, -} -``` - -### `#[derive(PrintProgramError)]` - -This derive macro automatically derives the trait `solana_program_error::PrintProgramError`. - -Your enum must implement the following traits in order for this macro to work: - -- `Clone` -- `Debug` -- `DecodeError` (above) -- `Eq` -- `IntoProgramError` (above) -- `thiserror::Error` -- `num_derive::FromPrimitive` -- `PartialEq` - -Sample code: - -```rust -/// Example error -#[derive( - Clone, - Debug, - DecodeError, Eq, IntoProgramError, thiserror::Error, num_derive::FromPrimitive, + num_enum::TryFromPrimitive, PartialEq, )] pub enum ExampleError { @@ -136,11 +96,13 @@ This procedural macro will give you all of the required implementations out of t - `Eq` - `thiserror::Error` - `num_derive::FromPrimitive` +- `num_enum::TryFromPrimitive` - `PartialEq` It also imports the required crates so you don't have to in your program: - `num_derive` +- `num_enum` - `num_traits` - `thiserror` @@ -266,27 +228,11 @@ impl From for solana_program_error::ProgramError { solana_program_error::ProgramError::Custom(e as u32) } } -impl solana_decode_error::DecodeError for ExampleError { - fn type_of() -> &'static str { - "ExampleError" - } -} -impl solana_program_error::PrintProgramError for ExampleError { - fn print(&self) - where - E: 'static + std::error::Error + solana_decode_error::DecodeError - + solana_program_error::PrintProgramError - + num_traits::FromPrimitive, - { +impl solana_program_error::ToStr for ExampleError { + fn to_str(&self) -> &'static str { match self { - ExampleError::MintHasNoMintAuthority => { - ::solana_msg::sol_log("Mint has no mint authority") - } - ExampleError::IncorrectMintAuthority => { - ::solana_msg::sol_log( - "Incorrect mint authority has signed the instruction", - ) - } + ExampleError::MintHasNoMintAuthority => "Mint has no mint authority", + ExampleError::IncorrectMintAuthority => "Incorrect mint authority has signed the instruction", } } } diff --git a/program-error/derive/src/lib.rs b/program-error/derive/src/lib.rs index 555006a9..0d85cbe8 100644 --- a/program-error/derive/src/lib.rs +++ b/program-error/derive/src/lib.rs @@ -2,7 +2,7 @@ // Required to include `#[allow(clippy::integer_arithmetic)]` // below since the tokens generated by `quote!` in the implementation -// for `MacroType::PrintProgramError` and `MacroType::SplProgramError` +// for `MacroType::ToStr` and `MacroType::SplProgramError` // trigger the lint upstream through `quote_token_with_context` within the // `quote` crate // @@ -33,20 +33,13 @@ pub fn into_program_error(input: TokenStream) -> TokenStream { .into() } -/// Derive macro to add `solana_decode_error::DecodeError` trait -#[proc_macro_derive(DecodeError)] -pub fn decode_error(input: TokenStream) -> TokenStream { - let ItemEnum { ident, .. } = parse_macro_input!(input as ItemEnum); - MacroType::DecodeError { ident }.generate_tokens().into() -} - -/// Derive macro to add `solana_program_error::PrintProgramError` trait -#[proc_macro_derive(PrintProgramError)] -pub fn print_program_error(input: TokenStream) -> TokenStream { +/// Derive macro to add `solana_program_error::ToStr` trait +#[proc_macro_derive(ToStr)] +pub fn to_str(input: TokenStream) -> TokenStream { let ItemEnum { ident, variants, .. } = parse_macro_input!(input as ItemEnum); - MacroType::PrintProgramError { ident, variants } + MacroType::ToStr { ident, variants } .generate_tokens() .into() } @@ -61,8 +54,7 @@ pub fn print_program_error(input: TokenStream) -> TokenStream { /// - `thiserror::Error` /// - `num_derive::FromPrimitive` /// - `Into` -/// - `solana_decode_error::DecodeError` -/// - `solana_program_error::PrintProgramError` +/// - `solana_program_error::ToStr` /// /// Optionally, you can add `hash_error_code_start: u32` argument to create /// a unique `u32` _starting_ error codes from the names of the enum variants. diff --git a/program-error/derive/src/macro_impl.rs b/program-error/derive/src/macro_impl.rs index d29b674e..ce11848c 100644 --- a/program-error/derive/src/macro_impl.rs +++ b/program-error/derive/src/macro_impl.rs @@ -1,7 +1,7 @@ //! The actual token generator for the macro use { - crate::parser::{SolanaDecodeError, SolanaProgramError, SplProgramErrorArgs}, + crate::parser::{SolanaProgramError, SplProgramErrorArgs}, proc_macro2::Span, quote::quote, sha2::{Digest, Sha256}, @@ -20,10 +20,7 @@ pub enum MacroType { IntoProgramError { ident: Ident, }, - DecodeError { - ident: Ident, - }, - PrintProgramError { + ToStr { ident: Ident, variants: Punctuated, }, @@ -37,18 +34,13 @@ impl MacroType { /// Generates the corresponding tokens based on variant selection pub fn generate_tokens(&mut self) -> proc_macro2::TokenStream { let default_solana_program_error = SolanaProgramError::default(); - let default_solana_decode_error = SolanaDecodeError::default(); match self { Self::IntoProgramError { ident } => { into_program_error(ident, &default_solana_program_error) } - Self::DecodeError { ident } => decode_error(ident, &default_solana_decode_error), - Self::PrintProgramError { ident, variants } => print_program_error( - ident, - variants, - &default_solana_program_error, - &default_solana_decode_error, - ), + Self::ToStr { ident, variants } => { + to_str(ident, variants, &default_solana_program_error) + } Self::SplProgramError { args, item_enum } => spl_program_error(args, item_enum), } } @@ -68,52 +60,31 @@ pub fn into_program_error(ident: &Ident, import: &SolanaProgramError) -> proc_ma import.wrap(this_impl) } -/// Builds the implementation of `solana_decode_error::DecodeError` -pub fn decode_error(ident: &Ident, import: &SolanaDecodeError) -> proc_macro2::TokenStream { - let this_impl = quote! { - impl #import::DecodeError for #ident { - fn type_of() -> &'static str { - stringify!(#ident) - } - } - }; - import.wrap(this_impl) -} - /// Builds the implementation of -/// `solana_program_error::PrintProgramError` -pub fn print_program_error( +/// `solana_program_error::ToStr` +pub fn to_str( ident: &Ident, variants: &Punctuated, program_error_import: &SolanaProgramError, - decode_error_import: &SolanaDecodeError, ) -> proc_macro2::TokenStream { let ppe_match_arms = variants.iter().map(|variant| { let variant_ident = &variant.ident; let error_msg = get_error_message(variant) .unwrap_or_else(|| String::from("Unknown custom program error")); quote! { - #ident::#variant_ident => { - ::solana_msg::msg!(#error_msg) - } + #ident::#variant_ident => #error_msg } }); let this_impl = quote! { - impl #program_error_import::PrintProgramError for #ident { - fn print(&self) - where - E: 'static - + std::error::Error - + #program_error_import::PrintProgramError - + num_traits::FromPrimitive, - { + impl #program_error_import::ToStr for #ident { + fn to_str(&self) -> &'static str { match self { #(#ppe_match_arms),* } } } }; - program_error_import.wrap(decode_error_import.wrap(this_impl)) + program_error_import.wrap(this_impl) } /// Helper to parse out the string literal from the `#[error(..)]` attribute @@ -142,25 +113,17 @@ pub fn spl_program_error( let ident = &item_enum.ident; let variants = &item_enum.variants; let into_program_error = into_program_error(ident, &args.program_error_import); - let decode_error = decode_error(ident, &args.decode_error_import); - let print_program_error = print_program_error( - ident, - variants, - &args.program_error_import, - &args.decode_error_import, - ); + let to_str = to_str(ident, variants, &args.program_error_import); quote! { #[repr(u32)] - #[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)] + #[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, num_enum::TryFromPrimitive, PartialEq)] #[num_traits = "num_traits"] #item_enum #into_program_error - #decode_error - - #print_program_error + #to_str } } diff --git a/program-error/derive/src/parser.rs b/program-error/derive/src/parser.rs index a22864e2..636e39b6 100644 --- a/program-error/derive/src/parser.rs +++ b/program-error/derive/src/parser.rs @@ -17,8 +17,6 @@ pub struct SplProgramErrorArgs { pub hash_error_code_start: Option, /// Crate to use for solana_program_error pub program_error_import: SolanaProgramError, - /// Crate to use for solana_decode_error - pub decode_error_import: SolanaDecodeError, } /// Struct representing the path to a `solana_program_error` crate, which may @@ -50,40 +48,10 @@ impl Default for SolanaProgramError { } } -/// Struct representing the path to a `solana_decode_error` crate, which may -/// be renamed or otherwise. -pub struct SolanaDecodeError { - import: Ident, - explicit: bool, -} -impl quote::ToTokens for SolanaDecodeError { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.import.to_tokens(tokens); - } -} -impl SolanaDecodeError { - pub fn wrap(&self, output: TokenStream) -> TokenStream { - if self.explicit { - output - } else { - decode_error_anon_const_trick(output) - } - } -} -impl Default for SolanaDecodeError { - fn default() -> Self { - Self { - import: Ident::new("_solana_decode_error", Span::call_site()), - explicit: false, - } - } -} - impl Parse for SplProgramErrorArgs { fn parse(input: ParseStream) -> syn::Result { let mut hash_error_code_start = None; let mut program_error_import = None; - let mut decode_error_import = None; while !input.is_empty() { match SplProgramErrorArgParser::parse(input)? { SplProgramErrorArgParser::HashErrorCodes { value, .. } => { @@ -95,18 +63,11 @@ impl Parse for SplProgramErrorArgs { explicit: true, }); } - SplProgramErrorArgParser::SolanaDecodeErrorCrate { value, .. } => { - decode_error_import = Some(SolanaDecodeError { - import: value.parse()?, - explicit: true, - }); - } } } Ok(Self { hash_error_code_start, program_error_import: program_error_import.unwrap_or(SolanaProgramError::default()), - decode_error_import: decode_error_import.unwrap_or(SolanaDecodeError::default()), }) } } @@ -124,11 +85,6 @@ enum SplProgramErrorArgParser { value: LitStr, _comma: Option, }, - SolanaDecodeErrorCrate { - _equals_sign: Token![=], - value: LitStr, - _comma: Option, - }, } impl Parse for SplProgramErrorArgParser { @@ -155,17 +111,10 @@ impl Parse for SplProgramErrorArgParser { _comma, }) } - "solana_decode_error" => { - let _equals_sign = input.parse::()?; - let value = input.parse::()?; - let _comma: Option = input.parse().unwrap_or(None); - Ok(Self::SolanaDecodeErrorCrate { - _equals_sign, - value, - _comma, - }) + _ => { + Err(input + .error("Expected argument 'hash_error_code_start', 'solana_program_error'")) } - _ => Err(input.error("Expected argument 'hash_error_code_start', 'solana_program_error', or 'solana_decode_error'")), } } } @@ -198,13 +147,3 @@ fn program_error_anon_const_trick(exp: TokenStream) -> TokenStream { }; } } - -// Same thing, but for solana_decode_error -fn decode_error_anon_const_trick(exp: TokenStream) -> TokenStream { - quote! { - const _: () = { - extern crate solana_decode_error as _solana_decode_error; - #exp - }; - } -} diff --git a/program-error/src/lib.rs b/program-error/src/lib.rs index b44f9ae2..8b3a054a 100644 --- a/program-error/src/lib.rs +++ b/program-error/src/lib.rs @@ -9,9 +9,7 @@ extern crate self as spl_program_error; // Make these available downstream for the macro to work without // additional imports pub use { - num_derive, num_traits, solana_decode_error, solana_msg, solana_program_error, - spl_program_error_derive::{ - spl_program_error, DecodeError, IntoProgramError, PrintProgramError, - }, + num_derive, num_traits, solana_decode_error, solana_program_error, + spl_program_error_derive::{spl_program_error, IntoProgramError, ToStr}, thiserror, }; diff --git a/program-error/tests/bench.rs b/program-error/tests/bench.rs index db3c5624..3536ffe3 100644 --- a/program-error/tests/bench.rs +++ b/program-error/tests/bench.rs @@ -17,26 +17,13 @@ impl From for solana_program_error::ProgramError { solana_program_error::ProgramError::Custom(e as u32) } } -impl solana_decode_error::DecodeError for ExampleError { - fn type_of() -> &'static str { - "ExampleError" - } -} -impl solana_program_error::PrintProgramError for ExampleError { - fn print(&self) - where - E: 'static - + std::error::Error - + solana_program_error::PrintProgramError - + num_traits::FromPrimitive, - { +impl solana_program_error::ToStr for ExampleError { + fn to_str(&self) -> &'static str { match self { - ExampleError::MintHasNoMintAuthority => { - solana_msg::msg!("Mint has no mint authority") - } + ExampleError::MintHasNoMintAuthority => "Mint has no mint authority", ExampleError::IncorrectMintAuthority => { - solana_msg::msg!("Incorrect mint authority has signed the instruction") + "Incorrect mint authority has signed the instruction" } } } diff --git a/program-error/tests/mod.rs b/program-error/tests/mod.rs index d9f15873..2b756c8d 100644 --- a/program-error/tests/mod.rs +++ b/program-error/tests/mod.rs @@ -1,36 +1,15 @@ pub mod bench; -pub mod decode; pub mod into; -pub mod print; pub mod spl; +pub mod to_str; #[cfg(test)] mod tests { use { super::*, - serial_test::serial, - solana_decode_error::DecodeError, - solana_program_error::{PrintProgramError, ProgramError}, - std::sync::{Arc, RwLock}, + solana_program_error::{ProgramError, ToStr}, }; - // Used to capture output for `PrintProgramError` for testing - lazy_static::lazy_static! { - static ref EXPECTED_DATA: Arc>> = Arc::new(RwLock::new(Vec::new())); - } - fn set_expected_data(expected_data: Vec) { - *EXPECTED_DATA.write().unwrap() = expected_data; - } - pub struct SyscallStubs {} - impl solana_sysvar::program_stubs::SyscallStubs for SyscallStubs { - fn sol_log(&self, message: &str) { - assert_eq!( - message, - String::from_utf8_lossy(&EXPECTED_DATA.read().unwrap()) - ); - } - } - // `#[derive(IntoProgramError)]` #[test] fn test_derive_into_program_error() { @@ -45,73 +24,32 @@ mod tests { ); } - // `#[derive(DecodeError)]` + // `#[derive(ToStr)]` #[test] - fn test_derive_decode_error() { + fn test_derive_to_str() { // `Into` assert_eq!( Into::::into(bench::ExampleError::MintHasNoMintAuthority), - Into::::into(decode::ExampleError::MintHasNoMintAuthority), + Into::::into(to_str::ExampleError::MintHasNoMintAuthority), ); assert_eq!( Into::::into(bench::ExampleError::IncorrectMintAuthority), - Into::::into(decode::ExampleError::IncorrectMintAuthority), - ); - // `DecodeError` - assert_eq!( - >::type_of(), - >::type_of(), - ); - } - // `#[derive(PrintProgramError)]` - #[test] - #[serial] - fn test_derive_print_program_error() { - use std::sync::Once; - static ONCE: Once = Once::new(); - - ONCE.call_once(|| { - solana_sysvar::program_stubs::set_syscall_stubs(Box::new(SyscallStubs {})); - }); - // `Into` - assert_eq!( - Into::::into(bench::ExampleError::MintHasNoMintAuthority), - Into::::into(print::ExampleError::MintHasNoMintAuthority), + Into::::into(to_str::ExampleError::IncorrectMintAuthority), ); + // `ToStr` assert_eq!( - Into::::into(bench::ExampleError::IncorrectMintAuthority), - Into::::into(print::ExampleError::IncorrectMintAuthority), + ToStr::to_str::(&to_str::ExampleError::MintHasNoMintAuthority,), + "Mint has no mint authority" ); - // `DecodeError` assert_eq!( - >::type_of(), - >::type_of(), - ); - // `PrintProgramError` - set_expected_data("Mint has no mint authority".as_bytes().to_vec()); - PrintProgramError::print::( - &print::ExampleError::MintHasNoMintAuthority, - ); - set_expected_data( + ToStr::to_str::(&to_str::ExampleError::IncorrectMintAuthority,), "Incorrect mint authority has signed the instruction" - .as_bytes() - .to_vec(), - ); - PrintProgramError::print::( - &print::ExampleError::IncorrectMintAuthority, ); } // `#[spl_program_error]` #[test] - #[serial] fn test_spl_program_error() { - use std::sync::Once; - static ONCE: Once = Once::new(); - - ONCE.call_once(|| { - solana_sysvar::program_stubs::set_syscall_stubs(Box::new(SyscallStubs {})); - }); // `Into` assert_eq!( Into::::into(bench::ExampleError::MintHasNoMintAuthority), @@ -121,19 +59,14 @@ mod tests { Into::::into(bench::ExampleError::IncorrectMintAuthority), Into::::into(spl::ExampleError::IncorrectMintAuthority), ); - // `DecodeError` + // `ToStr` assert_eq!( - >::type_of(), - >::type_of(), + ToStr::to_str::(&spl::ExampleError::MintHasNoMintAuthority), + "Mint has no mint authority" ); - // `PrintProgramError` - set_expected_data("Mint has no mint authority".as_bytes().to_vec()); - PrintProgramError::print::(&spl::ExampleError::MintHasNoMintAuthority); - set_expected_data( - "Incorrect mint authority has signed the instruction" - .as_bytes() - .to_vec(), + assert_eq!( + ToStr::to_str::(&spl::ExampleError::IncorrectMintAuthority), + "Incorrect mint authority has signed the instruction", ); - PrintProgramError::print::(&spl::ExampleError::IncorrectMintAuthority); } } diff --git a/program-error/tests/print.rs b/program-error/tests/print.rs deleted file mode 100644 index 8b68f66a..00000000 --- a/program-error/tests/print.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Tests `#[derive(PrintProgramError)]` - -use spl_program_error::*; - -/// Example error -#[derive( - Clone, - Debug, - DecodeError, - Eq, - IntoProgramError, - PrintProgramError, - thiserror::Error, - num_derive::FromPrimitive, - PartialEq, -)] -pub enum ExampleError { - /// Mint has no mint authority - #[error("Mint has no mint authority")] - MintHasNoMintAuthority, - /// Incorrect mint authority has signed the instruction - #[error("Incorrect mint authority has signed the instruction")] - IncorrectMintAuthority, -} - -/// Tests that all macros compile -#[test] -fn test_macros_compile() { - let _ = ExampleError::MintHasNoMintAuthority; -} diff --git a/program-error/tests/spl.rs b/program-error/tests/spl.rs index 4e86e170..b01bb3ce 100644 --- a/program-error/tests/spl.rs +++ b/program-error/tests/spl.rs @@ -89,20 +89,3 @@ enum ExampleSolanaProgramError { fn test_macros_compile_with_solana_program_error_crate() { let _ = ExampleSolanaProgramError::VeryInformativeError; } - -/// Example error with solana_decode_error crate set -#[spl_program_error(solana_decode_error = "solana_decode_error")] -enum ExampleSolanaDecodeError { - /// This is a very informative error - #[error("This is a very informative error")] - VeryInformativeError, - /// This is a super important error - #[error("This is a super important error")] - SuperImportantError, -} - -/// Tests that all macros compile -#[test] -fn test_macros_compile_with_solana_decode_error_crate() { - let _ = ExampleSolanaDecodeError::VeryInformativeError; -} diff --git a/program-error/tests/decode.rs b/program-error/tests/to_str.rs similarity index 87% rename from program-error/tests/decode.rs rename to program-error/tests/to_str.rs index 0c7c209b..d1020514 100644 --- a/program-error/tests/decode.rs +++ b/program-error/tests/to_str.rs @@ -1,4 +1,4 @@ -//! Tests `#[derive(DecodeError)]` +//! Tests `#[derive(ToStr)]` use spl_program_error::*; @@ -6,13 +6,15 @@ use spl_program_error::*; #[derive( Clone, Debug, - DecodeError, Eq, IntoProgramError, + ToStr, thiserror::Error, num_derive::FromPrimitive, + num_enum::TryFromPrimitive, PartialEq, )] +#[repr(u32)] pub enum ExampleError { /// Mint has no mint authority #[error("Mint has no mint authority")] diff --git a/tlv-account-resolution/Cargo.toml b/tlv-account-resolution/Cargo.toml index 589ca9d3..12a4c38a 100644 --- a/tlv-account-resolution/Cargo.toml +++ b/tlv-account-resolution/Cargo.toml @@ -14,13 +14,13 @@ test-sbf = [] [dependencies] bytemuck = { version = "1.23.1", features = ["derive"] } num-derive = "0.4" +num_enum = "0.7" num-traits = "0.2" serde = { version = "1.0.219", optional = true } solana-account-info = "2.2.1" solana-decode-error = "2.2.1" solana-instruction = { version = "2.2.1", features = ["std"] } -solana-program-error = "2.2.1" -solana-msg = "2.2.1" +solana-program-error = "2.2.2" solana-pubkey = { version = "2.2.1", features = ["curve25519"] } spl-discriminator = { version = "0.4.0", path = "../discriminator" } spl-program-error = { version = "0.7.0", path = "../program-error" } diff --git a/tlv-account-resolution/src/error.rs b/tlv-account-resolution/src/error.rs index c2809048..445cfd26 100644 --- a/tlv-account-resolution/src/error.rs +++ b/tlv-account-resolution/src/error.rs @@ -1,13 +1,18 @@ //! Error types -use { - solana_msg::msg, - solana_program_error::{PrintProgramError, ProgramError}, -}; +use solana_program_error::{ProgramError, ToStr}; /// Errors that may be returned by the Account Resolution library. #[repr(u32)] -#[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)] +#[derive( + Clone, + Debug, + Eq, + thiserror::Error, + num_enum::TryFromPrimitive, + num_derive::FromPrimitive, + PartialEq, +)] pub enum AccountResolutionError { /// Incorrect account provided #[error("Incorrect account provided")] @@ -81,73 +86,68 @@ impl From for ProgramError { } } -impl PrintProgramError for AccountResolutionError { - fn print(&self) - where - E: 'static + std::error::Error + PrintProgramError + num_traits::FromPrimitive, - { +impl ToStr for AccountResolutionError { + fn to_str(&self) -> &'static str { match self { AccountResolutionError::IncorrectAccount => { - msg!("Incorrect account provided") + "Incorrect account provided" } AccountResolutionError::NotEnoughAccounts => { - msg!("Not enough accounts provided") + "Not enough accounts provided" } AccountResolutionError::TlvUninitialized => { - msg!("No value initialized in TLV data") + "No value initialized in TLV data" } AccountResolutionError::TlvInitialized => { - msg!("Some value initialized in TLV data") + "Some value initialized in TLV data" } AccountResolutionError::TooManyPubkeys => { - msg!("Too many pubkeys provided") + "Too many pubkeys provided" } AccountResolutionError::InvalidPubkey => { - msg!("Failed to parse `Pubkey` from bytes") + "Failed to parse `Pubkey` from bytes" } AccountResolutionError::AccountTypeNotAccountMeta => { - msg!( - "Attempted to deserialize an `AccountMeta` but the underlying type has PDA configs rather than a fixed address", - ) + "Attempted to deserialize an `AccountMeta` but the underlying type has PDA configs rather than a fixed address" } AccountResolutionError::SeedConfigsTooLarge => { - msg!("Provided list of seed configurations too large for a validation account",) + "Provided list of seed configurations too large for a validation account" } AccountResolutionError::NotEnoughBytesForSeed => { - msg!("Not enough bytes available to pack seed configuration",) + "Not enough bytes available to pack seed configuration" } AccountResolutionError::InvalidBytesForSeed => { - msg!("The provided bytes are not valid for a seed configuration",) + "The provided bytes are not valid for a seed configuration" } AccountResolutionError::InvalidSeedConfig => { - msg!("Tried to pack an invalid seed configuration",) + "Tried to pack an invalid seed configuration" } AccountResolutionError::InstructionDataTooSmall => { - msg!("Instruction data too small for seed configuration",) + "Instruction data too small for seed configuration" } AccountResolutionError::AccountNotFound => { - msg!("Could not find account at specified index",) + "Could not find account at specified index" } AccountResolutionError::CalculationFailure => { - msg!("Error in checked math operation") + "Error in checked math operation" } AccountResolutionError::AccountDataNotFound => { - msg!("Could not find account data at specified index",) + "Could not find account data at specified index" } AccountResolutionError::AccountDataTooSmall => { - msg!("Account data too small for requested seed configuration",) + "Account data too small for requested seed configuration" } AccountResolutionError::AccountFetchFailed => { - msg!("Failed to fetch account") + "Failed to fetch account" } AccountResolutionError::NotEnoughBytesForPubkeyData => { - msg!("Not enough bytes available to pack pubkey data configuration",) + "Not enough bytes available to pack pubkey data configuration" } AccountResolutionError::InvalidBytesForPubkeyData => { - msg!("The provided bytes are not valid for a pubkey data configuration",) + "The provided bytes are not valid for a pubkey data configuration" } AccountResolutionError::InvalidPubkeyDataConfig => { - msg!("Tried to pack an invalid pubkey data configuration",) + "Tried to pack an invalid pubkey data configuration" } } } diff --git a/tlv-account-resolution/src/lib.rs b/tlv-account-resolution/src/lib.rs index e015fc97..5bf4fe8a 100644 --- a/tlv-account-resolution/src/lib.rs +++ b/tlv-account-resolution/src/lib.rs @@ -16,6 +16,6 @@ pub mod state; // Export current sdk types for downstream users building with a different sdk // version pub use { - solana_account_info, solana_decode_error, solana_instruction, solana_msg, solana_program_error, + solana_account_info, solana_decode_error, solana_instruction, solana_program_error, solana_pubkey, }; diff --git a/type-length-value/Cargo.toml b/type-length-value/Cargo.toml index 9f8fc535..f5d66470 100644 --- a/type-length-value/Cargo.toml +++ b/type-length-value/Cargo.toml @@ -14,11 +14,12 @@ derive = ["dep:spl-type-length-value-derive", "solana-program-error/borsh"] [dependencies] bytemuck = { version = "1.23.1", features = ["derive"] } num-derive = "0.4" +num_enum = "0.7" num-traits = "0.2" solana-account-info = "2.2.1" solana-decode-error = "2.2.1" solana-msg = "2.2.1" -solana-program-error = "2.2.1" +solana-program-error = "2.2.2" spl-discriminator = { version = "0.4.0", path = "../discriminator" } spl-type-length-value-derive = { version = "0.2", path = "./derive", optional = true } spl-pod = { version = "0.5.1", path = "../pod" } diff --git a/type-length-value/src/error.rs b/type-length-value/src/error.rs index ce12a4f4..68591a65 100644 --- a/type-length-value/src/error.rs +++ b/type-length-value/src/error.rs @@ -1,12 +1,17 @@ //! Error types -use { - solana_msg::msg, - solana_program_error::{PrintProgramError, ProgramError}, -}; +use solana_program_error::{ProgramError, ToStr}; /// Errors that may be returned by the Token program. #[repr(u32)] -#[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)] +#[derive( + Clone, + Debug, + Eq, + thiserror::Error, + num_enum::TryFromPrimitive, + num_derive::FromPrimitive, + PartialEq, +)] pub enum TlvError { /// Type not found in TLV data #[error("Type not found in TLV data")] @@ -22,18 +27,11 @@ impl From for ProgramError { } } -impl PrintProgramError for TlvError { - fn print(&self) - where - E: 'static + std::error::Error + PrintProgramError + num_traits::FromPrimitive, - { +impl ToStr for TlvError { + fn to_str(&self) -> &'static str { match self { - TlvError::TypeNotFound => { - msg!("Type not found in TLV data") - } - TlvError::TypeAlreadyExists => { - msg!("Type already exists in TLV data") - } + TlvError::TypeNotFound => "Type not found in TLV data", + TlvError::TypeAlreadyExists => "Type already exists in TLV data", } } }