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:
parent
9a7deaa39b
commit
2a10db05c5
@ -585,47 +585,44 @@ pub mod pallet {
|
|||||||
|
|
||||||
if current_block < next_attempt_block { return weight; }
|
if current_block < next_attempt_block { return weight; }
|
||||||
|
|
||||||
weight.saturating_accrue(T::DbWeight::get().reads(1));
|
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
|
||||||
let attestation_len = NetworkAttestations::<T>::decode_len(&network_id);
|
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 {
|
Some(attestations.iter().flatten().copied().collect())
|
||||||
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
|
}
|
||||||
NetworkAttestations::<T>::mutate(&network_id, |attestations| {
|
);
|
||||||
attestations.bounded_resize(len, None);
|
|
||||||
});
|
if let Some(mut block_numbers) = maybe_block_numbers.as_mut() {
|
||||||
return weight;
|
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> =
|
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
|
let next_release_block = attestation_delay_converted.saturating_add(current_block);
|
||||||
.saturating_add(current_block);
|
|
||||||
|
|
||||||
weight.saturating_accrue(T::DbWeight::get().writes(1));
|
|
||||||
NextConsensusAttemptBlocks::<T>::insert(network_id, next_release_block);
|
NextConsensusAttemptBlocks::<T>::insert(network_id, next_release_block);
|
||||||
|
weight.saturating_accrue(T::DbWeight::get().writes(1));
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1032,12 +1029,14 @@ impl<T: Config> Pallet<T> {
|
|||||||
rpc_endpoints: &Vec<Vec<u8>>,
|
rpc_endpoints: &Vec<Vec<u8>>,
|
||||||
should_skip_submission: impl Fn(usize, BlockNumberFor<T>, u64) -> bool,
|
should_skip_submission: impl Fn(usize, BlockNumberFor<T>, u64) -> bool,
|
||||||
) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> {
|
) -> WeavingResult<T, WeavingOk<NetworkIdOf<T>>> {
|
||||||
let attestations_count = NetworkAttestations::<T>::get(&network_id)
|
let authorities_count = Authorities::<T>::decode_len()
|
||||||
.iter()
|
.unwrap_or(0);
|
||||||
.flatten()
|
|
||||||
.count();
|
|
||||||
|
|
||||||
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,
|
attestations_count,
|
||||||
NextConsensusAttemptBlocks::<T>::get(&network_id),
|
NextConsensusAttemptBlocks::<T>::get(&network_id),
|
||||||
T::AttestationDelay::get(),
|
T::AttestationDelay::get(),
|
||||||
|
|||||||
@ -97,12 +97,13 @@ fn should_successfully_compute_median_and_start_weaving_round() {
|
|||||||
|
|
||||||
assert!(GhostWeaver::tapestry_drafts(network_id).is_none());
|
assert!(GhostWeaver::tapestry_drafts(network_id).is_none());
|
||||||
|
|
||||||
System::set_block_number(LOCK_BLOCK_EXPIRATION + 3);
|
let target_block = LOCK_BLOCK_EXPIRATION + 1 + AttestationDelay::get();
|
||||||
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 3);
|
System::set_block_number(target_block);
|
||||||
|
GhostWeaver::on_initialize(target_block);
|
||||||
|
|
||||||
let round = GhostWeaver::tapestry_drafts(network_id);
|
let round = GhostWeaver::tapestry_drafts(network_id);
|
||||||
assert!(round.is_some());
|
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(), att2, sig2).is_ok());
|
||||||
assert!(GhostWeaver::extend_warp(RuntimeOrigin::none(), att3, sig3).is_ok());
|
assert!(GhostWeaver::extend_warp(RuntimeOrigin::none(), att3, sig3).is_ok());
|
||||||
|
|
||||||
System::set_block_number(LOCK_BLOCK_EXPIRATION + 11);
|
let first_attempt_block = LOCK_BLOCK_EXPIRATION + 9 + AttestationDelay::get();
|
||||||
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 11);
|
System::set_block_number(first_attempt_block);
|
||||||
|
GhostWeaver::on_initialize(first_attempt_block);
|
||||||
|
|
||||||
let cooldown_block = GhostWeaver::next_consensus_attempt_blocks(network_id);
|
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);
|
TapestryDrafts::<TestRuntime>::remove(network_id);
|
||||||
|
|
||||||
System::set_block_number(LOCK_BLOCK_EXPIRATION + 12);
|
System::set_block_number(first_attempt_block + 1);
|
||||||
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION+ 12);
|
GhostWeaver::on_initialize(first_attempt_block + 1);
|
||||||
assert!(TapestryDrafts::<TestRuntime>::get(network_id).is_none());
|
assert!(TapestryDrafts::<TestRuntime>::get(network_id).is_none());
|
||||||
|
|
||||||
System::set_block_number(LOCK_BLOCK_EXPIRATION + 14);
|
System::set_block_number(cooldown_block);
|
||||||
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 14);
|
GhostWeaver::on_initialize(cooldown_block);
|
||||||
assert!(TapestryDrafts::<TestRuntime>::get(network_id).is_some());
|
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)
|
.and_then(|opt| *opt)
|
||||||
.unwrap_or(0);
|
.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!(prev_utxo_block, 0);
|
||||||
assert_eq!(curr_utxo_block, 960127);
|
assert_eq!(curr_utxo_block, 960127);
|
||||||
|
|
||||||
assert!(TapestryDrafts::<TestRuntime>::get(&utxo_network_id).is_none());
|
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());
|
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)
|
.and_then(|opt| *opt)
|
||||||
.unwrap_or(0);
|
.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!(prev_evm_block, 0);
|
||||||
assert_eq!(curr_evm_block, 25639556);
|
assert_eq!(curr_evm_block, 25639556);
|
||||||
|
|
||||||
assert!(TapestryDrafts::<TestRuntime>::get(&evm_network_id).is_none());
|
assert!(TapestryDrafts::<TestRuntime>::get(&evm_network_id).is_none());
|
||||||
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 3u64);
|
GhostWeaver::on_initialize(target_evm_block);
|
||||||
GhostWeaver::on_initialize(LOCK_BLOCK_EXPIRATION + 4u64);
|
|
||||||
assert!(TapestryDrafts::<TestRuntime>::get(&evm_network_id).is_some());
|
assert!(TapestryDrafts::<TestRuntime>::get(&evm_network_id).is_some());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "casper-runtime"
|
name = "casper-runtime"
|
||||||
version = "4.0.21"
|
version = "4.0.22"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
description = "Runtime of the Casper Network"
|
description = "Runtime of the Casper Network"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
|||||||
@ -124,7 +124,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
|||||||
spec_name: create_runtime_str!("casper"),
|
spec_name: create_runtime_str!("casper"),
|
||||||
impl_name: create_runtime_str!("casper-svengali"),
|
impl_name: create_runtime_str!("casper-svengali"),
|
||||||
authoring_version: 0,
|
authoring_version: 0,
|
||||||
spec_version: 15,
|
spec_version: 16,
|
||||||
impl_version: 0,
|
impl_version: 0,
|
||||||
apis: RUNTIME_API_VERSIONS,
|
apis: RUNTIME_API_VERSIONS,
|
||||||
transaction_version: 1,
|
transaction_version: 1,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user