diff --git a/Cargo.lock b/Cargo.lock index eb221b766..920cc8c64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5725,6 +5725,7 @@ dependencies = [ "hex", "log", "pallet-balances", + "pallet-ddc-verification", "parity-scale-codec", "scale-info", "sp-core", 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-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-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index bc3fe15d5..ae47d63f7 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -15,7 +15,7 @@ pub struct Cluster { pub reserve_id: AccountId, pub props: ClusterProps, pub status: ClusterStatus, - pub last_validated_era_id: DdcEra, + pub last_paid_era: DdcEra, } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Serialize, Deserialize)] @@ -44,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 c96f4e2d5..ed86010ea 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,28 +890,22 @@ 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; + cluster.last_paid_era = 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)?; - 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 56c9d8237..d85f42288 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); + assert_eq!(updated_cluster.last_paid_era, 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/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..76e4bb8cb 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, BucketUsage, ClusterId, }; use frame_support::{ parameter_types, @@ -67,12 +67,9 @@ pub struct Bucket { cluster_id: ClusterId, is_public: bool, is_removed: bool, - total_customers_usage: Option, -} - -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct BucketParams { - is_public: bool, + // 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)] @@ -241,8 +238,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 @@ -324,27 +321,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,33 +627,59 @@ 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, - ) -> 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); 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, - 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); @@ -687,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, @@ -707,6 +713,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 { @@ -715,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; @@ -750,7 +767,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-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-nodes/src/lib.rs b/pallets/ddc-nodes/src/lib.rs index 2a0e2fdc8..413268b0d 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::*; @@ -122,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(()) } @@ -160,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>; @@ -226,7 +234,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,17 +275,14 @@ 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, 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 eb3de5d31..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-payouts/Cargo.toml b/pallets/ddc-payouts/Cargo.toml index 02eab6699..83c8b4d04 100644 --- a/pallets/ddc-payouts/Cargo.toml +++ b/pallets/ddc-payouts/Cargo.toml @@ -33,6 +33,7 @@ ddc-primitives = { 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 } diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs deleted file mode 100644 index c3aa81ff1..000000000 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ /dev/null @@ -1,525 +0,0 @@ -//! DdcPayouts pallet benchmarking. - -use ddc_primitives::{ - traits::ValidatorVisitor, ClusterId, ClusterParams, ClusterProtocolParams, NodePubKey, -}; -pub use frame_benchmarking::{account, benchmarks, whitelist_account}; -use frame_system::RawOrigin; -use sp_runtime::{AccountId32, Perquintill}; -use sp_std::prelude::*; - -use super::*; -use crate::Pallet as DdcPayouts; - -const CERE: u128 = 10000000000; - -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()]); - dac_account -} - -fn create_account(name: &'static str, idx: u32, seed: u32) -> T::AccountId { - 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); -} - -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 + (30.44 * 24.0 * 3600.0) as i64; - - 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); -} - -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]); - 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 payers: Vec<(NodePubKey, BucketId, CustomerUsage)> = (0..b).map(|i| { - 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 = 1; - 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(); - - }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payers, MMRProof::default()) - 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 payees: Vec<(NodePubKey, NodeUsage)> = (0..b).map(|i| { - let provider = create_account::("provider", i, i); - endow_account::(&provider, T::Currency::minimum_balance().saturated_into()); - let 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 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()) - 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 2d45ea29c..33b50de92 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -14,31 +14,24 @@ #![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)] mod tests; +pub mod migrations; + +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::BillingReportParams; use ddc_primitives::{ traits::{ - bucket::BucketVisitor as BucketVisitorType, - cluster::{ClusterCreator as ClusterCreatorType, ClusterProtocol as ClusterProtocolType}, - customer::{ - CustomerCharger as CustomerChargerType, CustomerDepositor as CustomerDepositorType, - }, - node::NodeVisitor as NodeVisitorType, - pallet::PalletVisitor as PalletVisitorType, - payout::PayoutVisitor, + bucket::BucketManager, cluster::ClusterProtocol as ClusterProtocolType, + customer::CustomerCharger as CustomerChargerType, node::NodeManager, + 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, 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::{ @@ -53,14 +46,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 { @@ -70,15 +55,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; @@ -105,7 +81,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)] @@ -119,14 +95,11 @@ pub mod pallet { type PalletId: Get; type Currency: LockableCurrency>; type CustomerCharger: CustomerChargerType; - type BucketVisitor: BucketVisitorType; - type NodeVisitor: NodeVisitorType; - type CustomerDepositor: CustomerDepositorType; + type BucketManager: BucketManager; + type NodeManager: NodeManager; type TreasuryVisitor: PalletVisitorType; type ClusterProtocol: ClusterProtocolType>; type NominatorsAndValidatorsList: SortedListProvider; - type ClusterCreator: ClusterCreatorType>; - type WeightInfo: WeightInfo; type VoteScoreToU64: Convert, u64>; type ValidatorVisitor: ValidatorVisitor; type AccountIdConverter: From + Into; @@ -227,9 +200,6 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, }, - AuthorisedCaller { - authorised_caller: T::AccountId, - }, ChargeError { cluster_id: ClusterId, era: DdcEra, @@ -245,7 +215,7 @@ pub mod pallet { pub enum Error { BillingReportDoesNotExist, NotExpectedState, - Unauthorised, + Unauthorized, BatchIndexAlreadyProcessed, BatchIndexIsOutOfRange, BatchesMissed, @@ -276,10 +246,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 = @@ -341,36 +307,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 = ProviderReward::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: &BucketUsage, + 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 { - // 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, + 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 { - ensure_root(origin)?; // requires Governance approval + // Check if the Vec contains all integers between 1 and rewarding_max_batch_index + ensure!(!batches.is_empty(), Error::::BatchesMissed); - AuthorisedCaller::::put(authorised_caller.clone()); + ensure!((*max_batch_index + 1) as usize == batches.len(), Error::::BatchesMissed); - Self::deposit_event(Event::::AuthorisedCaller { authorised_caller }); + for index in 0..*max_batch_index + 1 { + ensure!(batches.contains(&index), Error::::BatchesMissed); + } Ok(()) } + } - #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::begin_billing_report())] - pub fn begin_billing_report( - origin: OriginFor, + 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 @@ -392,20 +584,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) @@ -422,21 +605,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, BucketUsage)], 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 @@ -466,19 +641,20 @@ pub mod pallet { era, batch_index, billing_report.charging_max_batch_index, - &payers, - &batch_proof + payers, + &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)?; + 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::( &cluster_id, - &customer_usage, + customer_usage, bucket_id, &customer_id, updated_billing_report.start_era, @@ -498,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, @@ -604,18 +780,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)?; @@ -708,20 +873,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) @@ -742,21 +899,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 @@ -784,7 +933,7 @@ pub mod pallet { era, batch_index, billing_report.rewarding_max_batch_index, - &payees, + payees, &batch_proof ), Error::::BatchValidationFailed @@ -794,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::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 @@ -884,18 +1033,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)?; @@ -937,18 +1075,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)?; @@ -966,366 +1093,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::BucketVisitor::get_total_customer_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); - } - } - - AuthorisedCaller::::set(self.authorised_caller.clone()); - 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( @@ -1338,8 +1118,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( @@ -1380,45 +1159,40 @@ 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); + #[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"); + } - for index in 0..*max_batch_index + 1 { - ensure!(batches.contains(&index), Error::::BatchesMissed); + 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"); } - Ok(()) + 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/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/mock.rs b/pallets/ddc-payouts/src/mock.rs index ed1ad03c4..20500868d 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -2,17 +2,15 @@ #![allow(dead_code)] +#[cfg(feature = "runtime-benchmarks")] +use ddc_primitives::BucketParams; use ddc_primitives::{ traits::{ - bucket::BucketVisitor, - cluster::{ClusterCreator, ClusterProtocol}, - customer::{CustomerCharger, CustomerDepositor}, - node::NodeVisitor, - 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, }; use frame_election_provider_support::SortedListProvider; use frame_support::{ @@ -128,34 +126,33 @@ impl crate::pallet::Config for Test { type PalletId = PayoutsPalletId; type Currency = Balances; type CustomerCharger = TestCustomerCharger; - type BucketVisitor = TestBucketVisitor; - type CustomerDepositor = TestCustomerDepositor; + type BucketManager = TestBucketManager; 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 +179,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; @@ -192,10 +199,6 @@ impl ValidatorVisitor for MockValidatorVisitor where ::AccountId: From, { - #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(_validators: Vec) { - unimplemented!() - } fn is_ocw_validator(caller: T::AccountId) -> bool { caller == VALIDATOR_OCW_KEY_32.into() } @@ -204,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 @@ -221,8 +224,8 @@ where } } -pub struct TestBucketVisitor; -impl BucketVisitor for TestBucketVisitor +pub struct TestBucketManager; +impl BucketManager for TestBucketManager where ::AccountId: From, { @@ -251,13 +254,32 @@ where } } - fn get_total_customer_usage( + fn get_total_bucket_usage( _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: &T::AccountId, - ) -> Result, DispatchError> { + ) -> Result, DispatchError> { Ok(None) } + + fn inc_total_bucket_usage( + _cluster_id: &ClusterId, + _bucket_id: BucketId, + _content_owner: T::AccountId, + _customer_usage: &BucketUsage, + ) -> 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; @@ -270,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; @@ -309,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-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 4e9a3b7f9..8932ff796 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, @@ -158,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, @@ -166,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 = @@ -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()), + let payers1 = vec![(node_key, bucket_id4, BucketUsage::default())]; + 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 @@ -272,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 @@ -298,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(); @@ -324,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; @@ -348,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 } @@ -378,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, @@ -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(), )); @@ -633,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, @@ -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(); @@ -836,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, @@ -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(), )); @@ -1096,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, @@ -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(), )); @@ -1356,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, @@ -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(), )); @@ -1615,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, @@ -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(), )); @@ -1875,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, @@ -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(), )); @@ -2135,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, @@ -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(), )); @@ -2386,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, @@ -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(), )); @@ -2464,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 = @@ -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 ); }) @@ -2559,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, @@ -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()); @@ -2740,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, @@ -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); @@ -2863,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 @@ -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, @@ -2996,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 @@ -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, @@ -3062,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 @@ -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 @@ -3222,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, @@ -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,) + ); }) } @@ -3558,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, @@ -3616,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(), @@ -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(), )); @@ -3863,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, @@ -3934,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 { @@ -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(), )); @@ -4178,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, @@ -4249,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), @@ -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(), )); @@ -4535,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), @@ -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(), )); @@ -4726,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 @@ -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 ); }) @@ -4896,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, @@ -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()); @@ -4993,120 +4544,70 @@ 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(); 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 ); }) @@ -5177,70 +4664,52 @@ 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!(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-payouts/src/weights.rs b/pallets/ddc-payouts/src/weights.rs deleted file mode 100644 index 063b7141c..000000000 --- a/pallets/ddc-payouts/src/weights.rs +++ /dev/null @@ -1,273 +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 set_authorised_caller() -> 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; -} - -/// 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 { - 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::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 { - 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::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) - // 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::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) - // 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::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 { - 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::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) - // 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::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 { - 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::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 { - 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::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 { - Weight::from_parts(26_119_000_u64, 0) - .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 { - Weight::from_parts(28_303_000_u64, 0) - .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) - // 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::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) - // 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::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 { - Weight::from_parts(29_155_000_u64, 0) - .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) - // 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::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 { - Weight::from_parts(29_986_000_u64, 0) - .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 { - 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-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..853abbc1a 100644 --- a/pallets/ddc-staking/src/testing_utils.rs +++ b/pallets/ddc-staking/src/testing_utils.rs @@ -1,16 +1,23 @@ //! 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; +#[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; @@ -47,6 +54,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, @@ -57,7 +65,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 { @@ -81,6 +89,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, @@ -94,7 +103,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-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/pallets/ddc-verification/src/benchmarking.rs b/pallets/ddc-verification/src/benchmarking.rs index 6695674e5..f7f0aa2b2 100644 --- a/pallets/ddc-verification/src/benchmarking.rs +++ b/pallets/ddc-verification/src/benchmarking.rs @@ -1,10 +1,21 @@ #![cfg(feature = "runtime-benchmarks")] -use frame_benchmarking::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_128, blake2_256}; +use sp_runtime::{ + traits::AccountIdConversion, AccountId32, Perquintill, SaturatedConversion, Saturating, +}; use sp_std::vec; use super::*; +use crate::EraActivity; #[allow(unused)] use crate::Pallet as DdcVerification; @@ -12,26 +23,827 @@ use crate::Pallet as DdcVerification; mod benchmarks { use super::*; + fn create_validator_account() -> T::AccountId { + let validator = create_account::("validator_account", 0, 0); + ValidatorToStashKey::::insert(&validator.clone(), &validator.clone()); + ValidatorSet::::put(vec![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, 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); + } + + #[allow(clippy::too_many_arguments)] + 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::(); + let cluster_id = ClusterId::from([1; 20]); + 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] + set_prepare_era_for_payout( + RawOrigin::Signed(validator), + cluster_id, + era.clone(), + payers_merkle_root_hash, + payees_merkle_root_hash, + payers_batch_merkle_root_hashes, + payees_batch_merkle_root_hashes, + ); + + 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, 10000000000000000); + + 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()]); + + #[extrinsic_call] + set_validator_key(RawOrigin::Signed(validator.clone()), validator.clone()); + + assert!(>::contains_key(validator.clone())); + 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, BucketUsage)> = 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 = BucketUsage { + 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 create_billing_reports() { + fn end_rewarding_providers() { let cluster_id = ClusterId::from([1; 20]); - let era: DdcEra = 1; - let merkel_root_hash: MmrRootHash = array_bytes::hex_n_into_unchecked( - "95803defe6ea9f41e7ec6afa497064f21bfded027d8812efacbdf984e630cbdc", + 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, ); - let caller: T::AccountId = whitelisted_caller(); + #[extrinsic_call] - create_billing_reports( - RawOrigin::Signed(caller), + 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, - merkel_root_hash, + 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, ); - 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)); + #[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); } - impl_benchmark_test_suite!(DdcVerification, crate::mock::new_test_ext(), crate::mock::Test); + #[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); + } + + #[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); + } } diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index d6dc6ac5c..63d92ca26 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -12,48 +12,52 @@ 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, NodeVisitor, PayoutVisitor, + ClusterManager, ClusterValidator, CustomerVisitor, NodeManager, PayoutProcessor, ValidatorVisitor, }, - ActivityHash, BatchIndex, ClusterId, ClusterStatus, CustomerUsage, DdcEra, MMRProof, - NodeParams, NodePubKey, NodeUsage, PayoutState, StorageNodeParams, + ActivityHash, BatchIndex, BucketUsage, ClusterId, ClusterStatus, DdcEra, EraValidation, + EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage, PayoutState, + StorageNodeParams, }; 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 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")] +pub mod benchmarking; + #[cfg(test)] pub(crate) mod mock; #[cfg(test)] @@ -69,6 +73,9 @@ pub mod proto { mod signature; +pub(crate) type BalanceOf = + <::Currency as Currency<::AccountId>>::Balance; + #[frame_support::pallet] pub mod pallet { @@ -107,9 +114,9 @@ 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 NodeVisitor: NodeVisitor; + type NodeManager: NodeManager; /// The output of the `ActivityHasher` function. type ActivityHash: Member + Parameter @@ -142,9 +149,19 @@ 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< + AccountId = Self::AccountId, + Balance = BalanceOf, + >; 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. @@ -457,7 +474,7 @@ pub mod pallet { /// Bad requests. BadRequest, /// Not a validator. - Unauthorised, + Unauthorized, /// Already signed era. AlreadySignedEra, NotExpectedState, @@ -511,27 +528,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, @@ -562,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, } @@ -1884,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( @@ -1894,7 +1889,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, _, _, _)) = @@ -1960,7 +1955,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(( @@ -2025,12 +2020,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(); @@ -2077,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, @@ -2100,9 +2097,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)); } @@ -2124,7 +2121,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)) = @@ -2217,7 +2214,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)) = @@ -2276,12 +2273,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(); @@ -2351,9 +2350,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)); } @@ -2367,7 +2366,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)); @@ -2478,7 +2477,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 { @@ -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 } })?; @@ -3244,7 +3243,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 +3412,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::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, @@ -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); @@ -3510,6 +3509,176 @@ 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::::Unauthorized); + + 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::::Unauthorized); + 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, BucketUsage)>, + batch_proof: MMRProof, + ) -> DispatchResult { + let sender = ensure_signed(origin)?; + ensure!(Self::is_ocw_validator(sender.clone()), Error::::Unauthorized); + 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::::Unauthorized); + 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::::Unauthorized); + 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::::Unauthorized); + 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::::Unauthorized); + 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::::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); + + T::ClusterValidator::set_last_paid_era(&cluster_id, era_id) + } + /// Emit consensus errors. /// /// The origin must be a validator. @@ -3519,14 +3688,14 @@ 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::call_index(10)] + #[pallet::weight(::WeightInfo::emit_consensus_errors(errors.len() as u32))] pub fn emit_consensus_errors( origin: OriginFor, 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 { @@ -3764,185 +3933,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::create_billing_reports())] // todo! implement weights - pub fn set_validator_key( - origin: OriginFor, - ddc_validator_pub: T::AccountId, - ) -> DispatchResult { - let controller = ensure_signed(origin)?; - - let stash = T::StakingVisitor::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::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::begin_billing_report(sender, 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::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::begin_charging_customers(sender, cluster_id, era_id, max_batch_index) - } - - #[pallet::call_index(5)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - // todo! remove clippy::too_many_arguments - 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::PayoutVisitor::send_charging_customers_batch( - sender, - cluster_id, - era_id, - batch_index, - &payers, - batch_proof, - ) - } - - #[pallet::call_index(6)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::end_charging_customers(sender, cluster_id, era_id) - } - - #[pallet::call_index(7)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::begin_rewarding_providers( - sender, - cluster_id, - era_id, - max_batch_index, - total_node_usage, - ) - } - - #[pallet::call_index(8)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::send_rewarding_providers_batch( - sender, - cluster_id, - era_id, - batch_index, - &payees, - batch_proof, - ) - } - - #[pallet::call_index(9)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::end_rewarding_providers(sender, cluster_id, era_id) - } - - #[pallet::call_index(10)] - #[pallet::weight(::WeightInfo::create_billing_reports())] // todo! implement weights - 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::PayoutVisitor::end_billing_report(sender, 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(::WeightInfo::create_billing_reports())] // todo! implement weights + #[pallet::weight(::WeightInfo::set_era_validations())] pub fn set_era_validations( origin: OriginFor, cluster_id: ClusterId, @@ -3980,11 +3972,6 @@ pub mod pallet { } impl ValidatorVisitor for Pallet { - #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(validators: Vec) { - ValidatorSet::::put(validators); - } - fn is_ocw_validator(caller: T::AccountId) -> bool { if ValidatorToStashKey::::contains_key(caller.clone()) { >::get().contains(&caller) @@ -3998,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 0d79e64e8..48154207e 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, @@ -222,8 +228,8 @@ impl crate::Config for Test { type WeightInfo = (); type ClusterManager = TestClusterManager; type ClusterValidator = TestClusterValidator; - type NodeVisitor = MockNodeVisitor; - type PayoutVisitor = MockPayoutVisitor; + type NodeManager = MockNodeManager; + type PayoutProcessor = MockPayoutProcessor; type AuthorityId = sr25519::AuthorityId; type OffchainIdentifierId = crypto::OffchainIdentifierId; type ActivityHasher = sp_runtime::traits::BlakeTwo256; @@ -234,10 +240,17 @@ 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; + 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; @@ -250,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: &BucketUsage, + ) -> 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 = @@ -342,124 +418,109 @@ 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()) } } -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)], + _payers: &[(NodePubKey, BucketId, BucketUsage)], _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 + } + + #[cfg(feature = "runtime-benchmarks")] + fn create_billing_report(_vault: T::AccountId, _params: BillingReportParams) { + unimplemented!() } } -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 +663,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 +674,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/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/pallets/ddc-verification/src/weights.rs b/pallets/ddc-verification/src/weights.rs index 6603465a4..4d217b25d 100644 --- a/pallets/ddc-verification/src/weights.rs +++ b/pallets/ddc-verification/src/weights.rs @@ -1,27 +1,22 @@ //! 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: `bench`, CPU: `AMD EPYC-Milan Processor` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: -// ./target/release/cere +// ./target/debug/cere // benchmark // pallet -// --chain -// dev -// --wasm-execution=compiled -// --pallet -// pallet_ddc_verification -// --extrinsic -// * -// --steps -// 50 -// --repeat -// 20 -// --output=./pallets/ddc-verification/src/weights.rs +// --chain=dev +// --execution=wasm +// --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 +27,387 @@ 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; + fn set_era_validations() -> 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(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)) + } + // 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(614_541_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(858_688_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(692_927_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(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)) + .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(6_076_476_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(695_822_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(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)) + .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(703_757_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(1_343_575_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(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) + // 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(498_523_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: `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(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)) + } + // 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(614_541_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(858_688_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(692_927_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(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)) + .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(6_076_476_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(695_822_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(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)) + .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(703_757_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(1_343_575_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(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) + // 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(498_523_000_u64, 0) + .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index b8518e384..3c59c3f17 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}; @@ -31,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 { @@ -265,18 +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 usage of node provider +/// 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 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 a node #[derive( PartialEq, Eq, Encode, Decode, Debug, TypeInfo, Default, Clone, Serialize, Deserialize, )] @@ -287,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 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 + 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, @@ -317,6 +337,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 +387,39 @@ 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, +} + +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/bucket.rs b/primitives/src/traits/bucket.rs index c9396fba1..db087f324 100644 --- a/primitives/src/traits/bucket.rs +++ b/primitives/src/traits/bucket.rs @@ -1,22 +1,29 @@ use sp_runtime::{DispatchError, DispatchResult}; -use crate::{BucketId, ClusterId, CustomerUsage}; - +#[cfg(feature = "runtime-benchmarks")] +use crate::BucketParams; +use crate::{BucketId, BucketUsage, ClusterId}; 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, + customer_usage: &BucketUsage, ) -> 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/cluster.rs b/primitives/src/traits/cluster.rs index efc95d807..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 /// @@ -113,19 +113,19 @@ 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 + /// 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 /// /// 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; } 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/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/payout.rs b/primitives/src/traits/payout.rs index 075845962..351617e6f 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -1,58 +1,44 @@ 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, }; -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, 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, max_batch_index: BatchIndex, ) -> DispatchResult; - // todo! factor out into PayoutProcessor fn send_charging_customers_batch( - origin: T::AccountId, cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(NodePubKey, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, BucketUsage)], 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, 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, batch_index: BatchIndex, @@ -60,21 +46,11 @@ pub trait PayoutVisitor { 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_id: 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_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; @@ -89,4 +65,7 @@ pub trait PayoutVisitor { 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 a5e817228..954b1b1bf 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -1,21 +1,17 @@ use frame_system::Config; -#[cfg(feature = "runtime-benchmarks")] -use scale_info::prelude::vec::Vec; use crate::{ - BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, + BatchIndex, BucketId, BucketUsage, ClusterId, DdcEra, MMRProof, NodePubKey, NodeUsage, }; pub trait ValidatorVisitor { - #[cfg(feature = "runtime-benchmarks")] - fn setup_validators(validators: Vec); fn is_ocw_validator(caller: T::AccountId) -> bool; fn is_customers_batch_valid( cluster_id: ClusterId, 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( diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 196d81fab..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, }; @@ -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; @@ -1185,8 +1169,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,16 +1223,13 @@ impl pallet_ddc_payouts::Config for Runtime { type PalletId = PayoutsPalletId; type Currency = Balances; type CustomerCharger = DdcCustomers; - type BucketVisitor = DdcCustomers; - type CustomerDepositor = DdcCustomers; + type BucketManager = DdcCustomers; type ClusterProtocol = DdcClusters; 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 NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type AccountIdConverter = AccountId32; } @@ -1296,14 +1276,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,8 +1311,8 @@ 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 PayoutVisitor = pallet_ddc_payouts::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; + type PayoutProcessor = pallet_ddc_payouts::Pallet; type AuthorityId = ddc_primitives::sr25519::AuthorityId; type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; type ActivityHasher = BlakeTwo256; @@ -1345,10 +1323,17 @@ 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; + 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!( @@ -1448,6 +1433,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. @@ -1460,22 +1446,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, @@ -1514,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] @@ -1525,6 +1494,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 72f4dfbff..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, }; @@ -1225,16 +1225,13 @@ impl pallet_ddc_payouts::Config for Runtime { type PalletId = PayoutsPalletId; type Currency = Balances; type CustomerCharger = DdcCustomers; - type BucketVisitor = DdcCustomers; - type CustomerDepositor = DdcCustomers; + type BucketManager = DdcCustomers; type ClusterProtocol = DdcClusters; 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 NodeVisitor = pallet_ddc_nodes::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; type AccountIdConverter = AccountId32; } @@ -1250,8 +1247,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 +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; } @@ -1337,8 +1331,8 @@ 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 PayoutVisitor = pallet_ddc_payouts::Pallet; + type NodeManager = pallet_ddc_nodes::Pallet; + type PayoutProcessor = pallet_ddc_payouts::Pallet; type AuthorityId = ddc_primitives::sr25519::AuthorityId; type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; type ActivityHasher = BlakeTwo256; @@ -1349,10 +1343,17 @@ 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; + 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!( @@ -1469,9 +1470,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, @@ -1505,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] @@ -1531,6 +1538,7 @@ mod benches { [pallet_whitelist, Whitelist] [pallet_collective, TechComm] [pallet_ddc_clusters_gov, DdcClustersGov] + [pallet_ddc_verification, DdcVerification] ); }