Skip to content

Commit

Permalink
Merge branch 'deployment-0.9.30' into payout-events
Browse files Browse the repository at this point in the history
  • Loading branch information
aie0 authored Nov 28, 2023
2 parents 70932e1 + eef0c94 commit 3b67300
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- 'dev'
- 'deployment-0.9.30'
workflow_dispatch:

env:
Expand Down Expand Up @@ -68,7 +69,7 @@ jobs:
tags: |
${{ steps.login-ecr.outputs.registry }}/pos-network-node:${{ env.GITHUB_SHA }}
${{ steps.login-ecr.outputs.registry }}/pos-network-node:dev-latest
${{ steps.login-ecr.outputs.registry }}/pos-network-node:deployment-0.9.30-latest
- name: Copy wasm artifacts from the image
run: |
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

164 changes: 90 additions & 74 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub mod pallet {

let mut updated_billing_report = billing_report;
for payer in payers {
let customer_charge = get_customer_charge::<T>(cluster_id, &payer.1)?;
let mut customer_charge = get_customer_charge::<T>(cluster_id, &payer.1)?;
let total_customer_charge = (|| -> Option<u128> {
customer_charge
.transfer
Expand All @@ -370,30 +370,6 @@ pub mod pallet {
})()
.ok_or(Error::<T>::ArithmeticOverflow)?;

let temp_total_customer_storage_charge = updated_billing_report
.total_customer_charge
.storage
.checked_add(customer_charge.storage)
.ok_or(Error::<T>::ArithmeticOverflow)?;

let temp_total_customer_transfer_charge = updated_billing_report
.total_customer_charge
.transfer
.checked_add(customer_charge.transfer)
.ok_or(Error::<T>::ArithmeticOverflow)?;

let temp_total_customer_puts_charge = updated_billing_report
.total_customer_charge
.puts
.checked_add(customer_charge.puts)
.ok_or(Error::<T>::ArithmeticOverflow)?;

let temp_total_customer_gets_charge = updated_billing_report
.total_customer_charge
.gets
.checked_add(customer_charge.gets)
.ok_or(Error::<T>::ArithmeticOverflow)?;

let customer_id = payer.0.clone();
let amount_actually_charged = match T::CustomerCharger::charge_content_owner(
customer_id.clone(),
Expand Down Expand Up @@ -426,16 +402,19 @@ pub mod pallet {
customer_id,
amount: total_customer_charge,
});
} else {
updated_billing_report.total_customer_charge.storage =
temp_total_customer_storage_charge;
updated_billing_report.total_customer_charge.transfer =
temp_total_customer_transfer_charge;
updated_billing_report.total_customer_charge.puts =
temp_total_customer_puts_charge;
updated_billing_report.total_customer_charge.gets =
temp_total_customer_gets_charge;

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);

customer_charge.storage = ratio * customer_charge.storage;
customer_charge.transfer = ratio * customer_charge.transfer;
customer_charge.gets = ratio * customer_charge.gets;
customer_charge.puts = ratio * customer_charge.puts;
}
} else {
Self::deposit_event(Event::<T>::Charged {
cluster_id,
era,
Expand All @@ -444,6 +423,30 @@ pub mod pallet {
amount: total_customer_charge,
});
}

updated_billing_report.total_customer_charge.storage = updated_billing_report
.total_customer_charge
.storage
.checked_add(customer_charge.storage)
.ok_or(Error::<T>::ArithmeticOverflow)?;

updated_billing_report.total_customer_charge.transfer = updated_billing_report
.total_customer_charge
.transfer
.checked_add(customer_charge.transfer)
.ok_or(Error::<T>::ArithmeticOverflow)?;

updated_billing_report.total_customer_charge.puts = updated_billing_report
.total_customer_charge
.puts
.checked_add(customer_charge.puts)
.ok_or(Error::<T>::ArithmeticOverflow)?;

updated_billing_report.total_customer_charge.gets = updated_billing_report
.total_customer_charge
.gets
.checked_add(customer_charge.gets)
.ok_or(Error::<T>::ArithmeticOverflow)?;
}

updated_billing_report
Expand Down Expand Up @@ -494,50 +497,59 @@ pub mod pallet {
let validators_fee = fees.validators_share * total_customer_charge;
let cluster_reserve_fee = fees.cluster_reserve_share * total_customer_charge;

charge_treasury_fees::<T>(
treasury_fee,
&billing_report.vault,
&T::TreasuryVisitor::get_account_id(),
)?;
Self::deposit_event(Event::<T>::TreasuryFeesCollected {
cluster_id,
era,
amount: treasury_fee,
});

charge_cluster_reserve_fees::<T>(
cluster_reserve_fee,
&billing_report.vault,
&T::ClusterVisitor::get_reserve_account_id(&cluster_id)
.map_err(|_| Error::<T>::NotExpectedClusterState)?,
)?;
Self::deposit_event(Event::<T>::ClusterReserveFeesCollected {
cluster_id,
era,
amount: cluster_reserve_fee,
});

charge_validator_fees::<T>(validators_fee, &billing_report.vault)?;
Self::deposit_event(Event::<T>::ValidatorFeesCollected {
cluster_id,
era,
amount: validators_fee,
});
if treasury_fee > 0 {
charge_treasury_fees::<T>(
treasury_fee,
&billing_report.vault,
&T::TreasuryVisitor::get_account_id(),
)?;

Self::deposit_event(Event::<T>::TreasuryFeesCollected {
cluster_id,
era,
amount: treasury_fee,
});
}

if cluster_reserve_fee > 0 {
charge_cluster_reserve_fees::<T>(
cluster_reserve_fee,
&billing_report.vault,
&T::ClusterVisitor::get_reserve_account_id(&cluster_id)
.map_err(|_| Error::<T>::NotExpectedClusterState)?,
)?;
Self::deposit_event(Event::<T>::ClusterReserveFeesCollected {
cluster_id,
era,
amount: cluster_reserve_fee,
});
}

if validators_fee > 0 {
charge_validator_fees::<T>(validators_fee, &billing_report.vault)?;
Self::deposit_event(Event::<T>::ValidatorFeesCollected {
cluster_id,
era,
amount: validators_fee,
});
}

// 1 - (X + Y + Z) > 0, 0 < X + Y + Z < 1
let total_left_from_one =
(fees.treasury_share + fees.validators_share + fees.cluster_reserve_share)
.left_from_one();

// X * Z < X, 0 < Z < 1
billing_report.total_customer_charge.transfer =
total_left_from_one * billing_report.total_customer_charge.transfer;
billing_report.total_customer_charge.storage =
total_left_from_one * billing_report.total_customer_charge.storage;
billing_report.total_customer_charge.puts =
total_left_from_one * billing_report.total_customer_charge.puts;
billing_report.total_customer_charge.gets =
total_left_from_one * billing_report.total_customer_charge.gets;
if !total_left_from_one.is_zero() {
// X * Z < X, 0 < Z < 1
billing_report.total_customer_charge.transfer =
total_left_from_one * billing_report.total_customer_charge.transfer;
billing_report.total_customer_charge.storage =
total_left_from_one * billing_report.total_customer_charge.storage;
billing_report.total_customer_charge.puts =
total_left_from_one * billing_report.total_customer_charge.puts;
billing_report.total_customer_charge.gets =
total_left_from_one * billing_report.total_customer_charge.gets;
}

billing_report.state = State::CustomersChargedWithFees;
ActiveBillingReports::<T>::insert(cluster_id, era, billing_report);
Expand Down Expand Up @@ -692,7 +704,7 @@ pub mod pallet {
let caller = ensure_signed(origin)?;
ensure!(Self::authorised_caller() == Some(caller), Error::<T>::Unauthorised);

let billing_report = ActiveBillingReports::<T>::try_get(cluster_id, era)
let mut billing_report = ActiveBillingReports::<T>::try_get(cluster_id, era)
.map_err(|_| Error::<T>::BillingReportDoesNotExist)?;

ensure!(billing_report.state == State::ProvidersRewarded, Error::<T>::NotExpectedState);
Expand All @@ -711,7 +723,11 @@ pub mod pallet {
Error::<T>::NotDistributedBalance
);

ActiveBillingReports::<T>::remove(cluster_id, era);
billing_report.charging_processed_batches.clear();
billing_report.rewarding_processed_batches.clear();
billing_report.state = State::Finalized;

ActiveBillingReports::<T>::insert(cluster_id, era, billing_report);
Self::deposit_event(Event::<T>::BillingReportFinalized { cluster_id, era });

Ok(())
Expand Down
27 changes: 22 additions & 5 deletions pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<T: Config> CustomerCharger<T> for TestCustomerCharger {

let mut amount_to_charge = amount;
if amount_to_charge < 50_000_000 {
amount_to_charge = PARTIAL_CHARGE;
amount_to_charge = PARTIAL_CHARGE; // for user 3
}

let charge = amount_to_charge.saturated_into::<BalanceOf<T>>();
Expand All @@ -139,6 +139,9 @@ pub const VALIDATOR1_ACCOUNT_ID: AccountId = 111;
pub const VALIDATOR2_ACCOUNT_ID: AccountId = 222;
pub const VALIDATOR3_ACCOUNT_ID: AccountId = 333;
pub const PARTIAL_CHARGE: u128 = 100;
pub const USER3_BALANCE: u128 = 1000;

pub const FREE_CLUSTER_ID: ClusterId = ClusterId::zero();

pub const PRICING_PARAMS: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
Expand All @@ -153,6 +156,12 @@ pub const PRICING_FEES: ClusterFeesParams = ClusterFeesParams {
cluster_reserve_share: Perbill::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),
};

pub struct TestTreasuryVisitor;
impl<T: frame_system::Config> PalletVisitor<T> for TestTreasuryVisitor {
fn get_account_id() -> T::AccountId {
Expand Down Expand Up @@ -230,6 +239,14 @@ 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 {
Ok(PRICING_FEES_ZERO)
} else {
Ok(PRICING_FEES)
}
}

pub struct TestClusterVisitor;
impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {
fn cluster_has_node(_cluster_id: &ClusterId, _node_pub_key: &NodePubKey) -> bool {
Expand Down Expand Up @@ -263,8 +280,8 @@ impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {
Ok(PRICING_PARAMS)
}

fn get_fees_params(_cluster_id: &ClusterId) -> Result<ClusterFeesParams, ClusterVisitorError> {
Ok(PRICING_FEES)
fn get_fees_params(cluster_id: &ClusterId) -> Result<ClusterFeesParams, ClusterVisitorError> {
get_fees(cluster_id)
}

fn get_reserve_account_id(
Expand All @@ -287,8 +304,8 @@ impl ExtBuilder {
let _ = pallet_balances::GenesisConfig::<Test> {
balances: vec![
(1, 1000000000000000000000000),
(2, 10), // < PARTIAL_CHARGE
(3, 1000), // > PARTIAL_CHARGE
(2, 10), // < PARTIAL_CHARGE
(3, USER3_BALANCE), // > PARTIAL_CHARGE
(4, 1000000000000000000000000),
],
}
Expand Down
Loading

0 comments on commit 3b67300

Please sign in to comment.