diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index b4ae8710e..2d6a24877 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -137,7 +137,7 @@ pub mod pallet { /// The current storage version. const STORAGE_VERSION: frame_support::traits::StorageVersion = - frame_support::traits::StorageVersion::new(1); + frame_support::traits::StorageVersion::new(2); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index 11ac2f497..22ee43323 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -260,6 +260,9 @@ pub mod pallet { FailedToFetchCurrentValidator { validator: T::AccountId, }, + FailedToFetchNodeProvider { + validator: T::AccountId, + }, } /// Consensus Errors @@ -358,6 +361,7 @@ pub mod pallet { era_id: DdcEra, }, FailedToFetchCurrentValidator, + FailedToFetchNodeProvider, } #[pallet::error] @@ -489,8 +493,6 @@ pub mod pallet { pub(crate) struct NodeActivity { /// Node id. pub(crate) node_id: String, - /// Provider id. - pub(crate) provider_id: String, /// Total amount of stored bytes. pub(crate) stored_bytes: u64, /// Total amount of transferred bytes. @@ -1684,9 +1686,10 @@ pub mod pallet { payees: nodes_activity_batched[i] .iter() .map(|activity| { - let provider_id = - T::AccountId::decode(&mut &activity.provider_id.as_bytes()[..]) - .unwrap(); + let node_id = activity.clone().node_id; + + let provider_id = Self::fetch_provider_id(node_id).unwrap(); // todo! remove unwrap + let node_usage = NodeUsage { transferred_bytes: activity.transferred_bytes, stored_bytes: activity.stored_bytes, @@ -1830,6 +1833,36 @@ pub mod pallet { } } + 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 @@ -2300,6 +2333,11 @@ 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: @@ -2324,6 +2362,10 @@ pub mod pallet { node_pub_key: node_pub_key.clone(), } })?; + for node_activity in usage.clone() { + let provider_id = Self::get_node_provider_id(node_pub_key).unwrap(); + Self::store_provider_id(node_activity.node_id, provider_id); + } node_usages.push((node_pub_key.clone(), usage)); } @@ -2677,6 +2719,11 @@ pub mod pallet { validator: caller.clone(), }); }, + OCWError::FailedToFetchNodeProvider => { + Self::deposit_event(Event::FailedToFetchNodeProvider { + validator: caller.clone(), + }); + }, } } diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index efcd8c16b..f4faaaafb 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -17,7 +17,7 @@ use frame_support::{ }; use frame_system::mocking::MockBlock; use pallet_staking::BalanceOf; -use sp_core::H256; +use sp_core::{ByteArray, H256}; use sp_runtime::{ curve::PiecewiseLinear, testing::{TestXt, UintAuthorityId}, @@ -558,7 +558,10 @@ impl NodeVisitor for MockNodeVisitor { } fn get_node_provider_id(_node_pub_key: &NodePubKey) -> Result { - unimplemented!() + let temp: AccountId = AccountId::from([0xa; 32]); + let account_1 = T::AccountId::decode(&mut &temp.as_slice()[..]).unwrap(); + + Ok(account_1) } } diff --git a/pallets/ddc-verification/src/tests.rs b/pallets/ddc-verification/src/tests.rs index 55ed57105..d4a3c2e3e 100644 --- a/pallets/ddc-verification/src/tests.rs +++ b/pallets/ddc-verification/src/tests.rs @@ -43,7 +43,6 @@ fn get_validators() -> Vec { fn get_node_activities() -> Vec { let node1 = NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, number_of_puts: 10, @@ -51,7 +50,6 @@ fn get_node_activities() -> Vec { }; let node2 = NodeActivity { node_id: "1".to_string(), - provider_id: "1".to_string(), stored_bytes: 101, transferred_bytes: 51, number_of_puts: 11, @@ -59,7 +57,6 @@ fn get_node_activities() -> Vec { }; let node3 = NodeActivity { node_id: "2".to_string(), - provider_id: "2".to_string(), stored_bytes: 102, transferred_bytes: 52, number_of_puts: 12, @@ -67,7 +64,6 @@ fn get_node_activities() -> Vec { }; let node4 = NodeActivity { node_id: "3".to_string(), - provider_id: "3".to_string(), stored_bytes: 103, transferred_bytes: 53, number_of_puts: 13, @@ -75,7 +71,6 @@ fn get_node_activities() -> Vec { }; let node5 = NodeActivity { node_id: "4".to_string(), - provider_id: "4".to_string(), stored_bytes: 104, transferred_bytes: 54, number_of_puts: 14, @@ -103,7 +98,6 @@ fn fetch_node_usage_works() { // Create a sample NodeActivity instance let node_activity1 = NodeActivity { - provider_id: "1".to_string(), node_id: "1".to_string(), stored_bytes: 100, transferred_bytes: 50, @@ -111,7 +105,6 @@ fn fetch_node_usage_works() { number_of_gets: 20, }; let node_activity2 = NodeActivity { - provider_id: "2".to_string(), node_id: "2".to_string(), stored_bytes: 110, transferred_bytes: 510, @@ -538,7 +531,6 @@ fn test_get_consensus_nodes_activity_success() { ( node_pubkey_0, vec![NodeActivity { - provider_id: "0".to_string(), node_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, @@ -549,7 +541,6 @@ fn test_get_consensus_nodes_activity_success() { ( node_pubkey_1, vec![NodeActivity { - provider_id: "0".to_string(), node_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, @@ -560,7 +551,6 @@ fn test_get_consensus_nodes_activity_success() { ( node_pubkey_2, vec![NodeActivity { - provider_id: "0".to_string(), node_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, @@ -679,7 +669,6 @@ fn test_get_consensus_nodes_activity_not_enough_nodes() { ( node_pubkey_0, vec![NodeActivity { - provider_id: "0".to_string(), node_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, @@ -690,7 +679,6 @@ fn test_get_consensus_nodes_activity_not_enough_nodes() { ( node_pubkey_1, vec![NodeActivity { - provider_id: "0".to_string(), node_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, @@ -1008,7 +996,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus() { node_pubkey_0, vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, number_of_puts: 10, @@ -1019,7 +1006,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus() { node_pubkey_1, vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 200, transferred_bytes: 100, number_of_puts: 20, @@ -1030,7 +1016,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus() { node_pubkey_2, vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 300, transferred_bytes: 150, number_of_puts: 30, @@ -1184,7 +1169,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus2() { node_pubkey_0.clone(), vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, number_of_puts: 10, @@ -1195,7 +1179,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus2() { node_pubkey_1.clone(), vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 200, transferred_bytes: 100, number_of_puts: 20, @@ -1206,7 +1189,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus2() { node_pubkey_2.clone(), vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 300, transferred_bytes: 150, number_of_puts: 30, @@ -1217,7 +1199,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus2() { node_pubkey_0, vec![NodeActivity { node_id: "1".to_string(), - provider_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, number_of_puts: 10, @@ -1228,7 +1209,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus2() { node_pubkey_1, vec![NodeActivity { node_id: "1".to_string(), - provider_id: "0".to_string(), stored_bytes: 200, transferred_bytes: 100, number_of_puts: 20, @@ -1239,7 +1219,6 @@ fn test_get_consensus_nodes_activity_not_in_consensus2() { node_pubkey_2, vec![NodeActivity { node_id: "1".to_string(), - provider_id: "0".to_string(), stored_bytes: 300, transferred_bytes: 150, number_of_puts: 30, @@ -1292,7 +1271,6 @@ fn test_get_consensus_nodes_activity_diff_errors() { node_pubkey_0.clone(), vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, number_of_puts: 10, @@ -1303,7 +1281,6 @@ fn test_get_consensus_nodes_activity_diff_errors() { node_pubkey_1.clone(), vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 200, transferred_bytes: 100, number_of_puts: 20, @@ -1314,7 +1291,6 @@ fn test_get_consensus_nodes_activity_diff_errors() { node_pubkey_2.clone(), vec![NodeActivity { node_id: "0".to_string(), - provider_id: "0".to_string(), stored_bytes: 300, transferred_bytes: 150, number_of_puts: 30, @@ -1325,7 +1301,6 @@ fn test_get_consensus_nodes_activity_diff_errors() { node_pubkey_0, vec![NodeActivity { node_id: "1".to_string(), - provider_id: "0".to_string(), stored_bytes: 100, transferred_bytes: 50, number_of_puts: 10, @@ -1336,7 +1311,6 @@ fn test_get_consensus_nodes_activity_diff_errors() { node_pubkey_1, vec![NodeActivity { node_id: "1".to_string(), - provider_id: "0".to_string(), stored_bytes: 200, transferred_bytes: 100, number_of_puts: 20, diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 9ccab23f8..11f672d2e 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1401,13 +1401,7 @@ pub type SignedPayload = generic::SignedPayload; pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Runtime migrations -type Migrations = ( - pallet_ddc_clusters::migrations::v2::MigrateToV2, - pallet_ddc_staking::migrations::v1::MigrateToV1, - pallet_ddc_customers::migration::MigrateToV2, - pallet_ddc_customers::migration::MigrateToV1, - migrations::Unreleased, -); +type Migrations = (pallet_ddc_customers::migration::MigrateToV2, migrations::Unreleased); /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 76933d0fe..4b69773cd 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -1413,7 +1413,6 @@ type Migrations = ( pallet_ddc_clusters::migrations::v2::MigrateToV2, pallet_ddc_staking::migrations::v1::MigrateToV1, pallet_ddc_customers::migration::MigrateToV2, - pallet_ddc_customers::migration::MigrateToV1, migrations::Unreleased, );