From ca0988e01125c9c3e009343e369fccf2375c86ea Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Mon, 16 Nov 2026 20:10:51 +0300 Subject: [PATCH] fix offchain submission probability window calculation and saturating block math Signed-off-by: Uncle Stretch --- pallets/weaver/Cargo.toml | 2 +- pallets/weaver/src/lib.rs | 62 ++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/pallets/weaver/Cargo.toml b/pallets/weaver/Cargo.toml index a9b7678..fd9e8c1 100644 --- a/pallets/weaver/Cargo.toml +++ b/pallets/weaver/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-weaver" -version = "0.0.28" +version = "0.0.29" description = "Weaving a secure cryptographic tapestry across different external chains." license.workspace = true authors.workspace = true diff --git a/pallets/weaver/src/lib.rs b/pallets/weaver/src/lib.rs index 023306b..012f437 100644 --- a/pallets/weaver/src/lib.rs +++ b/pallets/weaver/src/lib.rs @@ -24,7 +24,7 @@ use sp_runtime::{ }, traits::{ TrailingZeroInput, BlockNumberProvider, Saturating, - UniqueSaturatedInto, + UniqueSaturatedInto, One, }, RuntimeAppPublic, Perbill, PerThing, }; @@ -531,11 +531,10 @@ pub mod pallet { let mut weight = T::DbWeight::get().reads(1); let converted_block: usize = current_block.unique_saturated_into(); - let (network_id, data) = match T::NetworkDataHandler::network_for_block(converted_block) - { - Some((network_id, data)) => (network_id, data), - None => return weight, - }; + let Some((network_id, data)) = + T::NetworkDataHandler::network_for_block(converted_block) else { + return weight; + }; weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); let len = Authorities::::decode_len().unwrap_or_default(); @@ -600,8 +599,10 @@ pub mod pallet { let attestation_delay_converted: BlockNumberFor = T::AttestationDelay::get().unique_saturated_into(); + let next_release_block = attestation_delay_converted + .saturating_add(current_block); + weight.saturating_accrue(T::DbWeight::get().writes(1)); - let next_release_block = current_block + attestation_delay_converted; NextConsensusAttemptBlocks::::insert(network_id, next_release_block); let mut block_numbers = NetworkAttestations::::get(&network_id) @@ -830,13 +831,25 @@ impl Pallet { return Err(WeavingError::NoEndpointsAvailable(network_id)); } - let check_probability = |values_count: usize, block_until: BlockNumberFor| -> bool { + let should_skip_submission = | + values_count: usize, + block_until: BlockNumberFor, + constant_delay: u64, + | -> bool { let total_authorities = Authorities::::decode_len().unwrap_or(1); + let constant_block_delay: BlockNumberFor = constant_delay + .unique_saturated_into(); + + let blocks_remaining = block_until.saturating_sub(current_block); + let elapsed_blocks = constant_block_delay + .saturating_sub(blocks_remaining) + .saturating_add(One::one()); + let submission_probability = get_submission_probability( total_authorities, values_count, - block_until, - current_block, + constant_block_delay, + elapsed_blocks ); let seed = sp_io::offchain::random_seed(); @@ -855,7 +868,7 @@ impl Pallet { deadline, &network_data, &rpc_endpoints, - check_probability, + should_skip_submission, ), None => Self::try_request_and_submit_block( network_id, @@ -863,7 +876,7 @@ impl Pallet { deadline, &network_data, &rpc_endpoints, - check_probability, + should_skip_submission, ), } } @@ -875,17 +888,17 @@ impl Pallet { deadline: rt_offchain::Timestamp, network_data: &NetworkData, rpc_endpoints: &Vec>, - check_probability: impl Fn(usize, BlockNumberFor) -> bool, + should_skip_submission: impl Fn(usize, BlockNumberFor, u64) -> bool, ) -> WeavingResult>> { - let weaving_session = CurrentWeavingSession::::get(&network_id); - - let block_until = tapestry_draft.until.saturating_sub(current_block); - let weaving_count = WeavingStates::::get(&network_id).count as usize; - - if check_probability(weaving_count, block_until) { + if should_skip_submission( + WeavingStates::::get(&network_id).count as usize, + tapestry_draft.until, + T::WeavingDelay::get(), + ) { return Ok(WeavingOk::Skipped(network_id)); } + let weaving_session = CurrentWeavingSession::::get(&network_id); let target_block = tapestry_draft.median; let request_id = current_block.unique_saturated_into(); @@ -1017,17 +1030,18 @@ impl Pallet { deadline: rt_offchain::Timestamp, network_data: &NetworkData, rpc_endpoints: &Vec>, - check_probability: impl Fn(usize, BlockNumberFor) -> bool, + should_skip_submission: impl Fn(usize, BlockNumberFor, u64) -> bool, ) -> WeavingResult>> { - let next_attempt_block = NextConsensusAttemptBlocks::::get(&network_id); - let block_until = next_attempt_block.saturating_sub(current_block); - let attestations_count = NetworkAttestations::::get(&network_id) .iter() .flatten() .count(); - if check_probability(attestations_count, block_until) { + if should_skip_submission( + attestations_count, + NextConsensusAttemptBlocks::::get(&network_id), + T::AttestationDelay::get(), + ) { return Ok(WeavingOk::Skipped(network_id)); }