mine event

This commit is contained in:
Hardhat Chad
2025-09-09 18:05:48 -07:00
parent 40fbf13361
commit b85fd9e240
2 changed files with 46 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ use crate::state::SwapDirection;
pub enum OreEvent { pub enum OreEvent {
Reset = 0, Reset = 0,
Swap = 1, Swap = 1,
Mine = 2,
} }
#[repr(C)] #[repr(C)]
@@ -81,6 +82,31 @@ pub struct SwapEvent {
pub ts: i64, pub ts: i64,
} }
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct MineEvent {
/// The event discriminator.
pub disc: u64,
/// The authority of the mine.
pub authority: Pubkey,
/// The block that was mined.
pub block_id: u64,
/// The nonce that was mined.
pub nonce: u64,
/// The total hashpower the miner had.
pub hashpower: u64,
/// Whether or not the miner is the new best.
pub is_best: u64,
/// The timestamp of the event.
pub ts: i64,
}
impl SwapEvent { impl SwapEvent {
pub fn direction(&self) -> SwapDirection { pub fn direction(&self) -> SwapDirection {
SwapDirection::try_from(self.direction as u8).unwrap() SwapDirection::try_from(self.direction as u8).unwrap()
@@ -89,3 +115,4 @@ impl SwapEvent {
event!(ResetEvent); event!(ResetEvent);
event!(SwapEvent); event!(SwapEvent);
event!(MineEvent);

View File

@@ -13,7 +13,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Load accounts. // Load accounts.
let clock = Clock::get()?; let clock = Clock::get()?;
let [signer_info, block_info, miner_info] = accounts else { let [signer_info, block_info, market_info, miner_info, ore_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys); return Err(ProgramError::NotEnoughAccountKeys);
}; };
signer_info.is_signer()?; signer_info.is_signer()?;
@@ -22,11 +22,13 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
.assert_mut(|b| clock.slot >= b.end_slot)? // Block has stopped trading .assert_mut(|b| clock.slot >= b.end_slot)? // Block has stopped trading
.assert_mut(|b| clock.slot < b.end_slot + MINING_WINDOW)? // Give 1500 slots to submit hashes .assert_mut(|b| clock.slot < b.end_slot + MINING_WINDOW)? // Give 1500 slots to submit hashes
.assert_mut(|b| b.slot_hash != [0; 32])?; // Slot hash is set .assert_mut(|b| b.slot_hash != [0; 32])?; // Slot hash is set
market_info.as_account::<Market>(&ore_api::ID)?;
let miner = miner_info let miner = miner_info
.as_account_mut::<Miner>(&ore_api::ID)? .as_account_mut::<Miner>(&ore_api::ID)?
.assert_mut(|m| m.authority == *signer_info.key)? // Account belongs to authority .assert_mut(|m| m.authority == *signer_info.key)? // Account belongs to authority
.assert_mut(|m| m.block_id == block.id)? // Only allow miner to submit hashes for their current block .assert_mut(|m| m.block_id == block.id)? // Only allow miner to submit hashes for their current block
.assert_mut(|m| m.hashpower > nonce)?; // Only allow miner to submit nonces for their hashpower range .assert_mut(|m| m.hashpower > nonce)?; // Only allow miner to submit nonces for their hashpower range
ore_program.is_program(&ore_api::ID)?;
// Check if the signer is authorized. // Check if the signer is authorized.
if !AUTHORIZED_ACCOUNTS.contains(signer_info.key) { if !AUTHORIZED_ACCOUNTS.contains(signer_info.key) {
@@ -54,6 +56,22 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
block.best_hash_miner = miner.authority; block.best_hash_miner = miner.authority;
} }
// Emit event.
program_log(
&[market_info.clone(), ore_program.clone()],
&MineEvent {
disc: 2,
authority: *signer_info.key,
block_id: block.id,
nonce,
hashpower: miner.hashpower,
is_best: (block.best_hash_miner == miner.authority) as u64,
ts: clock.unix_timestamp,
}
.to_bytes(),
)?;
// Only allow miners to submit 1 hash per block. // Only allow miners to submit 1 hash per block.
miner.hashpower = 0; miner.hashpower = 0;