Skip to content

Commit

Permalink
usage u64 + random tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aie0 committed Dec 21, 2023
1 parent aca442d commit 6ceef25
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pallets/ddc-payouts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ benchmarks! {
let total_node_usage = NodeUsage {
transferred_bytes: 200000000u64.saturating_mul(b.into()), // 200 mb per provider
stored_bytes: 100000000u64.saturating_mul(b.into()), // 100 mb per provider
number_of_gets: 10u128.saturating_mul(b.into()), // 10 gets per provider
number_of_puts: 5u128.saturating_mul(b.into()), // 5 puts per provider
number_of_gets: 10u64.saturating_mul(b.into()), // 10 gets per provider
number_of_puts: 10u64.saturating_mul(b.into()), // 5 puts per provider
};
let charging_max_batch_index = 0;
let mut charging_processed_batches : BoundedBTreeSet<BatchIndex, MaxBatchesCount> = BoundedBTreeSet::default();
Expand Down
14 changes: 6 additions & 8 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ type BatchIndex = u16;
pub struct CustomerUsage {
pub transferred_bytes: u64,
pub stored_bytes: u64,
pub number_of_puts: u128,
pub number_of_gets: u128,
pub number_of_puts: u64,
pub number_of_gets: u64,
}

/// Stores usage of node provider
#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)]
pub struct NodeUsage {
pub transferred_bytes: u64,
pub stored_bytes: u64,
pub number_of_puts: u128,
pub number_of_gets: u128,
pub number_of_puts: u64,
pub number_of_gets: u64,
}

/// Stores reward in tokens(units) of node provider as per NodeUsage
Expand Down Expand Up @@ -951,13 +951,11 @@ pub mod pallet {
})()
.ok_or(Error::<T>::ArithmeticOverflow)?;

total.gets = usage
.number_of_gets
total.gets = (usage.number_of_gets as u128)
.checked_mul(pricing.unit_per_get_request)
.ok_or(Error::<T>::ArithmeticOverflow)?;

total.puts = usage
.number_of_puts
total.puts = (usage.number_of_puts as u128)
.checked_mul(pricing.unit_per_put_request)
.ok_or(Error::<T>::ArithmeticOverflow)?;

Expand Down
34 changes: 31 additions & 3 deletions pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{self as pallet_ddc_payouts, *};
use ddc_primitives::{
ClusterBondingParams, ClusterFeesParams, ClusterGovParams, ClusterParams, ClusterPricingParams,
NodeType,
NodeType, DOLLARS,
};
use ddc_traits::{
cluster::{ClusterCreator, ClusterVisitor, ClusterVisitorError},
Expand All @@ -16,7 +16,7 @@ use frame_election_provider_support::SortedListProvider;

use frame_support::{
construct_runtime, parameter_types,
traits::{ConstU32, ConstU64, Everything},
traits::{ConstU32, ConstU64, Everything, Randomness},
weights::constants::RocksDbWeight,
PalletId,
};
Expand Down Expand Up @@ -59,6 +59,21 @@ parameter_types! {
pub static ExistentialDeposit: Balance = 1;
}

#[derive(Default, Clone)]
pub struct MockRandomness(H256);

impl Randomness<H256, BlockNumber> for MockRandomness {
fn random(subject: &[u8]) -> (H256, BlockNumber) {
let (mut r, b) = Self::random_seed();
r.as_mut()[0..subject.len()].copy_from_slice(subject);
(r, b)
}

fn random_seed() -> (H256, BlockNumber) {
(H256::default(), BlockNumber::default())
}
}

impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
Expand Down Expand Up @@ -204,6 +219,7 @@ 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 CERE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(10u8);

pub const PRICING_PARAMS: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: 2_000_000,
Expand All @@ -219,6 +235,13 @@ pub const PRICING_PARAMS_ONE: ClusterPricingParams = ClusterPricingParams {
unit_per_get_request: 10_000_000_000,
};

pub const PRICING_PARAMS_CERE: ClusterPricingParams = ClusterPricingParams {
unit_per_mb_streamed: DOLLARS,
unit_per_mb_stored: DOLLARS,
unit_per_put_request: DOLLARS,
unit_per_get_request: DOLLARS,
};

pub const PRICING_FEES: ClusterFeesParams = ClusterFeesParams {
treasury_share: Perquintill::from_percent(1),
validators_share: Perquintill::from_percent(10),
Expand Down Expand Up @@ -315,7 +338,10 @@ impl<T: frame_system::Config> SortedListProvider<T::AccountId> for TestValidator
}

pub fn get_fees(cluster_id: &ClusterId) -> ClusterFeesParams {
if *cluster_id == FREE_CLUSTER_ID || *cluster_id == ONE_CLUSTER_ID {
if *cluster_id == FREE_CLUSTER_ID ||
*cluster_id == ONE_CLUSTER_ID ||
*cluster_id == CERE_CLUSTER_ID
{
PRICING_FEES_ZERO
} else {
PRICING_FEES
Expand All @@ -325,6 +351,8 @@ pub fn get_fees(cluster_id: &ClusterId) -> ClusterFeesParams {
pub fn get_pricing(cluster_id: &ClusterId) -> ClusterPricingParams {
if *cluster_id == FREE_CLUSTER_ID || *cluster_id == ONE_CLUSTER_ID {
PRICING_PARAMS_ONE
} else if *cluster_id == CERE_CLUSTER_ID {
PRICING_PARAMS_CERE
} else {
PRICING_PARAMS
}
Expand Down
Loading

0 comments on commit 6ceef25

Please sign in to comment.