Skip to content

Commit

Permalink
Merge pull request #479 from Cerebellum-Network/feature/ddc-verificat…
Browse files Browse the repository at this point in the history
…ion-ocw-mutex

Allow only one instance of DDC verification OCW to run at a time
  • Loading branch information
khssnv authored Nov 20, 2024
2 parents 20ee149 + 034ca93 commit d094be6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
43 changes: 31 additions & 12 deletions pallets/ddc-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ use scale_info::prelude::{format, string::String};
use serde::{Deserialize, Serialize};
use sp_application_crypto::RuntimeAppPublic;
use sp_core::crypto::UncheckedFrom;
pub use sp_io::crypto::sr25519_public_keys;
pub use sp_io::{
crypto::sr25519_public_keys,
offchain::{
local_storage_clear, local_storage_compare_and_set, local_storage_get, local_storage_set,
},
};
use sp_runtime::{
offchain::{http, Duration, StorageKind},
traits::{Hash, IdentifyAccount},
Expand Down Expand Up @@ -95,6 +100,8 @@ pub mod pallet {
const RESPONSE_TIMEOUT: u64 = 20000;
pub const BUCKETS_AGGREGATES_FETCH_BATCH_SIZE: usize = 100;
pub const NODES_AGGREGATES_FETCH_BATCH_SIZE: usize = 10;
pub const IS_RUNNING_KEY: &[u8] = b"offchain::validator::is_running";
pub const IS_RUNNING_VALUE: &[u8] = &[1];

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down Expand Up @@ -718,6 +725,16 @@ pub mod pallet {
return;
}

// Allow only one instance of the offchain worker to run at a time.
if !local_storage_compare_and_set(
StorageKind::PERSISTENT,
IS_RUNNING_KEY,
None,
IS_RUNNING_VALUE,
) {
return;
}

let verification_key = unwrap_or_log_error!(
Self::collect_verification_pub_key(),
"❌ Error collecting validator verification key"
Expand Down Expand Up @@ -1248,6 +1265,9 @@ pub mod pallet {
}
}
}

// Allow the next invocation of the offchain worker hook to run.
local_storage_clear(StorageKind::PERSISTENT, IS_RUNNING_KEY);
}
}

Expand Down Expand Up @@ -2417,13 +2437,13 @@ pub mod pallet {
pub(crate) fn store_verification_account_id(account_id: T::AccountId) {
let validator: Vec<u8> = account_id.encode();
let key = format!("offchain::validator::{:?}", DAC_VERIFICATION_KEY_TYPE).into_bytes();
sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &key, &validator);
local_storage_set(StorageKind::PERSISTENT, &key, &validator);
}

pub(crate) fn fetch_verification_account_id() -> Result<T::AccountId, OCWError> {
let key = format!("offchain::validator::{:?}", DAC_VERIFICATION_KEY_TYPE).into_bytes();

match sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &key) {
match local_storage_get(StorageKind::PERSISTENT, &key) {
Some(data) => {
let account_id = T::AccountId::decode(&mut &data[..])
.map_err(|_| OCWError::FailedToFetchVerificationKey)?;
Expand Down Expand Up @@ -2459,7 +2479,7 @@ pub mod pallet {
.encode();

// Store the serialized data in local offchain storage
sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &key, &encoded_tuple);
local_storage_set(StorageKind::PERSISTENT, &key, &encoded_tuple);
}

pub(crate) fn get_nodes_total_usage(
Expand Down Expand Up @@ -2517,11 +2537,10 @@ pub mod pallet {
let key = Self::derive_key(cluster_id, era_id);

// Retrieve encoded tuple from local storage
let encoded_tuple =
match sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &key) {
Some(data) => data,
None => return None,
};
let encoded_tuple = match local_storage_get(StorageKind::PERSISTENT, &key) {
Some(data) => data,
None => return None,
};

// Attempt to decode tuple from bytes
match Decode::decode(&mut &encoded_tuple[..]) {
Expand Down Expand Up @@ -2550,8 +2569,8 @@ pub mod pallet {

pub(crate) fn _store_and_fetch_nonce(node_id: String) -> u64 {
let key = format!("offchain::activities::nonce::{:?}", node_id).into_bytes();
let encoded_nonce = sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &key)
.unwrap_or_else(|| 0.encode());
let encoded_nonce =
local_storage_get(StorageKind::PERSISTENT, &key).unwrap_or_else(|| 0.encode());

let nonce_data = match Decode::decode(&mut &encoded_nonce[..]) {
Ok(nonce) => nonce,
Expand All @@ -2564,7 +2583,7 @@ pub mod pallet {

let new_nonce = nonce_data + 1;

sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &key, &new_nonce.encode());
local_storage_set(StorageKind::PERSISTENT, &key, &new_nonce.encode());
nonce_data
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 61004,
spec_version: 61005,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 23,
Expand Down Expand Up @@ -1317,7 +1317,7 @@ impl pallet_ddc_verification::Config for Runtime {
type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId;
type ActivityHasher = BlakeTwo256;
const MAJORITY: u8 = 67;
const BLOCK_TO_START: u16 = 30; // every 30 blocks
const BLOCK_TO_START: u16 = 1; // every block
const DAC_REDUNDANCY_FACTOR: u16 = 3;
type AggregatorsQuorum = MajorityOfAggregators;
const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion runtime/cere/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 61004,
spec_version: 61005,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 23,
Expand Down

0 comments on commit d094be6

Please sign in to comment.