mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-15 07:26:45 +00:00
optimization
This commit is contained in:
@@ -10,7 +10,7 @@ use solana_program::{
|
||||
use crate::{
|
||||
loaders::*,
|
||||
state::{Bus, Treasury},
|
||||
BUS, BUS_COUNT, BUS_EPOCH_REWARDS, EPOCH_DURATION, MAX_EPOCH_REWARDS, SMOOTHING_FACTOR,
|
||||
BUS_COUNT, BUS_EPOCH_REWARDS, EPOCH_DURATION, MAX_EPOCH_REWARDS, SMOOTHING_FACTOR,
|
||||
TARGET_EPOCH_REWARDS, TREASURY,
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use solana_program::{
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
|
||||
use crate::{instruction::*, BUS, INITIAL_DIFFICULTY, MINT_ADDRESS};
|
||||
use crate::{instruction::*, BUS, INITIAL_DIFFICULTY, MINT_ADDRESS, TREASURY_ADDRESS};
|
||||
use crate::{
|
||||
loaders::*,
|
||||
state::{Bus, Treasury},
|
||||
@@ -77,10 +77,13 @@ pub fn process_initialize<'a, 'info>(
|
||||
}
|
||||
|
||||
// Account 11: Treasury
|
||||
let treasury_account_info = load_uninitialized_pda(
|
||||
let treasury_info = load_uninitialized_pda(
|
||||
next_account_info(accounts_iter)?,
|
||||
&[TREASURY, &[args.treasury_bump]],
|
||||
)?;
|
||||
if !treasury_info.key.eq(&TREASURY_ADDRESS) {
|
||||
return Err(ProgramError::InvalidSeeds);
|
||||
}
|
||||
|
||||
// Account 12: Treasury tokens
|
||||
let treasury_tokens = load_uninitialized_account(next_account_info(accounts_iter)?)?;
|
||||
@@ -132,14 +135,14 @@ pub fn process_initialize<'a, 'info>(
|
||||
|
||||
// Initialize treasury
|
||||
create_pda(
|
||||
treasury_account_info,
|
||||
treasury_info,
|
||||
&crate::id(),
|
||||
size_of::<Treasury>(),
|
||||
&[TREASURY, &[args.treasury_bump]],
|
||||
system_program,
|
||||
signer,
|
||||
)?;
|
||||
let mut treasury_data = treasury_account_info.data.borrow_mut();
|
||||
let mut treasury_data = treasury_info.data.borrow_mut();
|
||||
let mut treasury = bytemuck::try_from_bytes_mut::<Treasury>(&mut treasury_data).unwrap();
|
||||
treasury.bump = args.treasury_bump as u64;
|
||||
treasury.admin = *signer.key;
|
||||
@@ -162,14 +165,14 @@ pub fn process_initialize<'a, 'info>(
|
||||
&spl_token::instruction::initialize_mint(
|
||||
&spl_token::id(),
|
||||
mint.key,
|
||||
treasury_account_info.key,
|
||||
treasury_info.key,
|
||||
None,
|
||||
TOKEN_DECIMALS,
|
||||
)?,
|
||||
&[
|
||||
token_program.clone(),
|
||||
mint.clone(),
|
||||
treasury_account_info.clone(),
|
||||
treasury_info.clone(),
|
||||
rent_sysvar.clone(),
|
||||
],
|
||||
&[&[MINT, &[args.mint_bump]]],
|
||||
@@ -179,7 +182,7 @@ pub fn process_initialize<'a, 'info>(
|
||||
solana_program::program::invoke(
|
||||
&spl_associated_token_account::instruction::create_associated_token_account(
|
||||
signer.key,
|
||||
treasury_account_info.key,
|
||||
treasury_info.key,
|
||||
mint.key,
|
||||
&spl_token::id(),
|
||||
),
|
||||
@@ -187,7 +190,7 @@ pub fn process_initialize<'a, 'info>(
|
||||
associated_token_program.clone(),
|
||||
signer.clone(),
|
||||
treasury_tokens.clone(),
|
||||
treasury_account_info.clone(),
|
||||
treasury_info.clone(),
|
||||
mint.clone(),
|
||||
system_program.clone(),
|
||||
token_program.clone(),
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
account_info::{next_account_info, AccountInfo},
|
||||
clock::Clock,
|
||||
entrypoint::ProgramResult,
|
||||
keccak::{hashv, Hash},
|
||||
keccak::{hashv, Hash as KeccakHash},
|
||||
program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
slot_hashes::SlotHash,
|
||||
sysvar::{self, Sysvar},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
instruction::MineArgs,
|
||||
loaders::*,
|
||||
state::{Bus, Proof, Treasury},
|
||||
EPOCH_DURATION,
|
||||
};
|
||||
|
||||
pub fn process_mine<'a, 'info>(
|
||||
@@ -11,16 +23,83 @@ pub fn process_mine<'a, 'info>(
|
||||
accounts: &'a [AccountInfo<'info>],
|
||||
data: &[u8],
|
||||
) -> ProgramResult {
|
||||
// TODO
|
||||
// let accounts_iter = &mut accounts.iter();
|
||||
// let args =
|
||||
// bytemuck::try_from_bytes::<MineArgs>(data).or(Err(ProgramError::InvalidInstructionData))?;
|
||||
|
||||
// let [signer, bus_info, proof_info, treasury_info, slot_hashes_info] = accounts else {
|
||||
// return Err(ProgramError::NotEnoughAccountKeys);
|
||||
// };
|
||||
// let _ = load_signer(signer)?;
|
||||
// let _ = load_bus(bus_info)?;
|
||||
// let _ = load_proof(proof_info, signer.key)?;
|
||||
// let _ = load_treasury(treasury_info)?;
|
||||
// let _ = load_account(slot_hashes_info, sysvar::slot_hashes::id())?;
|
||||
|
||||
// Account 1: Signer
|
||||
// let signer = load_signer(next_account_info(accounts_iter)?)?;
|
||||
|
||||
// Account 2: Bus
|
||||
// let bus_info = load_bus(next_account_info(accounts_iter)?)?;
|
||||
|
||||
// Account 3: Proof
|
||||
// let proof_info = load_proof(next_account_info(accounts_iter)?, signer.key)?;
|
||||
|
||||
// Account 4: Treasury
|
||||
// let treasury_info = load_treasury(next_account_info(accounts_iter)?)?;
|
||||
|
||||
// Account 5: Slot hashes svsvar
|
||||
// let slot_hashes_info =
|
||||
// load_account(next_account_info(accounts_iter)?, sysvar::slot_hashes::id())?;
|
||||
|
||||
// Validate epoch is active
|
||||
// let clock = Clock::get().unwrap();
|
||||
// let treasury_data = treasury_info.data.borrow();
|
||||
// let treasury = bytemuck::try_from_bytes::<Treasury>(&treasury_data).unwrap();
|
||||
// let epoch_end_at = treasury.epoch_start_at.saturating_add(EPOCH_DURATION);
|
||||
// if !clock.unix_timestamp.lt(&epoch_end_at) {
|
||||
// return Err(ProgramError::Custom(1));
|
||||
// }
|
||||
|
||||
// Validate provided hash
|
||||
// let mut proof_data = proof_info.data.borrow_mut();
|
||||
// let mut proof = bytemuck::try_from_bytes_mut::<Proof>(&mut proof_data).unwrap();
|
||||
// validate_hash(
|
||||
// proof.hash.into(),
|
||||
// args.hash.into(),
|
||||
// *signer.key,
|
||||
// u64::from_le_bytes(args.nonce),
|
||||
// treasury.difficulty.into(),
|
||||
// )?;
|
||||
|
||||
// Update claimable rewards
|
||||
// let mut bus_data = bus_info.data.borrow_mut();
|
||||
// let mut bus = bytemuck::try_from_bytes_mut::<Bus>(&mut bus_data).unwrap();
|
||||
// if !bus.available_rewards.ge(&treasury.reward_rate) {
|
||||
// return Err(ProgramError::Custom(1));
|
||||
// }
|
||||
// bus.available_rewards = bus.available_rewards.saturating_sub(treasury.reward_rate);
|
||||
// proof.claimable_rewards = proof.claimable_rewards.saturating_add(treasury.reward_rate);
|
||||
|
||||
// Hash most recent slot hash into the next challenge to prevent pre-mining attacks
|
||||
// let slot_hash_bytes = &slot_hashes_info.data.borrow()[0..size_of::<SlotHash>()];
|
||||
// proof.hash = hashv(&[KeccakHash::from(args.hash).as_ref(), slot_hash_bytes]).into();
|
||||
|
||||
// Update lifetime stats
|
||||
// proof.total_hashes = proof.total_hashes.saturating_add(1);
|
||||
// proof.total_rewards = proof.total_rewards.saturating_add(1);
|
||||
|
||||
// TODO Log?
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn validate_hash(
|
||||
current_hash: Hash,
|
||||
hash: Hash,
|
||||
current_hash: KeccakHash,
|
||||
hash: KeccakHash,
|
||||
signer: Pubkey,
|
||||
nonce: u64,
|
||||
difficulty: Hash,
|
||||
difficulty: KeccakHash,
|
||||
) -> Result<(), ProgramError> {
|
||||
// Validate hash correctness.
|
||||
let hash_ = hashv(&[
|
||||
|
||||
@@ -1,10 +1,54 @@
|
||||
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};
|
||||
use std::mem::size_of;
|
||||
|
||||
use solana_program::{
|
||||
account_info::{next_account_info, AccountInfo},
|
||||
entrypoint::ProgramResult,
|
||||
keccak::hashv,
|
||||
program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
system_program,
|
||||
};
|
||||
|
||||
use crate::{instruction::ProofArgs, loaders::*, state::Proof, utils::create_pda, PROOF};
|
||||
|
||||
pub fn process_proof<'a, 'info>(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &'a [AccountInfo<'info>],
|
||||
data: &[u8],
|
||||
) -> ProgramResult {
|
||||
// TODO
|
||||
let accounts_iter = &mut accounts.iter();
|
||||
let args = bytemuck::try_from_bytes::<ProofArgs>(data)
|
||||
.or(Err(ProgramError::InvalidInstructionData))?;
|
||||
|
||||
// Account 1: Signer
|
||||
let signer = load_signer(next_account_info(accounts_iter)?)?;
|
||||
|
||||
// Account 2: Proof
|
||||
let proof_info = load_uninitialized_pda(
|
||||
next_account_info(accounts_iter)?,
|
||||
&[PROOF, signer.key.as_ref(), &[args.bump]],
|
||||
)?;
|
||||
|
||||
// Account 3: System program
|
||||
let system_program = load_account(next_account_info(accounts_iter)?, system_program::id())?;
|
||||
|
||||
// Initialize proof
|
||||
create_pda(
|
||||
proof_info,
|
||||
&crate::id(),
|
||||
size_of::<Proof>(),
|
||||
&[PROOF, signer.key.as_ref(), &[args.bump]],
|
||||
system_program,
|
||||
signer,
|
||||
)?;
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let mut proof = bytemuck::try_from_bytes_mut::<Proof>(&mut proof_data).unwrap();
|
||||
proof.bump = args.bump as u64;
|
||||
proof.authority = *signer.key;
|
||||
proof.claimable_rewards = 0;
|
||||
proof.hash = hashv(&[&signer.key.to_bytes()]).into();
|
||||
proof.total_hashes = 0;
|
||||
proof.total_rewards = 0;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user