Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #50 from subspace/pallet-feeds-add-totals
Browse files Browse the repository at this point in the history
pallet-feeds: added Totals map, TotalObjectsAndSize struct
  • Loading branch information
isSerge authored Oct 4, 2021
2 parents 3d10233 + 9132312 commit c075b9e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions crates/pallet-feeds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use scale_info::TypeInfo;
use sp_std::vec::Vec;

#[frame_support::pallet]
Expand All @@ -20,18 +21,30 @@ pub mod pallet {
pub type FeedId = u64;
pub type ObjectMetadata = Vec<u8>;

#[derive(Decode, Encode, TypeInfo, Default)]
pub struct TotalObjectsAndSize {
pub size: u64,
pub objects: u64,
}

#[pallet::storage]
pub type Feeds<T: Config> =
StorageMap<_, Blake2_128Concat, FeedId, ObjectMetadata, OptionQuery>;

#[pallet::storage]
pub type Totals<T: Config> =
StorageMap<_, Blake2_128Concat, FeedId, TotalObjectsAndSize, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn current_feed_id)]
pub type CurrentFeedId<T: Config> = StorageValue<_, FeedId, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
DataSubmitted(ObjectMetadata, T::AccountId),
/// New object is added \[object_metadata, account_id, object_size\]
DataSubmitted(ObjectMetadata, T::AccountId, u64),
/// New feed is created \[feed_id, account_id\]
FeedCreated(FeedId, T::AccountId),
}

Expand All @@ -52,17 +65,23 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

// TODO: add data handling
let object_size = data.len() as u64;

log::debug!("metadata: {:?}", metadata);
log::debug!("data.len: {:?}", data.len());
log::debug!("object_size: {:?}", object_size);

let current_feed_id = Self::current_feed_id();

ensure!(current_feed_id >= feed_id, Error::<T>::UknownFeedId);

Feeds::<T>::insert(feed_id, metadata.clone());

Self::deposit_event(Event::DataSubmitted(metadata, who));
Totals::<T>::mutate(feed_id, |feed_totals| {
feed_totals.size += object_size;
feed_totals.objects += 1;
});

Self::deposit_event(Event::DataSubmitted(metadata, who, object_size));

Ok(())
}
Expand All @@ -76,6 +95,8 @@ pub mod pallet {

CurrentFeedId::<T>::mutate(|feed_id| *feed_id = feed_id.saturating_add(1));

Totals::<T>::insert(feed_id, TotalObjectsAndSize::default());

Self::deposit_event(Event::FeedCreated(feed_id, who));

Ok(())
Expand Down

0 comments on commit c075b9e

Please sign in to comment.