scale submission probability linearly with elapsed blocks and update skip logic
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
parent
cc7721e389
commit
07b222468f
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ghost-helpers"
|
||||
version = "0.0.16"
|
||||
version = "0.0.17"
|
||||
description = "Cryptographic utility suite for custom runtimes: optimized Bitmaps, UTXO parsing, Merkle Tree proofs, and Hash Chain components."
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
@ -52,30 +52,26 @@ where
|
||||
|
||||
pub fn get_submission_probability<B>(
|
||||
participants_count: usize,
|
||||
values_count: usize,
|
||||
blocks_left: B
|
||||
participated_count: usize,
|
||||
block_until: B,
|
||||
current_block: B
|
||||
) -> Perbill
|
||||
where
|
||||
B: UniqueSaturatedInto<u32>,
|
||||
B: UniqueSaturatedInto<u64>,
|
||||
{
|
||||
// y = min(1, (1/6) + (A-C)/2x)
|
||||
let a: u32 = participants_count.unique_saturated_into();
|
||||
let c: u32 = values_count.unique_saturated_into();
|
||||
let x: u32 = blocks_left.unique_saturated_into();
|
||||
// P = ((participants_count - participated_count) / participants_count)
|
||||
// * (current_block / block_until)
|
||||
|
||||
if a == 0 { return Perbill::zero(); }
|
||||
let participants_count_u32: u32 = participants_count.unique_saturated_into();
|
||||
let participated_count_u32: u32 = participated_count.unique_saturated_into();
|
||||
let block_until_u64: u64 = block_until.unique_saturated_into();
|
||||
let current_block_u64: u64 = current_block.unique_saturated_into();
|
||||
|
||||
let part_1 = Perbill::from_percent(6);
|
||||
let part_2 = if x > 0 {
|
||||
let numerator = a.saturating_sub(c);
|
||||
let denominator = x.saturating_mul(2);
|
||||
Perbill::from_rational(numerator, denominator)
|
||||
} else {
|
||||
if c < a { Perbill::one() }
|
||||
else { Perbill::zero() }
|
||||
};
|
||||
let participants_left = participants_count_u32.saturating_sub(participated_count_u32);
|
||||
let authorities_ratio = Perbill::from_rational(participants_left, participants_count_u32);
|
||||
let blocks_ratio = Perbill::from_rational(current_block_u64, block_until_u64);
|
||||
|
||||
part_1.saturating_add(part_2)
|
||||
authorities_ratio.saturating_mul(blocks_ratio)
|
||||
}
|
||||
|
||||
macro_rules! impl_ghost_hasher {
|
||||
@ -186,36 +182,67 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_base_probability_when_plenty_of_time() {
|
||||
let prob = get_submission_probability(100, 0, 10000u32);
|
||||
assert!(prob > Perbill::from_percent(6));
|
||||
assert!(prob < Perbill::from_percent(7));
|
||||
fn test_submission_probability_handles_zero_participants() {
|
||||
assert_eq!(get_submission_probability(0, 0, 100u32, 50u32), Perbill::from_percent(50));
|
||||
assert_eq!(get_submission_probability(0, 0, 100u32, 150u32), Perbill::one())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_urgency_increases_as_blocks_shrink() {
|
||||
let prob_far = get_submission_probability(20, 10, 10u32);
|
||||
assert_eq!(prob_far, Perbill::from_percent(56));
|
||||
|
||||
let prob_close = get_submission_probability(20, 10, 2u32);
|
||||
assert_eq!(prob_close, Perbill::one());
|
||||
fn test_submission_probability_handles_deadline_edge_cases() {
|
||||
assert_eq!(get_submission_probability(10, 0, 0u32, 0u32), Perbill::one());
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 100u32), Perbill::one());
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 0u32), Perbill::zero());
|
||||
assert_eq!(get_submission_probability(10, 2, 100u32, 101u32), Perbill::from_percent(80));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deadline_reached_but_votes_missing() {
|
||||
let prob = get_submission_probability(10, 5, 0u32);
|
||||
assert_eq!(prob, Perbill::one());
|
||||
fn test_submission_probability_handles_probability_stragglers_edge_cases() {
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 0u32), Perbill::zero());
|
||||
assert_eq!(get_submission_probability(10, 10, 100u32, 0u32), Perbill::zero());
|
||||
assert_eq!(get_submission_probability(10, 15, 100u32, 0u32), Perbill::zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_votes_collected() {
|
||||
let prob = get_submission_probability(10, 10, 5u32);
|
||||
assert_eq!(prob, Perbill::from_percent(6));
|
||||
fn test_submission_probability_calculates_normal_probabilities_accurately() {
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 25u32), Perbill::from_percent(25));
|
||||
assert_eq!(get_submission_probability(10, 5, 100u32, 50u32), Perbill::from_percent(25));
|
||||
assert_eq!(get_submission_probability(4, 1, 10u32, 2u32), Perbill::from_percent(15));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_participants_edge_case() {
|
||||
let prob = get_submission_probability(0, 0, 5u32);
|
||||
assert_eq!(prob, Perbill::zero());
|
||||
fn test_submission_probability_progression_from_start_to_deadline() {
|
||||
// Tick 0: nobody participated, 0 blocks elapsed -> 0%
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 0u32), Perbill::zero());
|
||||
|
||||
// Tick 1: nobody participated, 1 block elapsed -> 1%
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 1u32), Perbill::from_percent(1));
|
||||
|
||||
// Deadline reached: nobody participated, 100/100 blocks elapsed -> 100%
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 100u32), Perbill::one());
|
||||
|
||||
// Past deadline: clamps safely at 100% (or authorities_ratio if some participated)
|
||||
assert_eq!(get_submission_probability(10, 0, 100u32, 150u32), Perbill::one());
|
||||
assert_eq!(get_submission_probability(10, 4, 100u32, 150u32), Perbill::from_percent(60));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_submission_probability_participation_saturation() {
|
||||
// Everyone already participated -> 0% regardless of block progress
|
||||
assert_eq!(get_submission_probability(10, 10, 100u32, 50u32), Perbill::zero());
|
||||
assert_eq!(get_submission_probability(10, 10, 100u32, 100u32), Perbill::zero());
|
||||
|
||||
// More participated than total (defensive saturating_sub check) -> 0%
|
||||
assert_eq!(get_submission_probability(10, 15, 100u32, 50u32), Perbill::zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_submission_probability_repeating_fractions_and_precision() {
|
||||
// 3 participants, 1 participated -> 2/3 left
|
||||
// 1 of 3 blocks elapsed -> 1/3 time
|
||||
// Expected: (2/3) * (1/3) = 2/9 = 222_222_222 parts per billion
|
||||
assert_eq!(get_submission_probability(3, 1, 3u32, 1u32), Perbill::from_parts(222_222_221));
|
||||
|
||||
// Large block window (e.g., 10,000 blocks) -> 1 block elapsed = 0.01% (100_000 ppb)
|
||||
assert_eq!(get_submission_probability(10, 0, 10_000u32, 1u32), Perbill::from_parts(100_000));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user