Skip to content

Commit

Permalink
zero fees tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aie0 committed Nov 27, 2023
1 parent 8d6c7c4 commit 064a11a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
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.

20 changes: 18 additions & 2 deletions pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ 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,
unit_per_mb_stored: 3_000_000,
Expand All @@ -154,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 @@ -231,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 @@ -264,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 Down
111 changes: 111 additions & 0 deletions pallets/ddc-payouts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,117 @@ fn end_charging_customers_works() {
})
}

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

let dac_account = 123u64;
let user1 = 1u64;
let cluster_id = ClusterId::zero();
let era = 100;
let max_batch_index = 0;
let batch_index = 0;
let usage1 = CustomerUsage {
transferred_bytes: 23452345,
stored_bytes: 3345234523,
number_of_puts: 4456456345234523,
number_of_gets: 523423,
};
let payers = vec![(user1, usage1.clone())];

assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account));

assert_ok!(DdcPayouts::begin_billing_report(
RuntimeOrigin::signed(dac_account),
cluster_id,
era,
));

assert_ok!(DdcPayouts::begin_charging_customers(
RuntimeOrigin::signed(dac_account),
cluster_id,
era,
max_batch_index,
));

assert_ok!(DdcPayouts::send_charging_customers_batch(
RuntimeOrigin::signed(dac_account),
cluster_id,
era,
batch_index,
payers,
));

let report_before = DdcPayouts::active_billing_reports(cluster_id, era).unwrap();
let charge = calculate_charge(usage1);
System::assert_last_event(
Event::Charged { cluster_id, era, customer_id: user1, amount: charge }.into(),
);

let mut balance = Balances::free_balance(DdcPayouts::sub_account_id(cluster_id, era));
assert_eq!(balance, charge);
assert_eq!(System::events().len(), 4 + 3); // 3 for Currency::transfer

assert_ok!(DdcPayouts::end_charging_customers(
RuntimeOrigin::signed(dac_account),
cluster_id,
era,
));

System::assert_has_event(Event::ChargingFinished { cluster_id, era }.into());
assert_eq!(System::events().len(), 7 + 1);

let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap();
assert_eq!(report_after.state, State::CustomersChargedWithFees);

let fees = get_fees(&cluster_id).unwrap();

let total_left_from_one = (fees.treasury_share +
fees.validators_share +
fees.cluster_reserve_share)
.left_from_one();

assert_eq!(total_left_from_one, Perbill::one());

assert_eq!(fees.treasury_share, Perbill::zero());
assert_eq!(fees.validators_share, Perbill::zero());
assert_eq!(fees.cluster_reserve_share, Perbill::zero());

balance = Balances::free_balance(TREASURY_ACCOUNT_ID);
assert_eq!(balance, 0);

balance = Balances::free_balance(RESERVE_ACCOUNT_ID);
assert_eq!(balance, 0);

balance = Balances::free_balance(VALIDATOR1_ACCOUNT_ID);
assert_eq!(balance, 0);

balance = Balances::free_balance(VALIDATOR2_ACCOUNT_ID);
assert_eq!(balance, 0);

balance = Balances::free_balance(VALIDATOR3_ACCOUNT_ID);
assert_eq!(balance, 0);

assert_eq!(
report_after.total_customer_charge.transfer,
total_left_from_one * report_before.total_customer_charge.transfer
);
assert_eq!(
report_after.total_customer_charge.storage,
total_left_from_one * report_before.total_customer_charge.storage
);
assert_eq!(
report_after.total_customer_charge.puts,
total_left_from_one * report_before.total_customer_charge.puts
);
assert_eq!(
report_after.total_customer_charge.gets,
total_left_from_one * report_before.total_customer_charge.gets
);
})
}

#[test]
fn begin_rewarding_providers_fails_uninitialised() {
ExtBuilder.build_and_execute(|| {
Expand Down
2 changes: 2 additions & 0 deletions traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] }
ddc-primitives = { path = "../primitives", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }
4 changes: 4 additions & 0 deletions traits/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use codec::{Decode, Encode};
use ddc_primitives::{ClusterFeesParams, ClusterId, ClusterPricingParams, NodePubKey, NodeType};
use frame_system::Config;
use sp_runtime::RuntimeDebug;
use scale_info::TypeInfo;

pub trait ClusterVisitor<T: Config> {
fn cluster_has_node(cluster_id: &ClusterId, node_pub_key: &NodePubKey) -> bool;
Expand Down Expand Up @@ -30,6 +33,7 @@ pub trait ClusterVisitor<T: Config> {
) -> Result<T::BlockNumber, ClusterVisitorError>;
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub enum ClusterVisitorError {
ClusterDoesNotExist,
ClusterGovParamsNotSet,
Expand Down
5 changes: 5 additions & 0 deletions traits/src/customer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use codec::{Decode, Encode};
use sp_runtime::RuntimeDebug;
use scale_info::TypeInfo;

pub trait CustomerCharger<T: frame_system::Config> {
fn charge_content_owner(
content_owner: T::AccountId,
Expand All @@ -6,6 +10,7 @@ pub trait CustomerCharger<T: frame_system::Config> {
) -> Result<u128, CustomerChargerError>;
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub enum CustomerChargerError {
NotOwner,
ArithmeticUnderflow,
Expand Down

0 comments on commit 064a11a

Please sign in to comment.