flush out

This commit is contained in:
Hardhat Chad
2025-07-16 17:21:20 -07:00
parent 39ff6f51cd
commit acbb1be65f
12 changed files with 144 additions and 176 deletions

View File

@@ -9,30 +9,21 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
let id = u64::from_le_bytes(args.id);
// Load accounts.
let clock = Clock::get()?;
let [signer_info, block_info, system_program, ore_program] = accounts else {
let [signer_info, block_info, market_info, system_program, ore_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
block_info
.is_empty()?
.is_writable()?
.has_seeds(&[BLOCK, &id.to_le_bytes()], &ore_api::ID)?;
.is_empty()? // Account has not been initialized
.is_writable()? // Account is writable
.has_seeds(&[BLOCK, &id.to_le_bytes()], &ore_api::ID)?; // Account has correct seeds
market_info
.as_account::<Market>(&ore_api::ID)?
.assert(|m| m.block_id < id)?; // Only allow opening blocks in forward bias
system_program.is_program(&system_program::ID)?;
ore_program.is_program(&ore_api::ID)?;
// TODO
// Error out if start slot is within the current period.
let start_slot = id * 1500;
let current_block = clock.slot / 1500;
let current_period_start = current_block * 1500;
let current_period_end = current_period_start + 1500;
if start_slot < current_period_end {
return Err(ProgramError::InvalidArgument);
}
// Initialize block.
// Create block account.
create_program_account::<Block>(
block_info,
system_program,
@@ -43,7 +34,12 @@ 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 = ONE_ORE * generate_lode(block.id) as u64;
block.reward = calculate_reward(block.id);
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.total_hashpower = 0;
// Emit event.
@@ -66,7 +62,7 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
Ok(())
}
fn generate_lode(block_id: u64) -> u8 {
fn calculate_reward(block_id: u64) -> u64 {
// Generate noise.
let noise_seed = block_id.to_le_bytes();
let noise = hash(&noise_seed);
@@ -75,20 +71,18 @@ fn generate_lode(block_id: u64) -> u8 {
let byte_value = noise[0];
// Map to 1-10 using integer division
let reward = (byte_value / 25) + 1;
let n = (byte_value / 25) + 1;
// Ensure the value doesn't exceed 10
if reward > 10 {
10
} else {
reward
}
let n = n.min(10);
n as u64 * ONE_ORE
}
#[test]
fn test_lode_rewards() {
for i in 0u64..1000 {
let lode_reward = ONE_ORE * generate_lode(i) as u64;
let lode_reward = ONE_ORE * calculate_reward(i) as u64;
let target_block_reward = ONE_ORE * 10;
let expected_hashes_per_block = HASHPOWER_LIQUIDITY / 2;
let expected_qualifying_hashes =