mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-19 23:16:46 +00:00
continued refactor
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -2607,6 +2607,7 @@ dependencies = [
|
||||
"spl-token",
|
||||
"static_assertions",
|
||||
"thiserror",
|
||||
"utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2624,6 +2625,7 @@ dependencies = [
|
||||
"spl-associated-token-account",
|
||||
"spl-token",
|
||||
"tokio",
|
||||
"utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5722,6 +5724,14 @@ version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||
|
||||
[[package]]
|
||||
name = "utils"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["core/*"]
|
||||
members = ["core/*", "utils"]
|
||||
|
||||
[workspace.package]
|
||||
version = "2.0.0"
|
||||
@@ -13,8 +13,10 @@ readme = "./README.md"
|
||||
keywords = ["solana", "crypto", "mining"]
|
||||
|
||||
[workspace.dependencies]
|
||||
bytemuck = "1.14.3"
|
||||
drillx = { git = "https://github.com/regolith-labs/drillx", branch = "master", features = ["solana"] }
|
||||
mpl-token-metadata = "4.1.2"
|
||||
solana-program = "1.18"
|
||||
spl-token = { version = "^4", features = ["no-entrypoint"] }
|
||||
spl-associated-token-account = { version = "^2.2", features = [ "no-entrypoint" ] }
|
||||
utils = { path = "utils" }
|
||||
|
||||
@@ -22,4 +22,5 @@ solana-program.workspace = true
|
||||
spl-token.workspace = true
|
||||
spl-associated-token-account.workspace = true
|
||||
static_assertions = "1.1.0"
|
||||
thiserror = "1.0.57"
|
||||
thiserror = "1.0.57"
|
||||
utils.workspace = true
|
||||
13
core/api/src/event.rs
Normal file
13
core/api/src/event.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
|
||||
use crate::utils::impl_to_bytes;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct MineEvent {
|
||||
pub difficulty: u64,
|
||||
pub reward: u64,
|
||||
pub timing: i64,
|
||||
}
|
||||
|
||||
impl_to_bytes!(MineEvent);
|
||||
@@ -8,7 +8,10 @@ use solana_program::{
|
||||
system_program, sysvar,
|
||||
};
|
||||
|
||||
use crate::{consts::*, impl_instruction_from_bytes, impl_to_bytes};
|
||||
use crate::{
|
||||
consts::*,
|
||||
utils::{impl_instruction_from_bytes, impl_to_bytes},
|
||||
};
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, ShankInstruction, TryFromPrimitive)]
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
pub mod consts;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod instruction;
|
||||
pub mod state;
|
||||
pub mod utils;
|
||||
|
||||
pub(crate) use utils;
|
||||
|
||||
use solana_program::declare_id;
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use shank::ShankAccount;
|
||||
|
||||
use crate::{
|
||||
impl_account_from_bytes, impl_to_bytes,
|
||||
utils::{AccountDiscriminator, Discriminator},
|
||||
};
|
||||
use crate::utils::{impl_account_from_bytes, impl_to_bytes, Discriminator};
|
||||
|
||||
use super::AccountDiscriminator;
|
||||
|
||||
/// Bus accounts are responsible for distributing mining rewards.
|
||||
/// There are 8 busses total to minimize write-lock contention and allow for parallel mine operations.
|
||||
@@ -23,8 +22,8 @@ pub struct Bus {
|
||||
}
|
||||
|
||||
impl Discriminator for Bus {
|
||||
fn discriminator() -> AccountDiscriminator {
|
||||
AccountDiscriminator::Bus
|
||||
fn discriminator() -> u8 {
|
||||
AccountDiscriminator::Bus.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,9 @@ use bytemuck::{Pod, Zeroable};
|
||||
use shank::ShankAccount;
|
||||
use solana_program::pubkey::Pubkey;
|
||||
|
||||
use crate::{
|
||||
impl_account_from_bytes, impl_to_bytes,
|
||||
utils::{AccountDiscriminator, Discriminator},
|
||||
};
|
||||
use crate::utils::{impl_account_from_bytes, impl_to_bytes, Discriminator};
|
||||
|
||||
use super::AccountDiscriminator;
|
||||
|
||||
/// Config is a singleton account which manages admin configurable variables.
|
||||
#[repr(C)]
|
||||
@@ -28,8 +27,8 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Discriminator for Config {
|
||||
fn discriminator() -> AccountDiscriminator {
|
||||
AccountDiscriminator::Config
|
||||
fn discriminator() -> u8 {
|
||||
AccountDiscriminator::Config.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,3 +7,14 @@ pub use bus::*;
|
||||
pub use config::*;
|
||||
pub use proof::*;
|
||||
pub use treasury::*;
|
||||
|
||||
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
|
||||
pub enum AccountDiscriminator {
|
||||
Bus = 100,
|
||||
Config = 101,
|
||||
Proof = 102,
|
||||
Treasury = 103,
|
||||
}
|
||||
|
||||
@@ -2,10 +2,9 @@ use bytemuck::{Pod, Zeroable};
|
||||
use shank::ShankAccount;
|
||||
use solana_program::pubkey::Pubkey;
|
||||
|
||||
use crate::{
|
||||
impl_account_from_bytes, impl_to_bytes,
|
||||
utils::{AccountDiscriminator, Discriminator},
|
||||
};
|
||||
use crate::utils::{impl_account_from_bytes, impl_to_bytes, Discriminator};
|
||||
|
||||
use super::AccountDiscriminator;
|
||||
|
||||
/// Proof accounts track a miner's current hash, claimable rewards, and lifetime stats.
|
||||
/// Every miner is allowed one proof account which is required by the program to mine or claim rewards.
|
||||
@@ -41,8 +40,8 @@ pub struct Proof {
|
||||
}
|
||||
|
||||
impl Discriminator for Proof {
|
||||
fn discriminator() -> AccountDiscriminator {
|
||||
AccountDiscriminator::Proof
|
||||
fn discriminator() -> u8 {
|
||||
AccountDiscriminator::Proof.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use shank::ShankAccount;
|
||||
|
||||
use crate::{
|
||||
impl_account_from_bytes, impl_to_bytes,
|
||||
utils::{AccountDiscriminator, Discriminator},
|
||||
};
|
||||
use crate::utils::{impl_account_from_bytes, impl_to_bytes, Discriminator};
|
||||
|
||||
use super::AccountDiscriminator;
|
||||
|
||||
/// Treasury is a singleton account which manages all program wide variables.
|
||||
/// It is the mint authority for the Ore token and also the authority of the program-owned token account.
|
||||
@@ -13,8 +12,8 @@ use crate::{
|
||||
pub struct Treasury {}
|
||||
|
||||
impl Discriminator for Treasury {
|
||||
fn discriminator() -> AccountDiscriminator {
|
||||
AccountDiscriminator::Treasury
|
||||
fn discriminator() -> u8 {
|
||||
AccountDiscriminator::Treasury.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ ore-api = { path = "../api" }
|
||||
solana-program.workspace = true
|
||||
spl-token.workspace = true
|
||||
spl-associated-token-account.workspace = true
|
||||
utils.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
bs64 = "0.1.2"
|
||||
|
||||
@@ -8,6 +8,8 @@ use solana_program::{
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
pub(crate) use utils;
|
||||
|
||||
#[cfg(not(feature = "no-entrypoint"))]
|
||||
solana_program::entrypoint!(process_instruction);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use ore_api::{
|
||||
consts::*,
|
||||
state::{Bus, Config, Proof, Treasury},
|
||||
utils::{AccountDeserialize, Discriminator},
|
||||
};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey,
|
||||
@@ -9,6 +8,8 @@ use solana_program::{
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
|
||||
use crate::utils::{AccountDeserialize, Discriminator};
|
||||
|
||||
/// Errors if:
|
||||
/// - Account is not a signer.
|
||||
pub fn load_signer<'a, 'info>(info: &'a AccountInfo<'info>) -> Result<(), ProgramError> {
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
use ore_api::{
|
||||
consts::*, error::OreError, instruction::ClaimArgs, state::Proof, utils::AccountDeserialize,
|
||||
};
|
||||
use ore_api::{consts::*, error::OreError, instruction::ClaimArgs, state::Proof};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{loaders::*, utils::AccountDeserialize};
|
||||
|
||||
/// Claim distributes Ore from the treasury to a miner. Its responsibilies include:
|
||||
/// 1. Decrement the miner's claimable balance.
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use ore_api::{state::Proof, utils::AccountDeserialize};
|
||||
use ore_api::state::Proof;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey, system_program,
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{loaders::*, utils::AccountDeserialize};
|
||||
|
||||
/// Close closes a proof account and returns the rent to the owner. Its responsibilities include:
|
||||
/// 1. Realloc proof account size to 0.
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
use ore_api::{
|
||||
state::{Config, Proof},
|
||||
utils::AccountDeserialize,
|
||||
};
|
||||
use ore_api::state::{Config, Proof};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{loaders::*, 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>(
|
||||
|
||||
@@ -4,9 +4,6 @@ use ore_api::{
|
||||
consts::*,
|
||||
instruction::*,
|
||||
state::{Bus, Config, Treasury},
|
||||
utils::create_pda,
|
||||
utils::AccountDeserialize,
|
||||
utils::Discriminator,
|
||||
};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
@@ -18,7 +15,10 @@ use solana_program::{
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{
|
||||
loaders::*,
|
||||
utils::{create_pda, AccountDeserialize, Discriminator},
|
||||
};
|
||||
|
||||
/// Initialize sets up the Ore program. Its responsibilities include:
|
||||
/// 1. Initialize the 8 bus accounts.
|
||||
|
||||
@@ -4,9 +4,9 @@ use drillx::Solution;
|
||||
use ore_api::{
|
||||
consts::*,
|
||||
error::OreError,
|
||||
event::MineEvent,
|
||||
instruction::{MineArgs, OreInstruction},
|
||||
state::{Bus, Config, Proof},
|
||||
utils::{AccountDeserialize, MineEvent},
|
||||
};
|
||||
use solana_program::program::set_return_data;
|
||||
#[allow(deprecated)]
|
||||
@@ -25,7 +25,7 @@ use solana_program::{
|
||||
sysvar::{self, instructions::load_current_index, Sysvar},
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{loaders::*, utils::AccountDeserialize};
|
||||
|
||||
/// Mine is the primary workhorse instruction of the Ore program. Its responsibilities include:
|
||||
/// 1. Calculate the hash from the provided nonce.
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use ore_api::{
|
||||
consts::*,
|
||||
instruction::OpenArgs,
|
||||
state::Proof,
|
||||
utils::{create_pda, AccountDeserialize, Discriminator},
|
||||
};
|
||||
use ore_api::{consts::*, instruction::OpenArgs, state::Proof};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
blake3::hashv,
|
||||
@@ -18,7 +13,10 @@ use solana_program::{
|
||||
sysvar::{self, Sysvar},
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{
|
||||
loaders::*,
|
||||
utils::{create_pda, AccountDeserialize, Discriminator},
|
||||
};
|
||||
|
||||
/// Register generates a new hash chain for a prospective miner. Its responsibilities include:
|
||||
/// 1. Initialize a new proof account.
|
||||
|
||||
@@ -2,7 +2,6 @@ use ore_api::{
|
||||
consts::*,
|
||||
error::OreError,
|
||||
state::{Bus, Config},
|
||||
utils::AccountDeserialize,
|
||||
};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
|
||||
@@ -10,8 +9,12 @@ use solana_program::{
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
|
||||
use crate::loaders::{
|
||||
load_bus, load_config, load_mint, load_program, load_signer, load_token_account, load_treasury,
|
||||
use crate::{
|
||||
loaders::{
|
||||
load_bus, load_config, load_mint, load_program, load_signer, load_token_account,
|
||||
load_treasury,
|
||||
},
|
||||
utils::AccountDeserialize,
|
||||
};
|
||||
|
||||
/// Reset sets up the Ore program for the next epoch. Its responsibilities include:
|
||||
@@ -165,9 +168,9 @@ pub(crate) fn calculate_new_reward_rate(current_rate: u64, epoch_rewards: u64) -
|
||||
mod tests {
|
||||
use rand::{distributions::Uniform, Rng};
|
||||
|
||||
use crate::{
|
||||
calculate_new_reward_rate, BUS_EPOCH_REWARDS, MAX_EPOCH_REWARDS, SMOOTHING_FACTOR,
|
||||
TARGET_EPOCH_REWARDS,
|
||||
use crate::calculate_new_reward_rate;
|
||||
use ore_api::consts::{
|
||||
BUS_EPOCH_REWARDS, MAX_EPOCH_REWARDS, SMOOTHING_FACTOR, TARGET_EPOCH_REWARDS,
|
||||
};
|
||||
|
||||
const FUZZ_SIZE: u64 = 10_000;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use ore_api::{consts::*, instruction::StakeArgs, state::Proof, utils::AccountDeserialize};
|
||||
use ore_api::{consts::*, instruction::StakeArgs, state::Proof};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
|
||||
program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar,
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{loaders::*, utils::AccountDeserialize};
|
||||
|
||||
/// Stake deposits Ore into a miner's proof account to earn multiplier. Its responsibilies include:
|
||||
/// 1. Transfer tokens from the miner to the treasury account.
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use ore_api::{state::Proof, utils::AccountDeserialize};
|
||||
use ore_api::state::Proof;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
use crate::loaders::*;
|
||||
use crate::{loaders::*, utils::AccountDeserialize};
|
||||
|
||||
/// Update changes the miner authority on a proof account.
|
||||
pub fn process_update<'a, 'info>(
|
||||
|
||||
0
stake/api/src/lib.rs
Normal file
0
stake/api/src/lib.rs
Normal file
0
stake/program/src/lib.rs
Normal file
0
stake/program/src/lib.rs
Normal file
18
utils/Cargo.toml
Normal file
18
utils/Cargo.toml
Normal file
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "utils"
|
||||
description = "Utils for building ORE programs"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
homepage.workspace = true
|
||||
documentation.workspace = true
|
||||
repository.workspace = true
|
||||
keywords.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "lib"]
|
||||
name = "utils"
|
||||
|
||||
[dependencies]
|
||||
bytemuck.workspace = true
|
||||
solana-program.workspace = true
|
||||
@@ -1,5 +1,3 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey, rent::Rent, sysvar::Sysvar,
|
||||
@@ -73,17 +71,8 @@ pub fn create_pda<'a, 'info>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
|
||||
pub enum AccountDiscriminator {
|
||||
Bus = 100,
|
||||
Config = 101,
|
||||
Proof = 102,
|
||||
Treasury = 103,
|
||||
}
|
||||
|
||||
pub trait Discriminator {
|
||||
fn discriminator() -> AccountDiscriminator;
|
||||
fn discriminator() -> u8; //AccountDiscriminator;
|
||||
}
|
||||
|
||||
pub trait AccountDeserialize {
|
||||
@@ -109,7 +98,7 @@ macro_rules! impl_account_from_bytes {
|
||||
fn try_from_bytes(
|
||||
data: &[u8],
|
||||
) -> Result<&Self, solana_program::program_error::ProgramError> {
|
||||
if (Self::discriminator() as u8).ne(&data[0]) {
|
||||
if Self::discriminator().ne(&data[0]) {
|
||||
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
|
||||
}
|
||||
bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
|
||||
@@ -119,7 +108,7 @@ macro_rules! impl_account_from_bytes {
|
||||
fn try_from_bytes_mut(
|
||||
data: &mut [u8],
|
||||
) -> Result<&mut Self, solana_program::program_error::ProgramError> {
|
||||
if (Self::discriminator() as u8).ne(&data[0]) {
|
||||
if Self::discriminator().ne(&data[0]) {
|
||||
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
|
||||
}
|
||||
bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
|
||||
@@ -144,13 +133,3 @@ macro_rules! impl_instruction_from_bytes {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct MineEvent {
|
||||
pub difficulty: u64,
|
||||
pub reward: u64,
|
||||
pub timing: i64,
|
||||
}
|
||||
|
||||
impl_to_bytes!(MineEvent);
|
||||
Reference in New Issue
Block a user