From aac3360885769cdd1c982dada3ec7e67f3178cab Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Fri, 19 Jul 2024 19:31:38 +0000 Subject: [PATCH] make min difficulty dynamic --- Cargo.toml | 4 ++-- api/src/consts.rs | 20 ++++++++++++++------ program/src/initialize.rs | 4 ++-- program/src/reset.rs | 12 ++++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 407ba36..bffb131 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,8 @@ const-crypto = "0.1.0" drillx = { version = "2.0.0-beta.1", features = ["solana"] } mpl-token-metadata = "4.1.2" num_enum = "0.7.2" -ore-api = { path = "api", version = "2.0.0-beta.2" } -ore-utils = { path = "utils", version = "2.0.0-beta.2", features = ["spl"] } +ore-api = { path = "api", version = "2.0.0-beta.3" } +ore-utils = { path = "utils", version = "2.0.0-beta.3", features = ["spl"] } shank = "0.3.0" solana-program = "^1.18" spl-token = { version = "^4", features = ["no-entrypoint"] } diff --git a/api/src/consts.rs b/api/src/consts.rs index c22a4d4..6658a27 100644 --- a/api/src/consts.rs +++ b/api/src/consts.rs @@ -2,17 +2,25 @@ use array_const_fn_init::array_const_fn_init; use const_crypto::ed25519; use solana_program::{pubkey, pubkey::Pubkey}; -/// The reward rate to intialize the program with. -pub const INITIAL_BASE_REWARD_RATE: u64 = 10u64.pow(3u32); - /// The admin allowed to initialize the program. -pub const INITIAL_ADMIN: Pubkey = pubkey!("HBUh9g46wk2X89CvaNN15UmsznP59rh6od1h8JwYAopk"); +pub const ADMIN: Pubkey = pubkey!("HBUh9g46wk2X89CvaNN15UmsznP59rh6od1h8JwYAopk"); + +/// The reward rate to intialize the program with. +pub const INITIAL_BASE_REWARD_RATE: u64 = 2u64.pow(25); // 10u64.pow(3u32); + +/// The minimum threshold for the base reward rate, at which point the min difficulty should be increased +// TODO 2^8 (0.00000000256) +pub const BASE_REWARD_RATE_MIN_THRESHOLD: u64 = 2u64.pow(24); + +/// The maximum threshold for the base reward rate, at which point the min difficulty should be decreased. +// TODO 2^32 (0.04294967296) +pub const BASE_REWARD_RATE_MAX_THRESHOLD: u64 = 2u64.pow(26); /// The spam/liveness tolerance in seconds. pub const TOLERANCE: i64 = 5; -/// The minimum difficulty required of all submitted hashes. -pub const MIN_DIFFICULTY: u32 = 8; +/// The minimum difficulty to initialize the program with. +pub const INITIAL_MIN_DIFFICULTY: u32 = 0; // 8; /// The decimal precision of the ORE token. /// There are 100 billion indivisible units per ORE (called "grains"). diff --git a/program/src/initialize.rs b/program/src/initialize.rs index 3a30d86..ce850e3 100644 --- a/program/src/initialize.rs +++ b/program/src/initialize.rs @@ -91,7 +91,7 @@ pub fn process_initialize<'a, 'info>( load_sysvar(rent_sysvar, sysvar::rent::id())?; // Check signer - if signer.key.ne(&INITIAL_ADMIN) { + if signer.key.ne(&ADMIN) { return Err(ProgramError::MissingRequiredSignature); } @@ -140,7 +140,7 @@ 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.min_difficulty = MIN_DIFFICULTY as u64; + config.min_difficulty = INITIAL_MIN_DIFFICULTY as u64; config.top_staker = Pubkey::new_from_array([0; 32]); config.top_staker_balance = 0; diff --git a/program/src/reset.rs b/program/src/reset.rs index 47d151e..63c0754 100644 --- a/program/src/reset.rs +++ b/program/src/reset.rs @@ -94,6 +94,18 @@ pub fn process_reset<'a, 'info>( config.base_reward_rate = calculate_new_reward_rate(config.base_reward_rate, total_theoretical_rewards); + // If base_reward_rate is too low, then increment difficulty by 1 and double base reward rate + if config.base_reward_rate.le(&BASE_REWARD_RATE_MIN_THRESHOLD) { + config.min_difficulty = config.min_difficulty.checked_add(1).unwrap(); + config.base_reward_rate = config.base_reward_rate.checked_mul(2).unwrap(); + } + + // If base reward rate is too high, then decrement difficulty by 1 and halve base reward rate + if config.base_reward_rate.ge(&BASE_REWARD_RATE_MAX_THRESHOLD) && config.min_difficulty.gt(&0) { + config.min_difficulty = config.min_difficulty.checked_sub(1).unwrap(); + config.base_reward_rate = config.base_reward_rate.checked_div(2).unwrap(); + } + // Max supply check let mint = Mint::unpack(&mint_info.data.borrow()).expect("Failed to parse mint"); if mint.supply.ge(&MAX_SUPPLY) {