250 lines
8.6 KiB
Rust
250 lines
8.6 KiB
Rust
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
use ghost_traits::hashing::GhostHasher;
|
|
use sp_core::H256;
|
|
use sp_io::hashing::{blake2_256, keccak_256, sha2_256};
|
|
use sp_runtime::{Perbill, traits::{Saturating, UniqueSaturatedInto}};
|
|
|
|
pub mod bounded_bitmap;
|
|
pub mod hash_chain;
|
|
pub mod merkle_tree;
|
|
pub mod networks;
|
|
pub mod utxo;
|
|
pub mod lcg;
|
|
|
|
pub type AuthIndexU8 = u8;
|
|
pub type AuthIndexU16 = u16;
|
|
pub type AuthIndexU32 = u32;
|
|
pub type AuthIndexU64 = u64;
|
|
pub type AuthIndexU128 = u128;
|
|
|
|
pub type BitmapChunkU8 = u8;
|
|
pub type BitmapChunkU16 = u16;
|
|
pub type BitmapChunkU32 = u32;
|
|
pub type BitmapChunkU64 = u64;
|
|
pub type BitmapChunkU128 = u128;
|
|
|
|
pub type DkgIndexU8 = u8;
|
|
pub type DkgIndexU16 = u16;
|
|
pub type DkgIndexU32 = u32;
|
|
pub type DkgIndexU64 = u64;
|
|
pub type DkgIndexU128 = u128;
|
|
|
|
pub type NetworkIdU8 = u8;
|
|
pub type NetworkIdU16 = u16;
|
|
pub type NetworkIdU32 = u32;
|
|
pub type NetworkIdU64 = u64;
|
|
pub type NetworkIdU128 = u128;
|
|
|
|
pub fn get_byzantium_quorum_threshold<I>(value: I) -> I
|
|
where
|
|
I: num_traits::PrimInt,
|
|
{
|
|
let zero = I::zero();
|
|
let one = I::one();
|
|
let three = one + one + one;
|
|
|
|
if value == zero { return zero; }
|
|
|
|
let f = (value - one) / three;
|
|
value - f
|
|
}
|
|
|
|
pub fn get_submission_probability<B>(
|
|
participants_count: usize,
|
|
participated_count: usize,
|
|
block_duration: B,
|
|
elapsed_blocks: B
|
|
) -> Perbill
|
|
where
|
|
B: UniqueSaturatedInto<u64>,
|
|
{
|
|
// P = ((participants_count - participated_count) / participants_count)
|
|
// * (elapsed_blocks / block_duration)
|
|
|
|
let participants_count_u32: u32 = participants_count.unique_saturated_into();
|
|
let participated_count_u32: u32 = participated_count.unique_saturated_into();
|
|
|
|
let block_duration_u64: u64 = block_duration.unique_saturated_into();
|
|
let elapsed_blocks_u64: u64 = elapsed_blocks.unique_saturated_into();
|
|
|
|
let participants_left = participants_count_u32.saturating_sub(participated_count_u32);
|
|
let authorities_ratio = Perbill::from_rational(participants_left, participants_count_u32);
|
|
let time_window_ratio = Perbill::from_rational(elapsed_blocks_u64, block_duration_u64);
|
|
|
|
authorities_ratio.saturating_mul(time_window_ratio)
|
|
}
|
|
|
|
macro_rules! impl_ghost_hasher {
|
|
(
|
|
struct $name:ident;
|
|
hash_type: $hash_type:ty;
|
|
hash_bytes_len: $hash_bytes_len:expr;
|
|
fn hash($data:ident) -> $hash_res:expr
|
|
) => {
|
|
pub struct $name;
|
|
|
|
impl GhostHasher for $name {
|
|
type Hash = $hash_type;
|
|
type HashBytes = [u8; $hash_bytes_len];
|
|
|
|
fn hash($data: &[u8]) -> Self::Hash {
|
|
$hash_res
|
|
}
|
|
|
|
fn hash_len() -> usize {
|
|
$hash_bytes_len
|
|
}
|
|
|
|
fn empty() -> Self::Hash {
|
|
<$hash_type>::zero()
|
|
}
|
|
|
|
fn combine_hashes(left: Self::Hash, right: Self::Hash) -> Self::Hash {
|
|
let mut combined = [0u8; $hash_bytes_len * 2];
|
|
combined[..$hash_bytes_len].copy_from_slice(left.as_ref());
|
|
combined[$hash_bytes_len..].copy_from_slice(right.as_ref());
|
|
Self::hash(&combined)
|
|
}
|
|
|
|
fn combine_hashes_prefixed(p: u8, l: Self::Hash, r: Self::Hash) -> Self::Hash {
|
|
let mut combined = [p; $hash_bytes_len * 2 + 1];
|
|
combined[1..1 + $hash_bytes_len].copy_from_slice(l.as_ref());
|
|
combined[1 + $hash_bytes_len..].copy_from_slice(r.as_ref());
|
|
Self::hash(&combined)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl_ghost_hasher!(
|
|
struct SubstrateBlake2Hasher;
|
|
hash_type: H256;
|
|
hash_bytes_len: 32;
|
|
fn hash(data) -> H256::from(blake2_256(data))
|
|
);
|
|
|
|
impl_ghost_hasher!(
|
|
struct SubstrateKeccakHasher;
|
|
hash_type: H256;
|
|
hash_bytes_len: 32;
|
|
fn hash(data) -> H256::from(keccak_256(data))
|
|
);
|
|
|
|
impl_ghost_hasher!(
|
|
struct UtxoSha2Hasher;
|
|
hash_type: H256;
|
|
hash_bytes_len: 32;
|
|
fn hash(data) -> {
|
|
let first_pass: [u8; 32] = sha2_256(data);
|
|
let second_pass: [u8; 32] = sha2_256(&first_pass);
|
|
|
|
H256::from(second_pass)
|
|
}
|
|
);
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_bft_threshold_standard_paper_cases() {
|
|
// Lower boundaries (f increases here)
|
|
assert_eq!(get_byzantium_quorum_threshold(1u32), 1);
|
|
assert_eq!(get_byzantium_quorum_threshold(4u32), 3);
|
|
assert_eq!(get_byzantium_quorum_threshold(7u32), 5);
|
|
assert_eq!(get_byzantium_quorum_threshold(10u32), 7);
|
|
|
|
// Upper boundaries (max nodes for a given 'f')
|
|
assert_eq!(get_byzantium_quorum_threshold(3u32), 3);
|
|
assert_eq!(get_byzantium_quorum_threshold(6u32), 5);
|
|
assert_eq!(get_byzantium_quorum_threshold(9u32), 7);
|
|
}
|
|
|
|
#[test]
|
|
fn test_bft_threshold_zero_nodes() {
|
|
assert_eq!(get_byzantium_quorum_threshold(0u32), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_bft_threshold_integer_boundaries() {
|
|
let max_val = usize::MAX;
|
|
let expected_f = (max_val - 1) / 3;
|
|
let expected_honest = max_val - expected_f;
|
|
|
|
assert_eq!(get_byzantium_quorum_threshold(max_val), expected_honest);
|
|
}
|
|
|
|
#[test]
|
|
fn test_bft_threshold_different_types() {
|
|
assert_eq!(get_byzantium_quorum_threshold(4u8), 3u8);
|
|
assert_eq!(get_byzantium_quorum_threshold(4u64), 3u64);
|
|
assert_eq!(get_byzantium_quorum_threshold(4usize), 3usize);
|
|
}
|
|
|
|
#[test]
|
|
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_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_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_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_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));
|
|
}
|
|
}
|