initialize consensus deadline on resize and guard unready attestations in OCW

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2026-11-17 14:47:19 +03:00
parent 9a7deaa39b
commit 2a10db05c5
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
4 changed files with 65 additions and 53 deletions

View File

@ -585,47 +585,44 @@ pub mod pallet {
if current_block < next_attempt_block { return weight; }
weight.saturating_accrue(T::DbWeight::get().reads(1));
let attestation_len = NetworkAttestations::<T>::decode_len(&network_id);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
let mut maybe_block_numbers = NetworkAttestations::<T>::mutate(
&network_id,
|attestations| -> Option<Vec<ExternalBlockNumber>> {
if attestations.len() != len {
attestations.bounded_resize(len, None);
return None;
}
if attestation_len.unwrap_or(0) != len {
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
NetworkAttestations::<T>::mutate(&network_id, |attestations| {
attestations.bounded_resize(len, None);
});
return weight;
Some(attestations.iter().flatten().copied().collect())
}
);
if let Some(mut block_numbers) = maybe_block_numbers.as_mut() {
if let Some(median_block) = Self::validate_block_numbers_consensus(
&mut block_numbers,
data.block_deviation,
weaving_threshold,
) {
let tapestry_draft = TapestryDraftBuilder::default()
.with_block_delay(T::WeavingDelay::get())
.with_current_block(current_block)
.with_median_external(median_block)
.build();
TapestryStrands::<T>::remove(&network_id);
TapestryDrafts::<T>::insert(&network_id, tapestry_draft);
weight.saturating_accrue(T::DbWeight::get().writes(2));
}
}
let attestation_delay_converted: BlockNumberFor<T> =
T::AttestationDelay::get().unique_saturated_into();
let next_release_block = attestation_delay_converted
.saturating_add(current_block);
let next_release_block = attestation_delay_converted.saturating_add(current_block);
weight.saturating_accrue(T::DbWeight::get().writes(1));
NextConsensusAttemptBlocks::<T>::insert(network_id, next_release_block);
let mut block_numbers = NetworkAttestations::<T>::get(&network_id)
.iter()
.flatten()
.copied()
.collect();
if let Some(median_block) = Self::validate_block_numbers_consensus(
&mut block_numbers,
data.block_deviation,
weaving_threshold,
) {
let tapestry_draft = TapestryDraftBuilder::default()
.with_block_delay(T::WeavingDelay::get())
.with_current_block(current_block)
.with_median_external(median_block)
.build();
TapestryStrands::<T>::remove(&network_id);
TapestryDrafts::<T>::insert(&network_id, tapestry_draft);
weight.saturating_accrue(T::DbWeight::get().writes(2));
}
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
}
@ -1032,12 +1029,14 @@ impl<T: Config> Pallet<T> {
rpc_endpoints: &Vec<Vec<u8>>,
should_skip_submission: impl Fn(usize, BlockNumberFor<T>, u64) -> bool,
) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> {
let attestations_count = NetworkAttestations::<T>::get(&network_id)
.iter()
.flatten()
.count();
let authorities_count = Authorities::<T>::decode_len()
.unwrap_or(0);
if should_skip_submission(
let attestations = NetworkAttestations::<T>::get(&network_id);
let attestations_count = attestations.iter().flatten().count();
let attestations_not_ready = authorities_count != attestations.len();
if attestations_not_ready || should_skip_submission(
attestations_count,
NextConsensusAttemptBlocks::<T>::get(&network_id),
T::AttestationDelay::get(),

View File

@ -97,12 +97,13 @@ fn should_successfully_compute_median_and_start_weaving_round() {
assert!(GhostWeaver::tapestry_drafts(network_id).is_none());
System::set_block_number(LOCK_BLOCK_EXPIRATION + 3);
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 3);
let target_block = LOCK_BLOCK_EXPIRATION + 1 + AttestationDelay::get();
System::set_block_number(target_block);
GhostWeaver::on_initialize(target_block);
let round = GhostWeaver::tapestry_drafts(network_id);
assert!(round.is_some());
assert_eq!(round.unwrap().until, LOCK_BLOCK_EXPIRATION + 3 + WeavingDelay::get());
assert_eq!(round.unwrap().until, target_block + WeavingDelay::get());
});
}
@ -140,20 +141,21 @@ fn should_apply_cooldown_delay_on_consensus_failure() {
assert!(GhostWeaver::extend_warp(RuntimeOrigin::none(), att2, sig2).is_ok());
assert!(GhostWeaver::extend_warp(RuntimeOrigin::none(), att3, sig3).is_ok());
System::set_block_number(LOCK_BLOCK_EXPIRATION + 11);
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 11);
let first_attempt_block = LOCK_BLOCK_EXPIRATION + 9 + AttestationDelay::get();
System::set_block_number(first_attempt_block);
GhostWeaver::on_initialize(first_attempt_block);
let cooldown_block = GhostWeaver::next_consensus_attempt_blocks(network_id);
assert_eq!(cooldown_block, LOCK_BLOCK_EXPIRATION + 14);
assert_eq!(cooldown_block, first_attempt_block + AttestationDelay::get());
TapestryDrafts::<TestRuntime>::remove(network_id);
System::set_block_number(LOCK_BLOCK_EXPIRATION + 12);
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION+ 12);
System::set_block_number(first_attempt_block + 1);
GhostWeaver::on_initialize(first_attempt_block + 1);
assert!(TapestryDrafts::<TestRuntime>::get(network_id).is_none());
System::set_block_number(LOCK_BLOCK_EXPIRATION + 14);
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 14);
System::set_block_number(cooldown_block);
GhostWeaver::on_initialize(cooldown_block);
assert!(TapestryDrafts::<TestRuntime>::get(network_id).is_some());
});
}
@ -340,11 +342,17 @@ fn offchain_worker_should_fetch_block_and_submit_unsigned_tx() {
.and_then(|opt| *opt)
.unwrap_or(0);
let target_utxo_block = if curr_utxo_block % 2 == (LOCK_BLOCK_EXPIRATION + 1) % 2 {
curr_utxo_block
} else {
curr_utxo_block + 1
};
assert_eq!(prev_utxo_block, 0);
assert_eq!(curr_utxo_block, 960127);
assert!(TapestryDrafts::<TestRuntime>::get(&utxo_network_id).is_none());
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 3u64);
GhostWeaver::on_initialize(target_utxo_block);
assert!(TapestryDrafts::<TestRuntime>::get(&utxo_network_id).is_some());
});
@ -367,12 +375,17 @@ fn offchain_worker_should_fetch_block_and_submit_unsigned_tx() {
.and_then(|opt| *opt)
.unwrap_or(0);
let target_evm_block = if curr_evm_block % 2 == (LOCK_BLOCK_EXPIRATION + 2) % 2 {
curr_evm_block
} else {
curr_evm_block + 1
};
assert_eq!(prev_evm_block, 0);
assert_eq!(curr_evm_block, 25639556);
assert!(TapestryDrafts::<TestRuntime>::get(&evm_network_id).is_none());
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 3u64);
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 4u64);
GhostWeaver::on_initialize(target_evm_block);
assert!(TapestryDrafts::<TestRuntime>::get(&evm_network_id).is_some());
});
}

View File

@ -1,6 +1,6 @@
[package]
name = "casper-runtime"
version = "4.0.21"
version = "4.0.22"
build = "build.rs"
description = "Runtime of the Casper Network"
edition.workspace = true

View File

@ -124,7 +124,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("casper"),
impl_name: create_runtime_str!("casper-svengali"),
authoring_version: 0,
spec_version: 15,
spec_version: 16,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,