diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index af50c033b..02eba8dce 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -378,12 +378,12 @@ pub mod pallet { .ok_or(Error::::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(), @@ -391,7 +391,7 @@ pub mod pallet { ) { Ok(actually_charged) => actually_charged, Err(_e) => 0, - }; */ + }; if amount_actually_charged < total_customer_charge { // debt diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 25e6b443c..86d2581dc 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -117,7 +117,10 @@ impl CustomerCharger 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 } @@ -133,6 +136,7 @@ impl CustomerCharger 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; @@ -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, @@ -150,6 +155,13 @@ 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), @@ -240,7 +252,7 @@ impl SortedListProvider for TestValidator } pub fn get_fees(cluster_id: &ClusterId) -> Result { - 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) @@ -275,9 +287,13 @@ impl ClusterVisitor for TestClusterVisitor { } fn get_pricing_params( - _cluster_id: &ClusterId, + cluster_id: &ClusterId, ) -> Result { - 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 { @@ -303,10 +319,11 @@ impl ExtBuilder { let _ = pallet_balances::GenesisConfig:: { balances: vec![ - (1, 1000000000000000000000000), + (1, 10000000000000000000000000000), (2, 10), // < PARTIAL_CHARGE (3, USER3_BALANCE), // > PARTIAL_CHARGE (4, 1000000000000000000000000), + (5, 1000000000000000000000000), ], } .assimilate_storage(&mut storage); diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 1e2ea13f2..9fc491601 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -257,24 +257,31 @@ fn send_charging_customers_batch_fails_uninitialised() { }) } -fn calculate_charge_parts(usage: CustomerUsage) -> CustomerCharge { +fn calculate_charge_parts(cluster_id: ClusterId, usage: CustomerUsage) -> CustomerCharge { + let pricing_params = + if cluster_id == FREE_CLUSTER_ID || cluster_id == ONE_CLUSTER_ID { + PRICING_PARAMS_ONE + } else { + PRICING_PARAMS + }; + CustomerCharge { - transfer: PRICING_PARAMS.unit_per_mb_streamed * (usage.transferred_bytes as u128) / + transfer: pricing_params.unit_per_mb_streamed * (usage.transferred_bytes as u128) / byte_unit::MEBIBYTE, - storage: (PRICING_PARAMS.unit_per_mb_stored * usage.stored_bytes as u128) / + storage: (pricing_params.unit_per_mb_stored * usage.stored_bytes as u128) / byte_unit::MEBIBYTE, - puts: PRICING_PARAMS.unit_per_put_request * usage.number_of_puts, - gets: PRICING_PARAMS.unit_per_get_request * usage.number_of_gets, + puts: pricing_params.unit_per_put_request * usage.number_of_puts, + gets: pricing_params.unit_per_get_request * usage.number_of_gets, } } -fn calculate_charge(usage: CustomerUsage) -> u128 { - let charge = calculate_charge_parts(usage); +fn calculate_charge(cluster_id: ClusterId, usage: CustomerUsage) -> u128 { + let charge = calculate_charge_parts(cluster_id, usage); charge.transfer + charge.storage + charge.puts + charge.gets } #[test] -fn send_charging_customers_batch_works() { +fn send_charging_customers_batch_works1() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); @@ -343,17 +350,17 @@ fn send_charging_customers_batch_works() { payers1, )); - let usage4_charge = calculate_charge(usage4.clone()); + let usage4_charge = calculate_charge(cluster_id, usage4.clone()); let mut balance = Balances::free_balance(DdcPayouts::sub_account_id(cluster_id, era)); assert_eq!(balance, usage4_charge); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor).unwrap(); - let mut debt = calculate_charge(usage2.clone()); + let mut debt = calculate_charge(cluster_id, usage2.clone()); assert_eq!(user2_debt, debt); let mut report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); - let charge2 = calculate_charge_parts(usage2); - let charge4 = calculate_charge_parts(usage4); + let charge2 = calculate_charge_parts(cluster_id, usage2); + let charge4 = calculate_charge_parts(cluster_id, usage4); assert_eq!(charge2.puts + charge4.puts, report.total_customer_charge.puts); assert_eq!(charge2.gets + charge4.gets, report.total_customer_charge.gets); assert_eq!(charge2.storage + charge4.storage, report.total_customer_charge.storage); @@ -410,13 +417,13 @@ fn send_charging_customers_batch_works() { era, batch_index, customer_id: user1, - amount: calculate_charge(usage1.clone()), + amount: calculate_charge(cluster_id, usage1.clone()), } .into(), ); report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); - let charge1 = calculate_charge_parts(usage1); + let charge1 = calculate_charge_parts(cluster_id, usage1); assert_eq!( charge1.puts + before_total_customer_charge.puts, report.total_customer_charge.puts @@ -438,10 +445,11 @@ fn send_charging_customers_batch_works() { let user1_debt = DdcPayouts::debtor_customers(cluster_id, user1); assert_eq!(user1_debt, None); - let balance_before = Balances::free_balance(DdcPayouts::sub_account_id(cluster_id, era)); + let mut balance_before = + Balances::free_balance(DdcPayouts::sub_account_id(cluster_id, era)); // batch 3 - batch_index += 2; + batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( RuntimeOrigin::signed(dac_account), @@ -451,8 +459,8 @@ fn send_charging_customers_batch_works() { payers3, )); - let user3_charge = calculate_charge(usage3.clone()); - let charge3 = calculate_charge_parts(usage3); + let user3_charge = calculate_charge(cluster_id, usage3.clone()); + let charge3 = calculate_charge_parts(cluster_id, usage3); let ratio = Perbill::from_rational(PARTIAL_CHARGE, user3_charge); report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_eq!( @@ -503,6 +511,77 @@ fn send_charging_customers_batch_works() { }) } +#[test] +fn send_charging_customers_batch_works2() { + ExtBuilder.build_and_execute(|| { + System::set_block_number(1); + + let dac_account = 123u128; + let user5 = 5u128; + let cluster_id = ONE_CLUSTER_ID; + let era = 100; + let max_batch_index = 0; + let mut batch_index = 0; + let usage5 = CustomerUsage { + // should pass without debt + transferred_bytes: 1024, + stored_bytes: 1024, + number_of_puts: 1, + number_of_gets: 1, + }; + let payers5 = vec![(user5.clone(), usage5.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_eq!(System::events().len(), 3); + + // batch 1 + let mut report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); + let before_total_customer_charge = report.total_customer_charge.clone(); + let balance_before = Balances::free_balance(DdcPayouts::sub_account_id(cluster_id, era)); + assert_ok!(DdcPayouts::send_charging_customers_batch( + RuntimeOrigin::signed(dac_account), + cluster_id, + era, + batch_index, + payers5, + )); + + let usage5_charge = calculate_charge(cluster_id, usage5.clone()); + let charge5 = calculate_charge_parts(cluster_id, usage5); + let balance = Balances::free_balance(DdcPayouts::sub_account_id(cluster_id, era)); + report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); + assert_eq!(balance, usage5_charge + balance_before); + assert_eq!( + charge5.puts + before_total_customer_charge.puts, + report.total_customer_charge.puts + ); + assert_eq!( + charge5.gets + before_total_customer_charge.gets, + report.total_customer_charge.gets + ); + assert_eq!( + charge5.storage + before_total_customer_charge.storage, + report.total_customer_charge.storage + ); + assert_eq!( + charge5.transfer + before_total_customer_charge.transfer, + report.total_customer_charge.transfer + ); + }) +} + #[test] fn end_charging_customers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { @@ -617,7 +696,7 @@ fn end_charging_customers_works() { )); let report_before = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); - let charge = calculate_charge(usage1); + let charge = calculate_charge(cluster_id, usage1); System::assert_last_event( Event::Charged { cluster_id, era, batch_index, customer_id: user1, amount: charge } .into(), @@ -710,8 +789,8 @@ fn end_charging_customers_works_zero_fees() { let usage1 = CustomerUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, - number_of_puts: 4456456345234523, - number_of_gets: 523423, + number_of_puts: 1, + number_of_gets: 1, }; let payers = vec![(user1, usage1.clone())]; @@ -739,7 +818,7 @@ fn end_charging_customers_works_zero_fees() { )); let report_before = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); - let charge = calculate_charge(usage1); + let charge = calculate_charge(cluster_id, usage1); System::assert_last_event( Event::Charged { cluster_id, era, customer_id: user1, batch_index, amount: charge } .into(),