mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-15 23:16:46 +00:00
mining
This commit is contained in:
@@ -5,7 +5,7 @@ use steel::*;
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, miner_info, miner_rewards_info, mint_info, opener_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program, ore_program] =
|
||||
let [signer_info, block_info, miner_info, miner_rewards_info, mint_info, opener_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -23,7 +23,6 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
associated_token_program.is_program(&spl_associated_token_account::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Load miner rewards.
|
||||
if miner_rewards_info.data_is_empty() {
|
||||
@@ -65,19 +64,5 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
// Close block.
|
||||
block_info.close(opener_info)?;
|
||||
|
||||
// Emit event.
|
||||
// program_log(
|
||||
// block.id,
|
||||
// &[block_info.clone(), ore_program.clone()],
|
||||
// &CloseEvent {
|
||||
// authority: *signer_info.key,
|
||||
// id: block.id,
|
||||
// burned_base: base_burned,
|
||||
// burned_quote: quote_burned,
|
||||
// ts: clock.unix_timestamp,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// )?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2,20 +2,16 @@ use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// No-op, use instruction data for logging w/o truncation.
|
||||
pub fn process_log(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Load data
|
||||
let block_id_bytes = data[..8].try_into().unwrap();
|
||||
let block_id = u64::from_le_bytes(block_id_bytes);
|
||||
|
||||
pub fn process_log(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let [signer_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info
|
||||
.is_signer()?
|
||||
.has_seeds(&[BLOCK, &block_id.to_le_bytes()], &ore_api::ID)?;
|
||||
.as_account::<Market>(&ore_api::ID)?;
|
||||
|
||||
// For data integrity, only a block can log messages.
|
||||
// For data integrity, only the market can log messages.
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, authority_info, block_info, miner_info, ore_program] = accounts else {
|
||||
let [signer_info, authority_info, block_info, miner_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
@@ -23,24 +23,16 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *authority_info.key)? // Account belongs to authority
|
||||
.assert_mut(|m| m.block_id <= block.id)?; // Only allow miner to submit hashes in forward bias
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// 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(&miner.authority.to_bytes());
|
||||
args[72..].copy_from_slice(&miner.seed);
|
||||
miner.hash = hash(&args);
|
||||
miner.block_id = block.id;
|
||||
}
|
||||
.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
|
||||
|
||||
// Generate secure hash with provided nonce.
|
||||
let mut seed = [0u8; 40];
|
||||
seed[..8].copy_from_slice(&miner.hash.as_ref());
|
||||
seed[8..40].copy_from_slice(&nonce.to_le_bytes());
|
||||
let mut seed = [0u8; 112];
|
||||
seed[..8].copy_from_slice(&block.id.to_le_bytes());
|
||||
seed[8..40].copy_from_slice(&block.slot_hash);
|
||||
seed[40..72].copy_from_slice(&miner.authority.to_bytes());
|
||||
seed[72..].copy_from_slice(&miner.seed);
|
||||
seed[104..].copy_from_slice(&nonce.to_le_bytes());
|
||||
let h = hash(&seed);
|
||||
|
||||
// If hash is best hash, update best hash.
|
||||
@@ -49,21 +41,5 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
block.best_hash_miner = miner.authority;
|
||||
}
|
||||
|
||||
// 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: 0,
|
||||
// ts: clock.unix_timestamp,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// )?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -33,30 +33,13 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
block.id = id;
|
||||
block.opener = *signer_info.key;
|
||||
block.reward = 0;
|
||||
block.best_hash = [0; 32];
|
||||
block.best_hash_miner = Pubkey::default();
|
||||
block.start_slot = u64::MAX; // Set by reset
|
||||
block.end_slot = u64::MAX; // Set by reset
|
||||
block.slot_hash = [0; 32]; // Set by mine
|
||||
block.reward = 0; // Set by reset instruction
|
||||
block.start_slot = u64::MAX; // Set by reset instruction
|
||||
block.end_slot = u64::MAX; // Set by reset instruction
|
||||
block.slot_hash = [0; 32]; // Set by reset instruction
|
||||
block.total_hashpower = 0;
|
||||
|
||||
// Emit event.
|
||||
// program_log(
|
||||
// id,
|
||||
// &[block_info.clone(), ore_program.clone()],
|
||||
// &OpenEvent {
|
||||
// disc: OreEvent::Open as u64,
|
||||
// id,
|
||||
// start_slot,
|
||||
// signer: *signer_info.key,
|
||||
// reward_config: block.reward,
|
||||
// // liquidity_base: market.base.liquidity() as u64,
|
||||
// // liquidity_quote: market.quote.liquidity() as u64,
|
||||
// ts: clock.unix_timestamp,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// )?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -76,12 +76,19 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
swap_event.authority = *signer_info.key;
|
||||
swap_event.block_id = block.id;
|
||||
|
||||
// Reset miner.
|
||||
if miner.block_id != block.id {
|
||||
miner.block_id = block.id;
|
||||
miner.hashpower = 0;
|
||||
}
|
||||
|
||||
// Transfer tokens
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
// Update hashpower.
|
||||
block.total_hashpower += swap_event.base_to_transfer;
|
||||
miner.hashpower += swap_event.base_to_transfer;
|
||||
miner.total_hashpower += swap_event.base_to_transfer;
|
||||
block.total_hashpower += swap_event.base_to_transfer;
|
||||
|
||||
// Transfer ORE from signer to market.
|
||||
transfer(
|
||||
@@ -94,8 +101,9 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
// Update hashpower.
|
||||
block.total_hashpower -= swap_event.base_to_transfer;
|
||||
miner.hashpower -= swap_event.base_to_transfer;
|
||||
miner.total_hashpower -= swap_event.base_to_transfer;
|
||||
block.total_hashpower -= swap_event.base_to_transfer;
|
||||
|
||||
// Trasnfer ORE from market to signer.
|
||||
transfer_signed(
|
||||
|
||||
Reference in New Issue
Block a user