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 paritytech#68 from subspace/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
nazar-pc authored Oct 11, 2021
2 parents 7d403f0 + 5ef4f8d commit 1a999e2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 81 deletions.
46 changes: 2 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 68 additions & 32 deletions crates/pallet-feeds/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
// Copyright (C) 2021 Subspace Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Pallet feeds, used for storing arbitrary user-provided data combined into feeds.
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, missing_debug_implementations)]

use codec::{Compact, CompactLen};
use core::mem;
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]
pub mod pallet {
mod pallet {
use super::*;
use frame_support::dispatch::DispatchResult;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;

#[pallet::config]
pub trait Config: frame_system::Config {
/// `pallet-feeds` events
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}

/// Pallet feeds, used for storing arbitrary user-provided data combined into feeds.
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

pub type PutDataObject = Vec<u8>;
pub type FeedId = u64;
pub type ObjectMetadata = Vec<u8>;
/// User-provided object to store
pub(super) type PutDataObject = Vec<u8>;
/// ID of the feed
pub(super) type FeedId = u64;
/// User-provided object metadata (not addressable directly, but available in an even)
pub(super) type ObjectMetadata = Vec<u8>;

/// Total amount of data and number of objects stored in a feed
#[derive(Decode, Encode, TypeInfo, Default)]
pub struct TotalObjectsAndSize {
pub size: u64,
pub objects: u64,
#[cfg_attr(feature = "std", derive(Debug))]
pub(super) struct TotalObjectsAndSize {
/// Total size of objects in bytes
pub(super) size: u64,
/// Total number of objects
pub(super) count: u64,
}

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

#[pallet::storage]
pub type Totals<T: Config> =
pub(super) 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>;
pub(super) type CurrentFeedId<T: Config> = StorageValue<_, FeedId, ValueQuery>;

/// `pallet-feeds` events
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand All @@ -51,14 +82,34 @@ pub mod pallet {
FeedCreated(FeedId, T::AccountId),
}

/// `pallet-feeds` errors
#[pallet::error]
pub enum Error<T> {
UknownFeedId,
/// `FeedId` doesn't exist
UnknownFeedId,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
// TODO: add proper weights
/// Create a new feed
#[pallet::weight(10_000)]
pub fn create(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;

let feed_id = Self::current_feed_id();

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(())
}

// TODO: add proper weights
/// Put a new object into a feed
#[pallet::weight(10_000)]
pub fn put(
origin: OriginFor<T>,
Expand All @@ -75,39 +126,24 @@ pub mod pallet {

let current_feed_id = Self::current_feed_id();

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

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

Totals::<T>::mutate(feed_id, |feed_totals| {
feed_totals.size += object_size;
feed_totals.objects += 1;
feed_totals.count += 1;
});

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

Ok(())
}

// TODO: add proper weights
#[pallet::weight(10_000)]
pub fn create(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;

let feed_id = Self::current_feed_id();

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(())
}
}
}

/// Mapping to the object offset and size within an extrinsic
#[derive(Debug)]
pub struct CallObjectLocation {
/// Offset
pub offset: usize,
Expand Down
2 changes: 0 additions & 2 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ hex-literal = { version = "0.3.3", optional = true }
pallet-balances = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "91b386ff07a85b3dd50ff3ed29c97e6b29d15f05" }
pallet-feeds = { version = "0.1.0", default-features = false, path = "../pallet-feeds" }
pallet-offences-subspace = { version = "0.1.0", default-features = false, path = "../pallet-offences-subspace" }
pallet-randomness-collective-flip = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "91b386ff07a85b3dd50ff3ed29c97e6b29d15f05" }
pallet-subspace = { version = "0.1.0", default-features = false, path = "../pallet-subspace" }
pallet-sudo = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "91b386ff07a85b3dd50ff3ed29c97e6b29d15f05" }
pallet-timestamp = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "91b386ff07a85b3dd50ff3ed29c97e6b29d15f05" }
Expand Down Expand Up @@ -61,7 +60,6 @@ std = [
"pallet-balances/std",
"pallet-feeds/std",
"pallet-offences-subspace/std",
"pallet-randomness-collective-flip/std",
"pallet-subspace/std",
"pallet-sudo/std",
"pallet-timestamp/std",
Expand Down
3 changes: 0 additions & 3 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ impl frame_system::Config for Runtime {
type OnSetCode = ();
}

impl pallet_randomness_collective_flip::Config for Runtime {}

parameter_types! {
pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS;
pub const EraDuration: u32 = ERA_DURATION_IN_BLOCKS;
Expand Down Expand Up @@ -368,7 +366,6 @@ construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Subspace: pallet_subspace::{Pallet, Call, Storage, Config, Event, ValidateUnsigned},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Expand Down

0 comments on commit 1a999e2

Please sign in to comment.