From ad4c712fbfd6d4bb6854829d3b92cb26284fe0e7 Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Thu, 19 Nov 2026 14:41:41 +0300 Subject: [PATCH] stagger DKG offchain submissions using adaptive probability Signed-off-by: Uncle Stinky --- pallets/exodus/Cargo.toml | 2 +- pallets/exodus/src/lib.rs | 79 +++++++++++++++++++++++++++-- pallets/exodus/src/types/success.rs | 2 + 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/pallets/exodus/Cargo.toml b/pallets/exodus/Cargo.toml index 987ff78..2fc2198 100644 --- a/pallets/exodus/Cargo.toml +++ b/pallets/exodus/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-exodus" -version = "0.1.8" +version = "0.1.9" description = "Threshold signature generation with DKG included" license.workspace = true authors.workspace = true diff --git a/pallets/exodus/src/lib.rs b/pallets/exodus/src/lib.rs index 92e5263..3a6cb9e 100644 --- a/pallets/exodus/src/lib.rs +++ b/pallets/exodus/src/lib.rs @@ -20,8 +20,8 @@ use sp_runtime::{ storage::StorageValueRef, storage_lock::{StorageLock, Time}, }, - traits::UniqueSaturatedInto, - Saturating, Perbill, + traits::{One, TrailingZeroInput, UniqueSaturatedInto}, + Saturating, Perbill, PerThing, }; use sp_std::{vec, vec::Vec, collections::btree_map::BTreeMap}; @@ -30,7 +30,8 @@ use sp_runtime::traits::BlockNumberProvider; use sp_io::hashing::blake2_256; use ghost_helpers::{ - get_byzantium_quorum_threshold, SubstrateBlake2Hasher, + get_byzantium_quorum_threshold, get_submission_probability, + SubstrateBlake2Hasher, lcg::PerfectSlotMapperU128, hash_chain::cumulative_hash, networks::{NetworkData, NetworkCurve, NetworkType}, @@ -3786,6 +3787,68 @@ impl Pallet { Ok(results) } + fn should_skip_submission( + network_curve: NetworkCurve, + current_block: BlockNumberFor, + qualification_authorities: &PendingDkgAuthorities, + ) -> bool { + let dkg_phase = qualification_authorities.get_phase(); + let dkg_index = qualification_authorities.get_dkg_index(); + + let dkg_round_period: BlockNumberFor = T::DkgRoundPeriod::get() + .unique_saturated_into(); + + let blocks_remaining = qualification_authorities + .get_release_block() + .saturating_sub(current_block); + + if blocks_remaining <= 2u32.into() { + return false; + } + + let elapsed_blocks = dkg_round_period + .saturating_sub(blocks_remaining) + .saturating_add(One::one()); + + let total_authorities = if dkg_phase == DkgPhase::Round0 { + let indexes = qualification_authorities.get_indexes(); + indexes.active_bits_count() as usize + } else { + qualification_authorities.count_ones::() + }; + + let values_count = match dkg_phase { + DkgPhase::Round0 => qualification_authorities.count_ones::(), + DkgPhase::Round1 => Round1Packages::::get(network_curve).count_ones::(), + DkgPhase::Round2 => Round2Packages::::get(network_curve).count_ones::(), + DkgPhase::Round3 => Complaints::::get(network_curve).len(), + DkgPhase::Round4 => Justifications::::get(network_curve).len(), + DkgPhase::Round5 => { + let consensus = VerifyingKeyConsensus::::get(network_curve, dkg_index); + consensus.state.participants.count_ones::() + }, + DkgPhase::Round6 => { + let shares = VerifyingSharesParticipants::::get(network_curve, dkg_index); + shares.count_ones::() + }, + _ => return false, + }; + + let submission_probability = get_submission_probability( + total_authorities, + values_count, + dkg_round_period, + elapsed_blocks + ); + + let seed = sp_io::offchain::random_seed(); + let randomness = ::decode(&mut TrailingZeroInput::new(seed.as_ref())) + .expect("input is padded with zeroes; qed"); + let random_roll = Perbill::from_parts(randomness % Perbill::ACCURACY); + + random_roll >= submission_probability + } + fn dkg_run_process( authority_index: AuthIndex, authority_key: T::AuthorityId, @@ -3796,6 +3859,16 @@ impl Pallet { let block_longevity: BlockNumberFor = T::UnsignedLongevity::get().unique_saturated_into(); + if Self::should_skip_submission( + network_curve, + current_block, + qualification_authorities, + ) { + return Ok(ExodusOk::DkgRoundSubmissionSkipped( + authority_index, network_curve, + )); + } + let dkg_storage = DkgStorage::default() .with_network_curve(network_curve) .with_current_block(current_block) diff --git a/pallets/exodus/src/types/success.rs b/pallets/exodus/src/types/success.rs index 0250c9f..db36ad8 100644 --- a/pallets/exodus/src/types/success.rs +++ b/pallets/exodus/src/types/success.rs @@ -10,6 +10,7 @@ pub enum ExodusOk { ExodusSignedPartially(ExodusSession, NetworkId, RoastSession, AuthIndex, NetworkCurve), ExodusVerifyingShareMissed(AuthIndex, DkgIndex, NetworkCurve), + DkgRoundSubmissionSkipped(AuthIndex, NetworkCurve), DkgNotPartOfRound(u8, AuthIndex, NetworkCurve), DkgRoundRegistered(u8, AuthIndex, NetworkCurve), DkgPreviousPurged(AuthIndex, NetworkCurve, u32), @@ -47,6 +48,7 @@ where }, ExodusOk::ExodusVerifyingShareMissed(auth_index, dkg_index, curve) => write!(fmt, "No verifying share found for authority #{auth_index} in EXODUS round based on DKG session #{dkg_index} for curve {curve}."), + ExodusOk::DkgRoundSubmissionSkipped(auth_index, curve) => write!(fmt, "DKG authority #{auth_index} skipped submission for network curve {curve} temporarily."), ExodusOk::DkgNotPartOfRound(round, auth_index, curve) => write!(fmt, "DKG authority #{auth_index} is not part of Round {round} for network curve {curve}."), ExodusOk::DkgRoundRegistered(round, auth_index, curve) => write!(fmt, "DKG Round {round} registered by authority #{auth_index} for network curve {curve}."), ExodusOk::DkgPreviousPurged(auth_index, curve, removed_count) => write!(fmt, "Storage prepared for a new DKG session by authority #{auth_index} for network curve {curve}. {removed_count} entries removed."),