Skip to content

Commit

Permalink
refactor: Error codes (#698)
Browse files Browse the repository at this point in the history
Ensure that error codes provided by each crate don't overlap - both with
Anchor (which has error codes up to 6000 range). The ranges proposed here
are:

* `light-hasher`: 7000 range
* `light-bounded-vec`: 8000 range
* `light-hash-set`: 9000 range
* `light-concurrent-merkle-tree`: 10000 range
* `light-indexed-merkle-tree`: 11000 range
* `light-utils`: 12000 range
* `circuit-lib/verifier`: 13000 range
  • Loading branch information
vadorovsky authored May 10, 2024
1 parent 2f71c66 commit 7c0cb5c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 120 deletions.
12 changes: 6 additions & 6 deletions circuit-lib/verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub enum VerifierError {
impl From<VerifierError> for u32 {
fn from(e: VerifierError) -> u32 {
match e {
VerifierError::PublicInputsTryIntoFailed => 8001,
VerifierError::DecompressG1Failed => 8002,
VerifierError::DecompressG2Failed => 8003,
VerifierError::InvalidPublicInputsLength => 8004,
VerifierError::CreateGroth16VerifierFailed => 8005,
VerifierError::ProofVerificationFailed => 8006,
VerifierError::PublicInputsTryIntoFailed => 13001,
VerifierError::DecompressG1Failed => 13002,
VerifierError::DecompressG2Failed => 13003,
VerifierError::InvalidPublicInputsLength => 13004,
VerifierError::CreateGroth16VerifierFailed => 13005,
VerifierError::ProofVerificationFailed => 13006,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions merkle-tree/bounded-vec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub enum BoundedVecError {
impl From<BoundedVecError> for u32 {
fn from(e: BoundedVecError) -> u32 {
match e {
BoundedVecError::Full => 5001,
BoundedVecError::ArraySize(_, _) => 5002,
BoundedVecError::Full => 8001,
BoundedVecError::ArraySize(_, _) => 8002,
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions merkle-tree/concurrent/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,28 @@ pub enum ConcurrentMerkleTreeError {
impl From<ConcurrentMerkleTreeError> for u32 {
fn from(e: ConcurrentMerkleTreeError) -> u32 {
match e {
ConcurrentMerkleTreeError::IntegerOverflow => 2001,
ConcurrentMerkleTreeError::HeightZero => 2002,
ConcurrentMerkleTreeError::ChangelogZero => 2003,
ConcurrentMerkleTreeError::HeightHigherThanMax => 2004,
ConcurrentMerkleTreeError::RootsZero => 2005,
ConcurrentMerkleTreeError::RootHigherThanMax => 2006,
ConcurrentMerkleTreeError::BytesRead => 2007,
ConcurrentMerkleTreeError::TreeFull => 2008,
ConcurrentMerkleTreeError::BatchGreaterThanChangelog(_, _) => 2009,
ConcurrentMerkleTreeError::InvalidProofLength(_, _) => 2010,
ConcurrentMerkleTreeError::InvalidProof(_, _) => 2011,
ConcurrentMerkleTreeError::CannotUpdateLeaf => 2012,
ConcurrentMerkleTreeError::CannotUpdateEmpty => 2013,
ConcurrentMerkleTreeError::AppendOnly => 2014,
ConcurrentMerkleTreeError::EmptyLeaves => 2015,
ConcurrentMerkleTreeError::EmptyChangelogEntries => 2016,
ConcurrentMerkleTreeError::MerklePathsEmptyNode => 2017,
ConcurrentMerkleTreeError::StructBufferSize(_, _) => 2018,
ConcurrentMerkleTreeError::FilledSubtreesBufferSize(_, _) => 2019,
ConcurrentMerkleTreeError::ChangelogBufferSize(_, _) => 2020,
ConcurrentMerkleTreeError::RootBufferSize(_, _) => 2021,
ConcurrentMerkleTreeError::CanopyBufferSize(_, _) => 2022,
ConcurrentMerkleTreeError::IntegerOverflow => 10001,
ConcurrentMerkleTreeError::HeightZero => 10002,
ConcurrentMerkleTreeError::ChangelogZero => 10003,
ConcurrentMerkleTreeError::HeightHigherThanMax => 10004,
ConcurrentMerkleTreeError::RootsZero => 10005,
ConcurrentMerkleTreeError::RootHigherThanMax => 10006,
ConcurrentMerkleTreeError::BytesRead => 10007,
ConcurrentMerkleTreeError::TreeFull => 10008,
ConcurrentMerkleTreeError::BatchGreaterThanChangelog(_, _) => 10009,
ConcurrentMerkleTreeError::InvalidProofLength(_, _) => 10010,
ConcurrentMerkleTreeError::InvalidProof(_, _) => 10011,
ConcurrentMerkleTreeError::CannotUpdateLeaf => 10012,
ConcurrentMerkleTreeError::CannotUpdateEmpty => 10013,
ConcurrentMerkleTreeError::AppendOnly => 10014,
ConcurrentMerkleTreeError::EmptyLeaves => 10015,
ConcurrentMerkleTreeError::EmptyChangelogEntries => 10016,
ConcurrentMerkleTreeError::MerklePathsEmptyNode => 10017,
ConcurrentMerkleTreeError::StructBufferSize(_, _) => 10018,
ConcurrentMerkleTreeError::FilledSubtreesBufferSize(_, _) => 10019,
ConcurrentMerkleTreeError::ChangelogBufferSize(_, _) => 100,
ConcurrentMerkleTreeError::RootBufferSize(_, _) => 10021,
ConcurrentMerkleTreeError::CanopyBufferSize(_, _) => 10022,
ConcurrentMerkleTreeError::Hasher(e) => e.into(),
ConcurrentMerkleTreeError::BoundedVec(e) => e.into(),
}
Expand Down
12 changes: 6 additions & 6 deletions merkle-tree/hash-set/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ pub enum HashSetError {
impl From<HashSetError> for u32 {
fn from(e: HashSetError) -> u32 {
match e {
HashSetError::Full => 6001,
HashSetError::ElementAlreadyExists => 6002,
HashSetError::ElementDoesNotExist => 6003,
HashSetError::UsizeConv => 6004,
HashSetError::IntegerOverflow => 6005,
HashSetError::BufferSize(_, _) => 6006,
HashSetError::Full => 9001,
HashSetError::ElementAlreadyExists => 9002,
HashSetError::ElementDoesNotExist => 9003,
HashSetError::UsizeConv => 9004,
HashSetError::IntegerOverflow => 9005,
HashSetError::BufferSize(_, _) => 9006,
HashSetError::Utils(e) => e.into(),
}
}
Expand Down
78 changes: 6 additions & 72 deletions merkle-tree/hasher/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,9 @@
#[cfg(not(target_os = "solana"))]
use light_poseidon::PoseidonError;
#[cfg(target_os = "solana")]
use solana_program::poseidon::PoseidonSyscallError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum PoseidonSyscallError {
#[error("Invalid parameters.")]
InvalidParameters,
#[error("Invalid endianness.")]
InvalidEndianness,
#[error("Invalid number of inputs. Maximum allowed is 12.")]
InvalidNumberOfInputs,
#[error("Input is an empty slice.")]
EmptyInput,
#[error(
"Invalid length of the input. The length matching the modulus of the prime field is 32."
)]
InvalidInputLength,
#[error("Failed to convert bytest into a prime field element.")]
BytesToPrimeFieldElement,
#[error("Input is larger than the modulus of the prime field.")]
InputLargerThanModulus,
#[error("Failed to convert a vector of bytes into an array.")]
VecToArray,
#[error("Failed to convert the number of inputs from u64 to u8.")]
U64Tou8,
#[error("Failed to convert bytes to BigInt")]
BytesToBigInt,
#[error("Invalid width. Choose a width between 2 and 16 for 1 to 15 inputs.")]
InvalidWidthCircom,
#[error("Unexpected error")]
Unexpected,
}

impl From<u64> for PoseidonSyscallError {
fn from(error: u64) -> Self {
match error {
1 => PoseidonSyscallError::InvalidParameters,
2 => PoseidonSyscallError::InvalidEndianness,
3 => PoseidonSyscallError::InvalidNumberOfInputs,
4 => PoseidonSyscallError::EmptyInput,
5 => PoseidonSyscallError::InvalidInputLength,
6 => PoseidonSyscallError::BytesToPrimeFieldElement,
7 => PoseidonSyscallError::InputLargerThanModulus,
8 => PoseidonSyscallError::VecToArray,
9 => PoseidonSyscallError::U64Tou8,
10 => PoseidonSyscallError::BytesToBigInt,
11 => PoseidonSyscallError::InvalidWidthCircom,
_ => PoseidonSyscallError::Unexpected,
}
}
}

impl From<PoseidonSyscallError> for u64 {
fn from(error: PoseidonSyscallError) -> Self {
match error {
PoseidonSyscallError::InvalidParameters => 2001,
PoseidonSyscallError::InvalidEndianness => 2002,
PoseidonSyscallError::InvalidNumberOfInputs => 2003,
PoseidonSyscallError::EmptyInput => 2004,
PoseidonSyscallError::InvalidInputLength => 2005,
PoseidonSyscallError::BytesToPrimeFieldElement => 2006,
PoseidonSyscallError::InputLargerThanModulus => 2007,
PoseidonSyscallError::VecToArray => 2008,
PoseidonSyscallError::U64Tou8 => 2009,
PoseidonSyscallError::BytesToBigInt => 2010,
PoseidonSyscallError::InvalidWidthCircom => 2011,
PoseidonSyscallError::Unexpected => 2012,
}
}
}

#[derive(Debug, Error)]
pub enum HasherError {
#[error("Integer overflow, value too large")]
Expand All @@ -90,12 +24,12 @@ pub enum HasherError {
impl From<HasherError> for u32 {
fn from(e: HasherError) -> u32 {
match e {
HasherError::IntegerOverflow => 1001,
HasherError::IntegerOverflow => 7001,
#[cfg(not(target_os = "solana"))]
HasherError::Poseidon(_) => 1002,
HasherError::Poseidon(_) => 7002,
#[cfg(target_os = "solana")]
HasherError::PoseidonSyscall(e) => (u64::from(e)).try_into().unwrap_or(1001),
HasherError::UnknownSolanaSyscall(e) => e.try_into().unwrap_or(1001),
HasherError::PoseidonSyscall(e) => (u64::from(e)).try_into().unwrap_or(7002),
HasherError::UnknownSolanaSyscall(e) => e.try_into().unwrap_or(7002),
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion merkle-tree/hasher/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ impl Hasher for Poseidon {
// Call via a system call to perform the calculation.
#[cfg(target_os = "solana")]
{
use crate::{errors::PoseidonSyscallError, HASH_BYTES};
use solana_program::poseidon::PoseidonSyscallError;

use crate::HASH_BYTES;

let mut hash_result = [0; HASH_BYTES];
let result = unsafe {
Expand Down
12 changes: 6 additions & 6 deletions merkle-tree/indexed/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ pub enum IndexedMerkleTreeError {
impl From<IndexedMerkleTreeError> for u32 {
fn from(e: IndexedMerkleTreeError) -> u32 {
match e {
IndexedMerkleTreeError::IntegerOverflow => 10001,
IndexedMerkleTreeError::IndexHigherThanMax => 10002,
IndexedMerkleTreeError::LowElementNotFound => 10003,
IndexedMerkleTreeError::LowElementGreaterOrEqualToNewElement => 10004,
IndexedMerkleTreeError::NewElementGreaterOrEqualToNextElement => 10005,
IndexedMerkleTreeError::ChangelogBufferSize(_, _) => 10006,
IndexedMerkleTreeError::IntegerOverflow => 11001,
IndexedMerkleTreeError::IndexHigherThanMax => 11002,
IndexedMerkleTreeError::LowElementNotFound => 11003,
IndexedMerkleTreeError::LowElementGreaterOrEqualToNewElement => 11004,
IndexedMerkleTreeError::NewElementGreaterOrEqualToNextElement => 11005,
IndexedMerkleTreeError::ChangelogBufferSize(_, _) => 11006,
IndexedMerkleTreeError::Hasher(e) => e.into(),
IndexedMerkleTreeError::ConcurrentMerkleTree(e) => e.into(),
IndexedMerkleTreeError::Utils(e) => e.into(),
Expand Down
2 changes: 1 addition & 1 deletion test-programs/compressed-pda-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ async fn test_with_address() {
event,
Err(BanksClientError::TransactionError(
// ElementAlreadyExists
TransactionError::InstructionError(0, InstructionError::Custom(6002))
TransactionError::InstructionError(0, InstructionError::Custom(9002))
))
));

Expand Down
8 changes: 4 additions & 4 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ pub enum UtilsError {
impl From<UtilsError> for u32 {
fn from(e: UtilsError) -> u32 {
match e {
UtilsError::InputTooLarge(_) => 9001,
UtilsError::InvalidChunkSize => 9002,
UtilsError::InvalidSeeds => 9003,
UtilsError::InvalidRolloverThreshold => 9004,
UtilsError::InputTooLarge(_) => 12001,
UtilsError::InvalidChunkSize => 12002,
UtilsError::InvalidSeeds => 12003,
UtilsError::InvalidRolloverThreshold => 12004,
}
}
}
Expand Down

0 comments on commit 7c0cb5c

Please sign in to comment.