normalize difficulty

This commit is contained in:
Hardhat Chad
2024-07-19 16:50:25 +00:00
parent 81afef2ef2
commit 52aa6beefa
4 changed files with 19 additions and 13 deletions

View File

@@ -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(())
}

View File

@@ -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(

View File

@@ -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();
}