Skip to content

Commit

Permalink
perbill -> perquantil, payout dust
Browse files Browse the repository at this point in the history
  • Loading branch information
aie0 committed Dec 4, 2023
1 parent fc34dde commit c55d302
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 118 deletions.
8 changes: 4 additions & 4 deletions pallets/ddc-clusters/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use codec::{Decode, Encode};
use ddc_primitives::ClusterId;
use frame_support::{pallet_prelude::*, parameter_types};
use scale_info::TypeInfo;
use sp_runtime::Perbill;
use sp_runtime::Perquintill;

parameter_types! {
pub MaxClusterParamsLen: u16 = 2048;
Expand Down Expand Up @@ -32,9 +32,9 @@ pub struct ClusterParams<AccountId> {
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
#[scale_info(skip_type_params(Balance, BlockNumber, T))]
pub struct ClusterGovParams<Balance, BlockNumber> {
pub treasury_share: Perbill,
pub validators_share: Perbill,
pub cluster_reserve_share: Perbill,
pub treasury_share: Perquintill,
pub validators_share: Perquintill,
pub cluster_reserve_share: Perquintill,
pub cdn_bond_size: Balance,
pub cdn_chill_delay: BlockNumber,
pub cdn_unbonding_delay: BlockNumber,
Expand Down
26 changes: 13 additions & 13 deletions pallets/ddc-clusters/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use frame_support::{assert_noop, assert_ok, error::BadOrigin};
use frame_system::Config;
use hex_literal::hex;
use pallet_ddc_nodes::{CDNNodeParams, NodeParams};
use sp_runtime::{traits::Hash, AccountId32, Perbill};
use sp_runtime::{traits::Hash, AccountId32, Perquintill};

#[test]
fn create_cluster_works() {
ExtBuilder.build_and_execute(|| {
System::set_block_number(1);

let cluster_gov_params = ClusterGovParams {
treasury_share: Perbill::from_float(0.05),
validators_share: Perbill::from_float(0.01),
cluster_reserve_share: Perbill::from_float(0.02),
treasury_share: Perquintill::from_float(0.05),
validators_share: Perquintill::from_float(0.01),
cluster_reserve_share: Perquintill::from_float(0.02),
cdn_bond_size: 100,
cdn_chill_delay: 50,
cdn_unbonding_delay: 50,
Expand Down Expand Up @@ -98,9 +98,9 @@ fn add_and_delete_node_works() {
AccountId::from([2; 32]),
ClusterParams { node_provider_auth_contract: AccountId::from([1; 32]) },
ClusterGovParams {
treasury_share: Perbill::from_float(0.05),
validators_share: Perbill::from_float(0.01),
cluster_reserve_share: Perbill::from_float(0.02),
treasury_share: Perquintill::from_float(0.05),
validators_share: Perquintill::from_float(0.01),
cluster_reserve_share: Perquintill::from_float(0.02),
cdn_bond_size: 100,
cdn_chill_delay: 50,
cdn_unbonding_delay: 50,
Expand Down Expand Up @@ -309,9 +309,9 @@ fn set_cluster_params_works() {
AccountId::from([2; 32]),
ClusterParams { node_provider_auth_contract: AccountId::from([1; 32]) },
ClusterGovParams {
treasury_share: Perbill::from_float(0.05),
validators_share: Perbill::from_float(0.01),
cluster_reserve_share: Perbill::from_float(0.02),
treasury_share: Perquintill::from_float(0.05),
validators_share: Perquintill::from_float(0.01),
cluster_reserve_share: Perquintill::from_float(0.02),
cdn_bond_size: 100,
cdn_chill_delay: 50,
cdn_unbonding_delay: 50,
Expand Down Expand Up @@ -354,9 +354,9 @@ fn set_cluster_gov_params_works() {
System::set_block_number(1);

let cluster_gov_params = ClusterGovParams {
treasury_share: Perbill::from_float(0.05),
validators_share: Perbill::from_float(0.01),
cluster_reserve_share: Perbill::from_float(0.02),
treasury_share: Perquintill::from_float(0.05),
validators_share: Perquintill::from_float(0.01),
cluster_reserve_share: Perquintill::from_float(0.02),
cdn_bond_size: 100,
cdn_chill_delay: 50,
cdn_unbonding_delay: 50,
Expand Down
8 changes: 4 additions & 4 deletions pallets/ddc-customers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
Perbill,
Perquintill,
};

/// The AccountId alias in this test module.
Expand Down Expand Up @@ -143,9 +143,9 @@ impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {

fn get_fees_params(_cluster_id: &ClusterId) -> Result<ClusterFeesParams, ClusterVisitorError> {
Ok(ClusterFeesParams {
treasury_share: Perbill::from_percent(1),
validators_share: Perbill::from_percent(10),
cluster_reserve_share: Perbill::from_percent(2),
treasury_share: Perquintill::from_percent(1),
validators_share: Perquintill::from_percent(10),
cluster_reserve_share: Perquintill::from_percent(2),
})
}

Expand Down
78 changes: 44 additions & 34 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use frame_support::{
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use sp_runtime::{PerThing, Perbill};
use sp_runtime::{PerThing, Perquintill};
use sp_std::prelude::*;

type BatchIndex = u16;
Expand Down Expand Up @@ -89,6 +89,7 @@ pub type BalanceOf<T> =

parameter_types! {
pub MaxBatchesCount: u16 = 1000;
pub MaxDust: u16 = 20000;
}

#[frame_support::pallet]
Expand Down Expand Up @@ -427,8 +428,10 @@ pub mod pallet {
if amount_actually_charged > 0 {
// something was charged and should be added
// calculate ratio
let ratio =
Perbill::from_rational(amount_actually_charged, total_customer_charge);
let ratio = Perquintill::from_rational(
amount_actually_charged,
total_customer_charge,
);

customer_charge.storage = ratio * customer_charge.storage;
customer_charge.transfer = ratio * customer_charge.transfer;
Expand Down Expand Up @@ -654,19 +657,21 @@ pub mod pallet {
.ok_or(Error::<T>::ArithmeticOverflow)?;

let node_provider_id = payee.0;
let reward: BalanceOf<T> = amount_to_reward.saturated_into::<BalanceOf<T>>();

<T as pallet::Config>::Currency::transfer(
&updated_billing_report.vault,
&node_provider_id,
reward,
ExistenceRequirement::AllowDeath,
)?;

updated_billing_report
.total_distributed_reward
.checked_add(amount_to_reward)
.ok_or(Error::<T>::ArithmeticOverflow)?;
if amount_to_reward > 0 {
let reward: BalanceOf<T> = amount_to_reward.saturated_into::<BalanceOf<T>>();

<T as pallet::Config>::Currency::transfer(
&updated_billing_report.vault,
&node_provider_id,
reward,
ExistenceRequirement::AllowDeath,
)?;

updated_billing_report.total_distributed_reward = updated_billing_report
.total_distributed_reward
.checked_add(amount_to_reward)
.ok_or(Error::<T>::ArithmeticOverflow)?;
}

Self::deposit_event(Event::<T>::Rewarded {
cluster_id,
Expand Down Expand Up @@ -708,6 +713,22 @@ pub mod pallet {
&billing_report.rewarding_max_batch_index,
)?;

let expected_amount_to_reward = (|| -> Option<u128> {
billing_report
.total_customer_charge
.transfer
.checked_add(billing_report.total_customer_charge.storage)?
.checked_add(billing_report.total_customer_charge.puts)?
.checked_add(billing_report.total_customer_charge.gets)
})()
.ok_or(Error::<T>::ArithmeticOverflow)?;

ensure!(
expected_amount_to_reward - billing_report.total_distributed_reward <=
MaxDust::get().into(),
Error::<T>::NotDistributedBalance
);

billing_report.state = State::ProvidersRewarded;
ActiveBillingReports::<T>::insert(cluster_id, era, billing_report);

Expand All @@ -729,20 +750,6 @@ pub mod pallet {
.map_err(|_| Error::<T>::BillingReportDoesNotExist)?;

ensure!(billing_report.state == State::ProvidersRewarded, Error::<T>::NotExpectedState);
let expected_amount_to_reward = (|| -> Option<u128> {
billing_report
.total_customer_charge
.transfer
.checked_add(billing_report.total_customer_charge.storage)?
.checked_add(billing_report.total_customer_charge.puts)?
.checked_add(billing_report.total_customer_charge.gets)
})()
.ok_or(Error::<T>::ArithmeticOverflow)?;

ensure!(
expected_amount_to_reward == billing_report.total_distributed_reward,
Error::<T>::NotDistributedBalance
);

billing_report.charging_processed_batches.clear();
billing_report.rewarding_processed_batches.clear();
Expand Down Expand Up @@ -811,20 +818,23 @@ pub mod pallet {
) -> Option<NodeReward> {
let mut node_reward = NodeReward::default();

let mut ratio = Perbill::from_rational(
let mut ratio = Perquintill::from_rational(
node_usage.transferred_bytes,
total_nodes_usage.transferred_bytes,
);

// ratio multiplied by X will be > 0, < X no overflow
node_reward.transfer = ratio * total_customer_charge.transfer;

ratio = Perbill::from_rational(node_usage.stored_bytes, total_nodes_usage.stored_bytes);
ratio = Perquintill::from_rational(node_usage.stored_bytes, total_nodes_usage.stored_bytes);
node_reward.storage = ratio * total_customer_charge.storage;

ratio = Perbill::from_rational(node_usage.number_of_puts, total_nodes_usage.number_of_puts);
ratio =
Perquintill::from_rational(node_usage.number_of_puts, total_nodes_usage.number_of_puts);
node_reward.puts = ratio * total_customer_charge.puts;

ratio = Perbill::from_rational(node_usage.number_of_gets, total_nodes_usage.number_of_gets);
ratio =
Perquintill::from_rational(node_usage.number_of_gets, total_nodes_usage.number_of_gets);
node_reward.gets = ratio * total_customer_charge.gets;

Some(node_reward)
Expand Down
14 changes: 7 additions & 7 deletions pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
DispatchError,
DispatchError, Perquintill,
};
use sp_std::prelude::*;

Expand Down Expand Up @@ -163,15 +163,15 @@ pub const PRICING_PARAMS_ONE: ClusterPricingParams = ClusterPricingParams {
};

pub const PRICING_FEES: ClusterFeesParams = ClusterFeesParams {
treasury_share: Perbill::from_percent(1),
validators_share: Perbill::from_percent(10),
cluster_reserve_share: Perbill::from_percent(2),
treasury_share: Perquintill::from_percent(1),
validators_share: Perquintill::from_percent(10),
cluster_reserve_share: Perquintill::from_percent(2),
};

pub const PRICING_FEES_ZERO: ClusterFeesParams = ClusterFeesParams {
treasury_share: Perbill::from_percent(0),
validators_share: Perbill::from_percent(0),
cluster_reserve_share: Perbill::from_percent(0),
treasury_share: Perquintill::from_percent(0),
validators_share: Perquintill::from_percent(0),
cluster_reserve_share: Perquintill::from_percent(0),
};

pub struct TestTreasuryVisitor;
Expand Down
Loading

0 comments on commit c55d302

Please sign in to comment.