From 41be4300ff21130920260f5906a903a6b99fb250 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Fri, 3 May 2024 02:59:47 +0000 Subject: [PATCH] return data --- src/processor/mine.rs | 29 +++++++++++++---------------- src/utils.rs | 11 +++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/processor/mine.rs b/src/processor/mine.rs index a0d168f..1bc4ba7 100644 --- a/src/processor/mine.rs +++ b/src/processor/mine.rs @@ -6,7 +6,6 @@ use solana_program::{ clock::Clock, entrypoint::ProgramResult, keccak::hashv, - log::sol_log, program_error::ProgramError, pubkey::Pubkey, sanitize::SanitizeError, @@ -21,7 +20,7 @@ use crate::{ instruction::{MineArgs, OreInstruction}, loaders::*, state::{Bus, Config, Proof}, - utils::AccountDeserialize, + utils::{AccountDeserialize, MineEvent}, MIN_DIFFICULTY, ONE_MINUTE, TWO_YEARS, }; @@ -92,7 +91,6 @@ pub fn process_mine<'a, 'info>( // Validate hash satisfies the minimnum difficulty let difficulty = drillx::difficulty(hx); - sol_log(&format!("Diff {}", difficulty)); if difficulty.lt(&MIN_DIFFICULTY) { return Err(OreError::HashTooEasy.into()); } @@ -102,12 +100,12 @@ pub fn process_mine<'a, 'info>( let mut reward = config .base_reward_rate .saturating_mul(2u64.saturating_pow(difficulty)); - sol_log(&format!("Base {}", reward)); // Apply staking multiplier. // To prevent flash loan attacks, only apply if the last deposit was at least 1 block ago. // The multiplier can range 1x to 2x. To receive the maximum multiplier, the stake balance must be // greater than or equal to two years worth of rewards at the selected difficulty. + let mut multiplier = 1f32; if clock.slot.gt(&proof.last_deposit_slot) { let upper_bound = reward.saturating_mul(TWO_YEARS); let staking_reward = proof @@ -115,9 +113,10 @@ pub fn process_mine<'a, 'info>( .min(upper_bound) .saturating_mul(reward) .saturating_div(upper_bound); - reward = reward.saturating_add(staking_reward); - sol_log(&format!("Staking {}", staking_reward)); - } + let multiplied_reward = reward.saturating_add(staking_reward); + multiplier = ((multiplied_reward as f64) / (reward as f64)) as f32; + reward = multiplied_reward; + }; // Apply spam penalty let t = clock.unix_timestamp; @@ -125,7 +124,6 @@ pub fn process_mine<'a, 'info>( let t_spam = t_target.saturating_sub(config.tolerance_spam); if t.lt(&t_spam) { reward = 0; - sol_log("Spam penalty"); } // Apply liveness penalty @@ -136,11 +134,6 @@ pub fn process_mine<'a, 'info>( .saturating_mul(t.saturating_sub(t_liveness) as u64) .saturating_div(ONE_MINUTE as u64), ); - sol_log(&format!( - "Liveness penalty ({} sec) {}", - t.saturating_sub(t_liveness), - reward, - )); } // Limit payout amount to whatever is left in the bus @@ -149,8 +142,6 @@ pub fn process_mine<'a, 'info>( let reward_actual = reward.min(bus.rewards); // Update balances - sol_log(&format!("Total {}", reward)); - sol_log(&format!("Bus {}", bus.rewards)); bus.theoretical_rewards = bus.theoretical_rewards.saturating_add(reward); bus.rewards = bus .rewards @@ -174,7 +165,13 @@ pub fn process_mine<'a, 'info>( proof.total_rewards = proof.total_rewards.saturating_add(reward); // Log the mined rewards - set_return_data(reward_actual.to_le_bytes().as_slice()); + set_return_data(bytemuck::bytes_of(&MineEvent { + difficulty, + multiplier, + reward, + reward_actual, + timing: t.saturating_sub(t_target), + })); Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index df3eac4..c4d80bf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,4 @@ +use bytemuck::{Pod, Zeroable}; use num_enum::{IntoPrimitive, TryFromPrimitive}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, @@ -78,6 +79,16 @@ pub(crate) fn create_pda<'a, 'info>( Ok(()) } +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] +pub struct MineEvent { + pub difficulty: u32, + pub multiplier: f32, + pub reward: u64, + pub reward_actual: u64, + pub timing: i64, +} + #[repr(u8)] #[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] pub enum AccountDiscriminator {