From dcaa726998f4e704f1e38502a6589a119ea8802b Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Mon, 8 Aug 2022 17:00:11 +0300 Subject: [PATCH 01/15] fix: IpfsNode struct fields and the get_storage function returns. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Martín Noblía --- bin/node/pallets/pallet-ipfs/rpc/src/lib.rs | 4 +- bin/node/pallets/pallet-ipfs/src/functions.rs | 21 +++++---- bin/node/pallets/pallet-ipfs/src/lib.rs | 45 ++++++++++++------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/rpc/src/lib.rs b/bin/node/pallets/pallet-ipfs/rpc/src/lib.rs index 8b8dd0581..e5904d8eb 100644 --- a/bin/node/pallets/pallet-ipfs/rpc/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/rpc/src/lib.rs @@ -14,8 +14,8 @@ use std::sync::Arc; #[derive(Default, Debug, Serialize, Deserialize)] pub struct GetStorageResponse { pub available_storage: u64, + pub maximum_storage: u64, pub files: usize, - pub total_files: usize, } #[rpc] @@ -68,8 +68,8 @@ where // TODO: this should change the IPFS partition path. Ok(mount) => { resp.available_storage = mount.avail.as_u64(); + resp.maximum_storage = mount.total.as_u64(); resp.files = mount.files; - resp.total_files = mount.files_total; }, Err(e) => { log::error!("{:?}", e); diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index 3686f5a9e..97700d4c3 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -12,8 +12,8 @@ use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Serialize, Deserialize)] pub struct GetStorageResponseRPC { pub available_storage: u64, + pub maximum_storage: u64, pub files: usize, - pub total_files: usize, } #[derive(Serialize, Deserialize, Debug)] @@ -443,7 +443,7 @@ impl Pallet { if !IPFSNodes::::contains_key(public_key.clone()) { if let Some(ipfs_node) = &IPFSNodes::::iter().nth(0) { - if let Some(ipfs_maddr) = ipfs_node.1.multiaddress.clone().pop() { + if let Some(ipfs_maddr) = ipfs_node.1.addr.clone().pop() { if let IpfsResponse::Success = Self::ipfs_request(IpfsRequest::Connect(ipfs_maddr.clone()), deadline)? { @@ -454,7 +454,7 @@ impl Pallet { &ipfs_node.0 ); - if let Some(next_ipfs_maddr) = ipfs_node.1.multiaddress.clone().pop() { + if let Some(next_ipfs_maddr) = ipfs_node.1.addr.clone().pop() { if let IpfsResponse::Success = Self::ipfs_request( IpfsRequest::Connect(next_ipfs_maddr.clone()), deadline, @@ -480,20 +480,25 @@ impl Pallet { } let avail_storage = Self::get_validator_storage().unwrap().result.available_storage; + let max_storage = Self::get_validator_storage().unwrap().result.maximum_storage; let files = Self::get_validator_storage().unwrap().result.files; - let files_total = Self::get_validator_storage().unwrap().result.total_files; let results = signer.send_signed_transaction(|_account| Call::submit_ipfs_identity { public_key: public_key.clone(), multiaddress: addrs.clone(), - storage_size: avail_storage.clone(), - files: files as u64, - files_total: files_total as u64, + available_storage: avail_storage.clone() as i32, + max_storage: max_storage as i32, + files: files as i32, }); for (_, res) in &results { match res { - Ok(()) => log::info!("Submitted ipfs identity results"), + Ok(()) => { + log::info!("Submitted ipfs identity results"); + // log::info!("\n\navailable storage: {:?}", avail_storage); + // log::info!("\nmax storage: {:?}", max_storage); + // log::info!("\nfiles: {:?}", files); + }, Err(e) => log::error!("Failed to submit tx: {:?}", e), } } diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index b8b462ef6..752008650 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -22,7 +22,7 @@ use sp_core::{ Bytes, }; use sp_io::offchain::timestamp; -use sp_std::{convert::TryInto, vec::Vec}; +use sp_std::{convert::TryInto, str::from_utf8, vec::Vec}; pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"chfs"); @@ -81,6 +81,7 @@ pub mod pallet { pallet_prelude::*, }; use scale_info::TypeInfo; + use scale_info::prelude::string::String; use sp_core::offchain::OpaqueMultiaddr; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; @@ -104,11 +105,21 @@ pub mod pallet { #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] #[scale_info(skip_type_params(T))] pub struct IpfsNode { - pub multiaddress: Vec, - pub public_key: Vec, - pub avail_storage: u64, - pub files: u64, - pub files_total: u64, + pub peer_id: Vec, + pub addr: Vec, + pub avail: i32, + pub max: i32, + pub files: i32, + } + + // #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] + // #[scale_info(skip_type_params(T))] + pub struct IpfsNodeDB { + pub peer_id: String, + pub addr: String, + pub avail: i32, + pub max: i32, + pub files: i32, } #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] @@ -465,22 +476,26 @@ pub mod pallet { origin: OriginFor, public_key: Vec, multiaddress: Vec, - storage_size: u64, - files: u64, - files_total: u64, + available_storage: i32, + max_storage: i32, + files: i32, ) -> DispatchResult { let signer = ensure_signed(origin)?; + // let addr = from_utf8(&multiaddress.encode()).unwrap(); + // let peer_id = from_utf8(&public_key.encode()).unwrap(); + let ipfs_node: IpfsNode = IpfsNode { - multiaddress, - public_key: public_key.clone(), - avail_storage: storage_size, + peer_id: public_key.clone(), + addr: multiaddress, + avail: available_storage, + max: max_storage, files, - files_total, }; - >::insert(public_key.clone(), ipfs_node.clone()); - Self::deposit_event(Event::PublishedIdentity(signer.clone())); + >::insert(public_key, ipfs_node); + + Self::deposit_event(Event::PublishedIdentity(signer)); Ok(()) } From f99d18dfac9c6bef1f69c213c01a037b02bd9362 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Mon, 8 Aug 2022 17:10:27 +0300 Subject: [PATCH 02/15] add: UpdateDuration type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UpdateDuration is equal the EraDuration. Co-Authored-By: Martín Noblía --- bin/node/pallets/pallet-ipfs/src/lib.rs | 3 +++ bin/node/pallets/pallet-ipfs/src/mock.rs | 2 ++ bin/node/runtime/src/lib.rs | 2 ++ 3 files changed, 7 insertions(+) diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 752008650..385820d1b 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -165,6 +165,9 @@ pub mod pallet { #[pallet::constant] type DefaultAssetLifetime: Get; + #[pallet::constant] + type UpdateDuration: Get; + type Call: From>; type AuthorityId: AppCrypto; diff --git a/bin/node/pallets/pallet-ipfs/src/mock.rs b/bin/node/pallets/pallet-ipfs/src/mock.rs index f459451b8..801e671ac 100755 --- a/bin/node/pallets/pallet-ipfs/src/mock.rs +++ b/bin/node/pallets/pallet-ipfs/src/mock.rs @@ -80,6 +80,7 @@ parameter_types! { pub const MetadataDepositBase: u64 = 1; pub const MetadataDepositPerByte: u64 = 1; pub const MaxIpfsOwned: u32 = 5; + pub const EpochDuration: u64 = 6; pub const DefaultAssetLifetime: u32 = 60; } @@ -135,6 +136,7 @@ impl Config for Test { type Event = Event; type AuthorityId = pallet_cherry::crypto::AuthorityId; type MaxIpfsOwned = MaxIpfsOwned; + type UpdateDuration = EpochDuration; type DefaultAssetLifetime = DefaultAssetLifetime; type WeightInfo = pallet_cherry::weights::SubstrateWeight; } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 329a49ca9..347b6ad7d 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -916,6 +916,7 @@ impl pallet_transaction_storage::Config for Runtime { } parameter_types! { + pub const UpdateDuration: u64 = EPOCH_DURATION_IN_SLOTS as u64 * 6; pub const MaxIpfsOwned: u32 = 5; pub const DefaultAssetLifetime: BlockNumber = DEFAULT_ASSET_LIFETIME; } @@ -925,6 +926,7 @@ impl pallet_ipfs::Config for Runtime { type Currency = Balances; type AuthorityId = pallet_ipfs::crypto::AuthorityId; type Call = Call; + type UpdateDuration = UpdateDuration; type MaxIpfsOwned = MaxIpfsOwned; type DefaultAssetLifetime = DefaultAssetLifetime; type WeightInfo = pallet_ipfs::weights::SubstrateWeight; From 7a0d8fd7431702d32891ded0465dd83b6afb8f17 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Mon, 8 Aug 2022 17:24:12 +0300 Subject: [PATCH 03/15] wip: publish ipfs node stats function and event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Martín Noblía --- bin/node/pallets/pallet-ipfs/src/functions.rs | 9 +++++++++ bin/node/pallets/pallet-ipfs/src/lib.rs | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index 97700d4c3..a4c24f57e 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -591,4 +591,13 @@ impl Pallet { Ok(resp) } + + pub fn publish_ipfs_node_db_info() -> Result<(), Error> { + let _deadline = Some(timestamp().add(Duration::from_millis(5_000))); + + // get the info from IpfsNode storage + // convert types to match IpfsNodeBD struct + + Ok(()) + } } diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 385820d1b..bdae87919 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -166,7 +166,7 @@ pub mod pallet { type DefaultAssetLifetime: Get; #[pallet::constant] - type UpdateDuration: Get; + type UpdateDuration: Get; type Call: From>; @@ -232,6 +232,7 @@ pub mod pallet { DeleteIpfsAsset(T::AccountId, Vec), UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), + UpdateDB(T::AccountId), } // Storage items. @@ -286,6 +287,12 @@ pub mod pallet { } } + if block_no % T::UpdateDuration::get() == 0u32.into() { + if let Err(e) = Self::publish_ipfs_node_db_info() { + log::error!("IPFS: Encountered an error while publishing metadata {:?}", e); + } + } + if let Err(e) = Self::ipfs_garbage_collector(block_no) { log::error!("IPFS::GARBAGE_COLLECTOR::ERROR: {:?}", e); } From d1be4112068ed6d50de6b2c048d5eed96c6bb78d Mon Sep 17 00:00:00 2001 From: Charalampos Mitrodimas Date: Mon, 8 Aug 2022 18:54:19 +0300 Subject: [PATCH 04/15] cargo +nightly fmt --- bin/node/pallets/pallet-ipfs/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index bdae87919..390294af2 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -80,8 +80,7 @@ pub mod pallet { offchain::{AppCrypto, CreateSignedTransaction}, pallet_prelude::*, }; - use scale_info::TypeInfo; - use scale_info::prelude::string::String; + use scale_info::{prelude::string::String, TypeInfo}; use sp_core::offchain::OpaqueMultiaddr; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; From b5bae13b798e44ac6f6212ce32155c0bd30c0a05 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Mon, 8 Aug 2022 19:21:52 +0300 Subject: [PATCH 05/15] fix: UpdateDuration type to u64 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Martín Noblía --- bin/node/pallets/pallet-ipfs/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 390294af2..ecee273a3 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -165,7 +165,7 @@ pub mod pallet { type DefaultAssetLifetime: Get; #[pallet::constant] - type UpdateDuration: Get; + type UpdateDuration: Get; type Call: From>; @@ -286,7 +286,7 @@ pub mod pallet { } } - if block_no % T::UpdateDuration::get() == 0u32.into() { + if block_no % T::UpdateDuration::get().try_into().ok().unwrap() == 0u32.into() { if let Err(e) = Self::publish_ipfs_node_db_info() { log::error!("IPFS: Encountered an error while publishing metadata {:?}", e); } From a63875ece2ff24c572d27f193743d03177fdb9e6 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Mon, 8 Aug 2022 19:32:21 +0300 Subject: [PATCH 06/15] fix: naming of event and function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Martín Noblía --- bin/node/pallets/pallet-ipfs/src/functions.rs | 2 +- bin/node/pallets/pallet-ipfs/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index a4c24f57e..23abc8915 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -592,7 +592,7 @@ impl Pallet { Ok(resp) } - pub fn publish_ipfs_node_db_info() -> Result<(), Error> { + pub fn emit_ipfs_stats() -> Result<(), Error> { let _deadline = Some(timestamp().add(Duration::from_millis(5_000))); // get the info from IpfsNode storage diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index ecee273a3..2e6a6fc1a 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -231,7 +231,7 @@ pub mod pallet { DeleteIpfsAsset(T::AccountId, Vec), UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), - UpdateDB(T::AccountId), + ExportIpfsStats(T::AccountId), } // Storage items. @@ -287,7 +287,7 @@ pub mod pallet { } if block_no % T::UpdateDuration::get().try_into().ok().unwrap() == 0u32.into() { - if let Err(e) = Self::publish_ipfs_node_db_info() { + if let Err(e) = Self::emit_ipfs_stats() { log::error!("IPFS: Encountered an error while publishing metadata {:?}", e); } } From ae0dd6b8292180d980b733bde2c9e7c866eba90e Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Tue, 9 Aug 2022 10:54:45 +0300 Subject: [PATCH 07/15] fix: renamed IpfsNode struct fields --- bin/node/pallets/pallet-ipfs/src/functions.rs | 8 +++--- bin/node/pallets/pallet-ipfs/src/lib.rs | 26 ++++++------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index 23abc8915..ff334e368 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -443,7 +443,7 @@ impl Pallet { if !IPFSNodes::::contains_key(public_key.clone()) { if let Some(ipfs_node) = &IPFSNodes::::iter().nth(0) { - if let Some(ipfs_maddr) = ipfs_node.1.addr.clone().pop() { + if let Some(ipfs_maddr) = ipfs_node.1.multiaddress.clone().pop() { if let IpfsResponse::Success = Self::ipfs_request(IpfsRequest::Connect(ipfs_maddr.clone()), deadline)? { @@ -454,7 +454,7 @@ impl Pallet { &ipfs_node.0 ); - if let Some(next_ipfs_maddr) = ipfs_node.1.addr.clone().pop() { + if let Some(next_ipfs_maddr) = ipfs_node.1.multiaddress.clone().pop() { if let IpfsResponse::Success = Self::ipfs_request( IpfsRequest::Connect(next_ipfs_maddr.clone()), deadline, @@ -593,10 +593,10 @@ impl Pallet { } pub fn emit_ipfs_stats() -> Result<(), Error> { - let _deadline = Some(timestamp().add(Duration::from_millis(5_000))); + let _deadline = Some(timestamp().add(Duration::from_millis(5_0000))); // get the info from IpfsNode storage - // convert types to match IpfsNodeBD struct + // convert types Ok(()) } diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 2e6a6fc1a..99c60b148 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -104,20 +104,10 @@ pub mod pallet { #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] #[scale_info(skip_type_params(T))] pub struct IpfsNode { - pub peer_id: Vec, - pub addr: Vec, - pub avail: i32, - pub max: i32, - pub files: i32, - } - - // #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] - // #[scale_info(skip_type_params(T))] - pub struct IpfsNodeDB { - pub peer_id: String, - pub addr: String, - pub avail: i32, - pub max: i32, + pub public_key: Vec, + pub multiaddress: Vec, + pub avail_storage: i32, + pub max_storage: i32, pub files: i32, } @@ -495,10 +485,10 @@ pub mod pallet { // let peer_id = from_utf8(&public_key.encode()).unwrap(); let ipfs_node: IpfsNode = IpfsNode { - peer_id: public_key.clone(), - addr: multiaddress, - avail: available_storage, - max: max_storage, + public_key: public_key.clone(), + multiaddress, + avail_storage: available_storage, + max_storage, files, }; From 2173b074377b9ae7663a7cb0353adb65ad951700 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Tue, 9 Aug 2022 12:43:20 +0300 Subject: [PATCH 08/15] add: emit_ipfs_stats function --- bin/node/pallets/pallet-ipfs/src/functions.rs | 28 +++++++++++++++---- bin/node/pallets/pallet-ipfs/src/lib.rs | 23 ++++++++------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index ff334e368..72dca31da 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -3,10 +3,8 @@ use super::*; use frame_system::pallet_prelude::BlockNumberFor; use sp_runtime::{ offchain::{http, ipfs, IpfsRequest, IpfsResponse}, - SaturatedConversion, }; use sp_std::str; - use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Serialize, Deserialize)] @@ -486,9 +484,9 @@ impl Pallet { let results = signer.send_signed_transaction(|_account| Call::submit_ipfs_identity { public_key: public_key.clone(), multiaddress: addrs.clone(), - available_storage: avail_storage.clone() as i32, - max_storage: max_storage as i32, - files: files as i32, + available_storage: avail_storage.clone(), + max_storage, + files: files as u64, }); for (_, res) in &results { @@ -593,10 +591,28 @@ impl Pallet { } pub fn emit_ipfs_stats() -> Result<(), Error> { - let _deadline = Some(timestamp().add(Duration::from_millis(5_0000))); + let deadline = Some(timestamp().add(Duration::from_millis(5_0000))); + + let (public_key, _addrs) = if let IpfsResponse::Identity(public_key, addrs) = + Self::ipfs_request(IpfsRequest::Identity, deadline)? + { + (public_key, addrs) + } else { + unreachable!("only `Identity` is a valid response type."); + }; // get the info from IpfsNode storage + let node = Self::ipfs_nodes(&public_key).ok_or(>::IpfsNodeNotExist)?; + // convert types + let item_addr = from_utf8(&node.multiaddress.encode()).unwrap().as_bytes().to_vec(); + let item_peer_id = from_utf8(&node.public_key.encode()).unwrap().as_bytes().to_vec(); + let avail = node.avail_storage as i32; + let max = node.max_storage as i32; + let files = node.files as i32; + + // emit event + Self::deposit_event(Event::ExportIpfsStats(item_addr, item_peer_id, avail, max, files)); Ok(()) } diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 99c60b148..6f6be28ce 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -80,7 +80,7 @@ pub mod pallet { offchain::{AppCrypto, CreateSignedTransaction}, pallet_prelude::*, }; - use scale_info::{prelude::string::String, TypeInfo}; + use scale_info::TypeInfo; use sp_core::offchain::OpaqueMultiaddr; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; @@ -106,9 +106,9 @@ pub mod pallet { pub struct IpfsNode { pub public_key: Vec, pub multiaddress: Vec, - pub avail_storage: i32, - pub max_storage: i32, - pub files: i32, + pub avail_storage: u64, + pub max_storage: u64, + pub files: u64, } #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] @@ -199,6 +199,7 @@ pub mod pallet { RequestFailed, FeeOutOfBounds, HttpFetchingError, + IpfsNodeNotExist, } #[pallet::event] @@ -221,7 +222,7 @@ pub mod pallet { DeleteIpfsAsset(T::AccountId, Vec), UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), - ExportIpfsStats(T::AccountId), + ExportIpfsStats(Vec, Vec, i32, i32, i32), } // Storage items. @@ -276,7 +277,8 @@ pub mod pallet { } } - if block_no % T::UpdateDuration::get().try_into().ok().unwrap() == 0u32.into() { + let x: T::BlockNumber = T::UpdateDuration::get().try_into().ok().unwrap(); + if block_no % x == 0u32.into() { if let Err(e) = Self::emit_ipfs_stats() { log::error!("IPFS: Encountered an error while publishing metadata {:?}", e); } @@ -475,15 +477,12 @@ pub mod pallet { origin: OriginFor, public_key: Vec, multiaddress: Vec, - available_storage: i32, - max_storage: i32, - files: i32, + available_storage: u64, + max_storage: u64, + files: u64, ) -> DispatchResult { let signer = ensure_signed(origin)?; - // let addr = from_utf8(&multiaddress.encode()).unwrap(); - // let peer_id = from_utf8(&public_key.encode()).unwrap(); - let ipfs_node: IpfsNode = IpfsNode { public_key: public_key.clone(), multiaddress, From 02bfb1d57b7940609120e209c689bd97a2cd392c Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Tue, 9 Aug 2022 13:40:26 +0300 Subject: [PATCH 09/15] add: submit_ipfs_emit_stats_result function fix: type convertion error and ExportIpfsStats event arguments --- bin/node/pallets/pallet-ipfs/src/functions.rs | 33 +++++++++++++++---- bin/node/pallets/pallet-ipfs/src/lib.rs | 33 ++++++++++++++++--- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index 72dca31da..bbd563d74 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -1,11 +1,9 @@ use super::*; use frame_system::pallet_prelude::BlockNumberFor; -use sp_runtime::{ - offchain::{http, ipfs, IpfsRequest, IpfsResponse}, -}; -use sp_std::str; use serde::{Deserialize, Serialize}; +use sp_runtime::offchain::{http, ipfs, IpfsRequest, IpfsResponse}; +use sp_std::str; #[derive(Default, Debug, Serialize, Deserialize)] pub struct GetStorageResponseRPC { @@ -603,16 +601,37 @@ impl Pallet { // get the info from IpfsNode storage let node = Self::ipfs_nodes(&public_key).ok_or(>::IpfsNodeNotExist)?; - + // convert types let item_addr = from_utf8(&node.multiaddress.encode()).unwrap().as_bytes().to_vec(); - let item_peer_id = from_utf8(&node.public_key.encode()).unwrap().as_bytes().to_vec(); + let item_peer_id = node.public_key; let avail = node.avail_storage as i32; let max = node.max_storage as i32; let files = node.files as i32; // emit event - Self::deposit_event(Event::ExportIpfsStats(item_addr, item_peer_id, avail, max, files)); + let signer = Signer::::all_accounts(); + if !signer.can_sign() { + log::error!("No local accounts available. Consider adding one via `author_insertKey` RPC method."); + } + + let results = + signer.send_signed_transaction(|_account| Call::submit_ipfs_emit_stats_result { + peer_id: item_peer_id.clone(), + multiaddress: item_addr.clone(), + avail_storage: avail, + max_storage: max, + files, + }); + + for (_, res) in &results { + match res { + Ok(()) => { + log::info!("Emitted ipfs node stats"); + }, + Err(e) => log::error!("Failed to submit transaction: {:?}", e), + } + } Ok(()) } diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 6f6be28ce..d7a3879e6 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -222,7 +222,7 @@ pub mod pallet { DeleteIpfsAsset(T::AccountId, Vec), UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), - ExportIpfsStats(Vec, Vec, i32, i32, i32), + ExportIpfsStats(T::AccountId, Vec, Vec, i32, i32, i32), } // Storage items. @@ -277,10 +277,12 @@ pub mod pallet { } } - let x: T::BlockNumber = T::UpdateDuration::get().try_into().ok().unwrap(); - if block_no % x == 0u32.into() { + if block_no % T::UpdateDuration::get().try_into().ok().unwrap() == 0u32.into() { if let Err(e) = Self::emit_ipfs_stats() { - log::error!("IPFS: Encountered an error while publishing metadata {:?}", e); + log::error!( + "IPFS: Encountered an error while publishing IPFS Node stats {:?}", + e + ); } } @@ -594,6 +596,29 @@ pub mod pallet { Ok(()) } + #[pallet::weight(0)] + pub fn submit_ipfs_emit_stats_result( + origin: OriginFor, + peer_id: Vec, + multiaddress: Vec, + avail_storage: i32, + max_storage: i32, + files: i32, + ) -> DispatchResult { + let signer = ensure_signed(origin)?; + + Self::deposit_event(Event::ExportIpfsStats( + signer, + peer_id, + multiaddress, + avail_storage, + max_storage, + files, + )); + + Ok(()) + } + /// Give ownership of an asset to user. #[pallet::weight(0)] pub fn add_owner( From 2e544403921416c1ce72659404e43da3611062d0 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Tue, 9 Aug 2022 14:04:40 +0300 Subject: [PATCH 10/15] fix: duration of UpdateDuration for time_dev and time_prod --- bin/node/runtime/src/constants.rs | 4 ++++ bin/node/runtime/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/node/runtime/src/constants.rs b/bin/node/runtime/src/constants.rs index 193d431bd..19307cf05 100644 --- a/bin/node/runtime/src/constants.rs +++ b/bin/node/runtime/src/constants.rs @@ -72,6 +72,9 @@ pub mod time_dev { // Attempting to do so will brick block production. pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 2 * MINUTES; pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = EPOCH_DURATION_IN_BLOCKS; + + pub const UPDATE_DURATION: BlockNumber = EPOCH_DURATION_IN_SLOTS * 3; + pub const SESSIONS_PER_ERA: sp_staking::SessionIndex = 3; pub const BONDING_DURATION: pallet_staking::EraIndex = 24 * 8; pub const SLASH_DEFER_DURATION: pallet_staking::EraIndex = 24 * 2; @@ -137,6 +140,7 @@ pub mod time_prod { pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10 * MINUTES; pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = EPOCH_DURATION_IN_BLOCKS; + pub const UPDATE_DURATION: BlockNumber = EPOCH_DURATION_IN_SLOTS * 6; // NOTE: Currently it is not possible to change the epoch duration after the chain has started. // Attempting to do so will brick block production. pub const SESSIONS_PER_ERA: sp_staking::SessionIndex = 6; diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 347b6ad7d..baf6f1a50 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -916,7 +916,7 @@ impl pallet_transaction_storage::Config for Runtime { } parameter_types! { - pub const UpdateDuration: u64 = EPOCH_DURATION_IN_SLOTS as u64 * 6; + pub const UpdateDuration: u64 = UPDATE_DURATION as u64; pub const MaxIpfsOwned: u32 = 5; pub const DefaultAssetLifetime: BlockNumber = DEFAULT_ASSET_LIFETIME; } From ea8a3ca523810645a67e356ad5a199569ccc9528 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Tue, 9 Aug 2022 19:14:19 +0300 Subject: [PATCH 11/15] add: submit storage payment result extrinsic --- bin/node/pallets/pallet-ipfs/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index d7a3879e6..17f90a71c 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -223,6 +223,7 @@ pub mod pallet { UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), ExportIpfsStats(T::AccountId, Vec, Vec, i32, i32, i32), + StorageValidatorPaid(T::AccountId, Vec, BalanceOf), } // Storage items. @@ -619,6 +620,23 @@ pub mod pallet { Ok(()) } + #[pallet::weight(0)] + pub fn submit_storage_validator_payment_result( + origin: OriginFor, + peer_id: Vec, + payment: BalanceOf, + ) -> DispatchResult { + let signer = ensure_signed(origin)?; + + Self::deposit_event(Event::StorageValidatorPaid( + signer, + peer_id, + payment, + )); + + Ok(()) + } + /// Give ownership of an asset to user. #[pallet::weight(0)] pub fn add_owner( From 92b5ee46677b775cd5acfc6a39da60ef08a0142d Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Tue, 9 Aug 2022 19:21:44 +0300 Subject: [PATCH 12/15] fix: function and event name --- bin/node/pallets/pallet-ipfs/src/lib.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 17f90a71c..6c006f463 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -223,7 +223,7 @@ pub mod pallet { UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), ExportIpfsStats(T::AccountId, Vec, Vec, i32, i32, i32), - StorageValidatorPaid(T::AccountId, Vec, BalanceOf), + SessionResults(T::AccountId, Vec, BalanceOf), } // Storage items. @@ -621,18 +621,14 @@ pub mod pallet { } #[pallet::weight(0)] - pub fn submit_storage_validator_payment_result( + pub fn submit_session_results( origin: OriginFor, peer_id: Vec, payment: BalanceOf, ) -> DispatchResult { let signer = ensure_signed(origin)?; - Self::deposit_event(Event::StorageValidatorPaid( - signer, - peer_id, - payment, - )); + Self::deposit_event(Event::SessionResults(signer, peer_id, payment)); Ok(()) } From deb1c666c800ad621a00faee0c1817456f241a48 Mon Sep 17 00:00:00 2001 From: Fotis Zikos Date: Fri, 12 Aug 2022 19:04:56 +0300 Subject: [PATCH 13/15] fix: storage types and multiaddr conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Martín Noblía --- bin/node/pallets/pallet-ipfs/src/functions.rs | 6 +++--- bin/node/pallets/pallet-ipfs/src/lib.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index bbd563d74..292bf86f7 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -603,10 +603,10 @@ impl Pallet { let node = Self::ipfs_nodes(&public_key).ok_or(>::IpfsNodeNotExist)?; // convert types - let item_addr = from_utf8(&node.multiaddress.encode()).unwrap().as_bytes().to_vec(); + let item_addr = node.multiaddress.encode(); let item_peer_id = node.public_key; - let avail = node.avail_storage as i32; - let max = node.max_storage as i32; + let avail = node.avail_storage as i64; + let max = node.max_storage as i64; let files = node.files as i32; // emit event diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 6c006f463..103e192da 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -222,7 +222,7 @@ pub mod pallet { DeleteIpfsAsset(T::AccountId, Vec), UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), - ExportIpfsStats(T::AccountId, Vec, Vec, i32, i32, i32), + ExportIpfsStats(T::AccountId, Vec, Vec, i64, i64, i32), SessionResults(T::AccountId, Vec, BalanceOf), } @@ -602,8 +602,8 @@ pub mod pallet { origin: OriginFor, peer_id: Vec, multiaddress: Vec, - avail_storage: i32, - max_storage: i32, + avail_storage: i64, + max_storage: i64, files: i32, ) -> DispatchResult { let signer = ensure_signed(origin)?; From 23b4ad62460a9cc5fef885af1b7a0f35e1db69af Mon Sep 17 00:00:00 2001 From: elsuizo Date: Tue, 16 Aug 2022 12:29:38 -0300 Subject: [PATCH 14/15] fix: #123 refactor `ExportIpfsStats` to contain a `Vec Pallet { pub fn emit_ipfs_stats() -> Result<(), Error> { let deadline = Some(timestamp().add(Duration::from_millis(5_0000))); - let (public_key, _addrs) = if let IpfsResponse::Identity(public_key, addrs) = - Self::ipfs_request(IpfsRequest::Identity, deadline)? - { - (public_key, addrs) - } else { - unreachable!("only `Identity` is a valid response type."); - }; - - // get the info from IpfsNode storage - let node = Self::ipfs_nodes(&public_key).ok_or(>::IpfsNodeNotExist)?; - - // convert types - let item_addr = node.multiaddress.encode(); - let item_peer_id = node.public_key; - let avail = node.avail_storage as i64; - let max = node.max_storage as i64; - let files = node.files as i32; - + Self::ipfs_request(IpfsRequest::Identity, deadline)?; // emit event let signer = Signer::::all_accounts(); if !signer.can_sign() { log::error!("No local accounts available. Consider adding one via `author_insertKey` RPC method."); } - let results = - signer.send_signed_transaction(|_account| Call::submit_ipfs_emit_stats_result { - peer_id: item_peer_id.clone(), - multiaddress: item_addr.clone(), - avail_storage: avail, - max_storage: max, - files, - }); + let results = signer.send_signed_transaction(move |_account| { + Call::submit_ipfs_emit_stats_result { nodes: IPFSNodes::::iter_values().collect() } + }); for (_, res) in &results { match res { diff --git a/bin/node/pallets/pallet-ipfs/src/lib.rs b/bin/node/pallets/pallet-ipfs/src/lib.rs index 103e192da..3ef516c89 100755 --- a/bin/node/pallets/pallet-ipfs/src/lib.rs +++ b/bin/node/pallets/pallet-ipfs/src/lib.rs @@ -22,7 +22,7 @@ use sp_core::{ Bytes, }; use sp_io::offchain::timestamp; -use sp_std::{convert::TryInto, str::from_utf8, vec::Vec}; +use sp_std::{convert::TryInto, vec::Vec}; pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"chfs"); @@ -222,7 +222,7 @@ pub mod pallet { DeleteIpfsAsset(T::AccountId, Vec), UnpinIpfsAsset(T::AccountId, Vec), ExtendIpfsStorageDuration(T::AccountId, Vec), - ExportIpfsStats(T::AccountId, Vec, Vec, i64, i64, i32), + ExportIpfsStats(Vec), SessionResults(T::AccountId, Vec, BalanceOf), } @@ -600,22 +600,11 @@ pub mod pallet { #[pallet::weight(0)] pub fn submit_ipfs_emit_stats_result( origin: OriginFor, - peer_id: Vec, - multiaddress: Vec, - avail_storage: i64, - max_storage: i64, - files: i32, + nodes: Vec, ) -> DispatchResult { - let signer = ensure_signed(origin)?; + ensure_signed(origin)?; - Self::deposit_event(Event::ExportIpfsStats( - signer, - peer_id, - multiaddress, - avail_storage, - max_storage, - files, - )); + Self::deposit_event(Event::ExportIpfsStats(nodes)); Ok(()) } From a266c155aa67079959c24f0ce5e7ecf4223f5267 Mon Sep 17 00:00:00 2001 From: elsuizo Date: Tue, 16 Aug 2022 12:41:51 -0300 Subject: [PATCH 15/15] remove unnecessary `move` keyword in a closure --- bin/node/pallets/pallet-ipfs/src/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node/pallets/pallet-ipfs/src/functions.rs b/bin/node/pallets/pallet-ipfs/src/functions.rs index 874158d05..f86de1ddf 100644 --- a/bin/node/pallets/pallet-ipfs/src/functions.rs +++ b/bin/node/pallets/pallet-ipfs/src/functions.rs @@ -598,7 +598,7 @@ impl Pallet { log::error!("No local accounts available. Consider adding one via `author_insertKey` RPC method."); } - let results = signer.send_signed_transaction(move |_account| { + let results = signer.send_signed_transaction(|_account| { Call::submit_ipfs_emit_stats_result { nodes: IPFSNodes::::iter_values().collect() } });