Skip to content

Commit

Permalink
more tests (#266)
Browse files Browse the repository at this point in the history
### Description
<!-- Describe what change this PR is implementing -->

### Types of Changes
<!--- What types of changes does your code introduce? -->
- [x] Tech Debt (Code improvements)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Dependency upgrade (A change in substrate or any 3rd party crate
version)

### Migrations and Hooks
<!--- Check the following box with an x if the following applies: -->
- [ ] This change requires a runtime migration.
- [ ] Modifies `on_initialize`
- [ ] Modifies `on_finalize`

### Checklist
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [x] Change has been tested locally.
- [x] Change adds / updates tests.
- [x] Changelog doc updated.
  • Loading branch information
aie0 authored Feb 15, 2024
1 parent 330315b commit 185894f
Show file tree
Hide file tree
Showing 4 changed files with 1,520 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [D] Changes is `Cere Dev` Runtime

## [vNext]
- Added ChargeError event to payout pallet

## [4.8.9]

Expand Down
20 changes: 19 additions & 1 deletion pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ pub mod pallet {
AuthorisedCaller {
authorised_caller: T::AccountId,
},
ChargeError {
cluster_id: ClusterId,
era: DdcEra,
batch_index: BatchIndex,
customer_id: T::AccountId,
amount: u128,
error: DispatchError,
},
}

#[pallet::error]
Expand Down Expand Up @@ -457,7 +465,17 @@ pub mod pallet {
total_customer_charge,
) {
Ok(actually_charged) => actually_charged,
Err(_e) => 0,
Err(e) => {
Self::deposit_event(Event::<T>::ChargeError {
cluster_id,
era,
batch_index,
customer_id: customer_id.clone(),
amount: total_customer_charge,
error: e,
});
0
},
};

if amount_actually_charged < total_customer_charge {
Expand Down
72 changes: 63 additions & 9 deletions pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ impl<T: Config> CustomerCharger<T> for TestCustomerCharger {
let account_5 = T::AccountId::decode(&mut &temp[..]).unwrap();

if content_owner == account_1 ||
content_owner == account_2 ||
content_owner == account_3 ||
content_owner == account_4 ||
content_owner == account_5
Expand All @@ -165,15 +164,16 @@ impl<T: Config> CustomerCharger<T> for TestCustomerCharger {
}

if amount_to_charge < 50_000_000 && content_owner == account_3 {
assert!(PARTIAL_CHARGE < amount);
amount_to_charge = PARTIAL_CHARGE; // for user 3
}

if content_owner == account_2 {
assert!(USER2_BALANCE < amount);
amount_to_charge = USER2_BALANCE; // for user 2
}

let charge = amount_to_charge.saturated_into::<BalanceOf<T>>();

<T as pallet::Config>::Currency::transfer(
&content_owner,
&billing_vault,
Expand All @@ -189,6 +189,8 @@ pub const ACCOUNT_ID_2: AccountId = 2;
pub const ACCOUNT_ID_3: AccountId = 3;
pub const ACCOUNT_ID_4: AccountId = 4;
pub const ACCOUNT_ID_5: AccountId = 5;
pub const ACCOUNT_ID_6: AccountId = 6;
pub const ACCOUNT_ID_7: AccountId = 7;
pub struct TestClusterCreator;
impl<T: Config> ClusterCreator<T, Balance> for TestClusterCreator {
fn create_new_cluster(
Expand Down Expand Up @@ -222,21 +224,55 @@ pub const VALIDATOR1_SCORE: u64 = 30;
pub const VALIDATOR2_SCORE: u64 = 45;
pub const VALIDATOR3_SCORE: u64 = 25;

pub const PARTIAL_CHARGE: u128 = 100;
pub const USER2_BALANCE: u128 = 10;
pub const PARTIAL_CHARGE: u128 = 10;
pub const USER2_BALANCE: u128 = 5;
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 NO_FEE_CLUSTER_ID: ClusterId = ClusterId::zero();
pub const ONE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(4u8);
pub const CERE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(10u8);

pub const HIGH_FEES_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(5u8);
pub const GET_PUT_ZERO_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(3u8);
pub const STORAGE_ZERO_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(6u8);
pub const STREAM_ZERO_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(7u8);
pub const PUT_ZERO_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(8u8);
pub const GET_ZERO_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(9u8);
pub const STORAGE_STREAM_ZERO_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(11u8);
pub const PRICING_PARAMS: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
unit_per_mb_stored: 3_000_000,
unit_per_put_request: 4_000_000,
unit_per_get_request: 5_000_000,
};

pub const PRICING_PARAMS_STREAM_ZERO: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 0,
unit_per_mb_stored: 3_000_000,
unit_per_put_request: 4_000_000,
unit_per_get_request: 5_000_000,
};

pub const PRICING_PARAMS_STORAGE_ZERO: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
unit_per_mb_stored: 0,
unit_per_put_request: 4_000_000,
unit_per_get_request: 5_000_000,
};

pub const PRICING_PARAMS_GET_ZERO: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
unit_per_mb_stored: 3_000_000,
unit_per_put_request: 4_000_000,
unit_per_get_request: 0,
};

pub const PRICING_PARAMS_PUT_ZERO: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
unit_per_mb_stored: 3_000_000,
unit_per_put_request: 0,
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,
Expand All @@ -257,6 +293,12 @@ pub const PRICING_FEES: ClusterFeesParams = ClusterFeesParams {
cluster_reserve_share: Perquintill::from_percent(2),
};

pub const PRICING_FEES_HIGH: ClusterFeesParams = ClusterFeesParams {
treasury_share: Perquintill::from_percent(10),
validators_share: Perquintill::from_percent(20),
cluster_reserve_share: Perquintill::from_percent(20),
};

pub const PRICING_FEES_ZERO: ClusterFeesParams = ClusterFeesParams {
treasury_share: Perquintill::from_percent(0),
validators_share: Perquintill::from_percent(0),
Expand Down Expand Up @@ -343,21 +385,31 @@ impl<T: frame_system::Config> SortedListProvider<T::AccountId> for TestValidator
}

pub fn get_fees(cluster_id: &ClusterId) -> ClusterFeesParams {
if *cluster_id == FREE_CLUSTER_ID ||
if *cluster_id == NO_FEE_CLUSTER_ID ||
*cluster_id == ONE_CLUSTER_ID ||
*cluster_id == CERE_CLUSTER_ID
{
PRICING_FEES_ZERO
} else if *cluster_id == HIGH_FEES_CLUSTER_ID {
PRICING_FEES_HIGH
} else {
PRICING_FEES
}
}

pub fn get_pricing(cluster_id: &ClusterId) -> ClusterPricingParams {
if *cluster_id == FREE_CLUSTER_ID || *cluster_id == ONE_CLUSTER_ID {
if *cluster_id == ONE_CLUSTER_ID || *cluster_id == NO_FEE_CLUSTER_ID {
PRICING_PARAMS_ONE
} else if *cluster_id == CERE_CLUSTER_ID {
PRICING_PARAMS_CERE
} else if *cluster_id == STORAGE_ZERO_CLUSTER_ID {
PRICING_PARAMS_STORAGE_ZERO
} else if *cluster_id == STREAM_ZERO_CLUSTER_ID {
PRICING_PARAMS_STREAM_ZERO
} else if *cluster_id == PUT_ZERO_CLUSTER_ID {
PRICING_PARAMS_PUT_ZERO
} else if *cluster_id == GET_ZERO_CLUSTER_ID {
PRICING_PARAMS_GET_ZERO
} else {
PRICING_PARAMS
}
Expand Down Expand Up @@ -427,6 +479,8 @@ impl ExtBuilder {
(3, USER3_BALANCE), // > PARTIAL_CHARGE
(4, 1000000000000000000000000),
(5, 1000000000000000000000000),
(6, 1000000000000000000000000),
(7, 1000000000000000000000000),
],
}
.assimilate_storage(&mut storage);
Expand Down
Loading

0 comments on commit 185894f

Please sign in to comment.