From 350b92213e8a18edf549ebb49b2380fd8ae5a769 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Sat, 16 Mar 2024 03:31:19 +0000 Subject: [PATCH] update errors --- src/error.rs | 12 ++++++------ src/processor/claim.rs | 2 +- src/processor/mine.rs | 26 +++++++++++++------------- src/processor/reset.rs | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/error.rs b/src/error.rs index 4ca9232..f0798a5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,15 +10,15 @@ pub enum OreError { #[error("The epoch has ended and needs reset")] NeedsReset = 1, #[error("The epoch is active and cannot be reset at this time")] - InvalidReset = 2, + ResetTooEarly = 2, #[error("The provided hash was invalid")] - InvalidHash = 3, + HashInvalid = 3, #[error("The provided hash does not satisfy the difficulty requirement")] - InsufficientHashDifficulty = 4, - #[error("The bus has insufficient rewards to mine at this time")] - InsufficientBusRewards = 5, + DifficultyNotSatisfied = 4, + #[error("The bus has insufficient rewards to issue at this time")] + BusRewardsInsufficient = 5, #[error("The claim amount cannot be larger than the claimable rewards")] - InvalidClaimAmount = 6, + ClaimTooLarge = 6, } impl From for ProgramError { diff --git a/src/processor/claim.rs b/src/processor/claim.rs index 64f64ed..9222db1 100644 --- a/src/processor/claim.rs +++ b/src/processor/claim.rs @@ -52,7 +52,7 @@ pub fn process_claim<'a, 'info>( let mut proof_data = proof_info.data.borrow_mut(); let proof = Proof::try_from_bytes_mut(&mut proof_data)?; if proof.claimable_rewards.lt(&amount) { - return Err(OreError::InvalidClaimAmount.into()); + return Err(OreError::ClaimTooLarge.into()); } // Update claimable amount diff --git a/src/processor/mine.rs b/src/processor/mine.rs index 6e80f26..c03ef59 100644 --- a/src/processor/mine.rs +++ b/src/processor/mine.rs @@ -4,7 +4,7 @@ use solana_program::{ account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, - keccak::{hashv, Hash as KeccakHash}, + keccak::{hashv, Hash as KeccakHash, HASH_BYTES}, program::set_return_data, program_error::ProgramError, program_memory::sol_memcmp, @@ -81,7 +81,7 @@ pub fn process_mine<'a, 'info>( let mut bus_data = bus_info.data.borrow_mut(); let bus = Bus::try_from_bytes_mut(&mut bus_data)?; if bus.rewards.lt(&treasury.reward_rate) { - return Err(OreError::InsufficientBusRewards.into()); + return Err(OreError::BusRewardsInsufficient.into()); } bus.rewards = bus.rewards.saturating_sub(treasury.reward_rate); proof.claimable_rewards = proof.claimable_rewards.saturating_add(treasury.reward_rate); @@ -118,13 +118,13 @@ pub(crate) fn validate_hash( signer.as_ref(), nonce.to_le_bytes().as_slice(), ]); - if sol_memcmp(hash.as_ref(), hash_.as_ref(), 32) != 0 { - return Err(OreError::InvalidHash.into()); + if sol_memcmp(hash.as_ref(), hash_.as_ref(), HASH_BYTES) != 0 { + return Err(OreError::HashInvalid.into()); } // Validate hash difficulty if hash.gt(&difficulty) { - return Err(OreError::InsufficientHashDifficulty.into()); + return Err(OreError::DifficultyNotSatisfied.into()); } Ok(()) @@ -133,7 +133,7 @@ pub(crate) fn validate_hash( #[cfg(test)] mod tests { use solana_program::{ - keccak::{hashv, Hash}, + keccak::{hashv, Hash, HASH_BYTES}, pubkey::Pubkey, }; @@ -141,10 +141,10 @@ mod tests { #[test] fn test_validate_hash_pass() { - let h1 = Hash::new_from_array([1; 32]); + let h1 = Hash::new_from_array([1; HASH_BYTES]); let signer = Pubkey::new_unique(); let nonce = 10u64; - let difficulty = Hash::new_from_array([255; 32]); + let difficulty = Hash::new_from_array([255; HASH_BYTES]); let h2 = hashv(&[ h1.to_bytes().as_slice(), signer.to_bytes().as_slice(), @@ -156,21 +156,21 @@ mod tests { #[test] fn test_validate_hash_fail() { - let h1 = Hash::new_from_array([1; 32]); + let h1 = Hash::new_from_array([1; HASH_BYTES]); let signer = Pubkey::new_unique(); let nonce = 10u64; - let difficulty = Hash::new_from_array([255; 32]); - let h2 = Hash::new_from_array([2; 32]); + let difficulty = Hash::new_from_array([255; HASH_BYTES]); + let h2 = Hash::new_from_array([2; HASH_BYTES]); let res = validate_hash(h1, h2, signer, nonce, difficulty); assert!(res.is_err()); } #[test] fn test_validate_hash_fail_difficulty() { - let h1 = Hash::new_from_array([1; 32]); + let h1 = Hash::new_from_array([1; HASH_BYTES]); let signer = Pubkey::new_unique(); let nonce = 10u64; - let difficulty = Hash::new_from_array([0; 32]); + let difficulty = Hash::new_from_array([0; HASH_BYTES]); let h2 = hashv(&[ h1.to_bytes().as_slice(), signer.to_bytes().as_slice(), diff --git a/src/processor/reset.rs b/src/processor/reset.rs index 6a8cec5..8940f0b 100644 --- a/src/processor/reset.rs +++ b/src/processor/reset.rs @@ -72,7 +72,7 @@ pub fn process_reset<'a, 'info>( let treasury = Treasury::try_from_bytes_mut(&mut treasury_data)?; let threshold = treasury.last_reset_at.saturating_add(EPOCH_DURATION); if clock.unix_timestamp.lt(&threshold) { - return Err(OreError::InvalidReset.into()); + return Err(OreError::ResetTooEarly.into()); } // Record current timestamp