Skip to content

Commit

Permalink
ocw + payout step1 (#377)
Browse files Browse the repository at this point in the history
## Description
<!-- Describe what change this PR is implementing -->

## Types of Changes
Please select the branch type you are merging and fill in the relevant
template.
<!--- Check the following box with an x if the following applies: -->
- [ ] Hotfix
- [ ] Release
- [x] Fix or Feature

## Fix or Feature
<!--- Check the following box with an x if the following applies: -->

### Types of Changes
<!--- What types of changes does your code introduce? -->
- [x] Tech Debt (Code improvements)
- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Dependency upgrade (A change in substrate or any 3rd party crate
version)

### Migrations and Hooks
<!--- Check the following box with an x if the following applies: -->
- [ ] This change requires a runtime migration.
- [ ] Modifies `on_initialize`
- [ ] Modifies `on_finalize`

### Checklist for Fix or Feature
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [x] Change has been tested locally.
- [x] Change adds / updates tests if applicable.
- [ ] Changelog doc updated.
- [ ] `spec_version` has been incremented.
- [ ] `network-relayer`'s
[events](https://github.com/Cerebellum-Network/network-relayer/blob/dev-cere/shared/substrate/events.go)
have been updated according to the blockchain events if applicable.
- [x] All CI checks have been passed successfully

## Checklist for Hotfix
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [ ] Changelog has been updated.
- [ ] Crate version has been updated.
- [ ] `spec_version` has been incremented.
- [ ] Transaction version has been updated if required.
- [ ] Pull Request to `dev` has been created.
- [ ] Pull Request to `staging` has been created.
- [ ] `network-relayer`'s
[events](https://github.com/Cerebellum-Network/network-relayer/blob/dev-cere/shared/substrate/events.go)
have been updated according to the blockchain events if applicable.
- [ ] All CI checks have been passed successfully

## Checklist for Release
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [ ] Change has been deployed to Devnet.
- [ ] Change has been tested in Devnet.
- [ ] Change has been deployed to Qanet.
- [ ] Change has been tested in Qanet.
- [ ] Change has been deployed to Testnet.
- [ ] Change has been tested in Testnet.
- [ ] Changelog has been updated.
- [ ] Crate version has been updated.
- [ ] Spec version has been updated.
- [ ] Transaction version has been updated if required.
- [ ] All CI checks have been passed successfully
  • Loading branch information
aie0 authored Jun 27, 2024
1 parent faf8a4a commit b2cee07
Show file tree
Hide file tree
Showing 10 changed files with 710 additions and 44 deletions.
5 changes: 3 additions & 2 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub mod pallet {
/// Bucket with specific id created
BucketCreated { cluster_id: ClusterId, bucket_id: BucketId },
/// Bucket with specific id updated
BucketUpdated { bucket_id: BucketId },
BucketUpdated { cluster_id: ClusterId, bucket_id: BucketId },
/// Bucket nodes usage with specific id updated
BucketTotalNodesUsageUpdated {
cluster_id: ClusterId,
Expand Down Expand Up @@ -532,8 +532,9 @@ pub mod pallet {
ensure!(bucket.owner_id == owner, Error::<T>::NotBucketOwner);

bucket.is_public = bucket_params.is_public;
let cluster_id = bucket.cluster_id;
<Buckets<T>>::insert(bucket_id, bucket);
Self::deposit_event(Event::<T>::BucketUpdated { bucket_id });
Self::deposit_event(Event::<T>::BucketUpdated { cluster_id, bucket_id });

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/ddc-customers/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ fn set_bucket_params_works() {

// Checking that event was emitted
assert_eq!(System::events().len(), 2);
System::assert_last_event(Event::BucketUpdated { bucket_id }.into());
System::assert_last_event(Event::BucketUpdated { cluster_id, bucket_id }.into());
})
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/ddc-payouts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ benchmarks! {
(provider, bucket_id, node_usage)
}).collect();

}: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payees)
}: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payees,0, vec![], (0, ActivityHash::default()))
verify {
assert!(ActiveBillingReports::<T>::contains_key(cluster_id, era));
let billing_report = ActiveBillingReports::<T>::get(cluster_id, era).unwrap();
Expand Down
140 changes: 137 additions & 3 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// PayoutProcessor trait
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::begin_billing_report())]
pub fn begin_billing_report(
Expand Down Expand Up @@ -340,6 +342,8 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// PayoutProcessor trait
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::begin_charging_customers())]
pub fn begin_charging_customers(
Expand Down Expand Up @@ -367,9 +371,12 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// + pass values by reference PayoutProcessor trait
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::send_charging_customers_batch(payers.len().saturated_into()))]
#[allow(clippy::too_many_arguments)] // todo! need to refactor this
// todo! remove clippy::too_many_arguments
#[allow(clippy::too_many_arguments)]
pub fn send_charging_customers_batch(
origin: OriginFor<T>,
cluster_id: ClusterId,
Expand Down Expand Up @@ -544,6 +551,8 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// PayoutProcessor trait
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::end_charging_customers())]
pub fn end_charging_customers(
Expand Down Expand Up @@ -646,6 +655,8 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// PayoutProcessor trait
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::begin_rewarding_providers())]
pub fn begin_rewarding_providers(
Expand Down Expand Up @@ -678,14 +689,21 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// + pass values by reference PayoutProcessor trait
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::send_rewarding_providers_batch(payees.len().saturated_into()))]
// todo! remove clippy::too_many_arguments
#[allow(clippy::too_many_arguments)]
pub fn send_rewarding_providers_batch(
origin: OriginFor<T>,
cluster_id: ClusterId,
era: DdcEra,
batch_index: BatchIndex,
payees: Vec<(T::AccountId, BucketId, NodeUsage)>,
mmr_size: u64,
proof: Vec<ActivityHash>,
leaf_with_position: (u64, ActivityHash),
) -> DispatchResult {
let caller = ensure_signed(origin)?;
ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::<T>::Unauthorised);
Expand Down Expand Up @@ -717,7 +735,8 @@ pub mod pallet {
era,
batch_index,
&payees,
&[] // todo! pass from newly added input
MerkleProof::new(mmr_size, proof),
leaf_with_position
),
Error::<T>::BatchValidationFailed
);
Expand Down Expand Up @@ -802,6 +821,8 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// PayoutProcessor trait
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::end_rewarding_providers())]
pub fn end_rewarding_providers(
Expand Down Expand Up @@ -853,6 +874,8 @@ pub mod pallet {
Ok(())
}

// todo! remove extrensics from payout pallet and factor the extrensics implementation into
// PayoutProcessor trait
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::end_billing_report())]
pub fn end_billing_report(
Expand Down Expand Up @@ -1081,7 +1104,118 @@ pub mod pallet {
}

impl<T: Config> PayoutVisitor<T> for Pallet<T> {
fn get_billing_report_status(cluster_id: ClusterId, era: DdcEra) -> PayoutState {
fn begin_billing_report(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
start_era: i64,
end_era: i64,
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::begin_billing_report(origin, cluster_id, era_id, start_era, end_era)
}

fn begin_charging_customers(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
max_batch_index: BatchIndex,
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::begin_charging_customers(origin, cluster_id, era_id, max_batch_index)
}

fn send_charging_customers_batch(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
batch_index: BatchIndex,
payers: Vec<(T::AccountId, BucketId, CustomerUsage)>,
mmr_size: u64,
proof: Vec<ActivityHash>,
leaf_with_position: (u64, ActivityHash),
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::send_charging_customers_batch(
origin,
cluster_id,
era_id,
batch_index,
payers,
mmr_size,
proof,
leaf_with_position,
)
}

fn end_charging_customers(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::end_charging_customers(origin, cluster_id, era_id)
}

fn begin_rewarding_providers(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
max_batch_index: BatchIndex,
total_node_usage: NodeUsage,
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::begin_rewarding_providers(
origin,
cluster_id,
era_id,
max_batch_index,
total_node_usage,
)
}

fn send_rewarding_providers_batch(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
batch_index: BatchIndex,
payees: Vec<(T::AccountId, BucketId, NodeUsage)>,
mmr_size: u64,
proof: Vec<ActivityHash>,
leaf_with_position: (u64, ActivityHash),
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::send_rewarding_providers_batch(
origin,
cluster_id,
era_id,
batch_index,
payees,
mmr_size,
proof,
leaf_with_position,
)
}

fn end_rewarding_providers(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::end_rewarding_providers(origin, cluster_id, era_id)
}

fn end_billing_report(
origin: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
) -> DispatchResult {
let origin = frame_system::RawOrigin::Signed(origin).into();
Self::end_billing_report(origin, cluster_id, era_id)
}

fn get_billing_report_status(cluster_id: &ClusterId, era: DdcEra) -> PayoutState {
let billing_report = ActiveBillingReports::<T>::get(cluster_id, era);

match billing_report {
Expand Down
3 changes: 2 additions & 1 deletion pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ where
_era: DdcEra,
_batch_index: BatchIndex,
_payees: &[(T::AccountId, BucketId, NodeUsage)],
_adjacent_hashes: &[ActivityHash],
_proof: MerkleProof<ActivityHash, MergeActivityHash>,
_leaf_with_position: (u64, ActivityHash),
) -> bool {
true
}
Expand Down
Loading

0 comments on commit b2cee07

Please sign in to comment.