Skip to content

Commit

Permalink
Merge pull request #480 from Cerebellum-Network/feature/last-paid-era…
Browse files Browse the repository at this point in the history
…-setter

Allow root origin to set last paid era of a cluster
  • Loading branch information
khssnv authored Nov 21, 2024
2 parents d094be6 + 95c6f7a commit 419f80f
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 37 deletions.
4 changes: 2 additions & 2 deletions pallets/ddc-clusters/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ fn set_last_validated_era_works() {

// Cluster doesn't exist
assert_noop!(
DdcClusters::set_last_paid_era(&cluster_id, era_id),
<DdcClusters as ClusterValidator<Test>>::set_last_paid_era(&cluster_id, era_id),
Error::<Test>::ClusterDoesNotExist
);

Expand Down Expand Up @@ -619,7 +619,7 @@ fn set_last_validated_era_works() {
}
));

assert_ok!(DdcClusters::set_last_paid_era(&cluster_id, era_id));
assert_ok!(<DdcClusters as ClusterValidator<Test>>::set_last_paid_era(&cluster_id, era_id));

let updated_cluster = DdcClusters::clusters(cluster_id).unwrap();
assert_eq!(updated_cluster.last_paid_era, era_id);
Expand Down
9 changes: 3 additions & 6 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ mod tests;

pub mod migrations;

#[cfg(feature = "runtime-benchmarks")]
use ddc_primitives::BillingReportParams;
use ddc_primitives::{
traits::{
bucket::BucketManager, cluster::ClusterProtocol as ClusterProtocolType,
customer::CustomerCharger as CustomerChargerType, node::NodeManager,
pallet::PalletVisitor as PalletVisitorType, payout::PayoutProcessor,
},
BatchIndex, BucketId, BucketUsage, ClusterId, CustomerCharge, DdcEra, MMRProof, NodePubKey,
NodeUsage, PayoutError, PayoutState, ProviderReward, MAX_PAYOUT_BATCH_COUNT,
MAX_PAYOUT_BATCH_SIZE, MILLICENTS,
BatchIndex, BillingReportParams, BucketId, BucketUsage, ClusterId, CustomerCharge, DdcEra,
MMRProof, NodePubKey, NodeUsage, PayoutError, PayoutState, ProviderReward,
MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, MILLICENTS,
};
use frame_election_provider_support::SortedListProvider;
use frame_support::{
Expand Down Expand Up @@ -1160,7 +1158,6 @@ pub mod pallet {
Ok(None)
}

#[cfg(feature = "runtime-benchmarks")]
fn create_billing_report(vault: T::AccountId, params: BillingReportParams) {
let mut charging_processed_batches =
BoundedBTreeSet::<BatchIndex, MaxBatchesCount>::new();
Expand Down
88 changes: 67 additions & 21 deletions pallets/ddc-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use ddc_primitives::{
ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutProcessor,
ValidatorVisitor,
},
ActivityHash, BatchIndex, BucketUsage, ClusterId, ClusterStatus, DdcEra, EraValidation,
EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage, PayoutState,
ActivityHash, BatchIndex, BillingReportParams, BucketUsage, ClusterId, ClusterStatus, DdcEra,
EraValidation, EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage, PayoutState,
StorageNodeParams,
};
use frame_support::{
Expand Down Expand Up @@ -511,6 +511,8 @@ pub mod pallet {
FailToVerifyMerkleProof,
/// No Era Validation exist
NoEraValidation,
/// Given era is already validated and paid.
EraAlreadyPaid,
}

/// Era validations
Expand Down Expand Up @@ -1272,6 +1274,33 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
pub(crate) fn do_skip_era_validation(
cluster_id: &ClusterId,
era_id: DdcEra,
) -> DispatchResult {
let era_validations = <EraValidations<T>>::get(cluster_id, era_id);

if era_validations.is_none() {
let mut era_validation = EraValidation {
status: EraValidationStatus::PayoutSkipped,
..Default::default()
};

let signed_validators = era_validation
.validators
.entry((ActivityHash::default(), ActivityHash::default()))
.or_insert_with(Vec::new);

let validators = <ValidatorSet<T>>::get();

signed_validators.extend(validators);

<EraValidations<T>>::insert(cluster_id, era_id, era_validation);
}

Ok(())
}

#[allow(clippy::type_complexity)]
pub(crate) fn process_dac_era(
cluster_id: &ClusterId,
Expand Down Expand Up @@ -3952,6 +3981,10 @@ pub mod pallet {
Ok(())
}

/// Set PayoutSkipped state of a given era if it is not validated yet. Otherwise does
/// nothing.
///
/// Emits `EraValidationReady`.
#[pallet::call_index(11)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_era_validations())]
pub fn set_era_validations(
Expand All @@ -3960,31 +3993,44 @@ pub mod pallet {
era_id: DdcEra,
) -> DispatchResult {
ensure_root(origin)?;
let era_validations = <EraValidations<T>>::get(cluster_id, era_id);

if era_validations.is_none() {
let mut era_validation = EraValidation {
payers_merkle_root_hash: ActivityHash::default(),
payees_merkle_root_hash: ActivityHash::default(),
start_era: Default::default(),
end_era: Default::default(),
validators: Default::default(),
status: EraValidationStatus::PayoutSkipped,
};
Self::do_skip_era_validation(&cluster_id, era_id)?;
Self::deposit_event(Event::<T>::EraValidationReady { cluster_id, era_id });

let signed_validators = era_validation
.validators
.entry((ActivityHash::default(), ActivityHash::default()))
.or_insert_with(Vec::new);
Ok(())
}

let validators = <ValidatorSet<T>>::get();
/// Continue DAC validation from an era after a given one. It updates `last_paid_era` of a
/// given cluster, creates an empty billing report with a finalized state, and sets an empty
/// validation result on validators (in case it does not exist yet).
#[pallet::call_index(12)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::skip_dac_validation_to_era())]
pub fn skip_dac_validation_to_era(
origin: OriginFor<T>,
cluster_id: ClusterId,
era_id: DdcEra,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(
era_id > T::ClusterValidator::get_last_paid_era(&cluster_id)?,
Error::<T>::EraAlreadyPaid
);

signed_validators.extend(validators);
Self::do_skip_era_validation(&cluster_id, era_id)?;

<EraValidations<T>>::insert(cluster_id, era_id, era_validation);
}
let billing_report_params = BillingReportParams {
cluster_id,
era: era_id,
state: PayoutState::Finalized,
..Default::default()
};

Self::deposit_event(Event::<T>::EraValidationReady { cluster_id, era_id });
T::PayoutProcessor::create_billing_report(
T::AccountId::decode(&mut [0u8; 32].as_slice()).unwrap(),
billing_report_params,
);

T::ClusterValidator::set_last_paid_era(&cluster_id, era_id)?;

Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion pallets/ddc-verification/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ impl<T: Config> PayoutProcessor<T> for MockPayoutProcessor {
PayoutState::NotInitialized
}

#[cfg(feature = "runtime-benchmarks")]
fn create_billing_report(_vault: T::AccountId, _params: BillingReportParams) {
unimplemented!()
}
Expand Down
13 changes: 13 additions & 0 deletions pallets/ddc-verification/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub trait WeightInfo {
fn end_billing_report() -> Weight;
fn emit_consensus_errors(b: u32, ) -> Weight;
fn set_era_validations() -> Weight;
fn skip_dac_validation_to_era() -> Weight;
}

/// Weights for pallet_ddc_verification using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -225,6 +226,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}

fn skip_dac_validation_to_era() -> Weight {
Weight::from_parts(0_u64, 0)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -410,4 +417,10 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}

fn skip_dac_validation_to_era() -> Weight {
Weight::from_parts(0_u64, 0)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
}
14 changes: 14 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ pub struct EraValidation<T: Config> {
pub status: EraValidationStatus,
}

impl<T: Config> Default for EraValidation<T> {
fn default() -> Self {
EraValidation {
validators: Default::default(),
start_era: Default::default(),
end_era: Default::default(),
payers_merkle_root_hash: Default::default(),
payees_merkle_root_hash: Default::default(),
status: EraValidationStatus::PayoutSkipped,
}
}
}

#[derive(Default)]
pub struct BillingReportParams {
pub cluster_id: ClusterId,
pub era: DdcEra,
Expand Down
7 changes: 2 additions & 5 deletions primitives/src/traits/payout.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use sp_runtime::DispatchResult;

#[cfg(feature = "runtime-benchmarks")]
use crate::BillingReportParams;
use crate::{
BatchIndex, BucketId, BucketUsage, ClusterId, DdcEra, MMRProof, NodePubKey, NodeUsage,
PayoutError, PayoutState,
BatchIndex, BillingReportParams, BucketId, BucketUsage, ClusterId, DdcEra, MMRProof,
NodePubKey, NodeUsage, PayoutError, PayoutState,
};

pub trait PayoutProcessor<T: frame_system::Config> {
Expand Down Expand Up @@ -66,6 +64,5 @@ pub trait PayoutProcessor<T: frame_system::Config> {
era_id: DdcEra,
) -> Result<Option<BatchIndex>, PayoutError>;

#[cfg(feature = "runtime-benchmarks")]
fn create_billing_report(vault: T::AccountId, params: BillingReportParams);
}
2 changes: 1 addition & 1 deletion 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: 61005,
spec_version: 61006,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 23,
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: 61005,
spec_version: 61006,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 23,
Expand Down

0 comments on commit 419f80f

Please sign in to comment.