diff --git a/program/src/reset.rs b/program/src/reset.rs index e866fda..1a0479e 100644 --- a/program/src/reset.rs +++ b/program/src/reset.rs @@ -28,12 +28,14 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?; // Sample slot hash. - let (winning_square, square_prospects) = + let mut r = 0; + let (winning_square, square_deployed) = if let Ok(slot_hash) = get_slot_hash(board.end_slot, slot_hashes_sysvar) { board.slot_hash = slot_hash; - let winning_square = get_winning_square(&slot_hash); - let square_prospects = board.deployed[winning_square]; - (winning_square, square_prospects) + r = rng(&slot_hash); + let winning_square = get_winning_square(r); + let square_deployed = board.deployed[winning_square]; + (winning_square, square_deployed) } else { // Cannot get slot hash. No one wins. board.slot_hash = [u8::MAX; 32]; @@ -41,7 +43,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul }; // No one won. Vault all deployed. - if square_prospects == 0 { + if square_deployed == 0 { // Update board. board.total_vaulted = board.total_deployed; treasury.balance += board.total_deployed; @@ -68,7 +70,6 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul // Do SOL transfers. board_info.send(board.total_deployed, &treasury_info); - return Ok(()); } @@ -87,20 +88,20 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul treasury.balance += vault_amount; // Record miner rewards. - let mut top_miner = None; - let mut top_miner_prospects = 0; + let mut miner_deployments = [0; 16]; let mut rewards_sol = [0; 16]; let mut checksum = 0; for (i, miner_info) in miner_accounts.iter().enumerate() { let miner = miner_info .as_account_mut::(&ore_api::ID)? .assert_mut(|m| m.round_id == board.id)?; - let miner_prospects = miner.deployed[winning_square]; - let rewards = miner_prospects + (winnings * miner_prospects / square_prospects); // Winners get their own prospect back plus their share of the winnings. - checksum += miner_prospects; + let miner_deployed = miner.deployed[winning_square]; + let rewards = miner_deployed + (winnings * miner_deployed / square_deployed); // Winners get their own prospect back plus their share of the winnings. + checksum += miner_deployed; miner.rewards_sol += rewards; miner.lifetime_rewards_sol += rewards; rewards_sol[i] = rewards; + miner_deployments[i] = miner_deployed; // Check if miner was provided in correct order. if miner.authority != square.miners[winning_square][i] { @@ -109,23 +110,17 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul ProgramError::InvalidAccountData, )); } - - // Find the top winner. - if miner_prospects > top_miner_prospects { - top_miner_prospects = miner_prospects; - top_miner = Some(i); - } } // Verify checksum. - if checksum != square_prospects { + if checksum != square_deployed { // This can only happen if the caller didn't provide full set of winning miners. return Err(trace("Invalid checksum", ProgramError::InvalidAccountData)); } - // Payout reward to top miner. + // Payout 1 ORE to the winning miner, proportional to their deployed amount on the winning square. let mut mint_amount = 0; - if let Some(i) = top_miner { + if let Some(i) = get_winning_miner(r, square_deployed, miner_deployments) { let miner = miner_accounts[i].as_account_mut::(&ore_api::ID)?; mint_amount = ONE_ORE.min(MAX_SUPPLY - mint.supply()); if mint_amount > 0 { @@ -212,18 +207,41 @@ pub fn get_slot_hash( Ok(slot_hash) } -fn get_winning_square(slot_hash: &[u8]) -> usize { - // Use slot hash to generate a random u64 +// Use slot hash to generate a random u64 +fn rng(slot_hash: &[u8]) -> 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; + r +} - // Returns a value in the range [0, 24] inclusive +fn get_winning_square(r: u64) -> usize { + // Returns a value in the range [0, 25) (r % 25) as usize } +/// Randomly selects a winning miner based on their proportional deposits. +/// Uses the slot hash as a source of randomness to select a value between 0 and total_deployed. +/// Each miner's chance of winning is proportional to their deposit amount relative to total deposits. +/// Returns the index of the winning miner, or None if no miners have deposits. +fn get_winning_miner(r: u64, total_deployed: u64, miner_deployed: [u64; 16]) -> Option { + // Returns a value in the range [0, total_deployed) + let x = (r % total_deployed) as u64; + + // Find the miner that deposited the winning amount. + let mut cumulative_deployed = 0; + for (i, deployed) in miner_deployed.iter().enumerate() { + cumulative_deployed += *deployed; + if x < cumulative_deployed { + return Some(i); + } + } + + return None; +} + #[cfg(test)] mod tests { #[test]