Skip to content

Commit

Permalink
add check for collision error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Aug 30, 2023
1 parent f1c2286 commit 5da0e02
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
22 changes: 16 additions & 6 deletions libraries/program-error/derive/src/macro_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
};

const SPL_ERROR_HASH_NAMESPACE: &str = "spl_program_error";
const SPL_ERROR_HASH_MIN_VALUE: u32 = 10_0000;

/// The type of macro being called, thus directing which tokens to generate
#[allow(clippy::enum_variant_names)]
Expand Down Expand Up @@ -177,10 +178,19 @@ fn u32_from_hash(enum_ident: &Ident, variant_ident: &Ident) -> u32 {
"{}:{}:{}",
SPL_ERROR_HASH_NAMESPACE, enum_ident, variant_ident
);
let hash = solana_program::hash::hash(hash_input.as_bytes());
u32::from_le_bytes(
hash.to_bytes()[13..17]
.try_into()
.expect("Unable to convert hash to u32"),
)

// We don't want our error code to start at any number below `10_0000`!
let mut nonce: u32 = 0;
loop {
let hash = solana_program::hash::hashv(&[hash_input.as_bytes(), &nonce.to_le_bytes()]);
let d = u32::from_le_bytes(
hash.to_bytes()[13..17]
.try_into()
.expect("Unable to convert hash to u32"),
);
if d >= SPL_ERROR_HASH_MIN_VALUE {
return d;
}
nonce += 1;
}
}
17 changes: 12 additions & 5 deletions libraries/program-error/tests/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn test_macros_compile() {
}

/// Example library error with namespace
#[spl_program_error(hash_error_code_start = 1_275_525_928)]
#[spl_program_error(hash_error_code_start = 3_234_444_947)]
enum ExampleLibraryError {
/// This is a very informative error
#[error("This is a very informative error")]
Expand All @@ -40,10 +40,17 @@ enum ExampleLibraryError {
#[test]
fn test_library_error_codes() {
fn get_error_code_check(hash_input: &str) -> u32 {
let preimage = solana_program::hash::hashv(&[hash_input.as_bytes()]);
let mut bytes = [0u8; 4];
bytes.copy_from_slice(&preimage.to_bytes()[13..17]);
u32::from_le_bytes(bytes)
let mut nonce: u32 = 0;
loop {
let hash = solana_program::hash::hashv(&[hash_input.as_bytes(), &nonce.to_le_bytes()]);
let mut bytes = [0u8; 4];
bytes.copy_from_slice(&hash.to_bytes()[13..17]);
let error_code = u32::from_le_bytes(bytes);
if error_code >= 10_000 {
return error_code;
}
nonce += 1;
}
}

let first_error_as_u32 = ExampleLibraryError::VeryInformativeError as u32;
Expand Down
2 changes: 1 addition & 1 deletion libraries/tlv-account-resolution/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use spl_program_error::*;

/// Errors that may be returned by the Account Resolution library.
#[spl_program_error(hash_error_code_start = 3_300_368_793)]
#[spl_program_error(hash_error_code_start = 2_127_734_810)]
pub enum AccountResolutionError {
/// Incorrect account provided
#[error("Incorrect account provided")]
Expand Down
2 changes: 1 addition & 1 deletion libraries/type-length-value/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use spl_program_error::*;

/// Errors that may be returned by the Token program.
#[spl_program_error(hash_error_code_start = 3_979_750_883)]
#[spl_program_error(hash_error_code_start = 3_787_709_112)]
pub enum TlvError {
/// Type not found in TLV data
#[error("Type not found in TLV data")]
Expand Down
2 changes: 1 addition & 1 deletion token-metadata/interface/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use spl_program_error::*;

/// Errors that may be returned by the interface.
#[spl_program_error(hash_error_code_start = 1_907_429_725)]
#[spl_program_error(hash_error_code_start = 933_549_204)]
pub enum TokenMetadataError {
/// Incorrect account provided
#[error("Incorrect account provided")]
Expand Down
2 changes: 1 addition & 1 deletion token/transfer-hook-interface/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use spl_program_error::*;

/// Errors that may be returned by the interface.
#[spl_program_error(hash_error_code_start = 3_496_640_673)]
#[spl_program_error(hash_error_code_start = 528_258_895)]
pub enum TransferHookError {
/// Incorrect account provided
#[error("Incorrect account provided")]
Expand Down

0 comments on commit 5da0e02

Please sign in to comment.