mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
normalize difficulty
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user