This commit is contained in:
Hardhat Chad
2025-07-16 16:24:41 -07:00
parent 9470c573f3
commit 084757cc8d
23 changed files with 262 additions and 1430 deletions

View File

@@ -1,6 +1,6 @@
use ore_api::{prelude::*, sdk::program_log};
use solana_nostd_keccak::hash;
use solana_program::slot_hashes::SlotHashes;
use solana_program::{log::sol_log, slot_hashes::SlotHashes};
use steel::*;
/// Mine a block.
@@ -11,34 +11,23 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Load accounts.
let clock = Clock::get()?;
let [signer_info, authority_info, block_info, commitment_info, market_info, miner_info, mint_hash_info, mint_ore_info, permit_info, recipient_info, treasury_info, system_program, token_program, ore_program, slot_hashes_sysvar] =
let [signer_info, authority_info, block_info, market_info, miner_info, mint_info, recipient_info, treasury_info, system_program, token_program, ore_program, slot_hashes_sysvar] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
authority_info.is_writable()?;
let block = block_info
.as_account_mut::<Block>(&ore_api::ID)?
.assert_mut(|b| clock.slot >= b.start_slot)?
.assert_mut(|b| clock.slot < b.start_slot + 1500)?;
commitment_info
.is_writable()?
.has_address(&commitment_pda(block.id).0)?
.as_token_account()?;
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
// .assert_mut(|b| clock.slot >= b.start_slot)?
// .assert_mut(|b| clock.slot < b.start_slot + 1500)?;
let market = market_info
.as_account::<Market>(&ore_api::ID)?
.assert(|m| m.id == block.id)?;
mint_hash_info.has_address(&market.base.mint)?.as_mint()?;
mint_ore_info.has_address(&MINT_ADDRESS)?.as_mint()?;
.assert(|m| m.block_id == block.id)?;
mint_info.has_address(&MINT_ADDRESS)?.as_mint()?;
let miner = miner_info
.as_account_mut::<Miner>(&ore_api::ID)?
.assert_mut(|m| m.authority == *authority_info.key)?;
let permit = permit_info
.as_account_mut::<Permit>(&ore_api::ID)?
.assert_mut(|p| p.authority == miner.authority)?
.assert_mut(|p| p.block_id == block.id)?
.assert_mut(|p| p.executor == *signer_info.key || p.executor == Pubkey::default())?;
recipient_info
.is_writable()?
.as_associated_token_account(&miner.authority, &MINT_ADDRESS)?;
@@ -48,130 +37,82 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
ore_program.is_program(&ore_api::ID)?;
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
// Reduce permit amount.
let amount = permit.commitment.min(amount);
permit.commitment -= amount;
// Pay executor fee.
// if permit.fee > 0 {
// permit_info.send(permit.fee * amount, signer_info);
// }
// Close permit account, if empty.
let permit_commitment = permit.commitment;
if permit_commitment == 0 {
permit_info.close(authority_info)?;
}
// Burn hash tokens.
burn_signed(
commitment_info,
mint_hash_info,
block_info,
token_program,
amount,
&[BLOCK, &block.id.to_le_bytes()],
)?;
// burn_signed(
// commitment_info,
// mint_hash_info,
// block_info,
// token_program,
// amount,
// &[BLOCK, &block.id.to_le_bytes()],
// )?;
// Set block slot hash.
if block.slot_hash == [0; 32] {
let slot_hashes =
bincode::deserialize::<SlotHashes>(slot_hashes_sysvar.data.borrow().as_ref()).unwrap();
let Some(slot_hash) = slot_hashes.get(&block.start_slot) else {
// If mine is not called within ~2.5 minutes of the block starting,
// then the slot hash will be unavailable and secure hashes cannot be generated.
return Ok(());
};
block.slot_hash = slot_hash.to_bytes();
}
// if block.slot_hash == [0; 32] {
// let slot_hashes =
// bincode::deserialize::<SlotHashes>(slot_hashes_sysvar.data.borrow().as_ref()).unwrap();
// let Some(slot_hash) = slot_hashes.get(&block.start_slot) else {
// // If mine is not called within ~2.5 minutes of the block starting,
// // then the slot hash will be unavailable and secure hashes cannot be generated.
// return Ok(());
// };
// block.slot_hash = slot_hash.to_bytes();
// }
// Reset miner hash if mining new block.
if miner.block_id != block.id {
let mut args = [0u8; 104];
args[..8].copy_from_slice(&block.id.to_le_bytes());
args[8..40].copy_from_slice(&block.slot_hash);
args[40..72].copy_from_slice(&permit.authority.to_bytes());
args[72..].copy_from_slice(&permit.seed);
miner.hash = hash(&args);
miner.block_id = block.id;
}
// if miner.block_id != block.id {
// let mut args = [0u8; 104];
// args[..8].copy_from_slice(&block.id.to_le_bytes());
// args[8..40].copy_from_slice(&block.slot_hash);
// args[40..72].copy_from_slice(&miner.authority.to_bytes());
// args[72..].copy_from_slice(&miner.seed);
// miner.hash = hash(&args);
// miner.block_id = block.id;
// }
// Mine and accumulate rewards.
let mut nugget_reward = 0;
for _ in 0..amount {
// Update stats
block.total_deployed += 1;
miner.total_deployed += 1;
// let mut nugget_reward = 0;
// for _ in 0..amount {
// // Update stats
// // block.total_deployed += 1;
// Generate hash.
miner.hash = hash(miner.hash.as_ref());
// // Generate hash.
// miner.hash = hash(miner.hash.as_ref());
// Score and increment rewards.
let score = difficulty(miner.hash) as u64;
if score >= block.reward.nugget_threshold {
nugget_reward += block.reward.nugget_reward;
}
// // Score and increment rewards.
// let score = difficulty(miner.hash) as u64;
// if score >= block.reward.nugget_threshold {
// nugget_reward += block.reward.nugget_reward;
// }
// If hash is best hash, update best hash.
if miner.hash < block.reward.lode_hash {
block.reward.lode_hash = miner.hash;
block.reward.lode_authority = miner.authority;
}
}
// // If hash is best hash, update best hash.
// if miner.hash < block.reward.lode_hash {
// block.reward.lode_hash = miner.hash;
// block.reward.lode_authority = miner.authority;
// }
// }
// Payout ORE.
if nugget_reward > 0 {
// Limit payout to supply cap.
let ore_mint = mint_ore_info.as_mint()?;
let max_reward = MAX_SUPPLY.saturating_sub(ore_mint.supply());
let reward_amount = nugget_reward.min(max_reward);
// Update stats.
block.total_rewards += reward_amount;
miner.total_rewards += reward_amount;
// Mint to recipient.
mint_to_signed_with_bump(
mint_ore_info,
recipient_info,
treasury_info,
token_program,
reward_amount,
&[TREASURY],
TREASURY_BUMP,
)?;
// Emit event.
program_log(
block.id,
&[block_info.clone(), ore_program.clone()],
&RewardEvent {
disc: OreEvent::Reward as u64,
amount: reward_amount,
authority: miner.authority,
block_id: block.id,
rewards_type: RewardsType::Nugget as u64,
ts: clock.unix_timestamp,
}
.to_bytes(),
)?;
}
// Log mint.
// let ore_mint = mint_ore_info.as_mint()?;
// let mint_authority = ore_mint.mint_authority();
// sol_log(format!("mint_authority: {:?}", mint_authority).as_str());
// sol_log(format!("treasury: {:?}", treasury_info.key).as_str());
// Emit event.
program_log(
block.id,
&[block_info.clone(), ore_program.clone()],
&MineEvent {
disc: OreEvent::Mine as u64,
authority: miner.authority,
block_id: block.id,
deployed: amount,
total_deployed: block.total_deployed,
remaining_commitment: permit_commitment,
ts: clock.unix_timestamp,
}
.to_bytes(),
)?;
// program_log(
// block.id,
// &[block_info.clone(), ore_program.clone()],
// &MineEvent {
// disc: OreEvent::Mine as u64,
// authority: miner.authority,
// block_id: block.id,
// deployed: amount,
// total_deployed: block.total_deployed,
// remaining_commitment: 0,
// ts: clock.unix_timestamp,
// }
// .to_bytes(),
// )?;
Ok(())
}