rearrange offchain worker execution sequence

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2025-12-04 18:21:58 +03:00
parent 0323c25d08
commit a6ce55c1e8
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
3 changed files with 21 additions and 28 deletions

2
Cargo.lock generated
View File

@ -3837,7 +3837,7 @@ dependencies = [
[[package]]
name = "ghost-slow-clap"
version = "0.4.6"
version = "0.4.7"
dependencies = [
"frame-benchmarking",
"frame-support",

View File

@ -1,6 +1,6 @@
[package]
name = "ghost-slow-clap"
version = "0.4.6"
version = "0.4.7"
description = "Applause protocol for the EVM bridge"
license.workspace = true
authors.workspace = true

View File

@ -788,34 +788,18 @@ impl<T: Config> Pallet<T> {
fn start_slow_clapping(block_number: BlockNumberFor<T>) -> OffchainResult<T, ()> {
let session_index = T::ValidatorSet::session_index();
let networks_len = T::NetworkDataHandler::iter().count();
let network_in_use = T::NetworkDataHandler::iter()
.nth(
block_number
.into()
.as_usize()
.checked_rem(networks_len)
.unwrap_or_default(),
)
let networks = T::NetworkDataHandler::iter().collect::<Vec<_>>();
let network_index = block_number
.into()
.as_usize()
.checked_rem(networks.len())
.unwrap_or_default();
let network_in_use = networks
.iter()
.nth(network_index)
.ok_or(OffchainErr::NoStoredNetworks)?;
let network_id_encoded = network_in_use.0.encode();
let rate_limit_delay_key = Self::create_storage_key(b"rate-limit-", &network_id_encoded);
let rate_limit_delay = Self::read_persistent_offchain_storage(
&rate_limit_delay_key,
network_in_use.1.rate_limit_delay,
);
let network_lock_key = Self::create_storage_key(b"network-lock-", &network_id_encoded);
let block_until =
rt_offchain::Duration::from_millis(rate_limit_delay.max(MIN_LOCK_GUARD_PERIOD));
let mut network_lock = StorageLock::<Time>::with_deadline(&network_lock_key, block_until);
let _lock_guard = network_lock
.try_lock()
.map_err(|_| OffchainErr::OffchainTimeoutPeriod(network_in_use.0))?;
log::info!(
target: LOG_TARGET,
"👻 Offchain worker started for network #{:?} at block #{:?}",
@ -823,6 +807,15 @@ impl<T: Config> Pallet<T> {
block_number,
);
let network_id_encoded = network_in_use.0.encode();
let network_lock_key = Self::create_storage_key(b"network-lock-", &network_id_encoded);
let lock_until = rt_offchain::Duration::from_millis(MIN_LOCK_GUARD_PERIOD);
let mut network_lock = StorageLock::<Time>::with_deadline(&network_lock_key, lock_until);
let _lock_guard = network_lock
.try_lock()
.map_err(|_| OffchainErr::OffchainTimeoutPeriod(network_in_use.0))?;
Self::do_evm_claps_or_save_block(session_index, network_in_use.0, &network_in_use.1)
}