Skip to content

Commit

Permalink
Feature/token utility events (#331)
Browse files Browse the repository at this point in the history
## Description
<!-- Describe what change this PR is implementing -->
[Link](Cerebellum-Network/network-relayer#180)
to Network Relayer companion PR

## 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? -->
- [ ] Tech Debt (Code improvements)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] 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.
- [x] Changelog doc updated.

## Checklist for Hotfix
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [ ] 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.
- [ ] Pull Request to `dev` has been created.
- [ ] Pull Request to `staging` has been created.

## 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.

---------

Co-authored-by: Vic Genin <victor.genin@live.com>
Co-authored-by: aie0 <149175774+aie0@users.noreply.github.com>
  • Loading branch information
3 people authored May 24, 2024
1 parent 1c1576b commit d97d5c7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
10 changes: 7 additions & 3 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ Please select the branch type you are merging and fill in the relevant template.
- [ ] Change has been tested locally.
- [ ] 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.
- [ ] All CI checks have been passed successfully

## Checklist for Hotfix
<!--- All boxes need to be checked. Follow this checklist before requiring PR review -->
- [ ] 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.
- [ ] `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 -->
Expand All @@ -54,3 +57,4 @@ Please select the branch type you are merging and fill in the relevant template.
- [ ] Crate version has been updated.
- [ ] Spec version has been updated.
- [ ] Transaction version has been updated if required.
- [ ] All CI checks have been passed successfully
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [C,D] Introduce new events to the DDC Payouts Pallet
- [C,D] `pallet-ddc-clusters-gov`: Introduction of the Cluster Governance pallet for managing clusters protocol parameters.

## [5.3.0]

### Changed

- [C,D] Updated Substrate to polkadot-v1.1.0
- [C,D] Introduction of the OpenGov
- [C,D] `pallet-ddc-clusters`: Added Erasure coding and Replication in cluster params
Expand Down
29 changes: 26 additions & 3 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,24 @@ pub mod pallet {
cluster_id: ClusterId,
era: DdcEra,
},
Rewarded {
ProviderRewarded {
cluster_id: ClusterId,
era: DdcEra,
batch_index: BatchIndex,
stored_bytes: u64,
transferred_bytes: u64,
number_of_puts: u64,
number_of_gets: u64,
node_provider_id: T::AccountId,
rewarded: u128,
expected_to_reward: u128,
},
ValidatorRewarded {
cluster_id: ClusterId,
era: DdcEra,
validator_id: T::AccountId,
amount: u128,
},
NotDistributedReward {
cluster_id: ClusterId,
era: DdcEra,
Expand Down Expand Up @@ -632,7 +642,7 @@ pub mod pallet {
}

if validators_fee > 0 {
charge_validator_fees::<T>(validators_fee, &billing_report.vault)?;
charge_validator_fees::<T>(validators_fee, &billing_report.vault, cluster_id, era)?;
Self::deposit_event(Event::<T>::ValidatorFeesCollected {
cluster_id,
era,
Expand Down Expand Up @@ -785,10 +795,14 @@ pub mod pallet {
.ok_or(Error::<T>::ArithmeticOverflow)?;
}

Self::deposit_event(Event::<T>::Rewarded {
Self::deposit_event(Event::<T>::ProviderRewarded {
cluster_id,
era,
batch_index,
stored_bytes: payee.1.stored_bytes,
transferred_bytes: payee.1.transferred_bytes,
number_of_puts: payee.1.number_of_puts,
number_of_gets: payee.1.number_of_gets,
node_provider_id,
rewarded: reward_,
expected_to_reward: amount_to_reward,
Expand Down Expand Up @@ -935,6 +949,8 @@ pub mod pallet {
fn charge_validator_fees<T: Config>(
validators_fee: u128,
vault: &T::AccountId,
cluster_id: ClusterId,
era: DdcEra,
) -> DispatchResult {
let stakers = get_current_exposure_ratios::<T>()?;

Expand All @@ -947,6 +963,13 @@ pub mod pallet {
amount_to_deduct.saturated_into::<BalanceOf<T>>(),
ExistenceRequirement::AllowDeath,
)?;

pallet::Pallet::deposit_event(Event::<T>::ValidatorRewarded {
cluster_id,
era,
validator_id: staker_id.clone(),
amount: amount_to_deduct,
});
}

Ok(())
Expand Down
62 changes: 53 additions & 9 deletions pallets/ddc-payouts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ fn end_charging_customers_works() {
Event::ValidatorFeesCollected { cluster_id, era, amount: validator_fee }.into(),
);

let transfers = 3 + 3 + 3 * 3; // for Currency::transfer
let transfers = 3 + 3 + 3 + 3 * 3; // for Currency::transfer
assert_eq!(System::events().len(), 5 + 1 + 3 + transfers);

let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap();
Expand All @@ -2531,31 +2531,63 @@ fn end_charging_customers_works() {
.left_from_one();

balance = Balances::free_balance(TREASURY_ACCOUNT_ID);
assert_eq!(balance, get_fees(&cluster_id).treasury_share * charge);
let mut expected_fees = get_fees(&cluster_id).treasury_share * charge;
assert_eq!(balance, expected_fees);

balance = Balances::free_balance(RESERVE_ACCOUNT_ID);
assert_eq!(balance, get_fees(&cluster_id).cluster_reserve_share * charge);
expected_fees = get_fees(&cluster_id).cluster_reserve_share * charge;
assert_eq!(balance, expected_fees);

balance = Balances::free_balance(VALIDATOR1_ACCOUNT_ID);
let mut ratio = Perquintill::from_rational(
VALIDATOR1_SCORE,
VALIDATOR1_SCORE + VALIDATOR2_SCORE + VALIDATOR3_SCORE,
);
assert_eq!(balance, get_fees(&cluster_id).validators_share * ratio * charge);
expected_fees = get_fees(&cluster_id).validators_share * ratio * charge;
assert_eq!(balance, expected_fees);
System::assert_has_event(
Event::ValidatorRewarded {
cluster_id,
era,
validator_id: VALIDATOR1_ACCOUNT_ID,
amount: expected_fees,
}
.into(),
);

balance = Balances::free_balance(VALIDATOR2_ACCOUNT_ID);
ratio = Perquintill::from_rational(
VALIDATOR2_SCORE,
VALIDATOR1_SCORE + VALIDATOR2_SCORE + VALIDATOR3_SCORE,
);
assert_eq!(balance, get_fees(&cluster_id).validators_share * ratio * charge);
expected_fees = get_fees(&cluster_id).validators_share * ratio * charge;
assert_eq!(balance, expected_fees);
System::assert_has_event(
Event::ValidatorRewarded {
cluster_id,
era,
validator_id: VALIDATOR2_ACCOUNT_ID,
amount: expected_fees,
}
.into(),
);

balance = Balances::free_balance(VALIDATOR3_ACCOUNT_ID);
ratio = Perquintill::from_rational(
VALIDATOR3_SCORE,
VALIDATOR1_SCORE + VALIDATOR2_SCORE + VALIDATOR3_SCORE,
);
assert_eq!(balance, get_fees(&cluster_id).validators_share * ratio * charge);
expected_fees = get_fees(&cluster_id).validators_share * ratio * charge;
assert_eq!(balance, expected_fees);
System::assert_has_event(
Event::ValidatorRewarded {
cluster_id,
era,
validator_id: VALIDATOR3_ACCOUNT_ID,
amount: expected_fees,
}
.into(),
);

assert_eq!(
report_after.total_customer_charge.transfer,
Expand Down Expand Up @@ -3216,11 +3248,15 @@ fn send_rewarding_providers_batch_works() {
let mut report_reward = DdcPayouts::active_billing_reports(cluster_id, era).unwrap();

System::assert_has_event(
Event::Rewarded {
Event::ProviderRewarded {
cluster_id,
era,
node_provider_id: node1,
batch_index: batch_node_index,
stored_bytes: node_usage1.stored_bytes,
transferred_bytes: node_usage1.transferred_bytes,
number_of_puts: node_usage1.number_of_puts,
number_of_gets: node_usage1.number_of_gets,
rewarded: balance_node1,
expected_to_reward: balance_node1,
}
Expand Down Expand Up @@ -3254,11 +3290,15 @@ fn send_rewarding_providers_batch_works() {
assert_eq!(report_reward.total_distributed_reward, balance_node1 + balance_node2);

System::assert_has_event(
Event::Rewarded {
Event::ProviderRewarded {
cluster_id,
era,
node_provider_id: node2,
batch_index: batch_node_index,
stored_bytes: node_usage2.stored_bytes,
transferred_bytes: node_usage2.transferred_bytes,
number_of_puts: node_usage2.number_of_puts,
number_of_gets: node_usage2.number_of_gets,
rewarded: balance_node2,
expected_to_reward: balance_node2,
}
Expand Down Expand Up @@ -3301,11 +3341,15 @@ fn send_rewarding_providers_batch_works() {
assert_eq!(balance_node3, transfer_charge + storage_charge + puts_charge + gets_charge);

System::assert_has_event(
Event::Rewarded {
Event::ProviderRewarded {
cluster_id,
era,
node_provider_id: node3,
batch_index: batch_node_index + 1,
stored_bytes: node_usage3.stored_bytes,
transferred_bytes: node_usage3.transferred_bytes,
number_of_puts: node_usage3.number_of_puts,
number_of_gets: node_usage3.number_of_gets,
rewarded: balance_node3,
expected_to_reward: balance_node3,
}
Expand Down

0 comments on commit d97d5c7

Please sign in to comment.