From 93657d02ccb8ff1a546cc7b5df40800047730196 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Tue, 30 Apr 2024 18:47:48 +0000 Subject: [PATCH] update tolerance --- src/error.rs | 2 ++ src/instruction.rs | 41 ++++++++++++++++++++++++++++- src/lib.rs | 2 +- src/processor/initialize.rs | 2 +- src/processor/mod.rs | 2 ++ src/processor/update_tolerance.rs | 43 +++++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/processor/update_tolerance.rs diff --git a/src/error.rs b/src/error.rs index 5fdaf61..452e229 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,6 +17,8 @@ pub enum OreError { StakeTooLarge = 4, #[error("The clock time is invalid")] ClockInvalid = 5, + #[error("The tolerance cannot be negative")] + ToleranceNegative = 6, } impl From for ProgramError { diff --git a/src/instruction.rs b/src/instruction.rs index ce4465c..0a32ac1 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -105,7 +105,12 @@ pub enum OreInstruction { #[account(0, name = "ore_program", desc = "Ore program")] #[account(1, name = "signer", desc = "Admin signer", signer)] #[account(2, name = "config", desc = "Ore config account", writable)] - Pause = 102, + UpdateTolerance = 102, + + #[account(0, name = "ore_program", desc = "Ore program")] + #[account(1, name = "signer", desc = "Admin signer", signer)] + #[account(2, name = "config", desc = "Ore config account", writable)] + Pause = 103, } impl OreInstruction { @@ -167,6 +172,13 @@ pub struct UpdateAdminArgs { pub new_admin: Pubkey, } +#[repr(C)] +#[derive(Clone, Copy, Debug, Pod, Zeroable)] +pub struct UpdateToleranceArgs { + pub tolerance_liveness: u64, + pub tolerance_spam: u64, +} + #[repr(C)] #[derive(Clone, Copy, Debug, Pod, Zeroable)] pub struct PauseArgs { @@ -180,6 +192,7 @@ impl_to_bytes!(ClaimArgs); impl_to_bytes!(StakeArgs); impl_to_bytes!(UpgradeArgs); impl_to_bytes!(UpdateAdminArgs); +impl_to_bytes!(UpdateToleranceArgs); impl_to_bytes!(PauseArgs); impl_instruction_from_bytes!(InitializeArgs); @@ -189,6 +202,7 @@ impl_instruction_from_bytes!(ClaimArgs); impl_instruction_from_bytes!(StakeArgs); impl_instruction_from_bytes!(UpgradeArgs); impl_instruction_from_bytes!(UpdateAdminArgs); +impl_instruction_from_bytes!(UpdateToleranceArgs); impl_instruction_from_bytes!(PauseArgs); /// Builds a reset instruction. @@ -406,6 +420,31 @@ pub fn update_admin(signer: Pubkey, new_admin: Pubkey) -> Instruction { } } +/// Build an update_tolerance instruction. +pub fn update_tolerance( + signer: Pubkey, + new_liveness_tolerance: u64, + new_spam_tolerance: u64, +) -> Instruction { + Instruction { + program_id: crate::id(), + accounts: vec![ + AccountMeta::new(signer, true), + AccountMeta::new(CONFIG_ADDRESS, false), + ], + data: [ + OreInstruction::UpdateTolerance.to_vec(), + UpdateToleranceArgs { + tolerance_liveness: new_liveness_tolerance, + tolerance_spam: new_spam_tolerance, + } + .to_bytes() + .to_vec(), + ] + .concat(), + } +} + /// Build a pause instruction. pub fn pause(signer: Pubkey, paused: bool) -> Instruction { Instruction { diff --git a/src/lib.rs b/src/lib.rs index d6c17cf..17c0395 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,6 @@ use solana_program::{ program_error::ProgramError, pubkey::Pubkey, }; -// TODO Admin fn for min difficulty? // TODO Admin fn for spam/liveness tolerances? // TODO Alternative to bincode? // TODO Is downgrade necessary? @@ -46,6 +45,7 @@ pub fn process_instruction( OreInstruction::Upgrade => process_upgrade(program_id, accounts, data)?, OreInstruction::Initialize => process_initialize(program_id, accounts, data)?, OreInstruction::UpdateAdmin => process_update_admin(program_id, accounts, data)?, + OreInstruction::UpdateTolerance => process_update_tolerance(program_id, accounts, data)?, OreInstruction::Pause => process_pause(program_id, accounts, data)?, } diff --git a/src/processor/initialize.rs b/src/processor/initialize.rs index cdf8d0c..85c424a 100644 --- a/src/processor/initialize.rs +++ b/src/processor/initialize.rs @@ -18,7 +18,7 @@ use crate::{ utils::AccountDeserialize, utils::Discriminator, BUS, BUS_COUNT, CONFIG, INITIAL_BASE_REWARD_RATE, INITIAL_TOLERANCE, METADATA, MINT, - MINT_ADDRESS, MINT_NOISE, TOKEN_DECIMALS, TREASURY, TWO_YEARS, + MINT_ADDRESS, MINT_NOISE, TOKEN_DECIMALS, TREASURY, }; /// Initialize sets up the Ore program. Its responsibilities include: diff --git a/src/processor/mod.rs b/src/processor/mod.rs index 66b20b4..f1dd9cc 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -6,6 +6,7 @@ mod register; mod reset; mod stake; mod update_admin; +mod update_tolerance; mod upgrade; pub use claim::*; @@ -16,4 +17,5 @@ pub use register::*; pub use reset::*; pub use stake::*; pub use update_admin::*; +pub use update_tolerance::*; pub use upgrade::*; diff --git a/src/processor/update_tolerance.rs b/src/processor/update_tolerance.rs new file mode 100644 index 0000000..25ea1c9 --- /dev/null +++ b/src/processor/update_tolerance.rs @@ -0,0 +1,43 @@ +use solana_program::{ + account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, + pubkey::Pubkey, +}; + +use crate::{ + error::OreError, instruction::UpdateToleranceArgs, loaders::*, state::Config, + utils::AccountDeserialize, +}; + +pub fn process_update_tolerance<'a, 'info>( + _program_id: &Pubkey, + accounts: &'a [AccountInfo<'info>], + data: &[u8], +) -> ProgramResult { + // Parse args + let args = UpdateToleranceArgs::try_from_bytes(data)?; + + // Load accounts + let [signer, config_info] = accounts else { + return Err(ProgramError::NotEnoughAccountKeys); + }; + load_signer(signer)?; + load_config(config_info, true)?; + + // Validate signer is admin + let mut config_data = config_info.data.borrow_mut(); + let config = Config::try_from_bytes_mut(&mut config_data)?; + if config.admin.ne(&signer.key) { + return Err(ProgramError::MissingRequiredSignature); + } + + // Update tolerances + config.tolerance_liveness = args.tolerance_liveness as i64; + config.tolerance_spam = args.tolerance_spam as i64; + + // Sanity checks + if config.tolerance_liveness.lt(&0) || config.tolerance_spam.lt(&0) { + return Err(OreError::ToleranceNegative.into()); + } + + Ok(()) +}