From b2f51555686597cd4800a143cc75226e34be78fb Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Wed, 30 Oct 2024 18:03:04 +0100 Subject: [PATCH 01/20] wip: verifying customers batch hash in merkle path --- .vscode/settings.json | 2 +- pallets/ddc-clusters/src/lib.rs | 17 +++ pallets/ddc-payouts/src/lib.rs | 6 +- pallets/ddc-payouts/src/mock.rs | 2 +- pallets/ddc-verification/src/lib.rs | 162 +++++++++++++++------------ pallets/ddc-verification/src/mock.rs | 3 +- primitives/src/traits/payout.rs | 4 +- primitives/src/traits/validator.rs | 3 +- runtime/cere-dev/src/lib.rs | 4 +- 9 files changed, 118 insertions(+), 85 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 0196fd738..7089a4652 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", - "editor.formatOnSave": true + "editor.formatOnSave": false } } \ No newline at end of file diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index c96f4e2d5..22f40ad21 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -430,6 +430,23 @@ pub mod pallet { Self::do_join_cluster(cluster, node_pub_key) } + + #[pallet::call_index(6)] + #[pallet::weight(::WeightInfo::validate_node())] + pub fn set_last_validated_era( + origin: OriginFor, + cluster_id: ClusterId, + era: DdcEra, + ) -> DispatchResult { + ensure_root(origin)?; + + let mut cluster = + Clusters::::try_get(cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; + + cluster.last_validated_era_id = era; + Clusters::::insert(cluster_id, cluster); + Ok(()) + } } impl Pallet { diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index e7ffc16d0..c0f979776 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -431,7 +431,7 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payers: Vec<(T::AccountId, BucketId, CustomerUsage)>, + payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let caller = ensure_signed(origin)?; @@ -472,7 +472,7 @@ pub mod pallet { ); let mut updated_billing_report = billing_report; - for (customer_id, bucket_id, customer_usage) in payers { + for (customer_id, node_id, bucket_id, customer_usage) in payers { log::info!("๐Ÿญsend_charging_customers_batch get_customer_charge customer_id: {:?} - bucket_id: {:?} - era:{:?} - cluster-id:{:?}", Self::get_account_id_string(customer_id.clone()), bucket_id, era, cluster_id); let mut customer_charge = get_customer_charge::( &cluster_id, @@ -1220,7 +1220,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, BucketId, CustomerUsage)], + payers: &[(T::AccountId, String, BucketId, CustomerUsage)], batch_proof: MMRProof, ) -> DispatchResult { log::info!( diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index bf62e5ef4..48318d747 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -187,7 +187,7 @@ where _cluster_id: ClusterId, _era: DdcEra, _batch_index: BatchIndex, - _payers: &[(T::AccountId, BucketId, CustomerUsage)], + _payers: &[(T::AccountId, String, BucketId, CustomerUsage)], _batch_proof: &MMRProof, ) -> bool { true diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index f1d3adcc1..1bdb6058f 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -561,7 +561,7 @@ pub mod pallet { pub struct CustomerBatch { pub(crate) batch_index: BatchIndex, - pub(crate) payers: Vec<(T::AccountId, BucketId, CustomerUsage)>, + pub(crate) payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)>, pub(crate) batch_proof: MMRProof, } @@ -1123,23 +1123,24 @@ pub mod pallet { // todo! factor out as macro as this is repetitive match Self::prepare_send_charging_customers_batch(&cluster_id, batch_size.into()) { Ok(Some((era_id, batch_payout))) => { - let payers_log: Vec<(String, BucketId, CustomerUsage)> = batch_payout - .payers - .clone() - .into_iter() - .map(|(acc_id, bucket_id, customer_usage)| { - let account_id: T::AccountIdConverter = acc_id.into(); - let account_id_32: AccountId32 = account_id.into(); - let account_ref: &[u8; 32] = account_id_32.as_ref(); - (hex::encode(account_ref), bucket_id, customer_usage) - }) - .collect(); + let payers_log: Vec<(String, String, BucketId, CustomerUsage)> = + batch_payout + .payers + .clone() + .into_iter() + .map(|(acc_id, node_id, bucket_id, customer_usage)| { + let account_id: T::AccountIdConverter = acc_id.into(); + let account_id_32: AccountId32 = account_id.into(); + let account_ref: &[u8; 32] = account_id_32.as_ref(); + (hex::encode(account_ref), node_id, bucket_id, customer_usage) + }) + .collect(); log::info!( - "๐Ÿญ๐ŸŽ prepare_send_charging_customers_batch processed successfully for cluster_id: {:?}, era_id: {:?} , batch_payout: {:?}", - cluster_id, - era_id, - payers_log - ); + "๐Ÿญ๐ŸŽ prepare_send_charging_customers_batch processed successfully for cluster_id: {:?}, era_id: {:?} , batch_payout: {:?}", + cluster_id, + era_id, + payers_log + ); if let Some((_, res)) = signer.send_signed_transaction(|_acc| { Call::send_charging_customers_batch { @@ -2181,7 +2182,7 @@ pub mod pallet { PayoutState::ChargingCustomers { if let Some(( - bucket_nodes_activity_in_consensus, + customers_total_activity, _, customers_activity_batch_roots, _, @@ -2194,7 +2195,7 @@ pub mod pallet { cluster_id, batch_size, era_id, - bucket_nodes_activity_in_consensus, + customers_total_activity, customers_activity_batch_roots, ) } else { @@ -2203,7 +2204,7 @@ pub mod pallet { let _ = Self::process_dac_era(cluster_id, Some(era_activity), batch_size)?; if let Some(( - bucket_nodes_activity_in_consensus, + customers_total_activity, _, customers_activity_batch_roots, _, @@ -2216,7 +2217,7 @@ pub mod pallet { cluster_id, batch_size, era_id, - bucket_nodes_activity_in_consensus, + customers_total_activity, customers_activity_batch_roots, ) } else { @@ -2235,7 +2236,7 @@ pub mod pallet { cluster_id: &ClusterId, batch_size: usize, era_id: DdcEra, - bucket_nodes_activity_in_consensus: Vec, + customers_total_activity: Vec, customers_activity_batch_roots: Vec, ) -> Result)>, Vec> { let batch_index = T::PayoutVisitor::get_next_customer_batch_for_payment( @@ -2249,7 +2250,7 @@ pub mod pallet { let i: usize = index.into(); // todo! store batched activity to avoid splitting it again each time let customers_activity_batched = - Self::split_to_batches(&bucket_nodes_activity_in_consensus, batch_size); + Self::split_to_batches(&customers_total_activity, batch_size); let batch_root = customers_activity_batch_roots[i]; let store = MemStore::default(); @@ -2294,13 +2295,14 @@ pub mod pallet { let account_id = T::CustomerVisitor::get_bucket_owner(&activity.bucket_id) .unwrap(); + let node_id = activity.node_id.clone(); let customer_usage = CustomerUsage { transferred_bytes: activity.transferred_bytes, stored_bytes: activity.stored_bytes, number_of_puts: activity.number_of_puts, number_of_gets: activity.number_of_gets, }; - (account_id, activity.bucket_id, customer_usage) + (account_id, node_id, activity.bucket_id, customer_usage) }) .collect(), batch_proof, @@ -2344,20 +2346,14 @@ pub mod pallet { if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == PayoutState::CustomersChargedWithFees { - if let Some(( - _, - _, - _, - nodes_activity_in_consensus, - _, - nodes_activity_batch_roots, - )) = Self::fetch_validation_activities::( - cluster_id, era_id, - ) { + if let Some((_, _, _, nodes_total_activity, _, nodes_activity_batch_roots)) = + Self::fetch_validation_activities::( + cluster_id, era_id, + ) { Self::fetch_reward_activities( cluster_id, era_id, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_batch_roots, nodes_total_usage, ) @@ -2370,7 +2366,7 @@ pub mod pallet { _, _, _, - nodes_activity_in_consensus, + nodes_total_activity, _, nodes_activity_batch_roots, )) = Self::fetch_validation_activities::( @@ -2379,7 +2375,7 @@ pub mod pallet { Self::fetch_reward_activities( cluster_id, era_id, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_batch_roots, nodes_total_usage, ) @@ -2398,7 +2394,7 @@ pub mod pallet { pub(crate) fn fetch_reward_activities( cluster_id: &ClusterId, era_id: DdcEra, - nodes_activity_in_consensus: Vec, + nodes_total_activity: Vec, nodes_activity_batch_roots: Vec, current_nodes_total_usage: i64, ) -> Result, Vec> { @@ -2416,7 +2412,7 @@ pub mod pallet { number_of_gets: 0, }; - for activity in nodes_activity_in_consensus { + for activity in nodes_total_activity { total_node_usage.transferred_bytes += activity.transferred_bytes; total_node_usage.stored_bytes += activity.stored_bytes; total_node_usage.number_of_puts += activity.number_of_puts; @@ -2439,21 +2435,15 @@ pub mod pallet { if T::PayoutVisitor::get_billing_report_status(cluster_id, era_id) == PayoutState::RewardingProviders { - if let Some(( - _, - _, - _, - nodes_activity_in_consensus, - _, - nodes_activity_batch_roots, - )) = Self::fetch_validation_activities::( - cluster_id, era_id, - ) { + if let Some((_, _, _, nodes_total_activity, _, nodes_activity_batch_roots)) = + Self::fetch_validation_activities::( + cluster_id, era_id, + ) { Self::fetch_reward_provider_batch( cluster_id, batch_size, era_id, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_batch_roots, ) } else { @@ -2465,7 +2455,7 @@ pub mod pallet { _, _, _, - nodes_activity_in_consensus, + nodes_total_activity, _, nodes_activity_batch_roots, )) = Self::fetch_validation_activities::( @@ -2475,7 +2465,7 @@ pub mod pallet { cluster_id, batch_size, era_id, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_batch_roots, ) } else { @@ -2494,7 +2484,7 @@ pub mod pallet { cluster_id: &ClusterId, batch_size: usize, era_id: DdcEra, - nodes_activity_in_consensus: Vec, + nodes_total_activity: Vec, nodes_activity_batch_roots: Vec, ) -> Result)>, Vec> { let batch_index = T::PayoutVisitor::get_next_provider_batch_for_payment( @@ -2508,7 +2498,7 @@ pub mod pallet { let i: usize = index.into(); // todo! store batched activity to avoid splitting it again each time let nodes_activity_batched = - Self::split_to_batches(&nodes_activity_in_consensus, batch_size); + Self::split_to_batches(&nodes_total_activity, batch_size); let batch_root = nodes_activity_batch_roots[i]; let store = MemStore::default(); @@ -2668,19 +2658,19 @@ pub mod pallet { // todo! (3) add tests cluster_id: &ClusterId, era_id: DdcEra, - bucket_nodes_activity_in_consensus: &[A], + customers_total_activity: &[A], customers_activity_root: ActivityHash, customers_activity_batch_roots: &[ActivityHash], - nodes_activity_in_consensus: &[B], + nodes_total_activity: &[B], nodes_activity_root: ActivityHash, nodes_activity_batch_roots: &[ActivityHash], ) { let key = Self::derive_key(cluster_id, era_id); let encoded_tuple = ( - bucket_nodes_activity_in_consensus, + customers_total_activity, customers_activity_root, customers_activity_batch_roots, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_root, nodes_activity_batch_roots, ) @@ -2754,17 +2744,17 @@ pub mod pallet { // Attempt to decode tuple from bytes match Decode::decode(&mut &encoded_tuple[..]) { Ok(( - bucket_nodes_activity_in_consensus, + customers_total_activity, customers_activity_root, customers_activity_batch_roots, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_root, nodes_activity_batch_roots, )) => Some(( - bucket_nodes_activity_in_consensus, + customers_total_activity, customers_activity_root, customers_activity_batch_roots, - nodes_activity_in_consensus, + nodes_total_activity, nodes_activity_root, nodes_activity_batch_roots, )), @@ -2840,13 +2830,13 @@ pub mod pallet { pub(crate) fn convert_to_batch_merkle_roots( cluster_id: &ClusterId, era_id: DdcEra, - activities: Vec>, + batches: Vec>, ) -> Result, OCWError> { - activities + batches .into_iter() - .map(|inner_vec| { + .map(|batch| { let activity_hashes: Vec = - inner_vec.into_iter().map(|a| a.hash::()).collect(); + batch.into_iter().map(|a| a.hash::()).collect(); Self::create_merkle_root(cluster_id, era_id, &activity_hashes).map_err(|_| { OCWError::FailedToCreateMerkleRoot { cluster_id: *cluster_id, era_id } }) @@ -3695,8 +3685,10 @@ pub mod pallet { ensure!(!signed_validators.contains(&caller.clone()), Error::::AlreadySignedEra); signed_validators.push(caller.clone()); - let percent = Percent::from_percent(T::MAJORITY); - let threshold = percent * >::get().len(); + // let percent = Percent::from_percent(T::MAJORITY); + // let threshold = percent * >::get().len(); + + let threshold = 1; let mut should_deposit_ready_event = false; if threshold <= signed_validators.len() { @@ -4075,7 +4067,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: Vec<(T::AccountId, BucketId, CustomerUsage)>, + payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -4233,18 +4225,40 @@ pub mod pallet { fn is_customers_batch_valid( cluster_id: ClusterId, era_id: DdcEra, - _batch_index: BatchIndex, - _payers: &[(T::AccountId, BucketId, CustomerUsage)], + batch_index: BatchIndex, + payers: &[(T::AccountId, String, BucketId, CustomerUsage)], batch_proof: &MMRProof, ) -> bool { let validation_era = EraValidations::::get(cluster_id, era_id); match validation_era { Some(valid_era) => { - //Self::create_merkle_root(leaves) + let activity_hashes = payers + .into_iter() + .map(|(bucket_owner, node_id, bucket_id, usage)| { + let mut data = bucket_id.encode(); + 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()); + T::ActivityHasher::hash(&data).into() + }) + .collect::>(); - let root = valid_era.payers_merkle_root_hash; - Self::proof_merkle_leaf(root, batch_proof).unwrap_or(false) + let batch_hash = + Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) + .expect("batch_hash to be created"); + let batch_position = batch_proof.leaf_with_position.0; + + let root_hash = valid_era.payers_merkle_root_hash; + + let proof: MerkleProof = + MerkleProof::new(batch_proof.mmr_size, batch_proof.proof.clone()); + proof + .verify(root_hash, vec![(batch_position, batch_hash)]) + .map_err(|_| Error::::FailToVerifyMerkleProof) + .unwrap_or(false) }, None => false, } diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index c774d3c61..dff1508d5 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -17,6 +17,7 @@ use frame_support::{ }; use frame_system::mocking::MockBlock; use pallet_staking::BalanceOf; +use scale_info::prelude::string::String; use sp_core::{ByteArray, H256}; use sp_runtime::{ curve::PiecewiseLinear, @@ -405,7 +406,7 @@ impl PayoutVisitor for MockPayoutVisitor { _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, - _payers: &[(T::AccountId, BucketId, CustomerUsage)], + _payers: &[(T::AccountId, String, BucketId, CustomerUsage)], _batch_proof: MMRProof, ) -> DispatchResult { Ok(()) diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index e8c6a6351..1919a1835 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -1,10 +1,10 @@ +use scale_info::prelude::string::String; use sp_runtime::DispatchResult; use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodeUsage, PayoutError, PayoutState, }; - pub trait PayoutProcessor {} pub trait PayoutVisitor { @@ -31,7 +31,7 @@ pub trait PayoutVisitor { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, BucketId, CustomerUsage)], + payers: &[(T::AccountId, String, BucketId, CustomerUsage)], batch_proof: MMRProof, ) -> DispatchResult; diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index 773c5586d..04b6f2f6d 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -1,4 +1,5 @@ use frame_system::Config; +use scale_info::prelude::string::String; #[cfg(feature = "runtime-benchmarks")] use sp_std::prelude::*; @@ -12,7 +13,7 @@ pub trait ValidatorVisitor { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, BucketId, CustomerUsage)], + payers: &[(T::AccountId, String, BucketId, CustomerUsage)], 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 b57a7cfd3..9170626a2 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -156,7 +156,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 61002, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 21, + transaction_version: 22, state_version: 0, }; @@ -1339,7 +1339,7 @@ impl pallet_ddc_verification::Config for Runtime { type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; type ActivityHasher = BlakeTwo256; const MAJORITY: u8 = 67; - const BLOCK_TO_START: u16 = 30; // every 30 blocks + const BLOCK_TO_START: u16 = 10; // every 10 blocks const DAC_REDUNDANCY_FACTOR: u16 = 3; type AggregatorsQuorum = MajorityOfAggregators; const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE; From acbb6a47e50165cdbfa3019efc327b9376555929 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 1 Nov 2024 13:59:27 +0100 Subject: [PATCH 02/20] fix: checking MMR proof for a batch --- pallets/ddc-payouts/src/lib.rs | 9 +++--- pallets/ddc-payouts/src/mock.rs | 2 +- pallets/ddc-verification/src/lib.rs | 41 ++++++++++++++++++++++------ pallets/ddc-verification/src/mock.rs | 2 +- primitives/src/lib.rs | 3 +- primitives/src/traits/payout.rs | 2 +- primitives/src/traits/validator.rs | 2 +- runtime/cere-dev/src/lib.rs | 2 +- 8 files changed, 44 insertions(+), 19 deletions(-) diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index c0f979776..fce5d7f91 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -749,8 +749,9 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payees: Vec<(T::AccountId, NodeUsage)>, /* todo! we need to pass NodePubKey inside - * NodeUsage and more provider_id */ + payees: Vec<(T::AccountId, String, NodeUsage)>, /* todo! we need to pass NodePubKey + * inside + * NodeUsage and more provider_id */ batch_proof: MMRProof, ) -> DispatchResult { let caller = ensure_signed(origin)?; @@ -790,7 +791,7 @@ pub mod pallet { let max_dust = MaxDust::get().saturated_into::>(); let mut updated_billing_report = billing_report.clone(); - for (node_provider_id, delta_node_usage) in payees { + for (node_provider_id, node_id, delta_node_usage) in payees { // todo! deduce node_provider_id from delta_node_usage.node_id // todo! get T::NodeVisitor::get_total_usage(delta_node_usage.node_id).stored_bytes let mut total_node_stored_bytes: i64 = 0; @@ -1277,7 +1278,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, NodeUsage)], + payees: &[(T::AccountId, String, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult { log::info!( diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 48318d747..d720f85ed 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -197,7 +197,7 @@ where _cluster_id: ClusterId, _era: DdcEra, _batch_index: BatchIndex, - _payees: &[(T::AccountId, NodeUsage)], + _payees: &[(T::AccountId, String, NodeUsage)], _batch_proof: &MMRProof, ) -> bool { true diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 1bdb6058f..661bbf56c 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -30,6 +30,7 @@ use frame_system::{ }; pub use pallet::*; use polkadot_ckb_merkle_mountain_range::{ + helper::leaf_index_to_pos, util::{MemMMR, MemStore}, MerkleProof, MMR, }; @@ -567,7 +568,7 @@ pub mod pallet { pub struct ProviderBatch { pub(crate) batch_index: BatchIndex, - pub(crate) payees: Vec<(T::AccountId, NodeUsage)>, + pub(crate) payees: Vec<(T::AccountId, String, NodeUsage)>, pub(crate) batch_proof: MMRProof, } @@ -2544,14 +2545,14 @@ pub mod pallet { .iter() .map(|activity| { let node_id = activity.clone().node_id; - let provider_id = Self::fetch_provider_id(node_id).unwrap(); // todo! remove unwrap + let provider_id = Self::fetch_provider_id(node_id.clone()).unwrap(); // todo! remove unwrap let node_usage = NodeUsage { transferred_bytes: activity.transferred_bytes, stored_bytes: activity.stored_bytes, number_of_puts: activity.number_of_puts, number_of_gets: activity.number_of_gets, }; - (provider_id, node_usage) + (provider_id, node_id, node_usage) }) .collect(), batch_proof, @@ -4121,7 +4122,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: Vec<(T::AccountId, NodeUsage)>, + payees: Vec<(T::AccountId, String, NodeUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -4249,7 +4250,7 @@ pub mod pallet { let batch_hash = Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) .expect("batch_hash to be created"); - let batch_position = batch_proof.leaf_with_position.0; + let batch_position = leaf_index_to_pos(batch_index.into()); // batch_proof.leaf_with_position.0; let root_hash = valid_era.payers_merkle_root_hash; @@ -4268,16 +4269,38 @@ pub mod pallet { fn is_providers_batch_valid( cluster_id: ClusterId, era_id: DdcEra, - _batch_index: BatchIndex, - _payees: &[(T::AccountId, NodeUsage)], + batch_index: BatchIndex, + payees: &[(T::AccountId, String, NodeUsage)], batch_proof: &MMRProof, ) -> bool { let validation_era = EraValidations::::get(cluster_id, era_id); match validation_era { Some(valid_era) => { - let root = valid_era.payees_merkle_root_hash; - Self::proof_merkle_leaf(root, batch_proof).unwrap_or(false) + let activity_hashes = payees + .into_iter() + .map(|(node_provider, node_id, usage)| { + let mut data = 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()); + T::ActivityHasher::hash(&data).into() + }) + .collect::>(); + + let batch_hash = + Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) + .expect("batch_hash to be created"); + let batch_position = leaf_index_to_pos(batch_index.into()); // batch_proof.leaf_with_position.0; + + let root_hash = valid_era.payees_merkle_root_hash; + let proof: MerkleProof = + MerkleProof::new(batch_proof.mmr_size, batch_proof.proof.clone()); + proof + .verify(root_hash, vec![(batch_position, batch_hash)]) + .map_err(|_| Error::::FailToVerifyMerkleProof) + .unwrap_or(false) }, None => false, } diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index dff1508d5..218477b2f 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -435,7 +435,7 @@ impl PayoutVisitor for MockPayoutVisitor { _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, - _payees: &[(T::AccountId, NodeUsage)], + _payees: &[(T::AccountId, String, NodeUsage)], _batch_proof: MMRProof, ) -> DispatchResult { Ok(()) diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 2487b086c..5039971b3 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -20,7 +20,8 @@ parameter_types! { } pub const MAX_PAYOUT_BATCH_COUNT: u16 = 1000; -pub const MAX_PAYOUT_BATCH_SIZE: u16 = 1000; +// pub const MAX_PAYOUT_BATCH_SIZE: u16 = 1000; +pub const MAX_PAYOUT_BATCH_SIZE: u16 = 2; pub const MILLICENTS: u128 = 100_000; pub const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent. pub const DOLLARS: u128 = 100 * CENTS; diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index 1919a1835..abbea88d3 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -57,7 +57,7 @@ pub trait PayoutVisitor { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, NodeUsage)], + payees: &[(T::AccountId, String, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult; diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index 04b6f2f6d..58fb58e89 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -20,7 +20,7 @@ pub trait ValidatorVisitor { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, NodeUsage)], + payees: &[(T::AccountId, String, NodeUsage)], batch_proof: &MMRProof, ) -> bool; } diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 9170626a2..4aa9f4801 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1339,7 +1339,7 @@ impl pallet_ddc_verification::Config for Runtime { type OffchainIdentifierId = ddc_primitives::crypto::OffchainIdentifierId; type ActivityHasher = BlakeTwo256; const MAJORITY: u8 = 67; - const BLOCK_TO_START: u16 = 10; // every 10 blocks + const BLOCK_TO_START: u16 = 30; // every 30 blocks const DAC_REDUNDANCY_FACTOR: u16 = 3; type AggregatorsQuorum = MajorityOfAggregators; const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE; From 29f4f8d6489f6c08ee5b49061f2b4255eb8e41b4 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Fri, 1 Nov 2024 19:40:31 +0100 Subject: [PATCH 03/20] fix: tests and benchmarks are fixed --- pallets/ddc-clusters/src/lib.rs | 17 -- pallets/ddc-payouts/src/benchmarking.rs | 10 +- pallets/ddc-payouts/src/lib.rs | 4 +- pallets/ddc-payouts/src/tests.rs | 258 ++++++++++++++++-------- pallets/ddc-verification/src/lib.rs | 16 +- pallets/ddc-verification/src/tests.rs | 4 +- primitives/src/lib.rs | 3 +- 7 files changed, 188 insertions(+), 124 deletions(-) diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 22f40ad21..c96f4e2d5 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -430,23 +430,6 @@ pub mod pallet { Self::do_join_cluster(cluster, node_pub_key) } - - #[pallet::call_index(6)] - #[pallet::weight(::WeightInfo::validate_node())] - pub fn set_last_validated_era( - origin: OriginFor, - cluster_id: ClusterId, - era: DdcEra, - ) -> DispatchResult { - ensure_root(origin)?; - - let mut cluster = - Clusters::::try_get(cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; - - cluster.last_validated_era_id = era; - Clusters::::insert(cluster_id, cluster); - Ok(()) - } } impl Pallet { diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs index 6481dd71e..bb41ad735 100644 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ b/pallets/ddc-payouts/src/benchmarking.rs @@ -223,7 +223,7 @@ benchmarks! { }); let batch_index: BatchIndex = 0; - let payers: Vec<(T::AccountId, BucketId, CustomerUsage)> = (0..b).map(|i| { + let payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)> = (0..b).map(|i| { let customer = create_account::("customer", i, i); if b % 2 == 0 { @@ -241,8 +241,9 @@ benchmarks! { number_of_puts: 5, // 5 puts }; let bucket_id: BucketId = 1; + let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - (customer, bucket_id, customer_usage) + (customer, node_id, bucket_id, customer_usage) }).collect(); }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payers, MMRProof::default()) @@ -396,7 +397,7 @@ benchmarks! { whitelist_account!(dac_account); let batch_index: BatchIndex = 0; - let payees: Vec<(T::AccountId, NodeUsage)> = (0..b).map(|i| { + let payees: Vec<(T::AccountId, String, 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 { @@ -405,7 +406,8 @@ benchmarks! { number_of_gets: 10, // 10 gets number_of_puts: 5, // 5 puts }; - (provider, node_usage) + let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + (provider, node_id, node_usage) }).collect(); }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payees, MMRProof::default()) diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index fce5d7f91..20504fc68 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -472,7 +472,7 @@ pub mod pallet { ); let mut updated_billing_report = billing_report; - for (customer_id, node_id, bucket_id, customer_usage) in payers { + for (customer_id, _node_id, bucket_id, customer_usage) in payers { log::info!("๐Ÿญsend_charging_customers_batch get_customer_charge customer_id: {:?} - bucket_id: {:?} - era:{:?} - cluster-id:{:?}", Self::get_account_id_string(customer_id.clone()), bucket_id, era, cluster_id); let mut customer_charge = get_customer_charge::( &cluster_id, @@ -791,7 +791,7 @@ pub mod pallet { let max_dust = MaxDust::get().saturated_into::>(); let mut updated_billing_report = billing_report.clone(); - for (node_provider_id, node_id, delta_node_usage) in payees { + for (node_provider_id, _node_id, delta_node_usage) in payees { // todo! deduce node_provider_id from delta_node_usage.node_id // todo! get T::NodeVisitor::get_total_usage(delta_node_usage.node_id).stored_bytes let mut total_node_stored_bytes: i64 = 0; diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 3a67aecb5..b2a994d08 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -210,8 +210,10 @@ fn send_charging_customers_batch_fails_uninitialised() { number_of_gets: 100, number_of_puts: 200, }; - let payers1 = vec![(user1, bucket_id1, customer_usage)]; - let payers2 = vec![(user2.clone(), bucket_id2, CustomerUsage::default())]; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let payers1 = vec![(user1, node_id.clone(), bucket_id1, customer_usage)]; + let payers2 = vec![(user2.clone(), node_id.clone(), bucket_id2, CustomerUsage::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 @@ -284,7 +286,7 @@ fn send_charging_customers_batch_fails_uninitialised() { max_batch_index, )); - let payers1 = vec![(user2, bucket_id2, CustomerUsage::default())]; + let payers1 = vec![(user2, node_id, bucket_id2, CustomerUsage::default())]; assert_ok!(DdcPayouts::send_charging_customers_batch( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -417,6 +419,8 @@ fn send_charging_customers_batch_works() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -454,11 +458,11 @@ fn send_charging_customers_batch_works() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; 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 = @@ -675,6 +679,8 @@ fn end_charging_customers_works_small_usage_1_hour() { let user6 = AccountId::from([6; 32]); let user7 = AccountId::from([7; 32]); let cluster_id = HIGH_FEES_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -693,9 +699,10 @@ fn end_charging_customers_works_small_usage_1_hour() { number_of_puts: 0, number_of_gets: 0, }; + let payers1 = vec![ - (user6.clone(), bucket_id6, usage6.clone()), - (user7.clone(), bucket_id7, usage7.clone()), + (user6.clone(), node_id.clone(), bucket_id6, usage6.clone()), + (user7.clone(), node_id.clone(), bucket_id7, usage7.clone()), ]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -912,12 +919,15 @@ fn send_charging_customers_batch_works_for_day() { number_of_puts: 3456345, number_of_gets: 242334563456423, }; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id.clone(), bucket_id3, usage3.clone())]; 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 = @@ -1136,6 +1146,8 @@ fn send_charging_customers_batch_works_for_day_free_storage() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = STORAGE_ZERO_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1173,11 +1185,12 @@ fn send_charging_customers_batch_works_for_day_free_storage() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id.clone(), bucket_id3, usage3.clone())]; 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 = @@ -1396,6 +1409,8 @@ fn send_charging_customers_batch_works_for_day_free_stream() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = STREAM_ZERO_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1433,11 +1448,11 @@ fn send_charging_customers_batch_works_for_day_free_stream() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; 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 = @@ -1656,6 +1671,8 @@ fn send_charging_customers_batch_works_for_day_free_get() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = GET_ZERO_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1692,12 +1709,13 @@ fn send_charging_customers_batch_works_for_day_free_get() { number_of_puts: 3456345, number_of_gets: 242334563456423, }; + let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; 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 = @@ -1916,6 +1934,8 @@ fn send_charging_customers_batch_works_for_day_free_put() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = PUT_ZERO_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1952,12 +1972,13 @@ fn send_charging_customers_batch_works_for_day_free_put() { number_of_puts: 3456345, number_of_gets: 242334563456423, }; + let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; 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 = @@ -2176,6 +2197,8 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = STORAGE_STREAM_ZERO_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -2213,11 +2236,11 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), bucket_id2, usage2.clone()), - (user4.clone(), bucket_id4, usage4.clone()), + (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), + (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; 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 = @@ -2433,6 +2456,8 @@ fn send_charging_customers_batch_works_zero_fees() { let dac_account = AccountId::from([123; 32]); let user5 = AccountId::from([5; 32]); let cluster_id = ONE_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -2444,7 +2469,8 @@ fn send_charging_customers_batch_works_zero_fees() { number_of_puts: 1, number_of_gets: 1, }; - let payers5 = vec![(user5, bucket_id5, usage5.clone())]; + + let payers5 = vec![(user5, node_id.clone(), bucket_id5, usage5.clone())]; 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 = @@ -2512,11 +2538,14 @@ fn end_charging_customers_fails_uninitialised() { let dac_account = AccountId::from([123; 32]); let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 2; let batch_index = 1; let bucket_id1: BucketId = 1; - let payers = vec![(user1, bucket_id1, CustomerUsage::default())]; + + let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::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 = @@ -2605,6 +2634,8 @@ fn end_charging_customers_works() { let dac_account = AccountId::from([123; 32]); let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -2615,7 +2646,7 @@ fn end_charging_customers_works() { number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let payers = vec![(user1.clone(), bucket_id1, usage1.clone())]; + let payers = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -2788,6 +2819,8 @@ fn end_charging_customers_works_zero_fees() { let dac_account = AccountId::from([123; 32]); let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::zero(); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -2798,7 +2831,7 @@ fn end_charging_customers_works_zero_fees() { number_of_puts: 1, number_of_gets: 1, }; - let payers = vec![(user1.clone(), bucket_id1, usage1.clone())]; + let payers = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -2919,7 +2952,9 @@ fn begin_rewarding_providers_fails_uninitialised() { let max_batch_index = 2; let batch_index = 1; let bucket_id1: BucketId = 1; - let payers = vec![(user1, bucket_id1, CustomerUsage::default())]; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; let node_usage = NodeUsage::default(); let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -3054,8 +3089,10 @@ fn begin_rewarding_providers_works() { let max_batch_index = 0; let batch_index = 0; let bucket_id1: BucketId = 1; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let total_node_usage = NodeUsage::default(); - let payers = vec![(user1, bucket_id1, CustomerUsage::default())]; + let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::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 @@ -3122,14 +3159,18 @@ fn send_rewarding_providers_batch_fails_uninitialised() { let user2 = AccountId::from([4; 32]); let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 1; let batch_index = 0; let bucket_id1: BucketId = 1; let bucket_id2: BucketId = 2; - let payers1 = vec![(user1, bucket_id1, CustomerUsage::default())]; - let payers2 = vec![(user2, bucket_id2, CustomerUsage::default())]; - let payees = vec![(node1, NodeUsage::default())]; + + let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; + + let payees = vec![(node1, node_id, NodeUsage::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 @@ -3287,6 +3328,8 @@ fn send_rewarding_providers_batch_works() { let node2 = AccountId::from([11; 32]); let node3 = AccountId::from([12; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let max_node_batch_index = 1; @@ -3339,10 +3382,12 @@ fn send_rewarding_providers_batch_works() { node_usage3.number_of_gets, }; - let payers = vec![(user1, bucket_id1, usage1)]; - let payees1 = - vec![(node1.clone(), node_usage1.clone()), (node2.clone(), node_usage2.clone())]; - let payees2 = vec![(node3.clone(), node_usage3.clone())]; + let payers = vec![(user1, node_id.clone(), bucket_id1, usage1)]; + let payees1 = vec![ + (node1.clone(), node_id.clone(), node_usage1.clone()), + (node2.clone(), node_id.clone(), node_usage2.clone()), + ]; + let payees2 = vec![(node3.clone(), node_id, node_usage3.clone())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -3583,6 +3628,8 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let user_batch_size = 10; let node_batch_size = 10; @@ -3626,8 +3673,8 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { number_of_gets: usage1.number_of_gets * 2, }; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); for i in 10..10 + num_nodes { let node_usage = match i % 3 { @@ -3641,7 +3688,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_usage)); + node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -3652,8 +3699,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<(AccountId, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); for user_id in 100u8..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::one(), @@ -3680,7 +3727,12 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push((AccountId::from([user_id; 32]), bucketid1, user_usage)); + user_batch.push(( + AccountId::from([user_id; 32]), + node_id.clone(), + bucketid1, + user_usage, + )); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -3715,7 +3767,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { MMRProof::default(), )); - for (customer_id, _bucket_id, usage) in batch.iter() { + for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); System::assert_has_event( @@ -3796,7 +3848,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { )); let mut batch_charge = 0; - for (node1, node_usage1) in batch.iter() { + for (node1, _node_id, node_usage1) in batch.iter() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -3854,6 +3906,8 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let user_batch_size = 10; let node_batch_size = 10; @@ -3892,8 +3946,8 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { number_of_gets: usage1.number_of_gets * 2, }; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); for i in 10u8..10 + num_nodes { let ratio = match i % 5 { @@ -3920,7 +3974,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_usage)); + node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -3931,8 +3985,9 @@ 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<(AccountId, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); + for user_id in 100u8..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::from_float(1_000_000.0), @@ -3959,7 +4014,12 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push((AccountId::from([user_id; 32]), bucket_id, user_usage)); + user_batch.push(( + AccountId::from([user_id; 32]), + node_id.clone(), + bucket_id, + user_usage, + )); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -3994,7 +4054,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { MMRProof::default(), )); - for (customer_id, _bucket_id, usage) in batch.iter() { + for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); System::assert_has_event( @@ -4075,7 +4135,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { )); let mut batch_charge = 0; - for (node1, node_usage1) in batch.iter() { + for (node1, _node_id, node_usage1) in batch.iter() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -4133,6 +4193,8 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; + let node_id: String = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let bucketid1: BucketId = 1; let user_batch_size = 10; @@ -4170,8 +4232,8 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { number_of_gets: usage1.number_of_gets * 2, }; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); for i in 10u8..10 + num_nodes { let ratio = match i % 5 { @@ -4198,7 +4260,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_usage)); + node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -4209,8 +4271,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<(AccountId, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); for user_id in 100u8..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::from_float(1_000_000.0), @@ -4237,7 +4299,12 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push((AccountId::from([user_id; 32]), bucketid1, user_usage)); + user_batch.push(( + AccountId::from([user_id; 32]), + node_id.clone(), + bucketid1, + user_usage, + )); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -4272,7 +4339,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { MMRProof::default(), )); - for (customer_id, _bucket_id, usage) in batch.iter() { + for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); System::assert_has_event( @@ -4353,7 +4420,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { )); let mut batch_charge = 0; - for (node1, node_usage1) in batch.iter() { + for (node1, _node_id, node_usage1) in batch.iter() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -4421,14 +4488,16 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = CERE_CLUSTER_ID; + let node_id: String = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let user_batch_size = 10; let node_batch_size = 10; let mut batch_user_index = 0; let mut batch_node_index = 0; let bucket_id1: BucketId = 1; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); for i in 10u8..10 + num_nodes { let node_usage = NodeUsage { @@ -4443,7 +4512,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_usage)); + node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -4454,8 +4523,8 @@ 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<(AccountId, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); for user_id in 100u8..100 + num_users { let user_usage = CustomerUsage { transferred_bytes: generate_random_u64(&mock_randomness, min, max), @@ -4474,7 +4543,12 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push((AccountId::from([user_id; 32]), bucket_id1, user_usage)); + user_batch.push(( + AccountId::from([user_id; 32]), + node_id.clone(), + bucket_id1, + user_usage, + )); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -4509,7 +4583,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { MMRProof::default(), )); - for (customer_id, _bucket_id, usage) in batch.iter() { + for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); System::assert_has_event( @@ -4590,7 +4664,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { )); let mut batch_charge = 0; - for (node1, node_usage1) in batch.iter() { + for (node1, _node_id, node_usage1) in batch.iter() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -4641,14 +4715,16 @@ fn end_rewarding_providers_fails_uninitialised() { let user2 = AccountId::from([4; 32]); let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id: String = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 1; let batch_index = 0; let bucket_id1: BucketId = 1; let bucket_id2: BucketId = 2; - let payers1 = vec![(user1, bucket_id1, CustomerUsage::default())]; - let payers2 = vec![(user2, bucket_id2, CustomerUsage::default())]; - let payees = vec![(node1, NodeUsage::default())]; + let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; + let payees = vec![(node1, node_id, NodeUsage::default())]; let total_node_usage = NodeUsage::default(); let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -4818,6 +4894,8 @@ fn end_rewarding_providers_works() { let user1 = AccountId::from([1; 32]); let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -4837,8 +4915,8 @@ fn end_rewarding_providers_works() { number_of_gets: usage1.number_of_gets * 2 / 3, }; let total_node_usage = node_usage1.clone(); - let payers = vec![(user1, bucket_id1, usage1)]; - let payees = vec![(node1, node_usage1)]; + let payers = vec![(user1, node_id.clone(), bucket_id1, usage1)]; + let payees = vec![(node1, node_id, node_usage1)]; assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); @@ -4920,14 +4998,16 @@ fn end_billing_report_fails_uninitialised() { let user2 = AccountId::from([4; 32]); let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 1; let batch_index = 0; let bucket_id1: BucketId = 1; let bucket_id2: BucketId = 2; - let payers1 = vec![(user1, bucket_id1, CustomerUsage::default())]; - let payers2 = vec![(user2, bucket_id2, CustomerUsage::default())]; - let payees = vec![(node1, NodeUsage::default())]; + let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; + let payees = vec![(node1, node_id, NodeUsage::default())]; let total_node_usage = NodeUsage::default(); assert_noop!( @@ -5101,13 +5181,15 @@ fn end_billing_report_works() { let user1 = AccountId::from([3; 32]); let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); + let node_id = + String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); let era = 100; let max_batch_index = 0; let batch_index = 0; let total_node_usage = NodeUsage::default(); let bucket_id1 = 1; - let payers = vec![(user1, bucket_id1, CustomerUsage::default())]; - let payees = vec![(node1, NodeUsage::default())]; + let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + let payees = vec![(node1, node_id, NodeUsage::default())]; assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 661bbf56c..fa64a5e9e 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -2924,7 +2924,7 @@ pub mod pallet { /// Parameters: /// - `root`: merkle root /// - `leaf`: Leaf of the tree - pub(crate) fn proof_merkle_leaf( + pub(crate) fn _proof_merkle_leaf( root: ActivityHash, batch_proof: &MMRProof, ) -> Result> { @@ -3686,10 +3686,8 @@ pub mod pallet { ensure!(!signed_validators.contains(&caller.clone()), Error::::AlreadySignedEra); signed_validators.push(caller.clone()); - // let percent = Percent::from_percent(T::MAJORITY); - // let threshold = percent * >::get().len(); - - let threshold = 1; + let percent = Percent::from_percent(T::MAJORITY); + let threshold = percent * >::get().len(); let mut should_deposit_ready_event = false; if threshold <= signed_validators.len() { @@ -4235,8 +4233,8 @@ pub mod pallet { match validation_era { Some(valid_era) => { let activity_hashes = payers - .into_iter() - .map(|(bucket_owner, node_id, bucket_id, usage)| { + .iter() + .map(|(_bucket_owner, node_id, bucket_id, usage)| { let mut data = bucket_id.encode(); data.extend_from_slice(&node_id.encode()); data.extend_from_slice(&usage.stored_bytes.encode()); @@ -4278,8 +4276,8 @@ pub mod pallet { match validation_era { Some(valid_era) => { let activity_hashes = payees - .into_iter() - .map(|(node_provider, node_id, usage)| { + .iter() + .map(|(_node_provider, node_id, usage)| { let mut data = node_id.encode(); data.extend_from_slice(&usage.stored_bytes.encode()); data.extend_from_slice(&usage.transferred_bytes.encode()); diff --git a/pallets/ddc-verification/src/tests.rs b/pallets/ddc-verification/src/tests.rs index add71a6f4..284cfd366 100644 --- a/pallets/ddc-verification/src/tests.rs +++ b/pallets/ddc-verification/src/tests.rs @@ -2520,7 +2520,7 @@ fn proof_merkle_leaf_works() { assert_eq!(leaf_position.len(), 1); assert_eq!(position.len(), 1); - assert!(DdcVerification::proof_merkle_leaf( + assert!(DdcVerification::_proof_merkle_leaf( root, &MMRProof { mmr_size: mmr.mmr_size(), @@ -2531,7 +2531,7 @@ fn proof_merkle_leaf_works() { .unwrap()); assert_noop!( - DdcVerification::proof_merkle_leaf( + DdcVerification::_proof_merkle_leaf( root, &MMRProof { mmr_size: 0, diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 5039971b3..2487b086c 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -20,8 +20,7 @@ parameter_types! { } pub const MAX_PAYOUT_BATCH_COUNT: u16 = 1000; -// pub const MAX_PAYOUT_BATCH_SIZE: u16 = 1000; -pub const MAX_PAYOUT_BATCH_SIZE: u16 = 2; +pub const MAX_PAYOUT_BATCH_SIZE: u16 = 1000; pub const MILLICENTS: u128 = 100_000; pub const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent. pub const DOLLARS: u128 = 100 * CENTS; From 29b2c1bf0055c12e3445d81260b2d21cef172fdd Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Mon, 4 Nov 2024 15:32:41 +0100 Subject: [PATCH 04/20] chore: hash and position of the leaf is removed from tx parameters --- pallets/ddc-verification/src/lib.rs | 48 ++++++++++----------------- pallets/ddc-verification/src/tests.rs | 23 ++++++++----- primitives/src/lib.rs | 1 - 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index fa64a5e9e..0cfdcbe67 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -2281,11 +2281,7 @@ pub mod pallet { .proof_items() .to_vec(); - let batch_proof = MMRProof { - mmr_size: mmr.mmr_size(), - proof, - leaf_with_position: leaf_position[0], - }; + let batch_proof = MMRProof { mmr_size: mmr.mmr_size(), proof }; Ok(Some(( era_id, CustomerBatch { @@ -2532,11 +2528,7 @@ pub mod pallet { // todo! attend [i] through get(i).ok_or() // todo! attend accountid conversion - let batch_proof = MMRProof { - mmr_size: mmr.mmr_size(), - proof, - leaf_with_position: leaf_position[0], - }; + let batch_proof = MMRProof { mmr_size: mmr.mmr_size(), proof }; Ok(Some(( era_id, ProviderBatch { @@ -2922,16 +2914,21 @@ pub mod pallet { /// Verify whether leaf is part of tree /// /// Parameters: - /// - `root`: merkle root - /// - `leaf`: Leaf of the tree - pub(crate) fn _proof_merkle_leaf( - root: ActivityHash, + /// - `root_hash`: merkle root hash + /// - `batch_hash`: hash of the batch + /// - `batch_index`: index of the batch + /// - `batch_proof`: MMR proofs + pub(crate) fn proof_merkle_leaf( + root_hash: ActivityHash, + batch_hash: ActivityHash, + batch_index: BatchIndex, batch_proof: &MMRProof, ) -> Result> { + let batch_position = leaf_index_to_pos(batch_index.into()); let proof: MerkleProof = MerkleProof::new(batch_proof.mmr_size, batch_proof.proof.clone()); proof - .verify(root, vec![batch_proof.leaf_with_position]) + .verify(root_hash, vec![(batch_position, batch_hash)]) .map_err(|_| Error::::FailToVerifyMerkleProof) } @@ -4232,6 +4229,8 @@ pub mod pallet { match validation_era { Some(valid_era) => { + let root_hash = valid_era.payers_merkle_root_hash; + let activity_hashes = payers .iter() .map(|(_bucket_owner, node_id, bucket_id, usage)| { @@ -4248,15 +4247,8 @@ pub mod pallet { let batch_hash = Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) .expect("batch_hash to be created"); - let batch_position = leaf_index_to_pos(batch_index.into()); // batch_proof.leaf_with_position.0; - let root_hash = valid_era.payers_merkle_root_hash; - - let proof: MerkleProof = - MerkleProof::new(batch_proof.mmr_size, batch_proof.proof.clone()); - proof - .verify(root_hash, vec![(batch_position, batch_hash)]) - .map_err(|_| Error::::FailToVerifyMerkleProof) + Self::proof_merkle_leaf(root_hash, batch_hash, batch_index, batch_proof) .unwrap_or(false) }, None => false, @@ -4275,6 +4267,8 @@ pub mod pallet { match validation_era { Some(valid_era) => { + let root_hash = valid_era.payees_merkle_root_hash; + let activity_hashes = payees .iter() .map(|(_node_provider, node_id, usage)| { @@ -4290,14 +4284,8 @@ pub mod pallet { let batch_hash = Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) .expect("batch_hash to be created"); - let batch_position = leaf_index_to_pos(batch_index.into()); // batch_proof.leaf_with_position.0; - let root_hash = valid_era.payees_merkle_root_hash; - let proof: MerkleProof = - MerkleProof::new(batch_proof.mmr_size, batch_proof.proof.clone()); - proof - .verify(root_hash, vec![(batch_position, batch_hash)]) - .map_err(|_| Error::::FailToVerifyMerkleProof) + Self::proof_merkle_leaf(root_hash, batch_hash, batch_index, batch_proof) .unwrap_or(false) }, None => false, diff --git a/pallets/ddc-verification/src/tests.rs b/pallets/ddc-verification/src/tests.rs index 284cfd366..14194ecfc 100644 --- a/pallets/ddc-verification/src/tests.rs +++ b/pallets/ddc-verification/src/tests.rs @@ -2516,27 +2516,34 @@ fn proof_merkle_leaf_works() { .map(|&(ref l, p)| (p, *l)) .collect(); let position: Vec = leaf_position.clone().into_iter().map(|(p, _)| p).collect(); - let root = mmr.get_root().unwrap(); + let root_hash = mmr.get_root().unwrap(); assert_eq!(leaf_position.len(), 1); assert_eq!(position.len(), 1); - assert!(DdcVerification::_proof_merkle_leaf( - root, + + let leave_index = 2; + let leaf_hash = c; + assert!(DdcVerification::proof_merkle_leaf( + root_hash, + leaf_hash, + leave_index, &MMRProof { mmr_size: mmr.mmr_size(), proof: mmr.gen_proof(position.clone()).unwrap().proof_items().to_vec(), - leaf_with_position: leaf_position[0] } ) .unwrap()); + let leave_index = 5; + let leaf_hash = f; assert_noop!( - DdcVerification::_proof_merkle_leaf( - root, + DdcVerification::proof_merkle_leaf( + root_hash, + leaf_hash, + leave_index, &MMRProof { - mmr_size: 0, + mmr_size: mmr.mmr_size(), proof: mmr.gen_proof(position).unwrap().proof_items().to_vec(), - leaf_with_position: (6, f) } ), Error::::FailToVerifyMerkleProof diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 2487b086c..afca88b44 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -291,7 +291,6 @@ pub struct NodeUsage { pub struct MMRProof { pub mmr_size: u64, pub proof: Vec, - pub leaf_with_position: (u64, ActivityHash), } #[derive(Debug, PartialEq)] From c054878f50c82651cdb90a0465099b3af46ab2c2 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 5 Nov 2024 02:27:22 +0100 Subject: [PATCH 05/20] chore: fetching node provider from the node data during payout --- pallets/ddc-payouts/src/benchmarking.rs | 15 +- pallets/ddc-payouts/src/lib.rs | 13 +- pallets/ddc-payouts/src/mock.rs | 82 ++++++++- pallets/ddc-payouts/src/tests.rs | 220 +++++++++++++++++++----- pallets/ddc-verification/src/lib.rs | 71 ++------ pallets/ddc-verification/src/mock.rs | 2 +- primitives/src/traits/payout.rs | 6 +- primitives/src/traits/validator.rs | 6 +- 8 files changed, 294 insertions(+), 121 deletions(-) diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs index bb41ad735..f8f282a7a 100644 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ b/pallets/ddc-payouts/src/benchmarking.rs @@ -1,9 +1,11 @@ //! DdcPayouts pallet benchmarking. -use ddc_primitives::{traits::ValidatorVisitor, ClusterId, ClusterParams, ClusterProtocolParams}; +use ddc_primitives::{ + traits::ValidatorVisitor, ClusterId, ClusterParams, ClusterProtocolParams, NodePubKey, +}; pub use frame_benchmarking::{account, benchmarks, whitelist_account}; use frame_system::RawOrigin; -use sp_runtime::Perquintill; +use sp_runtime::{AccountId32, Perquintill}; use sp_std::prelude::*; use super::*; @@ -397,7 +399,7 @@ benchmarks! { whitelist_account!(dac_account); let batch_index: BatchIndex = 0; - let payees: Vec<(T::AccountId, String, NodeUsage)> = (0..b).map(|i| { + 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 { @@ -406,8 +408,11 @@ benchmarks! { number_of_gets: 10, // 10 gets number_of_puts: 5, // 5 puts }; - let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - (provider, node_id, node_usage) + 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()) diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 20504fc68..ec5343a8e 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -38,7 +38,8 @@ use ddc_primitives::{ payout::PayoutVisitor, }, BatchIndex, BucketId, BucketVisitorError, ClusterId, CustomerUsage, DdcEra, MMRProof, - NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, MILLICENTS, + NodePubKey, NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, + MILLICENTS, }; use frame_election_provider_support::SortedListProvider; use frame_support::{ @@ -749,9 +750,7 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payees: Vec<(T::AccountId, String, NodeUsage)>, /* todo! we need to pass NodePubKey - * inside - * NodeUsage and more provider_id */ + payees: Vec<(NodePubKey, NodeUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let caller = ensure_signed(origin)?; @@ -791,9 +790,9 @@ pub mod pallet { let max_dust = MaxDust::get().saturated_into::>(); let mut updated_billing_report = billing_report.clone(); - for (node_provider_id, _node_id, delta_node_usage) in payees { - // todo! deduce node_provider_id from delta_node_usage.node_id + 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 mut total_node_stored_bytes: i64 = 0; total_node_stored_bytes = total_node_stored_bytes @@ -1278,7 +1277,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, String, NodeUsage)], + payees: &[(NodePubKey, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult { log::info!( diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index d720f85ed..af28518ea 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -29,7 +29,7 @@ use sp_io::TestExternalities; use sp_runtime::TryRuntimeError; use sp_runtime::{ traits::{BlakeTwo256, IdentifyAccount, Identity, IdentityLookup, Verify}, - BuildStorage, DispatchError, MultiSignature, Perquintill, + AccountId32, BuildStorage, DispatchError, MultiSignature, Perquintill, }; use sp_std::prelude::*; @@ -157,8 +157,30 @@ where fn exists(_node_pub_key: &NodePubKey) -> bool { unimplemented!() } - fn get_node_provider_id(_node_pub_key: &NodePubKey) -> Result { - unimplemented!() + fn get_node_provider_id(pub_key: &NodePubKey) -> Result { + match pub_key { + NodePubKey::StoragePubKey(key) if key == &NODE1_PUB_KEY_32 => + Ok(NODE_PROVIDER1_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE2_PUB_KEY_32 => + Ok(NODE_PROVIDER2_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE3_PUB_KEY_32 => + Ok(NODE_PROVIDER3_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE4_PUB_KEY_32 => + Ok(NODE_PROVIDER4_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE5_PUB_KEY_32 => + Ok(NODE_PROVIDER5_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE6_PUB_KEY_32 => + Ok(NODE_PROVIDER6_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE7_PUB_KEY_32 => + Ok(NODE_PROVIDER7_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE8_PUB_KEY_32 => + Ok(NODE_PROVIDER8_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE9_PUB_KEY_32 => + Ok(NODE_PROVIDER9_KEY_32.clone().into()), + NodePubKey::StoragePubKey(key) if key == &NODE10_PUB_KEY_32 => + Ok(NODE_PROVIDER10_KEY_32.clone().into()), + _ => Ok(AccountId32::new([0; 32]).clone().into()), + } } fn get_node_params(_node_pub_key: &NodePubKey) -> Result { unimplemented!() @@ -197,7 +219,7 @@ where _cluster_id: ClusterId, _era: DdcEra, _batch_index: BatchIndex, - _payees: &[(T::AccountId, String, NodeUsage)], + _payees: &[(NodePubKey, NodeUsage)], _batch_proof: &MMRProof, ) -> bool { true @@ -311,6 +333,58 @@ pub const PARTIAL_CHARGE: u128 = 10; pub const USER2_BALANCE: u128 = 5; pub const USER3_BALANCE: u128 = 1000; +pub const NODE1_PUB_KEY_32: AccountId32 = AccountId32::new([ + 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, +]); +pub const NODE2_PUB_KEY_32: AccountId32 = AccountId32::new([ + 49, 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, +]); +pub const NODE3_PUB_KEY_32: AccountId32 = AccountId32::new([ + 50, 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, +]); +pub const NODE4_PUB_KEY_32: AccountId32 = AccountId32::new([ + 51, 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, +]); +pub const NODE5_PUB_KEY_32: AccountId32 = AccountId32::new([ + 52, 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, +]); +pub const NODE6_PUB_KEY_32: AccountId32 = AccountId32::new([ + 53, 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, +]); +pub const NODE7_PUB_KEY_32: AccountId32 = AccountId32::new([ + 54, 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, +]); +pub const NODE8_PUB_KEY_32: AccountId32 = AccountId32::new([ + 55, 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, +]); +pub const NODE9_PUB_KEY_32: AccountId32 = AccountId32::new([ + 56, 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, +]); +pub const NODE10_PUB_KEY_32: AccountId32 = AccountId32::new([ + 57, 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, +]); + +pub const NODE_PROVIDER1_KEY_32: AccountId32 = AccountId32::new([10; 32]); +pub const NODE_PROVIDER2_KEY_32: AccountId32 = AccountId32::new([11; 32]); +pub const NODE_PROVIDER3_KEY_32: AccountId32 = AccountId32::new([12; 32]); +pub const NODE_PROVIDER4_KEY_32: AccountId32 = AccountId32::new([13; 32]); +pub const NODE_PROVIDER5_KEY_32: AccountId32 = AccountId32::new([14; 32]); +pub const NODE_PROVIDER6_KEY_32: AccountId32 = AccountId32::new([15; 32]); +pub const NODE_PROVIDER7_KEY_32: AccountId32 = AccountId32::new([16; 32]); +pub const NODE_PROVIDER8_KEY_32: AccountId32 = AccountId32::new([17; 32]); +pub const NODE_PROVIDER9_KEY_32: AccountId32 = AccountId32::new([18; 32]); +pub const NODE_PROVIDER10_KEY_32: AccountId32 = AccountId32::new([19; 32]); + pub const NO_FEE_CLUSTER_ID: ClusterId = ClusterId::zero(); pub const ONE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(4u8); pub const CERE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(10u8); diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index b2a994d08..f22303bc1 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -4,7 +4,7 @@ use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; use ddc_primitives::ClusterId; use frame_support::{assert_noop, assert_ok, error::BadOrigin, traits::Randomness}; use sp_core::H256; -use sp_runtime::Perquintill; +use sp_runtime::{AccountId32, Perquintill}; use super::{mock::*, *}; @@ -3157,10 +3157,13 @@ fn send_rewarding_providers_batch_fails_uninitialised() { let dac_account = AccountId::from([2; 32]); let user1 = AccountId::from([3; 32]); let user2 = AccountId::from([4; 32]); - let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + format!("0x{}", "302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 1; let batch_index = 0; @@ -3170,7 +3173,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; - let payees = vec![(node1, node_id, NodeUsage::default())]; + let payees = vec![(node_key, NodeUsage::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 @@ -3324,9 +3327,6 @@ fn send_rewarding_providers_batch_works() { let dac_account = AccountId::from([123; 32]); let user1 = AccountId::from([1; 32]); - let node1 = AccountId::from([10; 32]); - let node2 = AccountId::from([11; 32]); - let node3 = AccountId::from([12; 32]); let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); @@ -3384,10 +3384,12 @@ fn send_rewarding_providers_batch_works() { let payers = vec![(user1, node_id.clone(), bucket_id1, usage1)]; let payees1 = vec![ - (node1.clone(), node_id.clone(), node_usage1.clone()), - (node2.clone(), node_id.clone(), node_usage2.clone()), + (NodePubKey::StoragePubKey(NODE1_PUB_KEY_32.clone()), node_usage1.clone()), + (NodePubKey::StoragePubKey(NODE2_PUB_KEY_32.clone()), node_usage2.clone()), ]; - let payees2 = vec![(node3.clone(), node_id, node_usage3.clone())]; + let payees2 = + vec![(NodePubKey::StoragePubKey(NODE3_PUB_KEY_32.clone()), node_usage3.clone())]; + let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -3492,7 +3494,7 @@ fn send_rewarding_providers_batch_works() { ); let mut gets_charge = ratio1_gets * report_after.total_customer_charge.gets; - let balance_node1 = Balances::free_balance(node1.clone()); + let balance_node1 = Balances::free_balance(NODE_PROVIDER1_KEY_32.clone()); assert_eq!(balance_node1, transfer_charge + storage_charge + puts_charge + gets_charge); let mut report_reward = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); @@ -3500,7 +3502,7 @@ fn send_rewarding_providers_batch_works() { Event::Rewarded { cluster_id, era, - node_provider_id: node1, + node_provider_id: NODE_PROVIDER1_KEY_32.clone(), batch_index: batch_node_index, rewarded: balance_node1, expected_to_reward: balance_node1, @@ -3532,7 +3534,7 @@ fn send_rewarding_providers_batch_works() { ); gets_charge = ratio2_gets * report_after.total_customer_charge.gets; - let balance_node2 = Balances::free_balance(node2.clone()); + let balance_node2 = Balances::free_balance(NODE_PROVIDER2_KEY_32.clone()); assert_eq!(balance_node2, transfer_charge + storage_charge + puts_charge + gets_charge); assert_eq!(report_reward.total_distributed_reward, balance_node1 + balance_node2); @@ -3540,7 +3542,7 @@ fn send_rewarding_providers_batch_works() { Event::Rewarded { cluster_id, era, - node_provider_id: node2, + node_provider_id: NODE_PROVIDER2_KEY_32, batch_index: batch_node_index, rewarded: balance_node2, expected_to_reward: balance_node2, @@ -3583,14 +3585,14 @@ fn send_rewarding_providers_batch_works() { gets_charge = ratio3_gets * report_after.total_customer_charge.gets; report_reward = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); - let balance_node3 = Balances::free_balance(node3.clone()); + let balance_node3 = Balances::free_balance(NODE_PROVIDER3_KEY_32.clone()); assert_eq!(balance_node3, transfer_charge + storage_charge + puts_charge + gets_charge); System::assert_has_event( Event::Rewarded { cluster_id, era, - node_provider_id: node3, + node_provider_id: NODE_PROVIDER3_KEY_32.clone(), batch_index: batch_node_index + 1, rewarded: balance_node3, expected_to_reward: balance_node3, @@ -3630,6 +3632,32 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { let cluster_id = ONE_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let nodes_keys = [ + NODE1_PUB_KEY_32, + NODE2_PUB_KEY_32, + NODE3_PUB_KEY_32, + NODE4_PUB_KEY_32, + NODE5_PUB_KEY_32, + NODE6_PUB_KEY_32, + NODE7_PUB_KEY_32, + NODE8_PUB_KEY_32, + NODE9_PUB_KEY_32, + NODE10_PUB_KEY_32, + ]; + let storage_nodes: Vec = + nodes_keys.iter().map(|key| NodePubKey::StoragePubKey(key.clone())).collect(); + let providers_accounts = [ + NODE_PROVIDER1_KEY_32, + NODE_PROVIDER2_KEY_32, + NODE_PROVIDER3_KEY_32, + NODE_PROVIDER4_KEY_32, + NODE_PROVIDER5_KEY_32, + NODE_PROVIDER6_KEY_32, + NODE_PROVIDER7_KEY_32, + NODE_PROVIDER8_KEY_32, + NODE_PROVIDER9_KEY_32, + NODE_PROVIDER10_KEY_32, + ]; let era = 100; let user_batch_size = 10; let node_batch_size = 10; @@ -3673,8 +3701,8 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { number_of_gets: usage1.number_of_gets * 2, }; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(NodePubKey, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); for i in 10..10 + num_nodes { let node_usage = match i % 3 { @@ -3688,7 +3716,8 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); + let node_key = storage_nodes[i - num_nodes].clone(); + node_batch.push((node_key.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -3848,7 +3877,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { )); let mut batch_charge = 0; - for (node1, _node_id, node_usage1) in batch.iter() { + for (i, (_node_key, node_usage1)) in batch.iter().enumerate() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -3873,7 +3902,9 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { ); let gets_charge = ratio1_gets * report_after.total_customer_charge.gets; - let balance_node1 = Balances::free_balance(node1); + let balance_node1 = Balances::free_balance( + providers_accounts.get(i).expect("Node provider to be found"), + ); assert!( (transfer_charge + storage_charge + puts_charge + gets_charge) - balance_node1 < MAX_DUST.into() @@ -3908,6 +3939,32 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { let cluster_id = ONE_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let nodes_keys = [ + NODE1_PUB_KEY_32, + NODE2_PUB_KEY_32, + NODE3_PUB_KEY_32, + NODE4_PUB_KEY_32, + NODE5_PUB_KEY_32, + NODE6_PUB_KEY_32, + NODE7_PUB_KEY_32, + NODE8_PUB_KEY_32, + NODE9_PUB_KEY_32, + NODE10_PUB_KEY_32, + ]; + let storage_nodes: Vec = + nodes_keys.iter().map(|key| NodePubKey::StoragePubKey(key.clone())).collect(); + let providers_accounts = [ + NODE_PROVIDER1_KEY_32, + NODE_PROVIDER2_KEY_32, + NODE_PROVIDER3_KEY_32, + NODE_PROVIDER4_KEY_32, + NODE_PROVIDER5_KEY_32, + NODE_PROVIDER6_KEY_32, + NODE_PROVIDER7_KEY_32, + NODE_PROVIDER8_KEY_32, + NODE_PROVIDER9_KEY_32, + NODE_PROVIDER10_KEY_32, + ]; let era = 100; let user_batch_size = 10; let node_batch_size = 10; @@ -3946,10 +4003,10 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { number_of_gets: usage1.number_of_gets * 2, }; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(NodePubKey, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); - for i in 10u8..10 + num_nodes { + for i in 10..10 + num_nodes { let ratio = match i % 5 { 0 => Perquintill::from_float(1_000_000.0), 1 => Perquintill::from_float(10_000_000.0), @@ -3974,7 +4031,8 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); + let node_key = storage_nodes[i - num_nodes].clone(); + node_batch.push((node_key.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -4135,7 +4193,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { )); let mut batch_charge = 0; - for (node1, _node_id, node_usage1) in batch.iter() { + for (i, (_node_key, node_usage1)) in batch.iter().enumerate() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -4160,7 +4218,9 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { ); let gets_charge = ratio1_gets * report_after.total_customer_charge.gets; - let balance_node1 = Balances::free_balance(node1); + let balance_node1 = Balances::free_balance( + providers_accounts.get(i).expect("Node provider to be found"), + ); assert!( (transfer_charge + storage_charge + puts_charge + gets_charge) - balance_node1 < MAX_DUST.into() @@ -4195,6 +4255,32 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { let cluster_id = ONE_CLUSTER_ID; let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let nodes_keys = [ + NODE1_PUB_KEY_32, + NODE2_PUB_KEY_32, + NODE3_PUB_KEY_32, + NODE4_PUB_KEY_32, + NODE5_PUB_KEY_32, + NODE6_PUB_KEY_32, + NODE7_PUB_KEY_32, + NODE8_PUB_KEY_32, + NODE9_PUB_KEY_32, + NODE10_PUB_KEY_32, + ]; + let storage_nodes: Vec = + nodes_keys.iter().map(|key| NodePubKey::StoragePubKey(key.clone())).collect(); + let providers_accounts = [ + NODE_PROVIDER1_KEY_32, + NODE_PROVIDER2_KEY_32, + NODE_PROVIDER3_KEY_32, + NODE_PROVIDER4_KEY_32, + NODE_PROVIDER5_KEY_32, + NODE_PROVIDER6_KEY_32, + NODE_PROVIDER7_KEY_32, + NODE_PROVIDER8_KEY_32, + NODE_PROVIDER9_KEY_32, + NODE_PROVIDER10_KEY_32, + ]; let era = 100; let bucketid1: BucketId = 1; let user_batch_size = 10; @@ -4232,10 +4318,10 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { number_of_gets: usage1.number_of_gets * 2, }; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(NodePubKey, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); - for i in 10u8..10 + num_nodes { + for i in 10..10 + num_nodes { let ratio = match i % 5 { 0 => Perquintill::from_float(1_000_000.0), 1 => Perquintill::from_float(0.5), @@ -4260,7 +4346,8 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); + let node_key = storage_nodes[i - num_nodes].clone(); + node_batch.push((node_key.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -4420,7 +4507,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { )); let mut batch_charge = 0; - for (node1, _node_id, node_usage1) in batch.iter() { + for (i, (_node_key, node_usage1)) in batch.iter().enumerate() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -4445,7 +4532,9 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { ); let gets_charge = ratio1_gets * report_after.total_customer_charge.gets; - let balance_node1 = Balances::free_balance(node1); + let balance_node1 = Balances::free_balance( + providers_accounts.get(i).expect("Node provider to be found"), + ); assert!( (transfer_charge + storage_charge + puts_charge + gets_charge) - balance_node1 < MAX_DUST.into() @@ -4490,16 +4579,42 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { let cluster_id = CERE_CLUSTER_ID; let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let nodes_keys = [ + NODE1_PUB_KEY_32, + NODE2_PUB_KEY_32, + NODE3_PUB_KEY_32, + NODE4_PUB_KEY_32, + NODE5_PUB_KEY_32, + NODE6_PUB_KEY_32, + NODE7_PUB_KEY_32, + NODE8_PUB_KEY_32, + NODE9_PUB_KEY_32, + NODE10_PUB_KEY_32, + ]; + let storage_nodes: Vec = + nodes_keys.iter().map(|key| NodePubKey::StoragePubKey(key.clone())).collect(); + let providers_accounts = [ + NODE_PROVIDER1_KEY_32, + NODE_PROVIDER2_KEY_32, + NODE_PROVIDER3_KEY_32, + NODE_PROVIDER4_KEY_32, + NODE_PROVIDER5_KEY_32, + NODE_PROVIDER6_KEY_32, + NODE_PROVIDER7_KEY_32, + NODE_PROVIDER8_KEY_32, + NODE_PROVIDER9_KEY_32, + NODE_PROVIDER10_KEY_32, + ]; let era = 100; let user_batch_size = 10; let node_batch_size = 10; let mut batch_user_index = 0; let mut batch_node_index = 0; let bucket_id1: BucketId = 1; - let mut payees: Vec> = Vec::new(); - let mut node_batch: Vec<(AccountId, String, NodeUsage)> = Vec::new(); + let mut payees: Vec> = Vec::new(); + let mut node_batch: Vec<(NodePubKey, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); - for i in 10u8..10 + num_nodes { + for i in 10..10 + num_nodes { let node_usage = NodeUsage { transferred_bytes: generate_random_u64(&mock_randomness, min, max), stored_bytes: (generate_random_u64(&mock_randomness, min, max)) as i64, @@ -4512,7 +4627,8 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { total_nodes_usage.number_of_puts += node_usage.number_of_puts; total_nodes_usage.number_of_gets += node_usage.number_of_gets; - node_batch.push((AccountId::from([i; 32]), node_id.clone(), node_usage)); + let node_key = storage_nodes[i - num_nodes].clone(); + node_batch.push((node_key.clone(), node_usage)); if node_batch.len() == node_batch_size { payees.push(node_batch.clone()); node_batch.clear(); @@ -4664,7 +4780,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { )); let mut batch_charge = 0; - for (node1, _node_id, node_usage1) in batch.iter() { + for (i, (_node_key, node_usage1)) in batch.iter().enumerate() { let ratio1_transfer = Perquintill::from_rational( node_usage1.transferred_bytes, total_nodes_usage.transferred_bytes, @@ -4689,7 +4805,9 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { ); let gets_charge = ratio1_gets * report_after.total_customer_charge.gets; - let balance_node1 = Balances::free_balance(node1); + let balance_node1 = Balances::free_balance( + providers_accounts.get(i).expect("Node provider to be found"), + ); assert!( (transfer_charge + storage_charge + puts_charge + gets_charge) - balance_node1 < MAX_DUST.into() @@ -4713,10 +4831,13 @@ fn end_rewarding_providers_fails_uninitialised() { let dac_account = AccountId::from([2; 32]); let user1 = AccountId::from([3; 32]); let user2 = AccountId::from([4; 32]); - let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 1; let batch_index = 0; @@ -4724,7 +4845,7 @@ fn end_rewarding_providers_fails_uninitialised() { let bucket_id2: BucketId = 2; let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; - let payees = vec![(node1, node_id, NodeUsage::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 @@ -4892,10 +5013,13 @@ fn end_rewarding_providers_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let dac_account = AccountId::from([2; 32]); let user1 = AccountId::from([1; 32]); - let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -4916,7 +5040,7 @@ fn end_rewarding_providers_works() { }; let total_node_usage = node_usage1.clone(); let payers = vec![(user1, node_id.clone(), bucket_id1, usage1)]; - let payees = vec![(node1, node_id, node_usage1)]; + let payees = vec![(node_key, node_usage1)]; assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); @@ -4996,10 +5120,13 @@ fn end_billing_report_fails_uninitialised() { let dac_account = AccountId::from([2; 32]); let user1 = AccountId::from([3; 32]); let user2 = AccountId::from([4; 32]); - let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 1; let batch_index = 0; @@ -5007,7 +5134,7 @@ fn end_billing_report_fails_uninitialised() { let bucket_id2: BucketId = 2; let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; - let payees = vec![(node1, node_id, NodeUsage::default())]; + let payees = vec![(node_key, NodeUsage::default())]; let total_node_usage = NodeUsage::default(); assert_noop!( @@ -5179,7 +5306,6 @@ fn end_billing_report_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let dac_account = AccountId::from([2; 32]); let user1 = AccountId::from([3; 32]); - let node1 = AccountId::from([33; 32]); let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); @@ -5189,7 +5315,7 @@ fn end_billing_report_works() { let total_node_usage = NodeUsage::default(); let bucket_id1 = 1; let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; - let payees = vec![(node1, node_id, NodeUsage::default())]; + let payees = vec![(NodePubKey::StoragePubKey(NODE1_PUB_KEY_32), NodeUsage::default())]; assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 0cfdcbe67..45dc868e0 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -566,9 +566,9 @@ pub mod pallet { pub(crate) batch_proof: MMRProof, } - pub struct ProviderBatch { + pub struct ProviderBatch { pub(crate) batch_index: BatchIndex, - pub(crate) payees: Vec<(T::AccountId, String, NodeUsage)>, + pub(crate) payees: Vec<(NodePubKey, NodeUsage)>, pub(crate) batch_proof: MMRProof, } @@ -2425,7 +2425,7 @@ pub mod pallet { pub(crate) fn prepare_send_rewarding_providers_batch( cluster_id: &ClusterId, batch_size: usize, - ) -> Result)>, Vec> { + ) -> Result, Vec> { if let Some((era_id, start, end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { @@ -2483,7 +2483,7 @@ pub mod pallet { era_id: DdcEra, nodes_total_activity: Vec, nodes_activity_batch_roots: Vec, - ) -> Result)>, Vec> { + ) -> Result, Vec> { let batch_index = T::PayoutVisitor::get_next_provider_batch_for_payment( cluster_id, era_id, ) @@ -2536,15 +2536,15 @@ pub mod pallet { payees: nodes_activity_batched[i] .iter() .map(|activity| { - let node_id = activity.clone().node_id; - let provider_id = Self::fetch_provider_id(node_id.clone()).unwrap(); // todo! remove unwrap + let node_key = Self::node_key_from_hex(activity.node_id.clone()) + .expect("Node Public Key to be decoded"); let node_usage = NodeUsage { transferred_bytes: activity.transferred_bytes, stored_bytes: activity.stored_bytes, number_of_puts: activity.number_of_puts, number_of_gets: activity.number_of_gets, }; - (provider_id, node_id, node_usage) + (node_key, node_usage) }) .collect(), batch_proof, @@ -2778,36 +2778,7 @@ pub mod pallet { sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &key, &new_nonce.encode()); nonce_data } - pub(crate) fn store_provider_id( - // todo! (3) add tests - node_id: String, - provider_id: A, - ) { - let key = format!("offchain::activities::provider_id::{:?}", node_id).into_bytes(); - let encoded_tuple = provider_id.encode(); - - // Store the serialized data in local offchain storage - sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, &key, &encoded_tuple); - } - - pub(crate) fn fetch_provider_id(node_id: String) -> Option { - let key = format!("offchain::activities::provider_id::{:?}", node_id).into_bytes(); - // Retrieve encoded tuple from local storage - let encoded_tuple = - match sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &key) { - Some(data) => data, - None => return None, - }; - match Decode::decode(&mut &encoded_tuple[..]) { - Ok(provider_id) => Some(provider_id), - Err(err) => { - // Print error message with details of the decoding error - log::error!("๐Ÿฆ€Decoding error while fetching provider id: {:?}", err); - None - }, - } - } /// Converts a vector of activity batches into their corresponding Merkle roots. /// /// This function takes a vector of activity batches, where each batch is a vector of @@ -3494,11 +3465,6 @@ pub mod pallet { Ok(dac_nodes) } - fn get_node_provider_id(node_pub_key: &NodePubKey) -> Result { - T::NodeVisitor::get_node_provider_id(node_pub_key) - .map_err(|_| OCWError::FailedToFetchNodeProvider) - } - /// Fetch node usage of an era. /// /// Parameters: @@ -3527,13 +3493,6 @@ pub mod pallet { let aggregates = aggregates_res.expect("Nodes Aggregates Response to be available"); - // todo: this is tech debt that needs to be refactored, the mapping logic needs to - // be moved to payouts pallet - for aggregate in aggregates.clone() { - let provider_id = Self::get_node_provider_id(node_key).unwrap(); - Self::store_provider_id(aggregate.node_id, provider_id); - } - nodes_aggregates.push(( AggregatorInfo { node_pub_key: node_key.clone(), @@ -3621,6 +3580,14 @@ pub mod pallet { Ok(processed_eras_by_nodes) } + + pub fn node_key_from_hex(hex_str: String) -> Result { + let bytes_vec = hex::decode(hex_str)?; + let bytes_arr: [u8; 32] = + bytes_vec.try_into().map_err(|_| hex::FromHexError::InvalidStringLength)?; + let pub_key = AccountId32::from(bytes_arr); + Ok(NodePubKey::StoragePubKey(pub_key)) + } } #[pallet::call] @@ -4117,7 +4084,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: Vec<(T::AccountId, String, NodeUsage)>, + payees: Vec<(NodePubKey, NodeUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -4260,7 +4227,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, String, NodeUsage)], + payees: &[(NodePubKey, NodeUsage)], batch_proof: &MMRProof, ) -> bool { let validation_era = EraValidations::::get(cluster_id, era_id); @@ -4271,8 +4238,8 @@ pub mod pallet { let activity_hashes = payees .iter() - .map(|(_node_provider, node_id, usage)| { - let mut data = node_id.encode(); + .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()); diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index 218477b2f..1f7d479e9 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -435,7 +435,7 @@ impl PayoutVisitor for MockPayoutVisitor { _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, - _payees: &[(T::AccountId, String, NodeUsage)], + _payees: &[(NodePubKey, NodeUsage)], _batch_proof: MMRProof, ) -> DispatchResult { Ok(()) diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index abbea88d3..16f385a30 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -2,8 +2,8 @@ use scale_info::prelude::string::String; use sp_runtime::DispatchResult; use crate::{ - BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodeUsage, PayoutError, - PayoutState, + BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, + PayoutError, PayoutState, }; pub trait PayoutProcessor {} @@ -57,7 +57,7 @@ pub trait PayoutVisitor { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, String, NodeUsage)], + payees: &[(NodePubKey, NodeUsage)], batch_proof: MMRProof, ) -> DispatchResult; diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index 58fb58e89..a975812ea 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -3,7 +3,9 @@ use scale_info::prelude::string::String; #[cfg(feature = "runtime-benchmarks")] use sp_std::prelude::*; -use crate::{BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodeUsage}; +use crate::{ + BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, +}; pub trait ValidatorVisitor { #[cfg(feature = "runtime-benchmarks")] @@ -20,7 +22,7 @@ pub trait ValidatorVisitor { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payees: &[(T::AccountId, String, NodeUsage)], + payees: &[(NodePubKey, NodeUsage)], batch_proof: &MMRProof, ) -> bool; } From f0cd53fd85c29e2d7ca1a20c2d50d665db2349ef Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Wed, 6 Nov 2024 17:48:44 +0100 Subject: [PATCH 06/20] chore: merging with 'dev' branch From 1a4f9c624c80ee8865d2b03035d20024c25f0f5c Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 12:28:04 +0100 Subject: [PATCH 07/20] chore: merging with 'dev' branch From 04bac35e6e0a9da0a75e1745390998db4f7dcaae Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 03:11:00 +0100 Subject: [PATCH 08/20] chore: fetching bucket owner from the bucket data during payout --- pallets/ddc-customers/src/lib.rs | 5 + pallets/ddc-payouts/src/lib.rs | 9 +- pallets/ddc-payouts/src/mock.rs | 118 ++++++--- pallets/ddc-payouts/src/tests.rs | 350 +++++++++++++++------------ pallets/ddc-verification/src/lib.rs | 43 ++-- pallets/ddc-verification/src/mock.rs | 2 +- primitives/src/traits/bucket.rs | 4 +- primitives/src/traits/payout.rs | 3 +- primitives/src/traits/validator.rs | 5 +- 9 files changed, 318 insertions(+), 221 deletions(-) diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index c2531a3e9..639b22ec3 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -648,6 +648,11 @@ pub mod pallet { } impl BucketVisitor 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( cluster_id: &ClusterId, bucket_id: BucketId, diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index ec5343a8e..c4ea9369d 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -432,7 +432,7 @@ pub mod pallet { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)>, + payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let caller = ensure_signed(origin)?; @@ -473,8 +473,9 @@ pub mod pallet { ); let mut updated_billing_report = billing_report; - for (customer_id, _node_id, bucket_id, customer_usage) in payers { - log::info!("๐Ÿญsend_charging_customers_batch get_customer_charge customer_id: {:?} - bucket_id: {:?} - era:{:?} - cluster-id:{:?}", Self::get_account_id_string(customer_id.clone()), bucket_id, era, cluster_id); + for (_node_key, bucket_id, customer_usage) in payers { + let customer_id = T::BucketVisitor::get_bucket_owner_id(bucket_id)?; + let mut customer_charge = get_customer_charge::( &cluster_id, &customer_usage, @@ -1220,7 +1221,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, String, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: MMRProof, ) -> DispatchResult { log::info!( diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index af28518ea..ec3d2f35f 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -179,7 +179,8 @@ where Ok(NODE_PROVIDER9_KEY_32.clone().into()), NodePubKey::StoragePubKey(key) if key == &NODE10_PUB_KEY_32 => Ok(NODE_PROVIDER10_KEY_32.clone().into()), - _ => Ok(AccountId32::new([0; 32]).clone().into()), + + _ => Err(DispatchError::Other("Unexpected node pub_key")), } } fn get_node_params(_node_pub_key: &NodePubKey) -> Result { @@ -209,12 +210,11 @@ where _cluster_id: ClusterId, _era: DdcEra, _batch_index: BatchIndex, - _payers: &[(T::AccountId, String, BucketId, CustomerUsage)], + _payers: &[(NodePubKey, BucketId, CustomerUsage)], _batch_proof: &MMRProof, ) -> bool { true } - fn is_providers_batch_valid( _cluster_id: ClusterId, _era: DdcEra, @@ -227,7 +227,35 @@ where } pub struct TestBucketVisitor; -impl BucketVisitor for TestBucketVisitor { +impl BucketVisitor for TestBucketVisitor +where + ::AccountId: From, +{ + fn get_bucket_owner_id(bucket_id: BucketId) -> Result { + match bucket_id { + BUCKET_ID1 => Ok(CUSTOMER1_KEY_32.clone().into()), + BUCKET_ID2 => Ok(CUSTOMER2_KEY_32.clone().into()), + BUCKET_ID3 => Ok(CUSTOMER3_KEY_32.clone().into()), + BUCKET_ID4 => Ok(CUSTOMER4_KEY_32.clone().into()), + BUCKET_ID5 => Ok(CUSTOMER5_KEY_32.clone().into()), + BUCKET_ID6 => Ok(CUSTOMER6_KEY_32.clone().into()), + BUCKET_ID7 => Ok(CUSTOMER7_KEY_32.clone().into()), + + BUCKET_ID100 => Ok(CUSTOMER100_KEY_32.clone().into()), + BUCKET_ID101 => Ok(CUSTOMER101_KEY_32.clone().into()), + BUCKET_ID102 => Ok(CUSTOMER102_KEY_32.clone().into()), + BUCKET_ID103 => Ok(CUSTOMER103_KEY_32.clone().into()), + BUCKET_ID104 => Ok(CUSTOMER104_KEY_32.clone().into()), + BUCKET_ID105 => Ok(CUSTOMER105_KEY_32.clone().into()), + BUCKET_ID106 => Ok(CUSTOMER106_KEY_32.clone().into()), + BUCKET_ID107 => Ok(CUSTOMER107_KEY_32.clone().into()), + BUCKET_ID108 => Ok(CUSTOMER108_KEY_32.clone().into()), + BUCKET_ID109 => Ok(CUSTOMER109_KEY_32.clone().into()), + + _ => Err(DispatchError::Other("Unexpected bucket_id")), + } + } + fn get_total_customer_usage( _cluster_id: &ClusterId, _bucket_id: BucketId, @@ -238,7 +266,10 @@ impl BucketVisitor for TestBucketVisitor { } pub struct TestCustomerCharger; -impl CustomerCharger for TestCustomerCharger { +impl CustomerCharger for TestCustomerCharger +where + ::AccountId: From, +{ fn charge_content_owner( _cluster_id: &ClusterId, _bucket_id: BucketId, @@ -248,16 +279,11 @@ impl CustomerCharger for TestCustomerCharger { amount: u128, ) -> Result { let mut amount_to_charge = amount; - let mut temp: [u8; 32] = ACCOUNT_ID_1; - let account_1 = T::AccountId::decode(&mut &temp[..]).unwrap(); - temp = ACCOUNT_ID_2; - let account_2 = T::AccountId::decode(&mut &temp[..]).unwrap(); - temp = ACCOUNT_ID_3; - let account_3 = T::AccountId::decode(&mut &temp[..]).unwrap(); - temp = ACCOUNT_ID_4; - let account_4 = T::AccountId::decode(&mut &temp[..]).unwrap(); - temp = ACCOUNT_ID_5; - let account_5 = T::AccountId::decode(&mut &temp[..]).unwrap(); + let account_1: T::AccountId = CUSTOMER1_KEY_32.into(); + let account_2: T::AccountId = CUSTOMER2_KEY_32.into(); + let account_3: T::AccountId = CUSTOMER3_KEY_32.into(); + let account_4: T::AccountId = CUSTOMER4_KEY_32.into(); + let account_5: T::AccountId = CUSTOMER5_KEY_32.into(); if content_owner == account_1 || content_owner == account_3 || @@ -288,13 +314,6 @@ impl CustomerCharger for TestCustomerCharger { } } -pub const ACCOUNT_ID_1: [u8; 32] = [1; 32]; -pub const ACCOUNT_ID_2: [u8; 32] = [2; 32]; -pub const ACCOUNT_ID_3: [u8; 32] = [3; 32]; -pub const ACCOUNT_ID_4: [u8; 32] = [4; 32]; -pub const ACCOUNT_ID_5: [u8; 32] = [5; 32]; -pub const ACCOUNT_ID_6: [u8; 32] = [6; 32]; -pub const ACCOUNT_ID_7: [u8; 32] = [7; 32]; pub struct TestClusterCreator; impl ClusterCreator for TestClusterCreator { fn create_cluster( @@ -385,6 +404,47 @@ pub const NODE_PROVIDER8_KEY_32: AccountId32 = AccountId32::new([17; 32]); pub const NODE_PROVIDER9_KEY_32: AccountId32 = AccountId32::new([18; 32]); pub const NODE_PROVIDER10_KEY_32: AccountId32 = AccountId32::new([19; 32]); +pub const BUCKET_ID1: BucketId = 1; +pub const BUCKET_ID2: BucketId = 2; +pub const BUCKET_ID3: BucketId = 3; +pub const BUCKET_ID4: BucketId = 4; +pub const BUCKET_ID5: BucketId = 5; +pub const BUCKET_ID6: BucketId = 6; +pub const BUCKET_ID7: BucketId = 7; +pub const BUCKET_ID8: BucketId = 8; +pub const BUCKET_ID9: BucketId = 9; +pub const BUCKET_ID10: BucketId = 10; + +pub const BUCKET_ID100: BucketId = 100; +pub const BUCKET_ID101: BucketId = 101; +pub const BUCKET_ID102: BucketId = 102; +pub const BUCKET_ID103: BucketId = 103; +pub const BUCKET_ID104: BucketId = 104; +pub const BUCKET_ID105: BucketId = 105; +pub const BUCKET_ID106: BucketId = 106; +pub const BUCKET_ID107: BucketId = 107; +pub const BUCKET_ID108: BucketId = 108; +pub const BUCKET_ID109: BucketId = 109; + +pub const CUSTOMER1_KEY_32: AccountId32 = AccountId32::new([1; 32]); +pub const CUSTOMER2_KEY_32: AccountId32 = AccountId32::new([2; 32]); +pub const CUSTOMER3_KEY_32: AccountId32 = AccountId32::new([3; 32]); +pub const CUSTOMER4_KEY_32: AccountId32 = AccountId32::new([4; 32]); +pub const CUSTOMER5_KEY_32: AccountId32 = AccountId32::new([5; 32]); +pub const CUSTOMER6_KEY_32: AccountId32 = AccountId32::new([6; 32]); +pub const CUSTOMER7_KEY_32: AccountId32 = AccountId32::new([7; 32]); + +pub const CUSTOMER100_KEY_32: AccountId32 = AccountId32::new([100; 32]); +pub const CUSTOMER101_KEY_32: AccountId32 = AccountId32::new([101; 32]); +pub const CUSTOMER102_KEY_32: AccountId32 = AccountId32::new([102; 32]); +pub const CUSTOMER103_KEY_32: AccountId32 = AccountId32::new([103; 32]); +pub const CUSTOMER104_KEY_32: AccountId32 = AccountId32::new([104; 32]); +pub const CUSTOMER105_KEY_32: AccountId32 = AccountId32::new([105; 32]); +pub const CUSTOMER106_KEY_32: AccountId32 = AccountId32::new([106; 32]); +pub const CUSTOMER107_KEY_32: AccountId32 = AccountId32::new([107; 32]); +pub const CUSTOMER108_KEY_32: AccountId32 = AccountId32::new([108; 32]); +pub const CUSTOMER109_KEY_32: AccountId32 = AccountId32::new([109; 32]); + pub const NO_FEE_CLUSTER_ID: ClusterId = ClusterId::zero(); pub const ONE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(4u8); pub const CERE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(10u8); @@ -667,13 +727,13 @@ impl ExtBuilder { let _balance_genesis = pallet_balances::GenesisConfig:: { balances: vec![ - ([1; 32].into(), 10000000000000000000000000000), - ([2; 32].into(), USER2_BALANCE), // < PARTIAL_CHARGE - ([3; 32].into(), USER3_BALANCE), // > PARTIAL_CHARGE - ([4; 32].into(), 1000000000000000000000000), - ([5; 32].into(), 1000000000000000000000000), - ([6; 32].into(), 1000000000000000000000000), - ([7; 32].into(), 1000000000000000000000000), + (CUSTOMER1_KEY_32, 10000000000000000000000000000), + (CUSTOMER2_KEY_32, USER2_BALANCE), // < PARTIAL_CHARGE + (CUSTOMER3_KEY_32, USER3_BALANCE), // > PARTIAL_CHARGE + (CUSTOMER4_KEY_32, 1000000000000000000000000), + (CUSTOMER5_KEY_32, 1000000000000000000000000), + (CUSTOMER6_KEY_32, 1000000000000000000000000), + (CUSTOMER7_KEY_32, 1000000000000000000000000), ], } .assimilate_storage(&mut storage); diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index f22303bc1..cb84ab02a 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -196,24 +196,21 @@ fn send_charging_customers_batch_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([3; 32]); - let user2 = AccountId::from([4; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; let batch_index = 1; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let customer_usage = CustomerUsage { transferred_bytes: 100, stored_bytes: -800, number_of_gets: 100, number_of_puts: 200, }; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - let payers1 = vec![(user1, node_id.clone(), bucket_id1, customer_usage)]; - let payers2 = vec![(user2.clone(), node_id.clone(), bucket_id2, CustomerUsage::default())]; + 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 start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -286,7 +283,7 @@ fn send_charging_customers_batch_fails_uninitialised() { max_batch_index, )); - let payers1 = vec![(user2, node_id, bucket_id2, CustomerUsage::default())]; + let payers1 = vec![(node_key, bucket_id4, CustomerUsage::default())]; assert_ok!(DdcPayouts::send_charging_customers_batch( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -419,8 +416,7 @@ fn send_charging_customers_batch_works() { let user3_debtor = AccountId::from([3; 32]); let user4 = AccountId::from([4; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -458,11 +454,11 @@ fn send_charging_customers_batch_works() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -681,6 +677,10 @@ fn end_charging_customers_works_small_usage_1_hour() { let cluster_id = HIGH_FEES_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -701,8 +701,8 @@ fn end_charging_customers_works_small_usage_1_hour() { }; let payers1 = vec![ - (user6.clone(), node_id.clone(), bucket_id6, usage6.clone()), - (user7.clone(), node_id.clone(), bucket_id7, usage7.clone()), + (node_key.clone(), bucket_id6, usage6.clone()), + (node_key.clone(), bucket_id7, usage7.clone()), ]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -890,7 +890,6 @@ fn send_charging_customers_batch_works_for_day() { let bucket_id2: BucketId = 2; let bucket_id3: BucketId = 3; let bucket_id4: BucketId = 4; - let usage1 = CustomerUsage { // should pass without debt transferred_bytes: 23452345, @@ -921,13 +920,18 @@ fn send_charging_customers_batch_works_for_day() { }; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); + let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -1148,6 +1152,11 @@ fn send_charging_customers_batch_works_for_day_free_storage() { let cluster_id = STORAGE_ZERO_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); + let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1185,12 +1194,12 @@ fn send_charging_customers_batch_works_for_day_free_storage() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id.clone(), bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -1411,6 +1420,10 @@ fn send_charging_customers_batch_works_for_day_free_stream() { let cluster_id = STREAM_ZERO_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1448,11 +1461,11 @@ fn send_charging_customers_batch_works_for_day_free_stream() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -1673,6 +1686,10 @@ fn send_charging_customers_batch_works_for_day_free_get() { let cluster_id = GET_ZERO_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1711,11 +1728,11 @@ fn send_charging_customers_batch_works_for_day_free_get() { }; let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -1936,6 +1953,10 @@ fn send_charging_customers_batch_works_for_day_free_put() { let cluster_id = PUT_ZERO_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -1974,11 +1995,11 @@ fn send_charging_customers_batch_works_for_day_free_put() { }; let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -2199,6 +2220,10 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { let cluster_id = STORAGE_STREAM_ZERO_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 3; let mut batch_index = 0; @@ -2236,11 +2261,11 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { number_of_gets: 242334563456423, }; let payers1 = vec![ - (user2_debtor.clone(), node_id.clone(), bucket_id2, usage2.clone()), - (user4.clone(), node_id.clone(), bucket_id4, usage4.clone()), + (node_key.clone(), bucket_id2, usage2.clone()), + (node_key.clone(), bucket_id4, usage4.clone()), ]; - let payers2 = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; - let payers3 = vec![(user3_debtor.clone(), node_id, bucket_id3, usage3.clone())]; + let payers2 = vec![(node_key.clone(), bucket_id1, usage1.clone())]; + let payers3 = vec![(node_key.clone(), bucket_id3, usage3.clone())]; 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 = @@ -2458,6 +2483,10 @@ fn send_charging_customers_batch_works_zero_fees() { let cluster_id = ONE_CLUSTER_ID; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -2470,7 +2499,7 @@ fn send_charging_customers_batch_works_zero_fees() { number_of_gets: 1, }; - let payers5 = vec![(user5, node_id.clone(), bucket_id5, usage5.clone())]; + let payers5 = vec![(node_key.clone(), bucket_id5, usage5.clone())]; 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 = @@ -2540,12 +2569,16 @@ fn end_charging_customers_fails_uninitialised() { let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 2; let batch_index = 1; let bucket_id1: BucketId = 1; - let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::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 = @@ -2636,6 +2669,10 @@ fn end_charging_customers_works() { let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -2646,7 +2683,7 @@ fn end_charging_customers_works() { number_of_puts: 4456456345234523, number_of_gets: 523423, }; - let payers = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers = vec![(node_key.clone(), bucket_id1, usage1.clone())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -2821,6 +2858,10 @@ fn end_charging_customers_works_zero_fees() { let cluster_id = ClusterId::zero(); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -2831,7 +2872,7 @@ fn end_charging_customers_works_zero_fees() { number_of_puts: 1, number_of_gets: 1, }; - let payers = vec![(user1.clone(), node_id.clone(), bucket_id1, usage1.clone())]; + let payers = vec![(node_key.clone(), bucket_id1, usage1.clone())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); // Midnight @@ -2954,7 +2995,11 @@ fn begin_rewarding_providers_fails_uninitialised() { let bucket_id1: BucketId = 1; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + 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, + ])); + let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::default())]; let node_usage = NodeUsage::default(); let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -3091,8 +3136,12 @@ fn begin_rewarding_providers_works() { let bucket_id1: BucketId = 1; let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let total_node_usage = NodeUsage::default(); - let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; + let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::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 @@ -3155,23 +3204,15 @@ fn send_rewarding_providers_batch_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([3; 32]); - let user2 = AccountId::from([4; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - format!("0x{}", "302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 1; let batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - - let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; - let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; + 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 payees = vec![(node_key, NodeUsage::default())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -3330,6 +3371,10 @@ fn send_rewarding_providers_batch_works() { let cluster_id = ClusterId::from([12; 20]); let node_id = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); let era = 100; let max_batch_index = 0; let max_node_batch_index = 1; @@ -3382,7 +3427,7 @@ fn send_rewarding_providers_batch_works() { node_usage3.number_of_gets, }; - let payers = vec![(user1, node_id.clone(), bucket_id1, usage1)]; + let payers = vec![(node_key.clone(), bucket_id1, usage1)]; let payees1 = vec![ (NodePubKey::StoragePubKey(NODE1_PUB_KEY_32.clone()), node_usage1.clone()), (NodePubKey::StoragePubKey(NODE2_PUB_KEY_32.clone()), node_usage2.clone()), @@ -3630,8 +3675,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let nodes_keys = [ NODE1_PUB_KEY_32, NODE2_PUB_KEY_32, @@ -3658,10 +3702,16 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { NODE_PROVIDER9_KEY_32, NODE_PROVIDER10_KEY_32, ]; + let customers_accounts = [ + CUSTOMER100_KEY_32, + CUSTOMER101_KEY_32, + CUSTOMER102_KEY_32, + CUSTOMER103_KEY_32, + CUSTOMER104_KEY_32, + ]; let era = 100; let user_batch_size = 10; let node_batch_size = 10; - let bucketid1: BucketId = 1; let mut batch_user_index = 0; let mut batch_node_index = 0; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -3728,9 +3778,9 @@ 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<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); - for user_id in 100u8..100 + num_users { + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); + for user_id in 100..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::one(), 1 => Perquintill::from_float(0.5), @@ -3756,12 +3806,8 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push(( - AccountId::from([user_id; 32]), - node_id.clone(), - bucketid1, - user_usage, - )); + let bucket_id = user_id.into(); + user_batch.push((node_key.clone(), bucket_id, user_usage)); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -3796,14 +3842,15 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { MMRProof::default(), )); - for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { + for (i, (_node_key, bucket_id, usage)) in batch.iter().enumerate() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); + let customer_id = customers_accounts[i].clone(); System::assert_has_event( Event::Charged { cluster_id, era, - bucket_id: bucketid1, + bucket_id: bucket_id.clone(), customer_id: customer_id.clone(), batch_index: batch_user_index, amount: charge, @@ -3927,7 +3974,6 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { System::set_block_number(1); 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(); @@ -3937,8 +3983,8 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); + let nodes_keys = [ NODE1_PUB_KEY_32, NODE2_PUB_KEY_32, @@ -3951,8 +3997,10 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { NODE9_PUB_KEY_32, NODE10_PUB_KEY_32, ]; + let storage_nodes: Vec = nodes_keys.iter().map(|key| NodePubKey::StoragePubKey(key.clone())).collect(); + let providers_accounts = [ NODE_PROVIDER1_KEY_32, NODE_PROVIDER2_KEY_32, @@ -3965,13 +4013,20 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { NODE_PROVIDER9_KEY_32, NODE_PROVIDER10_KEY_32, ]; + + let customers_accounts = [ + CUSTOMER100_KEY_32, + CUSTOMER101_KEY_32, + CUSTOMER102_KEY_32, + CUSTOMER103_KEY_32, + CUSTOMER104_KEY_32, + ]; + let era = 100; let user_batch_size = 10; let node_batch_size = 10; - let bucketid1: BucketId = 1; let mut batch_user_index = 0; let mut batch_node_index = 0; - let bucket_id: BucketId = 1; let usage1 = CustomerUsage { transferred_bytes: 1024, stored_bytes: 1024, @@ -4043,10 +4098,10 @@ 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<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); - for user_id in 100u8..100 + num_users { + for user_id in 100..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::from_float(1_000_000.0), 1 => Perquintill::from_float(10_000_000.0), @@ -4072,12 +4127,8 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push(( - AccountId::from([user_id; 32]), - node_id.clone(), - bucket_id, - user_usage, - )); + let bucket_id = user_id.into(); + user_batch.push((node_key.clone(), bucket_id, user_usage)); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -4112,14 +4163,15 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { MMRProof::default(), )); - for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { + for (i, (_node_key, bucket_id, usage)) in batch.iter().enumerate() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); + let customer_id = customers_accounts[i].clone(); System::assert_has_event( Event::Charged { cluster_id, era, - bucket_id: bucketid1, + bucket_id: bucket_id.clone(), customer_id: customer_id.clone(), batch_index: batch_user_index, amount: charge, @@ -4243,7 +4295,6 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { System::set_block_number(1); 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(); @@ -4253,8 +4304,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; - let node_id: String = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let nodes_keys = [ NODE1_PUB_KEY_32, NODE2_PUB_KEY_32, @@ -4281,8 +4331,15 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { NODE_PROVIDER9_KEY_32, NODE_PROVIDER10_KEY_32, ]; + let customers_accounts = [ + CUSTOMER100_KEY_32, + CUSTOMER101_KEY_32, + CUSTOMER102_KEY_32, + CUSTOMER103_KEY_32, + CUSTOMER104_KEY_32, + ]; + let era = 100; - let bucketid1: BucketId = 1; let user_batch_size = 10; let node_batch_size = 10; let mut batch_user_index = 0; @@ -4358,8 +4415,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<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); for user_id in 100u8..100 + num_users { let ratio = match user_id % 5 { 0 => Perquintill::from_float(1_000_000.0), @@ -4386,12 +4443,8 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push(( - AccountId::from([user_id; 32]), - node_id.clone(), - bucketid1, - user_usage, - )); + let bucket_id = user_id.into(); + user_batch.push((node_key.clone(), bucket_id, user_usage)); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -4426,15 +4479,16 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { MMRProof::default(), )); - for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { + for (i, (_node_key, bucket_id, usage)) in batch.iter().enumerate() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); + let customer_id = customers_accounts[i].clone(); System::assert_has_event( Event::Charged { cluster_id, era, customer_id: customer_id.clone(), - bucket_id: bucketid1, + bucket_id: bucket_id.clone(), batch_index: batch_user_index, amount: charge, } @@ -4564,7 +4618,6 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { System::set_block_number(1); 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(); @@ -4577,8 +4630,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = CERE_CLUSTER_ID; - let node_id: String = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let nodes_keys = [ NODE1_PUB_KEY_32, NODE2_PUB_KEY_32, @@ -4605,12 +4657,24 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { NODE_PROVIDER9_KEY_32, NODE_PROVIDER10_KEY_32, ]; + let customers_accounts = [ + CUSTOMER100_KEY_32, + CUSTOMER101_KEY_32, + CUSTOMER102_KEY_32, + CUSTOMER103_KEY_32, + CUSTOMER104_KEY_32, + CUSTOMER105_KEY_32, + CUSTOMER106_KEY_32, + CUSTOMER107_KEY_32, + CUSTOMER108_KEY_32, + CUSTOMER109_KEY_32, + ]; + let era = 100; let user_batch_size = 10; let node_batch_size = 10; let mut batch_user_index = 0; let mut batch_node_index = 0; - let bucket_id1: BucketId = 1; let mut payees: Vec> = Vec::new(); let mut node_batch: Vec<(NodePubKey, NodeUsage)> = Vec::new(); let mut total_nodes_usage = NodeUsage::default(); @@ -4639,9 +4703,9 @@ 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<(AccountId, String, BucketId, CustomerUsage)> = Vec::new(); - for user_id in 100u8..100 + num_users { + let mut payers: Vec> = Vec::new(); + let mut user_batch: Vec<(NodePubKey, BucketId, CustomerUsage)> = Vec::new(); + for user_id in 100..100 + num_users { let user_usage = CustomerUsage { transferred_bytes: generate_random_u64(&mock_randomness, min, max), stored_bytes: (generate_random_u64(&mock_randomness, min, max)) as i64, @@ -4659,12 +4723,8 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { .unwrap(); total_charge += expected_charge; - user_batch.push(( - AccountId::from([user_id; 32]), - node_id.clone(), - bucket_id1, - user_usage, - )); + let bucket_id = user_id.into(); + user_batch.push((node_key.clone(), bucket_id, user_usage)); if user_batch.len() == user_batch_size { payers.push(user_batch.clone()); user_batch.clear(); @@ -4699,14 +4759,15 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { MMRProof::default(), )); - for (customer_id, _node_id, _bucket_id, usage) in batch.iter() { + for (i, (_node_key, bucket_id, usage)) in batch.iter().enumerate() { let charge = calculate_charge_for_month(cluster_id, usage.clone()); + let customer_id = customers_accounts[i].clone(); System::assert_has_event( Event::Charged { cluster_id, era, - bucket_id: bucket_id1, + bucket_id: bucket_id.clone(), customer_id: customer_id.clone(), batch_index: batch_user_index, amount: charge, @@ -4829,22 +4890,15 @@ fn end_rewarding_providers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([3; 32]); - let user2 = AccountId::from([4; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id: String = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 1; let batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; - let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; + 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 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 @@ -5039,7 +5093,7 @@ fn end_rewarding_providers_works() { number_of_gets: usage1.number_of_gets * 2 / 3, }; let total_node_usage = node_usage1.clone(); - let payers = vec![(user1, node_id.clone(), bucket_id1, usage1)]; + let payers = vec![(node_key.clone(), bucket_id1, usage1)]; let payees = vec![(node_key, node_usage1)]; assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); @@ -5118,22 +5172,16 @@ fn end_billing_report_fails_uninitialised() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let root_account = AccountId::from([1; 32]); let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([3; 32]); - let user2 = AccountId::from([4; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 1; let batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let payers1 = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; - let payers2 = vec![(user2, node_id.clone(), bucket_id2, CustomerUsage::default())]; + 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 payees = vec![(node_key, NodeUsage::default())]; let total_node_usage = NodeUsage::default(); @@ -5305,17 +5353,15 @@ fn end_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; let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([3; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let batch_index = 0; let total_node_usage = NodeUsage::default(); - let bucket_id1 = 1; - let payers = vec![(user1, node_id.clone(), bucket_id1, CustomerUsage::default())]; - let payees = vec![(NodePubKey::StoragePubKey(NODE1_PUB_KEY_32), NodeUsage::default())]; + let bucket_id3 = BUCKET_ID3; + let payers = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; + let payees = vec![(node_key.clone(), NodeUsage::default())]; assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 45dc868e0..2092218cc 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -560,9 +560,9 @@ pub mod pallet { } } - pub struct CustomerBatch { + pub struct CustomerBatch { pub(crate) batch_index: BatchIndex, - pub(crate) payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)>, + pub(crate) payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, pub(crate) batch_proof: MMRProof, } @@ -1124,23 +1124,10 @@ pub mod pallet { // todo! factor out as macro as this is repetitive match Self::prepare_send_charging_customers_batch(&cluster_id, batch_size.into()) { Ok(Some((era_id, batch_payout))) => { - let payers_log: Vec<(String, String, BucketId, CustomerUsage)> = - batch_payout - .payers - .clone() - .into_iter() - .map(|(acc_id, node_id, bucket_id, customer_usage)| { - let account_id: T::AccountIdConverter = acc_id.into(); - let account_id_32: AccountId32 = account_id.into(); - let account_ref: &[u8; 32] = account_id_32.as_ref(); - (hex::encode(account_ref), node_id, bucket_id, customer_usage) - }) - .collect(); log::info!( - "๐Ÿญ๐ŸŽ prepare_send_charging_customers_batch processed successfully for cluster_id: {:?}, era_id: {:?} , batch_payout: {:?}", + "๐ŸŽ prepare_send_charging_customers_batch processed successfully for cluster_id: {:?}, era_id: {:?}", cluster_id, - era_id, - payers_log + era_id ); if let Some((_, res)) = signer.send_signed_transaction(|_acc| { @@ -1309,7 +1296,7 @@ pub mod pallet { "๐ŸŽ prepare_send_rewarding_providers_batch processed successfully for cluster_id: {:?}, era_id: {:?}", cluster_id, era_id - ); + ); if let Some((_, res)) = signer.send_signed_transaction(|_acc| { Call::send_rewarding_providers_batch { @@ -2175,7 +2162,7 @@ pub mod pallet { pub(crate) fn prepare_send_charging_customers_batch( cluster_id: &ClusterId, batch_size: usize, - ) -> Result)>, Vec> { + ) -> Result, Vec> { if let Some((era_id, start, end)) = Self::get_era_for_payout(cluster_id, EraValidationStatus::PayoutInProgress) { @@ -2239,7 +2226,7 @@ pub mod pallet { era_id: DdcEra, customers_total_activity: Vec, customers_activity_batch_roots: Vec, - ) -> Result)>, Vec> { + ) -> Result, Vec> { let batch_index = T::PayoutVisitor::get_next_customer_batch_for_payment( cluster_id, era_id, ) @@ -2289,17 +2276,16 @@ pub mod pallet { payers: customers_activity_batched[i] .iter() .map(|activity| { - let account_id = - T::CustomerVisitor::get_bucket_owner(&activity.bucket_id) - .unwrap(); - let node_id = activity.node_id.clone(); + 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 { transferred_bytes: activity.transferred_bytes, stored_bytes: activity.stored_bytes, number_of_puts: activity.number_of_puts, number_of_gets: activity.number_of_gets, }; - (account_id, node_id, activity.bucket_id, customer_usage) + (node_key, bucket_id, customer_usage) }) .collect(), batch_proof, @@ -4030,7 +4016,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)>, + payers: Vec<(NodePubKey, BucketId, CustomerUsage)>, batch_proof: MMRProof, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -4189,7 +4175,7 @@ pub mod pallet { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, String, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: &MMRProof, ) -> bool { let validation_era = EraValidations::::get(cluster_id, era_id); @@ -4200,8 +4186,9 @@ pub mod pallet { let activity_hashes = payers .iter() - .map(|(_bucket_owner, node_id, bucket_id, usage)| { + .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()); diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index 1f7d479e9..0d79e64e8 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -406,7 +406,7 @@ impl PayoutVisitor for MockPayoutVisitor { _cluster_id: ClusterId, _era_id: DdcEra, _batch_index: BatchIndex, - _payers: &[(T::AccountId, String, BucketId, CustomerUsage)], + _payers: &[(NodePubKey, BucketId, CustomerUsage)], _batch_proof: MMRProof, ) -> DispatchResult { Ok(()) diff --git a/primitives/src/traits/bucket.rs b/primitives/src/traits/bucket.rs index 908d005d0..2e7dadaf8 100644 --- a/primitives/src/traits/bucket.rs +++ b/primitives/src/traits/bucket.rs @@ -1,4 +1,4 @@ -use sp_runtime::DispatchResult; +use sp_runtime::{DispatchError, DispatchResult}; use crate::{BucketId, BucketVisitorError, ClusterId, CustomerUsage}; @@ -12,6 +12,8 @@ pub trait BucketManager { } pub trait BucketVisitor { + fn get_bucket_owner_id(bucket_id: BucketId) -> Result; + fn get_total_customer_usage( cluster_id: &ClusterId, bucket_id: BucketId, diff --git a/primitives/src/traits/payout.rs b/primitives/src/traits/payout.rs index 16f385a30..075845962 100644 --- a/primitives/src/traits/payout.rs +++ b/primitives/src/traits/payout.rs @@ -1,4 +1,3 @@ -use scale_info::prelude::string::String; use sp_runtime::DispatchResult; use crate::{ @@ -31,7 +30,7 @@ pub trait PayoutVisitor { cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, String, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: MMRProof, ) -> DispatchResult; diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index a975812ea..4d6f2edc2 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -1,7 +1,4 @@ use frame_system::Config; -use scale_info::prelude::string::String; -#[cfg(feature = "runtime-benchmarks")] -use sp_std::prelude::*; use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, @@ -15,7 +12,7 @@ pub trait ValidatorVisitor { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, - payers: &[(T::AccountId, String, BucketId, CustomerUsage)], + payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: &MMRProof, ) -> bool; fn is_providers_batch_valid( From 4c37f79505b730437d9c6985795e5fe1d8d344f4 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 03:58:02 +0100 Subject: [PATCH 09/20] fix: unussed variables are removed and clippy issues fixed --- .vscode/settings.json | 2 +- pallets/ddc-payouts/src/benchmarking.rs | 9 +- pallets/ddc-payouts/src/mock.rs | 14 +- pallets/ddc-payouts/src/tests.rs | 307 +++++++++--------------- 4 files changed, 126 insertions(+), 206 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7089a4652..0196fd738 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", - "editor.formatOnSave": false + "editor.formatOnSave": true } } \ No newline at end of file diff --git a/pallets/ddc-payouts/src/benchmarking.rs b/pallets/ddc-payouts/src/benchmarking.rs index f8f282a7a..c3aa81ff1 100644 --- a/pallets/ddc-payouts/src/benchmarking.rs +++ b/pallets/ddc-payouts/src/benchmarking.rs @@ -225,7 +225,7 @@ benchmarks! { }); let batch_index: BatchIndex = 0; - let payers: Vec<(T::AccountId, String, BucketId, CustomerUsage)> = (0..b).map(|i| { + let payers: Vec<(NodePubKey, BucketId, CustomerUsage)> = (0..b).map(|i| { let customer = create_account::("customer", i, i); if b % 2 == 0 { @@ -243,9 +243,12 @@ benchmarks! { number_of_puts: 5, // 5 puts }; let bucket_id: BucketId = 1; - let node_id: String = String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); + 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, + ])); - (customer, node_id, bucket_id, customer_usage) + (node_key, bucket_id, customer_usage) }).collect(); }: _(RawOrigin::Signed(dac_account.clone()), cluster_id, era, batch_index, payers, MMRProof::default()) diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index ec3d2f35f..f0fe0f0ce 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -299,8 +299,8 @@ where } if content_owner == account_2 { - assert!(USER2_BALANCE < amount); - amount_to_charge = USER2_BALANCE; // for user 2 + assert!(CUSTOMER2_BALANCE < amount); + amount_to_charge = CUSTOMER2_BALANCE; // for user 2 } let charge = amount_to_charge.saturated_into::>(); @@ -349,8 +349,10 @@ pub const VALIDATOR2_SCORE: u64 = 45; pub const VALIDATOR3_SCORE: u64 = 25; pub const PARTIAL_CHARGE: u128 = 10; -pub const USER2_BALANCE: u128 = 5; -pub const USER3_BALANCE: u128 = 1000; +// < PARTIAL_CHARGE +pub const CUSTOMER2_BALANCE: u128 = 5; +// > PARTIAL_CHARGE +pub const CUSTOMER3_BALANCE: u128 = 1000; pub const NODE1_PUB_KEY_32: AccountId32 = AccountId32::new([ 48, 47, 147, 125, 243, 160, 236, 76, 101, 142, 129, 34, 67, 158, 116, 141, 34, 116, 66, 235, @@ -728,8 +730,8 @@ impl ExtBuilder { let _balance_genesis = pallet_balances::GenesisConfig:: { balances: vec![ (CUSTOMER1_KEY_32, 10000000000000000000000000000), - (CUSTOMER2_KEY_32, USER2_BALANCE), // < PARTIAL_CHARGE - (CUSTOMER3_KEY_32, USER3_BALANCE), // > PARTIAL_CHARGE + (CUSTOMER2_KEY_32, CUSTOMER2_BALANCE), + (CUSTOMER3_KEY_32, CUSTOMER3_BALANCE), (CUSTOMER4_KEY_32, 1000000000000000000000000), (CUSTOMER5_KEY_32, 1000000000000000000000000), (CUSTOMER6_KEY_32, 1000000000000000000000000), diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index cb84ab02a..91f38a1fc 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -4,7 +4,7 @@ use chrono::{DateTime, NaiveDate, NaiveTime, Utc}; use ddc_primitives::ClusterId; use frame_support::{assert_noop, assert_ok, error::BadOrigin, traits::Randomness}; use sp_core::H256; -use sp_runtime::{AccountId32, Perquintill}; +use sp_runtime::Perquintill; use super::{mock::*, *}; @@ -411,19 +411,20 @@ fn send_charging_customers_batch_works() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let usage1 = CustomerUsage { // should pass without debt @@ -495,10 +496,10 @@ fn send_charging_customers_batch_works() { let usage4_charge = calculate_charge_for_month(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_month(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_month(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -519,7 +520,7 @@ fn send_charging_customers_batch_works() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -672,20 +673,15 @@ fn end_charging_customers_works_small_usage_1_hour() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user6 = AccountId::from([6; 32]); - let user7 = AccountId::from([7; 32]); + let user6: AccountId = CUSTOMER6_KEY_32; + let user7 = CUSTOMER7_KEY_32; let cluster_id = HIGH_FEES_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let batch_index = 0; - let bucket_id6: BucketId = 6; - let bucket_id7: BucketId = 7; + let bucket_id6: BucketId = BUCKET_ID6; + let bucket_id7: BucketId = BUCKET_ID7; let usage6 = CustomerUsage { transferred_bytes: 0, @@ -878,18 +874,20 @@ fn send_charging_customers_batch_works_for_day() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); + let usage1 = CustomerUsage { // should pass without debt transferred_bytes: 23452345, @@ -918,13 +916,6 @@ fn send_charging_customers_batch_works_for_day() { number_of_puts: 3456345, number_of_gets: 242334563456423, }; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); - let payers1 = vec![ (node_key.clone(), bucket_id2, usage2.clone()), (node_key.clone(), bucket_id4, usage4.clone()), @@ -968,10 +959,10 @@ fn send_charging_customers_batch_works_for_day() { let usage4_charge = calculate_charge_for_day(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_day(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_day(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -992,7 +983,7 @@ fn send_charging_customers_batch_works_for_day() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -1145,25 +1136,19 @@ fn send_charging_customers_batch_works_for_day_free_storage() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = STORAGE_ZERO_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); - + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let usage1 = CustomerUsage { // should pass without debt @@ -1236,10 +1221,10 @@ fn send_charging_customers_batch_works_for_day_free_storage() { let usage4_charge = calculate_charge_for_day(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_day(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_day(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -1260,7 +1245,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -1413,24 +1398,19 @@ fn send_charging_customers_batch_works_for_day_free_stream() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = STREAM_ZERO_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let usage1 = CustomerUsage { // should pass without debt @@ -1502,10 +1482,10 @@ fn send_charging_customers_batch_works_for_day_free_stream() { let usage4_charge = calculate_charge_for_day(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_day(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_day(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -1526,7 +1506,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -1679,24 +1659,19 @@ fn send_charging_customers_batch_works_for_day_free_get() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = GET_ZERO_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let usage1 = CustomerUsage { // should pass without debt @@ -1769,10 +1744,10 @@ fn send_charging_customers_batch_works_for_day_free_get() { let usage4_charge = calculate_charge_for_day(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_day(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_day(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -1793,7 +1768,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -1946,24 +1921,19 @@ fn send_charging_customers_batch_works_for_day_free_put() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = PUT_ZERO_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let usage1 = CustomerUsage { // should pass without debt @@ -2036,10 +2006,10 @@ fn send_charging_customers_batch_works_for_day_free_put() { let usage4_charge = calculate_charge_for_day(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_day(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_day(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -2060,7 +2030,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -2213,24 +2183,19 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); - let user2_debtor = AccountId::from([2; 32]); - let user3_debtor = AccountId::from([3; 32]); - let user4 = AccountId::from([4; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; + let user2_debtor: AccountId = CUSTOMER2_KEY_32; + let user3_debtor: AccountId = CUSTOMER3_KEY_32; + let user4: AccountId = CUSTOMER4_KEY_32; let cluster_id = STORAGE_STREAM_ZERO_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 3; let mut batch_index = 0; - let bucket_id1: BucketId = 1; - let bucket_id2: BucketId = 2; - let bucket_id3: BucketId = 3; - let bucket_id4: BucketId = 4; + let bucket_id1: BucketId = BUCKET_ID1; + let bucket_id2: BucketId = BUCKET_ID2; + let bucket_id3: BucketId = BUCKET_ID3; + let bucket_id4: BucketId = BUCKET_ID4; let usage1 = CustomerUsage { // should pass without debt @@ -2302,10 +2267,10 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { let usage4_charge = calculate_charge_for_day(cluster_id, usage4.clone()); let user2_debt = DdcPayouts::debtor_customers(cluster_id, user2_debtor.clone()).unwrap(); let expected_charge2 = calculate_charge_for_day(cluster_id, usage2.clone()); - let mut debt = expected_charge2 - USER2_BALANCE; + let mut debt = expected_charge2 - CUSTOMER2_BALANCE; assert_eq!(user2_debt, debt); - let ratio = Perquintill::from_rational(USER2_BALANCE, expected_charge2); + let ratio = Perquintill::from_rational(CUSTOMER2_BALANCE, expected_charge2); let mut charge2 = calculate_charge_parts_for_day(cluster_id, usage2); charge2.storage = ratio * charge2.storage; charge2.transfer = ratio * charge2.transfer; @@ -2326,7 +2291,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { customer_id: user2_debtor.clone(), bucket_id: bucket_id2, batch_index, - charged: USER2_BALANCE, + charged: CUSTOMER2_BALANCE, expected_to_charge: expected_charge2, } .into(), @@ -2477,20 +2442,13 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { fn send_charging_customers_batch_works_zero_fees() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); - let user5 = AccountId::from([5; 32]); let cluster_id = ONE_CLUSTER_ID; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let batch_index = 0; - let bucket_id5: BucketId = 5; + let bucket_id5: BucketId = BUCKET_ID5; let usage5 = CustomerUsage { // should pass without debt transferred_bytes: 1024, @@ -2565,18 +2523,12 @@ fn end_charging_customers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([100; 32]); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 2; let batch_index = 1; - let bucket_id1: BucketId = 1; + let bucket_id1: BucketId = BUCKET_ID1; let payers = vec![(node_key.clone(), bucket_id1, CustomerUsage::default())]; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -2665,18 +2617,13 @@ fn end_charging_customers_works() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); + let user1 = CUSTOMER1_KEY_32; let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let batch_index = 0; - let bucket_id1: BucketId = 1; + let bucket_id1: BucketId = BUCKET_ID1; let usage1 = CustomerUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, @@ -2854,18 +2801,13 @@ fn end_charging_customers_works_zero_fees() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); + let user1: AccountId = CUSTOMER1_KEY_32; let cluster_id = ClusterId::zero(); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let batch_index = 0; - let bucket_id1: BucketId = 1; + let bucket_id1: BucketId = BUCKET_ID1; let usage1 = CustomerUsage { transferred_bytes: 23452345, stored_bytes: 3345234523, @@ -2987,18 +2929,12 @@ fn begin_rewarding_providers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([3; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; let batch_index = 1; - let bucket_id1: BucketId = 1; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + 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 node_usage = NodeUsage::default(); let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -3128,18 +3064,12 @@ fn begin_rewarding_providers_works() { System::set_block_number(1); let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 0; let batch_index = 0; - let bucket_id1: BucketId = 1; - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + 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 start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -3365,16 +3295,9 @@ fn send_rewarding_providers_batch_fails_uninitialised() { fn send_rewarding_providers_batch_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); - let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let max_node_batch_index = 1; @@ -3850,7 +3773,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { Event::Charged { cluster_id, era, - bucket_id: bucket_id.clone(), + bucket_id: *bucket_id, customer_id: customer_id.clone(), batch_index: batch_user_index, amount: charge, @@ -4171,7 +4094,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { Event::Charged { cluster_id, era, - bucket_id: bucket_id.clone(), + bucket_id: *bucket_id, customer_id: customer_id.clone(), batch_index: batch_user_index, amount: charge, @@ -4488,7 +4411,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { cluster_id, era, customer_id: customer_id.clone(), - bucket_id: bucket_id.clone(), + bucket_id: *bucket_id, batch_index: batch_user_index, amount: charge, } @@ -4767,7 +4690,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { Event::Charged { cluster_id, era, - bucket_id: bucket_id.clone(), + bucket_id: *bucket_id, customer_id: customer_id.clone(), batch_index: batch_user_index, amount: charge, @@ -5060,20 +4983,13 @@ fn end_rewarding_providers_works() { System::set_block_number(1); 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; let dac_account = AccountId::from([2; 32]); - let user1 = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); - let node_id = - String::from("0x302f937df3a0ec4c658e8122439e748d227442ebd493cef521a1e14943844395"); - 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, - ])); + let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; let max_batch_index = 0; let batch_index = 0; @@ -5347,7 +5263,6 @@ fn end_billing_report_works() { System::set_block_number(1); 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(); From 6b3b52dff69431a6b800e021b9ebc8cb3c029ddc Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 04:05:46 +0100 Subject: [PATCH 10/20] chore: runtime version is upgraded --- runtime/cere-dev/src/lib.rs | 2 +- runtime/cere/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 4aa9f4801..a80286534 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -156,7 +156,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 61002, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 22, + transaction_version: 23, state_version: 0, }; diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 6521464b0..61b2de4af 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -150,7 +150,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 61002, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 21, + transaction_version: 23, state_version: 0, }; From c0ba34cb7158a1d541688a1682c617aaea0338ce Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 13:04:12 +0100 Subject: [PATCH 11/20] chore: deprecated extrinsic is removed from tests --- pallets/ddc-payouts/src/tests.rs | 112 ++++++------------------------- 1 file changed, 20 insertions(+), 92 deletions(-) diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 91f38a1fc..70819f1c4 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -8,37 +8,10 @@ use sp_runtime::Perquintill; use super::{mock::*, *}; -#[test] -fn set_authorised_caller_works() { - ExtBuilder.build_and_execute(|| { - System::set_block_number(1); - - let root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); - - assert_noop!( - DdcPayouts::set_authorised_caller( - RuntimeOrigin::signed(root_account), - dac_account.clone() - ), - BadOrigin - ); - - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - - System::assert_last_event( - Event::AuthorisedCaller { authorised_caller: dac_account.clone() }.into(), - ); - - assert_eq!(DdcPayouts::authorised_caller().unwrap(), dac_account); - }) -} - #[test] fn begin_billing_report_fails_for_unauthorised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([1; 20]); let era = 100; let start_date = NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(); // April 1st @@ -48,8 +21,6 @@ fn begin_billing_report_fails_for_unauthorised() { 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::set_authorised_caller(RuntimeOrigin::root(), dac_account)); - assert_noop!( DdcPayouts::begin_billing_report( RuntimeOrigin::signed(AccountId::from([3; 32])), @@ -89,8 +60,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account), cluster_id, @@ -136,8 +105,6 @@ fn begin_charging_customers_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), DAC_ACCOUNT_ID.into())); - assert_noop!( DdcPayouts::begin_charging_customers( RuntimeOrigin::signed(DAC_ACCOUNT_ID.into()), @@ -166,8 +133,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -242,8 +207,6 @@ fn send_charging_customers_batch_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_noop!( DdcPayouts::send_charging_customers_batch( RuntimeOrigin::signed(dac_account.clone()), @@ -466,7 +429,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -481,7 +443,7 @@ fn send_charging_customers_batch_works() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -549,7 +511,7 @@ fn send_charging_customers_batch_works() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -706,7 +668,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -721,7 +682,7 @@ fn end_charging_customers_works_small_usage_1_hour() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -929,7 +890,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -944,7 +904,7 @@ fn send_charging_customers_batch_works_for_day() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -1012,7 +972,7 @@ fn send_charging_customers_batch_works_for_day() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -1191,7 +1151,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -1206,7 +1165,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -1274,7 +1233,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -1452,7 +1411,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -1467,7 +1425,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -1535,7 +1493,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -1714,7 +1672,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -1729,7 +1686,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -1797,7 +1754,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -1976,7 +1933,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -1991,7 +1947,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -2059,7 +2015,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -2237,7 +2193,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -2252,7 +2207,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( @@ -2320,7 +2275,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { .into(), ); - assert_eq!(System::events().len(), 5 + 3 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 4 + 3 + 1); // 1 for Currency::transfer // batch 2 let mut before_total_customer_charge = report.total_customer_charge; @@ -2464,7 +2419,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -2479,7 +2433,7 @@ fn send_charging_customers_batch_works_zero_fees() { era, max_batch_index, )); - assert_eq!(System::events().len(), 3); + assert_eq!(System::events().len(), 2); // batch 1 let mut report = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); @@ -2551,8 +2505,6 @@ fn end_charging_customers_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_noop!( DdcPayouts::end_charging_customers( RuntimeOrigin::signed(dac_account.clone()), @@ -2638,8 +2590,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -2680,7 +2630,7 @@ fn end_charging_customers_works() { let mut balance = Balances::free_balance(DdcPayouts::account_id()); assert_eq!(balance - Balances::minimum_balance(), charge); - assert_eq!(System::events().len(), 4 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 3 + 1); // 1 for Currency::transfer assert_ok!(DdcPayouts::end_charging_customers( RuntimeOrigin::signed(dac_account), @@ -2707,7 +2657,7 @@ fn end_charging_customers_works() { ); let transfers = 3 + 3 + 3 * 3; // for Currency::transfer - assert_eq!(System::events().len(), 8 + 1 + 3 + transfers); + assert_eq!(System::events().len(), 7 + 1 + 3 + transfers); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_eq!(report_after.state, PayoutState::CustomersChargedWithFees); @@ -2822,8 +2772,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -2864,7 +2812,7 @@ fn end_charging_customers_works_zero_fees() { let mut balance = Balances::free_balance(DdcPayouts::account_id()); assert_eq!(balance - Balances::minimum_balance(), charge); - assert_eq!(System::events().len(), 4 + 1); // 1 for Currency::transfer + assert_eq!(System::events().len(), 3 + 1); // 1 for Currency::transfer assert_ok!(DdcPayouts::end_charging_customers( RuntimeOrigin::signed(dac_account), @@ -2873,7 +2821,7 @@ fn end_charging_customers_works_zero_fees() { )); System::assert_has_event(Event::ChargingFinished { cluster_id, era }.into()); - assert_eq!(System::events().len(), 5 + 1); + assert_eq!(System::events().len(), 4 + 1); let report_after = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_eq!(report_after.state, PayoutState::CustomersChargedWithFees); @@ -2966,8 +2914,6 @@ fn begin_rewarding_providers_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_noop!( DdcPayouts::begin_rewarding_providers( RuntimeOrigin::signed(dac_account.clone()), @@ -3079,8 +3025,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -3176,8 +3120,6 @@ fn send_rewarding_providers_batch_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_noop!( DdcPayouts::send_rewarding_providers_batch( RuntimeOrigin::signed(dac_account.clone()), @@ -3365,8 +3307,6 @@ 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::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -3740,7 +3680,6 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -4061,7 +4000,6 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -4377,7 +4315,6 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -4657,7 +4594,6 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { payers.push(user_batch.clone()); } - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -4845,8 +4781,6 @@ fn end_rewarding_providers_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_noop!( DdcPayouts::end_rewarding_providers( RuntimeOrigin::signed(dac_account.clone()), @@ -5012,8 +4946,6 @@ fn end_rewarding_providers_works() { let payers = vec![(node_key.clone(), bucket_id1, usage1)]; let payees = vec![(node_key, node_usage1)]; - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, @@ -5111,8 +5043,6 @@ fn end_billing_report_fails_uninitialised() { BadOrigin ); - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_noop!( DdcPayouts::end_billing_report( RuntimeOrigin::signed(dac_account.clone()), @@ -5278,8 +5208,6 @@ fn end_billing_report_works() { let payers = vec![(node_key.clone(), bucket_id3, CustomerUsage::default())]; let payees = vec![(node_key.clone(), NodeUsage::default())]; - assert_ok!(DdcPayouts::set_authorised_caller(RuntimeOrigin::root(), dac_account.clone())); - assert_ok!(DdcPayouts::begin_billing_report( RuntimeOrigin::signed(dac_account.clone()), cluster_id, From fd6423966eff0a6b6800ec4caca6dbf06bd87e2d Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 13:19:35 +0100 Subject: [PATCH 12/20] chore: unused account is removed from tests --- pallets/ddc-payouts/src/mock.rs | 10 +- pallets/ddc-payouts/src/tests.rs | 386 +++++++++++++++---------------- 2 files changed, 185 insertions(+), 211 deletions(-) diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index f0fe0f0ce..6480e3cc4 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -198,13 +198,7 @@ where unimplemented!() } fn is_ocw_validator(caller: T::AccountId) -> bool { - let account_id: [u8; 32] = [123; 32]; - let dac: [u8; 32] = DAC_ACCOUNT_ID; - let validators = [ - T::AccountId::decode(&mut &dac[..]).unwrap(), - T::AccountId::decode(&mut &account_id[..]).unwrap(), - ]; - validators.contains(&caller) + caller == VALIDATOR_OCW_KEY_32.into() } fn is_customers_batch_valid( _cluster_id: ClusterId, @@ -337,12 +331,12 @@ impl CustomerDepositor for TestCustomerDepositor { } } -pub const DAC_ACCOUNT_ID: [u8; 32] = [2; 32]; 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]; pub const VALIDATOR2_ACCOUNT_ID: [u8; 32] = [222; 32]; pub const VALIDATOR3_ACCOUNT_ID: [u8; 32] = [250; 32]; +pub const VALIDATOR_OCW_KEY_32: AccountId32 = AccountId32::new([123; 32]); pub const VALIDATOR1_SCORE: u64 = 30; pub const VALIDATOR2_SCORE: u64 = 45; diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 70819f1c4..9dd8319fe 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -50,18 +50,16 @@ fn begin_billing_report_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 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_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, start_era, @@ -80,14 +78,14 @@ fn begin_billing_report_works() { #[test] fn begin_charging_customers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let dac_account = AccountId::from([3; 32]); + let fake_ocw_account = AccountId::from([124; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; assert_noop!( DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(fake_ocw_account), cluster_id, era, max_batch_index, @@ -107,7 +105,7 @@ fn begin_charging_customers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(DAC_ACCOUNT_ID.into()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.into()), cluster_id, era, max_batch_index, @@ -122,19 +120,17 @@ fn begin_charging_customers_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; 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_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -142,7 +138,7 @@ fn begin_charging_customers_works() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, max_batch_index, @@ -160,7 +156,6 @@ fn begin_charging_customers_works() { fn send_charging_customers_batch_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; @@ -209,7 +204,7 @@ fn send_charging_customers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -220,7 +215,7 @@ fn send_charging_customers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -229,7 +224,7 @@ fn send_charging_customers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -240,7 +235,7 @@ fn send_charging_customers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -248,7 +243,7 @@ fn send_charging_customers_batch_fails_uninitialised() { let payers1 = vec![(node_key, bucket_id4, CustomerUsage::default())]; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -258,7 +253,7 @@ fn send_charging_customers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -270,7 +265,7 @@ fn send_charging_customers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -373,7 +368,6 @@ fn send_charging_customers_batch_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -430,7 +424,7 @@ fn send_charging_customers_batch_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -438,7 +432,7 @@ fn send_charging_customers_batch_works() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -447,7 +441,7 @@ fn send_charging_customers_batch_works() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -517,7 +511,7 @@ fn send_charging_customers_batch_works() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -566,7 +560,7 @@ fn send_charging_customers_batch_works() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -634,7 +628,6 @@ fn end_charging_customers_works_small_usage_1_hour() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user6: AccountId = CUSTOMER6_KEY_32; let user7 = CUSTOMER7_KEY_32; let cluster_id = HIGH_FEES_CLUSTER_ID; @@ -669,7 +662,7 @@ fn end_charging_customers_works_small_usage_1_hour() { let end_era: i64 = start_era + (1.0 * 1.0 * 3600.0) as i64; // 1 hour assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -677,7 +670,7 @@ fn end_charging_customers_works_small_usage_1_hour() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -686,7 +679,7 @@ fn end_charging_customers_works_small_usage_1_hour() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -751,7 +744,7 @@ fn end_charging_customers_works_small_usage_1_hour() { assert_eq!(balance, 0); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, )); @@ -834,7 +827,6 @@ fn send_charging_customers_batch_works_for_day() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -891,7 +883,7 @@ fn send_charging_customers_batch_works_for_day() { let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -899,7 +891,7 @@ fn send_charging_customers_batch_works_for_day() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -908,7 +900,7 @@ fn send_charging_customers_batch_works_for_day() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -978,7 +970,7 @@ fn send_charging_customers_batch_works_for_day() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1027,7 +1019,7 @@ fn send_charging_customers_batch_works_for_day() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -1095,7 +1087,6 @@ fn send_charging_customers_batch_works_for_day_free_storage() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -1152,7 +1143,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -1160,7 +1151,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -1169,7 +1160,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1239,7 +1230,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1288,7 +1279,7 @@ fn send_charging_customers_batch_works_for_day_free_storage() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -1356,7 +1347,6 @@ fn send_charging_customers_batch_works_for_day_free_stream() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -1412,7 +1402,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -1420,7 +1410,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -1429,7 +1419,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1499,7 +1489,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1548,7 +1538,7 @@ fn send_charging_customers_batch_works_for_day_free_stream() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -1616,7 +1606,6 @@ fn send_charging_customers_batch_works_for_day_free_get() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -1673,7 +1662,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -1681,7 +1670,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -1690,7 +1679,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1760,7 +1749,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -1809,7 +1798,7 @@ fn send_charging_customers_batch_works_for_day_free_get() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -1877,7 +1866,6 @@ fn send_charging_customers_batch_works_for_day_free_put() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -1934,7 +1922,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -1942,7 +1930,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -1951,7 +1939,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2021,7 +2009,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2070,7 +2058,7 @@ fn send_charging_customers_batch_works_for_day_free_put() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -2138,7 +2126,6 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let user2_debtor: AccountId = CUSTOMER2_KEY_32; let user3_debtor: AccountId = CUSTOMER3_KEY_32; @@ -2194,7 +2181,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { let end_era: i64 = start_era + (1.0 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -2202,7 +2189,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2211,7 +2198,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { // batch 1 assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2281,7 +2268,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { let mut before_total_customer_charge = report.total_customer_charge; batch_index += 1; assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2330,7 +2317,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { batch_index += 1; before_total_customer_charge = report.total_customer_charge.clone(); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -2397,7 +2384,7 @@ fn send_charging_customers_batch_works_for_day_free_storage_stream() { fn send_charging_customers_batch_works_zero_fees() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); + let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -2420,7 +2407,7 @@ fn send_charging_customers_batch_works_zero_fees() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -2428,7 +2415,7 @@ fn send_charging_customers_batch_works_zero_fees() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2440,7 +2427,7 @@ fn send_charging_customers_batch_works_zero_fees() { 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(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -2476,7 +2463,6 @@ fn send_charging_customers_batch_works_zero_fees() { fn end_charging_customers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([100; 32]); - let dac_account = AccountId::from([123; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -2507,7 +2493,7 @@ fn end_charging_customers_fails_uninitialised() { assert_noop!( DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -2515,7 +2501,7 @@ fn end_charging_customers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -2524,7 +2510,7 @@ fn end_charging_customers_fails_uninitialised() { assert_noop!( DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -2532,7 +2518,7 @@ fn end_charging_customers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2540,7 +2526,7 @@ fn end_charging_customers_fails_uninitialised() { assert_noop!( DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -2548,7 +2534,7 @@ fn end_charging_customers_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2557,7 +2543,11 @@ fn end_charging_customers_fails_uninitialised() { )); assert_noop!( - DdcPayouts::end_charging_customers(RuntimeOrigin::signed(dac_account), cluster_id, era,), + DdcPayouts::end_charging_customers( + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + cluster_id, + era, + ), Error::::BatchesMissed ); }) @@ -2568,7 +2558,6 @@ fn end_charging_customers_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1 = CUSTOMER1_KEY_32; let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -2591,7 +2580,7 @@ fn end_charging_customers_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -2599,14 +2588,14 @@ fn end_charging_customers_works() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, )); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2633,7 +2622,7 @@ fn end_charging_customers_works() { assert_eq!(System::events().len(), 3 + 1); // 1 for Currency::transfer assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, )); @@ -2750,7 +2739,6 @@ fn end_charging_customers_works_zero_fees() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let user1: AccountId = CUSTOMER1_KEY_32; let cluster_id = ClusterId::zero(); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -2773,7 +2761,7 @@ fn end_charging_customers_works_zero_fees() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -2781,14 +2769,14 @@ fn end_charging_customers_works_zero_fees() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, )); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2815,7 +2803,7 @@ fn end_charging_customers_works_zero_fees() { assert_eq!(System::events().len(), 3 + 1); // 1 for Currency::transfer assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, )); @@ -2876,7 +2864,6 @@ fn end_charging_customers_works_zero_fees() { fn begin_rewarding_providers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; @@ -2916,7 +2903,7 @@ fn begin_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2926,7 +2913,7 @@ fn begin_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -2935,7 +2922,7 @@ fn begin_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2945,7 +2932,7 @@ fn begin_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2953,7 +2940,7 @@ fn begin_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2963,7 +2950,7 @@ fn begin_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -2973,7 +2960,7 @@ fn begin_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -2983,7 +2970,7 @@ fn begin_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index + 1, @@ -2993,7 +2980,7 @@ fn begin_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, max_batch_index, @@ -3009,7 +2996,6 @@ fn begin_rewarding_providers_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 0; @@ -3026,7 +3012,7 @@ fn begin_rewarding_providers_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -3037,14 +3023,14 @@ fn begin_rewarding_providers_works() { assert_eq!(report.state, PayoutState::Initialized); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, )); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3053,13 +3039,13 @@ fn begin_rewarding_providers_works() { )); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, max_batch_index, @@ -3077,7 +3063,6 @@ fn begin_rewarding_providers_works() { fn send_rewarding_providers_batch_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -3122,7 +3107,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3133,7 +3118,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -3142,7 +3127,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3153,7 +3138,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -3161,7 +3146,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3172,7 +3157,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3182,7 +3167,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3193,7 +3178,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index + 1, @@ -3203,7 +3188,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3214,14 +3199,14 @@ fn send_rewarding_providers_batch_fails_uninitialised() { ); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, batch_index, @@ -3237,7 +3222,6 @@ fn send_rewarding_providers_batch_fails_uninitialised() { fn send_rewarding_providers_batch_works() { ExtBuilder.build_and_execute(|| { System::set_block_number(1); - let dac_account = AccountId::from([123; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -3308,7 +3292,7 @@ fn send_rewarding_providers_batch_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -3316,14 +3300,14 @@ fn send_rewarding_providers_batch_works() { )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, )); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -3333,7 +3317,7 @@ fn send_rewarding_providers_batch_works() { let report_before = DdcPayouts::active_billing_reports(cluster_id, era).unwrap(); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); @@ -3362,7 +3346,7 @@ fn send_rewarding_providers_batch_works() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_node_batch_index, @@ -3370,7 +3354,7 @@ fn send_rewarding_providers_batch_works() { )); assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_node_index, @@ -3460,7 +3444,7 @@ fn send_rewarding_providers_batch_works() { // batch 2 assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_node_index + 1, @@ -3521,7 +3505,7 @@ 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(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, )); @@ -3535,7 +3519,6 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { let num_nodes = 10; let num_users = 5; - let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -3681,14 +3664,14 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { } assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, end_era, )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payers.len() - 1) as u16, @@ -3696,7 +3679,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { for batch in payers.iter() { assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_user_index, @@ -3731,7 +3714,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { assert_eq!(balance1 - Balances::minimum_balance(), total_charge); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); @@ -3767,7 +3750,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payees.len() - 1) as u16, @@ -3777,7 +3760,7 @@ 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(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_node_index, @@ -3842,7 +3825,6 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let num_nodes = 10; let num_users = 5; - let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -4001,14 +3983,14 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { } assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, end_era, )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payers.len() - 1) as u16, @@ -4016,7 +3998,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { for batch in payers.iter() { assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_user_index, @@ -4051,7 +4033,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { assert_eq!(balance1 - Balances::minimum_balance(), total_charge); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); @@ -4087,7 +4069,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payees.len() - 1) as u16, @@ -4097,7 +4079,7 @@ 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(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_node_index, @@ -4162,7 +4144,6 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let num_nodes = 10; let num_users = 5; - let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -4316,14 +4297,14 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { } assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, end_era, )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payers.len() - 1) as u16, @@ -4331,7 +4312,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { for batch in payers.iter() { assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_user_index, @@ -4366,7 +4347,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { assert_eq!(balance1 - Balances::minimum_balance(), total_charge); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); @@ -4402,7 +4383,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payees.len() - 1) as u16, @@ -4412,7 +4393,7 @@ 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(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_node_index, @@ -4487,7 +4468,6 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { let max: u64 = 1024 * 1024; let num_nodes = 10; let num_users = 10; - let dac_account = AccountId::from([123; 32]); let bank = AccountId::from([1; 32]); let cluster_id = CERE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -4595,14 +4575,14 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { } assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, end_era, )); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payers.len() - 1) as u16, @@ -4610,7 +4590,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { for batch in payers.iter() { assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_user_index, @@ -4645,7 +4625,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { assert_eq!(balance1 - Balances::minimum_balance(), total_charge); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); @@ -4681,7 +4661,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, (payees.len() - 1) as u16, @@ -4691,7 +4671,7 @@ 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(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_node_index, @@ -4748,7 +4728,6 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { fn end_rewarding_providers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { let root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -4783,7 +4762,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4791,7 +4770,7 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -4800,7 +4779,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4808,7 +4787,7 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -4816,7 +4795,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4824,7 +4803,7 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -4834,7 +4813,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4842,7 +4821,7 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index + 1, @@ -4852,7 +4831,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4860,14 +4839,14 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4875,7 +4854,7 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -4884,7 +4863,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -4892,7 +4871,7 @@ fn end_rewarding_providers_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -4902,7 +4881,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, ), @@ -4921,7 +4900,6 @@ fn end_rewarding_providers_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 dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -4947,7 +4925,7 @@ fn end_rewarding_providers_works() { let payees = vec![(node_key, node_usage1)]; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -4958,14 +4936,14 @@ fn end_rewarding_providers_works() { assert_eq!(report.state, PayoutState::Initialized); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, )); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -4974,13 +4952,13 @@ fn end_rewarding_providers_works() { )); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -4988,7 +4966,7 @@ fn end_rewarding_providers_works() { )); assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -4997,7 +4975,7 @@ fn end_rewarding_providers_works() { )); assert_ok!(DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); @@ -5019,7 +4997,6 @@ fn end_billing_report_fails_uninitialised() { 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 root_account = AccountId::from([1; 32]); - let dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -5045,7 +5022,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5053,7 +5030,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -5062,7 +5039,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5070,7 +5047,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -5078,7 +5055,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5086,7 +5063,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -5096,7 +5073,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5104,7 +5081,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index + 1, @@ -5114,7 +5091,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5122,14 +5099,14 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5137,7 +5114,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -5146,7 +5123,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5154,7 +5131,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -5164,7 +5141,7 @@ fn end_billing_report_fails_uninitialised() { assert_noop!( DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, ), @@ -5172,7 +5149,7 @@ fn end_billing_report_fails_uninitialised() { ); assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index + 1, @@ -5181,7 +5158,11 @@ fn end_billing_report_fails_uninitialised() { )); assert_noop!( - DdcPayouts::end_billing_report(RuntimeOrigin::signed(dac_account), cluster_id, era,), + DdcPayouts::end_billing_report( + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), + cluster_id, + era, + ), Error::::NotExpectedState ); }) @@ -5197,7 +5178,6 @@ fn end_billing_report_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 dac_account = AccountId::from([2; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -5209,7 +5189,7 @@ fn end_billing_report_works() { let payees = vec![(node_key.clone(), NodeUsage::default())]; assert_ok!(DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, start_era, @@ -5220,14 +5200,14 @@ fn end_billing_report_works() { assert_eq!(report.state, PayoutState::Initialized); assert_ok!(DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, )); assert_ok!(DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -5236,13 +5216,13 @@ fn end_billing_report_works() { )); assert_ok!(DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_ok!(DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, max_batch_index, @@ -5250,7 +5230,7 @@ fn end_billing_report_works() { )); assert_ok!(DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, batch_index, @@ -5259,13 +5239,13 @@ fn end_billing_report_works() { )); assert_ok!(DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(dac_account.clone()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.clone()), cluster_id, era, )); assert_ok!(DdcPayouts::end_billing_report( - RuntimeOrigin::signed(dac_account), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, )); From c086919135d4be710010da54f9a9a839f84d8b2b Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 13:32:31 +0100 Subject: [PATCH 13/20] chore: unified faucet account in tests --- pallets/ddc-payouts/src/mock.rs | 3 +++ pallets/ddc-payouts/src/tests.rs | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 6480e3cc4..3d51fadcb 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -441,6 +441,8 @@ pub const CUSTOMER107_KEY_32: AccountId32 = AccountId32::new([107; 32]); pub const CUSTOMER108_KEY_32: AccountId32 = AccountId32::new([108; 32]); pub const CUSTOMER109_KEY_32: AccountId32 = AccountId32::new([109; 32]); +pub const BANK_KEY_32: AccountId32 = AccountId32::new([200; 32]); + pub const NO_FEE_CLUSTER_ID: ClusterId = ClusterId::zero(); pub const ONE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(4u8); pub const CERE_CLUSTER_ID: ClusterId = ClusterId::repeat_byte(10u8); @@ -730,6 +732,7 @@ impl ExtBuilder { (CUSTOMER5_KEY_32, 1000000000000000000000000), (CUSTOMER6_KEY_32, 1000000000000000000000000), (CUSTOMER7_KEY_32, 1000000000000000000000000), + (BANK_KEY_32, 10000000000000000000000000000), ], } .assimilate_storage(&mut storage); diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index 9dd8319fe..eafe6eb90 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -3519,7 +3519,7 @@ fn send_rewarding_providers_batch_100_nodes_small_usage_works() { let num_nodes = 10; let num_users = 5; - let bank = AccountId::from([1; 32]); + let bank: AccountId = BANK_KEY_32; let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let nodes_keys = [ @@ -3825,7 +3825,7 @@ fn send_rewarding_providers_batch_100_nodes_large_usage_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let num_nodes = 10; let num_users = 5; - let bank = AccountId::from([1; 32]); + let bank: AccountId = BANK_KEY_32; let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); @@ -4144,7 +4144,7 @@ fn send_rewarding_providers_batch_100_nodes_small_large_usage_works() { let end_era: i64 = start_era + (30.44 * 24.0 * 3600.0) as i64; let num_nodes = 10; let num_users = 5; - let bank = AccountId::from([1; 32]); + let bank: AccountId = BANK_KEY_32; let cluster_id = ONE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let nodes_keys = [ @@ -4468,7 +4468,7 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { let max: u64 = 1024 * 1024; let num_nodes = 10; let num_users = 10; - let bank = AccountId::from([1; 32]); + let bank: AccountId = BANK_KEY_32; let cluster_id = CERE_CLUSTER_ID; let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let nodes_keys = [ From f82a11792b0afd91ef0f74e829c2843a224cd9d1 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 13:37:43 +0100 Subject: [PATCH 14/20] chore: unused account is removed from tests --- pallets/ddc-payouts/src/tests.rs | 34 ++++++++++++-------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index eafe6eb90..ef316b890 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -11,11 +11,9 @@ use super::{mock::*, *}; #[test] fn begin_billing_report_fails_for_unauthorised() { ExtBuilder.build_and_execute(|| { - let root_account = AccountId::from([1; 32]); 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(); @@ -23,7 +21,7 @@ fn begin_billing_report_fails_for_unauthorised() { assert_noop!( DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(AccountId::from([3; 32])), + RuntimeOrigin::signed(CUSTOMER3_KEY_32), cluster_id, era, start_era, @@ -34,7 +32,7 @@ fn begin_billing_report_fails_for_unauthorised() { assert_noop!( DdcPayouts::begin_billing_report( - RuntimeOrigin::signed(root_account), + RuntimeOrigin::signed(AccountId::from([0; 32])), cluster_id, era, start_era, @@ -78,14 +76,13 @@ fn begin_billing_report_works() { #[test] fn begin_charging_customers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let fake_ocw_account = AccountId::from([124; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; assert_noop!( DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(fake_ocw_account), + RuntimeOrigin::signed(AccountId::from([124; 32])), cluster_id, era, max_batch_index, @@ -155,7 +152,6 @@ fn begin_charging_customers_works() { #[test] fn send_charging_customers_batch_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let root_account = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; @@ -172,7 +168,6 @@ fn send_charging_customers_batch_fails_uninitialised() { let payers1 = vec![(node_key.clone(), bucket_id3, customer_usage)]; let payers2 = vec![(node_key.clone(), bucket_id4, CustomerUsage::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 = DateTime::::from_naive_utc_and_offset(start_date.and_time(time), Utc).timestamp(); @@ -180,7 +175,7 @@ fn send_charging_customers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_charging_customers_batch( - RuntimeOrigin::signed(root_account), + RuntimeOrigin::signed(AccountId::from([0; 32])), cluster_id, era, batch_index, @@ -2462,7 +2457,6 @@ fn send_charging_customers_batch_works_zero_fees() { #[test] fn end_charging_customers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let root_account = AccountId::from([100; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -2479,7 +2473,7 @@ fn end_charging_customers_fails_uninitialised() { assert_noop!( DdcPayouts::end_charging_customers( - RuntimeOrigin::signed(root_account), + RuntimeOrigin::signed(AccountId::from([0; 32])), cluster_id, era, ), @@ -2863,7 +2857,6 @@ fn end_charging_customers_works_zero_fees() { #[test] fn begin_rewarding_providers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let root_account = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); let era = 100; let max_batch_index = 2; @@ -2881,7 +2874,7 @@ fn begin_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_rewarding_providers( - RuntimeOrigin::signed(root_account), + RuntimeOrigin::signed(AccountId::from([0; 32])), cluster_id, era, max_batch_index, @@ -3062,7 +3055,6 @@ fn begin_rewarding_providers_works() { #[test] fn send_rewarding_providers_batch_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let root_account = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -3083,7 +3075,7 @@ fn send_rewarding_providers_batch_fails_uninitialised() { assert_noop!( DdcPayouts::send_rewarding_providers_batch( - RuntimeOrigin::signed(root_account), + RuntimeOrigin::signed(AccountId::from([0; 32])), cluster_id, era, batch_index, @@ -4727,7 +4719,6 @@ fn send_rewarding_providers_batch_100_nodes_random_usage_works() { #[test] fn end_rewarding_providers_fails_uninitialised() { ExtBuilder.build_and_execute(|| { - let root_account = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -4740,7 +4731,6 @@ fn end_rewarding_providers_fails_uninitialised() { 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 - 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(); @@ -4748,7 +4738,7 @@ fn end_rewarding_providers_fails_uninitialised() { assert_noop!( DdcPayouts::end_rewarding_providers( - RuntimeOrigin::signed(root_account), + RuntimeOrigin::signed(AccountId::from([0; 32])), cluster_id, era, ), @@ -4991,12 +4981,10 @@ fn end_rewarding_providers_works() { fn end_billing_report_fails_uninitialised() { ExtBuilder.build_and_execute(|| { 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; - let root_account = AccountId::from([1; 32]); let cluster_id = ClusterId::from([12; 20]); let node_key = NodePubKey::StoragePubKey(NODE1_PUB_KEY_32); let era = 100; @@ -5011,7 +4999,11 @@ fn end_billing_report_fails_uninitialised() { let total_node_usage = NodeUsage::default(); assert_noop!( - DdcPayouts::end_billing_report(RuntimeOrigin::signed(root_account), cluster_id, era,), + DdcPayouts::end_billing_report( + RuntimeOrigin::signed(AccountId::from([0; 32])), + cluster_id, + era, + ), Error::::Unauthorised ); From c3a18c2851c973cca73650b2c4e3c934676a1547 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 14:02:51 +0100 Subject: [PATCH 15/20] chore: using generic type for bucket visitor --- pallets/ddc-customers/src/lib.rs | 12 +++++++----- pallets/ddc-payouts/src/lib.rs | 20 ++++---------------- pallets/ddc-payouts/src/mock.rs | 7 +++---- primitives/src/lib.rs | 7 ------- primitives/src/traits/bucket.rs | 4 ++-- 5 files changed, 16 insertions(+), 34 deletions(-) diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index 639b22ec3..1c2e98b2c 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -19,7 +19,7 @@ use ddc_primitives::{ cluster::{ClusterCreator, ClusterProtocol, ClusterQuery}, customer::{CustomerCharger, CustomerDepositor, CustomerVisitor}, }, - BucketId, BucketVisitorError, ClusterId, CustomerUsage, + BucketId, ClusterId, CustomerUsage, }; use frame_support::{ parameter_types, @@ -251,6 +251,8 @@ pub mod pallet { TransferFailed, /// Bucket is already removed AlreadyRemoved, + /// Bucket belongs to another cluster + ClusterMismatch, } #[pallet::genesis_config] @@ -657,10 +659,10 @@ pub mod pallet { cluster_id: &ClusterId, bucket_id: BucketId, content_owner: &T::AccountId, - ) -> Result, BucketVisitorError> { - let bucket = Self::buckets(bucket_id).ok_or(BucketVisitorError::NoBucketWithId)?; - ensure!(bucket.owner_id == *content_owner, BucketVisitorError::NotBucketOwner); - ensure!(bucket.cluster_id == *cluster_id, BucketVisitorError::IncorrectClusterId); + ) -> 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) } diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index c4ea9369d..34604d373 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -37,9 +37,8 @@ use ddc_primitives::{ pallet::PalletVisitor as PalletVisitorType, payout::PayoutVisitor, }, - BatchIndex, BucketId, BucketVisitorError, ClusterId, CustomerUsage, DdcEra, MMRProof, - NodePubKey, NodeUsage, PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, - MILLICENTS, + BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, + PayoutError, PayoutState, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, MILLICENTS, }; use frame_election_provider_support::SortedListProvider; use frame_support::{ @@ -1085,7 +1084,7 @@ pub mod pallet { customer_id: &T::AccountId, start_era: i64, end_era: i64, - ) -> Result> { + ) -> Result { let mut total = CustomerCharge::default(); let pricing = T::ClusterProtocol::get_pricing_params(cluster_id) @@ -1105,8 +1104,7 @@ pub mod pallet { Perquintill::from_rational(duration_seconds as u64, seconds_in_month as u64); let mut total_stored_bytes: i64 = - T::BucketVisitor::get_total_customer_usage(cluster_id, bucket_id, customer_id) - .map_err(Into::>::into)? + 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 @@ -1133,16 +1131,6 @@ pub mod pallet { Ok(total) } - impl From for Error { - fn from(error: BucketVisitorError) -> Self { - match error { - BucketVisitorError::NoBucketWithId => Error::::NoBucketWithId, - BucketVisitorError::NotBucketOwner => Error::::NotBucketOwner, - BucketVisitorError::IncorrectClusterId => Error::::IncorrectClusterId, - } - } - } - #[pallet::genesis_config] pub struct GenesisConfig { pub feeder_account: Option, diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 3d51fadcb..80d3ae9e8 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -11,9 +11,8 @@ use ddc_primitives::{ pallet::PalletVisitor, ClusterQuery, ValidatorVisitor, }, - BucketVisitorError, ClusterBondingParams, ClusterFeesParams, ClusterParams, - ClusterPricingParams, ClusterProtocolParams, ClusterStatus, NodeParams, NodePubKey, NodeType, - DOLLARS, + ClusterBondingParams, ClusterFeesParams, ClusterParams, ClusterPricingParams, + ClusterProtocolParams, ClusterStatus, NodeParams, NodePubKey, NodeType, DOLLARS, }; use frame_election_provider_support::SortedListProvider; use frame_support::{ @@ -254,7 +253,7 @@ where _cluster_id: &ClusterId, _bucket_id: BucketId, _content_owner: &T::AccountId, - ) -> Result, BucketVisitorError> { + ) -> Result, DispatchError> { Ok(None) } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index afca88b44..1a43ca8d7 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -299,13 +299,6 @@ pub enum NodeRepositoryError { StorageNodeDoesNotExist, } -#[derive(Debug, PartialEq)] -pub enum BucketVisitorError { - NoBucketWithId, - NotBucketOwner, - IncorrectClusterId, -} - #[derive(Debug, PartialEq)] pub enum PayoutError { BillingReportDoesNotExist, diff --git a/primitives/src/traits/bucket.rs b/primitives/src/traits/bucket.rs index 2e7dadaf8..c9396fba1 100644 --- a/primitives/src/traits/bucket.rs +++ b/primitives/src/traits/bucket.rs @@ -1,6 +1,6 @@ use sp_runtime::{DispatchError, DispatchResult}; -use crate::{BucketId, BucketVisitorError, ClusterId, CustomerUsage}; +use crate::{BucketId, ClusterId, CustomerUsage}; pub trait BucketManager { fn inc_total_customer_usage( @@ -18,5 +18,5 @@ pub trait BucketVisitor { cluster_id: &ClusterId, bucket_id: BucketId, content_owner: &T::AccountId, - ) -> Result, BucketVisitorError>; + ) -> Result, DispatchError>; } From 8a36f63768141cd5106b8cdaf526172deddfd778 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 14:11:47 +0100 Subject: [PATCH 16/20] fix: clippy and formatiing --- pallets/ddc-payouts/src/tests.rs | 2 +- primitives/src/lib.rs | 2 -- primitives/src/traits/validator.rs | 2 ++ 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/ddc-payouts/src/tests.rs b/pallets/ddc-payouts/src/tests.rs index ef316b890..4e9a3b7f9 100644 --- a/pallets/ddc-payouts/src/tests.rs +++ b/pallets/ddc-payouts/src/tests.rs @@ -102,7 +102,7 @@ fn begin_charging_customers_fails_uninitialised() { assert_noop!( DdcPayouts::begin_charging_customers( - RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32.into()), + RuntimeOrigin::signed(VALIDATOR_OCW_KEY_32), cluster_id, era, max_batch_index, diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 1a43ca8d7..1a6770074 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -322,7 +322,6 @@ pub const DAC_VERIFICATION_KEY_TYPE: KeyTypeId = KeyTypeId(*b"cer!"); pub mod sr25519 { mod app_sr25519 { - use scale_info::prelude::string::String; use sp_application_crypto::{app_crypto, sr25519}; use crate::DAC_VERIFICATION_KEY_TYPE; @@ -337,7 +336,6 @@ pub mod sr25519 { } pub mod crypto { - use scale_info::prelude::string::String; use sp_core::sr25519::Signature as Sr25519Signature; use sp_runtime::{ app_crypto::{app_crypto, sr25519}, diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index 4d6f2edc2..033d4cd2d 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -1,4 +1,6 @@ use frame_system::Config; +#[cfg(feature = "runtime-benchmarks")] +use scale_info::prelude::vec::Vec; use crate::{ BatchIndex, BucketId, ClusterId, CustomerUsage, DdcEra, MMRProof, NodePubKey, NodeUsage, From 435730814807a17af0b13509b3ef357e2bf581fd Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 14:25:35 +0100 Subject: [PATCH 17/20] chore: runtime version updated --- runtime/cere-dev/src/lib.rs | 4 ++-- runtime/cere/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index a80286534..652980838 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: 61002, + spec_version: 61003, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 23, + transaction_version: 24, state_version: 0, }; diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 61b2de4af..4df3d21d6 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: 61002, + spec_version: 61003, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 23, + transaction_version: 24, state_version: 0, }; From 0c3d287269a681848bbc3d6d8796a9e800a16a6f Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 15:00:20 +0100 Subject: [PATCH 18/20] chore: calculating mmr size based on max batch index --- pallets/ddc-payouts/src/lib.rs | 2 ++ pallets/ddc-payouts/src/mock.rs | 2 ++ pallets/ddc-verification/src/lib.rs | 36 ++++++++++++++++++--------- pallets/ddc-verification/src/tests.rs | 22 ++++++++-------- primitives/src/lib.rs | 1 - primitives/src/traits/validator.rs | 2 ++ 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/pallets/ddc-payouts/src/lib.rs b/pallets/ddc-payouts/src/lib.rs index 34604d373..2d45ea29c 100644 --- a/pallets/ddc-payouts/src/lib.rs +++ b/pallets/ddc-payouts/src/lib.rs @@ -465,6 +465,7 @@ pub mod pallet { cluster_id, era, batch_index, + billing_report.charging_max_batch_index, &payers, &batch_proof ), @@ -782,6 +783,7 @@ pub mod pallet { cluster_id, era, batch_index, + billing_report.rewarding_max_batch_index, &payees, &batch_proof ), diff --git a/pallets/ddc-payouts/src/mock.rs b/pallets/ddc-payouts/src/mock.rs index 80d3ae9e8..ed1ad03c4 100644 --- a/pallets/ddc-payouts/src/mock.rs +++ b/pallets/ddc-payouts/src/mock.rs @@ -203,6 +203,7 @@ where _cluster_id: ClusterId, _era: DdcEra, _batch_index: BatchIndex, + _max_batch_index: BatchIndex, _payers: &[(NodePubKey, BucketId, CustomerUsage)], _batch_proof: &MMRProof, ) -> bool { @@ -212,6 +213,7 @@ where _cluster_id: ClusterId, _era: DdcEra, _batch_index: BatchIndex, + _max_batch_index: BatchIndex, _payees: &[(NodePubKey, NodeUsage)], _batch_proof: &MMRProof, ) -> bool { diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 2092218cc..771c783f2 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -30,7 +30,7 @@ use frame_system::{ }; pub use pallet::*; use polkadot_ckb_merkle_mountain_range::{ - helper::leaf_index_to_pos, + helper::{leaf_index_to_mmr_size, leaf_index_to_pos}, util::{MemMMR, MemStore}, MerkleProof, MMR, }; @@ -2268,7 +2268,7 @@ pub mod pallet { .proof_items() .to_vec(); - let batch_proof = MMRProof { mmr_size: mmr.mmr_size(), proof }; + let batch_proof = MMRProof { proof }; Ok(Some(( era_id, CustomerBatch { @@ -2512,9 +2512,7 @@ pub mod pallet { .proof_items() .to_vec(); - // todo! attend [i] through get(i).ok_or() - // todo! attend accountid conversion - let batch_proof = MMRProof { mmr_size: mmr.mmr_size(), proof }; + let batch_proof = MMRProof { proof }; Ok(Some(( era_id, ProviderBatch { @@ -2879,11 +2877,13 @@ pub mod pallet { root_hash: ActivityHash, batch_hash: ActivityHash, batch_index: BatchIndex, + max_batch_index: BatchIndex, batch_proof: &MMRProof, ) -> Result> { let batch_position = leaf_index_to_pos(batch_index.into()); + let mmr_size = leaf_index_to_mmr_size(max_batch_index.into()); let proof: MerkleProof = - MerkleProof::new(batch_proof.mmr_size, batch_proof.proof.clone()); + MerkleProof::new(mmr_size, batch_proof.proof.clone()); proof .verify(root_hash, vec![(batch_position, batch_hash)]) .map_err(|_| Error::::FailToVerifyMerkleProof) @@ -4170,11 +4170,11 @@ pub mod pallet { } } - // todo! use batch_index and payers as part of the validation fn is_customers_batch_valid( cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, + max_batch_index: BatchIndex, payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: &MMRProof, ) -> bool { @@ -4202,18 +4202,24 @@ pub mod pallet { Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) .expect("batch_hash to be created"); - Self::proof_merkle_leaf(root_hash, batch_hash, batch_index, batch_proof) - .unwrap_or(false) + Self::proof_merkle_leaf( + root_hash, + batch_hash, + batch_index, + max_batch_index, + batch_proof, + ) + .unwrap_or(false) }, None => false, } } - // todo! use batch_index and payees as part of the validation fn is_providers_batch_valid( cluster_id: ClusterId, era_id: DdcEra, batch_index: BatchIndex, + max_batch_index: BatchIndex, payees: &[(NodePubKey, NodeUsage)], batch_proof: &MMRProof, ) -> bool { @@ -4239,8 +4245,14 @@ pub mod pallet { Self::create_merkle_root(&cluster_id, era_id, activity_hashes.as_slice()) .expect("batch_hash to be created"); - Self::proof_merkle_leaf(root_hash, batch_hash, batch_index, batch_proof) - .unwrap_or(false) + Self::proof_merkle_leaf( + root_hash, + batch_hash, + batch_index, + max_batch_index, + batch_proof, + ) + .unwrap_or(false) }, None => false, } diff --git a/pallets/ddc-verification/src/tests.rs b/pallets/ddc-verification/src/tests.rs index 14194ecfc..b8db476ea 100644 --- a/pallets/ddc-verification/src/tests.rs +++ b/pallets/ddc-verification/src/tests.rs @@ -2521,30 +2521,28 @@ fn proof_merkle_leaf_works() { assert_eq!(leaf_position.len(), 1); assert_eq!(position.len(), 1); - let leave_index = 2; + let max_leaf_index = 4; + + let leaf_index = 2; let leaf_hash = c; assert!(DdcVerification::proof_merkle_leaf( root_hash, leaf_hash, - leave_index, - &MMRProof { - mmr_size: mmr.mmr_size(), - proof: mmr.gen_proof(position.clone()).unwrap().proof_items().to_vec(), - } + leaf_index, + max_leaf_index, + &MMRProof { proof: mmr.gen_proof(position.clone()).unwrap().proof_items().to_vec() } ) .unwrap()); - let leave_index = 5; + let leaf_index = 5; let leaf_hash = f; assert_noop!( DdcVerification::proof_merkle_leaf( root_hash, leaf_hash, - leave_index, - &MMRProof { - mmr_size: mmr.mmr_size(), - proof: mmr.gen_proof(position).unwrap().proof_items().to_vec(), - } + leaf_index, + max_leaf_index, + &MMRProof { proof: mmr.gen_proof(position).unwrap().proof_items().to_vec() } ), Error::::FailToVerifyMerkleProof ); diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 1a6770074..491650bb9 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -289,7 +289,6 @@ pub struct NodeUsage { #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Default)] pub struct MMRProof { - pub mmr_size: u64, pub proof: Vec, } diff --git a/primitives/src/traits/validator.rs b/primitives/src/traits/validator.rs index 033d4cd2d..a5e817228 100644 --- a/primitives/src/traits/validator.rs +++ b/primitives/src/traits/validator.rs @@ -14,6 +14,7 @@ pub trait ValidatorVisitor { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, + max_batch_index: BatchIndex, payers: &[(NodePubKey, BucketId, CustomerUsage)], batch_proof: &MMRProof, ) -> bool; @@ -21,6 +22,7 @@ pub trait ValidatorVisitor { cluster_id: ClusterId, era: DdcEra, batch_index: BatchIndex, + max_batch_index: BatchIndex, payees: &[(NodePubKey, NodeUsage)], batch_proof: &MMRProof, ) -> bool; From e6993a6e71b14bce7b9cc77fd1b82a8c98357529 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 16:24:52 +0100 Subject: [PATCH 19/20] fix: cutting off '0x' prefix while decoding --- pallets/ddc-verification/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 771c783f2..d439a1f14 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -3568,7 +3568,13 @@ pub mod pallet { } pub fn node_key_from_hex(hex_str: String) -> Result { - let bytes_vec = hex::decode(hex_str)?; + let bytes_vec = if hex_str.len() == 66 { + // cut `0x` prefix + hex::decode(&hex_str[2..])? + } else { + hex::decode(hex_str)? + }; + let bytes_arr: [u8; 32] = bytes_vec.try_into().map_err(|_| hex::FromHexError::InvalidStringLength)?; let pub_key = AccountId32::from(bytes_arr); From da97857899006d9907f88cf507feabf08aab30df Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 7 Nov 2024 16:34:27 +0100 Subject: [PATCH 20/20] fix: 'transaction_version' is fixed --- runtime/cere-dev/src/lib.rs | 2 +- runtime/cere/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 652980838..196d81fab 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -156,7 +156,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 61003, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 24, + transaction_version: 22, state_version: 0, }; diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 4df3d21d6..72f4dfbff 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -150,7 +150,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 61003, impl_version: 0, apis: RUNTIME_API_VERSIONS, - transaction_version: 24, + transaction_version: 22, state_version: 0, };