From e2d1813fa946f109d668e3e1412b507457b34e94 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Wed, 13 Nov 2024 16:20:01 +0100 Subject: [PATCH 01/15] fix: benchmarking is fixed for payouts pallet --- Cargo.lock | 2 + pallets/ddc-clusters-gov/src/benchmarking.rs | 2 +- pallets/ddc-clusters-gov/src/lib.rs | 12 +- pallets/ddc-clusters-gov/src/mock.rs | 7 +- pallets/ddc-customers/src/benchmarking.rs | 2 +- pallets/ddc-customers/src/lib.rs | 79 ++++---- pallets/ddc-nodes/src/lib.rs | 10 +- pallets/ddc-nodes/src/tests.rs | 2 +- pallets/ddc-payouts/Cargo.toml | 3 + pallets/ddc-payouts/src/benchmarking.rs | 179 ++++++++++++++++--- pallets/ddc-payouts/src/lib.rs | 16 +- pallets/ddc-payouts/src/mock.rs | 63 +++++-- pallets/ddc-staking/src/benchmarking.rs | 2 +- pallets/ddc-staking/src/lib.rs | 12 +- pallets/ddc-staking/src/migrations.rs | 4 +- pallets/ddc-staking/src/mock.rs | 3 +- pallets/ddc-staking/src/testing_utils.rs | 4 +- pallets/ddc-verification/src/lib.rs | 53 +++--- pallets/ddc-verification/src/mock.rs | 16 +- primitives/src/lib.rs | 29 ++- primitives/src/traits/bucket.rs | 25 ++- primitives/src/traits/node.rs | 7 +- primitives/src/traits/validator.rs | 10 +- runtime/cere-dev/src/lib.rs | 13 +- runtime/cere/src/lib.rs | 13 +- 25 files changed, 396 insertions(+), 172 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb221b766..ced123b99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5725,7 +5725,9 @@ dependencies = [ "hex", "log", "pallet-balances", + "pallet-ddc-verification", "parity-scale-codec", + "polkadot-ckb-merkle-mountain-range", "scale-info", "sp-core", "sp-io", diff --git a/pallets/ddc-clusters-gov/src/benchmarking.rs b/pallets/ddc-clusters-gov/src/benchmarking.rs index 837ff2353..7f5893f3f 100644 --- a/pallets/ddc-clusters-gov/src/benchmarking.rs +++ b/pallets/ddc-clusters-gov/src/benchmarking.rs @@ -89,7 +89,7 @@ pub fn create_cluster_with_nodes( p2p_port: 9070_u16, }); - T::NodeCreator::create_node(node_pub_key.clone(), node_provider.clone(), node_params) + T::NodeManager::create_node(node_pub_key.clone(), node_provider.clone(), node_params) .expect("Node is not created"); T::StakerCreator::bond_stake_and_participate( diff --git a/pallets/ddc-clusters-gov/src/lib.rs b/pallets/ddc-clusters-gov/src/lib.rs index 788ee0656..f86e4f64c 100644 --- a/pallets/ddc-clusters-gov/src/lib.rs +++ b/pallets/ddc-clusters-gov/src/lib.rs @@ -16,12 +16,12 @@ use codec::{Decode, Encode}; #[cfg(feature = "runtime-benchmarks")] -use ddc_primitives::traits::{node::NodeCreator, staking::StakerCreator}; +use ddc_primitives::traits::staking::StakerCreator; use ddc_primitives::{ traits::{ cluster::{ClusterCreator, ClusterManager, ClusterProtocol, ClusterQuery}, cluster_gov::{DefaultVote, MemberCount, SeatsConsensus}, - node::NodeVisitor, + node::NodeManager, pallet::GetDdcOrigin, }, ClusterId, ClusterNodeStatus, ClusterProtocolParams, ClusterStatus, NodePubKey, @@ -138,14 +138,12 @@ pub mod pallet { type ClusterCreator: ClusterCreator>; type ClusterManager: ClusterManager; type ClusterProtocol: ClusterProtocol>; - type NodeVisitor: NodeVisitor; + type NodeManager: NodeManager; type SeatsConsensus: SeatsConsensus; type DefaultVote: DefaultVote; type MinValidatedNodesCount: Get; type ReferendumEnactmentDuration: Get>; #[cfg(feature = "runtime-benchmarks")] - type NodeCreator: NodeCreator; - #[cfg(feature = "runtime-benchmarks")] type StakerCreator: StakerCreator>; } @@ -488,7 +486,7 @@ pub mod pallet { if !is_validated_node { Err(Error::::NotValidatedNode.into()) } else { - let node_provider = T::NodeVisitor::get_node_provider_id(&node_pub_key)?; + let node_provider = T::NodeManager::get_node_provider_id(&node_pub_key)?; if origin == node_provider { Ok(()) } else { @@ -512,7 +510,7 @@ pub mod pallet { if node_state.status != ClusterNodeStatus::ValidationSucceeded { Err(Error::::NotValidatedNode.into()) } else { - let node_provider = T::NodeVisitor::get_node_provider_id(&node_pub_key)?; + let node_provider = T::NodeManager::get_node_provider_id(&node_pub_key)?; if origin == node_provider { let voting = ClusterProposalVoting::::get(cluster_id) .ok_or(Error::::ProposalMissing)?; diff --git a/pallets/ddc-clusters-gov/src/mock.rs b/pallets/ddc-clusters-gov/src/mock.rs index 7092ce96e..7d465ab58 100644 --- a/pallets/ddc-clusters-gov/src/mock.rs +++ b/pallets/ddc-clusters-gov/src/mock.rs @@ -291,8 +291,7 @@ impl pallet_ddc_staking::Config for Test { type ClusterProtocol = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; type ClusterManager = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; - type NodeCreator = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type ClusterBondingAmount = ClusterBondingAmount; type ClusterUnboningDelay = ClusterUnboningDelay; } @@ -322,14 +321,12 @@ impl crate::pallet::Config for Test { type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; type ClusterProtocol = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type SeatsConsensus = MockedSeatsConsensus; type DefaultVote = MockedDefaultVote; // pallet_ddc_clusters_gov::PrimeDefaultVote; type MinValidatedNodesCount = MinValidatedNodesCount; type ReferendumEnactmentDuration = ReferendumEnactmentDuration; #[cfg(feature = "runtime-benchmarks")] - type NodeCreator = pallet_ddc_nodes::Pallet; - #[cfg(feature = "runtime-benchmarks")] type StakerCreator = pallet_ddc_staking::Pallet; } diff --git a/pallets/ddc-customers/src/benchmarking.rs b/pallets/ddc-customers/src/benchmarking.rs index 04b1b1525..b718a8f97 100644 --- a/pallets/ddc-customers/src/benchmarking.rs +++ b/pallets/ddc-customers/src/benchmarking.rs @@ -1,7 +1,7 @@ //! DdcStaking pallet benchmarking. #![cfg(feature = "runtime-benchmarks")] -use ddc_primitives::{ClusterId, ClusterParams, ClusterProtocolParams}; +use ddc_primitives::{BucketParams, ClusterId, ClusterParams, ClusterProtocolParams}; use frame_benchmarking::{account, benchmarks, whitelist_account}; use frame_support::traits::Currency; use sp_runtime::Perquintill; diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index 1c2e98b2c..ff7475645 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -15,11 +15,11 @@ mod tests; use codec::{Decode, Encode}; use ddc_primitives::{ traits::{ - bucket::{BucketManager, BucketVisitor}, + bucket::BucketManager, cluster::{ClusterCreator, ClusterProtocol, ClusterQuery}, customer::{CustomerCharger, CustomerDepositor, CustomerVisitor}, }, - BucketId, ClusterId, CustomerUsage, + BucketId, BucketParams, ClusterId, CustomerUsage, }; use frame_support::{ parameter_types, @@ -70,11 +70,6 @@ pub struct Bucket { total_customers_usage: Option, } -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct BucketParams { - is_public: bool, -} - #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] #[scale_info(skip_type_params(T))] pub struct AccountsLedger { @@ -324,27 +319,10 @@ pub mod pallet { bucket_params: BucketParams, ) -> DispatchResult { let bucket_owner = ensure_signed(origin)?; - let cur_bucket_id = + let bucket_id = Self::buckets_count().checked_add(1).ok_or(Error::::ArithmeticOverflow)?; - ensure!( - >::cluster_exists(&cluster_id), - Error::::ClusterDoesNotExist - ); - - let bucket = Bucket { - bucket_id: cur_bucket_id, - owner_id: bucket_owner, - cluster_id, - is_public: bucket_params.is_public, - is_removed: false, - total_customers_usage: None, - }; - - >::set(cur_bucket_id); - >::insert(cur_bucket_id, bucket); - - Self::deposit_event(Event::::BucketCreated { cluster_id, bucket_id: cur_bucket_id }); + Self::do_create_bucket(cluster_id, bucket_id, bucket_owner, bucket_params)?; Ok(()) } @@ -647,15 +625,43 @@ pub mod pallet { Ok((ledger, unlocking_balance)) } + + fn do_create_bucket( + cluster_id: ClusterId, + bucket_id: BucketId, + owner_id: T::AccountId, + bucket_params: BucketParams, + ) -> DispatchResult { + ensure!( + >::cluster_exists(&cluster_id), + Error::::ClusterDoesNotExist + ); + + let bucket = Bucket { + bucket_id, + owner_id, + cluster_id, + is_public: bucket_params.is_public, + is_removed: false, + total_customers_usage: None, + }; + + >::set(bucket_id); + >::insert(bucket_id, bucket); + + Self::deposit_event(Event::::BucketCreated { cluster_id, bucket_id }); + + Ok(()) + } } - impl BucketVisitor for Pallet { + impl BucketManager for Pallet { fn get_bucket_owner_id(bucket_id: BucketId) -> Result { let bucket = Self::buckets(bucket_id).ok_or(Error::::BucketDoesNotExist)?; Ok(bucket.owner_id) } - fn get_total_customer_usage( + fn get_total_bucket_usage( cluster_id: &ClusterId, bucket_id: BucketId, content_owner: &T::AccountId, @@ -666,10 +672,8 @@ pub mod pallet { Ok(bucket.total_customers_usage) } - } - impl BucketManager for Pallet { - fn inc_total_customer_usage( + fn inc_total_bucket_usage( cluster_id: &ClusterId, bucket_id: BucketId, content_owner: T::AccountId, @@ -707,6 +711,17 @@ pub mod pallet { Ok(()) } + + #[cfg(feature = "runtime-benchmarks")] + fn create_bucket( + cluster_id: &ClusterId, + bucket_id: BucketId, + owner_id: T::AccountId, + bucket_params: BucketParams, + ) -> Result<(), DispatchError> { + Self::do_create_bucket(*cluster_id, bucket_id, owner_id, bucket_params)?; + Ok(()) + } } impl CustomerCharger for Pallet { @@ -750,7 +765,7 @@ pub mod pallet { actually_charged.checked_add(&charged).ok_or(Error::::ArithmeticUnderflow)?; } - Self::inc_total_customer_usage( + Self::inc_total_bucket_usage( cluster_id, bucket_id, content_owner.clone(), diff --git a/pallets/ddc-nodes/src/lib.rs b/pallets/ddc-nodes/src/lib.rs index 2a0e2fdc8..3f2f7ef31 100644 --- a/pallets/ddc-nodes/src/lib.rs +++ b/pallets/ddc-nodes/src/lib.rs @@ -28,10 +28,7 @@ pub mod benchmarking; pub mod testing_utils; use ddc_primitives::{ - traits::{ - node::{NodeCreator, NodeVisitor}, - staking::StakingVisitor, - }, + traits::{node::NodeManager, staking::StakingVisitor}, ClusterId, NodeParams, NodePubKey, NodeUsage, StorageNodeParams, StorageNodePubKey, }; use frame_support::pallet_prelude::*; @@ -226,7 +223,7 @@ pub mod pallet { } } - impl NodeVisitor for Pallet { + impl NodeManager for Pallet { fn get_cluster_id(node_pub_key: &NodePubKey) -> Result, DispatchError> { let node = Self::get(node_pub_key.clone()).map_err(|_| Error::::NodeDoesNotExist)?; Ok(*node.get_cluster_id()) @@ -267,9 +264,8 @@ pub mod pallet { Ok(total_usage) } - } - impl NodeCreator for Pallet { + #[cfg(feature = "runtime-benchmarks")] fn create_node( node_pub_key: NodePubKey, provider_id: T::AccountId, diff --git a/pallets/ddc-nodes/src/tests.rs b/pallets/ddc-nodes/src/tests.rs index eb3de5d31..3bc8230e3 100644 --- a/pallets/ddc-nodes/src/tests.rs +++ b/pallets/ddc-nodes/src/tests.rs @@ -132,7 +132,7 @@ fn create_storage_node_with_node_creator() { let account_id1 = AccountId::from([1; 32]); // Node created - assert_ok!(>::create_node( + assert_ok!(>::create_node( NodePubKey::StoragePubKey(node_pub_key.clone()), account_id1, NodeParams::StorageParams(storage_node_params) diff --git a/pallets/ddc-payouts/Cargo.toml b/pallets/ddc-payouts/Cargo.toml index 02eab6699..6613de0bf 100644 --- a/pallets/ddc-payouts/Cargo.toml +++ b/pallets/ddc-payouts/Cargo.toml @@ -29,10 +29,12 @@ sp-std = { workspace = true } # Cere dependencies ddc-primitives = { workspace = true } +polkadot-ckb-merkle-mountain-range = { workspace = true } [dev-dependencies] chrono = { workspace = true, default-features = true } pallet-balances = { workspace = true, default-features = true } +pallet-ddc-verification = { workspace = true, default-features = true } sp-core = { workspace = true, default-features = true } sp-tracing = { workspace = true, default-features = true } substrate-test-utils = { workspace = true, default-features = true } @@ -40,6 +42,7 @@ substrate-test-utils = { workspace = true, default-features = true } [features] default = ["std"] std = [ + "polkadot-ckb-merkle-mountain-range/std", "hex/std", "codec/std", "ddc-primitives/std", diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs index c3aa81ff1..a89cb5d24 100644 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ b/pallets/ddc-payouts/src/benchmarking.rs @@ -1,10 +1,17 @@ //! DdcPayouts pallet benchmarking. use ddc_primitives::{ - traits::ValidatorVisitor, ClusterId, ClusterParams, ClusterProtocolParams, NodePubKey, + traits::ValidatorVisitor, ActivityHash, BucketParams, ClusterId, ClusterParams, + ClusterProtocolParams, EraValidation, EraValidationStatus, MergeActivityHash, NodeParams, + NodePubKey, StorageNodeMode, StorageNodeParams, }; pub use frame_benchmarking::{account, benchmarks, whitelist_account}; use frame_system::RawOrigin; +use polkadot_ckb_merkle_mountain_range::{ + util::{MemMMR, MemStore}, + MMR, +}; +use scale_info::prelude::{collections::BTreeMap, format}; use sp_runtime::{AccountId32, Perquintill}; use sp_std::prelude::*; @@ -12,11 +19,11 @@ use super::*; use crate::Pallet as DdcPayouts; const CERE: u128 = 10000000000; +const AVG_SECONDS_MONTH: i64 = 2630016; // 30.44 * 24.0 * 3600.0; fn create_dac_account() -> T::AccountId { let dac_account = create_account::("dac_account", 0, 0); - authorize_account::(dac_account.clone()); - T::ValidatorVisitor::setup_validators(vec![dac_account.clone()]); + T::ValidatorVisitor::setup_validators(vec![(dac_account.clone(), dac_account.clone())]); dac_account } @@ -24,10 +31,6 @@ fn create_account(name: &'static str, idx: u32, seed: u32) -> T::Acco account::(name, idx, seed) } -fn authorize_account(account: T::AccountId) { - AuthorisedCaller::::put(account); -} - fn endow_account(account: &T::AccountId, amount: u128) { let balance = amount.saturated_into::>(); let _ = T::Currency::make_free_balance_be(account, balance); @@ -106,7 +109,7 @@ struct BillingReportParams { fn create_billing_report(params: BillingReportParams) { let vault = DdcPayouts::::sub_account_id(params.cluster_id, params.era); let start_era: i64 = 1_000_000_000; - let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; let billing_report = BillingReport:: { vault, @@ -125,6 +128,14 @@ fn create_billing_report(params: BillingReportParams) { ActiveBillingReports::::insert(params.cluster_id, params.era, billing_report); } +fn set_validation_era( + cluster_id: ClusterId, + era_id: DdcEra, + era_validation: EraValidation, +) { + T::ValidatorVisitor::setup_validation_era(cluster_id, era_id, era_validation); +} + benchmarks! { set_authorised_caller { @@ -225,7 +236,8 @@ benchmarks! { }); let batch_index: BatchIndex = 0; - let payers: Vec<(NodePubKey, BucketId, CustomerUsage)> = (0..b).map(|i| { + let mut payers_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = vec![]; + for i in 0..b { let customer = create_account::("customer", i, i); if b % 2 == 0 { @@ -242,16 +254,73 @@ benchmarks! { number_of_gets: 10, // 10 gets number_of_puts: 5, // 5 puts }; - let bucket_id: BucketId = 1; + let bucket_id: BucketId = (i + 1).into(); let node_key = NodePubKey::StoragePubKey(AccountId32::from([ 48, 47, 147, 125, 243, 160, 236, 76, 101, 142, 129, 34, 67, 158, 116, 141, 34, 116, 66, 235, 212, 147, 206, 245, 33, 161, 225, 73, 67, 132, 67, 149, ])); - (node_key, bucket_id, customer_usage) - }).collect(); + T::BucketManager::create_bucket( + &cluster_id, + bucket_id, + customer, + BucketParams { is_public: true } + ).expect("Bucket to be created"); + + payers_batch.push((node_key, bucket_id, customer_usage)); + } + + let activity_hashes = payers_batch.clone().into_iter().map(|(node_key, bucket_id, usage)| { + let mut data = bucket_id.encode(); + let node_id = format!("0x{}", node_key.get_hex()); + data.extend_from_slice(&node_id.encode()); + data.extend_from_slice(&usage.stored_bytes.encode()); + data.extend_from_slice(&usage.transferred_bytes.encode()); + data.extend_from_slice(&usage.number_of_puts.encode()); + data.extend_from_slice(&usage.number_of_gets.encode()); + sp_io::hashing::blake2_256(&data) + }).collect::>(); + + let store1 = MemStore::default(); + let mut mmr1: MMR> = MemMMR::::new(0, &store1); + for activity_hash in activity_hashes { + let _pos: u64 = mmr1.push(activity_hash).unwrap(); + } + + let batch_root = mmr1.get_root().unwrap(); + + let store2 = MemStore::default(); + let mut mmr2: MMR> = MemMMR::::new(0, &store2); + let pos = mmr2.push(batch_root).unwrap(); + let payers_merkle_root_hash = mmr2.get_root().unwrap(); + + let proof = mmr2 + .gen_proof(vec![pos]) + .unwrap() + .proof_items() + .to_vec(); + + let payees_merkle_root_hash = ActivityHash::default(); + + let mut validators = BTreeMap::new(); + validators.insert( + (payers_merkle_root_hash, payees_merkle_root_hash), + vec![dac_account.clone()], + ); + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let era_validation = EraValidation:: { + validators, + start_era, + end_era, + payers_merkle_root_hash, + payees_merkle_root_hash, + status: EraValidationStatus::PayoutInProgress, + }; - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payers, MMRProof::default()) + set_validation_era::(cluster_id, era, era_validation); + + }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payers_batch, MMRProof { proof }) verify { assert!(ActiveBillingReports::::contains_key(cluster_id, era)); let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); @@ -402,23 +471,89 @@ benchmarks! { whitelist_account!(dac_account); let batch_index: BatchIndex = 0; - let payees: Vec<(NodePubKey, NodeUsage)> = (0..b).map(|i| { + let mut payees_batch: Vec<(NodePubKey, NodeUsage)> = vec![]; + + for i in 0..b { let provider = create_account::("provider", i, i); + endow_account::(&provider, T::Currency::minimum_balance().saturated_into()); - let node_usage = NodeUsage { + + let usage = NodeUsage { transferred_bytes: 200000000, // 200 mb stored_bytes: 100000000, // 100 mb number_of_gets: 10, // 10 gets number_of_puts: 5, // 5 puts }; - let node_key = NodePubKey::StoragePubKey(AccountId32::from([ - 48, 47, 147, 125, 243, 160, 236, 76, 101, 142, 129, 34, 67, 158, 116, 141, 34, 116, 66, - 235, 212, 147, 206, 245, 33, 161, 225, 73, 67, 132, 67, 149, - ])); - (node_key, node_usage) - }).collect(); - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payees, MMRProof::default()) + let key = sp_io::hashing::blake2_256(&i.encode()); + let node_key = NodePubKey::StoragePubKey(AccountId32::from(key)); + + T::NodeManager::create_node( + node_key.clone(), + provider, + NodeParams::StorageParams(StorageNodeParams { + mode: StorageNodeMode::Storage, + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, + http_port: 35000u16, + grpc_port: 25000u16, + p2p_port: 15000u16, + }), + ).expect("Node to be created"); + + payees_batch.push((node_key, usage)); + } + + let activity_hashes = payees_batch.clone().into_iter().map(|(node_key, usage)| { + let mut data = format!("0x{}", node_key.get_hex()).encode(); + data.extend_from_slice(&usage.stored_bytes.encode()); + data.extend_from_slice(&usage.transferred_bytes.encode()); + data.extend_from_slice(&usage.number_of_puts.encode()); + data.extend_from_slice(&usage.number_of_gets.encode()); + sp_io::hashing::blake2_256(&data) + }).collect::>(); + + let store1 = MemStore::default(); + let mut mmr1: MMR> = MemMMR::::new(0, &store1); + for activity_hash in activity_hashes { + let _pos: u64 = mmr1.push(activity_hash).unwrap(); + } + + let batch_root = mmr1.get_root().unwrap(); + + let store2 = MemStore::default(); + let mut mmr2: MMR> = MemMMR::::new(0, &store2); + let pos = mmr2.push(batch_root).unwrap(); + let payees_merkle_root_hash = mmr2.get_root().unwrap(); + + let payers_merkle_root_hash = ActivityHash::default(); + + let proof = mmr2 + .gen_proof(vec![pos]) + .unwrap() + .proof_items() + .to_vec(); + + let mut validators = BTreeMap::new(); + validators.insert( + (payers_merkle_root_hash, payees_merkle_root_hash), + vec![dac_account.clone()], + ); + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let era_validation = EraValidation:: { + validators, + start_era, + end_era, + payers_merkle_root_hash, + payees_merkle_root_hash, + status: EraValidationStatus::PayoutInProgress, + }; + + set_validation_era::(cluster_id, era, era_validation); + + }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payees_batch, MMRProof { proof }) verify { assert!(ActiveBillingReports::::contains_key(cluster_id, era)); let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 2d45ea29c..58772cd9b 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -28,12 +28,12 @@ mod tests; use ddc_primitives::{ traits::{ - bucket::BucketVisitor as BucketVisitorType, + bucket::BucketManager, cluster::{ClusterCreator as ClusterCreatorType, ClusterProtocol as ClusterProtocolType}, customer::{ CustomerCharger as CustomerChargerType, CustomerDepositor as CustomerDepositorType, }, - node::NodeVisitor as NodeVisitorType, + node::NodeManager, pallet::PalletVisitor as PalletVisitorType, payout::PayoutVisitor, }, @@ -119,8 +119,8 @@ pub mod pallet { type PalletId: Get; type Currency: LockableCurrency>; type CustomerCharger: CustomerChargerType; - type BucketVisitor: BucketVisitorType; - type NodeVisitor: NodeVisitorType; + type BucketManager: BucketManager; + type NodeManager: NodeManager; type CustomerDepositor: CustomerDepositorType; type TreasuryVisitor: PalletVisitorType; type ClusterProtocol: ClusterProtocolType>; @@ -467,14 +467,14 @@ pub mod pallet { batch_index, billing_report.charging_max_batch_index, &payers, - &batch_proof + &batch_proof, ), Error::::BatchValidationFailed ); let mut updated_billing_report = billing_report; for (_node_key, bucket_id, customer_usage) in payers { - let customer_id = T::BucketVisitor::get_bucket_owner_id(bucket_id)?; + let customer_id = T::BucketManager::get_bucket_owner_id(bucket_id)?; let mut customer_charge = get_customer_charge::( &cluster_id, @@ -794,7 +794,7 @@ pub mod pallet { let mut updated_billing_report = billing_report.clone(); for (node_key, delta_node_usage) in payees { // todo! get T::NodeVisitor::get_total_usage(delta_node_usage.node_id).stored_bytes - let node_provider_id = T::NodeVisitor::get_node_provider_id(&node_key)?; + let node_provider_id = T::NodeManager::get_node_provider_id(&node_key)?; let mut total_node_stored_bytes: i64 = 0; total_node_stored_bytes = total_node_stored_bytes @@ -1106,7 +1106,7 @@ pub mod pallet { Perquintill::from_rational(duration_seconds as u64, seconds_in_month as u64); let mut total_stored_bytes: i64 = - T::BucketVisitor::get_total_customer_usage(cluster_id, bucket_id, customer_id)? + T::BucketManager::get_total_bucket_usage(cluster_id, bucket_id, customer_id)? .map_or(0, |customer_usage| customer_usage.stored_bytes); total_stored_bytes = total_stored_bytes diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index ed1ad03c4..bf8a0dd8d 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -4,16 +4,18 @@ use ddc_primitives::{ traits::{ - bucket::BucketVisitor, + bucket::BucketManager, cluster::{ClusterCreator, ClusterProtocol}, customer::{CustomerCharger, CustomerDepositor}, - node::NodeVisitor, + node::NodeManager, pallet::PalletVisitor, ClusterQuery, ValidatorVisitor, }, ClusterBondingParams, ClusterFeesParams, ClusterParams, ClusterPricingParams, ClusterProtocolParams, ClusterStatus, NodeParams, NodePubKey, NodeType, DOLLARS, }; +#[cfg(feature = "try-runtime")] +use ddc_primitives::{BucketParams, EraValidation}; use frame_election_provider_support::SortedListProvider; use frame_support::{ construct_runtime, parameter_types, @@ -128,34 +130,36 @@ impl crate::pallet::Config for Test { type PalletId = PayoutsPalletId; type Currency = Balances; type CustomerCharger = TestCustomerCharger; - type BucketVisitor = TestBucketVisitor; + type BucketManager = TestBucketManager; type CustomerDepositor = TestCustomerDepositor; type ClusterProtocol = TestClusterProtocol; type TreasuryVisitor = TestTreasuryVisitor; type NominatorsAndValidatorsList = TestValidatorVisitor; type ClusterCreator = TestClusterCreator; - type VoteScoreToU64 = Identity; type WeightInfo = (); type ValidatorVisitor = MockValidatorVisitor; - type NodeVisitor = MockNodeVisitor; + type NodeManager = MockNodeManager; type AccountIdConverter = AccountId; } -pub struct MockNodeVisitor; -impl NodeVisitor for MockNodeVisitor +pub struct MockNodeManager; +impl NodeManager for MockNodeManager where ::AccountId: From, { fn get_total_usage(_node_pub_key: &NodePubKey) -> Result, DispatchError> { unimplemented!() } + fn get_cluster_id(_node_pub_key: &NodePubKey) -> Result, DispatchError> { unimplemented!() } + fn exists(_node_pub_key: &NodePubKey) -> bool { unimplemented!() } + fn get_node_provider_id(pub_key: &NodePubKey) -> Result { match pub_key { NodePubKey::StoragePubKey(key) if key == &NODE1_PUB_KEY_32 => @@ -182,9 +186,19 @@ where _ => Err(DispatchError::Other("Unexpected node pub_key")), } } + fn get_node_params(_node_pub_key: &NodePubKey) -> Result { unimplemented!() } + + #[cfg(feature = "runtime-benchmarks")] + fn create_node( + _node_pub_key: NodePubKey, + _provider_id: T::AccountId, + _node_params: NodeParams, + ) -> DispatchResult { + unimplemented!() + } } pub struct MockValidatorVisitor; @@ -193,7 +207,15 @@ where ::AccountId: From, { #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(_validators: Vec) { + fn setup_validators(_validators_with_keys: Vec<(T::AccountId, T::AccountId)>) { + unimplemented!() + } + #[cfg(feature = "runtime-benchmarks")] + fn setup_validation_era( + _cluster_id: ClusterId, + _era_id: DdcEra, + _era_validation: EraValidation, + ) { unimplemented!() } fn is_ocw_validator(caller: T::AccountId) -> bool { @@ -221,8 +243,8 @@ where } } -pub struct TestBucketVisitor; -impl BucketVisitor for TestBucketVisitor +pub struct TestBucketManager; +impl BucketManager for TestBucketManager where ::AccountId: From, { @@ -251,13 +273,32 @@ where } } - fn get_total_customer_usage( + fn get_total_bucket_usage( _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: &T::AccountId, ) -> Result, DispatchError> { Ok(None) } + + fn inc_total_bucket_usage( + _cluster_id: &ClusterId, + _bucket_id: BucketId, + _content_owner: T::AccountId, + _customer_usage: &CustomerUsage, + ) -> DispatchResult { + unimplemented!() + } + + #[cfg(feature = "runtime-benchmarks")] + fn create_bucket( + _cluster_id: &ClusterId, + _bucket_id: BucketId, + _owner_id: T::AccountId, + _bucket_params: BucketParams, + ) -> Result<(), DispatchError> { + unimplemented!() + } } pub struct TestCustomerCharger; diff --git a/pallets/ddc-staking/src/benchmarking.rs b/pallets/ddc-staking/src/benchmarking.rs index ecd280e2a..7bda381c5 100644 --- a/pallets/ddc-staking/src/benchmarking.rs +++ b/pallets/ddc-staking/src/benchmarking.rs @@ -41,7 +41,7 @@ benchmarks! { let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); let node = NodePubKey::StoragePubKey(StorageNodePubKey::new([0; 32])); - let _ = T::NodeCreator::create_node( + let _ = T::NodeManager::create_node( node.clone(), stash.clone(), NodeParams::StorageParams(StorageNodeParams { diff --git a/pallets/ddc-staking/src/lib.rs b/pallets/ddc-staking/src/lib.rs index ee4ef92e1..4896e97b6 100644 --- a/pallets/ddc-staking/src/lib.rs +++ b/pallets/ddc-staking/src/lib.rs @@ -33,7 +33,7 @@ use core::fmt::Debug; use codec::{Decode, Encode, HasCompact}; use ddc_primitives::traits::{ cluster::{ClusterCreator, ClusterProtocol, ClusterQuery}, - node::{NodeCreator, NodeVisitor}, + node::NodeManager, staking::{StakerCreator, StakingVisitor, StakingVisitorError}, }; pub use ddc_primitives::{ClusterId, ClusterNodesCount, NodePubKey, NodeType}; @@ -179,9 +179,7 @@ pub mod pallet { type ClusterManager: ClusterManager; - type NodeVisitor: NodeVisitor; - - type NodeCreator: NodeCreator; + type NodeManager: NodeManager; type ClusterBondingAmount: Get>; @@ -419,7 +417,7 @@ pub mod pallet { } // Checks that the node is registered in the network - ensure!(T::NodeVisitor::exists(&node), Error::::NodeIsNotFound); + ensure!(T::NodeManager::exists(&node), Error::::NodeIsNotFound); frame_system::Pallet::::inc_consumers(&stash).map_err(|_| Error::::BadState)?; @@ -508,8 +506,8 @@ pub mod pallet { let node_pub_key = >::get(&ledger.stash).ok_or(Error::::BadState)?; - let unbonding_delay = if T::NodeVisitor::exists(&node_pub_key) { - let node_cluster_id = T::NodeVisitor::get_cluster_id(&node_pub_key) + let unbonding_delay = if T::NodeManager::exists(&node_pub_key) { + let node_cluster_id = T::NodeManager::get_cluster_id(&node_pub_key) .map_err(|_| Error::::NoCluster)?; if let Some(cluster_id) = node_cluster_id { diff --git a/pallets/ddc-staking/src/migrations.rs b/pallets/ddc-staking/src/migrations.rs index 6260ec042..abb0cfc10 100644 --- a/pallets/ddc-staking/src/migrations.rs +++ b/pallets/ddc-staking/src/migrations.rs @@ -2,7 +2,7 @@ pub mod v1 { #[cfg(feature = "try-runtime")] use ddc_primitives::ClusterStatus; use ddc_primitives::{ - traits::{ClusterProtocol, ClusterQuery, NodeVisitor}, + traits::{ClusterProtocol, ClusterQuery, NodeManager}, ClusterId, NodePubKey, }; use frame_support::{ @@ -54,7 +54,7 @@ pub mod v1 { let mut served_clusters: BTreeMap = BTreeMap::new(); for node_pub_key in bonded_nodes.iter() { - if let Ok(Some(cluster_id)) = T::NodeVisitor::get_cluster_id(node_pub_key) { + if let Ok(Some(cluster_id)) = T::NodeManager::get_cluster_id(node_pub_key) { served_clusters.insert(cluster_id, ()); } weight.saturating_accrue(T::DbWeight::get().reads(1)); diff --git a/pallets/ddc-staking/src/mock.rs b/pallets/ddc-staking/src/mock.rs index b1cb4729c..32b904fbe 100644 --- a/pallets/ddc-staking/src/mock.rs +++ b/pallets/ddc-staking/src/mock.rs @@ -187,8 +187,7 @@ impl crate::pallet::Config for Test { type ClusterProtocol = pallet_ddc_clusters::Pallet; type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; - type NodeCreator = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type ClusterBondingAmount = ClusterBondingAmount; type ClusterUnboningDelay = ClusterUnboningDelay; } diff --git a/pallets/ddc-staking/src/testing_utils.rs b/pallets/ddc-staking/src/testing_utils.rs index 71c6e4ad5..d72b175fb 100644 --- a/pallets/ddc-staking/src/testing_utils.rs +++ b/pallets/ddc-staking/src/testing_utils.rs @@ -57,7 +57,7 @@ pub fn create_stash_controller_node( T::Lookup::unlookup(controller.clone()); let node = NodePubKey::StoragePubKey(StorageNodePubKey::new([0; 32])); - T::NodeCreator::create_node( + T::NodeManager::create_node( node.clone(), stash.clone(), NodeParams::StorageParams(StorageNodeParams { @@ -94,7 +94,7 @@ pub fn create_stash_controller_node_with_balance( let node_pub = node_pub_key.clone(); match node_pub_key { NodePubKey::StoragePubKey(node_pub_key) => { - T::NodeCreator::create_node( + T::NodeManager::create_node( ddc_primitives::NodePubKey::StoragePubKey(node_pub_key), stash.clone(), NodeParams::StorageParams(StorageNodeParams { diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index d6dc6ac5c..35a71e8d4 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -14,11 +14,12 @@ use core::str; use base64ct::{Base64, Encoding}; use ddc_primitives::{ traits::{ - ClusterManager, ClusterValidator, CustomerVisitor, NodeVisitor, PayoutVisitor, + ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutVisitor, ValidatorVisitor, }, - ActivityHash, BatchIndex, ClusterId, ClusterStatus, CustomerUsage, DdcEra, MMRProof, - NodeParams, NodePubKey, NodeUsage, PayoutState, StorageNodeParams, + ActivityHash, BatchIndex, ClusterId, ClusterStatus, CustomerUsage, DdcEra, EraValidation, + EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage, PayoutState, + StorageNodeParams, }; use frame_support::{ pallet_prelude::*, @@ -109,7 +110,7 @@ pub mod pallet { type ClusterManager: ClusterManager; type PayoutVisitor: PayoutVisitor; /// DDC nodes read-only registry. - type NodeVisitor: NodeVisitor; + type NodeManager: NodeManager; /// The output of the `ActivityHasher` function. type ActivityHash: Member + Parameter @@ -511,27 +512,6 @@ pub mod pallet { #[pallet::getter(fn get_stash_for_ddc_validator)] pub type ValidatorToStashKey = StorageMap<_, Identity, T::AccountId, T::AccountId>; - #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] - pub enum EraValidationStatus { - ValidatingData, - ReadyForPayout, - PayoutInProgress, - PayoutFailed, - PayoutSuccess, - PayoutSkipped, - } - - #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] - #[scale_info(skip_type_params(T))] - pub struct EraValidation { - pub validators: BTreeMap<(ActivityHash, ActivityHash), Vec>, /* todo! change to signatures (T::AccountId, Signature) */ - pub start_era: i64, - pub end_era: i64, - pub payers_merkle_root_hash: ActivityHash, - pub payees_merkle_root_hash: ActivityHash, - pub status: EraValidationStatus, - } - /// Era activity of a node. #[derive( Debug, @@ -2478,7 +2458,7 @@ pub mod pallet { }; for node_pub_key in nodes.iter() { - match T::NodeVisitor::get_total_usage(node_pub_key) { + match T::NodeManager::get_total_usage(node_pub_key) { Ok(usage) => results.push(usage), Err(_err) => { errors.push(OCWError::FailedToFetchNodeTotalUsage { @@ -3244,7 +3224,7 @@ pub mod pallet { for node_pub_key in nodes { // Get the node parameters if let Ok(NodeParams::StorageParams(storage_params)) = - T::NodeVisitor::get_node_params(&node_pub_key) + T::NodeManager::get_node_params(&node_pub_key) { log::info!( "🏭📝 Obtained DAC Node for cluster_id: {:?} and with key: {:?}", @@ -3413,7 +3393,7 @@ pub mod pallet { /// /// Emits `BillingReportCreated` event when successful. #[pallet::call_index(0)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::create_billing_reports() + T::DbWeight::get().reads_writes(2, 5))] // todo! implement weights pub fn set_prepare_era_for_payout( origin: OriginFor, cluster_id: ClusterId, @@ -3981,10 +3961,25 @@ pub mod pallet { impl ValidatorVisitor for Pallet { #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(validators: Vec) { + fn setup_validators(validators_with_keys: Vec<(T::AccountId, T::AccountId)>) { + let mut validators = vec![]; + for (validator, verification_key) in validators_with_keys { + ValidatorToStashKey::::insert(&verification_key, &validator); + validators.push(validator); + } + ValidatorSet::::put(validators); } + #[cfg(feature = "runtime-benchmarks")] + fn setup_validation_era( + cluster_id: ClusterId, + era_id: DdcEra, + era_validation: EraValidation, + ) { + >::insert(cluster_id, era_id, era_validation); + } + fn is_ocw_validator(caller: T::AccountId) -> bool { if ValidatorToStashKey::::contains_key(caller.clone()) { >::get().contains(&caller) diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index 0d79e64e8..3d0a94c55 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -222,7 +222,7 @@ impl crate::Config for Test { type WeightInfo = (); type ClusterManager = TestClusterManager; type ClusterValidator = TestClusterValidator; - type NodeVisitor = MockNodeVisitor; + type NodeManager = MockNodeManager; type PayoutVisitor = MockPayoutVisitor; type AuthorityId = sr25519::AuthorityId; type OffchainIdentifierId = crypto::OffchainIdentifierId; @@ -458,8 +458,8 @@ impl PayoutVisitor for MockPayoutVisitor { } } -pub struct MockNodeVisitor; -impl NodeVisitor for MockNodeVisitor { +pub struct MockNodeManager; +impl NodeManager for MockNodeManager { fn get_total_usage(_node_pub_key: &NodePubKey) -> Result, DispatchError> { Ok(None) // todo! add more complex mock } @@ -602,6 +602,7 @@ impl NodeVisitor for MockNodeVisitor { fn get_cluster_id(_node_pub_key: &NodePubKey) -> Result, DispatchError> { unimplemented!() } + fn exists(_node_pub_key: &NodePubKey) -> bool { unimplemented!() } @@ -612,6 +613,15 @@ impl NodeVisitor for MockNodeVisitor { Ok(account_1) } + + #[cfg(feature = "runtime-benchmarks")] + fn create_node( + _node_pub_key: NodePubKey, + _provider_id: T::AccountId, + _node_params: NodeParams, + ) -> DispatchResult { + unimplemented!() + } } pub struct TestClusterManager; diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index b8518e384..b4e9a9b5a 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -3,9 +3,10 @@ use blake2::{Blake2s256, Digest}; use codec::{Decode, Encode}; use frame_support::parameter_types; +use frame_system::Config; use polkadot_ckb_merkle_mountain_range::Merge; use scale_info::{ - prelude::{string::String, vec::Vec}, + prelude::{collections::BTreeMap, string::String, vec::Vec}, TypeInfo, }; use serde::{Deserialize, Serialize}; @@ -317,6 +318,11 @@ pub enum PayoutState { Finalized = 7, } +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +pub struct BucketParams { + pub is_public: bool, +} + pub const DAC_VERIFICATION_KEY_TYPE: KeyTypeId = KeyTypeId(*b"cer!"); pub mod sr25519 { @@ -362,3 +368,24 @@ pub mod crypto { type GenericPublic = sp_core::sr25519::Public; } } + +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] +pub enum EraValidationStatus { + ValidatingData, + ReadyForPayout, + PayoutInProgress, + PayoutFailed, + PayoutSuccess, + PayoutSkipped, +} + +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] +#[scale_info(skip_type_params(T))] +pub struct EraValidation { + pub validators: BTreeMap<(ActivityHash, ActivityHash), Vec>, + pub start_era: i64, + pub end_era: i64, + pub payers_merkle_root_hash: ActivityHash, + pub payees_merkle_root_hash: ActivityHash, + pub status: EraValidationStatus, +} diff --git a/primitives/src/traits/bucket.rs b/primitives/src/traits/bucket.rs index c9396fba1..8f525c81b 100644 --- a/primitives/src/traits/bucket.rs +++ b/primitives/src/traits/bucket.rs @@ -1,22 +1,29 @@ use sp_runtime::{DispatchError, DispatchResult}; +#[cfg(feature = "runtime-benchmarks")] +use crate::BucketParams; use crate::{BucketId, ClusterId, CustomerUsage}; - pub trait BucketManager { - fn inc_total_customer_usage( + fn get_bucket_owner_id(bucket_id: BucketId) -> Result; + + fn get_total_bucket_usage( + cluster_id: &ClusterId, + bucket_id: BucketId, + content_owner: &T::AccountId, + ) -> Result, DispatchError>; + + fn inc_total_bucket_usage( cluster_id: &ClusterId, bucket_id: BucketId, content_owner: T::AccountId, customer_usage: &CustomerUsage, ) -> DispatchResult; -} - -pub trait BucketVisitor { - fn get_bucket_owner_id(bucket_id: BucketId) -> Result; - fn get_total_customer_usage( + #[cfg(feature = "runtime-benchmarks")] + fn create_bucket( cluster_id: &ClusterId, bucket_id: BucketId, - content_owner: &T::AccountId, - ) -> Result, DispatchError>; + owner_id: T::AccountId, + bucket_params: BucketParams, + ) -> Result<(), DispatchError>; } diff --git a/primitives/src/traits/node.rs b/primitives/src/traits/node.rs index 4d24adb60..57952af50 100644 --- a/primitives/src/traits/node.rs +++ b/primitives/src/traits/node.rs @@ -1,18 +1,17 @@ +#[cfg(feature = "runtime-benchmarks")] use frame_support::dispatch::DispatchResult; use frame_system::Config; use sp_runtime::DispatchError; use crate::{ClusterId, NodeParams, NodePubKey, NodeUsage}; -pub trait NodeVisitor { +pub trait NodeManager { fn get_cluster_id(node_pub_key: &NodePubKey) -> Result, DispatchError>; fn exists(node_pub_key: &NodePubKey) -> bool; fn get_node_provider_id(node_pub_key: &NodePubKey) -> Result; fn get_node_params(node_pub_key: &NodePubKey) -> Result; fn get_total_usage(node_pub_key: &NodePubKey) -> Result, DispatchError>; -} - -pub trait NodeCreator { + #[cfg(feature = "runtime-benchmarks")] fn create_node( node_pub_key: NodePubKey, provider_id: T::AccountId, diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index a5e817228..dd5b14749 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -2,13 +2,21 @@ use frame_system::Config; #[cfg(feature = "runtime-benchmarks")] use scale_info::prelude::vec::Vec; +#[cfg(feature = "runtime-benchmarks")] +use crate::EraValidation; use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, }; pub trait ValidatorVisitor { #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(validators: Vec); + fn setup_validators(validators_with_keys: Vec<(T::AccountId, T::AccountId)>); + #[cfg(feature = "runtime-benchmarks")] + fn setup_validation_era( + cluster_id: ClusterId, + era_id: DdcEra, + era_validation: EraValidation, + ); fn is_ocw_validator(caller: T::AccountId) -> bool; fn is_customers_batch_valid( cluster_id: ClusterId, diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 196d81fab..873017cf7 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1185,8 +1185,7 @@ impl pallet_ddc_staking::Config for Runtime { type ClusterProtocol = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; type ClusterManager = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; - type NodeCreator = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type ClusterBondingAmount = ClusterBondingAmount; type ClusterUnboningDelay = ClusterUnboningDelay; } @@ -1240,7 +1239,7 @@ impl pallet_ddc_payouts::Config for Runtime { type PalletId = PayoutsPalletId; type Currency = Balances; type CustomerCharger = DdcCustomers; - type BucketVisitor = DdcCustomers; + type BucketManager = DdcCustomers; type CustomerDepositor = DdcCustomers; type ClusterProtocol = DdcClusters; type TreasuryVisitor = TreasuryWrapper; @@ -1249,7 +1248,7 @@ impl pallet_ddc_payouts::Config for Runtime { type WeightInfo = pallet_ddc_payouts::weights::SubstrateWeight; type VoteScoreToU64 = IdentityConvert; // used for UseNominatorsAndValidatorsMap type ValidatorVisitor = pallet_ddc_verification::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type AccountIdConverter = AccountId32; } @@ -1296,14 +1295,12 @@ impl pallet_ddc_clusters_gov::Config for Runtime { type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; type ClusterProtocol = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type SeatsConsensus = pallet_ddc_clusters_gov::Unanimous; type DefaultVote = pallet_ddc_clusters_gov::NayAsDefaultVote; type MinValidatedNodesCount = MinValidatedNodesCount; type ReferendumEnactmentDuration = ReferendumEnactmentDuration; #[cfg(feature = "runtime-benchmarks")] - type NodeCreator = pallet_ddc_nodes::Pallet; - #[cfg(feature = "runtime-benchmarks")] type StakerCreator = pallet_ddc_staking::Pallet; } @@ -1333,7 +1330,7 @@ impl pallet_ddc_verification::Config for Runtime { type WeightInfo = pallet_ddc_verification::weights::SubstrateWeight; type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterValidator = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type PayoutVisitor = pallet_ddc_payouts::Pallet; type AuthorityId = ddc_primitives::sr25519::AuthorityId; type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 72f4dfbff..3b9c667f6 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1225,7 +1225,7 @@ impl pallet_ddc_payouts::Config for Runtime { type PalletId = PayoutsPalletId; type Currency = Balances; type CustomerCharger = DdcCustomers; - type BucketVisitor = DdcCustomers; + type BucketManager = DdcCustomers; type CustomerDepositor = DdcCustomers; type ClusterProtocol = DdcClusters; type TreasuryVisitor = TreasuryWrapper; @@ -1234,7 +1234,7 @@ impl pallet_ddc_payouts::Config for Runtime { type WeightInfo = pallet_ddc_payouts::weights::SubstrateWeight; type VoteScoreToU64 = IdentityConvert; type ValidatorVisitor = pallet_ddc_verification::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type AccountIdConverter = AccountId32; } @@ -1250,8 +1250,7 @@ impl pallet_ddc_staking::Config for Runtime { type ClusterProtocol = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; type ClusterManager = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; - type NodeCreator = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type ClusterBondingAmount = ClusterBondingAmount; type ClusterUnboningDelay = ClusterUnboningDelay; } @@ -1299,14 +1298,12 @@ impl pallet_ddc_clusters_gov::Config for Runtime { type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterCreator = pallet_ddc_clusters::Pallet; type ClusterProtocol = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type SeatsConsensus = pallet_ddc_clusters_gov::Unanimous; type DefaultVote = pallet_ddc_clusters_gov::NayAsDefaultVote; type MinValidatedNodesCount = MinValidatedNodesCount; type ReferendumEnactmentDuration = ReferendumEnactmentDuration; #[cfg(feature = "runtime-benchmarks")] - type NodeCreator = pallet_ddc_nodes::Pallet; - #[cfg(feature = "runtime-benchmarks")] type StakerCreator = pallet_ddc_staking::Pallet; } @@ -1337,7 +1334,7 @@ impl pallet_ddc_verification::Config for Runtime { type WeightInfo = pallet_ddc_verification::weights::SubstrateWeight; type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterValidator = pallet_ddc_clusters::Pallet; - type NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type PayoutVisitor = pallet_ddc_payouts::Pallet; type AuthorityId = ddc_primitives::sr25519::AuthorityId; type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; From 7cd03fd0f0cf1b2e5637e593260f0293a0d847b1 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Wed, 13 Nov 2024 17:06:50 +0100 Subject: [PATCH 02/15] fix: compilation error is fixed in tests --- pallets/ddc-nodes/src/lib.rs | 23 ++++++++++----- pallets/ddc-nodes/src/tests.rs | 37 ------------------------ pallets/ddc-staking/src/testing_utils.rs | 4 +++ 3 files changed, 20 insertions(+), 44 deletions(-) diff --git a/pallets/ddc-nodes/src/lib.rs b/pallets/ddc-nodes/src/lib.rs index 3f2f7ef31..413268b0d 100644 --- a/pallets/ddc-nodes/src/lib.rs +++ b/pallets/ddc-nodes/src/lib.rs @@ -119,10 +119,7 @@ pub mod pallet { node_params: NodeParams, ) -> DispatchResult { let caller_id = ensure_signed(origin)?; - let node = Node::::new(node_pub_key.clone(), caller_id, node_params) - .map_err(Into::>::into)?; - Self::create(node).map_err(Into::>::into)?; - Self::deposit_event(Event::::NodeCreated { node_pub_key }); + Self::do_create_node(node_pub_key, caller_id, node_params)?; Ok(()) } @@ -157,6 +154,20 @@ pub mod pallet { } } + impl Pallet { + fn do_create_node( + node_pub_key: NodePubKey, + provider_id: T::AccountId, + node_params: NodeParams, + ) -> DispatchResult { + let node = Node::::new(node_pub_key.clone(), provider_id, node_params) + .map_err(Into::>::into)?; + Self::create(node).map_err(Into::>::into)?; + Self::deposit_event(Event::::NodeCreated { node_pub_key }); + Ok(()) + } + } + pub trait NodeRepository { fn create(node: Node) -> Result<(), NodeRepositoryError>; fn get(node_pub_key: NodePubKey) -> Result, NodeRepositoryError>; @@ -271,9 +282,7 @@ pub mod pallet { provider_id: T::AccountId, node_params: NodeParams, ) -> DispatchResult { - let node = Node::::new(node_pub_key, provider_id, node_params) - .map_err(Into::>::into)?; - Self::create(node).map_err(Into::>::into)?; + Self::do_create_node(node_pub_key, provider_id, node_params)?; Ok(()) } } diff --git a/pallets/ddc-nodes/src/tests.rs b/pallets/ddc-nodes/src/tests.rs index 3bc8230e3..2af986cc7 100644 --- a/pallets/ddc-nodes/src/tests.rs +++ b/pallets/ddc-nodes/src/tests.rs @@ -114,43 +114,6 @@ fn create_storage_node_works() { }) } -#[test] -fn create_storage_node_with_node_creator() { - ExtBuilder.build_and_execute(|| { - System::set_block_number(1); - let bytes = [0u8; 32]; - let node_pub_key = AccountId32::from(bytes); - let storage_node_params = StorageNodeParams { - mode: StorageNodeMode::Storage, - host: vec![1u8; 255], - domain: vec![2u8; 255], - ssl: true, - http_port: 35000u16, - grpc_port: 25000u16, - p2p_port: 15000u16, - }; - let account_id1 = AccountId::from([1; 32]); - - // Node created - assert_ok!(>::create_node( - NodePubKey::StoragePubKey(node_pub_key.clone()), - account_id1, - NodeParams::StorageParams(storage_node_params) - )); - - // Check storage - assert!(StorageNodes::::contains_key(node_pub_key.clone())); - assert!(DdcNodes::exists(&NodePubKey::StoragePubKey(node_pub_key.clone()))); - if let Ok(cluster_id) = - DdcNodes::get_cluster_id(&NodePubKey::StoragePubKey(node_pub_key.clone())) - { - assert_eq!(cluster_id, None); - } - let storage_node = DdcNodes::storage_nodes(&node_pub_key).unwrap(); - assert_eq!(storage_node.pub_key, node_pub_key); - }) -} - #[test] fn set_storage_node_params_works() { ExtBuilder.build_and_execute(|| { diff --git a/pallets/ddc-staking/src/testing_utils.rs b/pallets/ddc-staking/src/testing_utils.rs index d72b175fb..e6a790200 100644 --- a/pallets/ddc-staking/src/testing_utils.rs +++ b/pallets/ddc-staking/src/testing_utils.rs @@ -1,10 +1,12 @@ //! Testing utils for ddc-staking. +#[cfg(feature = "runtime-benchmarks")] use ddc_primitives::{ ClusterId, ClusterParams, ClusterProtocolParams, NodeParams, StorageNodeMode, StorageNodeParams, StorageNodePubKey, }; use frame_benchmarking::account; +#[cfg(feature = "runtime-benchmarks")] use frame_support::traits::Currency; use frame_system::RawOrigin; use sp_runtime::{traits::StaticLookup, Perquintill}; @@ -47,6 +49,7 @@ pub fn create_funded_user_with_balance( } /// Create a stash and controller pair. +#[cfg(feature = "runtime-benchmarks")] pub fn create_stash_controller_node( n: u32, balance_factor: u32, @@ -81,6 +84,7 @@ pub fn create_stash_controller_node( } /// Create a stash and controller pair with fixed balance. +#[cfg(feature = "runtime-benchmarks")] pub fn create_stash_controller_node_with_balance( n: u32, balance_factor: u128, From 67d2e03807a4c16bd196c0b5b1758da25d5bc354 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Wed, 13 Nov 2024 23:09:51 +0100 Subject: [PATCH 03/15] feat: benchmarking for ddc-verification pallet calls part 1 --- .vscode/settings.json | 2 +- pallets/ddc-verification/src/benchmarking.rs | 82 ++++++++++++++++---- pallets/ddc-verification/src/lib.rs | 15 +++- pallets/ddc-verification/src/mock.rs | 2 +- runtime/cere-dev/src/lib.rs | 5 +- runtime/cere/src/lib.rs | 5 +- 6 files changed, 91 insertions(+), 20 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 0196fd738..7089a4652 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", - "editor.formatOnSave": true + "editor.formatOnSave": false } } \ No newline at end of file diff --git a/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index 6695674e5..a46d444f2 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -1,10 +1,13 @@ #![cfg(feature = "runtime-benchmarks")] -use frame_benchmarking::v2::*; +use frame_benchmarking::{account, v2::*}; use frame_system::RawOrigin; +use sp_io::hashing::blake2_256; +use sp_runtime::{SaturatedConversion, Saturating}; use sp_std::vec; use super::*; +use crate::EraActivity; #[allow(unused)] use crate::Pallet as DdcVerification; @@ -12,25 +15,76 @@ use crate::Pallet as DdcVerification; mod benchmarks { use super::*; + fn create_validator_account() -> T::AccountId { + let validator = create_account::("validator_account", 0, 0); + as ValidatorVisitor>::setup_validators(vec![( + validator.clone(), + validator.clone(), + )]); + validator + } + + fn create_account(name: &'static str, idx: u32, seed: u32) -> T::AccountId { + account::(name, idx, seed) + } + + fn assert_has_event(generic_event: ::RuntimeEvent) { + frame_system::Pallet::::assert_has_event(generic_event.into()); + } + + fn endow_account(account: &T::AccountId, amount: u128) { + let balance = amount.saturated_into::>(); + let _ = T::Currency::make_free_balance_be(account, amount.saturated_into()); + } + #[benchmark] - fn create_billing_reports() { + fn set_prepare_era_for_payout(b: Linear<1, 5>) { + let validator = create_validator_account::(); let cluster_id = ClusterId::from([1; 20]); - let era: DdcEra = 1; - let merkel_root_hash: MmrRootHash = array_bytes::hex_n_into_unchecked( - "95803defe6ea9f41e7ec6afa497064f21bfded027d8812efacbdf984e630cbdc", - ); - let caller: T::AccountId = whitelisted_caller(); + let era = EraActivity { id: 1, start: 1000, end: 2000 }; + + let payers_merkle_root_hash = blake2_256(&1.encode()); + let payees_merkle_root_hash = blake2_256(&2.encode()); + + let mut payers_batch_merkle_root_hashes = vec![]; + let mut payees_batch_merkle_root_hashes = vec![]; + + for i in 0..b { + payers_batch_merkle_root_hashes.push(blake2_256(&(i + 10).encode())); + payees_batch_merkle_root_hashes.push(blake2_256(&(i + 100).encode())) + } + #[extrinsic_call] - create_billing_reports( - RawOrigin::Signed(caller), + set_prepare_era_for_payout( + RawOrigin::Signed(validator), cluster_id, - era, - merkel_root_hash, + era.clone(), + payers_merkle_root_hash, + payees_merkle_root_hash, + payers_batch_merkle_root_hashes, + payees_batch_merkle_root_hashes, ); - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.merkle_root_hash, ActivityHash::from(merkel_root_hash)); + assert!(>::contains_key(cluster_id, era.id)); + assert_has_event::(Event::EraValidationReady { cluster_id, era_id: era.id }.into()); + } + + #[benchmark] + fn set_validator_key() { + let validator = create_account::("new_validator_account", 0, 0); + endow_account::(&validator, 10000000000000000000000); + + let bond = T::ValidatorStaking::minimum_validator_bond(); + let stash = + T::ValidatorStaking::bond(&validator, bond, &validator).expect("Bond to be created"); + + ValidatorSet::::put(vec![validator.clone()]); + + #[extrinsic_call] + set_validator_key(RawOrigin::Signed(validator.clone()), validator.clone()); + + assert!(>::contains_key(validator.clone())); + assert_has_event::(Event::ValidatorKeySet { validator }.into()); } impl_benchmark_test_suite!(DdcVerification, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 35a71e8d4..daa2b2a17 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -45,6 +45,8 @@ use sp_runtime::{ }; use sp_std::{collections::btree_map::BTreeMap, prelude::*}; pub mod weights; +#[cfg(feature = "runtime-benchmarks")] +use frame_support::traits::{Currency, LockableCurrency}; use itertools::Itertools; use rand::{prelude::*, rngs::SmallRng, SeedableRng}; use sp_core::crypto::UncheckedFrom; @@ -55,6 +57,9 @@ use sp_std::fmt::Debug; use crate::weights::WeightInfo; +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; + #[cfg(test)] pub(crate) mod mock; #[cfg(test)] @@ -70,6 +75,10 @@ pub mod proto { mod signature; +#[cfg(feature = "runtime-benchmarks")] +pub(crate) type BalanceOf = + <::Currency as Currency<::AccountId>>::Balance; + #[frame_support::pallet] pub mod pallet { @@ -143,9 +152,11 @@ pub mod pallet { const MAX_PAYOUT_BATCH_SIZE: u16; const MAX_MERKLE_NODE_IDENTIFIER: u16; /// The access to staking functionality. - type StakingVisitor: StakingInterface; + type ValidatorStaking: StakingInterface; type AccountIdConverter: From + Into; type CustomerVisitor: CustomerVisitor; + #[cfg(feature = "runtime-benchmarks")] + type Currency: LockableCurrency>; } /// The event type. @@ -3758,7 +3769,7 @@ pub mod pallet { ) -> DispatchResult { let controller = ensure_signed(origin)?; - let stash = T::StakingVisitor::stash_by_ctrl(&controller) + let stash = T::ValidatorStaking::stash_by_ctrl(&controller) .map_err(|_| Error::::NotController)?; ensure!( diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index 3d0a94c55..6c9d2f50b 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -234,7 +234,7 @@ impl crate::Config for Test { const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE; const MAX_PAYOUT_BATCH_COUNT: u16 = MAX_PAYOUT_BATCH_COUNT; type ActivityHash = H256; - type StakingVisitor = Staking; + type ValidatorStaking = Staking; type AccountIdConverter = AccountId; type CustomerVisitor = MockCustomerVisitor; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 4; diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 873017cf7..d57e4a475 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1342,10 +1342,12 @@ impl pallet_ddc_verification::Config for Runtime { const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE; const MAX_PAYOUT_BATCH_COUNT: u16 = MAX_PAYOUT_BATCH_COUNT; type ActivityHash = H256; - type StakingVisitor = pallet_staking::Pallet; + type ValidatorStaking = pallet_staking::Pallet; type AccountIdConverter = AccountId32; type CustomerVisitor = pallet_ddc_customers::Pallet; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 3; + #[cfg(feature = "runtime-benchmarks")] + type Currency = Balances; } construct_runtime!( @@ -1522,6 +1524,7 @@ mod benches { [pallet_whitelist, Whitelist] [pallet_collective, TechComm] [pallet_ddc_clusters_gov, DdcClustersGov] + [pallet_ddc_verification, DdcVerification] ); } diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 3b9c667f6..cfd1cadd5 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1346,10 +1346,12 @@ impl pallet_ddc_verification::Config for Runtime { const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE; const MAX_PAYOUT_BATCH_COUNT: u16 = MAX_PAYOUT_BATCH_COUNT; type ActivityHash = H256; - type StakingVisitor = pallet_staking::Pallet; + type ValidatorStaking = pallet_staking::Pallet; type AccountIdConverter = AccountId32; type CustomerVisitor = pallet_ddc_customers::Pallet; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 3; + #[cfg(feature = "runtime-benchmarks")] + type Currency = Balances; } construct_runtime!( @@ -1528,6 +1530,7 @@ mod benches { [pallet_whitelist, Whitelist] [pallet_collective, TechComm] [pallet_ddc_clusters_gov, DdcClustersGov] + [pallet_ddc_verification, DdcVerification] ); } From 9e8651292b2b5303d8a7fe414962778a1b7b7531 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 14 Nov 2024 14:23:15 +0100 Subject: [PATCH 04/15] feat: benchmarking for ddc-verification pallet calls part 2 --- pallets/ddc-staking/src/testing_utils.rs | 7 ++++- pallets/ddc-verification/src/benchmarking.rs | 28 ++++++++++++++++---- pallets/ddc-verification/src/lib.rs | 12 ++++----- pallets/ddc-verification/src/mock.rs | 1 + runtime/cere-dev/src/lib.rs | 1 - runtime/cere/src/lib.rs | 1 - 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/pallets/ddc-staking/src/testing_utils.rs b/pallets/ddc-staking/src/testing_utils.rs index e6a790200..853abbc1a 100644 --- a/pallets/ddc-staking/src/testing_utils.rs +++ b/pallets/ddc-staking/src/testing_utils.rs @@ -8,11 +8,16 @@ use ddc_primitives::{ use frame_benchmarking::account; #[cfg(feature = "runtime-benchmarks")] use frame_support::traits::Currency; +#[cfg(feature = "runtime-benchmarks")] use frame_system::RawOrigin; +#[cfg(feature = "runtime-benchmarks")] use sp_runtime::{traits::StaticLookup, Perquintill}; +#[cfg(feature = "runtime-benchmarks")] use sp_std::prelude::*; -use crate::{Pallet as DdcStaking, *}; +#[cfg(feature = "runtime-benchmarks")] +use crate::Pallet as DdcStaking; +use crate::*; const SEED: u32 = 0; diff --git a/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index a46d444f2..38387eba4 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -34,7 +34,7 @@ mod benchmarks { fn endow_account(account: &T::AccountId, amount: u128) { let balance = amount.saturated_into::>(); - let _ = T::Currency::make_free_balance_be(account, amount.saturated_into()); + let _ = T::Currency::make_free_balance_be(account, balance); } #[benchmark] @@ -72,11 +72,12 @@ mod benchmarks { #[benchmark] fn set_validator_key() { let validator = create_account::("new_validator_account", 0, 0); - endow_account::(&validator, 10000000000000000000000); + endow_account::(&validator, 10000000000000000); - let bond = T::ValidatorStaking::minimum_validator_bond(); - let stash = - T::ValidatorStaking::bond(&validator, bond, &validator).expect("Bond to be created"); + let min_bond = T::ValidatorStaking::minimum_validator_bond(); + let bond = min_bond.saturating_add(T::Currency::minimum_balance()); + + T::ValidatorStaking::bond(&validator, bond, &validator).expect("Bond to be created"); ValidatorSet::::put(vec![validator.clone()]); @@ -87,5 +88,22 @@ mod benchmarks { assert_has_event::(Event::ValidatorKeySet { validator }.into()); } + #[benchmark] + fn emit_consensus_errors(b: Linear<1, 5>) { + let validator = create_validator_account::(); + let mut errros = vec![]; + + for _i in 0..b { + errros.push(OCWError::SendChargingCustomersBatchTransactionError { + cluster_id: ClusterId::from([1; 20]), + era_id: 1, + batch_index: 0, + }); + } + + #[extrinsic_call] + emit_consensus_errors(RawOrigin::Signed(validator.clone()), errros); + } + impl_benchmark_test_suite!(DdcVerification, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index daa2b2a17..455a36833 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -45,8 +45,7 @@ use sp_runtime::{ }; use sp_std::{collections::btree_map::BTreeMap, prelude::*}; pub mod weights; -#[cfg(feature = "runtime-benchmarks")] -use frame_support::traits::{Currency, LockableCurrency}; +use frame_support::traits::Currency; use itertools::Itertools; use rand::{prelude::*, rngs::SmallRng, SeedableRng}; use sp_core::crypto::UncheckedFrom; @@ -75,7 +74,6 @@ pub mod proto { mod signature; -#[cfg(feature = "runtime-benchmarks")] pub(crate) type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -152,11 +150,13 @@ pub mod pallet { const MAX_PAYOUT_BATCH_SIZE: u16; const MAX_MERKLE_NODE_IDENTIFIER: u16; /// The access to staking functionality. - type ValidatorStaking: StakingInterface; + type ValidatorStaking: StakingInterface< + AccountId = Self::AccountId, + Balance = BalanceOf, + >; type AccountIdConverter: From + Into; type CustomerVisitor: CustomerVisitor; - #[cfg(feature = "runtime-benchmarks")] - type Currency: LockableCurrency>; + type Currency: Currency; } /// The event type. diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index 6c9d2f50b..f86f45a52 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -238,6 +238,7 @@ impl crate::Config for Test { type AccountIdConverter = AccountId; type CustomerVisitor = MockCustomerVisitor; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 4; + type Currency = Balances; } pub struct MockCustomerVisitor; diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index d57e4a475..1976bc8dd 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1346,7 +1346,6 @@ impl pallet_ddc_verification::Config for Runtime { type AccountIdConverter = AccountId32; type CustomerVisitor = pallet_ddc_customers::Pallet; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 3; - #[cfg(feature = "runtime-benchmarks")] type Currency = Balances; } diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index cfd1cadd5..4f88d7b5b 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1350,7 +1350,6 @@ impl pallet_ddc_verification::Config for Runtime { type AccountIdConverter = AccountId32; type CustomerVisitor = pallet_ddc_customers::Pallet; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 3; - #[cfg(feature = "runtime-benchmarks")] type Currency = Balances; } From a0328fc0d4c79ca9bf7d6ff18cd27848a2a991d5 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 14 Nov 2024 14:50:39 +0100 Subject: [PATCH 05/15] chore: 'AuthorisedCaller' storage item is removed --- node/service/chain-specs/example.json | 1 - pallets/ddc-payouts/src/benchmarking.rs | 8 --- pallets/ddc-payouts/src/lib.rs | 29 ++--------- pallets/ddc-payouts/src/migrations.rs | 69 +++++++++++++++++++++++++ pallets/ddc-payouts/src/weights.rs | 45 ---------------- runtime/cere-dev/src/lib.rs | 33 +----------- runtime/cere/src/lib.rs | 11 +++- 7 files changed, 82 insertions(+), 114 deletions(-) create mode 100644 pallets/ddc-payouts/src/migrations.rs diff --git a/node/service/chain-specs/example.json b/node/service/chain-specs/example.json index 008ca543f..3355d5ca4 100644 --- a/node/service/chain-specs/example.json +++ b/node/service/chain-specs/example.json @@ -215,7 +215,6 @@ }, "ddcPayouts": { "feederAccount": null, - "authorisedCaller": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", "debtorCustomers": [ [ "0x0000000000000000000000000000000000000001", diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs index a89cb5d24..de5a1ad6b 100644 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ b/pallets/ddc-payouts/src/benchmarking.rs @@ -138,14 +138,6 @@ fn set_validation_era( benchmarks! { - set_authorised_caller { - let dac_account = create_account::("dac_account", 0, 0); - - }: _(RawOrigin::Root, dac_account.clone()) - verify { - assert_eq!(AuthorisedCaller::::get(), Some(dac_account)); - } - begin_billing_report { let cluster_id = ClusterId::from([1; 20]); diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 58772cd9b..4fd9d38df 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -26,6 +26,8 @@ pub(crate) mod mock; #[cfg(test)] mod tests; +pub mod migrations; + use ddc_primitives::{ traits::{ bucket::BucketManager, @@ -105,7 +107,7 @@ pub mod pallet { /// The current storage version. const STORAGE_VERSION: frame_support::traits::StorageVersion = - frame_support::traits::StorageVersion::new(0); + frame_support::traits::StorageVersion::new(1); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] @@ -227,9 +229,6 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, }, - AuthorisedCaller { - authorised_caller: T::AccountId, - }, ChargeError { cluster_id: ClusterId, era: DdcEra, @@ -276,10 +275,6 @@ pub mod pallet { BillingReport, >; - #[pallet::storage] - #[pallet::getter(fn authorised_caller)] - pub type AuthorisedCaller = StorageValue<_, T::AccountId>; - #[pallet::storage] #[pallet::getter(fn debtor_customers)] pub type DebtorCustomers = @@ -342,23 +337,6 @@ pub mod pallet { #[pallet::call] impl Pallet { - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // PayoutProcessor trait - #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::set_authorised_caller())] - pub fn set_authorised_caller( - origin: OriginFor, - authorised_caller: T::AccountId, - ) -> DispatchResult { - ensure_root(origin)?; // requires Governance approval - - AuthorisedCaller::::put(authorised_caller.clone()); - - Self::deposit_event(Event::::AuthorisedCaller { authorised_caller }); - - Ok(()) - } - #[pallet::call_index(1)] #[pallet::weight(T::WeightInfo::begin_billing_report())] pub fn begin_billing_report( @@ -1169,7 +1147,6 @@ pub mod pallet { } } - AuthorisedCaller::::set(self.authorised_caller.clone()); for (cluster_id, customer_id, debt) in &self.debtor_customers { DebtorCustomers::::insert(cluster_id, customer_id, debt); } diff --git a/pallets/ddc-payouts/src/migrations.rs b/pallets/ddc-payouts/src/migrations.rs new file mode 100644 index 000000000..b53769f6f --- /dev/null +++ b/pallets/ddc-payouts/src/migrations.rs @@ -0,0 +1,69 @@ +use frame_support::{migration, traits::OnRuntimeUpgrade}; +use log; + +use super::*; + +const LOG_TARGET: &str = "ddc-payouts"; + +pub mod v1 { + use frame_support::pallet_prelude::*; + + use super::*; + + pub fn migrate_to_v1() -> Weight { + let on_chain_version = Pallet::::on_chain_storage_version(); + let current_version = Pallet::::current_storage_version(); + + log::info!( + target: LOG_TARGET, + "Running migration with current storage version {:?} / onchain {:?}", + current_version, + on_chain_version + ); + + if on_chain_version == 0 && current_version == 1 { + log::info!(target: LOG_TARGET, "Running migration to v1."); + + let res = migration::clear_storage_prefix( + >::name().as_bytes(), + b"AuthorisedCaller", + b"", + None, + None, + ); + + log::info!( + target: LOG_TARGET, + "Cleared '{}' entries from 'AuthorisedCaller' storage prefix.", + res.unique + ); + + if res.maybe_cursor.is_some() { + log::error!( + target: LOG_TARGET, + "Storage prefix 'AuthorisedCaller' is not completely cleared." + ); + } + + // Update storage version. + StorageVersion::new(1).put::>(); + log::info!( + target: LOG_TARGET, + "Storage migrated to version {:?}", + current_version + ); + + T::DbWeight::get().reads_writes(1, res.unique.into()) + } else { + log::info!(target: LOG_TARGET, " >>> Unused migration!"); + T::DbWeight::get().reads(1) + } + } + + pub struct MigrateToV1(sp_std::marker::PhantomData); + impl OnRuntimeUpgrade for MigrateToV1 { + fn on_runtime_upgrade() -> Weight { + migrate_to_v1::() + } + } +} diff --git a/pallets/ddc-payouts/src/weights.rs b/pallets/ddc-payouts/src/weights.rs index 063b7141c..9b621d531 100644 --- a/pallets/ddc-payouts/src/weights.rs +++ b/pallets/ddc-payouts/src/weights.rs @@ -28,7 +28,6 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_ddc_payouts. pub trait WeightInfo { - fn set_authorised_caller() -> Weight; fn begin_billing_report() -> Weight; fn begin_charging_customers() -> Weight; fn send_charging_customers_batch(b: u32, ) -> Weight; @@ -42,14 +41,6 @@ pub trait WeightInfo { /// Weights for pallet_ddc_payouts using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - // Storage: `DdcPayouts::AuthorisedCaller` (r:0 w:1) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) - fn set_authorised_caller() -> Weight { - Weight::from_parts(12_694_000_u64, 0) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_billing_report() -> Weight { @@ -57,8 +48,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_charging_customers() -> Weight { @@ -66,8 +55,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) @@ -88,8 +75,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().writes(5_u64)) .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(b as u64))) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) @@ -111,8 +96,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(12_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_rewarding_providers() -> Weight { @@ -120,8 +103,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) // Storage: `System::Account` (r:1000 w:1000) @@ -136,8 +117,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().writes(2_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(b as u64))) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_rewarding_providers() -> Weight { @@ -145,8 +124,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_billing_report() -> Weight { @@ -158,14 +135,6 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - // Storage: `DdcPayouts::AuthorisedCaller` (r:0 w:1) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) - fn set_authorised_caller() -> Weight { - Weight::from_parts(12_694_000_u64, 0) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_billing_report() -> Weight { @@ -173,8 +142,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_charging_customers() -> Weight { @@ -182,8 +149,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) @@ -204,8 +169,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().writes(5_u64)) .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(b as u64))) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) @@ -227,8 +190,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(12_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_rewarding_providers() -> Weight { @@ -236,8 +197,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) // Storage: `System::Account` (r:1000 w:1000) @@ -252,8 +211,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().writes(2_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(b as u64))) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_rewarding_providers() -> Weight { @@ -261,8 +218,6 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - // Storage: `DdcPayouts::AuthorisedCaller` (r:1 w:0) - // Proof: `DdcPayouts::AuthorisedCaller` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_billing_report() -> Weight { diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 1976bc8dd..30f445bec 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -527,22 +527,6 @@ impl_opaque_keys! { } } -fn transform_session_keys(v: AccountId, old: OldSessionKeys) -> SessionKeys { - SessionKeys { - grandpa: old.grandpa, - babe: old.babe, - im_online: old.im_online, - authority_discovery: old.authority_discovery, - ddc_verification: { - let mut id: ddc_primitives::sr25519::AuthorityId = - sp_core::sr25519::Public::from_raw([0u8; 32]).into(); - let id_raw: &mut [u8] = id.as_mut(); - id_raw[0..32].copy_from_slice(v.as_ref()); - id_raw[0..4].copy_from_slice(b"cer!"); - id - }, - } -} impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = ::AccountId; @@ -1446,6 +1430,7 @@ type Migrations = ( pallet_nomination_pools::migration::versioned_migrations::V6ToV7, pallet_staking::migrations::v14::MigrateToV14, pallet_grandpa::migrations::MigrateV4ToV5, + pallet_ddc_payouts::migrations::v1::MigrateToV1, ); /// Executive: handles dispatch to the various modules. @@ -1458,22 +1443,6 @@ pub type Executive = frame_executive::Executive< Migrations, >; -pub mod migrations { - use super::*; - - /// When this is removed, should also remove `OldSessionKeys`. - pub struct UpgradeSessionKeys; - impl frame_support::traits::OnRuntimeUpgrade for UpgradeSessionKeys { - fn on_runtime_upgrade() -> Weight { - Session::upgrade_keys::(transform_session_keys); - Perbill::from_percent(50) * RuntimeBlockWeights::get().max_block - } - } - - /// Unreleased migrations. Add new ones here: - pub type Unreleased = (UpgradeSessionKeys,); -} - type EventRecord = frame_system::EventRecord< ::RuntimeEvent, ::Hash, diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 4f88d7b5b..0416b471c 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1467,9 +1467,16 @@ pub mod migrations { } /// Unreleased migrations. Add new ones here: - pub type Unreleased = - (UpgradeSessionKeys, pallet_ddc_verification::migrations::v1::MigrateToV1); + pub type Unreleased = ( + pallet_ddc_customers::migration::v2::MigrateToV2, + pallet_ddc_clusters::migrations::v3::MigrateToV3, + pallet_ddc_nodes::migrations::v1::MigrateToV1, + UpgradeSessionKeys, + pallet_ddc_verification::migrations::v1::MigrateToV1, + pallet_ddc_payouts::migrations::v1::MigrateToV1, + ); } + /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< Runtime, From d39771239a6e0807b84bffce5050bf570c500e52 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 14 Nov 2024 16:46:45 +0100 Subject: [PATCH 06/15] refactor: ddc-payouts processor and tests --- pallets/ddc-payouts/src/lib.rs | 739 +++++++----------- pallets/ddc-payouts/src/tests.rs | 1071 +++++++------------------- pallets/ddc-verification/src/lib.rs | 69 +- pallets/ddc-verification/src/mock.rs | 93 +-- primitives/src/traits/payout.rs | 45 +- runtime/cere-dev/src/lib.rs | 2 +- runtime/cere/src/lib.rs | 2 +- 7 files changed, 619 insertions(+), 1402 deletions(-) diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 4fd9d38df..dd8d659dc 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -37,7 +37,7 @@ use ddc_primitives::{ }, node::NodeManager, pallet::PalletVisitor as PalletVisitorType, - payout::PayoutVisitor, + payout::PayoutProcessor, }, BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, MILLICENTS, @@ -336,19 +336,262 @@ pub mod pallet { } #[pallet::call] + impl Pallet {} + + fn charge_treasury_fees( + treasury_fee: u128, + vault: &T::AccountId, + treasury_vault: &T::AccountId, + ) -> DispatchResult { + let amount_to_deduct = treasury_fee.saturated_into::>(); + ::Currency::transfer( + vault, + treasury_vault, + amount_to_deduct, + ExistenceRequirement::AllowDeath, + ) + } + + fn charge_cluster_reserve_fees( + cluster_reserve_fee: u128, + vault: &T::AccountId, + reserve_vault: &T::AccountId, + ) -> DispatchResult { + let amount_to_deduct = cluster_reserve_fee.saturated_into::>(); + ::Currency::transfer( + vault, + reserve_vault, + amount_to_deduct, + ExistenceRequirement::AllowDeath, + ) + } + + fn get_current_exposure_ratios( + ) -> Result, DispatchError> { + let mut total_score = 0; + let mut individual_scores: Vec<(T::AccountId, u64)> = Vec::new(); + for staker_id in T::NominatorsAndValidatorsList::iter() { + let s = T::NominatorsAndValidatorsList::get_score(&staker_id) + .map_err(|_| Error::::ScoreRetrievalError)?; + let score = T::VoteScoreToU64::convert(s); + total_score += score; + + individual_scores.push((staker_id, score)); + } + + let mut result = Vec::new(); + for (staker_id, score) in individual_scores { + let ratio = Perquintill::from_rational(score, total_score); + result.push((staker_id, ratio)); + } + + Ok(result) + } + + fn charge_validator_fees( + validators_fee: u128, + vault: &T::AccountId, + cluster_id: ClusterId, + era: DdcEra, + ) -> DispatchResult { + let stakers = get_current_exposure_ratios::()?; + + for (staker_id, ratio) in stakers.iter() { + let amount_to_deduct = *ratio * validators_fee; + + ::Currency::transfer( + vault, + staker_id, + amount_to_deduct.saturated_into::>(), + ExistenceRequirement::AllowDeath, + )?; + + pallet::Pallet::deposit_event(Event::::ValidatorRewarded { + cluster_id, + era, + validator_id: staker_id.clone(), + amount: amount_to_deduct, + }); + } + + Ok(()) + } + + fn get_node_reward( + node_usage: &NodeUsage, + total_nodes_usage: &NodeUsage, + total_customer_charge: &CustomerCharge, + ) -> Option { + let mut node_reward = NodeReward::default(); + + let mut ratio = Perquintill::from_rational( + node_usage.transferred_bytes as u128, + total_nodes_usage.transferred_bytes as u128, + ); + + // ratio multiplied by X will be > 0, < X no overflow + node_reward.transfer = ratio * total_customer_charge.transfer; + + ratio = Perquintill::from_rational( + node_usage.stored_bytes as u128, + total_nodes_usage.stored_bytes as u128, + ); + node_reward.storage = ratio * total_customer_charge.storage; + + ratio = + Perquintill::from_rational(node_usage.number_of_puts, total_nodes_usage.number_of_puts); + node_reward.puts = ratio * total_customer_charge.puts; + + ratio = + Perquintill::from_rational(node_usage.number_of_gets, total_nodes_usage.number_of_gets); + node_reward.gets = ratio * total_customer_charge.gets; + + Some(node_reward) + } + + fn get_customer_charge( + cluster_id: &ClusterId, + usage: &CustomerUsage, + bucket_id: BucketId, + customer_id: &T::AccountId, + start_era: i64, + end_era: i64, + ) -> Result { + let mut total = CustomerCharge::default(); + + let pricing = T::ClusterProtocol::get_pricing_params(cluster_id) + .map_err(|_| Error::::NotExpectedClusterState)?; + + total.transfer = (|| -> Option { + (usage.transferred_bytes as u128) + .checked_mul(pricing.unit_per_mb_streamed)? + .checked_div(byte_unit::MEBIBYTE) + })() + .ok_or(Error::::ArithmeticOverflow)?; + + // Calculate the duration of the period in seconds + let duration_seconds = end_era - start_era; + let seconds_in_month = 30.44 * 24.0 * 3600.0; + let fraction_of_month = + Perquintill::from_rational(duration_seconds as u64, seconds_in_month as u64); + + let mut total_stored_bytes: i64 = + T::BucketManager::get_total_bucket_usage(cluster_id, bucket_id, customer_id)? + .map_or(0, |customer_usage| customer_usage.stored_bytes); + + total_stored_bytes = total_stored_bytes + .checked_add(usage.stored_bytes) + .ok_or(Error::::ArithmeticOverflow)? + .max(0); + + total.storage = fraction_of_month * + (|| -> Option { + (total_stored_bytes as u128) + .checked_mul(pricing.unit_per_mb_stored)? + .checked_div(byte_unit::MEBIBYTE) + })() + .ok_or(Error::::ArithmeticOverflow)?; + + total.gets = (usage.number_of_gets as u128) + .checked_mul(pricing.unit_per_get_request) + .ok_or(Error::::ArithmeticOverflow)?; + + total.puts = (usage.number_of_puts as u128) + .checked_mul(pricing.unit_per_put_request) + .ok_or(Error::::ArithmeticOverflow)?; + + Ok(total) + } + + #[pallet::genesis_config] + pub struct GenesisConfig { + pub feeder_account: Option, + pub authorised_caller: Option, + pub debtor_customers: Vec<(ClusterId, T::AccountId, u128)>, + } + + impl Default for GenesisConfig { + fn default() -> Self { + GenesisConfig { + feeder_account: None, + authorised_caller: Default::default(), + debtor_customers: Default::default(), + } + } + } + + #[pallet::genesis_build] + impl BuildGenesisConfig for GenesisConfig { + fn build(&self) { + let account_id = >::account_id(); + let min = ::Currency::minimum_balance(); + let balance = ::Currency::free_balance(&account_id); + if balance < min { + if let Some(vault) = &self.feeder_account { + let _ = ::Currency::transfer( + vault, + &account_id, + min - balance, + ExistenceRequirement::AllowDeath, + ); + } else { + let _ = ::Currency::make_free_balance_be(&account_id, min); + } + } + + for (cluster_id, customer_id, debt) in &self.debtor_customers { + DebtorCustomers::::insert(cluster_id, customer_id, debt); + } + } + } + impl Pallet { - #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::begin_billing_report())] - pub fn begin_billing_report( - origin: OriginFor, + pub fn account_id() -> T::AccountId { + T::PalletId::get().into_account_truncating() + } + + pub fn get_account_id_string(caller: T::AccountId) -> String { + let account_id: T::AccountIdConverter = caller.into(); + let account_id_32: AccountId32 = account_id.into(); + let account_ref: &[u8; 32] = account_id_32.as_ref(); + hex::encode(account_ref) + } + pub fn sub_account_id(cluster_id: ClusterId, era: DdcEra) -> T::AccountId { + let mut bytes = Vec::new(); + bytes.extend_from_slice(&cluster_id[..]); + bytes.extend_from_slice(&era.encode()); + let hash = blake2_128(&bytes); + + // "modl" + "payouts_" + hash is 28 bytes, the T::AccountId is 32 bytes, so we should be + // safe from the truncation and possible collisions caused by it. The rest 4 bytes will + // be fulfilled with trailing zeros. + T::PalletId::get().into_sub_account_truncating(hash) + } + + pub(crate) fn validate_batches( + batches: &BoundedBTreeSet, + max_batch_index: &BatchIndex, + ) -> DispatchResult { + // Check if the Vec contains all integers between 1 and rewarding_max_batch_index + ensure!(!batches.is_empty(), Error::::BatchesMissed); + + ensure!((*max_batch_index + 1) as usize == batches.len(), Error::::BatchesMissed); + + for index in 0..*max_batch_index + 1 { + ensure!(batches.contains(&index), Error::::BatchesMissed); + } + + Ok(()) + } + } + + impl PayoutProcessor for Pallet { + fn begin_billing_report( cluster_id: ClusterId, era: DdcEra, start_era: i64, end_era: i64, ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - ensure!( ActiveBillingReports::::try_get(cluster_id, era).is_err(), Error::::NotExpectedState @@ -370,20 +613,11 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // PayoutProcessor trait - #[pallet::call_index(2)] - #[pallet::weight(T::WeightInfo::begin_charging_customers())] - pub fn begin_charging_customers( - origin: OriginFor, + fn begin_charging_customers( cluster_id: ClusterId, era: DdcEra, max_batch_index: BatchIndex, ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); // - // todo! need to refactor this - ensure!(max_batch_index < MaxBatchesCount::get(), Error::::BatchIndexOverflow); let mut billing_report = ActiveBillingReports::::try_get(cluster_id, era) @@ -400,21 +634,13 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // + pass values by reference PayoutProcessor trait - #[pallet::call_index(3)] - #[pallet::weight(T::WeightInfo::send_charging_customers_batch(payers.len().saturated_into()))] - pub fn send_charging_customers_batch( - origin: OriginFor, + fn send_charging_customers_batch( cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, + payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: MMRProof, ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - ensure!( !payers.is_empty() && payers.len() <= MaxBatchSize::get() as usize, Error::::BatchSizeIsOutOfBounds @@ -451,7 +677,8 @@ pub mod pallet { ); let mut updated_billing_report = billing_report; - for (_node_key, bucket_id, customer_usage) in payers { + for (_node_key, bucket_ref, customer_usage) in payers { + let bucket_id = *bucket_ref; let customer_id = T::BucketManager::get_bucket_owner_id(bucket_id)?; let mut customer_charge = get_customer_charge::( @@ -582,18 +809,7 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // PayoutProcessor trait - #[pallet::call_index(4)] - #[pallet::weight(T::WeightInfo::end_charging_customers())] - pub fn end_charging_customers( - origin: OriginFor, - cluster_id: ClusterId, - era: DdcEra, - ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - + fn end_charging_customers(cluster_id: ClusterId, era: DdcEra) -> DispatchResult { let mut billing_report = ActiveBillingReports::::try_get(cluster_id, era) .map_err(|_| Error::::BillingReportDoesNotExist)?; @@ -686,20 +902,12 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // PayoutProcessor trait - #[pallet::call_index(5)] - #[pallet::weight(T::WeightInfo::begin_rewarding_providers())] - pub fn begin_rewarding_providers( - origin: OriginFor, + fn begin_rewarding_providers( cluster_id: ClusterId, era: DdcEra, max_batch_index: BatchIndex, total_node_usage: NodeUsage, ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - ensure!(max_batch_index < MaxBatchesCount::get(), Error::::BatchIndexOverflow); let mut billing_report = ActiveBillingReports::::try_get(cluster_id, era) @@ -720,21 +928,13 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // + pass values by reference PayoutProcessor trait - #[pallet::call_index(6)] - #[pallet::weight(T::WeightInfo::send_rewarding_providers_batch(payees.len().saturated_into()))] - pub fn send_rewarding_providers_batch( - origin: OriginFor, + fn send_rewarding_providers_batch( cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payees: Vec<(NodePubKey, NodeUsage)>, + payees: &[(NodePubKey, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - ensure!( !payees.is_empty() && payees.len() <= MaxBatchSize::get() as usize, Error::::BatchSizeIsOutOfBounds @@ -862,18 +1062,7 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // PayoutProcessor trait - #[pallet::call_index(7)] - #[pallet::weight(T::WeightInfo::end_rewarding_providers())] - pub fn end_rewarding_providers( - origin: OriginFor, - cluster_id: ClusterId, - era: DdcEra, - ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - + fn end_rewarding_providers(cluster_id: ClusterId, era: DdcEra) -> DispatchResult { let mut billing_report = ActiveBillingReports::::try_get(cluster_id, era) .map_err(|_| Error::::BillingReportDoesNotExist)?; @@ -915,18 +1104,7 @@ pub mod pallet { Ok(()) } - // todo! remove extrensics from payout pallet and factor the extrensics implementation into - // PayoutProcessor trait - #[pallet::call_index(8)] - #[pallet::weight(T::WeightInfo::end_billing_report())] - pub fn end_billing_report( - origin: OriginFor, - cluster_id: ClusterId, - era: DdcEra, - ) -> DispatchResult { - let caller = ensure_signed(origin)?; - ensure!(T::ValidatorVisitor::is_ocw_validator(caller), Error::::Unauthorised); - + fn end_billing_report(cluster_id: ClusterId, era: DdcEra) -> DispatchResult { let mut billing_report = ActiveBillingReports::::try_get(cluster_id, era) .map_err(|_| Error::::BillingReportDoesNotExist)?; @@ -944,365 +1122,19 @@ pub mod pallet { Ok(()) } - } - - fn charge_treasury_fees( - treasury_fee: u128, - vault: &T::AccountId, - treasury_vault: &T::AccountId, - ) -> DispatchResult { - let amount_to_deduct = treasury_fee.saturated_into::>(); - ::Currency::transfer( - vault, - treasury_vault, - amount_to_deduct, - ExistenceRequirement::AllowDeath, - ) - } - - fn charge_cluster_reserve_fees( - cluster_reserve_fee: u128, - vault: &T::AccountId, - reserve_vault: &T::AccountId, - ) -> DispatchResult { - let amount_to_deduct = cluster_reserve_fee.saturated_into::>(); - ::Currency::transfer( - vault, - reserve_vault, - amount_to_deduct, - ExistenceRequirement::AllowDeath, - ) - } - - fn get_current_exposure_ratios( - ) -> Result, DispatchError> { - let mut total_score = 0; - let mut individual_scores: Vec<(T::AccountId, u64)> = Vec::new(); - for staker_id in T::NominatorsAndValidatorsList::iter() { - let s = T::NominatorsAndValidatorsList::get_score(&staker_id) - .map_err(|_| Error::::ScoreRetrievalError)?; - let score = T::VoteScoreToU64::convert(s); - total_score += score; - - individual_scores.push((staker_id, score)); - } - - let mut result = Vec::new(); - for (staker_id, score) in individual_scores { - let ratio = Perquintill::from_rational(score, total_score); - result.push((staker_id, ratio)); - } - - Ok(result) - } - - fn charge_validator_fees( - validators_fee: u128, - vault: &T::AccountId, - cluster_id: ClusterId, - era: DdcEra, - ) -> DispatchResult { - let stakers = get_current_exposure_ratios::()?; - - for (staker_id, ratio) in stakers.iter() { - let amount_to_deduct = *ratio * validators_fee; - - ::Currency::transfer( - vault, - staker_id, - amount_to_deduct.saturated_into::>(), - ExistenceRequirement::AllowDeath, - )?; - - pallet::Pallet::deposit_event(Event::::ValidatorRewarded { - cluster_id, - era, - validator_id: staker_id.clone(), - amount: amount_to_deduct, - }); - } - - Ok(()) - } - - fn get_node_reward( - node_usage: &NodeUsage, - total_nodes_usage: &NodeUsage, - total_customer_charge: &CustomerCharge, - ) -> Option { - let mut node_reward = NodeReward::default(); - - let mut ratio = Perquintill::from_rational( - node_usage.transferred_bytes as u128, - total_nodes_usage.transferred_bytes as u128, - ); - - // ratio multiplied by X will be > 0, < X no overflow - node_reward.transfer = ratio * total_customer_charge.transfer; - - ratio = Perquintill::from_rational( - node_usage.stored_bytes as u128, - total_nodes_usage.stored_bytes as u128, - ); - node_reward.storage = ratio * total_customer_charge.storage; - - ratio = - Perquintill::from_rational(node_usage.number_of_puts, total_nodes_usage.number_of_puts); - node_reward.puts = ratio * total_customer_charge.puts; - - ratio = - Perquintill::from_rational(node_usage.number_of_gets, total_nodes_usage.number_of_gets); - node_reward.gets = ratio * total_customer_charge.gets; - - Some(node_reward) - } - - fn get_customer_charge( - cluster_id: &ClusterId, - usage: &CustomerUsage, - bucket_id: BucketId, - customer_id: &T::AccountId, - start_era: i64, - end_era: i64, - ) -> Result { - let mut total = CustomerCharge::default(); - - let pricing = T::ClusterProtocol::get_pricing_params(cluster_id) - .map_err(|_| Error::::NotExpectedClusterState)?; - - total.transfer = (|| -> Option { - (usage.transferred_bytes as u128) - .checked_mul(pricing.unit_per_mb_streamed)? - .checked_div(byte_unit::MEBIBYTE) - })() - .ok_or(Error::::ArithmeticOverflow)?; - - // Calculate the duration of the period in seconds - let duration_seconds = end_era - start_era; - let seconds_in_month = 30.44 * 24.0 * 3600.0; - let fraction_of_month = - Perquintill::from_rational(duration_seconds as u64, seconds_in_month as u64); - - let mut total_stored_bytes: i64 = - T::BucketManager::get_total_bucket_usage(cluster_id, bucket_id, customer_id)? - .map_or(0, |customer_usage| customer_usage.stored_bytes); - - total_stored_bytes = total_stored_bytes - .checked_add(usage.stored_bytes) - .ok_or(Error::::ArithmeticOverflow)? - .max(0); - - total.storage = fraction_of_month * - (|| -> Option { - (total_stored_bytes as u128) - .checked_mul(pricing.unit_per_mb_stored)? - .checked_div(byte_unit::MEBIBYTE) - })() - .ok_or(Error::::ArithmeticOverflow)?; - - total.gets = (usage.number_of_gets as u128) - .checked_mul(pricing.unit_per_get_request) - .ok_or(Error::::ArithmeticOverflow)?; - - total.puts = (usage.number_of_puts as u128) - .checked_mul(pricing.unit_per_put_request) - .ok_or(Error::::ArithmeticOverflow)?; - - Ok(total) - } - - #[pallet::genesis_config] - pub struct GenesisConfig { - pub feeder_account: Option, - pub authorised_caller: Option, - pub debtor_customers: Vec<(ClusterId, T::AccountId, u128)>, - } - - impl Default for GenesisConfig { - fn default() -> Self { - GenesisConfig { - feeder_account: None, - authorised_caller: Default::default(), - debtor_customers: Default::default(), - } - } - } - - #[pallet::genesis_build] - impl BuildGenesisConfig for GenesisConfig { - fn build(&self) { - let account_id = >::account_id(); - let min = ::Currency::minimum_balance(); - let balance = ::Currency::free_balance(&account_id); - if balance < min { - if let Some(vault) = &self.feeder_account { - let _ = ::Currency::transfer( - vault, - &account_id, - min - balance, - ExistenceRequirement::AllowDeath, - ); - } else { - let _ = ::Currency::make_free_balance_be(&account_id, min); - } - } - - for (cluster_id, customer_id, debt) in &self.debtor_customers { - DebtorCustomers::::insert(cluster_id, customer_id, debt); - } - } - } - - impl PayoutVisitor for Pallet { - fn begin_billing_report( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - start_era: i64, - end_era: i64, - ) -> DispatchResult { - log::info!( - "🏭begin_billing_report called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::begin_billing_report(origin, cluster_id, era_id, start_era, end_era) - } - - fn begin_charging_customers( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - max_batch_index: BatchIndex, - ) -> DispatchResult { - log::info!( - "🏭begin_charging_customers called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::begin_charging_customers(origin, cluster_id, era_id, max_batch_index) - } - - fn send_charging_customers_batch( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - batch_index: BatchIndex, - payers: &[(NodePubKey, BucketId, CustomerUsage)], - batch_proof: MMRProof, - ) -> DispatchResult { - log::info!( - "🏭send_charging_customers_batch called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::send_charging_customers_batch( - origin, - cluster_id, - era_id, - batch_index, - (*payers).to_vec(), - batch_proof, - ) - } - - fn end_charging_customers( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult { - log::info!( - "🏭end_charging_customers called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::end_charging_customers(origin, cluster_id, era_id) - } - - fn begin_rewarding_providers( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - max_batch_index: BatchIndex, - total_node_usage: NodeUsage, - ) -> DispatchResult { - log::info!( - "🏭begin_rewarding_providers called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::begin_rewarding_providers( - origin, - cluster_id, - era_id, - max_batch_index, - total_node_usage, - ) - } - - fn send_rewarding_providers_batch( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - batch_index: BatchIndex, - payees: &[(NodePubKey, NodeUsage)], - batch_proof: MMRProof, - ) -> DispatchResult { - log::info!( - "🏭send_rewarding_providers_batch called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::send_rewarding_providers_batch( - origin, - cluster_id, - era_id, - batch_index, - (*payees).to_vec(), - batch_proof, - ) - } - - fn end_rewarding_providers( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult { - log::info!( - "🏭end_rewarding_providers called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::end_rewarding_providers(origin, cluster_id, era_id) - } - - fn end_billing_report( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult { - log::info!( - "🏭end_billing_report called by: {:?}", - Self::get_account_id_string(origin.clone()) - ); - let origin = frame_system::RawOrigin::Signed(origin).into(); - Self::end_billing_report(origin, cluster_id, era_id) - } fn get_billing_report_status(cluster_id: &ClusterId, era: DdcEra) -> PayoutState { let billing_report = ActiveBillingReports::::get(cluster_id, era); - match billing_report { Some(report) => report.state, - None => PayoutState::NotInitialized, // Return NotInitialized if entry doesn't exist + None => PayoutState::NotInitialized, } } fn all_customer_batches_processed(cluster_id: &ClusterId, era_id: DdcEra) -> bool { let billing_report = match ActiveBillingReports::::try_get(cluster_id, era_id) { Ok(report) => report, - Err(_) => return false, /* Return false if there's any error (e.g., - * BillingReportDoesNotExist) */ + Err(_) => return false, }; Self::validate_batches( @@ -1315,8 +1147,7 @@ pub mod pallet { fn all_provider_batches_processed(cluster_id: &ClusterId, era_id: DdcEra) -> bool { let billing_report = match ActiveBillingReports::::try_get(cluster_id, era_id) { Ok(report) => report, - Err(_) => return false, /* Return false if there's any error (e.g., - * BillingReportDoesNotExist) */ + Err(_) => return false, }; Self::validate_batches( @@ -1358,44 +1189,4 @@ pub mod pallet { Ok(None) } } - - impl Pallet { - pub fn account_id() -> T::AccountId { - T::PalletId::get().into_account_truncating() - } - - pub fn get_account_id_string(caller: T::AccountId) -> String { - let account_id: T::AccountIdConverter = caller.into(); - let account_id_32: AccountId32 = account_id.into(); - let account_ref: &[u8; 32] = account_id_32.as_ref(); - hex::encode(account_ref) - } - pub fn sub_account_id(cluster_id: ClusterId, era: DdcEra) -> T::AccountId { - let mut bytes = Vec::new(); - bytes.extend_from_slice(&cluster_id[..]); - bytes.extend_from_slice(&era.encode()); - let hash = blake2_128(&bytes); - - // "modl" + "payouts_" + hash is 28 bytes, the T::AccountId is 32 bytes, so we should be - // safe from the truncation and possible collisions caused by it. The rest 4 bytes will - // be fulfilled with trailing zeros. - T::PalletId::get().into_sub_account_truncating(hash) - } - - pub(crate) fn validate_batches( - batches: &BoundedBTreeSet, - max_batch_index: &BatchIndex, - ) -> DispatchResult { - // Check if the Vec contains all integers between 1 and rewarding_max_batch_index - ensure!(!batches.is_empty(), Error::::BatchesMissed); - - ensure!((*max_batch_index + 1) as usize == batches.len(), Error::::BatchesMissed); - - for index in 0..*max_batch_index + 1 { - ensure!(batches.contains(&index), Error::::BatchesMissed); - } - - Ok(()) - } - } } diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 4e9a3b7f9..96e1a14a1 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -2,47 +2,12 @@ use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; use ddc_primitives::ClusterId; -use frame_support::{assert_noop, assert_ok, error::BadOrigin, traits::Randomness}; +use frame_support::{assert_noop, assert_ok, traits::Randomness}; use sp_core::H256; use sp_runtime::Perquintill; use super::{mock::*, *}; -#[test] -fn begin_billing_report_fails_for_unauthorised() { - ExtBuilder.build_and_execute(|| { - let cluster_id = ClusterId::from([1; 20]); - let era = 100; - let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st - let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight - let start_era: i64 = - DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); - let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - - assert_noop!( - DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(CUSTOMER3_KEY_32), - cluster_id, - era, - start_era, - end_era, - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - start_era, - end_era, - ), - Error::::Unauthorised - ); - }) -} - #[test] fn begin_billing_report_works() { ExtBuilder.build_and_execute(|| { @@ -56,12 +21,8 @@ fn begin_billing_report_works() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); System::assert_last_event(Event::BillingReportInitialized { cluster_id, era }.into()); @@ -81,28 +42,7 @@ fn begin_charging_customers_fails_uninitialised() { let max_batch_index = 2; assert_noop!( - DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(AccountId::from([124; 32])), - cluster_id, - era, - max_batch_index, - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::begin_charging_customers( - RuntimeOrigin::root(), - cluster_id, - era, - max_batch_index, - ), - BadOrigin - ); - - assert_noop!( - DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + >::begin_charging_customers( cluster_id, era, max_batch_index, @@ -126,16 +66,11 @@ fn begin_charging_customers_works() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -174,97 +109,63 @@ fn send_charging_customers_batch_fails_uninitialised() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_noop!( - DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - batch_index, - payers1.clone(), - MMRProof::default(), - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::root(), - cluster_id, - era, - batch_index, - payers1.clone(), - MMRProof::default(), - ), - BadOrigin - ); - - assert_noop!( - DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_charging_customers_batch( cluster_id, era, batch_index, - payers1.clone(), + &payers1.clone(), MMRProof::default(), ), Error::::BillingReportDoesNotExist ); - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); assert_noop!( - DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_charging_customers_batch( cluster_id, era, batch_index, - payers1.clone(), + &payers1.clone(), MMRProof::default(), ), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); let payers1 = vec![(node_key, bucket_id4, CustomerUsage::default())]; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1.clone(), + &payers1.clone(), MMRProof::default(), )); assert_noop!( - DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), ), Error::::BatchIndexAlreadyProcessed ); assert_noop!( - DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + >::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), ), Error::::BatchIndexAlreadyProcessed @@ -418,16 +319,11 @@ fn send_charging_customers_batch_works() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -435,12 +331,11 @@ fn send_charging_customers_batch_works() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -505,12 +400,11 @@ fn send_charging_customers_batch_works() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -554,12 +448,11 @@ fn send_charging_customers_batch_works() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -656,16 +549,11 @@ fn end_charging_customers_works_small_usage_1_hour() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 1.0 * 3600.0) as i64; // 1 hour - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -673,12 +561,11 @@ fn end_charging_customers_works_small_usage_1_hour() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -738,11 +625,7 @@ fn end_charging_customers_works_small_usage_1_hour() { balance = Balances::free_balance(AccountId::from(VALIDATOR3_ACCOUNT_ID)); assert_eq!(balance, 0); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); System::assert_has_event(Event::ChargingFinished { cluster_id, era }.into()); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); @@ -877,16 +760,11 @@ fn send_charging_customers_batch_works_for_day() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -894,12 +772,11 @@ fn send_charging_customers_batch_works_for_day() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -964,12 +841,11 @@ fn send_charging_customers_batch_works_for_day() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -1013,12 +889,11 @@ fn send_charging_customers_batch_works_for_day() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -1137,16 +1012,11 @@ fn send_charging_customers_batch_works_for_day_free_storage() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -1154,12 +1024,11 @@ fn send_charging_customers_batch_works_for_day_free_storage() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -1224,12 +1093,11 @@ fn send_charging_customers_batch_works_for_day_free_storage() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -1273,12 +1141,11 @@ fn send_charging_customers_batch_works_for_day_free_storage() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -1396,16 +1263,11 @@ fn send_charging_customers_batch_works_for_day_free_stream() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -1413,12 +1275,11 @@ fn send_charging_customers_batch_works_for_day_free_stream() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -1483,12 +1344,11 @@ fn send_charging_customers_batch_works_for_day_free_stream() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -1532,12 +1392,11 @@ fn send_charging_customers_batch_works_for_day_free_stream() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -1656,16 +1515,11 @@ fn send_charging_customers_batch_works_for_day_free_get() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -1673,12 +1527,11 @@ fn send_charging_customers_batch_works_for_day_free_get() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -1743,12 +1596,11 @@ fn send_charging_customers_batch_works_for_day_free_get() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -1792,12 +1644,11 @@ fn send_charging_customers_batch_works_for_day_free_get() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -1916,16 +1767,11 @@ fn send_charging_customers_batch_works_for_day_free_put() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -1933,12 +1779,11 @@ fn send_charging_customers_batch_works_for_day_free_put() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -2003,12 +1848,11 @@ fn send_charging_customers_batch_works_for_day_free_put() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -2052,12 +1896,11 @@ fn send_charging_customers_batch_works_for_day_free_put() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -2175,16 +2018,11 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -2192,12 +2030,11 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { assert_eq!(System::events().len(), 2); // batch 1 - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); @@ -2262,12 +2099,11 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { // batch 2 let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers2, + &payers2, MMRProof::default(), )); @@ -2311,12 +2147,11 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { // batch 3 batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers3, + &payers3, MMRProof::default(), )); @@ -2401,16 +2236,11 @@ fn send_charging_customers_batch_works_zero_fees() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, @@ -2421,12 +2251,11 @@ fn send_charging_customers_batch_works_zero_fees() { let mut report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); let before_total_customer_charge = report.total_customer_charge; let balance_before = Balances::free_balance(DdcPayouts::account_id()); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers5, + &payers5, MMRProof::default(), )); @@ -2472,76 +2301,40 @@ fn end_charging_customers_fails_uninitialised() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_noop!( - DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::end_charging_customers(RuntimeOrigin::root(), cluster_id, era,), - BadOrigin - ); - - assert_noop!( - DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_charging_customers(cluster_id, era,), Error::::BillingReportDoesNotExist ); - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); assert_noop!( - DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_charging_customers(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); assert_noop!( - DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_charging_customers(cluster_id, era,), Error::::BatchesMissed ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - ), + >::end_charging_customers(cluster_id, era,), Error::::BatchesMissed ); }) @@ -2573,27 +2366,21 @@ fn end_charging_customers_works() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); @@ -2615,11 +2402,7 @@ fn end_charging_customers_works() { assert_eq!(balance - Balances::minimum_balance(), charge); assert_eq!(System::events().len(), 3 + 1); // 1 for Currency::transfer - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); System::assert_has_event(Event::ChargingFinished { cluster_id, era }.into()); @@ -2754,27 +2537,21 @@ fn end_charging_customers_works_zero_fees() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); @@ -2796,11 +2573,7 @@ fn end_charging_customers_works_zero_fees() { assert_eq!(balance - Balances::minimum_balance(), charge); assert_eq!(System::events().len(), 3 + 1); // 1 for Currency::transfer - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); System::assert_has_event(Event::ChargingFinished { cluster_id, era }.into()); assert_eq!(System::events().len(), 4 + 1); @@ -2873,30 +2646,7 @@ fn begin_rewarding_providers_fails_uninitialised() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - max_batch_index, - node_usage.clone(), - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::root(), - cluster_id, - era, - max_batch_index, - node_usage.clone(), - ), - BadOrigin - ); - - assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -2905,17 +2655,12 @@ fn begin_rewarding_providers_fails_uninitialised() { Error::::BillingReportDoesNotExist ); - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -2924,16 +2669,14 @@ fn begin_rewarding_providers_fails_uninitialised() { Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -2942,18 +2685,16 @@ fn begin_rewarding_providers_fails_uninitialised() { Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers.clone(), + &payers, MMRProof::default(), )); assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -2962,18 +2703,16 @@ fn begin_rewarding_providers_fails_uninitialised() { Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index + 1, - payers, + &payers, MMRProof::default(), )); assert_noop!( - DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + >::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -3004,41 +2743,30 @@ fn begin_rewarding_providers_works() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); let mut report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_eq!(report.state, PayoutState::Initialized); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -3074,135 +2802,94 @@ fn send_rewarding_providers_batch_fails_uninitialised() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - batch_index, - payees.clone(), - MMRProof::default(), - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::root(), - cluster_id, - era, - batch_index, - payees.clone(), - MMRProof::default(), - ), - BadOrigin - ); - - assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees.clone(), + &payees, MMRProof::default(), ), Error::::BillingReportDoesNotExist ); - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees.clone(), + &payees, MMRProof::default(), ), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees.clone(), + &payees, MMRProof::default(), ), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees.clone(), + &payees, MMRProof::default(), ), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index + 1, - payers2, + &payers2, MMRProof::default(), )); assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + >::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees.clone(), + &payees, MMRProof::default(), ), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); assert_noop!( - DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + >::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees, + &payees, MMRProof::default(), ), Error::::NotExpectedState @@ -3283,36 +2970,26 @@ fn send_rewarding_providers_batch_works() { DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); let report_before = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); let total_left_from_one = (get_fees(&cluster_id).treasury_share + @@ -3337,20 +3014,18 @@ fn send_rewarding_providers_batch_works() { total_left_from_one * report_before.total_customer_charge.gets ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, max_node_batch_index, total_nodes_usage.clone(), )); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_node_index, - payees1, + &payees1, MMRProof::default(), )); @@ -3435,12 +3110,11 @@ fn send_rewarding_providers_batch_works() { ); // batch 2 - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_node_index + 1, - payees2, + &payees2, MMRProof::default(), )); @@ -3496,11 +3170,9 @@ fn send_rewarding_providers_batch_works() { assert!(expected_amount_to_reward - report_reward.total_distributed_reward <= 20000); - assert_ok!(DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - )); + assert_ok!( + >::end_rewarding_providers(cluster_id, era,) + ); }) } @@ -3655,27 +3327,21 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, (payers.len() - 1) as u16, )); for batch in payers.iter() { - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_user_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -3705,11 +3371,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { assert_eq!(report_before.vault, DdcPayouts::account_id()); assert_eq!(balance1 - Balances::minimum_balance(), total_charge); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); let total_left_from_one = (get_fees(&cluster_id).treasury_share + @@ -3741,8 +3403,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { total_left_from_one * report_before.total_customer_charge.gets ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, (payees.len() - 1) as u16, @@ -3751,12 +3412,11 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { for batch in payees.iter() { let before_batch = Balances::free_balance(DdcPayouts::account_id()); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_node_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -3974,27 +3634,21 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, (payers.len() - 1) as u16, )); for batch in payers.iter() { - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_user_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -4024,11 +3678,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { assert_eq!(report_before.vault, DdcPayouts::account_id()); assert_eq!(balance1 - Balances::minimum_balance(), total_charge); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); let total_left_from_one = (get_fees(&cluster_id).treasury_share + @@ -4060,8 +3710,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { total_left_from_one * report_before.total_customer_charge.gets ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, (payees.len() - 1) as u16, @@ -4070,12 +3719,11 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { for batch in payees.iter() { let before_batch = Balances::free_balance(DdcPayouts::account_id()); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_node_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -4288,27 +3936,21 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, (payers.len() - 1) as u16, )); for batch in payers.iter() { - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_user_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -4338,11 +3980,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { assert_eq!(report_before.vault, DdcPayouts::account_id()); assert_eq!(balance1 - Balances::minimum_balance(), total_charge); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); let total_left_from_one = (get_fees(&cluster_id).treasury_share + @@ -4374,8 +4012,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { total_left_from_one * report_before.total_customer_charge.gets ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, (payees.len() - 1) as u16, @@ -4384,12 +4021,11 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { for batch in payees.iter() { let before_batch = Balances::free_balance(DdcPayouts::account_id()); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_node_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -4566,27 +4202,21 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, (payers.len() - 1) as u16, )); for batch in payers.iter() { - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_user_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -4616,11 +4246,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { assert_eq!(report_before.vault, DdcPayouts::account_id()); assert_eq!(balance1 - Balances::minimum_balance(), total_charge); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); let total_left_from_one = (get_fees(&cluster_id).treasury_share + @@ -4652,8 +4278,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { total_left_from_one * report_before.total_customer_charge.gets ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, (payees.len() - 1) as u16, @@ -4662,12 +4287,11 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { for batch in payees.iter() { let before_batch = Balances::free_balance(DdcPayouts::account_id()); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_node_index, - batch.to_vec(), + &batch.to_vec(), MMRProof::default(), )); @@ -4737,114 +4361,64 @@ fn end_rewarding_providers_fails_uninitialised() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::end_rewarding_providers(RuntimeOrigin::root(), cluster_id, era,), - BadOrigin - ); - - assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::BillingReportDoesNotExist ); - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index + 1, - payers2, + &payers2, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -4852,29 +4426,20 @@ fn end_rewarding_providers_fails_uninitialised() { )); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::BatchesMissed ); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees, + &payees, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - ), + >::end_rewarding_providers(cluster_id, era,), Error::::BatchesMissed ); }) @@ -4914,61 +4479,47 @@ fn end_rewarding_providers_works() { let payers = vec![(node_key.clone(), bucket_id1, usage1)]; let payees = vec![(node_key, node_usage1)]; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); let mut report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_eq!(report.state, PayoutState::Initialized); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, max_batch_index, total_node_usage, )); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees, + &payees, MMRProof::default(), )); - assert_ok!(DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!( + >::end_rewarding_providers(cluster_id, era,) + ); System::assert_last_event(Event::RewardingFinished { cluster_id, era }.into()); @@ -4999,114 +4550,64 @@ fn end_billing_report_fails_uninitialised() { let total_node_usage = NodeUsage::default(); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(AccountId::from([0; 32])), - cluster_id, - era, - ), - Error::::Unauthorised - ); - - assert_noop!( - DdcPayouts::end_billing_report(RuntimeOrigin::root(), cluster_id, era,), - BadOrigin - ); - - assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::BillingReportDoesNotExist ); - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers1, + &payers1, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index + 1, - payers2, + &payers2, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, max_batch_index, @@ -5114,47 +4615,33 @@ fn end_billing_report_fails_uninitialised() { )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees.clone(), + &payees, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_index + 1, - payees, + &payees, MMRProof::default(), )); assert_noop!( - DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - ), + >::end_billing_report(cluster_id, era,), Error::::NotExpectedState ); }) @@ -5180,67 +4667,49 @@ fn end_billing_report_works() { let payers = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; let payees = vec![(node_key.clone(), NodeUsage::default())]; - assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - start_era, - end_era, + assert_ok!(>::begin_billing_report( + cluster_id, era, start_era, end_era, )); let report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_eq!(report.state, PayoutState::Initialized); - assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_charging_customers( cluster_id, era, max_batch_index, )); - assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_charging_customers_batch( cluster_id, era, batch_index, - payers, + &payers, MMRProof::default(), )); - assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!(>::end_charging_customers(cluster_id, era,)); - assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::begin_rewarding_providers( cluster_id, era, max_batch_index, total_node_usage, )); - assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), + assert_ok!(>::send_rewarding_providers_batch( cluster_id, era, batch_index, - payees, + &payees, MMRProof::default(), )); - assert_ok!(DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), - cluster_id, - era, - )); + assert_ok!( + >::end_rewarding_providers(cluster_id, era,) + ); - assert_ok!(DdcPayouts::end_billing_report( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), - cluster_id, - era, - )); + assert_ok!(>::end_billing_report(cluster_id, era,)); System::assert_last_event(Event::BillingReportFinalized { cluster_id, era }.into()); diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 455a36833..a9bc10c71 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -14,7 +14,7 @@ use core::str; use base64ct::{Base64, Encoding}; use ddc_primitives::{ traits::{ - ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutVisitor, + ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutProcessor, ValidatorVisitor, }, ActivityHash, BatchIndex, ClusterId, ClusterStatus, CustomerUsage, DdcEra, EraValidation, @@ -115,7 +115,7 @@ pub mod pallet { /// DDC clusters nodes manager. type ClusterValidator: ClusterValidator; type ClusterManager: ClusterManager; - type PayoutVisitor: PayoutVisitor; + type PayoutProcessor: PayoutProcessor; /// DDC nodes read-only registry. type NodeManager: NodeManager; /// The output of the `ActivityHasher` function. @@ -1885,7 +1885,7 @@ pub mod pallet { if let Some((era_id, start, end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::Initialized { if let Some((_, _, customers_activity_batch_roots, _, _, _)) = @@ -1951,7 +1951,7 @@ pub mod pallet { if let Some((era_id, start, end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::ChargingCustomers { if let Some(( @@ -2016,12 +2016,14 @@ pub mod pallet { customers_total_activity: Vec, customers_activity_batch_roots: Vec, ) -> Result, Vec> { - let batch_index = T::PayoutVisitor::get_next_customer_batch_for_payment( - cluster_id, era_id, - ) - .map_err(|_| { - vec![OCWError::BillingReportDoesNotExist { cluster_id: *cluster_id, era_id }] - })?; + let batch_index = + T::PayoutProcessor::get_next_customer_batch_for_payment(cluster_id, era_id) + .map_err(|_| { + vec![OCWError::BillingReportDoesNotExist { + cluster_id: *cluster_id, + era_id, + }] + })?; if let Some(index) = batch_index { let i: usize = index.into(); @@ -2091,9 +2093,9 @@ pub mod pallet { if let Some((era_id, _start, _end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::ChargingCustomers && - T::PayoutVisitor::all_customer_batches_processed(cluster_id, era_id) + T::PayoutProcessor::all_customer_batches_processed(cluster_id, era_id) { return Ok(Some(era_id)); } @@ -2115,7 +2117,7 @@ pub mod pallet { .filter_map(|usage| usage.as_ref().map(|u| u.stored_bytes)) .sum(); - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::CustomersChargedWithFees { if let Some((_, _, _, nodes_total_activity, _, nodes_activity_batch_roots)) = @@ -2208,7 +2210,7 @@ pub mod pallet { if let Some((era_id, start, end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::RewardingProviders { if let Some((_, _, _, nodes_total_activity, _, nodes_activity_batch_roots)) = @@ -2267,12 +2269,14 @@ pub mod pallet { nodes_total_activity: Vec, nodes_activity_batch_roots: Vec, ) -> Result, Vec> { - let batch_index = T::PayoutVisitor::get_next_provider_batch_for_payment( - cluster_id, era_id, - ) - .map_err(|_| { - vec![OCWError::BillingReportDoesNotExist { cluster_id: *cluster_id, era_id }] - })?; + let batch_index = + T::PayoutProcessor::get_next_provider_batch_for_payment(cluster_id, era_id) + .map_err(|_| { + vec![OCWError::BillingReportDoesNotExist { + cluster_id: *cluster_id, + era_id, + }] + })?; if let Some(index) = batch_index { let i: usize = index.into(); @@ -2342,9 +2346,9 @@ pub mod pallet { if let Some((era_id, _start, _end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::RewardingProviders && - T::PayoutVisitor::all_provider_batches_processed(cluster_id, era_id) + T::PayoutProcessor::all_provider_batches_processed(cluster_id, era_id) { return Ok(Some(era_id)); } @@ -2358,7 +2362,7 @@ pub mod pallet { if let Some((era_id, _start, _end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { - if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == + if T::PayoutProcessor::get_billing_report_status(cluster_id, era_id) == PayoutState::ProvidersRewarded { return Ok(Some(era_id)); @@ -3794,7 +3798,7 @@ pub mod pallet { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::begin_billing_report(sender, cluster_id, era_id, start_era, end_era)?; + T::PayoutProcessor::begin_billing_report(cluster_id, era_id, start_era, end_era)?; EraValidations::::try_mutate( cluster_id, @@ -3819,7 +3823,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::begin_charging_customers(sender, cluster_id, era_id, max_batch_index) + T::PayoutProcessor::begin_charging_customers(cluster_id, era_id, max_batch_index) } #[pallet::call_index(5)] @@ -3835,8 +3839,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::send_charging_customers_batch( - sender, + T::PayoutProcessor::send_charging_customers_batch( cluster_id, era_id, batch_index, @@ -3854,7 +3857,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::end_charging_customers(sender, cluster_id, era_id) + T::PayoutProcessor::end_charging_customers(cluster_id, era_id) } #[pallet::call_index(7)] @@ -3868,8 +3871,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::begin_rewarding_providers( - sender, + T::PayoutProcessor::begin_rewarding_providers( cluster_id, era_id, max_batch_index, @@ -3889,8 +3891,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::send_rewarding_providers_batch( - sender, + T::PayoutProcessor::send_rewarding_providers_batch( cluster_id, era_id, batch_index, @@ -3908,7 +3909,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::end_rewarding_providers(sender, cluster_id, era_id) + T::PayoutProcessor::end_rewarding_providers(cluster_id, era_id) } #[pallet::call_index(10)] @@ -3920,7 +3921,7 @@ pub mod pallet { ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutVisitor::end_billing_report(sender, cluster_id, era_id)?; + T::PayoutProcessor::end_billing_report(cluster_id, era_id)?; let mut era_validation = >::get(cluster_id, era_id).unwrap(); // should exist era_validation.status = EraValidationStatus::PayoutSuccess; diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index f86f45a52..f542ff715 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -223,7 +223,7 @@ impl crate::Config for Test { type ClusterManager = TestClusterManager; type ClusterValidator = TestClusterValidator; type NodeManager = MockNodeManager; - type PayoutVisitor = MockPayoutVisitor; + type PayoutProcessor = MockPayoutProcessor; type AuthorityId = sr25519::AuthorityId; type OffchainIdentifierId = crypto::OffchainIdentifierId; type ActivityHasher = sp_runtime::traits::BlakeTwo256; @@ -355,107 +355,90 @@ impl ClusterValidator for TestClusterValidator { } } -pub struct MockPayoutVisitor; -impl PayoutVisitor for MockPayoutVisitor { - fn get_next_customer_batch_for_payment( - _cluster_id: &ClusterId, - _era_id: DdcEra, - ) -> Result, PayoutError> { - Ok(None) - } - - fn get_next_provider_batch_for_payment( - _cluster_id: &ClusterId, - _era_id: DdcEra, - ) -> Result, PayoutError> { - Ok(None) - } - - fn all_customer_batches_processed(_cluster_id: &ClusterId, _era_id: DdcEra) -> bool { - true - } - - fn all_provider_batches_processed(_cluster_id: &ClusterId, _era_id: DdcEra) -> bool { - true - } - - fn get_billing_report_status(_cluster_id: &ClusterId, _era_id: DdcEra) -> PayoutState { - PayoutState::NotInitialized - } - +pub struct MockPayoutProcessor; +impl PayoutProcessor for MockPayoutProcessor { fn begin_billing_report( - _origin: T::AccountId, _cluster_id: ClusterId, _era_id: DdcEra, _start_era: i64, _end_era: i64, ) -> DispatchResult { - Ok(()) + unimplemented!() } fn begin_charging_customers( - _origin: T::AccountId, _cluster_id: ClusterId, _era_id: DdcEra, _max_batch_index: BatchIndex, ) -> DispatchResult { - Ok(()) + unimplemented!() } fn send_charging_customers_batch( - _origin: T::AccountId, _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, _payers: &[(NodePubKey, BucketId, CustomerUsage)], _batch_proof: MMRProof, ) -> DispatchResult { - Ok(()) + unimplemented!() } - fn end_charging_customers( - _origin: T::AccountId, - _cluster_id: ClusterId, - _era_id: DdcEra, - ) -> DispatchResult { - Ok(()) + fn end_charging_customers(_cluster_id: ClusterId, _era_id: DdcEra) -> DispatchResult { + unimplemented!() } fn begin_rewarding_providers( - _origin: T::AccountId, _cluster_id: ClusterId, _era_id: DdcEra, _max_batch_index: BatchIndex, _total_node_usage: NodeUsage, ) -> DispatchResult { - Ok(()) + unimplemented!() } fn send_rewarding_providers_batch( - _origin: T::AccountId, _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, _payees: &[(NodePubKey, NodeUsage)], _batch_proof: MMRProof, ) -> DispatchResult { - Ok(()) + unimplemented!() } - fn end_rewarding_providers( - _origin: T::AccountId, - _cluster_id: ClusterId, + fn end_rewarding_providers(_cluster_id: ClusterId, _era_id: DdcEra) -> DispatchResult { + unimplemented!() + } + + fn end_billing_report(_cluster_id: ClusterId, _era_id: DdcEra) -> DispatchResult { + unimplemented!() + } + + fn get_next_customer_batch_for_payment( + _cluster_id: &ClusterId, _era_id: DdcEra, - ) -> DispatchResult { - Ok(()) + ) -> Result, PayoutError> { + Ok(None) } - fn end_billing_report( - _origin: T::AccountId, - _cluster_id: ClusterId, + fn get_next_provider_batch_for_payment( + _cluster_id: &ClusterId, _era_id: DdcEra, - ) -> DispatchResult { - Ok(()) + ) -> Result, PayoutError> { + Ok(None) + } + + fn all_customer_batches_processed(_cluster_id: &ClusterId, _era_id: DdcEra) -> bool { + true + } + + fn all_provider_batches_processed(_cluster_id: &ClusterId, _era_id: DdcEra) -> bool { + true + } + + fn get_billing_report_status(_cluster_id: &ClusterId, _era_id: DdcEra) -> PayoutState { + PayoutState::NotInitialized } } diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index 075845962..666ab2396 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -4,75 +4,48 @@ use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, PayoutError, PayoutState, }; -pub trait PayoutProcessor {} - -pub trait PayoutVisitor { - // todo! factor out into PayoutProcessor +pub trait PayoutProcessor { fn begin_billing_report( - origin: T::AccountId, cluster_id: ClusterId, - era_id: DdcEra, + era: DdcEra, start_era: i64, end_era: i64, ) -> DispatchResult; - // todo! factor out into PayoutProcessor fn begin_charging_customers( - origin: T::AccountId, cluster_id: ClusterId, - era_id: DdcEra, + era: DdcEra, max_batch_index: BatchIndex, ) -> DispatchResult; - // todo! factor out into PayoutProcessor fn send_charging_customers_batch( - origin: T::AccountId, cluster_id: ClusterId, - era_id: DdcEra, + era: DdcEra, batch_index: BatchIndex, payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: MMRProof, ) -> DispatchResult; - // todo! factor out into PayoutProcessor - fn end_charging_customers( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult; + fn end_charging_customers(cluster_id: ClusterId, era: DdcEra) -> DispatchResult; - // todo! factor out into PayoutProcessor fn begin_rewarding_providers( - origin: T::AccountId, cluster_id: ClusterId, - era_id: DdcEra, + era: DdcEra, max_batch_index: BatchIndex, total_node_usage: NodeUsage, ) -> DispatchResult; - // todo! factor out into PayoutProcessor fn send_rewarding_providers_batch( - origin: T::AccountId, cluster_id: ClusterId, - era_id: DdcEra, + era: DdcEra, batch_index: BatchIndex, payees: &[(NodePubKey, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult; - // todo! factor out into PayoutProcessor - fn end_rewarding_providers( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult; + fn end_rewarding_providers(cluster_id: ClusterId, era: DdcEra) -> DispatchResult; - // todo! factor out into PayoutProcessor - fn end_billing_report( - origin: T::AccountId, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult; + fn end_billing_report(cluster_id: ClusterId, era: DdcEra) -> DispatchResult; fn get_billing_report_status(cluster_id: &ClusterId, era: DdcEra) -> PayoutState; diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 30f445bec..616dc650f 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1315,7 +1315,7 @@ impl pallet_ddc_verification::Config for Runtime { type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterValidator = pallet_ddc_clusters::Pallet; type NodeManager = pallet_ddc_nodes::Pallet; - type PayoutVisitor = pallet_ddc_payouts::Pallet; + type PayoutProcessor = pallet_ddc_payouts::Pallet; type AuthorityId = ddc_primitives::sr25519::AuthorityId; type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; type ActivityHasher = BlakeTwo256; diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 0416b471c..afee849a9 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1335,7 +1335,7 @@ impl pallet_ddc_verification::Config for Runtime { type ClusterManager = pallet_ddc_clusters::Pallet; type ClusterValidator = pallet_ddc_clusters::Pallet; type NodeManager = pallet_ddc_nodes::Pallet; - type PayoutVisitor = pallet_ddc_payouts::Pallet; + type PayoutProcessor = pallet_ddc_payouts::Pallet; type AuthorityId = ddc_primitives::sr25519::AuthorityId; type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; type ActivityHasher = BlakeTwo256; From 54e582cd3f2c9234e649682eb6f58f6632f206de Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 03:25:43 +0100 Subject: [PATCH 07/15] refactor: ddc-verification benchmarks --- pallets/ddc-payouts/src/benchmarking.rs | 643 +--------------- pallets/ddc-payouts/src/lib.rs | 79 +- pallets/ddc-payouts/src/mock.rs | 12 - pallets/ddc-payouts/src/weights.rs | 228 ------ pallets/ddc-verification/src/benchmarking.rs | 744 ++++++++++++++++++- pallets/ddc-verification/src/lib.rs | 72 +- pallets/ddc-verification/src/weights.rs | 382 +++++++++- primitives/src/lib.rs | 34 + primitives/src/traits/payout.rs | 22 +- primitives/src/traits/validator.rs | 12 - runtime/cere-dev/src/lib.rs | 7 +- runtime/cere/src/lib.rs | 7 +- 12 files changed, 1237 insertions(+), 1005 deletions(-) delete mode 100644 pallets/ddc-payouts/src/weights.rs diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs index de5a1ad6b..387652cee 100644 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ b/pallets/ddc-payouts/src/benchmarking.rs @@ -1,652 +1,17 @@ //! DdcPayouts pallet benchmarking. -use ddc_primitives::{ - traits::ValidatorVisitor, ActivityHash, BucketParams, ClusterId, ClusterParams, - ClusterProtocolParams, EraValidation, EraValidationStatus, MergeActivityHash, NodeParams, - NodePubKey, StorageNodeMode, StorageNodeParams, -}; pub use frame_benchmarking::{account, benchmarks, whitelist_account}; use frame_system::RawOrigin; -use polkadot_ckb_merkle_mountain_range::{ - util::{MemMMR, MemStore}, - MMR, -}; -use scale_info::prelude::{collections::BTreeMap, format}; -use sp_runtime::{AccountId32, Perquintill}; use sp_std::prelude::*; use super::*; use crate::Pallet as DdcPayouts; -const CERE: u128 = 10000000000; -const AVG_SECONDS_MONTH: i64 = 2630016; // 30.44 * 24.0 * 3600.0; - -fn create_dac_account() -> T::AccountId { - let dac_account = create_account::("dac_account", 0, 0); - T::ValidatorVisitor::setup_validators(vec![(dac_account.clone(), dac_account.clone())]); - dac_account -} - -fn create_account(name: &'static str, idx: u32, seed: u32) -> T::AccountId { - account::(name, idx, seed) -} - -fn endow_account(account: &T::AccountId, amount: u128) { - let balance = amount.saturated_into::>(); - let _ = T::Currency::make_free_balance_be(account, balance); -} - -fn endow_customer(customer: &T::AccountId, amount: u128) { - endow_account::(customer, amount); - T::CustomerDepositor::deposit( - customer.clone(), - // we need to keep min existensial deposit - amount - T::Currency::minimum_balance().saturated_into::(), - ) - .expect("Customer deposit failed"); -} - -fn create_cluster( - cluster_id: ClusterId, - cluster_manager_id: T::AccountId, - cluster_reserve_id: T::AccountId, - cluster_params: ClusterParams, - cluster_protocol_params: ClusterProtocolParams, BlockNumberFor>, -) { - T::ClusterCreator::create_cluster( - cluster_id, - cluster_manager_id, - cluster_reserve_id, - cluster_params, - cluster_protocol_params, - ) - .expect("Cluster is not created"); -} - -fn create_default_cluster(cluster_id: ClusterId) { - let cluster_manager = create_account::("cm", 0, 0); - let cluster_reserve = create_account::("cr", 0, 0); - let cluster_params = ClusterParams { - node_provider_auth_contract: Default::default(), - erasure_coding_required: 4, - erasure_coding_total: 6, - replication_total: 3, - }; - let cluster_protocol_params: ClusterProtocolParams, BlockNumberFor> = - ClusterProtocolParams { - treasury_share: Perquintill::from_percent(5), - validators_share: Perquintill::from_percent(10), - cluster_reserve_share: Perquintill::from_percent(15), - unit_per_mb_stored: CERE, - unit_per_mb_streamed: CERE, - unit_per_put_request: CERE, - unit_per_get_request: CERE, - ..Default::default() - }; - - create_cluster::( - cluster_id, - cluster_manager, - cluster_reserve, - cluster_params, - cluster_protocol_params, - ); -} - -struct BillingReportParams { - cluster_id: ClusterId, - era: DdcEra, - state: PayoutState, - total_customer_charge: CustomerCharge, - total_distributed_reward: u128, - total_node_usage: NodeUsage, - charging_max_batch_index: BatchIndex, - charging_processed_batches: BoundedBTreeSet, - rewarding_max_batch_index: BatchIndex, - rewarding_processed_batches: BoundedBTreeSet, -} - -fn create_billing_report(params: BillingReportParams) { - let vault = DdcPayouts::::sub_account_id(params.cluster_id, params.era); - let start_era: i64 = 1_000_000_000; - let end_era: i64 = start_era + AVG_SECONDS_MONTH; - - let billing_report = BillingReport:: { - vault, - start_era, - end_era, - state: params.state, - total_customer_charge: params.total_customer_charge, - total_distributed_reward: params.total_distributed_reward, - total_node_usage: params.total_node_usage, - charging_max_batch_index: params.charging_max_batch_index, - charging_processed_batches: params.charging_processed_batches, - rewarding_max_batch_index: params.rewarding_max_batch_index, - rewarding_processed_batches: params.rewarding_processed_batches, - }; - - ActiveBillingReports::::insert(params.cluster_id, params.era, billing_report); -} - -fn set_validation_era( - cluster_id: ClusterId, - era_id: DdcEra, - era_validation: EraValidation, -) { - T::ValidatorVisitor::setup_validation_era(cluster_id, era_id, era_validation); -} - benchmarks! { - - begin_billing_report { - - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let start_era: i64 = 1_000_000_000; - let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - - create_default_cluster::(cluster_id); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, start_era, end_era) - verify { - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::Initialized); - } - - begin_charging_customers { - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::Initialized; - let total_customer_charge = CustomerCharge::default(); - let total_distributed_reward : u128= 0; - let total_node_usage = NodeUsage::default(); - let charging_max_batch_index = BatchIndex::default(); - let charging_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - let rewarding_max_batch_index = BatchIndex::default(); - let rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge, - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - let max_batch_index: BatchIndex = 10; - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, max_batch_index) - verify { - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::ChargingCustomers); - assert_eq!(billing_report.charging_max_batch_index, max_batch_index); - } - - send_charging_customers_batch { - let b in 1 .. MaxBatchSize::get() as u32; - - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::ChargingCustomers; - let total_customer_charge = CustomerCharge::default(); - let total_distributed_reward : u128 = 0; - let total_node_usage = NodeUsage::default(); - let charging_max_batch_index = 0; - let charging_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - let rewarding_max_batch_index = BatchIndex::default(); - let rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge, - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let batch_index: BatchIndex = 0; - let mut payers_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = vec![]; - for i in 0..b { - let customer = create_account::("customer", i, i); - - if b % 2 == 0 { - // no customer debt path - endow_customer::(&customer, 1_000_000 * CERE); - } else { - // customer debt path - endow_customer::(&customer, 10 * CERE); - } - - let customer_usage = CustomerUsage { - transferred_bytes: 200000000, // 200 mb - stored_bytes: 100000000, // 100 mb - number_of_gets: 10, // 10 gets - number_of_puts: 5, // 5 puts - }; - let bucket_id: BucketId = (i + 1).into(); - let node_key = NodePubKey::StoragePubKey(AccountId32::from([ - 48, 47, 147, 125, 243, 160, 236, 76, 101, 142, 129, 34, 67, 158, 116, 141, 34, 116, 66, - 235, 212, 147, 206, 245, 33, 161, 225, 73, 67, 132, 67, 149, - ])); - - T::BucketManager::create_bucket( - &cluster_id, - bucket_id, - customer, - BucketParams { is_public: true } - ).expect("Bucket to be created"); - - payers_batch.push((node_key, bucket_id, customer_usage)); - } - - let activity_hashes = payers_batch.clone().into_iter().map(|(node_key, bucket_id, usage)| { - let mut data = bucket_id.encode(); - let node_id = format!("0x{}", node_key.get_hex()); - data.extend_from_slice(&node_id.encode()); - data.extend_from_slice(&usage.stored_bytes.encode()); - data.extend_from_slice(&usage.transferred_bytes.encode()); - data.extend_from_slice(&usage.number_of_puts.encode()); - data.extend_from_slice(&usage.number_of_gets.encode()); - sp_io::hashing::blake2_256(&data) - }).collect::>(); - - let store1 = MemStore::default(); - let mut mmr1: MMR> = MemMMR::::new(0, &store1); - for activity_hash in activity_hashes { - let _pos: u64 = mmr1.push(activity_hash).unwrap(); - } - - let batch_root = mmr1.get_root().unwrap(); - - let store2 = MemStore::default(); - let mut mmr2: MMR> = MemMMR::::new(0, &store2); - let pos = mmr2.push(batch_root).unwrap(); - let payers_merkle_root_hash = mmr2.get_root().unwrap(); - - let proof = mmr2 - .gen_proof(vec![pos]) - .unwrap() - .proof_items() - .to_vec(); - - let payees_merkle_root_hash = ActivityHash::default(); - - let mut validators = BTreeMap::new(); - validators.insert( - (payers_merkle_root_hash, payees_merkle_root_hash), - vec![dac_account.clone()], - ); - let start_era: i64 = 1_000_000_000; - let end_era: i64 = start_era + AVG_SECONDS_MONTH; - let era_validation = EraValidation:: { - validators, - start_era, - end_era, - payers_merkle_root_hash, - payees_merkle_root_hash, - status: EraValidationStatus::PayoutInProgress, - }; - - set_validation_era::(cluster_id, era, era_validation); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payers_batch, MMRProof { proof }) - verify { - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::ChargingCustomers); - assert!(billing_report.charging_processed_batches.contains(&batch_index)); - } - - end_charging_customers { - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::ChargingCustomers; - let total_customer_charge = CustomerCharge { - transfer: 200 * CERE, // price for 200 mb - storage: 100 * CERE, // price for 100 mb - gets: 10 * CERE, // price for 10 gets - puts: 5 * CERE, // price for 5 puts - }; - let total_distributed_reward : u128 = 0; - let total_node_usage = NodeUsage::default(); - let charging_max_batch_index = 0; - let mut charging_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - charging_processed_batches.try_insert(0).unwrap(); - let rewarding_max_batch_index = BatchIndex::default(); - let rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge: total_customer_charge.clone(), - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let vault = DdcPayouts::::sub_account_id(cluster_id, era); - let total_customer_charge_amount = total_customer_charge.transfer + total_customer_charge.storage + total_customer_charge.gets + total_customer_charge.puts; - endow_account::(&vault, total_customer_charge_amount); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era) - verify { - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::CustomersChargedWithFees); - assert!(billing_report.charging_processed_batches.contains(&charging_max_batch_index)); - } - - begin_rewarding_providers { - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::CustomersChargedWithFees; - let total_customer_charge = CustomerCharge { - transfer: 200 * CERE, // price for 200 mb - storage: 100 * CERE, // price for 100 mb - gets: 10 * CERE, // price for 10 gets - puts: 5 * CERE, // price for 5 puts - }; - let total_distributed_reward : u128 = 0; - let total_node_usage = NodeUsage::default(); - let charging_max_batch_index = 0; - let mut charging_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - charging_processed_batches.try_insert(0).unwrap(); - let rewarding_max_batch_index = BatchIndex::default(); - let rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge, - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let max_batch_index: BatchIndex = 10; - let total_node_usage = NodeUsage { - transferred_bytes: 200000000, // 200 mb - stored_bytes: 100000000, // 100 mb - number_of_gets: 10, // 10 gets - number_of_puts: 5, // 5 puts - }; - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, max_batch_index, total_node_usage) - verify { - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::RewardingProviders); - assert_eq!(billing_report.rewarding_max_batch_index, max_batch_index); - } - - send_rewarding_providers_batch { - let b in 1 .. MaxBatchSize::get() as u32; - - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::RewardingProviders; - let total_customer_charge = CustomerCharge { - transfer: (200 * CERE).saturating_mul(b.into()), // price for 200 mb per customer - storage: (100 * CERE).saturating_mul(b.into()), // price for 100 mb per customer - gets: (10 * CERE).saturating_mul(b.into()), // price for 10 gets per customer - puts: (5 * CERE).saturating_mul(b.into()), // price for 5 puts per customer - }; - let total_distributed_reward : u128 = 0; - let total_node_usage = NodeUsage { - transferred_bytes: 200000000u64.saturating_mul(b.into()), // 200 mb per provider - stored_bytes: 100000000i64.saturating_mul(b.into()), // 100 mb 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 = BoundedBTreeSet::default(); - charging_processed_batches.try_insert(0).unwrap(); - let rewarding_max_batch_index = 0; - let rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge: total_customer_charge.clone(), - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let vault = DdcPayouts::::sub_account_id(cluster_id, era); - let total_customer_charge_amount = total_customer_charge.transfer + total_customer_charge.storage + total_customer_charge.gets + total_customer_charge.puts; - endow_account::(&vault, total_customer_charge_amount + T::Currency::minimum_balance().saturated_into::()); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - let batch_index: BatchIndex = 0; - let mut payees_batch: Vec<(NodePubKey, NodeUsage)> = vec![]; - - for i in 0..b { - let provider = create_account::("provider", i, i); - - endow_account::(&provider, T::Currency::minimum_balance().saturated_into()); - - let usage = NodeUsage { - transferred_bytes: 200000000, // 200 mb - stored_bytes: 100000000, // 100 mb - number_of_gets: 10, // 10 gets - number_of_puts: 5, // 5 puts - }; - - let key = sp_io::hashing::blake2_256(&i.encode()); - let node_key = NodePubKey::StoragePubKey(AccountId32::from(key)); - - T::NodeManager::create_node( - node_key.clone(), - provider, - NodeParams::StorageParams(StorageNodeParams { - mode: StorageNodeMode::Storage, - host: vec![1u8; 255], - domain: vec![2u8; 255], - ssl: true, - http_port: 35000u16, - grpc_port: 25000u16, - p2p_port: 15000u16, - }), - ).expect("Node to be created"); - - payees_batch.push((node_key, usage)); - } - - let activity_hashes = payees_batch.clone().into_iter().map(|(node_key, usage)| { - let mut data = format!("0x{}", node_key.get_hex()).encode(); - data.extend_from_slice(&usage.stored_bytes.encode()); - data.extend_from_slice(&usage.transferred_bytes.encode()); - data.extend_from_slice(&usage.number_of_puts.encode()); - data.extend_from_slice(&usage.number_of_gets.encode()); - sp_io::hashing::blake2_256(&data) - }).collect::>(); - - let store1 = MemStore::default(); - let mut mmr1: MMR> = MemMMR::::new(0, &store1); - for activity_hash in activity_hashes { - let _pos: u64 = mmr1.push(activity_hash).unwrap(); - } - - let batch_root = mmr1.get_root().unwrap(); - - let store2 = MemStore::default(); - let mut mmr2: MMR> = MemMMR::::new(0, &store2); - let pos = mmr2.push(batch_root).unwrap(); - let payees_merkle_root_hash = mmr2.get_root().unwrap(); - - let payers_merkle_root_hash = ActivityHash::default(); - - let proof = mmr2 - .gen_proof(vec![pos]) - .unwrap() - .proof_items() - .to_vec(); - - let mut validators = BTreeMap::new(); - validators.insert( - (payers_merkle_root_hash, payees_merkle_root_hash), - vec![dac_account.clone()], - ); - let start_era: i64 = 1_000_000_000; - let end_era: i64 = start_era + AVG_SECONDS_MONTH; - let era_validation = EraValidation:: { - validators, - start_era, - end_era, - payers_merkle_root_hash, - payees_merkle_root_hash, - status: EraValidationStatus::PayoutInProgress, - }; - - set_validation_era::(cluster_id, era, era_validation); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payees_batch, MMRProof { proof }) + dummy_call { + let user = account::("name", 0, 0); + whitelist_account!(user); + }: _(RawOrigin::Signed(user), 100u128) verify { - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::RewardingProviders); - assert!(billing_report.rewarding_processed_batches.contains(&batch_index)); } - - end_rewarding_providers { - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::RewardingProviders; - let total_customer_charge = CustomerCharge { - transfer: 200 * CERE, // price for 200 mb - storage: 100 * CERE, // price for 100 mb - gets: 10 * CERE, // price for 10 gets - puts: 5 * CERE, // price for 5 puts - }; - let total_distributed_reward : u128 = total_customer_charge.transfer + total_customer_charge.storage + total_customer_charge.gets + total_customer_charge.puts; - let total_node_usage = NodeUsage { - transferred_bytes: 200000000, // 200 mb - stored_bytes: 100000000, // 100 mb - number_of_gets: 10, // 10 gets - number_of_puts: 5, // 5 puts - }; - let charging_max_batch_index = 0; - let mut charging_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - charging_processed_batches.try_insert(0).unwrap(); - let rewarding_max_batch_index = 0; - let mut rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - rewarding_processed_batches.try_insert(0).unwrap(); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge, - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era) - verify { - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::ProvidersRewarded); - } - - end_billing_report { - let cluster_id = ClusterId::from([1; 20]); - let era : DdcEra = 1; - let state = PayoutState::ProvidersRewarded; - let total_customer_charge = CustomerCharge { - transfer: 200 * CERE, // price for 200 mb - storage: 100 * CERE, // price for 100 mb - gets: 10 * CERE, // price for 10 gets - puts: 5 * CERE, // price for 5 puts - }; - let total_distributed_reward : u128 = total_customer_charge.transfer + total_customer_charge.storage + total_customer_charge.gets + total_customer_charge.puts; - let total_node_usage = NodeUsage { - transferred_bytes: 200000000, // 200 mb - stored_bytes: 100000000, // 100 mb - number_of_gets: 10, // 10 gets - number_of_puts: 5, // 5 puts - }; - let charging_max_batch_index = 0; - let mut charging_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - charging_processed_batches.try_insert(0).unwrap(); - let rewarding_max_batch_index = 0; - let mut rewarding_processed_batches : BoundedBTreeSet = BoundedBTreeSet::default(); - rewarding_processed_batches.try_insert(0).unwrap(); - - create_default_cluster::(cluster_id); - create_billing_report::(BillingReportParams { - cluster_id, - era, - state, - total_customer_charge, - total_distributed_reward, - total_node_usage, - charging_max_batch_index, - charging_processed_batches, - rewarding_max_batch_index, - rewarding_processed_batches, - }); - - let dac_account = create_dac_account::(); - whitelist_account!(dac_account); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era) - verify { - assert!(ActiveBillingReports::::contains_key(cluster_id, era)); - let billing_report = ActiveBillingReports::::get(cluster_id, era).unwrap(); - assert_eq!(billing_report.state, PayoutState::Finalized); - } - } diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index dd8d659dc..ef5ea7232 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -14,13 +14,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![recursion_limit = "256"] -pub mod weights; - -use crate::weights::WeightInfo; - -#[cfg(feature = "runtime-benchmarks")] -pub mod benchmarking; - #[cfg(test)] pub(crate) mod mock; #[cfg(test)] @@ -28,6 +21,12 @@ mod tests; pub mod migrations; +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::BillingReportParams; + +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; + use ddc_primitives::{ traits::{ bucket::BucketManager, @@ -39,8 +38,9 @@ use ddc_primitives::{ pallet::PalletVisitor as PalletVisitorType, payout::PayoutProcessor, }, - BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, - PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, MILLICENTS, + BatchIndex, BucketId, ClusterId, CustomerCharge, CustomerUsage, DdcEra, MMRProof, NodePubKey, + NodeReward, NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, + MILLICENTS, }; use frame_election_provider_support::SortedListProvider; use frame_support::{ @@ -55,14 +55,6 @@ pub use pallet::*; use scale_info::prelude::string::String; use sp_runtime::{traits::Convert, AccountId32, PerThing, Perquintill}; use sp_std::prelude::*; -/// Stores reward in tokens(units) of node provider as per NodeUsage -#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] -pub struct NodeReward { - pub transfer: u128, // reward in tokens for NodeUsage::transferred_bytes - pub storage: u128, // reward in tokens for NodeUsage::stored_bytes - pub puts: u128, // reward in tokens for NodeUsage::number_of_puts - pub gets: u128, // reward in tokens for NodeUsage::number_of_gets -} #[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] pub struct BillingReportDebt { @@ -72,15 +64,6 @@ pub struct BillingReportDebt { pub amount: u128, } -/// Stores charge in tokens(units) of customer as per CustomerUsage -#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] -pub struct CustomerCharge { - pub transfer: u128, // charge in tokens for CustomerUsage::transferred_bytes - pub storage: u128, // charge in tokens for CustomerUsage::stored_bytes - pub puts: u128, // charge in tokens for CustomerUsage::number_of_puts - pub gets: u128, // charge in tokens for CustomerUsage::number_of_gets -} - /// The balance type of this pallet. pub type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -128,7 +111,6 @@ pub mod pallet { type ClusterProtocol: ClusterProtocolType>; type NominatorsAndValidatorsList: SortedListProvider; type ClusterCreator: ClusterCreatorType>; - type WeightInfo: WeightInfo; type VoteScoreToU64: Convert, u64>; type ValidatorVisitor: ValidatorVisitor; type AccountIdConverter: From + Into; @@ -336,7 +318,13 @@ pub mod pallet { } #[pallet::call] - impl Pallet {} + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight(T::DbWeight::get().reads_writes(2, 5))] // todo! implement weights + pub fn dummy_call(origin: OriginFor, num: u128) -> DispatchResult { + Ok(()) + } + } fn charge_treasury_fees( treasury_fee: u128, @@ -1188,5 +1176,40 @@ pub mod pallet { Ok(None) } + + #[cfg(feature = "runtime-benchmarks")] + fn create_billing_report(vault: T::AccountId, params: BillingReportParams) { + let mut charging_processed_batches = + BoundedBTreeSet::::new(); + for batch in params.charging_processed_batches { + charging_processed_batches + .try_insert(batch) + .expect("Charging batch to be inserted"); + } + + let mut rewarding_processed_batches = + BoundedBTreeSet::::new(); + for batch in params.rewarding_processed_batches { + rewarding_processed_batches + .try_insert(batch) + .expect("Rewarding batch to be inserted"); + } + + let billing_report = BillingReport:: { + vault, + start_era: params.start_era, + end_era: params.end_era, + state: params.state, + total_customer_charge: params.total_customer_charge, + total_distributed_reward: params.total_distributed_reward, + total_node_usage: params.total_node_usage, + charging_max_batch_index: params.charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index: params.rewarding_max_batch_index, + rewarding_processed_batches, + }; + + ActiveBillingReports::::insert(params.cluster_id, params.era, billing_report); + } } } diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index bf8a0dd8d..583a1a63a 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -206,18 +206,6 @@ impl ValidatorVisitor for MockValidatorVisitor where ::AccountId: From, { - #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(_validators_with_keys: Vec<(T::AccountId, T::AccountId)>) { - unimplemented!() - } - #[cfg(feature = "runtime-benchmarks")] - fn setup_validation_era( - _cluster_id: ClusterId, - _era_id: DdcEra, - _era_validation: EraValidation, - ) { - unimplemented!() - } fn is_ocw_validator(caller: T::AccountId) -> bool { caller == VALIDATOR_OCW_KEY_32.into() } diff --git a/pallets/ddc-payouts/src/weights.rs b/pallets/ddc-payouts/src/weights.rs deleted file mode 100644 index 9b621d531..000000000 --- a/pallets/ddc-payouts/src/weights.rs +++ /dev/null @@ -1,228 +0,0 @@ -//! Autogenerated weights for pallet_ddc_payouts -//! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-07-05, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! HOSTNAME: `bench`, CPU: `AMD EPYC-Milan Processor` -//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 - -// Executed Command: -// ./target/release/cere -// benchmark -// pallet -// --chain=dev -// --execution=wasm -// --wasm-execution=compiled -// --pallet=pallet_ddc_payouts -// --extrinsic=* -// --steps=50 -// --repeat=20 -// --template=./.maintain/frame-weight-template.hbs -// --output=pallets/ddc-payouts/weights.rs - -#![cfg_attr(rustfmt, rustfmt_skip)] -#![allow(unused_parens)] -#![allow(unused_imports)] - -use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; -use sp_std::marker::PhantomData; - -/// Weight functions needed for pallet_ddc_payouts. -pub trait WeightInfo { - fn begin_billing_report() -> Weight; - fn begin_charging_customers() -> Weight; - fn send_charging_customers_batch(b: u32, ) -> Weight; - fn end_charging_customers() -> Weight; - fn begin_rewarding_providers() -> Weight; - fn send_rewarding_providers_batch(b: u32, ) -> Weight; - fn end_rewarding_providers() -> Weight; - fn end_billing_report() -> Weight; -} - -/// Weights for pallet_ddc_payouts using the Substrate node and recommended hardware. -pub struct SubstrateWeight(PhantomData); -impl WeightInfo for SubstrateWeight { - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn begin_billing_report() -> Weight { - Weight::from_parts(26_119_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn begin_charging_customers() -> Weight { - Weight::from_parts(28_303_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) - // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `DdcCustomers::Ledger` (r:999 w:999) - // Proof: `DdcCustomers::Ledger` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `System::Account` (r:2 w:2) - // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - // Storage: `DdcPayouts::DebtorCustomers` (r:999 w:999) - // Proof: `DdcPayouts::DebtorCustomers` (`max_values`: None, `max_size`: None, mode: `Measured`) - /// The range of component `b` is `[1, 1000]`. - fn send_charging_customers_batch(b: u32, ) -> Weight { - Weight::from_parts(135_154_000_u64, 0) - // Standard Error: 318_121 - .saturating_add(Weight::from_parts(79_437_612_u64, 0).saturating_mul(b as u64)) - .saturating_add(T::DbWeight::get().reads(7_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(b as u64))) - .saturating_add(T::DbWeight::get().writes(5_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(b as u64))) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) - // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `System::Account` (r:3 w:3) - // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - // Storage: `DdcClusters::Clusters` (r:1 w:0) - // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `Staking::Validators` (r:2 w:0) - // Proof: `Staking::Validators` (`max_values`: None, `max_size`: Some(45), added: 2520, mode: `MaxEncodedLen`) - // Storage: `Staking::Bonded` (r:1 w:0) - // Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`) - // Storage: `Staking::Ledger` (r:1 w:0) - // Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`) - // Storage: `Staking::Nominators` (r:1 w:0) - // Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`) - fn end_charging_customers() -> Weight { - Weight::from_parts(281_087_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(12_u64)) - .saturating_add(T::DbWeight::get().writes(4_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn begin_rewarding_providers() -> Weight { - Weight::from_parts(29_155_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `System::Account` (r:1000 w:1000) - // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - /// The range of component `b` is `[1, 1000]`. - fn send_rewarding_providers_batch(b: u32, ) -> Weight { - Weight::from_parts(91_662_000_u64, 0) - // Standard Error: 34_032 - .saturating_add(Weight::from_parts(62_733_592_u64, 0).saturating_mul(b as u64)) - .saturating_add(T::DbWeight::get().reads(3_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(b as u64))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(b as u64))) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn end_rewarding_providers() -> Weight { - Weight::from_parts(29_986_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn end_billing_report() -> Weight { - Weight::from_parts(29_615_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } -} - -// For backwards compatibility and tests -impl WeightInfo for () { - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn begin_billing_report() -> Weight { - Weight::from_parts(26_119_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn begin_charging_customers() -> Weight { - Weight::from_parts(28_303_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) - // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `DdcCustomers::Ledger` (r:999 w:999) - // Proof: `DdcCustomers::Ledger` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `System::Account` (r:2 w:2) - // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - // Storage: `DdcPayouts::DebtorCustomers` (r:999 w:999) - // Proof: `DdcPayouts::DebtorCustomers` (`max_values`: None, `max_size`: None, mode: `Measured`) - /// The range of component `b` is `[1, 1000]`. - fn send_charging_customers_batch(b: u32, ) -> Weight { - Weight::from_parts(135_154_000_u64, 0) - // Standard Error: 318_121 - .saturating_add(Weight::from_parts(79_437_612_u64, 0).saturating_mul(b as u64)) - .saturating_add(RocksDbWeight::get().reads(7_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(b as u64))) - .saturating_add(RocksDbWeight::get().writes(5_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(b as u64))) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) - // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `System::Account` (r:3 w:3) - // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - // Storage: `DdcClusters::Clusters` (r:1 w:0) - // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `Staking::Validators` (r:2 w:0) - // Proof: `Staking::Validators` (`max_values`: None, `max_size`: Some(45), added: 2520, mode: `MaxEncodedLen`) - // Storage: `Staking::Bonded` (r:1 w:0) - // Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`) - // Storage: `Staking::Ledger` (r:1 w:0) - // Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`) - // Storage: `Staking::Nominators` (r:1 w:0) - // Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`) - fn end_charging_customers() -> Weight { - Weight::from_parts(281_087_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(12_u64)) - .saturating_add(RocksDbWeight::get().writes(4_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn begin_rewarding_providers() -> Weight { - Weight::from_parts(29_155_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - // Storage: `System::Account` (r:1000 w:1000) - // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - /// The range of component `b` is `[1, 1000]`. - fn send_rewarding_providers_batch(b: u32, ) -> Weight { - Weight::from_parts(91_662_000_u64, 0) - // Standard Error: 34_032 - .saturating_add(Weight::from_parts(62_733_592_u64, 0).saturating_mul(b as u64)) - .saturating_add(RocksDbWeight::get().reads(3_u64)) - .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(b as u64))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(b as u64))) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn end_rewarding_providers() -> Weight { - Weight::from_parts(29_986_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } - // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn end_billing_report() -> Weight { - Weight::from_parts(29_615_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } -} diff --git a/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index 38387eba4..c098df78e 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -1,9 +1,17 @@ #![cfg(feature = "runtime-benchmarks")] -use frame_benchmarking::{account, v2::*}; +use ddc_primitives::{ + ActivityHash, BillingReportParams, BucketId, BucketParams, ClusterId, ClusterParams, + ClusterProtocolParams, CustomerCharge, EraValidation, EraValidationStatus, MergeActivityHash, + NodeParams, NodePubKey, PayoutState, StorageNodeMode, StorageNodeParams, AVG_SECONDS_MONTH, + DOLLARS as CERE, MAX_PAYOUT_BATCH_SIZE, +}; +use frame_benchmarking::{account, v2::*, whitelist_account}; use frame_system::RawOrigin; -use sp_io::hashing::blake2_256; -use sp_runtime::{SaturatedConversion, Saturating}; +use sp_io::hashing::{blake2_128, blake2_256}; +use sp_runtime::{ + traits::AccountIdConversion, AccountId32, Perquintill, SaturatedConversion, Saturating, +}; use sp_std::vec; use super::*; @@ -17,10 +25,8 @@ mod benchmarks { fn create_validator_account() -> T::AccountId { let validator = create_account::("validator_account", 0, 0); - as ValidatorVisitor>::setup_validators(vec![( - validator.clone(), - validator.clone(), - )]); + ValidatorToStashKey::::insert(&validator.clone(), &validator.clone()); + ValidatorSet::::put(vec![validator.clone()]); validator } @@ -37,6 +43,137 @@ mod benchmarks { let _ = T::Currency::make_free_balance_be(account, balance); } + fn endow_customer(customer: &T::AccountId, amount: u128) { + endow_account::(customer, amount); + T::CustomerDepositor::deposit( + customer.clone(), + // we need to keep min existensial deposit + amount - T::Currency::minimum_balance().saturated_into::(), + ) + .expect("Customer deposit failed"); + } + + fn create_cluster( + cluster_id: ClusterId, + cluster_manager_id: T::AccountId, + cluster_reserve_id: T::AccountId, + cluster_params: ClusterParams, + cluster_protocol_params: ClusterProtocolParams, BlockNumberFor>, + ) { + T::ClusterCreator::create_cluster( + cluster_id, + cluster_manager_id, + cluster_reserve_id, + cluster_params, + cluster_protocol_params, + ) + .expect("Cluster is not created"); + } + + fn create_default_cluster(cluster_id: ClusterId) { + let cluster_manager = create_account::("cm", 0, 0); + let cluster_reserve = create_account::("cr", 0, 0); + let cluster_params = ClusterParams { + node_provider_auth_contract: Default::default(), + erasure_coding_required: 4, + erasure_coding_total: 6, + replication_total: 3, + }; + let cluster_protocol_params: ClusterProtocolParams, BlockNumberFor> = + ClusterProtocolParams { + treasury_share: Perquintill::from_percent(5), + validators_share: Perquintill::from_percent(10), + cluster_reserve_share: Perquintill::from_percent(15), + unit_per_mb_stored: CERE, + unit_per_mb_streamed: CERE, + unit_per_put_request: CERE, + unit_per_get_request: CERE, + ..Default::default() + }; + + create_cluster::( + cluster_id, + cluster_manager, + cluster_reserve, + cluster_params, + cluster_protocol_params, + ); + } + + fn setup_validation_era( + cluster_id: ClusterId, + era_id: DdcEra, + validators: Vec, + payers_merkle_root_hash: ActivityHash, + payees_merkle_root_hash: ActivityHash, + status: EraValidationStatus, + ) { + let mut validations_map = BTreeMap::new(); + for validator in validators { + validations_map.insert( + (payers_merkle_root_hash, payees_merkle_root_hash), + vec![validator.clone()], + ); + } + + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + + let era_validation = EraValidation:: { + validators: validations_map, + start_era, + end_era, + payers_merkle_root_hash, + payees_merkle_root_hash, + status, + }; + + >::insert(cluster_id, era_id, era_validation); + } + + fn create_billing_report( + cluster_id: ClusterId, + era_id: DdcEra, + start_era: i64, + end_era: i64, + state: PayoutState, + total_customer_charge: CustomerCharge, + total_distributed_reward: u128, + total_node_usage: NodeUsage, + charging_max_batch_index: BatchIndex, + charging_processed_batches: Vec, + rewarding_max_batch_index: BatchIndex, + rewarding_processed_batches: Vec, + ) { + let hash = blake2_128(&0.encode()); + let vault = T::PalletId::get().into_sub_account_truncating(hash); + + let total_customer_charge_amount = total_customer_charge.transfer + + total_customer_charge.storage + + total_customer_charge.gets + + total_customer_charge.puts; + + endow_account::(&vault, total_customer_charge_amount); + + T::PayoutProcessor::create_billing_report( + vault.clone(), + BillingReportParams { + cluster_id, + era: era_id, + start_era, + end_era, + state, + total_customer_charge, + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + }, + ); + } + #[benchmark] fn set_prepare_era_for_payout(b: Linear<1, 5>) { let validator = create_validator_account::(); @@ -88,6 +225,599 @@ mod benchmarks { assert_has_event::(Event::ValidatorKeySet { validator }.into()); } + #[benchmark] + fn begin_billing_report() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + + create_default_cluster::(cluster_id); + let validator = create_validator_account::(); + whitelist_account!(validator); + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + blake2_256(&1.encode()), + blake2_256(&2.encode()), + EraValidationStatus::ReadyForPayout, + ); + + #[extrinsic_call] + begin_billing_report(RawOrigin::Signed(validator), cluster_id, era_id, start_era, end_era); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::Initialized); + } + + #[benchmark] + fn begin_charging_customers() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::Initialized; + let total_customer_charge = Default::default(); + let total_distributed_reward: u128 = 0; + let total_node_usage = Default::default(); + let charging_max_batch_index = 10; + let charging_processed_batches = Default::default(); + let rewarding_max_batch_index = Default::default(); + let rewarding_processed_batches = Default::default(); + + create_default_cluster::(cluster_id); + + let validator = create_validator_account::(); + whitelist_account!(validator); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + blake2_256(&1.encode()), + blake2_256(&2.encode()), + EraValidationStatus::ReadyForPayout, + ); + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge, + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + #[extrinsic_call] + begin_charging_customers( + RawOrigin::Signed(validator), + cluster_id, + era_id, + charging_max_batch_index, + ); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::ChargingCustomers); + } + + #[benchmark] + fn send_charging_customers_batch(b: Linear<1, { MAX_PAYOUT_BATCH_SIZE.into() }>) { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::ChargingCustomers; + let total_customer_charge = Default::default(); + let total_distributed_reward: u128 = 0; + let total_node_usage = Default::default(); + let charging_max_batch_index = 0; + let charging_processed_batches = Default::default(); + let rewarding_max_batch_index = Default::default(); + let rewarding_processed_batches = Default::default(); + + create_default_cluster::(cluster_id); + + let validator = create_validator_account::(); + whitelist_account!(validator); + + let batch_index: BatchIndex = 0; + let mut payers_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = vec![]; + for i in 0..b { + let customer = create_account::("customer", i, i); + + if b % 2 == 0 { + // no customer debt path + endow_customer::(&customer, 1_000_000 * CERE); + } else { + // customer debt path + endow_customer::(&customer, 10 * CERE); + } + + let customer_usage = CustomerUsage { + transferred_bytes: 200000000, // 200 mb + stored_bytes: 100000000, // 100 mb + number_of_gets: 10, // 10 gets + number_of_puts: 5, // 5 puts + }; + + let bucket_id: BucketId = (i + 1).into(); + let node_key = NodePubKey::StoragePubKey(AccountId32::from([ + 48, 47, 147, 125, 243, 160, 236, 76, 101, 142, 129, 34, 67, 158, 116, 141, 34, 116, + 66, 235, 212, 147, 206, 245, 33, 161, 225, 73, 67, 132, 67, 149, + ])); + + T::BucketManager::create_bucket( + &cluster_id, + bucket_id, + customer, + BucketParams { is_public: true }, + ) + .expect("Bucket to be created"); + + payers_batch.push((node_key, bucket_id, customer_usage)); + } + + let activity_hashes = payers_batch + .clone() + .into_iter() + .map(|(node_key, bucket_id, usage)| { + let mut data = bucket_id.encode(); + let node_id = format!("0x{}", node_key.get_hex()); + data.extend_from_slice(&node_id.encode()); + data.extend_from_slice(&usage.stored_bytes.encode()); + data.extend_from_slice(&usage.transferred_bytes.encode()); + data.extend_from_slice(&usage.number_of_puts.encode()); + data.extend_from_slice(&usage.number_of_gets.encode()); + sp_io::hashing::blake2_256(&data) + }) + .collect::>(); + + let store1 = MemStore::default(); + let mut mmr1: MMR> = + MemMMR::::new(0, &store1); + for activity_hash in activity_hashes { + let _pos: u64 = mmr1.push(activity_hash).unwrap(); + } + + let batch_root = mmr1.get_root().unwrap(); + + let store2 = MemStore::default(); + let mut mmr2: MMR> = + MemMMR::::new(0, &store2); + let pos = mmr2.push(batch_root).unwrap(); + let payers_merkle_root_hash = mmr2.get_root().unwrap(); + + let proof = mmr2.gen_proof(vec![pos]).unwrap().proof_items().to_vec(); + + let payees_merkle_root_hash = ActivityHash::default(); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + payers_merkle_root_hash, + payees_merkle_root_hash, + EraValidationStatus::PayoutInProgress, + ); + + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge, + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + #[extrinsic_call] + send_charging_customers_batch( + RawOrigin::Signed(validator), + cluster_id, + era_id, + batch_index, + payers_batch, + MMRProof { proof }, + ); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::ChargingCustomers); + assert!(T::PayoutProcessor::all_customer_batches_processed(&cluster_id, era_id)); + } + + #[benchmark] + fn end_charging_customers() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::ChargingCustomers; + let total_customer_charge = CustomerCharge { + transfer: 200 * CERE, // price for 200 mb + storage: 100 * CERE, // price for 100 mb + gets: 10 * CERE, // price for 10 gets + puts: 5 * CERE, // price for 5 puts + }; + let total_distributed_reward: u128 = 0; + let total_node_usage = Default::default(); + let charging_max_batch_index = 0; + let charging_processed_batches = vec![0]; + let rewarding_max_batch_index = Default::default(); + let rewarding_processed_batches = Default::default(); + + create_default_cluster::(cluster_id); + let validator = create_validator_account::(); + whitelist_account!(validator); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + blake2_256(&1.encode()), + blake2_256(&2.encode()), + EraValidationStatus::PayoutInProgress, + ); + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge.clone(), + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + #[extrinsic_call] + end_charging_customers(RawOrigin::Signed(validator), cluster_id, era_id); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::CustomersChargedWithFees); + assert!(T::PayoutProcessor::all_customer_batches_processed(&cluster_id, era_id)); + } + + #[benchmark] + fn begin_rewarding_providers() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::CustomersChargedWithFees; + let total_customer_charge = CustomerCharge { + transfer: 200 * CERE, // price for 200 mb + storage: 100 * CERE, // price for 100 mb + gets: 10 * CERE, // price for 10 gets + puts: 5 * CERE, // price for 5 puts + }; + let total_distributed_reward: u128 = 0; + let total_node_usage = Default::default(); + let charging_max_batch_index = 0; + let charging_processed_batches = vec![0]; + let rewarding_max_batch_index = 10; + let rewarding_processed_batches = Default::default(); + + create_default_cluster::(cluster_id); + let validator = create_validator_account::(); + whitelist_account!(validator); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + blake2_256(&1.encode()), + blake2_256(&2.encode()), + EraValidationStatus::PayoutInProgress, + ); + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge, + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + let total_node_usage = NodeUsage { + transferred_bytes: 200000000, // 200 mb + stored_bytes: 100000000, // 100 mb + number_of_gets: 10, // 10 gets + number_of_puts: 5, // 5 puts + }; + + #[extrinsic_call] + begin_rewarding_providers( + RawOrigin::Signed(validator), + cluster_id, + era_id, + rewarding_max_batch_index, + total_node_usage, + ); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::RewardingProviders); + } + + #[benchmark] + fn send_rewarding_providers_batch(b: Linear<1, { MAX_PAYOUT_BATCH_SIZE.into() }>) { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::RewardingProviders; + let total_customer_charge = CustomerCharge { + transfer: (200 * CERE).saturating_mul(b.into()), // price for 200 mb per customer + storage: (100 * CERE).saturating_mul(b.into()), // price for 100 mb per customer + gets: (10 * CERE).saturating_mul(b.into()), // price for 10 gets per customer + puts: (5 * CERE).saturating_mul(b.into()), // price for 5 puts per customer + }; + let total_distributed_reward: u128 = 0; + let total_node_usage = NodeUsage { + transferred_bytes: 200000000u64.saturating_mul(b.into()), // 200 mb per provider + stored_bytes: 100000000i64.saturating_mul(b.into()), // 100 mb 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 charging_processed_batches = vec![0]; + let rewarding_max_batch_index = 0; + let rewarding_processed_batches = vec![]; + + create_default_cluster::(cluster_id); + let validator = create_validator_account::(); + whitelist_account!(validator); + + let batch_index: BatchIndex = 0; + let mut payees_batch: Vec<(NodePubKey, NodeUsage)> = vec![]; + + for i in 0..b { + let provider = create_account::("provider", i, i); + + endow_account::(&provider, T::Currency::minimum_balance().saturated_into()); + + let usage = NodeUsage { + transferred_bytes: 200000000, // 200 mb + stored_bytes: 100000000, // 100 mb + number_of_gets: 10, // 10 gets + number_of_puts: 5, // 5 puts + }; + + let key = sp_io::hashing::blake2_256(&i.encode()); + let node_key = NodePubKey::StoragePubKey(AccountId32::from(key)); + + T::NodeManager::create_node( + node_key.clone(), + provider, + NodeParams::StorageParams(StorageNodeParams { + mode: StorageNodeMode::Storage, + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, + http_port: 35000u16, + grpc_port: 25000u16, + p2p_port: 15000u16, + }), + ) + .expect("Node to be created"); + + payees_batch.push((node_key, usage)); + } + + let activity_hashes = payees_batch + .clone() + .into_iter() + .map(|(node_key, usage)| { + let mut data = format!("0x{}", node_key.get_hex()).encode(); + data.extend_from_slice(&usage.stored_bytes.encode()); + data.extend_from_slice(&usage.transferred_bytes.encode()); + data.extend_from_slice(&usage.number_of_puts.encode()); + data.extend_from_slice(&usage.number_of_gets.encode()); + sp_io::hashing::blake2_256(&data) + }) + .collect::>(); + + let store1 = MemStore::default(); + let mut mmr1: MMR> = + MemMMR::::new(0, &store1); + for activity_hash in activity_hashes { + let _pos: u64 = mmr1.push(activity_hash).unwrap(); + } + + let batch_root = mmr1.get_root().unwrap(); + + let store2 = MemStore::default(); + let mut mmr2: MMR> = + MemMMR::::new(0, &store2); + let pos = mmr2.push(batch_root).unwrap(); + let payees_merkle_root_hash = mmr2.get_root().unwrap(); + + let payers_merkle_root_hash = ActivityHash::default(); + + let proof = mmr2.gen_proof(vec![pos]).unwrap().proof_items().to_vec(); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + payers_merkle_root_hash, + payees_merkle_root_hash, + EraValidationStatus::PayoutInProgress, + ); + + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge, + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + #[extrinsic_call] + send_rewarding_providers_batch( + RawOrigin::Signed(validator), + cluster_id, + era_id, + batch_index, + payees_batch, + MMRProof { proof }, + ); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::RewardingProviders); + assert!(T::PayoutProcessor::all_provider_batches_processed(&cluster_id, era_id)); + } + + #[benchmark] + fn end_rewarding_providers() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::RewardingProviders; + let total_customer_charge = CustomerCharge { + transfer: 200 * CERE, // price for 200 mb + storage: 100 * CERE, // price for 100 mb + gets: 10 * CERE, // price for 10 gets + puts: 5 * CERE, // price for 5 puts + }; + let total_distributed_reward: u128 = total_customer_charge.transfer + + total_customer_charge.storage + + total_customer_charge.gets + + total_customer_charge.puts; + let total_node_usage = NodeUsage { + transferred_bytes: 200000000, // 200 mb + stored_bytes: 100000000, // 100 mb + number_of_gets: 10, // 10 gets + number_of_puts: 5, // 5 puts + }; + let charging_max_batch_index = 0; + let charging_processed_batches = vec![0]; + let rewarding_max_batch_index = 0; + let rewarding_processed_batches = vec![0]; + + create_default_cluster::(cluster_id); + let validator = create_validator_account::(); + whitelist_account!(validator); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + blake2_256(&1.encode()), + blake2_256(&2.encode()), + EraValidationStatus::PayoutInProgress, + ); + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge.clone(), + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + #[extrinsic_call] + end_rewarding_providers(RawOrigin::Signed(validator), cluster_id, era_id); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::ProvidersRewarded); + } + + #[benchmark] + fn end_billing_report() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + let start_era: i64 = 1_000_000_000; + let end_era: i64 = start_era + AVG_SECONDS_MONTH; + let state = PayoutState::ProvidersRewarded; + let total_customer_charge = CustomerCharge { + transfer: 200 * CERE, // price for 200 mb + storage: 100 * CERE, // price for 100 mb + gets: 10 * CERE, // price for 10 gets + puts: 5 * CERE, // price for 5 puts + }; + let total_distributed_reward: u128 = total_customer_charge.transfer + + total_customer_charge.storage + + total_customer_charge.gets + + total_customer_charge.puts; + let total_node_usage = NodeUsage { + transferred_bytes: 200000000, // 200 mb + stored_bytes: 100000000, // 100 mb + number_of_gets: 10, // 10 gets + number_of_puts: 5, // 5 puts + }; + let charging_max_batch_index = 0; + let charging_processed_batches = vec![0]; + let rewarding_max_batch_index = 0; + let rewarding_processed_batches = vec![0]; + + create_default_cluster::(cluster_id); + let validator = create_validator_account::(); + whitelist_account!(validator); + + setup_validation_era::( + cluster_id, + era_id, + vec![validator.clone()], + blake2_256(&1.encode()), + blake2_256(&2.encode()), + EraValidationStatus::PayoutInProgress, + ); + create_billing_report::( + cluster_id, + era_id, + start_era, + end_era, + state, + total_customer_charge.clone(), + total_distributed_reward, + total_node_usage, + charging_max_batch_index, + charging_processed_batches, + rewarding_max_batch_index, + rewarding_processed_batches, + ); + + #[extrinsic_call] + end_billing_report(RawOrigin::Signed(validator), cluster_id, era_id); + + let status = T::PayoutProcessor::get_billing_report_status(&cluster_id, era_id); + assert_eq!(status, PayoutState::Finalized); + } + #[benchmark] fn emit_consensus_errors(b: Linear<1, 5>) { let validator = create_validator_account::(); diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index a9bc10c71..04bcf52a3 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -12,6 +12,8 @@ use core::str; use base64ct::{Base64, Encoding}; +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::traits::{BucketManager, ClusterCreator, CustomerDepositor}; use ddc_primitives::{ traits::{ ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutProcessor, @@ -23,37 +25,34 @@ use ddc_primitives::{ }; use frame_support::{ pallet_prelude::*, - traits::{Get, OneSessionHandler}, + traits::{Currency, Get, OneSessionHandler}, }; use frame_system::{ offchain::{AppCrypto, CreateSignedTransaction, SendSignedTransaction, Signer}, pallet_prelude::*, }; +use itertools::Itertools; pub use pallet::*; use polkadot_ckb_merkle_mountain_range::{ helper::{leaf_index_to_mmr_size, leaf_index_to_pos}, util::{MemMMR, MemStore}, MerkleProof, MMR, }; +use rand::{prelude::*, rngs::SmallRng, SeedableRng}; use scale_info::prelude::{format, string::String}; use serde::{Deserialize, Serialize}; use sp_application_crypto::RuntimeAppPublic; +use sp_core::crypto::UncheckedFrom; +pub use sp_io::crypto::sr25519_public_keys; use sp_runtime::{ offchain::{http, Duration, StorageKind}, - traits::Hash, + traits::{Hash, IdentifyAccount}, Percent, }; -use sp_std::{collections::btree_map::BTreeMap, prelude::*}; -pub mod weights; -use frame_support::traits::Currency; -use itertools::Itertools; -use rand::{prelude::*, rngs::SmallRng, SeedableRng}; -use sp_core::crypto::UncheckedFrom; -pub use sp_io::crypto::sr25519_public_keys; -use sp_runtime::traits::IdentifyAccount; use sp_staking::StakingInterface; -use sp_std::fmt::Debug; +use sp_std::{collections::btree_map::BTreeMap, fmt::Debug, prelude::*}; +pub mod weights; use crate::weights::WeightInfo; #[cfg(feature = "runtime-benchmarks")] @@ -157,6 +156,12 @@ pub mod pallet { type AccountIdConverter: From + Into; type CustomerVisitor: CustomerVisitor; type Currency: Currency; + #[cfg(feature = "runtime-benchmarks")] + type CustomerDepositor: CustomerDepositor; + #[cfg(feature = "runtime-benchmarks")] + type ClusterCreator: ClusterCreator>; + #[cfg(feature = "runtime-benchmarks")] + type BucketManager: BucketManager; } /// The event type. @@ -3408,7 +3413,7 @@ pub mod pallet { /// /// Emits `BillingReportCreated` event when successful. #[pallet::call_index(0)] - #[pallet::weight(::WeightInfo::create_billing_reports() + T::DbWeight::get().reads_writes(2, 5))] // todo! implement weights + #[pallet::weight(::WeightInfo::set_prepare_era_for_payout(payers_batch_merkle_root_hashes.len() as u32 + payees_batch_merkle_root_hashes.len() as u32))] pub fn set_prepare_era_for_payout( origin: OriginFor, cluster_id: ClusterId, @@ -3515,7 +3520,7 @@ pub mod pallet { /// Emits `NotEnoughNodesForConsensus` OR `ActivityNotInConsensus` event depend of error /// type, when successful. #[pallet::call_index(1)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::emit_consensus_errors(errors.len() as u32))] pub fn emit_consensus_errors( origin: OriginFor, errors: Vec, @@ -3766,7 +3771,7 @@ pub mod pallet { /// Parameters: /// - `ddc_validator_pub`: validator Key #[pallet::call_index(2)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::set_validator_key())] pub fn set_validator_key( origin: OriginFor, ddc_validator_pub: T::AccountId, @@ -3787,7 +3792,7 @@ pub mod pallet { } #[pallet::call_index(3)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::begin_billing_report())] pub fn begin_billing_report( origin: OriginFor, cluster_id: ClusterId, @@ -3814,7 +3819,7 @@ pub mod pallet { } #[pallet::call_index(4)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::begin_charging_customers())] pub fn begin_charging_customers( origin: OriginFor, cluster_id: ClusterId, @@ -3827,8 +3832,7 @@ pub mod pallet { } #[pallet::call_index(5)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - // todo! remove clippy::too_many_arguments + #[pallet::weight(::WeightInfo::send_charging_customers_batch(payers.len() as u32))] pub fn send_charging_customers_batch( origin: OriginFor, cluster_id: ClusterId, @@ -3849,7 +3853,7 @@ pub mod pallet { } #[pallet::call_index(6)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::end_charging_customers())] pub fn end_charging_customers( origin: OriginFor, cluster_id: ClusterId, @@ -3861,7 +3865,7 @@ pub mod pallet { } #[pallet::call_index(7)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::begin_rewarding_providers())] pub fn begin_rewarding_providers( origin: OriginFor, cluster_id: ClusterId, @@ -3880,7 +3884,7 @@ pub mod pallet { } #[pallet::call_index(8)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::send_rewarding_providers_batch(payees.len() as u32))] pub fn send_rewarding_providers_batch( origin: OriginFor, cluster_id: ClusterId, @@ -3901,7 +3905,7 @@ pub mod pallet { } #[pallet::call_index(9)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::end_rewarding_providers())] pub fn end_rewarding_providers( origin: OriginFor, cluster_id: ClusterId, @@ -3913,7 +3917,7 @@ pub mod pallet { } #[pallet::call_index(10)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::end_billing_report())] pub fn end_billing_report( origin: OriginFor, cluster_id: ClusterId, @@ -3934,7 +3938,7 @@ pub mod pallet { // todo! remove this after devnet testing #[pallet::call_index(11)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(1_000_000)] pub fn set_era_validations( origin: OriginFor, cluster_id: ClusterId, @@ -3972,26 +3976,6 @@ pub mod pallet { } impl ValidatorVisitor for Pallet { - #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(validators_with_keys: Vec<(T::AccountId, T::AccountId)>) { - let mut validators = vec![]; - for (validator, verification_key) in validators_with_keys { - ValidatorToStashKey::::insert(&verification_key, &validator); - validators.push(validator); - } - - ValidatorSet::::put(validators); - } - - #[cfg(feature = "runtime-benchmarks")] - fn setup_validation_era( - cluster_id: ClusterId, - era_id: DdcEra, - era_validation: EraValidation, - ) { - >::insert(cluster_id, era_id, era_validation); - } - fn is_ocw_validator(caller: T::AccountId) -> bool { if ValidatorToStashKey::::contains_key(caller.clone()) { >::get().contains(&caller) diff --git a/pallets/ddc-verification/src/weights.rs b/pallets/ddc-verification/src/weights.rs index 6603465a4..060dbe580 100644 --- a/pallets/ddc-verification/src/weights.rs +++ b/pallets/ddc-verification/src/weights.rs @@ -1,27 +1,23 @@ //! Autogenerated weights for pallet_ddc_verification //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-05-21, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! HOSTNAME: `192.168.1.4`, CPU: `` +//! DATE: 2024-11-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `Yahors-MacBook-Pro.local`, CPU: `` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: -// ./target/release/cere +// ./target/debug/cere // benchmark // pallet -// --chain -// dev +// --chain=dev +// --execution=wasm // --wasm-execution=compiled -// --pallet -// pallet_ddc_verification -// --extrinsic -// * -// --steps -// 50 -// --repeat -// 20 -// --output=./pallets/ddc-verification/src/weights.rs +// --pallet=pallet_ddc_verification +// --extrinsic=* +// --steps=50 +// --repeat=20 // --template=./.maintain/frame-weight-template.hbs +// --output=pallets/ddc-verification/src/weights.rs #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -32,28 +28,364 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_ddc_verification. pub trait WeightInfo { - fn create_billing_reports() -> Weight; + fn set_prepare_era_for_payout(b: u32, ) -> Weight; + fn set_validator_key() -> Weight; + fn begin_billing_report() -> Weight; + fn begin_charging_customers() -> Weight; + fn send_charging_customers_batch(b: u32, ) -> Weight; + fn end_charging_customers() -> Weight; + fn begin_rewarding_providers() -> Weight; + fn send_rewarding_providers_batch(b: u32, ) -> Weight; + fn end_rewarding_providers() -> Weight; + fn end_billing_report() -> Weight; + fn emit_consensus_errors(b: u32, ) -> Weight; } /// Weights for pallet_ddc_verification using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - // Storage: `DdcVerification::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcVerification::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn create_billing_reports() -> Weight { - Weight::from_parts(11_000_000_u64, 0) - .saturating_add(T::DbWeight::get().reads(1_u64)) + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `b` is `[1, 5]`. + fn set_prepare_era_for_payout(_b: u32, ) -> Weight { + Weight::from_parts(257_146_553_u64, 0) + .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } + // Storage: `Staking::Ledger` (r:1 w:0) + // Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorToStashKey` (r:0 w:1) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn set_validator_key() -> Weight { + Weight::from_parts(195_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn begin_billing_report() -> Weight { + Weight::from_parts(269_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn begin_charging_customers() -> Weight { + Weight::from_parts(223_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:0) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcCustomers::Buckets` (r:999 w:0) + // Proof: `DdcCustomers::Buckets` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) + // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcCustomers::Ledger` (r:999 w:999) + // Proof: `DdcCustomers::Ledger` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `System::Account` (r:2 w:2) + // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + // Storage: `DdcPayouts::DebtorCustomers` (r:999 w:999) + // Proof: `DdcPayouts::DebtorCustomers` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `b` is `[1, 1000]`. + fn send_charging_customers_batch(b: u32, ) -> Weight { + Weight::from_parts(1_192_000_000_u64, 0) + // Standard Error: 2_345_530 + .saturating_add(Weight::from_parts(782_075_355_u64, 0).saturating_mul(b as u64)) + .saturating_add(T::DbWeight::get().reads(10_u64)) + .saturating_add(T::DbWeight::get().reads((3_u64).saturating_mul(b as u64))) + .saturating_add(T::DbWeight::get().writes(5_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(b as u64))) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) + // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `System::Account` (r:3 w:3) + // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + // Storage: `DdcClusters::Clusters` (r:1 w:0) + // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `Staking::Validators` (r:2 w:0) + // Proof: `Staking::Validators` (`max_values`: None, `max_size`: Some(45), added: 2520, mode: `MaxEncodedLen`) + // Storage: `Staking::Bonded` (r:1 w:0) + // Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`) + // Storage: `Staking::Ledger` (r:1 w:0) + // Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`) + // Storage: `Staking::Nominators` (r:1 w:0) + // Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`) + fn end_charging_customers() -> Weight { + Weight::from_parts(2_169_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(13_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn begin_rewarding_providers() -> Weight { + Weight::from_parts(226_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:0) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcNodes::StorageNodes` (r:999 w:0) + // Proof: `DdcNodes::StorageNodes` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `System::Account` (r:1000 w:1000) + // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + /// The range of component `b` is `[1, 1000]`. + fn send_rewarding_providers_batch(b: u32, ) -> Weight { + Weight::from_parts(908_000_000_u64, 0) + // Standard Error: 334_349 + .saturating_add(Weight::from_parts(622_558_822_u64, 0).saturating_mul(b as u64)) + .saturating_add(T::DbWeight::get().reads(5_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(b as u64))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(b as u64))) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn end_rewarding_providers() -> Weight { + Weight::from_parts(232_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcClusters::Clusters` (r:1 w:1) + // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn end_billing_report() -> Weight { + Weight::from_parts(434_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(5_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// The range of component `b` is `[1, 5]`. + fn emit_consensus_errors(b: u32, ) -> Weight { + Weight::from_parts(100_407_885_u64, 0) + // Standard Error: 26_465 + .saturating_add(Weight::from_parts(23_011_156_u64, 0).saturating_mul(b as u64)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + } } // For backwards compatibility and tests impl WeightInfo for () { - // Storage: `DdcVerification::ActiveBillingReports` (r:1 w:1) - // Proof: `DdcVerification::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) - fn create_billing_reports() -> Weight { - Weight::from_parts(11_000_000_u64, 0) - .saturating_add(RocksDbWeight::get().reads(1_u64)) + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `b` is `[1, 5]`. + fn set_prepare_era_for_payout(_b: u32, ) -> Weight { + Weight::from_parts(257_146_553_u64, 0) + .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } + // Storage: `Staking::Ledger` (r:1 w:0) + // Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorToStashKey` (r:0 w:1) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn set_validator_key() -> Weight { + Weight::from_parts(195_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn begin_billing_report() -> Weight { + Weight::from_parts(269_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn begin_charging_customers() -> Weight { + Weight::from_parts(223_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:0) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcCustomers::Buckets` (r:999 w:0) + // Proof: `DdcCustomers::Buckets` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) + // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcCustomers::Ledger` (r:999 w:999) + // Proof: `DdcCustomers::Ledger` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `System::Account` (r:2 w:2) + // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + // Storage: `DdcPayouts::DebtorCustomers` (r:999 w:999) + // Proof: `DdcPayouts::DebtorCustomers` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `b` is `[1, 1000]`. + fn send_charging_customers_batch(b: u32, ) -> Weight { + Weight::from_parts(1_192_000_000_u64, 0) + // Standard Error: 2_345_530 + .saturating_add(Weight::from_parts(782_075_355_u64, 0).saturating_mul(b as u64)) + .saturating_add(RocksDbWeight::get().reads(10_u64)) + .saturating_add(RocksDbWeight::get().reads((3_u64).saturating_mul(b as u64))) + .saturating_add(RocksDbWeight::get().writes(5_u64)) + .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(b as u64))) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcClusters::ClustersGovParams` (r:1 w:0) + // Proof: `DdcClusters::ClustersGovParams` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `System::Account` (r:3 w:3) + // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + // Storage: `DdcClusters::Clusters` (r:1 w:0) + // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `Staking::Validators` (r:2 w:0) + // Proof: `Staking::Validators` (`max_values`: None, `max_size`: Some(45), added: 2520, mode: `MaxEncodedLen`) + // Storage: `Staking::Bonded` (r:1 w:0) + // Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`) + // Storage: `Staking::Ledger` (r:1 w:0) + // Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`) + // Storage: `Staking::Nominators` (r:1 w:0) + // Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`) + fn end_charging_customers() -> Weight { + Weight::from_parts(2_169_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(13_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn begin_rewarding_providers() -> Weight { + Weight::from_parts(226_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:0) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcNodes::StorageNodes` (r:999 w:0) + // Proof: `DdcNodes::StorageNodes` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `System::Account` (r:1000 w:1000) + // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + /// The range of component `b` is `[1, 1000]`. + fn send_rewarding_providers_batch(b: u32, ) -> Weight { + Weight::from_parts(908_000_000_u64, 0) + // Standard Error: 334_349 + .saturating_add(Weight::from_parts(622_558_822_u64, 0).saturating_mul(b as u64)) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(b as u64))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(b as u64))) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn end_rewarding_providers() -> Weight { + Weight::from_parts(232_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) + // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcClusters::Clusters` (r:1 w:1) + // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn end_billing_report() -> Weight { + Weight::from_parts(434_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + // Storage: `DdcVerification::ValidatorToStashKey` (r:1 w:0) + // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// The range of component `b` is `[1, 5]`. + fn emit_consensus_errors(b: u32, ) -> Weight { + Weight::from_parts(100_407_885_u64, 0) + // Standard Error: 26_465 + .saturating_add(Weight::from_parts(23_011_156_u64, 0).saturating_mul(b as u64)) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index b4e9a9b5a..68c1b0a25 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -32,6 +32,7 @@ pub type ClusterNodesCount = u16; pub type StorageNodePubKey = AccountId32; pub type ActivityHash = [u8; 32]; pub type BatchIndex = u16; +pub const AVG_SECONDS_MONTH: i64 = 2630016; // 30.44 * 24.0 * 3600.0; pub struct MergeActivityHash; impl Merge for MergeActivityHash { @@ -277,6 +278,15 @@ pub struct CustomerUsage { pub number_of_gets: u64, } +/// Stores charge in tokens(units) of customer as per CustomerUsage +#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] +pub struct CustomerCharge { + pub transfer: u128, // charge in tokens for CustomerUsage::transferred_bytes + pub storage: u128, // charge in tokens for CustomerUsage::stored_bytes + pub puts: u128, // charge in tokens for CustomerUsage::number_of_puts + pub gets: u128, // charge in tokens for CustomerUsage::number_of_gets +} + /// Stores usage of node provider #[derive( PartialEq, Eq, Encode, Decode, Debug, TypeInfo, Default, Clone, Serialize, Deserialize, @@ -288,6 +298,15 @@ pub struct NodeUsage { pub number_of_gets: u64, } +/// Stores reward in tokens(units) of node provider as per NodeUsage +#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] +pub struct NodeReward { + pub transfer: u128, // reward in tokens for NodeUsage::transferred_bytes + pub storage: u128, // reward in tokens for NodeUsage::stored_bytes + pub puts: u128, // reward in tokens for NodeUsage::number_of_puts + pub gets: u128, // reward in tokens for NodeUsage::number_of_gets +} + #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Default)] pub struct MMRProof { pub proof: Vec, @@ -389,3 +408,18 @@ pub struct EraValidation { pub payees_merkle_root_hash: ActivityHash, pub status: EraValidationStatus, } + +pub struct BillingReportParams { + pub cluster_id: ClusterId, + pub era: DdcEra, + pub start_era: i64, + pub end_era: i64, + pub state: PayoutState, + pub total_customer_charge: CustomerCharge, + pub total_distributed_reward: u128, + pub total_node_usage: NodeUsage, + pub charging_max_batch_index: BatchIndex, + pub charging_processed_batches: Vec, + pub rewarding_max_batch_index: BatchIndex, + pub rewarding_processed_batches: Vec, +} diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index 666ab2396..14a382eb3 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -1,26 +1,29 @@ use sp_runtime::DispatchResult; +#[cfg(feature = "runtime-benchmarks")] +use crate::BillingReportParams; use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, PayoutError, PayoutState, }; + pub trait PayoutProcessor { fn begin_billing_report( cluster_id: ClusterId, - era: DdcEra, + era_id: DdcEra, start_era: i64, end_era: i64, ) -> DispatchResult; fn begin_charging_customers( cluster_id: ClusterId, - era: DdcEra, + era_id: DdcEra, max_batch_index: BatchIndex, ) -> DispatchResult; fn send_charging_customers_batch( cluster_id: ClusterId, - era: DdcEra, + era_id: DdcEra, batch_index: BatchIndex, payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: MMRProof, @@ -30,24 +33,24 @@ pub trait PayoutProcessor { fn begin_rewarding_providers( cluster_id: ClusterId, - era: DdcEra, + era_id: DdcEra, max_batch_index: BatchIndex, total_node_usage: NodeUsage, ) -> DispatchResult; fn send_rewarding_providers_batch( cluster_id: ClusterId, - era: DdcEra, + era_id: DdcEra, batch_index: BatchIndex, payees: &[(NodePubKey, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult; - fn end_rewarding_providers(cluster_id: ClusterId, era: DdcEra) -> DispatchResult; + fn end_rewarding_providers(cluster_id: ClusterId, era_id: DdcEra) -> DispatchResult; - fn end_billing_report(cluster_id: ClusterId, era: DdcEra) -> DispatchResult; + fn end_billing_report(cluster_id: ClusterId, era_id: DdcEra) -> DispatchResult; - fn get_billing_report_status(cluster_id: &ClusterId, era: DdcEra) -> PayoutState; + fn get_billing_report_status(cluster_id: &ClusterId, era_id: DdcEra) -> PayoutState; fn all_customer_batches_processed(cluster_id: &ClusterId, era_id: DdcEra) -> bool; @@ -62,4 +65,7 @@ pub trait PayoutProcessor { cluster_id: &ClusterId, era_id: DdcEra, ) -> Result, PayoutError>; + + #[cfg(feature = "runtime-benchmarks")] + fn create_billing_report(vault: T::AccountId, params: BillingReportParams); } diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index dd5b14749..020c86b48 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -1,22 +1,10 @@ use frame_system::Config; -#[cfg(feature = "runtime-benchmarks")] -use scale_info::prelude::vec::Vec; -#[cfg(feature = "runtime-benchmarks")] -use crate::EraValidation; use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, }; pub trait ValidatorVisitor { - #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(validators_with_keys: Vec<(T::AccountId, T::AccountId)>); - #[cfg(feature = "runtime-benchmarks")] - fn setup_validation_era( - cluster_id: ClusterId, - era_id: DdcEra, - era_validation: EraValidation, - ); fn is_ocw_validator(caller: T::AccountId) -> bool; fn is_customers_batch_valid( cluster_id: ClusterId, diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 616dc650f..bb96fae44 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1229,7 +1229,6 @@ impl pallet_ddc_payouts::Config for Runtime { type TreasuryVisitor = TreasuryWrapper; type NominatorsAndValidatorsList = pallet_staking::UseNominatorsAndValidatorsMap; type ClusterCreator = DdcClusters; - type WeightInfo = pallet_ddc_payouts::weights::SubstrateWeight; type VoteScoreToU64 = IdentityConvert; // used for UseNominatorsAndValidatorsMap type ValidatorVisitor = pallet_ddc_verification::Pallet; type NodeManager = pallet_ddc_nodes::Pallet; @@ -1331,6 +1330,12 @@ impl pallet_ddc_verification::Config for Runtime { type CustomerVisitor = pallet_ddc_customers::Pallet; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 3; type Currency = Balances; + #[cfg(feature = "runtime-benchmarks")] + type CustomerDepositor = DdcCustomers; + #[cfg(feature = "runtime-benchmarks")] + type ClusterCreator = DdcClusters; + #[cfg(feature = "runtime-benchmarks")] + type BucketManager = DdcCustomers; } construct_runtime!( diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index afee849a9..a13697bb7 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1231,7 +1231,6 @@ impl pallet_ddc_payouts::Config for Runtime { type TreasuryVisitor = TreasuryWrapper; type NominatorsAndValidatorsList = pallet_staking::UseNominatorsAndValidatorsMap; type ClusterCreator = DdcClusters; - type WeightInfo = pallet_ddc_payouts::weights::SubstrateWeight; type VoteScoreToU64 = IdentityConvert; type ValidatorVisitor = pallet_ddc_verification::Pallet; type NodeManager = pallet_ddc_nodes::Pallet; @@ -1351,6 +1350,12 @@ impl pallet_ddc_verification::Config for Runtime { type CustomerVisitor = pallet_ddc_customers::Pallet; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 3; type Currency = Balances; + #[cfg(feature = "runtime-benchmarks")] + type CustomerDepositor = DdcCustomers; + #[cfg(feature = "runtime-benchmarks")] + type ClusterCreator = DdcClusters; + #[cfg(feature = "runtime-benchmarks")] + type BucketManager = DdcCustomers; } construct_runtime!( From 2bdeb4c782a0ceb71afd3aef4b86a133c51a1846 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 13:13:23 +0100 Subject: [PATCH 08/15] fix: cleaning up and fixing fmt and clippy issues --- .vscode/settings.json | 2 +- pallets/ddc-payouts/src/benchmarking.rs | 17 - pallets/ddc-payouts/src/lib.rs | 35 +- pallets/ddc-payouts/src/mock.rs | 42 +-- pallets/ddc-verification/src/benchmarking.rs | 12 + pallets/ddc-verification/src/lib.rs | 349 +++++++++---------- pallets/ddc-verification/src/mock.rs | 80 +++++ pallets/ddc-verification/src/weights.rs | 19 + runtime/cere-dev/src/lib.rs | 3 - runtime/cere/src/lib.rs | 3 - 10 files changed, 301 insertions(+), 261 deletions(-) delete mode 100644 pallets/ddc-payouts/src/benchmarking.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 7089a4652..0196fd738 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", - "editor.formatOnSave": false + "editor.formatOnSave": true } } \ No newline at end of file diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs deleted file mode 100644 index 387652cee..000000000 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ /dev/null @@ -1,17 +0,0 @@ -//! DdcPayouts pallet benchmarking. - -pub use frame_benchmarking::{account, benchmarks, whitelist_account}; -use frame_system::RawOrigin; -use sp_std::prelude::*; - -use super::*; -use crate::Pallet as DdcPayouts; - -benchmarks! { - dummy_call { - let user = account::("name", 0, 0); - whitelist_account!(user); - }: _(RawOrigin::Signed(user), 100u128) - verify { - } -} diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index ef5ea7232..4adb919a5 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -23,20 +23,11 @@ pub mod migrations; #[cfg(feature = "runtime-benchmarks")] use ddc_primitives::BillingReportParams; - -#[cfg(feature = "runtime-benchmarks")] -pub mod benchmarking; - use ddc_primitives::{ traits::{ - bucket::BucketManager, - cluster::{ClusterCreator as ClusterCreatorType, ClusterProtocol as ClusterProtocolType}, - customer::{ - CustomerCharger as CustomerChargerType, CustomerDepositor as CustomerDepositorType, - }, - node::NodeManager, - pallet::PalletVisitor as PalletVisitorType, - payout::PayoutProcessor, + bucket::BucketManager, cluster::ClusterProtocol as ClusterProtocolType, + customer::CustomerCharger as CustomerChargerType, node::NodeManager, + pallet::PalletVisitor as PalletVisitorType, payout::PayoutProcessor, }, BatchIndex, BucketId, ClusterId, CustomerCharge, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeReward, NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, @@ -106,11 +97,9 @@ pub mod pallet { type CustomerCharger: CustomerChargerType; type BucketManager: BucketManager; type NodeManager: NodeManager; - type CustomerDepositor: CustomerDepositorType; type TreasuryVisitor: PalletVisitorType; type ClusterProtocol: ClusterProtocolType>; type NominatorsAndValidatorsList: SortedListProvider; - type ClusterCreator: ClusterCreatorType>; type VoteScoreToU64: Convert, u64>; type ValidatorVisitor: ValidatorVisitor; type AccountIdConverter: From + Into; @@ -318,13 +307,7 @@ pub mod pallet { } #[pallet::call] - impl Pallet { - #[pallet::call_index(0)] - #[pallet::weight(T::DbWeight::get().reads_writes(2, 5))] // todo! implement weights - pub fn dummy_call(origin: OriginFor, num: u128) -> DispatchResult { - Ok(()) - } - } + impl Pallet {} fn charge_treasury_fees( treasury_fee: u128, @@ -658,7 +641,7 @@ pub mod pallet { era, batch_index, billing_report.charging_max_batch_index, - &payers, + payers, &batch_proof, ), Error::::BatchValidationFailed @@ -671,7 +654,7 @@ pub mod pallet { let mut customer_charge = get_customer_charge::( &cluster_id, - &customer_usage, + customer_usage, bucket_id, &customer_id, updated_billing_report.start_era, @@ -691,7 +674,7 @@ pub mod pallet { bucket_id, customer_id.clone(), updated_billing_report.vault.clone(), - &customer_usage, + customer_usage, total_customer_charge, ) { Ok(actually_charged) => actually_charged, @@ -950,7 +933,7 @@ pub mod pallet { era, batch_index, billing_report.rewarding_max_batch_index, - &payees, + payees, &batch_proof ), Error::::BatchValidationFailed @@ -960,7 +943,7 @@ pub mod pallet { let mut updated_billing_report = billing_report.clone(); for (node_key, delta_node_usage) in payees { // todo! get T::NodeVisitor::get_total_usage(delta_node_usage.node_id).stored_bytes - let node_provider_id = T::NodeManager::get_node_provider_id(&node_key)?; + let node_provider_id = T::NodeManager::get_node_provider_id(node_key)?; let mut total_node_stored_bytes: i64 = 0; total_node_stored_bytes = total_node_stored_bytes diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 583a1a63a..6a5d3a753 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -2,20 +2,16 @@ #![allow(dead_code)] +#[cfg(feature = "try-runtime")] +use ddc_primitives::BucketParams; use ddc_primitives::{ traits::{ - bucket::BucketManager, - cluster::{ClusterCreator, ClusterProtocol}, - customer::{CustomerCharger, CustomerDepositor}, - node::NodeManager, - pallet::PalletVisitor, - ClusterQuery, ValidatorVisitor, + bucket::BucketManager, cluster::ClusterProtocol, customer::CustomerCharger, + node::NodeManager, pallet::PalletVisitor, ClusterQuery, ValidatorVisitor, }, - ClusterBondingParams, ClusterFeesParams, ClusterParams, ClusterPricingParams, - ClusterProtocolParams, ClusterStatus, NodeParams, NodePubKey, NodeType, DOLLARS, + ClusterBondingParams, ClusterFeesParams, ClusterPricingParams, ClusterProtocolParams, + ClusterStatus, NodeParams, NodePubKey, NodeType, DOLLARS, }; -#[cfg(feature = "try-runtime")] -use ddc_primitives::{BucketParams, EraValidation}; use frame_election_provider_support::SortedListProvider; use frame_support::{ construct_runtime, parameter_types, @@ -131,13 +127,10 @@ impl crate::pallet::Config for Test { type Currency = Balances; type CustomerCharger = TestCustomerCharger; type BucketManager = TestBucketManager; - type CustomerDepositor = TestCustomerDepositor; type ClusterProtocol = TestClusterProtocol; type TreasuryVisitor = TestTreasuryVisitor; type NominatorsAndValidatorsList = TestValidatorVisitor; - type ClusterCreator = TestClusterCreator; type VoteScoreToU64 = Identity; - type WeightInfo = (); type ValidatorVisitor = MockValidatorVisitor; type NodeManager = MockNodeManager; type AccountIdConverter = AccountId; @@ -338,29 +331,6 @@ where } } -pub struct TestClusterCreator; -impl ClusterCreator for TestClusterCreator { - fn create_cluster( - _cluster_id: ClusterId, - _cluster_manager_id: T::AccountId, - _cluster_reserve_id: T::AccountId, - _cluster_params: ClusterParams, - _cluster_protocol_params: ClusterProtocolParams>, - ) -> DispatchResult { - Ok(()) - } -} - -pub struct TestCustomerDepositor; -impl CustomerDepositor for TestCustomerDepositor { - fn deposit(_customer: T::AccountId, _amount: u128) -> Result<(), DispatchError> { - Ok(()) - } - fn deposit_extra(_customer: T::AccountId, _amount: u128) -> Result<(), DispatchError> { - Ok(()) - } -} - pub const RESERVE_ACCOUNT_ID: [u8; 32] = [9; 32]; pub const TREASURY_ACCOUNT_ID: [u8; 32] = [8; 32]; pub const VALIDATOR1_ACCOUNT_ID: [u8; 32] = [111; 32]; diff --git a/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index c098df78e..5083ee6c5 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -131,6 +131,7 @@ mod benchmarks { >::insert(cluster_id, era_id, era_validation); } + #[allow(clippy::too_many_arguments)] fn create_billing_report( cluster_id: ClusterId, era_id: DdcEra, @@ -835,5 +836,16 @@ mod benchmarks { emit_consensus_errors(RawOrigin::Signed(validator.clone()), errros); } + #[benchmark] + fn set_era_validations() { + let cluster_id = ClusterId::from([1; 20]); + let era_id: DdcEra = 1; + + #[extrinsic_call] + set_era_validations(RawOrigin::Root, cluster_id, era_id); + + >::contains_key(cluster_id, era_id); + } + impl_benchmark_test_suite!(DdcVerification, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 04bcf52a3..ae57f1e2b 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -3510,6 +3510,178 @@ pub mod pallet { Ok(()) } + /// Set validator key. + /// + /// The origin must be a validator. + /// + /// Parameters: + /// - `ddc_validator_pub`: validator Key + #[pallet::call_index(1)] + #[pallet::weight(::WeightInfo::set_validator_key())] + pub fn set_validator_key( + origin: OriginFor, + ddc_validator_pub: T::AccountId, + ) -> DispatchResult { + let controller = ensure_signed(origin)?; + + let stash = T::ValidatorStaking::stash_by_ctrl(&controller) + .map_err(|_| Error::::NotController)?; + + ensure!( + >::get().contains(&ddc_validator_pub), + Error::::NotValidatorStash + ); + + ValidatorToStashKey::::insert(&ddc_validator_pub, &stash); + Self::deposit_event(Event::::ValidatorKeySet { validator: ddc_validator_pub }); + Ok(()) + } + + #[pallet::call_index(2)] + #[pallet::weight(::WeightInfo::begin_billing_report())] + pub fn begin_billing_report( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + start_era: i64, + end_era: i64, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + + T::PayoutProcessor::begin_billing_report(cluster_id, era_id, start_era, end_era)?; + + EraValidations::::try_mutate( + cluster_id, + era_id, + |maybe_era_validations| -> DispatchResult { + maybe_era_validations.as_mut().ok_or(Error::::NoEraValidation)?.status = + EraValidationStatus::PayoutInProgress; + Ok(()) + }, + )?; + + Ok(()) + } + + #[pallet::call_index(3)] + #[pallet::weight(::WeightInfo::begin_charging_customers())] + pub fn begin_charging_customers( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + max_batch_index: BatchIndex, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::begin_charging_customers(cluster_id, era_id, max_batch_index) + } + + #[pallet::call_index(4)] + #[pallet::weight(::WeightInfo::send_charging_customers_batch(payers.len() as u32))] + pub fn send_charging_customers_batch( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + batch_index: BatchIndex, + payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, + batch_proof: MMRProof, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::send_charging_customers_batch( + cluster_id, + era_id, + batch_index, + &payers, + batch_proof, + ) + } + + #[pallet::call_index(5)] + #[pallet::weight(::WeightInfo::end_charging_customers())] + pub fn end_charging_customers( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::end_charging_customers(cluster_id, era_id) + } + + #[pallet::call_index(6)] + #[pallet::weight(::WeightInfo::begin_rewarding_providers())] + pub fn begin_rewarding_providers( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + max_batch_index: BatchIndex, + total_node_usage: NodeUsage, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::begin_rewarding_providers( + cluster_id, + era_id, + max_batch_index, + total_node_usage, + ) + } + + #[pallet::call_index(7)] + #[pallet::weight(::WeightInfo::send_rewarding_providers_batch(payees.len() as u32))] + pub fn send_rewarding_providers_batch( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + batch_index: BatchIndex, + payees: Vec<(NodePubKey, NodeUsage)>, + batch_proof: MMRProof, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::send_rewarding_providers_batch( + cluster_id, + era_id, + batch_index, + &payees, + batch_proof, + ) + } + + #[pallet::call_index(8)] + #[pallet::weight(::WeightInfo::end_rewarding_providers())] + pub fn end_rewarding_providers( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::end_rewarding_providers(cluster_id, era_id) + } + + #[pallet::call_index(9)] + #[pallet::weight(::WeightInfo::end_billing_report())] + pub fn end_billing_report( + origin: OriginFor, + cluster_id: ClusterId, + era_id: DdcEra, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + T::PayoutProcessor::end_billing_report(cluster_id, era_id)?; + + let mut era_validation = >::get(cluster_id, era_id).unwrap(); // should exist + era_validation.status = EraValidationStatus::PayoutSuccess; + >::insert(cluster_id, era_id, era_validation); + + // todo(yahortsaryk): this should be renamed to `last_paid_era` to eliminate ambiguity, + // as the validation step is decoupled from payout step. + T::ClusterValidator::set_last_validated_era(&cluster_id, era_id) + } + /// Emit consensus errors. /// /// The origin must be a validator. @@ -3519,7 +3691,7 @@ pub mod pallet { /// /// Emits `NotEnoughNodesForConsensus` OR `ActivityNotInConsensus` event depend of error /// type, when successful. - #[pallet::call_index(1)] + #[pallet::call_index(10)] #[pallet::weight(::WeightInfo::emit_consensus_errors(errors.len() as u32))] pub fn emit_consensus_errors( origin: OriginFor, @@ -3764,181 +3936,8 @@ pub mod pallet { Ok(()) } - /// Set validator key. - /// - /// The origin must be a validator. - /// - /// Parameters: - /// - `ddc_validator_pub`: validator Key - #[pallet::call_index(2)] - #[pallet::weight(::WeightInfo::set_validator_key())] - pub fn set_validator_key( - origin: OriginFor, - ddc_validator_pub: T::AccountId, - ) -> DispatchResult { - let controller = ensure_signed(origin)?; - - let stash = T::ValidatorStaking::stash_by_ctrl(&controller) - .map_err(|_| Error::::NotController)?; - - ensure!( - >::get().contains(&ddc_validator_pub), - Error::::NotValidatorStash - ); - - ValidatorToStashKey::::insert(&ddc_validator_pub, &stash); - Self::deposit_event(Event::::ValidatorKeySet { validator: ddc_validator_pub }); - Ok(()) - } - - #[pallet::call_index(3)] - #[pallet::weight(::WeightInfo::begin_billing_report())] - pub fn begin_billing_report( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - start_era: i64, - end_era: i64, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - - T::PayoutProcessor::begin_billing_report(cluster_id, era_id, start_era, end_era)?; - - EraValidations::::try_mutate( - cluster_id, - era_id, - |maybe_era_validations| -> DispatchResult { - maybe_era_validations.as_mut().ok_or(Error::::NoEraValidation)?.status = - EraValidationStatus::PayoutInProgress; - Ok(()) - }, - )?; - - Ok(()) - } - - #[pallet::call_index(4)] - #[pallet::weight(::WeightInfo::begin_charging_customers())] - pub fn begin_charging_customers( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - max_batch_index: BatchIndex, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::begin_charging_customers(cluster_id, era_id, max_batch_index) - } - - #[pallet::call_index(5)] - #[pallet::weight(::WeightInfo::send_charging_customers_batch(payers.len() as u32))] - pub fn send_charging_customers_batch( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - batch_index: BatchIndex, - payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, - batch_proof: MMRProof, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::send_charging_customers_batch( - cluster_id, - era_id, - batch_index, - &payers, - batch_proof, - ) - } - - #[pallet::call_index(6)] - #[pallet::weight(::WeightInfo::end_charging_customers())] - pub fn end_charging_customers( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::end_charging_customers(cluster_id, era_id) - } - - #[pallet::call_index(7)] - #[pallet::weight(::WeightInfo::begin_rewarding_providers())] - pub fn begin_rewarding_providers( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - max_batch_index: BatchIndex, - total_node_usage: NodeUsage, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::begin_rewarding_providers( - cluster_id, - era_id, - max_batch_index, - total_node_usage, - ) - } - - #[pallet::call_index(8)] - #[pallet::weight(::WeightInfo::send_rewarding_providers_batch(payees.len() as u32))] - pub fn send_rewarding_providers_batch( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - batch_index: BatchIndex, - payees: Vec<(NodePubKey, NodeUsage)>, - batch_proof: MMRProof, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::send_rewarding_providers_batch( - cluster_id, - era_id, - batch_index, - &payees, - batch_proof, - ) - } - - #[pallet::call_index(9)] - #[pallet::weight(::WeightInfo::end_rewarding_providers())] - pub fn end_rewarding_providers( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::end_rewarding_providers(cluster_id, era_id) - } - - #[pallet::call_index(10)] - #[pallet::weight(::WeightInfo::end_billing_report())] - pub fn end_billing_report( - origin: OriginFor, - cluster_id: ClusterId, - era_id: DdcEra, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); - T::PayoutProcessor::end_billing_report(cluster_id, era_id)?; - - let mut era_validation = >::get(cluster_id, era_id).unwrap(); // should exist - era_validation.status = EraValidationStatus::PayoutSuccess; - >::insert(cluster_id, era_id, era_validation); - - // todo(yahortsaryk): this should be renamed to `last_paid_era` to eliminate ambiguity, - // as the validation step is decoupled from payout step. - T::ClusterValidator::set_last_validated_era(&cluster_id, era_id) - } - - // todo! remove this after devnet testing #[pallet::call_index(11)] - #[pallet::weight(1_000_000)] + #[pallet::weight(::WeightInfo::set_era_validations())] pub fn set_era_validations( origin: OriginFor, cluster_id: ClusterId, diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index f542ff715..c768e3668 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::traits::{BucketManager, ClusterCreator, CustomerDepositor}; use ddc_primitives::{ crypto, sr25519, traits::{ClusterManager, ClusterQuery}, @@ -5,6 +7,10 @@ use ddc_primitives::{ ClusterStatus, PayoutError, PayoutState, StorageNodeMode, StorageNodePubKey, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, }; +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::{ + BillingReportParams, BucketParams, ClusterId, ClusterParams, ClusterProtocolParams, +}; use frame_election_provider_support::{ bounds::{ElectionBounds, ElectionBoundsBuilder}, onchain, SequentialPhragmen, @@ -239,6 +245,12 @@ impl crate::Config for Test { type CustomerVisitor = MockCustomerVisitor; const MAX_MERKLE_NODE_IDENTIFIER: u16 = 4; type Currency = Balances; + #[cfg(feature = "runtime-benchmarks")] + type CustomerDepositor = MockCustomerDepositor; + #[cfg(feature = "runtime-benchmarks")] + type ClusterCreator = MockClusterCreator; + #[cfg(feature = "runtime-benchmarks")] + type BucketManager = MockBucketManager; } pub struct MockCustomerVisitor; @@ -251,6 +263,69 @@ impl CustomerVisitor for MockCustomerVisitor { } } +#[cfg(feature = "runtime-benchmarks")] +pub struct MockCustomerDepositor; +#[cfg(feature = "runtime-benchmarks")] +impl CustomerDepositor for MockCustomerDepositor { + fn deposit(_customer: T::AccountId, _amount: u128) -> Result<(), DispatchError> { + unimplemented!() + } + fn deposit_extra(_customer: T::AccountId, _amount: u128) -> Result<(), DispatchError> { + unimplemented!() + } +} + +#[cfg(feature = "runtime-benchmarks")] +pub struct MockClusterCreator; +#[cfg(feature = "runtime-benchmarks")] +impl ClusterCreator for MockClusterCreator { + fn create_cluster( + _cluster_id: ClusterId, + _cluster_manager_id: T::AccountId, + _cluster_reserve_id: T::AccountId, + _cluster_params: ClusterParams, + _initial_protocol_params: ClusterProtocolParams>, + ) -> DispatchResult { + unimplemented!() + } +} + +#[cfg(feature = "runtime-benchmarks")] +pub struct MockBucketManager; +#[cfg(feature = "runtime-benchmarks")] +impl BucketManager for MockBucketManager { + fn get_bucket_owner_id(_bucket_id: BucketId) -> Result { + unimplemented!() + } + + fn get_total_bucket_usage( + _cluster_id: &ClusterId, + _bucket_id: BucketId, + _content_owner: &T::AccountId, + ) -> Result, DispatchError> { + unimplemented!() + } + + fn inc_total_bucket_usage( + _cluster_id: &ClusterId, + _bucket_id: BucketId, + _content_owner: T::AccountId, + _customer_usage: &CustomerUsage, + ) -> DispatchResult { + unimplemented!() + } + + #[cfg(feature = "runtime-benchmarks")] + fn create_bucket( + _cluster_id: &ClusterId, + _bucket_id: BucketId, + _owner_id: T::AccountId, + _bucket_params: BucketParams, + ) -> Result<(), DispatchError> { + unimplemented!() + } +} + pub(crate) const VALIDATOR_VERIFICATION_PUB_KEY_HEX: &str = "4e7b7f176f8778a2dbef829f50466170634e747ab5c5e64cb131c9c5a01d975f"; pub(crate) const VALIDATOR_VERIFICATION_PRIV_KEY_HEX: &str = @@ -440,6 +515,11 @@ impl PayoutProcessor for MockPayoutProcessor { fn get_billing_report_status(_cluster_id: &ClusterId, _era_id: DdcEra) -> PayoutState { PayoutState::NotInitialized } + + #[cfg(feature = "runtime-benchmarks")] + fn create_billing_report(_vault: T::AccountId, _params: BillingReportParams) { + unimplemented!() + } } pub struct MockNodeManager; diff --git a/pallets/ddc-verification/src/weights.rs b/pallets/ddc-verification/src/weights.rs index 060dbe580..17ea2ba63 100644 --- a/pallets/ddc-verification/src/weights.rs +++ b/pallets/ddc-verification/src/weights.rs @@ -39,6 +39,7 @@ pub trait WeightInfo { fn end_rewarding_providers() -> Weight; fn end_billing_report() -> Weight; fn emit_consensus_errors(b: u32, ) -> Weight; + fn set_era_validations() -> Weight; } /// Weights for pallet_ddc_verification using the Substrate node and recommended hardware. @@ -214,6 +215,15 @@ impl WeightInfo for SubstrateWeight { .saturating_add(Weight::from_parts(23_011_156_u64, 0).saturating_mul(b as u64)) .saturating_add(T::DbWeight::get().reads(2_u64)) } + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn set_era_validations() -> Weight { + Weight::from_parts(180_000_000_u64, 0) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } } // For backwards compatibility and tests @@ -388,4 +398,13 @@ impl WeightInfo for () { .saturating_add(Weight::from_parts(23_011_156_u64, 0).saturating_mul(b as u64)) .saturating_add(RocksDbWeight::get().reads(2_u64)) } + // Storage: `DdcVerification::EraValidations` (r:1 w:1) + // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) + // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) + // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn set_era_validations() -> Weight { + Weight::from_parts(180_000_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } } diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index bb96fae44..7e065f80b 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1224,11 +1224,9 @@ impl pallet_ddc_payouts::Config for Runtime { type Currency = Balances; type CustomerCharger = DdcCustomers; type BucketManager = DdcCustomers; - type CustomerDepositor = DdcCustomers; type ClusterProtocol = DdcClusters; type TreasuryVisitor = TreasuryWrapper; type NominatorsAndValidatorsList = pallet_staking::UseNominatorsAndValidatorsMap; - type ClusterCreator = DdcClusters; type VoteScoreToU64 = IdentityConvert; // used for UseNominatorsAndValidatorsMap type ValidatorVisitor = pallet_ddc_verification::Pallet; type NodeManager = pallet_ddc_nodes::Pallet; @@ -1486,7 +1484,6 @@ mod benches { [pallet_ddc_clusters, DdcClusters] [pallet_ddc_staking, DdcStaking] [pallet_ddc_nodes, DdcNodes] - [pallet_ddc_payouts, DdcPayouts] [frame_system, SystemBench::] [pallet_timestamp, Timestamp] [pallet_treasury, Treasury] diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index a13697bb7..03f337e7b 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1226,11 +1226,9 @@ impl pallet_ddc_payouts::Config for Runtime { type Currency = Balances; type CustomerCharger = DdcCustomers; type BucketManager = DdcCustomers; - type CustomerDepositor = DdcCustomers; type ClusterProtocol = DdcClusters; type TreasuryVisitor = TreasuryWrapper; type NominatorsAndValidatorsList = pallet_staking::UseNominatorsAndValidatorsMap; - type ClusterCreator = DdcClusters; type VoteScoreToU64 = IdentityConvert; type ValidatorVisitor = pallet_ddc_verification::Pallet; type NodeManager = pallet_ddc_nodes::Pallet; @@ -1515,7 +1513,6 @@ mod benches { [pallet_ddc_clusters, DdcClusters] [pallet_ddc_staking, DdcStaking] [pallet_ddc_nodes, DdcNodes] - [pallet_ddc_payouts, DdcPayouts] [pallet_election_provider_multi_phase, ElectionProviderMultiPhase] [pallet_election_provider_support_benchmarking, EPSBench::] [pallet_fast_unstake, FastUnstake] From 342b5f2711567d5d4b1415f33996213e5e961fdc Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 13:43:37 +0100 Subject: [PATCH 09/15] chore: methods for last paid removed to eliminate ambiguity --- pallets/ddc-clusters/src/cluster.rs | 2 ++ pallets/ddc-clusters/src/lib.rs | 14 +++-------- pallets/ddc-clusters/src/tests.rs | 6 ++--- pallets/ddc-customers/src/lib.rs | 4 +-- pallets/ddc-payouts/src/lib.rs | 2 +- pallets/ddc-verification/src/lib.rs | 33 ++++++++++++------------- pallets/ddc-verification/src/mock.rs | 7 ++---- pallets/ddc-verification/src/tests.rs | 35 +++++++++++++++------------ primitives/src/traits/cluster.rs | 6 ++--- 9 files changed, 52 insertions(+), 57 deletions(-) diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index bc3fe15d5..9ce44edd8 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -15,6 +15,8 @@ pub struct Cluster { pub reserve_id: AccountId, pub props: ClusterProps, pub status: ClusterStatus, + // todo(yahortsaryk): this should be renamed to `last_paid_era` to eliminate ambiguity, + // as the validation step is decoupled from payout step. pub last_validated_era_id: DdcEra, } diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index c96f4e2d5..75a8bbd99 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -106,7 +106,7 @@ pub mod pallet { ClusterUnbonding { cluster_id: ClusterId }, ClusterUnbonded { cluster_id: ClusterId }, ClusterNodeValidated { cluster_id: ClusterId, node_pub_key: NodePubKey, succeeded: bool }, - ClusterEraValidated { cluster_id: ClusterId, era_id: DdcEra }, + ClusterEraPaid { cluster_id: ClusterId, era_id: DdcEra }, } #[pallet::error] @@ -890,24 +890,18 @@ pub mod pallet { } } impl ClusterValidator for Pallet { - fn set_last_validated_era( - cluster_id: &ClusterId, - era_id: DdcEra, - ) -> Result<(), DispatchError> { + fn set_last_paid_era(cluster_id: &ClusterId, era_id: DdcEra) -> Result<(), DispatchError> { let mut cluster = Clusters::::try_get(cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; cluster.last_validated_era_id = era_id; Clusters::::insert(cluster_id, cluster); - Self::deposit_event(Event::::ClusterEraValidated { - cluster_id: *cluster_id, - era_id, - }); + Self::deposit_event(Event::::ClusterEraPaid { cluster_id: *cluster_id, era_id }); Ok(()) } - fn get_last_validated_era(cluster_id: &ClusterId) -> Result { + fn get_last_paid_era(cluster_id: &ClusterId) -> Result { let cluster = Clusters::::try_get(cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; diff --git a/pallets/ddc-clusters/src/tests.rs b/pallets/ddc-clusters/src/tests.rs index 56c9d8237..fc9b41e8d 100644 --- a/pallets/ddc-clusters/src/tests.rs +++ b/pallets/ddc-clusters/src/tests.rs @@ -590,7 +590,7 @@ fn set_last_validated_era_works() { // Cluster doesn't exist assert_noop!( - DdcClusters::set_last_validated_era(&cluster_id, era_id), + DdcClusters::set_last_paid_era(&cluster_id, era_id), Error::::ClusterDoesNotExist ); @@ -619,14 +619,14 @@ fn set_last_validated_era_works() { } )); - assert_ok!(DdcClusters::set_last_validated_era(&cluster_id, era_id)); + assert_ok!(DdcClusters::set_last_paid_era(&cluster_id, era_id)); let updated_cluster = DdcClusters::clusters(cluster_id).unwrap(); assert_eq!(updated_cluster.last_validated_era_id, era_id); // Checking that event was emitted assert_eq!(System::events().len(), 3); - System::assert_last_event(Event::ClusterEraValidated { cluster_id, era_id }.into()) + System::assert_last_event(Event::ClusterEraPaid { cluster_id, era_id }.into()) }) } diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index ff7475645..a5a1914e1 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -236,8 +236,8 @@ pub mod pallet { BucketDoesNotExist, /// DDC Cluster with provided id doesn't exist ClusterDoesNotExist, - // unauthorised operation - Unauthorised, + // unauthorized operation + Unauthorized, // Arithmetic overflow ArithmeticOverflow, // Arithmetic underflow diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 4adb919a5..2bb5edc78 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -215,7 +215,7 @@ pub mod pallet { pub enum Error { BillingReportDoesNotExist, NotExpectedState, - Unauthorised, + Unauthorized, BatchIndexAlreadyProcessed, BatchIndexIsOutOfRange, BatchesMissed, diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index ae57f1e2b..b7ffca894 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -474,7 +474,7 @@ pub mod pallet { /// Bad requests. BadRequest, /// Not a validator. - Unauthorised, + Unauthorized, /// Already signed era. AlreadySignedEra, NotExpectedState, @@ -1880,7 +1880,6 @@ pub mod pallet { cluster_id: &ClusterId, ) -> Result, OCWError> { Ok(Self::get_era_for_payout(cluster_id, EraValidationStatus::ReadyForPayout)) - // todo! get start and end values based on result } pub(crate) fn prepare_begin_charging_customers( @@ -2737,7 +2736,7 @@ pub mod pallet { /// - `Ok(None)`: The validator did not participate in any era for the given cluster. /// - `Err(OCWError)`: An error occurred while retrieving the data. // todo! add tests for start and end era - pub(crate) fn get_last_validated_era( + pub(crate) fn get_last_paid_era( cluster_id: &ClusterId, validator: T::AccountId, ) -> Result, OCWError> { @@ -2783,11 +2782,11 @@ pub mod pallet { let this_validator = Self::fetch_verification_account_id()?; let last_validated_era_by_this_validator = - Self::get_last_validated_era(cluster_id, this_validator)? + Self::get_last_paid_era(cluster_id, this_validator)? .unwrap_or_else(DdcEra::default); let last_paid_era_for_cluster = - T::ClusterValidator::get_last_validated_era(cluster_id).map_err(|_| { + T::ClusterValidator::get_last_paid_era(cluster_id).map_err(|_| { OCWError::EraRetrievalError { cluster_id: *cluster_id, node_pub_key: None } })?; @@ -3425,7 +3424,7 @@ pub mod pallet { ) -> DispatchResult { let caller = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(caller.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(caller.clone()), Error::::Unauthorized); let mut era_validation = { let era_validations = >::get(cluster_id, era_activity.id); @@ -3547,7 +3546,7 @@ pub mod pallet { end_era: i64, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::begin_billing_report(cluster_id, era_id, start_era, end_era)?; @@ -3573,7 +3572,7 @@ pub mod pallet { max_batch_index: BatchIndex, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::begin_charging_customers(cluster_id, era_id, max_batch_index) } @@ -3588,7 +3587,7 @@ pub mod pallet { batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::send_charging_customers_batch( cluster_id, era_id, @@ -3606,7 +3605,7 @@ pub mod pallet { era_id: DdcEra, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::end_charging_customers(cluster_id, era_id) } @@ -3620,7 +3619,7 @@ pub mod pallet { total_node_usage: NodeUsage, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::begin_rewarding_providers( cluster_id, era_id, @@ -3640,7 +3639,7 @@ pub mod pallet { batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::send_rewarding_providers_batch( cluster_id, era_id, @@ -3658,7 +3657,7 @@ pub mod pallet { era_id: DdcEra, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::end_rewarding_providers(cluster_id, era_id) } @@ -3670,16 +3669,14 @@ pub mod pallet { era_id: DdcEra, ) -> DispatchResult { let sender = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); T::PayoutProcessor::end_billing_report(cluster_id, era_id)?; let mut era_validation = >::get(cluster_id, era_id).unwrap(); // should exist era_validation.status = EraValidationStatus::PayoutSuccess; >::insert(cluster_id, era_id, era_validation); - // todo(yahortsaryk): this should be renamed to `last_paid_era` to eliminate ambiguity, - // as the validation step is decoupled from payout step. - T::ClusterValidator::set_last_validated_era(&cluster_id, era_id) + T::ClusterValidator::set_last_paid_era(&cluster_id, era_id) } /// Emit consensus errors. @@ -3698,7 +3695,7 @@ pub mod pallet { errors: Vec, ) -> DispatchResult { let caller = ensure_signed(origin)?; - ensure!(Self::is_ocw_validator(caller.clone()), Error::::Unauthorised); + ensure!(Self::is_ocw_validator(caller.clone()), Error::::Unauthorized); for error in errors { match error { diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index c768e3668..1df98ad92 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -418,14 +418,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities { pub struct TestClusterValidator; impl ClusterValidator for TestClusterValidator { - fn set_last_validated_era( - _cluster_id: &ClusterId, - _era_id: DdcEra, - ) -> Result<(), DispatchError> { + fn set_last_paid_era(_cluster_id: &ClusterId, _era_id: DdcEra) -> Result<(), DispatchError> { unimplemented!() } - fn get_last_validated_era(_cluster_id: &ClusterId) -> Result { + fn get_last_paid_era(_cluster_id: &ClusterId) -> Result { Ok(Default::default()) } } diff --git a/pallets/ddc-verification/src/tests.rs b/pallets/ddc-verification/src/tests.rs index 9442324cc..a308d76f9 100644 --- a/pallets/ddc-verification/src/tests.rs +++ b/pallets/ddc-verification/src/tests.rs @@ -2355,10 +2355,11 @@ fn test_get_last_validated_era() { let validators = get_validators(); new_test_ext().execute_with(|| { - assert_ok!(Pallet::::get_last_validated_era(&cluster_id1, validators[0].clone()) - .map(|era| { + assert_ok!(Pallet::::get_last_paid_era(&cluster_id1, validators[0].clone()).map( + |era| { assert_eq!(era, None); - })); + } + )); let mut validators_map_1 = BTreeMap::new(); validators_map_1.insert( @@ -2378,16 +2379,18 @@ fn test_get_last_validated_era() { >::insert(cluster_id1, era_1, validation_1); // still no - different accountid - assert_ok!(Pallet::::get_last_validated_era(&cluster_id1, validators[0].clone()) - .map(|era| { + assert_ok!(Pallet::::get_last_paid_era(&cluster_id1, validators[0].clone()).map( + |era| { assert_eq!(era, None); - })); + } + )); // still no - different cluster id - assert_ok!(Pallet::::get_last_validated_era(&cluster_id2, validators[1].clone()) - .map(|era| { + assert_ok!(Pallet::::get_last_paid_era(&cluster_id2, validators[1].clone()).map( + |era| { assert_eq!(era, None); - })); + } + )); let mut validators_map_2 = BTreeMap::new(); validators_map_2 @@ -2405,15 +2408,17 @@ fn test_get_last_validated_era() { >::insert(cluster_id1, era_2, validation_2); // Now the last validated era should be ERA_2 - assert_ok!(Pallet::::get_last_validated_era(&cluster_id1, validators[2].clone()) - .map(|era| { + assert_ok!(Pallet::::get_last_paid_era(&cluster_id1, validators[2].clone()).map( + |era| { assert_eq!(era, Some(era_2)); - })); + } + )); - assert_ok!(Pallet::::get_last_validated_era(&cluster_id1, validators[1].clone()) - .map(|era| { + assert_ok!(Pallet::::get_last_paid_era(&cluster_id1, validators[1].clone()).map( + |era| { assert_eq!(era, Some(era_1)); - })); + } + )); }); } diff --git a/primitives/src/traits/cluster.rs b/primitives/src/traits/cluster.rs index efc95d807..ad7ec9a4a 100644 --- a/primitives/src/traits/cluster.rs +++ b/primitives/src/traits/cluster.rs @@ -113,8 +113,8 @@ pub trait ClusterValidator { /// /// # Events /// - /// Emits `ClusterEraValidated` event if the operation is successful. - fn set_last_validated_era(cluster_id: &ClusterId, era_id: DdcEra) -> Result<(), DispatchError>; + /// Emits `ClusterEraPaid` event if the operation is successful. + fn set_last_paid_era(cluster_id: &ClusterId, era_id: DdcEra) -> Result<(), DispatchError>; /// Retrieves the `last_validated_era_id` for the given cluster /// update. @@ -127,5 +127,5 @@ pub trait ClusterValidator { /// # Returns /// /// Returns `Ok(DdcEra)` identifier of the last validated era in cluster - fn get_last_validated_era(cluster_id: &ClusterId) -> Result; + fn get_last_paid_era(cluster_id: &ClusterId) -> Result; } From 75b5d5cf7f63fcf4060953a209646811bad2626c Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 13:47:46 +0100 Subject: [PATCH 10/15] fix: compilation issue in payouts tests --- pallets/ddc-payouts/src/mock.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 6a5d3a753..42b3b7bf2 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -4,6 +4,8 @@ #[cfg(feature = "try-runtime")] use ddc_primitives::BucketParams; +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::BucketParams; use ddc_primitives::{ traits::{ bucket::BucketManager, cluster::ClusterProtocol, customer::CustomerCharger, From 74c48760b3a79b5d8501c22427ebff4b4aba32cc Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 14:09:50 +0100 Subject: [PATCH 11/15] fix: unused import is removed --- pallets/ddc-payouts/src/mock.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 42b3b7bf2..e410922b9 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -2,8 +2,6 @@ #![allow(dead_code)] -#[cfg(feature = "try-runtime")] -use ddc_primitives::BucketParams; #[cfg(feature = "runtime-benchmarks")] use ddc_primitives::BucketParams; use ddc_primitives::{ From 512c3853cb69c8a05b9f3d642485b14eb9f51893 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 16:51:12 +0100 Subject: [PATCH 12/15] chore: ddc-verification weights from reference machine --- pallets/ddc-verification/src/benchmarking.rs | 2 - pallets/ddc-verification/src/weights.rs | 83 ++++++++++---------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index 5083ee6c5..a758710ef 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -846,6 +846,4 @@ mod benchmarks { >::contains_key(cluster_id, era_id); } - - impl_benchmark_test_suite!(DdcVerification, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/pallets/ddc-verification/src/weights.rs b/pallets/ddc-verification/src/weights.rs index 17ea2ba63..4d217b25d 100644 --- a/pallets/ddc-verification/src/weights.rs +++ b/pallets/ddc-verification/src/weights.rs @@ -2,7 +2,7 @@ //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev //! DATE: 2024-11-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! HOSTNAME: `Yahors-MacBook-Pro.local`, CPU: `` +//! HOSTNAME: `bench`, CPU: `AMD EPYC-Milan Processor` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: @@ -11,7 +11,6 @@ // pallet // --chain=dev // --execution=wasm -// --wasm-execution=compiled // --pallet=pallet_ddc_verification // --extrinsic=* // --steps=50 @@ -52,8 +51,10 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcVerification::EraValidations` (r:1 w:1) // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `b` is `[1, 5]`. - fn set_prepare_era_for_payout(_b: u32, ) -> Weight { - Weight::from_parts(257_146_553_u64, 0) + fn set_prepare_era_for_payout(b: u32, ) -> Weight { + Weight::from_parts(652_257_491_u64, 0) + // Standard Error: 315_135 + .saturating_add(Weight::from_parts(1_045_800_u64, 0).saturating_mul(b as u64)) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -64,7 +65,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcVerification::ValidatorToStashKey` (r:0 w:1) // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) fn set_validator_key() -> Weight { - Weight::from_parts(195_000_000_u64, 0) + Weight::from_parts(614_541_000_u64, 0) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -77,7 +78,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcVerification::EraValidations` (r:1 w:1) // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_billing_report() -> Weight { - Weight::from_parts(269_000_000_u64, 0) + Weight::from_parts(858_688_000_u64, 0) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } @@ -88,7 +89,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_charging_customers() -> Weight { - Weight::from_parts(223_000_000_u64, 0) + Weight::from_parts(692_927_000_u64, 0) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -112,9 +113,9 @@ impl WeightInfo for SubstrateWeight { // Proof: `DdcPayouts::DebtorCustomers` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `b` is `[1, 1000]`. fn send_charging_customers_batch(b: u32, ) -> Weight { - Weight::from_parts(1_192_000_000_u64, 0) - // Standard Error: 2_345_530 - .saturating_add(Weight::from_parts(782_075_355_u64, 0).saturating_mul(b as u64)) + Weight::from_parts(3_402_331_000_u64, 0) + // Standard Error: 6_536_370 + .saturating_add(Weight::from_parts(1_999_183_585_u64, 0).saturating_mul(b as u64)) .saturating_add(T::DbWeight::get().reads(10_u64)) .saturating_add(T::DbWeight::get().reads((3_u64).saturating_mul(b as u64))) .saturating_add(T::DbWeight::get().writes(5_u64)) @@ -141,7 +142,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `Staking::Nominators` (r:1 w:0) // Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`) fn end_charging_customers() -> Weight { - Weight::from_parts(2_169_000_000_u64, 0) + Weight::from_parts(6_076_476_000_u64, 0) .saturating_add(T::DbWeight::get().reads(13_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) } @@ -152,7 +153,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_rewarding_providers() -> Weight { - Weight::from_parts(226_000_000_u64, 0) + Weight::from_parts(695_822_000_u64, 0) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -170,9 +171,9 @@ impl WeightInfo for SubstrateWeight { // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) /// The range of component `b` is `[1, 1000]`. fn send_rewarding_providers_batch(b: u32, ) -> Weight { - Weight::from_parts(908_000_000_u64, 0) - // Standard Error: 334_349 - .saturating_add(Weight::from_parts(622_558_822_u64, 0).saturating_mul(b as u64)) + Weight::from_parts(2_651_124_000_u64, 0) + // Standard Error: 440_083 + .saturating_add(Weight::from_parts(1_673_309_967_u64, 0).saturating_mul(b as u64)) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(b as u64))) .saturating_add(T::DbWeight::get().writes(2_u64)) @@ -185,7 +186,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_rewarding_providers() -> Weight { - Weight::from_parts(232_000_000_u64, 0) + Weight::from_parts(703_757_000_u64, 0) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -200,7 +201,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcClusters::Clusters` (r:1 w:1) // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_billing_report() -> Weight { - Weight::from_parts(434_000_000_u64, 0) + Weight::from_parts(1_343_575_000_u64, 0) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } @@ -210,9 +211,9 @@ impl WeightInfo for SubstrateWeight { // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) /// The range of component `b` is `[1, 5]`. fn emit_consensus_errors(b: u32, ) -> Weight { - Weight::from_parts(100_407_885_u64, 0) - // Standard Error: 26_465 - .saturating_add(Weight::from_parts(23_011_156_u64, 0).saturating_mul(b as u64)) + Weight::from_parts(322_196_711_u64, 0) + // Standard Error: 285_972 + .saturating_add(Weight::from_parts(46_385_848_u64, 0).saturating_mul(b as u64)) .saturating_add(T::DbWeight::get().reads(2_u64)) } // Storage: `DdcVerification::EraValidations` (r:1 w:1) @@ -220,7 +221,7 @@ impl WeightInfo for SubstrateWeight { // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) fn set_era_validations() -> Weight { - Weight::from_parts(180_000_000_u64, 0) + Weight::from_parts(498_523_000_u64, 0) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -235,8 +236,10 @@ impl WeightInfo for () { // Storage: `DdcVerification::EraValidations` (r:1 w:1) // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `b` is `[1, 5]`. - fn set_prepare_era_for_payout(_b: u32, ) -> Weight { - Weight::from_parts(257_146_553_u64, 0) + fn set_prepare_era_for_payout(b: u32, ) -> Weight { + Weight::from_parts(652_257_491_u64, 0) + // Standard Error: 315_135 + .saturating_add(Weight::from_parts(1_045_800_u64, 0).saturating_mul(b as u64)) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } @@ -247,7 +250,7 @@ impl WeightInfo for () { // Storage: `DdcVerification::ValidatorToStashKey` (r:0 w:1) // Proof: `DdcVerification::ValidatorToStashKey` (`max_values`: None, `max_size`: None, mode: `Measured`) fn set_validator_key() -> Weight { - Weight::from_parts(195_000_000_u64, 0) + Weight::from_parts(614_541_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } @@ -260,7 +263,7 @@ impl WeightInfo for () { // Storage: `DdcVerification::EraValidations` (r:1 w:1) // Proof: `DdcVerification::EraValidations` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_billing_report() -> Weight { - Weight::from_parts(269_000_000_u64, 0) + Weight::from_parts(858_688_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } @@ -271,7 +274,7 @@ impl WeightInfo for () { // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_charging_customers() -> Weight { - Weight::from_parts(223_000_000_u64, 0) + Weight::from_parts(692_927_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } @@ -295,9 +298,9 @@ impl WeightInfo for () { // Proof: `DdcPayouts::DebtorCustomers` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `b` is `[1, 1000]`. fn send_charging_customers_batch(b: u32, ) -> Weight { - Weight::from_parts(1_192_000_000_u64, 0) - // Standard Error: 2_345_530 - .saturating_add(Weight::from_parts(782_075_355_u64, 0).saturating_mul(b as u64)) + Weight::from_parts(3_402_331_000_u64, 0) + // Standard Error: 6_536_370 + .saturating_add(Weight::from_parts(1_999_183_585_u64, 0).saturating_mul(b as u64)) .saturating_add(RocksDbWeight::get().reads(10_u64)) .saturating_add(RocksDbWeight::get().reads((3_u64).saturating_mul(b as u64))) .saturating_add(RocksDbWeight::get().writes(5_u64)) @@ -324,7 +327,7 @@ impl WeightInfo for () { // Storage: `Staking::Nominators` (r:1 w:0) // Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`) fn end_charging_customers() -> Weight { - Weight::from_parts(2_169_000_000_u64, 0) + Weight::from_parts(6_076_476_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(13_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) } @@ -335,7 +338,7 @@ impl WeightInfo for () { // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn begin_rewarding_providers() -> Weight { - Weight::from_parts(226_000_000_u64, 0) + Weight::from_parts(695_822_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } @@ -353,9 +356,9 @@ impl WeightInfo for () { // Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) /// The range of component `b` is `[1, 1000]`. fn send_rewarding_providers_batch(b: u32, ) -> Weight { - Weight::from_parts(908_000_000_u64, 0) - // Standard Error: 334_349 - .saturating_add(Weight::from_parts(622_558_822_u64, 0).saturating_mul(b as u64)) + Weight::from_parts(2_651_124_000_u64, 0) + // Standard Error: 440_083 + .saturating_add(Weight::from_parts(1_673_309_967_u64, 0).saturating_mul(b as u64)) .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(b as u64))) .saturating_add(RocksDbWeight::get().writes(2_u64)) @@ -368,7 +371,7 @@ impl WeightInfo for () { // Storage: `DdcPayouts::ActiveBillingReports` (r:1 w:1) // Proof: `DdcPayouts::ActiveBillingReports` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_rewarding_providers() -> Weight { - Weight::from_parts(232_000_000_u64, 0) + Weight::from_parts(703_757_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } @@ -383,7 +386,7 @@ impl WeightInfo for () { // Storage: `DdcClusters::Clusters` (r:1 w:1) // Proof: `DdcClusters::Clusters` (`max_values`: None, `max_size`: None, mode: `Measured`) fn end_billing_report() -> Weight { - Weight::from_parts(434_000_000_u64, 0) + Weight::from_parts(1_343_575_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) } @@ -393,9 +396,9 @@ impl WeightInfo for () { // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) /// The range of component `b` is `[1, 5]`. fn emit_consensus_errors(b: u32, ) -> Weight { - Weight::from_parts(100_407_885_u64, 0) - // Standard Error: 26_465 - .saturating_add(Weight::from_parts(23_011_156_u64, 0).saturating_mul(b as u64)) + Weight::from_parts(322_196_711_u64, 0) + // Standard Error: 285_972 + .saturating_add(Weight::from_parts(46_385_848_u64, 0).saturating_mul(b as u64)) .saturating_add(RocksDbWeight::get().reads(2_u64)) } // Storage: `DdcVerification::EraValidations` (r:1 w:1) @@ -403,7 +406,7 @@ impl WeightInfo for () { // Storage: `DdcVerification::ValidatorSet` (r:1 w:0) // Proof: `DdcVerification::ValidatorSet` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) fn set_era_validations() -> Weight { - Weight::from_parts(180_000_000_u64, 0) + Weight::from_parts(498_523_000_u64, 0) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } From 411b1b738ded7b585005f1ef63cff9b30ef3ca4f Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 19:23:14 +0100 Subject: [PATCH 13/15] refactor: 'CustomerUsage' is renamed to 'BucketUsage' --- pallets/ddc-clusters/src/cluster.rs | 4 +- pallets/ddc-customers/src/lib.rs | 14 +- pallets/ddc-customers/src/migration.rs | 2 +- pallets/ddc-customers/src/tests.rs | 2 +- pallets/ddc-payouts/src/lib.rs | 14 +- pallets/ddc-payouts/src/mock.rs | 8 +- pallets/ddc-payouts/src/tests.rs | 132 +++++++++---------- pallets/ddc-verification/src/benchmarking.rs | 4 +- pallets/ddc-verification/src/lib.rs | 10 +- pallets/ddc-verification/src/mock.rs | 6 +- primitives/src/lib.rs | 18 +-- primitives/src/traits/bucket.rs | 6 +- primitives/src/traits/customer.rs | 4 +- primitives/src/traits/payout.rs | 4 +- primitives/src/traits/validator.rs | 4 +- 15 files changed, 117 insertions(+), 115 deletions(-) diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index 9ce44edd8..ebe39aa8c 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -15,8 +15,8 @@ pub struct Cluster { pub reserve_id: AccountId, pub props: ClusterProps, pub status: ClusterStatus, - // todo(yahortsaryk): this should be renamed to `last_paid_era` to eliminate ambiguity, - // as the validation step is decoupled from payout step. + // todo(yahortsaryk): `last_validated_era_id` should be renamed to `last_paid_era` to eliminate + // ambiguity, as the validation step is decoupled from payout step. pub last_validated_era_id: DdcEra, } diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index a5a1914e1..76e4bb8cb 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -19,7 +19,7 @@ use ddc_primitives::{ cluster::{ClusterCreator, ClusterProtocol, ClusterQuery}, customer::{CustomerCharger, CustomerDepositor, CustomerVisitor}, }, - BucketId, BucketParams, ClusterId, CustomerUsage, + BucketId, BucketParams, BucketUsage, ClusterId, }; use frame_support::{ parameter_types, @@ -67,7 +67,9 @@ pub struct Bucket { cluster_id: ClusterId, is_public: bool, is_removed: bool, - total_customers_usage: Option, + // todo(yahortsaryk): `total_customers_usage` should be renamed to `total_usage` to eliminate + // ambiguity, as the bucket owner is the only customer of the bucket who pays for its usage. + total_customers_usage: Option, } #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] @@ -665,7 +667,7 @@ pub mod pallet { cluster_id: &ClusterId, bucket_id: BucketId, content_owner: &T::AccountId, - ) -> Result, DispatchError> { + ) -> Result, DispatchError> { let bucket = Self::buckets(bucket_id).ok_or(Error::::NoBucketWithId)?; ensure!(bucket.owner_id == *content_owner, Error::::NotBucketOwner); ensure!(bucket.cluster_id == *cluster_id, Error::::ClusterMismatch); @@ -677,7 +679,7 @@ pub mod pallet { cluster_id: &ClusterId, bucket_id: BucketId, content_owner: T::AccountId, - customer_usage: &CustomerUsage, + customer_usage: &BucketUsage, ) -> DispatchResult { let mut bucket = Self::buckets(bucket_id).ok_or(Error::::NoBucketWithId)?; ensure!(bucket.owner_id == content_owner, Error::::NotBucketOwner); @@ -691,7 +693,7 @@ pub mod pallet { total_customers_usage.number_of_gets += customer_usage.number_of_gets; }, None => { - bucket.total_customers_usage = Some(CustomerUsage { + bucket.total_customers_usage = Some(BucketUsage { transferred_bytes: customer_usage.transferred_bytes, stored_bytes: customer_usage.stored_bytes, number_of_puts: customer_usage.number_of_puts, @@ -730,7 +732,7 @@ pub mod pallet { bucket_id: BucketId, content_owner: T::AccountId, billing_vault: T::AccountId, - customer_usage: &CustomerUsage, + customer_usage: &BucketUsage, amount: u128, ) -> Result { let actually_charged: BalanceOf; diff --git a/pallets/ddc-customers/src/migration.rs b/pallets/ddc-customers/src/migration.rs index 576988f78..3c964972d 100644 --- a/pallets/ddc-customers/src/migration.rs +++ b/pallets/ddc-customers/src/migration.rs @@ -162,7 +162,7 @@ pub mod v2 { cluster_id: ClusterId, is_public: bool, is_removed: bool, - total_customers_usage: Option, // new field + total_customers_usage: Option, // new field } #[storage_alias] diff --git a/pallets/ddc-customers/src/tests.rs b/pallets/ddc-customers/src/tests.rs index 9f31edcf5..e6096f7b2 100644 --- a/pallets/ddc-customers/src/tests.rs +++ b/pallets/ddc-customers/src/tests.rs @@ -182,7 +182,7 @@ fn charge_content_owner_works() { let bucket_id2: BucketId = 2; let cluster_id = ClusterId::from([1; 20]); let bucket_1_params = BucketParams { is_public: false }; - let customer_usage = CustomerUsage { + let customer_usage = BucketUsage { transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 2bb5edc78..33b50de92 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -29,9 +29,9 @@ use ddc_primitives::{ customer::CustomerCharger as CustomerChargerType, node::NodeManager, pallet::PalletVisitor as PalletVisitorType, payout::PayoutProcessor, }, - BatchIndex, BucketId, ClusterId, CustomerCharge, CustomerUsage, DdcEra, MMRProof, NodePubKey, - NodeReward, NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, - MILLICENTS, + BatchIndex, BucketId, BucketUsage, ClusterId, CustomerCharge, DdcEra, MMRProof, NodePubKey, + NodeUsage, PayoutError, PayoutState, ProviderReward, MAX_PAYOUT_BATCH_COUNT, + MAX_PAYOUT_BATCH_SIZE, MILLICENTS, }; use frame_election_provider_support::SortedListProvider; use frame_support::{ @@ -392,8 +392,8 @@ pub mod pallet { node_usage: &NodeUsage, total_nodes_usage: &NodeUsage, total_customer_charge: &CustomerCharge, - ) -> Option { - let mut node_reward = NodeReward::default(); + ) -> Option { + let mut node_reward = ProviderReward::default(); let mut ratio = Perquintill::from_rational( node_usage.transferred_bytes as u128, @@ -422,7 +422,7 @@ pub mod pallet { fn get_customer_charge( cluster_id: &ClusterId, - usage: &CustomerUsage, + usage: &BucketUsage, bucket_id: BucketId, customer_id: &T::AccountId, start_era: i64, @@ -609,7 +609,7 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payers: &[(NodePubKey, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, BucketUsage)], batch_proof: MMRProof, ) -> DispatchResult { ensure!( diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index e410922b9..20500868d 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -207,7 +207,7 @@ where _era: DdcEra, _batch_index: BatchIndex, _max_batch_index: BatchIndex, - _payers: &[(NodePubKey, BucketId, CustomerUsage)], + _payers: &[(NodePubKey, BucketId, BucketUsage)], _batch_proof: &MMRProof, ) -> bool { true @@ -258,7 +258,7 @@ where _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: &T::AccountId, - ) -> Result, DispatchError> { + ) -> Result, DispatchError> { Ok(None) } @@ -266,7 +266,7 @@ where _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: T::AccountId, - _customer_usage: &CustomerUsage, + _customer_usage: &BucketUsage, ) -> DispatchResult { unimplemented!() } @@ -292,7 +292,7 @@ where _bucket_id: BucketId, content_owner: T::AccountId, billing_vault: T::AccountId, - _customer_usage: &CustomerUsage, + _customer_usage: &BucketUsage, amount: u128, ) -> Result { let mut amount_to_charge = amount; diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 96e1a14a1..8932ff796 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -93,7 +93,7 @@ fn send_charging_customers_batch_fails_uninitialised() { let batch_index = 1; let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let customer_usage = CustomerUsage { + let customer_usage = BucketUsage { transferred_bytes: 100, stored_bytes: -800, number_of_gets: 100, @@ -101,7 +101,7 @@ fn send_charging_customers_batch_fails_uninitialised() { }; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let payers1 = vec![(node_key.clone(), bucket_id3, customer_usage)]; - let payers2 = vec![(node_key.clone(), bucket_id4, CustomerUsage::default())]; + let payers2 = vec![(node_key.clone(), bucket_id4, BucketUsage::default())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight let start_era: i64 = @@ -140,7 +140,7 @@ fn send_charging_customers_batch_fails_uninitialised() { max_batch_index, )); - let payers1 = vec![(node_key, bucket_id4, CustomerUsage::default())]; + let payers1 = vec![(node_key, bucket_id4, BucketUsage::default())]; assert_ok!(>::send_charging_customers_batch( cluster_id, era, @@ -173,7 +173,7 @@ fn send_charging_customers_batch_fails_uninitialised() { }) } -fn calculate_charge_parts_for_day(cluster_id: ClusterId, usage: CustomerUsage) -> CustomerCharge { +fn calculate_charge_parts_for_day(cluster_id: ClusterId, usage: BucketUsage) -> CustomerCharge { let pricing_params = get_pricing(&cluster_id); // Calculate the duration of the period in seconds @@ -199,12 +199,12 @@ fn calculate_charge_parts_for_day(cluster_id: ClusterId, usage: CustomerUsage) - } } -fn calculate_charge_for_day(cluster_id: ClusterId, usage: CustomerUsage) -> u128 { +fn calculate_charge_for_day(cluster_id: ClusterId, usage: BucketUsage) -> u128 { let charge = calculate_charge_parts_for_day(cluster_id, usage); charge.transfer + charge.storage + charge.puts + charge.gets } -fn calculate_charge_parts_for_month(cluster_id: ClusterId, usage: CustomerUsage) -> CustomerCharge { +fn calculate_charge_parts_for_month(cluster_id: ClusterId, usage: BucketUsage) -> CustomerCharge { let pricing_params = get_pricing(&cluster_id); let fraction_of_month = Perquintill::one(); @@ -225,7 +225,7 @@ fn calculate_charge_parts_for_month(cluster_id: ClusterId, usage: CustomerUsage) } } -fn calculate_charge_parts_for_hour(cluster_id: ClusterId, usage: CustomerUsage) -> CustomerCharge { +fn calculate_charge_parts_for_hour(cluster_id: ClusterId, usage: BucketUsage) -> CustomerCharge { let pricing_params = get_pricing(&cluster_id); let duration_seconds = 1.0 * 1.0 * 3600.0; @@ -249,12 +249,12 @@ fn calculate_charge_parts_for_hour(cluster_id: ClusterId, usage: CustomerUsage) } } -fn calculate_charge_for_month(cluster_id: ClusterId, usage: CustomerUsage) -> u128 { +fn calculate_charge_for_month(cluster_id: ClusterId, usage: BucketUsage) -> u128 { let charge = calculate_charge_parts_for_month(cluster_id, usage); charge.transfer + charge.storage + charge.puts + charge.gets } -fn calculate_charge_for_hour(cluster_id: ClusterId, usage: CustomerUsage) -> u128 { +fn calculate_charge_for_hour(cluster_id: ClusterId, usage: BucketUsage) -> u128 { let charge = calculate_charge_parts_for_hour(cluster_id, usage); charge.transfer + charge.storage + charge.puts + charge.gets } @@ -279,28 +279,28 @@ fn send_charging_customers_batch_works() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -526,13 +526,13 @@ fn end_charging_customers_works_small_usage_1_hour() { let bucket_id6: BucketId = BUCKET_ID6; let bucket_id7: BucketId = BUCKET_ID7; - let usage6 = CustomerUsage { + let usage6 = BucketUsage { transferred_bytes: 0, stored_bytes: 474_957, number_of_puts: 0, number_of_gets: 0, }; - let usage7 = CustomerUsage { + let usage7 = BucketUsage { transferred_bytes: 474_957, stored_bytes: 0, number_of_puts: 0, @@ -719,28 +719,28 @@ fn send_charging_customers_batch_works_for_day() { let bucket_id4: BucketId = BUCKET_ID4; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -971,28 +971,28 @@ fn send_charging_customers_batch_works_for_day_free_storage() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -1223,28 +1223,28 @@ fn send_charging_customers_batch_works_for_day_free_stream() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -1474,28 +1474,28 @@ fn send_charging_customers_batch_works_for_day_free_get() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -1726,28 +1726,28 @@ fn send_charging_customers_batch_works_for_day_free_put() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -1978,28 +1978,28 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { // should pass without debt transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let usage2 = CustomerUsage { + let usage2 = BucketUsage { // should fail as not enough balance transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage3 = CustomerUsage { + let usage3 = BucketUsage { // should pass but with debt (partial charge) transferred_bytes: 1, stored_bytes: 2, number_of_puts: 3, number_of_gets: 4, }; - let usage4 = CustomerUsage { + let usage4 = BucketUsage { // should pass without debt transferred_bytes: 467457, stored_bytes: 45674567456, @@ -2221,7 +2221,7 @@ fn send_charging_customers_batch_works_zero_fees() { let max_batch_index = 0; let batch_index = 0; let bucket_id5: BucketId = BUCKET_ID5; - let usage5 = CustomerUsage { + let usage5 = BucketUsage { // should pass without debt transferred_bytes: 1024, stored_bytes: 1024, @@ -2293,7 +2293,7 @@ fn end_charging_customers_fails_uninitialised() { let batch_index = 1; let bucket_id1: BucketId = BUCKET_ID1; - let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::default())]; + let payers = vec![(node_key.clone(), bucket_id1, BucketUsage::default())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight let start_era: i64 = @@ -2352,7 +2352,7 @@ fn end_charging_customers_works() { let max_batch_index = 0; let batch_index = 0; let bucket_id1: BucketId = BUCKET_ID1; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, @@ -2523,7 +2523,7 @@ fn end_charging_customers_works_zero_fees() { let max_batch_index = 0; let batch_index = 0; let bucket_id1: BucketId = BUCKET_ID1; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 1, @@ -2636,7 +2636,7 @@ fn begin_rewarding_providers_fails_uninitialised() { let batch_index = 1; let bucket_id1: BucketId = BUCKET_ID1; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); - let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::default())]; + let payers = vec![(node_key.clone(), bucket_id1, BucketUsage::default())]; let node_usage = NodeUsage::default(); let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -2735,7 +2735,7 @@ fn begin_rewarding_providers_works() { let bucket_id1: BucketId = BUCKET_ID1; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let total_node_usage = NodeUsage::default(); - let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::default())]; + let payers = vec![(node_key.clone(), bucket_id1, BucketUsage::default())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -2790,8 +2790,8 @@ fn send_rewarding_providers_batch_fails_uninitialised() { let batch_index = 0; let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let payers1 = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; - let payers2 = vec![(node_key.clone(), bucket_id4, CustomerUsage::default())]; + let payers1 = vec![(node_key.clone(), bucket_id3, BucketUsage::default())]; + let payers2 = vec![(node_key.clone(), bucket_id4, BucketUsage::default())]; let payees = vec![(node_key, NodeUsage::default())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -2909,7 +2909,7 @@ fn send_rewarding_providers_batch_works() { let batch_index = 0; let batch_node_index = 0; let bucket_id1: BucketId = 1; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, @@ -3230,7 +3230,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { let start_era: i64 = DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 1024, stored_bytes: 1024, number_of_puts: 1, @@ -3288,8 +3288,8 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { } let mut total_charge = 0u128; - let mut payers: Vec> = Vec::new(); - let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, BucketUsage)> = Vec::new(); for user_id in 100..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::one(), @@ -3523,7 +3523,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { let node_batch_size = 10; let mut batch_user_index = 0; let mut batch_node_index = 0; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 1024, stored_bytes: 1024, number_of_puts: 1, @@ -3594,8 +3594,8 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { } let mut total_charge = 0u128; - let mut payers: Vec> = Vec::new(); - let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, BucketUsage)> = Vec::new(); for user_id in 100..100 + num_users { let ratio = match user_id % 5 { @@ -3826,7 +3826,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { let node_batch_size = 10; let mut batch_user_index = 0; let mut batch_node_index = 0; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 1024, stored_bytes: 1024, number_of_puts: 1, @@ -3897,8 +3897,8 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { } let mut total_charge = 0u128; - let mut payers: Vec> = Vec::new(); - let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, BucketUsage)> = Vec::new(); for user_id in 100u8..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::from_float(1_000_000.0), @@ -4171,10 +4171,10 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { } let mut total_charge = 0u128; - let mut payers: Vec> = Vec::new(); - let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, BucketUsage)> = Vec::new(); for user_id in 100..100 + num_users { - let user_usage = CustomerUsage { + let user_usage = BucketUsage { transferred_bytes: generate_random_u64(&mock_randomness, min, max), stored_bytes: (generate_random_u64(&mock_randomness, min, max)) as i64, number_of_puts: generate_random_u64(&mock_randomness, min, max), @@ -4350,8 +4350,8 @@ fn end_rewarding_providers_fails_uninitialised() { let batch_index = 0; let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let payers1 = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; - let payers2 = vec![(node_key.clone(), bucket_id4, CustomerUsage::default())]; + let payers1 = vec![(node_key.clone(), bucket_id3, BucketUsage::default())]; + let payers2 = vec![(node_key.clone(), bucket_id4, BucketUsage::default())]; let payees = vec![(node_key, NodeUsage::default())]; let total_node_usage = NodeUsage::default(); let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -4461,7 +4461,7 @@ fn end_rewarding_providers_works() { let max_batch_index = 0; let batch_index = 0; let bucket_id1: BucketId = 1; - let usage1 = CustomerUsage { + let usage1 = BucketUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, number_of_puts: 4456456345234523, @@ -4544,8 +4544,8 @@ fn end_billing_report_fails_uninitialised() { let bucket_id3: BucketId = BUCKET_ID3; let bucket_id4: BucketId = BUCKET_ID4; - let payers1 = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; - let payers2 = vec![(node_key.clone(), bucket_id4, CustomerUsage::default())]; + let payers1 = vec![(node_key.clone(), bucket_id3, BucketUsage::default())]; + let payers2 = vec![(node_key.clone(), bucket_id4, BucketUsage::default())]; let payees = vec![(node_key, NodeUsage::default())]; let total_node_usage = NodeUsage::default(); @@ -4664,7 +4664,7 @@ fn end_billing_report_works() { let batch_index = 0; let total_node_usage = NodeUsage::default(); let bucket_id3 = BUCKET_ID3; - let payers = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; + let payers = vec![(node_key.clone(), bucket_id3, BucketUsage::default())]; let payees = vec![(node_key.clone(), NodeUsage::default())]; assert_ok!(>::begin_billing_report( diff --git a/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index a758710ef..f7f0aa2b2 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -328,7 +328,7 @@ mod benchmarks { whitelist_account!(validator); let batch_index: BatchIndex = 0; - let mut payers_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = vec![]; + let mut payers_batch: Vec<(NodePubKey, BucketId, BucketUsage)> = vec![]; for i in 0..b { let customer = create_account::("customer", i, i); @@ -340,7 +340,7 @@ mod benchmarks { endow_customer::(&customer, 10 * CERE); } - let customer_usage = CustomerUsage { + let customer_usage = BucketUsage { transferred_bytes: 200000000, // 200 mb stored_bytes: 100000000, // 100 mb number_of_gets: 10, // 10 gets diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index b7ffca894..63d92ca26 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -19,7 +19,7 @@ use ddc_primitives::{ ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutProcessor, ValidatorVisitor, }, - ActivityHash, BatchIndex, ClusterId, ClusterStatus, CustomerUsage, DdcEra, EraValidation, + ActivityHash, BatchIndex, BucketUsage, ClusterId, ClusterStatus, DdcEra, EraValidation, EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage, PayoutState, StorageNodeParams, }; @@ -558,7 +558,7 @@ pub mod pallet { pub struct CustomerBatch { pub(crate) batch_index: BatchIndex, - pub(crate) payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, + pub(crate) payers: Vec<(NodePubKey, BucketId, BucketUsage)>, pub(crate) batch_proof: MMRProof, } @@ -2074,7 +2074,7 @@ pub mod pallet { let node_key = Self::node_key_from_hex(activity.node_id.clone()) .expect("Node Public Key to be decoded"); let bucket_id = activity.bucket_id; - let customer_usage = CustomerUsage { + let customer_usage = BucketUsage { transferred_bytes: activity.transferred_bytes, stored_bytes: activity.stored_bytes, number_of_puts: activity.number_of_puts, @@ -3583,7 +3583,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, + payers: Vec<(NodePubKey, BucketId, BucketUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -3985,7 +3985,7 @@ pub mod pallet { era_id: DdcEra, batch_index: BatchIndex, max_batch_index: BatchIndex, - payers: &[(NodePubKey, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, BucketUsage)], batch_proof: &MMRProof, ) -> bool { let validation_era = EraValidations::::get(cluster_id, era_id); diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index 1df98ad92..48154207e 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -302,7 +302,7 @@ impl BucketManager for MockBucketManager { _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: &T::AccountId, - ) -> Result, DispatchError> { + ) -> Result, DispatchError> { unimplemented!() } @@ -310,7 +310,7 @@ impl BucketManager for MockBucketManager { _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: T::AccountId, - _customer_usage: &CustomerUsage, + _customer_usage: &BucketUsage, ) -> DispatchResult { unimplemented!() } @@ -450,7 +450,7 @@ impl PayoutProcessor for MockPayoutProcessor { _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, - _payers: &[(NodePubKey, BucketId, CustomerUsage)], + _payers: &[(NodePubKey, BucketId, BucketUsage)], _batch_proof: MMRProof, ) -> DispatchResult { unimplemented!() diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 68c1b0a25..3c59c3f17 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -267,27 +267,27 @@ pub struct ClusterNodesStats { pub validation_failed: ClusterNodesCount, } -/// Stores usage of customers +/// Stores usage of a bucket #[derive( PartialEq, Eq, Encode, Decode, Debug, TypeInfo, Default, Clone, Serialize, Deserialize, )] -pub struct CustomerUsage { +pub struct BucketUsage { pub transferred_bytes: u64, pub stored_bytes: i64, pub number_of_puts: u64, pub number_of_gets: u64, } -/// Stores charge in tokens(units) of customer as per CustomerUsage +/// Stores charge in tokens(units) of customer as per BucketUsage #[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] pub struct CustomerCharge { - pub transfer: u128, // charge in tokens for CustomerUsage::transferred_bytes - pub storage: u128, // charge in tokens for CustomerUsage::stored_bytes - pub puts: u128, // charge in tokens for CustomerUsage::number_of_puts - pub gets: u128, // charge in tokens for CustomerUsage::number_of_gets + pub transfer: u128, // charge in tokens for BucketUsage::transferred_bytes + pub storage: u128, // charge in tokens for BucketUsage::stored_bytes + pub puts: u128, // charge in tokens for BucketUsage::number_of_puts + pub gets: u128, // charge in tokens for BucketUsage::number_of_gets } -/// Stores usage of node provider +/// Stores usage of a node #[derive( PartialEq, Eq, Encode, Decode, Debug, TypeInfo, Default, Clone, Serialize, Deserialize, )] @@ -300,7 +300,7 @@ pub struct NodeUsage { /// Stores reward in tokens(units) of node provider as per NodeUsage #[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)] -pub struct NodeReward { +pub struct ProviderReward { pub transfer: u128, // reward in tokens for NodeUsage::transferred_bytes pub storage: u128, // reward in tokens for NodeUsage::stored_bytes pub puts: u128, // reward in tokens for NodeUsage::number_of_puts diff --git a/primitives/src/traits/bucket.rs b/primitives/src/traits/bucket.rs index 8f525c81b..db087f324 100644 --- a/primitives/src/traits/bucket.rs +++ b/primitives/src/traits/bucket.rs @@ -2,7 +2,7 @@ use sp_runtime::{DispatchError, DispatchResult}; #[cfg(feature = "runtime-benchmarks")] use crate::BucketParams; -use crate::{BucketId, ClusterId, CustomerUsage}; +use crate::{BucketId, BucketUsage, ClusterId}; pub trait BucketManager { fn get_bucket_owner_id(bucket_id: BucketId) -> Result; @@ -10,13 +10,13 @@ pub trait BucketManager { cluster_id: &ClusterId, bucket_id: BucketId, content_owner: &T::AccountId, - ) -> Result, DispatchError>; + ) -> Result, DispatchError>; fn inc_total_bucket_usage( cluster_id: &ClusterId, bucket_id: BucketId, content_owner: T::AccountId, - customer_usage: &CustomerUsage, + customer_usage: &BucketUsage, ) -> DispatchResult; #[cfg(feature = "runtime-benchmarks")] diff --git a/primitives/src/traits/customer.rs b/primitives/src/traits/customer.rs index 3dd71a76a..59a8d1114 100644 --- a/primitives/src/traits/customer.rs +++ b/primitives/src/traits/customer.rs @@ -2,7 +2,7 @@ use core::u128; use sp_runtime::DispatchError; -use crate::{BucketId, ClusterId, CustomerUsage}; +use crate::{BucketId, BucketUsage, ClusterId}; pub trait CustomerCharger { fn charge_content_owner( @@ -10,7 +10,7 @@ pub trait CustomerCharger { bucket_id: BucketId, content_owner: T::AccountId, billing_vault: T::AccountId, - customer_usage: &CustomerUsage, + customer_usage: &BucketUsage, amount: u128, ) -> Result; } diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index 14a382eb3..351617e6f 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -3,7 +3,7 @@ use sp_runtime::DispatchResult; #[cfg(feature = "runtime-benchmarks")] use crate::BillingReportParams; use crate::{ - BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, + BatchIndex, BucketId, BucketUsage, ClusterId, DdcEra, MMRProof, NodePubKey, NodeUsage, PayoutError, PayoutState, }; @@ -25,7 +25,7 @@ pub trait PayoutProcessor { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(NodePubKey, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, BucketUsage)], batch_proof: MMRProof, ) -> DispatchResult; diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index 020c86b48..954b1b1bf 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -1,7 +1,7 @@ use frame_system::Config; use crate::{ - BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, + BatchIndex, BucketId, BucketUsage, ClusterId, DdcEra, MMRProof, NodePubKey, NodeUsage, }; pub trait ValidatorVisitor { @@ -11,7 +11,7 @@ pub trait ValidatorVisitor { era: DdcEra, batch_index: BatchIndex, max_batch_index: BatchIndex, - payers: &[(NodePubKey, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, BucketUsage)], batch_proof: &MMRProof, ) -> bool; fn is_providers_batch_valid( From e494bb78db6d64536d48a80e5a3a008528d5b0a3 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 15 Nov 2024 19:24:33 +0100 Subject: [PATCH 14/15] chore: 'spec_version' is increased --- runtime/cere-dev/src/lib.rs | 4 ++-- runtime/cere/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 7e065f80b..8a85ca908 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -153,10 +153,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 61003, + spec_version: 61004, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 22, + transaction_version: 23, state_version: 0, }; diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 03f337e7b..d5493af7e 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -147,10 +147,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 61003, + spec_version: 61004, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 22, + transaction_version: 23, state_version: 0, }; From beaea12a0d64184c317685fb8702efd18fd9c3e0 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Mon, 18 Nov 2024 12:16:32 +0100 Subject: [PATCH 15/15] chore: addressing PR comments --- Cargo.lock | 1 - pallets/ddc-clusters/src/cluster.rs | 6 ++---- pallets/ddc-clusters/src/lib.rs | 4 ++-- pallets/ddc-clusters/src/migrations.rs | 14 +++++++------- pallets/ddc-clusters/src/tests.rs | 2 +- pallets/ddc-payouts/Cargo.toml | 2 -- pallets/ddc-staking/src/tests.rs | 10 +++++----- primitives/src/traits/cluster.rs | 14 +++++++------- 8 files changed, 24 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ced123b99..920cc8c64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5727,7 +5727,6 @@ dependencies = [ "pallet-balances", "pallet-ddc-verification", "parity-scale-codec", - "polkadot-ckb-merkle-mountain-range", "scale-info", "sp-core", "sp-io", diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index ebe39aa8c..ae47d63f7 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -15,9 +15,7 @@ pub struct Cluster { pub reserve_id: AccountId, pub props: ClusterProps, pub status: ClusterStatus, - // todo(yahortsaryk): `last_validated_era_id` should be renamed to `last_paid_era` to eliminate - // ambiguity, as the validation step is decoupled from payout step. - pub last_validated_era_id: DdcEra, + pub last_paid_era: DdcEra, } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Serialize, Deserialize)] @@ -46,7 +44,7 @@ impl Cluster { replication_total: cluster_params.replication_total, }, status: ClusterStatus::Unbonded, - last_validated_era_id: DdcEra::default(), + last_paid_era: DdcEra::default(), } } diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 75a8bbd99..ed86010ea 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -894,7 +894,7 @@ pub mod pallet { let mut cluster = Clusters::::try_get(cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; - cluster.last_validated_era_id = era_id; + cluster.last_paid_era = era_id; Clusters::::insert(cluster_id, cluster); Self::deposit_event(Event::::ClusterEraPaid { cluster_id: *cluster_id, era_id }); @@ -905,7 +905,7 @@ pub mod pallet { let cluster = Clusters::::try_get(cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; - Ok(cluster.last_validated_era_id) + Ok(cluster.last_paid_era) } } diff --git a/pallets/ddc-clusters/src/migrations.rs b/pallets/ddc-clusters/src/migrations.rs index 335b1f6f5..c3b585364 100644 --- a/pallets/ddc-clusters/src/migrations.rs +++ b/pallets/ddc-clusters/src/migrations.rs @@ -465,7 +465,7 @@ pub mod v3 { pub reserve_id: AccountId, pub props: ClusterProps, pub status: ClusterStatus, - pub last_validated_era_id: DdcEra, // new field + pub last_paid_era: DdcEra, // new field } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Serialize, Deserialize)] @@ -521,7 +521,7 @@ pub mod v3 { replication_total: old_cluster.props.replication_total, }, status: old_cluster.status, - last_validated_era_id: 0, + last_paid_era: 0, }) }, ); @@ -610,7 +610,7 @@ pub mod v3 { erasure_coding_total: 48, replication_total: 20, }, - last_validated_era_id: 0, + last_paid_era: 0, status: ClusterStatus::Activated, }; @@ -625,7 +625,7 @@ pub mod v3 { erasure_coding_total: 48, replication_total: 20, }, - last_validated_era_id: 0, + last_paid_era: 0, status: ClusterStatus::Activated, }; @@ -647,21 +647,21 @@ pub mod v3 { ); assert_eq!(Clusters::::get(cluster_id0).unwrap().props.erasure_coding_total, 48); assert_eq!(Clusters::::get(cluster_id0).unwrap().props.replication_total, 20); - assert_eq!(Clusters::::get(cluster_id0).unwrap().last_validated_era_id, 0); + assert_eq!(Clusters::::get(cluster_id0).unwrap().last_paid_era, 0); assert_eq!( Clusters::::get(cluster_id1).unwrap().props.erasure_coding_required, 16 ); assert_eq!(Clusters::::get(cluster_id1).unwrap().props.erasure_coding_total, 48); assert_eq!(Clusters::::get(cluster_id1).unwrap().props.replication_total, 20); - assert_eq!(Clusters::::get(cluster_id1).unwrap().last_validated_era_id, 0); + assert_eq!(Clusters::::get(cluster_id1).unwrap().last_paid_era, 0); assert_eq!( Clusters::::get(cluster_id2).unwrap().props.erasure_coding_required, 16 ); assert_eq!(Clusters::::get(cluster_id2).unwrap().props.erasure_coding_total, 48); assert_eq!(Clusters::::get(cluster_id2).unwrap().props.replication_total, 20); - assert_eq!(Clusters::::get(cluster_id2).unwrap().last_validated_era_id, 0); + assert_eq!(Clusters::::get(cluster_id2).unwrap().last_paid_era, 0); }); } } diff --git a/pallets/ddc-clusters/src/tests.rs b/pallets/ddc-clusters/src/tests.rs index fc9b41e8d..d85f42288 100644 --- a/pallets/ddc-clusters/src/tests.rs +++ b/pallets/ddc-clusters/src/tests.rs @@ -622,7 +622,7 @@ fn set_last_validated_era_works() { assert_ok!(DdcClusters::set_last_paid_era(&cluster_id, era_id)); let updated_cluster = DdcClusters::clusters(cluster_id).unwrap(); - assert_eq!(updated_cluster.last_validated_era_id, era_id); + assert_eq!(updated_cluster.last_paid_era, era_id); // Checking that event was emitted assert_eq!(System::events().len(), 3); diff --git a/pallets/ddc-payouts/Cargo.toml b/pallets/ddc-payouts/Cargo.toml index 6613de0bf..83c8b4d04 100644 --- a/pallets/ddc-payouts/Cargo.toml +++ b/pallets/ddc-payouts/Cargo.toml @@ -29,7 +29,6 @@ sp-std = { workspace = true } # Cere dependencies ddc-primitives = { workspace = true } -polkadot-ckb-merkle-mountain-range = { workspace = true } [dev-dependencies] chrono = { workspace = true, default-features = true } @@ -42,7 +41,6 @@ substrate-test-utils = { workspace = true, default-features = true } [features] default = ["std"] std = [ - "polkadot-ckb-merkle-mountain-range/std", "hex/std", "codec/std", "ddc-primitives/std", diff --git a/pallets/ddc-staking/src/tests.rs b/pallets/ddc-staking/src/tests.rs index 164e70aff..e80eed4a5 100644 --- a/pallets/ddc-staking/src/tests.rs +++ b/pallets/ddc-staking/src/tests.rs @@ -772,7 +772,7 @@ fn bond_cluster_works() { replication_total: 0 }, status: ClusterStatus::Bonded, - last_validated_era_id: DdcEra::default() + last_paid_era: DdcEra::default() }) ); @@ -922,7 +922,7 @@ fn unbond_bonded_cluster_works() { replication_total: 0 }, status: ClusterStatus::Unbonding, - last_validated_era_id: DdcEra::default() + last_paid_era: DdcEra::default() }) ); @@ -1002,7 +1002,7 @@ fn unbond_activated_cluster_works() { replication_total: 0 }, status: ClusterStatus::Unbonding, - last_validated_era_id: DdcEra::default() + last_paid_era: DdcEra::default() }) ); @@ -1097,7 +1097,7 @@ fn withdraw_unbonded_cluster_works() { replication_total: 0 }, status: ClusterStatus::Unbonded, - last_validated_era_id: DdcEra::default() + last_paid_era: DdcEra::default() }) ); }); @@ -1184,7 +1184,7 @@ fn withdraw_activated_cluster_works() { replication_total: 0 }, status: ClusterStatus::Unbonded, - last_validated_era_id: DdcEra::default() + last_paid_era: DdcEra::default() }) ); }); diff --git a/primitives/src/traits/cluster.rs b/primitives/src/traits/cluster.rs index ad7ec9a4a..c13720877 100644 --- a/primitives/src/traits/cluster.rs +++ b/primitives/src/traits/cluster.rs @@ -98,14 +98,14 @@ pub trait ClusterManager: ClusterQuery { fn get_clusters(status: ClusterStatus) -> Result, DispatchError>; } pub trait ClusterValidator { - /// Updates the `last_validated_era_id` for the given cluster and emits an event indicating the + /// Updates the `last_paid_era` for the given cluster and emits an event indicating the /// update. /// /// # Parameters /// - /// - `cluster_id`: A reference to the unique identifier of the cluster that needs its last - /// validated era updated. - /// - `era_id`: The new era identifier to be set as the last validated era for the cluster. + /// - `cluster_id`: A reference to the unique identifier of the cluster that needs its last paid + /// era updated. + /// - `era_id`: The new era identifier to be set as the last paid era for the cluster. /// /// # Returns /// @@ -116,13 +116,13 @@ pub trait ClusterValidator { /// Emits `ClusterEraPaid` event if the operation is successful. fn set_last_paid_era(cluster_id: &ClusterId, era_id: DdcEra) -> Result<(), DispatchError>; - /// Retrieves the `last_validated_era_id` for the given cluster + /// Retrieves the `last_paid_era` for the given cluster /// update. /// /// # Parameters /// - /// - `cluster_id`: A reference to the unique identifier of the cluster the - /// `last_validated_era_id` is being retrieved for + /// - `cluster_id`: A reference to the unique identifier of the cluster the `last_paid_era` is + /// being retrieved for /// /// # Returns ///