diff --git a/api/src/state/config.rs b/api/src/state/config.rs index 970a5d1..3dacec7 100644 --- a/api/src/state/config.rs +++ b/api/src/state/config.rs @@ -10,20 +10,20 @@ use super::AccountDiscriminator; #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Pod, ShankAccount, Zeroable)] pub struct Config { - // TODO Remove this - pub admin: Pubkey, - /// The base reward rate paid out for a hash of minimum difficulty. pub base_reward_rate: u64, /// The timestamp of the last reset. pub last_reset_at: i64, - /// The largest known stake balance on the network. - pub max_stake: u64, + /// The minimum accepted difficulty. + pub min_difficulty: u64, /// The address of the proof account with the highest stake balance. pub top_staker: Pubkey, + + /// The largest known stake balance on the network. + pub top_staker_balance: u64, } impl Discriminator for Config { diff --git a/program/src/crown.rs b/program/src/crown.rs index e9a2463..41c2999 100644 --- a/program/src/crown.rs +++ b/program/src/crown.rs @@ -56,8 +56,8 @@ pub fn process_crown<'a, 'info>( } // Crown the new top staker. - config.max_stake = proof_new.balance; config.top_staker = *proof_new_info.key; + config.top_staker_balance = proof_new.balance; Ok(()) } diff --git a/program/src/initialize.rs b/program/src/initialize.rs index dc81adf..3a30d86 100644 --- a/program/src/initialize.rs +++ b/program/src/initialize.rs @@ -140,8 +140,9 @@ pub fn process_initialize<'a, 'info>( let config = Config::try_from_bytes_mut(&mut config_data)?; config.base_reward_rate = INITIAL_BASE_REWARD_RATE; config.last_reset_at = 0; - config.max_stake = 0; + config.min_difficulty = MIN_DIFFICULTY as u64; config.top_staker = Pubkey::new_from_array([0; 32]); + config.top_staker_balance = 0; // Initialize treasury create_pda( diff --git a/program/src/mine.rs b/program/src/mine.rs index cf016e0..f60b8a5 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -89,25 +89,30 @@ pub fn process_mine<'a, 'info>( // Validate hash satisfies the minimnum difficulty. let hash = solution.to_hash(); let difficulty = hash.difficulty(); - sol_log(&format!("Diff {}", difficulty)); - if difficulty.lt(&MIN_DIFFICULTY) { + if difficulty.lt(&(config.min_difficulty as u32)) { return Err(OreError::HashTooEasy.into()); } + + // Normalize difficulty and calculate reward rate + let normalized_difficulty = difficulty + .checked_sub(config.min_difficulty as u32) + .unwrap(); let mut reward = config .base_reward_rate - .checked_mul(2u64.checked_pow(difficulty).unwrap()) + .checked_mul(2u64.checked_pow(normalized_difficulty).unwrap()) .unwrap(); + sol_log(&format!("Diff {}", difficulty)); // 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. let t = clock.unix_timestamp; - if config.max_stake.gt(&0) && proof.last_stake_at.saturating_add(ONE_MINUTE).le(&t) { + if config.top_staker_balance.gt(&0) && proof.last_stake_at.saturating_add(ONE_MINUTE).le(&t) { let staking_reward = (reward as u128) - .checked_mul(proof.balance.min(config.max_stake) as u128) + .checked_mul(proof.balance.min(config.top_staker_balance) as u128) .unwrap() - .checked_div(config.max_stake as u128) + .checked_div(config.top_staker_balance as u128) .unwrap() as u64; reward = reward.checked_add(staking_reward).unwrap(); }