From 7e931a1254d6a12a1c63ea5e48da7f306c85cd05 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Tue, 24 Sep 2024 05:28:14 +0000 Subject: [PATCH] reimplement staking multiplier --- program/src/mine.rs | 24 ++++++++++++++++++++++++ program/src/reset.rs | 10 +++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/program/src/mine.rs b/program/src/mine.rs index 0335e6c..0daba4b 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -110,6 +110,30 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult .checked_mul(2u64.checked_pow(normalized_difficulty).unwrap()) .unwrap(); + // Apply staking multiplier. + // + // If user has greater than or equal to the max stake on the network, they receive 2x multiplier. + // Any stake less than this will receives between 1x and 2x multipler. The multipler is only active + // if the miner's last stake deposit was more than one minute ago to protect against flash loan attacks. + let mut bus_data = bus_info.data.borrow_mut(); + let bus = Bus::try_from_bytes_mut(&mut bus_data)?; + if proof.balance.gt(&0) && proof.last_stake_at.saturating_add(ONE_MINUTE).lt(&t) { + // Calculate staking reward. + if config.top_balance.gt(&0) { + let staking_reward = (reward as u128) + .checked_mul(proof.balance.min(config.top_balance) as u128) + .unwrap() + .checked_div(config.top_balance as u128) + .unwrap() as u64; + reward = reward.checked_add(staking_reward).unwrap(); + } + + // Update bus stake tracker. + if proof.balance.gt(&bus.top_balance) { + bus.top_balance = proof.balance; + } + } + // Apply boosts. // // Boosts are incentives that can multiply a miner's rewards by staking tokens in the ORE Boosts program. diff --git a/program/src/reset.rs b/program/src/reset.rs index a507b62..eda9489 100644 --- a/program/src/reset.rs +++ b/program/src/reset.rs @@ -56,16 +56,16 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul // Reset bus accounts and calculate actual rewards mined since last reset. let mut total_remaining_rewards = 0u64; let mut total_theoretical_rewards = 0u64; - // let mut top_balance = 0u64; + let mut top_balance = 0u64; for i in 0..BUS_COUNT { // Parse bus account. let mut bus_data = busses[i].data.borrow_mut(); let bus = Bus::try_from_bytes_mut(&mut bus_data)?; // Track top balance. - // if bus.top_balance.gt(&top_balance) { - // top_balance = bus.top_balance; - // } + if bus.top_balance.gt(&top_balance) { + top_balance = bus.top_balance; + } // Track accumulators. total_remaining_rewards = total_remaining_rewards.saturating_add(bus.rewards); @@ -80,7 +80,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul let total_epoch_rewards = MAX_EPOCH_REWARDS.saturating_sub(total_remaining_rewards); // Update global top balance. - config.top_balance = 0; // top_balance; + config.top_balance = top_balance; // Update base reward rate for next epoch. config.base_reward_rate =