mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
remove crown instruction
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
use ore_api::{
|
||||
error::OreError,
|
||||
loaders::*,
|
||||
state::{Config, Proof},
|
||||
};
|
||||
use ore_api::{loaders::*, state::Proof};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey, system_program,
|
||||
@@ -24,21 +20,13 @@ pub fn process_close<'a, 'info>(
|
||||
_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
// Load accounts
|
||||
let [signer, config_info, proof_info, system_program] = accounts else {
|
||||
let [signer, proof_info, system_program] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_config(config_info, false)?;
|
||||
load_proof(proof_info, signer.key, true)?;
|
||||
load_program(system_program, system_program::id())?;
|
||||
|
||||
// Validate the account is not the crowned top staker.
|
||||
let config_data = config_info.data.borrow();
|
||||
let config = Config::try_from_bytes(&config_data)?;
|
||||
if config.top_staker.eq(proof_info.key) {
|
||||
return Err(OreError::CannotClose.into());
|
||||
}
|
||||
|
||||
// Validate balance is zero
|
||||
let proof_data = proof_info.data.borrow();
|
||||
let proof = Proof::try_from_bytes(&proof_data)?;
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
use ore_api::{
|
||||
consts::ONE_MINUTE,
|
||||
error::OreError,
|
||||
loaders::*,
|
||||
state::{Config, Proof},
|
||||
};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
|
||||
program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar,
|
||||
};
|
||||
|
||||
use crate::utils::AccountDeserialize;
|
||||
|
||||
/// Crown flags an account as the top staker if their balance is greater than the last known top staker.
|
||||
pub fn process_crown<'a, 'info>(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &'a [AccountInfo<'info>],
|
||||
_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
// Load accounts
|
||||
let [signer, config_info, proof_info, proof_new_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_config(config_info, true)?;
|
||||
load_any_proof(proof_new_info, false)?;
|
||||
|
||||
// Load the proof accounts.
|
||||
let clock = Clock::get().unwrap();
|
||||
let proof_new_data = proof_new_info.data.borrow();
|
||||
let proof_new = Proof::try_from_bytes(&proof_new_data)?;
|
||||
if proof_new
|
||||
.last_stake_at
|
||||
.saturating_add(ONE_MINUTE)
|
||||
.gt(&clock.unix_timestamp)
|
||||
{
|
||||
return Err(OreError::CannotCrown.into());
|
||||
}
|
||||
|
||||
// If top staker is the default null address, skip this.
|
||||
let mut config_data = config_info.data.borrow_mut();
|
||||
let config = Config::try_from_bytes_mut(&mut config_data)?;
|
||||
if config.top_staker.ne(&Pubkey::new_from_array([0; 32])) {
|
||||
// Load current top staker
|
||||
load_any_proof(proof_info, false)?;
|
||||
let proof_data = proof_info.data.borrow();
|
||||
let proof = Proof::try_from_bytes(&proof_data)?;
|
||||
if proof_info.key.ne(&config.top_staker) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Compare balances
|
||||
if proof_new.balance.lt(&proof.balance) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
// Crown the new top staker.
|
||||
config.top_staker = *proof_new_info.key;
|
||||
config.top_staker_balance = proof_new.balance;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -141,8 +141,7 @@ pub fn process_initialize<'a, 'info>(
|
||||
config.base_reward_rate = INITIAL_BASE_REWARD_RATE;
|
||||
config.last_reset_at = 0;
|
||||
config.min_difficulty = INITIAL_MIN_DIFFICULTY as u64;
|
||||
config.top_staker = Pubkey::new_from_array([0; 32]);
|
||||
config.top_staker_balance = 0;
|
||||
config.top_balance = 0;
|
||||
|
||||
// Initialize treasury
|
||||
create_pda(
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
mod claim;
|
||||
mod close;
|
||||
mod crown;
|
||||
mod initialize;
|
||||
mod mine;
|
||||
mod open;
|
||||
@@ -11,7 +10,6 @@ mod upgrade;
|
||||
|
||||
use claim::*;
|
||||
use close::*;
|
||||
use crown::*;
|
||||
use initialize::*;
|
||||
use mine::*;
|
||||
use open::*;
|
||||
@@ -47,7 +45,6 @@ pub fn process_instruction(
|
||||
match OreInstruction::try_from(*tag).or(Err(ProgramError::InvalidInstructionData))? {
|
||||
OreInstruction::Claim => process_claim(program_id, accounts, data)?,
|
||||
OreInstruction::Close => process_close(program_id, accounts, data)?,
|
||||
OreInstruction::Crown => process_crown(program_id, accounts, data)?,
|
||||
OreInstruction::Mine => process_mine(program_id, accounts, data)?,
|
||||
OreInstruction::Open => process_open(program_id, accounts, data)?,
|
||||
OreInstruction::Reset => process_reset(program_id, accounts, data)?,
|
||||
|
||||
@@ -115,14 +115,14 @@ pub fn process_mine<'a, 'info>(
|
||||
// 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.
|
||||
if config.max_stake.gt(&0)
|
||||
if config.top_balance.gt(&0)
|
||||
&& proof.balance.gt(&0)
|
||||
&& proof.last_stake_at.saturating_add(ONE_MINUTE).le(&t)
|
||||
&& proof.last_stake_at.saturating_add(ONE_MINUTE).lt(&t)
|
||||
{
|
||||
let staking_reward = (reward as u128)
|
||||
.checked_mul(proof.balance.min(config.top_staker_balance) as u128)
|
||||
.checked_mul(proof.balance.min(config.top_balance) as u128)
|
||||
.unwrap()
|
||||
.checked_div(config.top_staker_balance as u128)
|
||||
.checked_div(config.top_balance as u128)
|
||||
.unwrap() as u64;
|
||||
reward = reward.checked_add(staking_reward).unwrap();
|
||||
}
|
||||
@@ -148,6 +148,9 @@ pub fn process_mine<'a, 'info>(
|
||||
bus.theoretical_rewards = bus.theoretical_rewards.checked_add(reward).unwrap();
|
||||
bus.rewards = bus.rewards.checked_sub(reward_actual).unwrap();
|
||||
proof.balance = proof.balance.checked_add(reward_actual).unwrap();
|
||||
if proof.balance.gt(&bus.top_balance) {
|
||||
bus.top_balance = proof.balance;
|
||||
}
|
||||
|
||||
// Hash recent slot hash into the next challenge to prevent pre-mining attacks
|
||||
proof.last_hash = hash.h;
|
||||
|
||||
@@ -79,9 +79,13 @@ pub fn process_reset<'a, 'info>(
|
||||
// Reset bus accounts and calculate actual rewards mined since last reset
|
||||
let mut total_remaining_rewards = 0u64;
|
||||
let mut total_theoretical_rewards = 0u64;
|
||||
let mut top_balance = 0u64;
|
||||
for i in 0..BUS_COUNT {
|
||||
let mut bus_data = busses[i].data.borrow_mut();
|
||||
let bus = Bus::try_from_bytes_mut(&mut bus_data)?;
|
||||
if bus.top_balance.gt(&top_balance) {
|
||||
top_balance = bus.top_balance;
|
||||
}
|
||||
total_remaining_rewards = total_remaining_rewards.saturating_add(bus.rewards);
|
||||
total_theoretical_rewards =
|
||||
total_theoretical_rewards.saturating_add(bus.theoretical_rewards);
|
||||
@@ -90,6 +94,9 @@ pub fn process_reset<'a, 'info>(
|
||||
}
|
||||
let total_epoch_rewards = MAX_EPOCH_REWARDS.saturating_sub(total_remaining_rewards);
|
||||
|
||||
// Update the top balance
|
||||
config.top_balance = top_balance;
|
||||
|
||||
// Update base reward rate for next epoch
|
||||
config.base_reward_rate =
|
||||
calculate_new_reward_rate(config.base_reward_rate, total_theoretical_rewards);
|
||||
|
||||
Reference in New Issue
Block a user