update tolerance

This commit is contained in:
Hardhat Chad
2024-04-30 18:47:48 +00:00
parent 4dd09b4a70
commit 93657d02cc
6 changed files with 89 additions and 3 deletions

View File

@@ -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<OreError> for ProgramError {

View File

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

View File

@@ -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)?,
}

View File

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

View File

@@ -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::*;

View File

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