forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement TracksInfo for referenda-tracks
- Loading branch information
Showing
9 changed files
with
577 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ()> {} | ||
} |
Oops, something went wrong.