From 7e6dc82f45b9aafd00eaae33d5a98588420be7d6 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Wed, 28 Aug 2024 16:37:56 +0000 Subject: [PATCH] fix overflow issue --- program/src/mine.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/program/src/mine.rs b/program/src/mine.rs index 069236b..627e4d3 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -14,7 +14,6 @@ use ore_boost_api::{ state::{Boost, Stake}, }; use ore_utils::*; -use solana_program::program::set_return_data; #[allow(deprecated)] use solana_program::{ account_info::AccountInfo, @@ -28,6 +27,7 @@ use solana_program::{ slot_hashes::SlotHash, sysvar::{self, Sysvar}, }; +use solana_program::{log, program::set_return_data}; /// Mine validates hashes and increments a miner's collectable balance. pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult { @@ -105,15 +105,17 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult let normalized_difficulty = difficulty .checked_sub(config.min_difficulty as u32) .unwrap(); - let mut reward = config + let base_reward = config .base_reward_rate .checked_mul(2u64.checked_pow(normalized_difficulty).unwrap()) .unwrap(); + let mut reward = base_reward; // Apply boosts. // // Boosts are incentives that can multiply a miner's rewards by staking tokens in the ORE Boosts program. // Up to 3 boosts can be applied on any given mine operation. + log::sol_log(&format!("Base: {}", base_reward)); let mut boosts = [Pubkey::new_from_array([0; 32]); 3]; for i in 0..3 { if optional_accounts.len().gt(&(i * 2)) { @@ -140,14 +142,15 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Apply multiplier if last stake was greater than one minute ago. if stake.last_stake_at.saturating_add(ONE_MINUTE).le(&t) { let multiplier = boost.multiplier.checked_sub(1).unwrap(); - let multiplier_reward = reward - .checked_mul(multiplier) + let boost_reward = (base_reward as u128) + .checked_mul(multiplier as u128) .unwrap() - .checked_mul(stake.balance) + .checked_mul(stake.balance as u128) .unwrap() - .checked_div(boost.total_stake) - .unwrap(); - reward = reward.checked_add(multiplier_reward).unwrap(); + .checked_div(boost.total_stake as u128) + .unwrap() as u64; + log::sol_log(&format!("Boost: {}", boost_reward)); + reward = reward.checked_add(boost_reward).unwrap(); } } }