binomial probability distribution

This commit is contained in:
Hardhat Chad
2025-09-11 12:29:17 -07:00
parent 16ea024346
commit 80735625d8
2 changed files with 74 additions and 46 deletions

View File

@@ -45,7 +45,8 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
block_prev.slot_hash = slot_hash;
// Calculate the block reward.
let block_reward = calculate_block_reward(&block_prev.slot_hash);
let limit = block_prev.reward.to_le_bytes()[0] as u64;
let block_reward = calculate_block_reward(&block_prev.slot_hash, limit);
// Limit the block reward to supply cap.
let max_reward = MAX_SUPPLY.saturating_sub(ore_mint.supply());
@@ -65,20 +66,18 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
&[TREASURY],
)?;
// Transfer any remaining ORE in market liquidity vault to the treasury as additional block reward.
let liq_amount = vault.amount();
block_prev.reward += liq_amount;
transfer_signed(
market_info,
// Burn any remaining ORE in market liquidity vault.
let burn_amount = vault.amount();
burn_signed(
vault_info,
treasury_tokens_info,
mint_info,
market_info,
token_program,
liq_amount,
burn_amount,
&[MARKET],
)?;
sol_log(&format!("Block reward: {:?}", block_prev.reward));
sol_log(&format!("Liq amount: {:?}", liq_amount));
sol_log(&format!("Burn amount: {:?}", burn_amount));
sol_log(&format!("Mint amount: {:?}", block_reward));
}
}
@@ -113,6 +112,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
block_next.start_at = clock.unix_timestamp;
block_next.start_slot = clock.slot;
block_next.end_slot = clock.slot + config.block_duration;
block_next.reward = u64::from_le_bytes([5, 0, 0, 0, 0, 0, 0, 0]);
// Mint tokens to the boost reserve.
mint_to_signed(
@@ -141,7 +141,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
Ok(())
}
fn get_slot_hash(
pub fn get_slot_hash(
slot: u64,
slot_hashes_sysvar: &AccountInfo<'_>,
) -> Result<[u8; 32], ProgramError> {
@@ -156,40 +156,15 @@ fn get_slot_hash(
Ok(slot_hash)
}
fn calculate_block_reward(slot_hash: &[u8]) -> u64 {
let block_distribution = [
(1u64, 737869762948382064), // 4%
(2u64, 1641760222560150093), // 4.9%
(3u64, 2564097426245627674), // 5%
(4u64, 3486434629931105255), // 5%
(5u64, 4408771833616582835), // 5%
(6u64, 5331109037302060416), // 5%
(7u64, 6253446240987537997), // 5%
(8u64, 7175783444673015578), // 5%
(9u64, 8098120648358493158), // 5%
(10u64, 9020457852043970739), // 5%
(11u64, 9942795055729448320), // 5%
(12u64, 10865132259414925901), // 5%
(13u64, 11787469463100403481), // 5%
(14u64, 12709806666785881062), // 5%
(15u64, 13632143870471358643), // 5%
(16u64, 14554481074156836224), // 5%
(17u64, 15476818277842313804), // 5%
(18u64, 16399155481527791385), // 5%
(19u64, 17321492685213268966), // 5%
(20u64, 18243829888898746547), // 5%
(100u64, 18428297329635842063), // 1%
(1000u64, u64::MAX), // 0.1%
];
fn calculate_block_reward(slot_hash: &[u8], limit: u64) -> u64 {
// Use slot hash to generate a random u64
let r1 = u64::from_le_bytes(slot_hash[0..8].try_into().unwrap());
let r2 = u64::from_le_bytes(slot_hash[8..16].try_into().unwrap());
let r3 = u64::from_le_bytes(slot_hash[16..24].try_into().unwrap());
let r4 = u64::from_le_bytes(slot_hash[24..32].try_into().unwrap());
let r = r1 ^ r2 ^ r3 ^ r4;
for (k, v) in block_distribution.iter() {
if r <= *v {
return *k * ONE_ORE;
}
}
0
// Use modulo to get a number between 0 and (limit-1), then add 1
let k = (r % limit) + 1;
k * ONE_ORE
}

View File

@@ -1,7 +1,8 @@
use ore_api::prelude::*;
use solana_program::slot_hashes;
use steel::*;
use crate::whitelist::AUTHORIZED_ACCOUNTS;
use crate::{reset::get_slot_hash, whitelist::AUTHORIZED_ACCOUNTS};
/// Swap in a hashpower market.
pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
@@ -13,7 +14,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Load accounts.
let clock = Clock::get()?;
let [signer_info, block_info, config_info, fee_collector_info, market_info, miner_info, mint_info, tokens_info, vault_info, system_program, token_program, associated_token_program, ore_program] =
let [signer_info, block_info, config_info, fee_collector_info, market_info, miner_info, mint_info, tokens_info, vault_info, system_program, token_program, associated_token_program, ore_program, slot_hashes_sysvar] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
@@ -48,6 +49,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
token_program.is_program(&spl_token::ID)?;
associated_token_program.is_program(&spl_associated_token_account::ID)?;
ore_program.is_program(&ore_api::ID)?;
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
// Load miner.
let miner = if miner_info.data_is_empty() {
@@ -155,8 +157,21 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
market.check_quote_vault(&vault)?;
// Update block reward.
let block_reward = vault.amount();
block.reward = block_reward;
// Use first byte for limit on current probability disribution.
// Use second byte for steps taken so far.
let clock = Clock::get()?;
let reward_bytes = block.reward.to_le_bytes();
let limit = reward_bytes[0];
let steps = reward_bytes[1];
let (limit, steps) = update_block_reward(
limit as u64,
steps as u64,
slot_hashes_sysvar,
block.start_slot,
clock.slot,
block.end_slot,
);
block.reward = u64::from_le_bytes([limit, steps, 0, 0, 0, 0, 0, 0]);
// Update swap event hashpower.
swap_event.miner_hashpower = miner.hashpower;
@@ -171,6 +186,44 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
Ok(())
}
fn update_block_reward(
mut limit: u64,
steps: u64,
slot_hash_sysvar: &AccountInfo<'_>,
start_slot: u64,
current_slot: u64,
end_slot: u64,
) -> (u8, u8) {
// Calculate how many steps should be taken.
let d = end_slot.saturating_sub(start_slot).saturating_div(10);
let target_steps = current_slot.saturating_sub(start_slot).saturating_div(d);
if target_steps <= steps {
return (limit as u8, steps as u8);
}
// Calculate new limit on probability distribution.
for i in (steps + 1)..target_steps {
let sample_slot = start_slot + (i * d);
if let Ok(slot_hash) = get_slot_hash(sample_slot, slot_hash_sysvar) {
// Use slot hash to generate a random u64
let r1 = u64::from_le_bytes(slot_hash[0..8].try_into().unwrap());
let r2 = u64::from_le_bytes(slot_hash[8..16].try_into().unwrap());
let r3 = u64::from_le_bytes(slot_hash[16..24].try_into().unwrap());
let r4 = u64::from_le_bytes(slot_hash[24..32].try_into().unwrap());
let r = r1 ^ r2 ^ r3 ^ r4;
// Use random number to get a 30% chance (3/10)
// Since r is random u64, checking if r <= (u64::MAX * 3/10)
let threshold = u64::MAX / 10 * 3;
if r <= threshold {
limit += 5;
}
}
}
(limit as u8, target_steps as u8)
}
fn calculate_sniper_fee(block: &Block, clock: &Clock, config: &Config) -> u64 {
let elapsed_slots = clock.slot.saturating_sub(block.start_slot);
if elapsed_slots >= config.sniper_fee_duration {