Skip to content

Commit

Permalink
Change TracksInfo::tracks to return an iterator
Browse files Browse the repository at this point in the history
Using an iterator instead of a static slice allows for more flexible
implementations of `TracksInfo` that can use the chain storage without
compromising a lot on the performance/memory penalty if we were to return
an owned `Vec` instead.
  • Loading branch information
olanod committed Oct 28, 2023
1 parent a706171 commit b059f67
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 83 deletions.
27 changes: 15 additions & 12 deletions polkadot/runtime/rococo/src/governance/fellowship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use frame_support::traits::{MapSuccess, TryMapSuccess};
use sp_runtime::traits::{CheckedReduceBy, ConstU16, Replace};
use sp_std::borrow::Cow::Borrowed;

use super::*;
use crate::{CENTS, DAYS};
Expand All @@ -32,12 +33,14 @@ pub struct TracksInfo;
impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
type Id = u16;
type RuntimeOrigin = <RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;
fn tracks() -> &'static [(Self::Id, pallet_referenda::TrackInfo<Balance, BlockNumber>)] {
type TracksIter = pallet_referenda::StaticTracksIter<Self::Id, Balance, BlockNumber>;

fn tracks() -> Self::TracksIter {
static DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 10] = [
(
0u16,
pallet_referenda::TrackInfo {
name: "candidates",
name: Borrowed("candidates"),
max_deciding: 10,
decision_deposit: 100 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -59,7 +62,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
1u16,
pallet_referenda::TrackInfo {
name: "members",
name: Borrowed("members"),
max_deciding: 10,
decision_deposit: 10 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -81,7 +84,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
2u16,
pallet_referenda::TrackInfo {
name: "proficients",
name: Borrowed("proficients"),
max_deciding: 10,
decision_deposit: 10 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -103,7 +106,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
3u16,
pallet_referenda::TrackInfo {
name: "fellows",
name: Borrowed("fellows"),
max_deciding: 10,
decision_deposit: 10 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -125,7 +128,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
4u16,
pallet_referenda::TrackInfo {
name: "senior fellows",
name: Borrowed("senior fellows"),
max_deciding: 10,
decision_deposit: 10 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -147,7 +150,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
5u16,
pallet_referenda::TrackInfo {
name: "experts",
name: Borrowed("experts"),
max_deciding: 10,
decision_deposit: 1 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -169,7 +172,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
6u16,
pallet_referenda::TrackInfo {
name: "senior experts",
name: Borrowed("senior experts"),
max_deciding: 10,
decision_deposit: 1 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -191,7 +194,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
7u16,
pallet_referenda::TrackInfo {
name: "masters",
name: Borrowed("masters"),
max_deciding: 10,
decision_deposit: 1 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -213,7 +216,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
8u16,
pallet_referenda::TrackInfo {
name: "senior masters",
name: Borrowed("senior masters"),
max_deciding: 10,
decision_deposit: 1 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -235,7 +238,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
(
9u16,
pallet_referenda::TrackInfo {
name: "grand masters",
name: Borrowed("grand masters"),
max_deciding: 10,
decision_deposit: 1 * 3 * CENTS,
prepare_period: 30 * MINUTES,
Expand All @@ -255,7 +258,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
},
),
];
&DATA[..]
DATA.iter().cloned()
}
fn track_for(id: &Self::RuntimeOrigin) -> Result<Self::Id, ()> {
use super::origins::Origin;
Expand Down
39 changes: 21 additions & 18 deletions polkadot/runtime/rococo/src/governance/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use super::*;
const fn percent(x: i32) -> sp_arithmetic::FixedI64 {
sp_arithmetic::FixedI64::from_rational(x as u128, 100)
}
use pallet_referenda::Curve;
use pallet_referenda::{Curve, StaticTracksIter};
use sp_std::borrow::Cow::Borrowed;
const APP_ROOT: Curve = Curve::make_reciprocal(4, 28, percent(80), percent(50), percent(100));
const SUP_ROOT: Curve = Curve::make_linear(28, 28, percent(0), percent(50));
const APP_STAKING_ADMIN: Curve = Curve::make_linear(17, 28, percent(50), percent(100));
Expand Down Expand Up @@ -69,7 +70,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
0,
pallet_referenda::TrackInfo {
name: "root",
name: Borrowed("root"),
max_deciding: 1,
decision_deposit: 100 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -83,7 +84,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
1,
pallet_referenda::TrackInfo {
name: "whitelisted_caller",
name: Borrowed("whitelisted_caller"),
max_deciding: 100,
decision_deposit: 10 * GRAND,
prepare_period: 6 * MINUTES,
Expand All @@ -97,7 +98,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
10,
pallet_referenda::TrackInfo {
name: "staking_admin",
name: Borrowed("staking_admin"),
max_deciding: 10,
decision_deposit: 5 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -111,7 +112,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
11,
pallet_referenda::TrackInfo {
name: "treasurer",
name: Borrowed("treasurer"),
max_deciding: 10,
decision_deposit: 1 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -125,7 +126,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
12,
pallet_referenda::TrackInfo {
name: "lease_admin",
name: Borrowed("lease_admin"),
max_deciding: 10,
decision_deposit: 5 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -139,7 +140,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
13,
pallet_referenda::TrackInfo {
name: "fellowship_admin",
name: Borrowed("fellowship_admin"),
max_deciding: 10,
decision_deposit: 5 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -153,7 +154,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
14,
pallet_referenda::TrackInfo {
name: "general_admin",
name: Borrowed("general_admin"),
max_deciding: 10,
decision_deposit: 5 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -167,7 +168,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
15,
pallet_referenda::TrackInfo {
name: "auction_admin",
name: Borrowed("auction_admin"),
max_deciding: 10,
decision_deposit: 5 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -181,7 +182,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
20,
pallet_referenda::TrackInfo {
name: "referendum_canceller",
name: Borrowed("referendum_canceller"),
max_deciding: 1_000,
decision_deposit: 10 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -195,7 +196,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
21,
pallet_referenda::TrackInfo {
name: "referendum_killer",
name: Borrowed("referendum_killer"),
max_deciding: 1_000,
decision_deposit: 50 * GRAND,
prepare_period: 8 * MINUTES,
Expand All @@ -209,7 +210,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
30,
pallet_referenda::TrackInfo {
name: "small_tipper",
name: Borrowed("small_tipper"),
max_deciding: 200,
decision_deposit: 1 * 3 * CENTS,
prepare_period: 1 * MINUTES,
Expand All @@ -223,7 +224,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
31,
pallet_referenda::TrackInfo {
name: "big_tipper",
name: Borrowed("big_tipper"),
max_deciding: 100,
decision_deposit: 10 * 3 * CENTS,
prepare_period: 4 * MINUTES,
Expand All @@ -237,7 +238,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
32,
pallet_referenda::TrackInfo {
name: "small_spender",
name: Borrowed("small_spender"),
max_deciding: 50,
decision_deposit: 100 * 3 * CENTS,
prepare_period: 10 * MINUTES,
Expand All @@ -251,7 +252,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
33,
pallet_referenda::TrackInfo {
name: "medium_spender",
name: Borrowed("medium_spender"),
max_deciding: 50,
decision_deposit: 200 * 3 * CENTS,
prepare_period: 10 * MINUTES,
Expand All @@ -265,7 +266,7 @@ const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 15
(
34,
pallet_referenda::TrackInfo {
name: "big_spender",
name: Borrowed("big_spender"),
max_deciding: 50,
decision_deposit: 400 * 3 * CENTS,
prepare_period: 10 * MINUTES,
Expand All @@ -282,8 +283,10 @@ pub struct TracksInfo;
impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
type Id = u16;
type RuntimeOrigin = <RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;
fn tracks() -> &'static [(Self::Id, pallet_referenda::TrackInfo<Balance, BlockNumber>)] {
&TRACKS_DATA[..]
type TracksIter = StaticTracksIter<Self::Id, Balance, BlockNumber>;

fn tracks() -> Self::TracksIter {
TRACKS_DATA.iter().cloned()
}
fn track_for(id: &Self::RuntimeOrigin) -> Result<Self::Id, ()> {
if let Ok(system_origin) = frame_system::RawOrigin::try_from(id.clone()) {
Expand Down
Loading

0 comments on commit b059f67

Please sign in to comment.