Skip to content

Commit

Permalink
Add pallet-referenda-tracks
Browse files Browse the repository at this point in the history
Implement TracksInfo for referenda-tracks
  • Loading branch information
olanod committed Dec 21, 2023
1 parent 0c15d96 commit 1b05e74
Show file tree
Hide file tree
Showing 9 changed files with 577 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ members = [
"substrate/frame/ranked-collective",
"substrate/frame/recovery",
"substrate/frame/referenda",
"substrate/frame/referenda-tracks",
"substrate/frame/remark",
"substrate/frame/root-offences",
"substrate/frame/root-testing",
Expand Down
58 changes: 58 additions & 0 deletions substrate/frame/referenda-tracks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "pallet-referenda-tracks"
version = "4.0.0-dev"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
repository.workspace = true
description = "FRAME pallet to manage voting tracks in referenda"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.188", optional = true }
frame-benchmarking = { path = "../benchmarking", default-features = false, optional = true }
frame-support = { path = "../support", default-features = false }
frame-system = { path = "../system", default-features = false }
sp-core = { path = "../../primitives/core", default-features = false }
sp-io = { path = "../../primitives/io", default-features = false }
sp-runtime = { path = "../../primitives/runtime", default-features = false }
sp-std = { path = "../../primitives/std", default-features = false }
pallet-referenda = { path = "../referenda", default-features = false }

[dev-dependencies]
sp-core = { path = "../../primitives/core", default-features = false}
pallet-scheduler = { path = "../scheduler" }
pallet-preimage = { path = "../preimage" }
pallet-balances = { path = "../balances" }

[features]
default = [ "std" ]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"serde",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
6 changes: 6 additions & 0 deletions substrate/frame/referenda-tracks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Remark Storage Pallet

Allows storing arbitrary data off chain.


License: Apache-2.0
47 changes: 47 additions & 0 deletions substrate/frame/referenda-tracks/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// 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.

//! Benchmarks for remarks pallet
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v1::{benchmarks, whitelisted_caller};
use frame_system::{EventRecord, Pallet as System, RawOrigin};
use sp_std::*;

#[cfg(test)]
use crate::Pallet as Remark;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = System::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

benchmarks! {
store {
let l in 1 .. 1024*1024;
let caller: T::AccountId = whitelisted_caller();
}: _(RawOrigin::Signed(caller.clone()), vec![0u8; l as usize])
verify {
assert_last_event::<T>(Event::Stored { sender: caller, content_hash: sp_io::hashing::blake2_256(&vec![0u8; l as usize]).into() }.into());
}

impl_benchmark_test_suite!(Remark, crate::mock::new_test_ext(), crate::mock::Test);
}
127 changes: 127 additions & 0 deletions substrate/frame/referenda-tracks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// 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.

//! Edit and manage referenda voring tracks.
#![cfg_attr(not(feature = "std"), no_std)]

mod benchmarking;
pub mod weights;

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

use core::iter::Map;
use frame_support::{storage::PrefixIterator, traits::OriginTrait};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_referenda::{BalanceOf, PalletsOriginOf, Track, TrackIdOf, TrackInfoOf};
use sp_core::Get;
use sp_std::{borrow::Cow, vec::Vec};

pub use pallet::*;
pub use weights::WeightInfo;

type TrackOf<T, I> = Track<<T as Config<I>>::TrackId, BalanceOf<T, I>, BlockNumberFor<T>>;

type TracksIter<T, I> = Map<
PrefixIterator<(<T as Config<I>>::TrackId, TrackInfoOf<T, I>)>,
fn((<T as Config<I>>::TrackId, TrackInfoOf<T, I>)) -> Cow<'static, TrackOf<T, I>>,
>;

impl<T: Config<I>, I> pallet_referenda::TracksInfo<BalanceOf<T, I>, BlockNumberFor<T>>
for Pallet<T, I>
{
type Id = T::TrackId;
type RuntimeOrigin = <T::RuntimeOrigin as OriginTrait>::PalletsOrigin;
type TracksIter = TracksIter<T, I>;

fn tracks() -> Self::TracksIter {
Tracks::<T, I>::iter().map(|(id, info)| Cow::Owned(Track { id, info }))
}
fn track_for(origin: &Self::RuntimeOrigin) -> Result<Self::Id, ()> {
OriginToTrackId::<T, I>::get(origin).ok_or(())
}
fn tracks_ids() -> Vec<Self::Id> {
TracksIds::<T, I>::get().into_inner()
}
fn info(id: Self::Id) -> Option<Cow<'static, TrackInfoOf<T, I>>> {
Tracks::<T, I>::get(id).map(Cow::Owned)
}
}

impl<T: Config<I>, I: 'static> Get<Vec<TrackOf<T, I>>> for crate::Pallet<T, I> {
fn get() -> Vec<Track<T::TrackId, BalanceOf<T, I>, BlockNumberFor<T>>> {
// expensive but it doesn't seem to be used anywhere
<Pallet<T, I> as pallet_referenda::TracksInfo<BalanceOf<T, I>, BlockNumberFor<T>>>::tracks()
.map(|t| t.into_owned())
.collect()
}
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config + pallet_referenda::Config<I> {
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;

type TrackId: Parameter + Member + Copy + MaxEncodedLen + Ord;

type MaxTracks: Get<u32>;
// type WeightInfo: WeightInfo;
}

#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
pub fn udpate(origin: OriginFor<T>, _id: TrackIdOf<T, I>) -> DispatchResultWithPostInfo {
let _sender = ensure_signed(origin)?;
// Self::deposit_event(Event::Foo { sender });
Ok(().into())
}
}

#[pallet::storage]
pub type TracksIds<T: Config<I>, I: 'static = ()> =
StorageValue<_, BoundedVec<T::TrackId, <T as Config<I>>::MaxTracks>, ValueQuery>;

#[pallet::storage]
pub type OriginToTrackId<T: Config<I>, I: 'static = ()> =
StorageMap<_, Blake2_128Concat, PalletsOriginOf<T>, T::TrackId>;

#[pallet::storage]
pub type Tracks<T: Config<I>, I: 'static = ()> =
StorageMap<_, Blake2_128Concat, T::TrackId, TrackInfoOf<T, I>>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
// Foo(T::AccountId),
}

#[pallet::error]
pub enum Error<T, I = ()> {}
}
Loading

0 comments on commit 1b05e74

Please sign in to comment.