From f7572aa2cf9a7d516acc8e1abae1256df8f3268f Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Tue, 30 Apr 2024 17:50:26 +0000 Subject: [PATCH] tolerance to config --- src/consts.rs | 3 +++ src/lib.rs | 1 + src/processor/initialize.rs | 8 +++++--- src/processor/mine.rs | 16 +++++++--------- src/state/bus.rs | 1 + src/state/config.rs | 6 ++++++ 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 9b3906f..8c7e73c 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -8,6 +8,9 @@ pub const INITIAL_BASE_REWARD_RATE: u64 = 10u64.pow(3u32); /// The minimum difficulty to initialize the program with. pub const INITIAL_MIN_DIFFICULTY: u32 = 12; +/// The spam/liveness tolerance to initialize the program with. +pub const INITIAL_TOLERANCE: i64 = 5; + /// The decimal precision of the ORE token. pub const TOKEN_DECIMALS: u8 = 11; diff --git a/src/lib.rs b/src/lib.rs index c15417e..8a26acc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ use solana_program::{ }; // TODO Admin fn for min difficulty? +// TODO Admin fn for spam/liveness tolerances? // TODO Is downgrade necessary? declare_id!("mineRHF5r6S7HyD9SppBfVMXMavDkJsxwGesEvxZr2A"); diff --git a/src/processor/initialize.rs b/src/processor/initialize.rs index 3e9a083..022c04a 100644 --- a/src/processor/initialize.rs +++ b/src/processor/initialize.rs @@ -17,9 +17,9 @@ use crate::{ utils::create_pda, utils::AccountDeserialize, utils::Discriminator, - BUS, BUS_COUNT, CONFIG, INITIAL_BASE_REWARD_RATE, INITIAL_MIN_DIFFICULTY, METADATA, - METADATA_NAME, METADATA_SYMBOL, METADATA_URI, MINT, MINT_ADDRESS, MINT_NOISE, TOKEN_DECIMALS, - TREASURY, + BUS, BUS_COUNT, CONFIG, INITIAL_BASE_REWARD_RATE, INITIAL_MIN_DIFFICULTY, INITIAL_TOLERANCE, + METADATA, METADATA_NAME, METADATA_SYMBOL, METADATA_URI, MINT, MINT_ADDRESS, MINT_NOISE, + TOKEN_DECIMALS, TREASURY, }; /// Initialize sets up the Ore program. Its responsibilities include: @@ -137,6 +137,8 @@ pub fn process_initialize<'a, 'info>( config.last_reset_at = 0; config.min_difficulty = INITIAL_MIN_DIFFICULTY; config.paused = 0; + config.tolerance_liveness = INITIAL_TOLERANCE; + config.tolerance_spam = INITIAL_TOLERANCE; // Initialize treasury create_pda( diff --git a/src/processor/mine.rs b/src/processor/mine.rs index 16bcddf..a7cc6f0 100644 --- a/src/processor/mine.rs +++ b/src/processor/mine.rs @@ -114,28 +114,26 @@ pub fn process_mine<'a, 'info>( } // Apply spam/liveness penalty - let tolerance_early = 5i64; // TODO Get from config - let tolerance_late = 5i64; // TODO Get from config let t = clock.unix_timestamp; let t_target = proof.last_hash_at.saturating_add(EPOCH_DURATION); - let t_early = t_target.saturating_sub(tolerance_early); - let t_late = t_target.saturating_add(tolerance_late); - if t.lt(&t_early) { + let t_spam = t_target.saturating_sub(config.tolerance_spam); + let t_liveness = t_target.saturating_add(config.tolerance_liveness); + if t.lt(&t_spam) { reward = 0; sol_log("Spam penalty"); - } else if t.gt(&t_late) { + } else if t.gt(&t_liveness) { reward = reward.saturating_sub( reward - .saturating_mul(t.saturating_sub(t_late) as u64) + .saturating_mul(t.saturating_sub(t_liveness) as u64) .saturating_div( t_target .saturating_add(EPOCH_DURATION) - .saturating_sub(t_late) as u64, + .saturating_sub(t_liveness) as u64, ), ); sol_log(&format!( "Liveness penalty ({} sec) {}", - t.saturating_sub(t_late), + t.saturating_sub(t_liveness), reward, )); } diff --git a/src/state/bus.rs b/src/state/bus.rs index a01fd49..b26b726 100644 --- a/src/state/bus.rs +++ b/src/state/bus.rs @@ -15,6 +15,7 @@ pub struct Bus { /// The ID of the bus account. pub id: u64, + // TODO Update logic to count up rather than down /// The quantity of rewards this bus can issue in the current epoch epoch. pub rewards: u64, diff --git a/src/state/config.rs b/src/state/config.rs index b796b69..725d8c3 100644 --- a/src/state/config.rs +++ b/src/state/config.rs @@ -27,6 +27,12 @@ pub struct Config { /// Is mining paused. pub paused: u32, + + /// Seconds prior to a miner's target time during which their hashes will not be penalized. + pub tolerance_spam: i64, + + /// Seconds after a miner's target time during which their hashes will not be penalized. + pub tolerance_liveness: i64, } impl Discriminator for Config {