fix offchain submission probability window calculation and saturating block math

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2026-11-16 20:10:51 +03:00
parent 0af8a71d6a
commit ca0988e011
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
2 changed files with 39 additions and 25 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-weaver" name = "ghost-weaver"
version = "0.0.28" version = "0.0.29"
description = "Weaving a secure cryptographic tapestry across different external chains." description = "Weaving a secure cryptographic tapestry across different external chains."
license.workspace = true license.workspace = true
authors.workspace = true authors.workspace = true

View File

@ -24,7 +24,7 @@ use sp_runtime::{
}, },
traits::{ traits::{
TrailingZeroInput, BlockNumberProvider, Saturating, TrailingZeroInput, BlockNumberProvider, Saturating,
UniqueSaturatedInto, UniqueSaturatedInto, One,
}, },
RuntimeAppPublic, Perbill, PerThing, RuntimeAppPublic, Perbill, PerThing,
}; };
@ -531,11 +531,10 @@ pub mod pallet {
let mut weight = T::DbWeight::get().reads(1); let mut weight = T::DbWeight::get().reads(1);
let converted_block: usize = current_block.unique_saturated_into(); let converted_block: usize = current_block.unique_saturated_into();
let (network_id, data) = match T::NetworkDataHandler::network_for_block(converted_block) let Some((network_id, data)) =
{ T::NetworkDataHandler::network_for_block(converted_block) else {
Some((network_id, data)) => (network_id, data), return weight;
None => return weight, };
};
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1));
let len = Authorities::<T>::decode_len().unwrap_or_default(); let len = Authorities::<T>::decode_len().unwrap_or_default();
@ -600,8 +599,10 @@ pub mod pallet {
let attestation_delay_converted: BlockNumberFor<T> = let attestation_delay_converted: BlockNumberFor<T> =
T::AttestationDelay::get().unique_saturated_into(); 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)); weight.saturating_accrue(T::DbWeight::get().writes(1));
let next_release_block = current_block + attestation_delay_converted;
NextConsensusAttemptBlocks::<T>::insert(network_id, next_release_block); NextConsensusAttemptBlocks::<T>::insert(network_id, next_release_block);
let mut block_numbers = NetworkAttestations::<T>::get(&network_id) let mut block_numbers = NetworkAttestations::<T>::get(&network_id)
@ -830,13 +831,25 @@ impl<T: Config> Pallet<T> {
return Err(WeavingError::NoEndpointsAvailable(network_id)); return Err(WeavingError::NoEndpointsAvailable(network_id));
} }
let check_probability = |values_count: usize, block_until: BlockNumberFor<T>| -> bool { let should_skip_submission = |
values_count: usize,
block_until: BlockNumberFor<T>,
constant_delay: u64,
| -> bool {
let total_authorities = Authorities::<T>::decode_len().unwrap_or(1); let total_authorities = Authorities::<T>::decode_len().unwrap_or(1);
let constant_block_delay: BlockNumberFor<T> = 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( let submission_probability = get_submission_probability(
total_authorities, total_authorities,
values_count, values_count,
block_until, constant_block_delay,
current_block, elapsed_blocks
); );
let seed = sp_io::offchain::random_seed(); let seed = sp_io::offchain::random_seed();
@ -855,7 +868,7 @@ impl<T: Config> Pallet<T> {
deadline, deadline,
&network_data, &network_data,
&rpc_endpoints, &rpc_endpoints,
check_probability, should_skip_submission,
), ),
None => Self::try_request_and_submit_block( None => Self::try_request_and_submit_block(
network_id, network_id,
@ -863,7 +876,7 @@ impl<T: Config> Pallet<T> {
deadline, deadline,
&network_data, &network_data,
&rpc_endpoints, &rpc_endpoints,
check_probability, should_skip_submission,
), ),
} }
} }
@ -875,17 +888,17 @@ impl<T: Config> Pallet<T> {
deadline: rt_offchain::Timestamp, deadline: rt_offchain::Timestamp,
network_data: &NetworkData, network_data: &NetworkData,
rpc_endpoints: &Vec<Vec<u8>>, rpc_endpoints: &Vec<Vec<u8>>,
check_probability: impl Fn(usize, BlockNumberFor<T>) -> bool, should_skip_submission: impl Fn(usize, BlockNumberFor<T>, u64) -> bool,
) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> { ) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> {
let weaving_session = CurrentWeavingSession::<T>::get(&network_id); if should_skip_submission(
WeavingStates::<T>::get(&network_id).count as usize,
let block_until = tapestry_draft.until.saturating_sub(current_block); tapestry_draft.until,
let weaving_count = WeavingStates::<T>::get(&network_id).count as usize; T::WeavingDelay::get(),
) {
if check_probability(weaving_count, block_until) {
return Ok(WeavingOk::Skipped(network_id)); return Ok(WeavingOk::Skipped(network_id));
} }
let weaving_session = CurrentWeavingSession::<T>::get(&network_id);
let target_block = tapestry_draft.median; let target_block = tapestry_draft.median;
let request_id = current_block.unique_saturated_into(); let request_id = current_block.unique_saturated_into();
@ -1017,17 +1030,18 @@ impl<T: Config> Pallet<T> {
deadline: rt_offchain::Timestamp, deadline: rt_offchain::Timestamp,
network_data: &NetworkData, network_data: &NetworkData,
rpc_endpoints: &Vec<Vec<u8>>, rpc_endpoints: &Vec<Vec<u8>>,
check_probability: impl Fn(usize, BlockNumberFor<T>) -> bool, should_skip_submission: impl Fn(usize, BlockNumberFor<T>, u64) -> bool,
) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> { ) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> {
let next_attempt_block = NextConsensusAttemptBlocks::<T>::get(&network_id);
let block_until = next_attempt_block.saturating_sub(current_block);
let attestations_count = NetworkAttestations::<T>::get(&network_id) let attestations_count = NetworkAttestations::<T>::get(&network_id)
.iter() .iter()
.flatten() .flatten()
.count(); .count();
if check_probability(attestations_count, block_until) { if should_skip_submission(
attestations_count,
NextConsensusAttemptBlocks::<T>::get(&network_id),
T::AttestationDelay::get(),
) {
return Ok(WeavingOk::Skipped(network_id)); return Ok(WeavingOk::Skipped(network_id));
} }