rotate endpoints on each offchain worker exection

Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
Uncle Stinky
2025-07-29 12:42:36 +03:00
parent 0375bd1434
commit 1d826fbf7e
3 changed files with 35 additions and 7 deletions

View File

@@ -28,7 +28,7 @@ use sp_runtime::{
storage_lock::{StorageLock, Time},
HttpError,
},
traits::{BlockNumberProvider, Convert, Saturating},
traits::{BlockNumberProvider, Convert, Saturating, TrailingZeroInput},
Perbill, RuntimeAppPublic, RuntimeDebug, SaturatedConversion,
};
use sp_staking::{
@@ -692,14 +692,35 @@ impl<T: Config> Pallet<T> {
let block_distance_key = Self::create_storage_key(b"block-distance-", &network_id_encoded);
let endpoint_key = Self::create_storage_key(b"endpoint-", &network_id_encoded);
let rpc_endpoint = Self::read_persistent_offchain_storage(
&endpoint_key,
network_data.default_endpoint.clone(),
);
let max_block_distance = Self::read_persistent_offchain_storage(
&block_distance_key,
network_data.block_distance,
);
let stored_endpoints = Self::read_persistent_offchain_storage(
&endpoint_key,
network_data.default_endpoints.clone(),
);
let random_seed = sp_io::offchain::random_seed();
let random_number = <u32>::decode(
&mut TrailingZeroInput::new(random_seed.as_ref())
).expect("input is padded with zeroes; qed");
let rpc_endpoint = if stored_endpoints.len() > 0 {
stored_endpoints
.iter()
.nth((random_number as usize)
.checked_rem(stored_endpoints.len())
.unwrap_or_default())
.expect("stored endpoint should be non empty; qed")
} else {
network_data.default_endpoints
.iter()
.nth((random_number as usize)
.checked_rem(network_data.default_endpoints.len())
.unwrap_or_default())
.expect("default endpoint should be non empty; qed")
};
StorageValueRef::persistent(&block_number_key)
.mutate(