mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-25 07:26:50 +00:00
continued refactor
This commit is contained in:
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
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,
|
||||
};
|
||||
|
||||
/// Creates a new pda
|
||||
#[inline(always)]
|
||||
pub fn create_pda<'a, 'info>(
|
||||
target_account: &'a AccountInfo<'info>,
|
||||
owner: &Pubkey,
|
||||
space: usize,
|
||||
pda_seeds: &[&[u8]],
|
||||
system_program: &'a AccountInfo<'info>,
|
||||
payer: &'a AccountInfo<'info>,
|
||||
) -> ProgramResult {
|
||||
let rent = Rent::get()?;
|
||||
if target_account.lamports().eq(&0) {
|
||||
// If balance is zero, create account
|
||||
solana_program::program::invoke_signed(
|
||||
&solana_program::system_instruction::create_account(
|
||||
payer.key,
|
||||
target_account.key,
|
||||
rent.minimum_balance(space),
|
||||
space as u64,
|
||||
owner,
|
||||
),
|
||||
&[
|
||||
payer.clone(),
|
||||
target_account.clone(),
|
||||
system_program.clone(),
|
||||
],
|
||||
&[pda_seeds],
|
||||
)?;
|
||||
} else {
|
||||
// Otherwise, if balance is nonzero:
|
||||
|
||||
// 1) transfer sufficient lamports for rent exemption
|
||||
let rent_exempt_balance = rent
|
||||
.minimum_balance(space)
|
||||
.saturating_sub(target_account.lamports());
|
||||
if rent_exempt_balance.gt(&0) {
|
||||
solana_program::program::invoke(
|
||||
&solana_program::system_instruction::transfer(
|
||||
payer.key,
|
||||
target_account.key,
|
||||
rent_exempt_balance,
|
||||
),
|
||||
&[
|
||||
payer.clone(),
|
||||
target_account.clone(),
|
||||
system_program.clone(),
|
||||
],
|
||||
)?;
|
||||
}
|
||||
|
||||
// 2) allocate space for the account
|
||||
solana_program::program::invoke_signed(
|
||||
&solana_program::system_instruction::allocate(target_account.key, space as u64),
|
||||
&[target_account.clone(), system_program.clone()],
|
||||
&[pda_seeds],
|
||||
)?;
|
||||
|
||||
// 3) assign our program as the owner
|
||||
solana_program::program::invoke_signed(
|
||||
&solana_program::system_instruction::assign(target_account.key, owner),
|
||||
&[target_account.clone(), system_program.clone()],
|
||||
&[pda_seeds],
|
||||
)?;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
pub trait AccountDeserialize {
|
||||
fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError>;
|
||||
fn try_from_bytes_mut(data: &mut [u8]) -> Result<&mut Self, ProgramError>;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_to_bytes {
|
||||
($struct_name:ident) => {
|
||||
impl $struct_name {
|
||||
pub fn to_bytes(&self) -> &[u8] {
|
||||
bytemuck::bytes_of(self)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_account_from_bytes {
|
||||
($struct_name:ident) => {
|
||||
impl crate::utils::AccountDeserialize for $struct_name {
|
||||
fn try_from_bytes(
|
||||
data: &[u8],
|
||||
) -> Result<&Self, solana_program::program_error::ProgramError> {
|
||||
if (Self::discriminator() as u8).ne(&data[0]) {
|
||||
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
|
||||
}
|
||||
bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
|
||||
solana_program::program_error::ProgramError::InvalidAccountData,
|
||||
))
|
||||
}
|
||||
fn try_from_bytes_mut(
|
||||
data: &mut [u8],
|
||||
) -> Result<&mut Self, solana_program::program_error::ProgramError> {
|
||||
if (Self::discriminator() as u8).ne(&data[0]) {
|
||||
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
|
||||
}
|
||||
bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
|
||||
solana_program::program_error::ProgramError::InvalidAccountData,
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_instruction_from_bytes {
|
||||
($struct_name:ident) => {
|
||||
impl $struct_name {
|
||||
pub fn try_from_bytes(
|
||||
data: &[u8],
|
||||
) -> Result<&Self, solana_program::program_error::ProgramError> {
|
||||
bytemuck::try_from_bytes::<Self>(data).or(Err(
|
||||
solana_program::program_error::ProgramError::InvalidInstructionData,
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[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