Skip to content

Commit

Permalink
Fees batches fixes (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
aie0 authored Dec 4, 2023
1 parent 8155a21 commit d36ae2c
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 146 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
86 changes: 48 additions & 38 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 @@ -378,20 +379,20 @@ pub mod pallet {
.ok_or(Error::<T>::ArithmeticOverflow)?;

let customer_id = payer.0.clone();
let amount_actually_charged = T::CustomerCharger::charge_content_owner(
/*let amount_actually_charged = T::CustomerCharger::charge_content_owner(
customer_id.clone(),
updated_billing_report.vault.clone(),
total_customer_charge,
)?;
/*
)?;*/

let amount_actually_charged = match T::CustomerCharger::charge_content_owner(
customer_id.clone(),
updated_billing_report.vault.clone(),
total_customer_charge,
) {
Ok(actually_charged) => actually_charged,
Err(_e) => 0,
}; */
};

if amount_actually_charged < total_customer_charge {
// debt
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
41 changes: 29 additions & 12 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 @@ -117,7 +117,10 @@ impl<T: Config> CustomerCharger<T> for TestCustomerCharger {
ensure!(amount > 1_000_000, DispatchError::BadOrigin); // any error will do

let mut amount_to_charge = amount;
if amount_to_charge < 50_000_000 {
let temp = ACCOUNT_ID_5.to_ne_bytes();
let account_5 = T::AccountId::decode(&mut &temp[..]).unwrap();

if amount_to_charge < 50_000_000 && content_owner != account_5 {
amount_to_charge = PARTIAL_CHARGE; // for user 3
}

Expand All @@ -133,6 +136,7 @@ impl<T: Config> CustomerCharger<T> for TestCustomerCharger {
}
}

pub const ACCOUNT_ID_5: AccountId = 5;
pub const RESERVE_ACCOUNT_ID: AccountId = 999;
pub const TREASURY_ACCOUNT_ID: AccountId = 888;
pub const VALIDATOR1_ACCOUNT_ID: AccountId = 111;
Expand All @@ -142,6 +146,7 @@ pub const PARTIAL_CHARGE: u128 = 100;
pub const USER3_BALANCE: u128 = 1000;

pub const FREE_CLUSTER_ID: ClusterId = ClusterId::zero();
pub const ONE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(5u8);

pub const PRICING_PARAMS: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
Expand All @@ -150,16 +155,23 @@ pub const PRICING_PARAMS: ClusterPricingParams = ClusterPricingParams {
unit_per_get_request: 5_000_000,
};

pub const PRICING_PARAMS_ONE: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 10_000_000_000,
unit_per_mb_stored: 10_000_000_000,
unit_per_put_request: 10_000_000_000,
unit_per_get_request: 10_000_000_000,
};

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 Expand Up @@ -240,7 +252,7 @@ impl<T: frame_system::Config> SortedListProvider<T::AccountId> for TestValidator
}

pub fn get_fees(cluster_id: &ClusterId) -> Result<ClusterFeesParams, ClusterVisitorError> {
if *cluster_id == FREE_CLUSTER_ID {
if *cluster_id == FREE_CLUSTER_ID || *cluster_id == ONE_CLUSTER_ID {
Ok(PRICING_FEES_ZERO)
} else {
Ok(PRICING_FEES)
Expand Down Expand Up @@ -275,9 +287,13 @@ impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {
}

fn get_pricing_params(
_cluster_id: &ClusterId,
cluster_id: &ClusterId,
) -> Result<ClusterPricingParams, ClusterVisitorError> {
Ok(PRICING_PARAMS)
if *cluster_id == FREE_CLUSTER_ID || *cluster_id == ONE_CLUSTER_ID {
Ok(PRICING_PARAMS_ONE)
} else {
Ok(PRICING_PARAMS)
}
}

fn get_fees_params(cluster_id: &ClusterId) -> Result<ClusterFeesParams, ClusterVisitorError> {
Expand All @@ -303,10 +319,11 @@ impl ExtBuilder {

let _ = pallet_balances::GenesisConfig::<Test> {
balances: vec![
(1, 1000000000000000000000000),
(1, 10000000000000000000000000000),
(2, 10), // < PARTIAL_CHARGE
(3, USER3_BALANCE), // > PARTIAL_CHARGE
(4, 1000000000000000000000000),
(5, 1000000000000000000000000),
],
}
.assimilate_storage(&mut storage);
Expand Down
Loading

0 comments on commit d36ae2c

Please sign in to comment.