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.

NOTE: This feature will benefit from the soon to be released
return_position_impl_trait_in_trait(rust-lang/rust#115822)
to not require the `TracksIter` associated type and from a bugfix in the
compiler(rust-lang/rust#116662) to declare the
DEFAULT_MAX_TRACK_NAME_LEN generic constant in the `TracksInfo` trait.
  • Loading branch information
olanod authored and pandres95 committed Dec 22, 2023
1 parent b35300c commit 11126c5
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

use super::Origin;
use crate::{Balance, BlockNumber, RuntimeOrigin, DAYS, DOLLARS, HOURS};
use sp_runtime::Perbill;
use pallet_referenda::{StaticTracksIter, Track};
use sp_runtime::{str_array as s, Perbill};
use sp_std::borrow::Cow::Borrowed;

/// Referendum `TrackId` type.
pub type TrackId = u16;
Expand Down Expand Up @@ -46,13 +48,15 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {

type RuntimeOrigin = <RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;

type TracksIter = StaticTracksIter<Self::Id, Balance, BlockNumber>;

/// Return the array of available tracks and their information.
fn tracks() -> &'static [(Self::Id, pallet_referenda::TrackInfo<Balance, BlockNumber>)] {
static DATA: [(TrackId, pallet_referenda::TrackInfo<Balance, BlockNumber>); 9] = [
(
constants::AMBASSADOR_TIER_1,
pallet_referenda::TrackInfo {
name: "ambassador tier 1",
fn tracks() -> Self::TracksIter {
static DATA: [Track<TrackId, Balance, BlockNumber>; 9] = [
Track {
id: constants::AMBASSADOR_TIER_1,
info: pallet_referenda::TrackInfo {
name: s("ambassador tier 1"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -70,11 +74,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::AMBASSADOR_TIER_2,
pallet_referenda::TrackInfo {
name: "ambassador tier 2",
},
Track {
id: constants::AMBASSADOR_TIER_2,
info: pallet_referenda::TrackInfo {
name: s("ambassador tier 2"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -92,11 +96,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::SENIOR_AMBASSADOR_TIER_3,
pallet_referenda::TrackInfo {
name: "senior ambassador tier 3",
},
Track {
id: constants::SENIOR_AMBASSADOR_TIER_3,
info: pallet_referenda::TrackInfo {
name: s("senior ambassador tier 3"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -114,11 +118,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::SENIOR_AMBASSADOR_TIER_4,
pallet_referenda::TrackInfo {
name: "senior ambassador tier 4",
},
Track {
id: constants::SENIOR_AMBASSADOR_TIER_4,
info: pallet_referenda::TrackInfo {
name: s("senior ambassador tier 4"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -136,11 +140,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::HEAD_AMBASSADOR_TIER_5,
pallet_referenda::TrackInfo {
name: "head ambassador tier 5",
},
Track {
id: constants::HEAD_AMBASSADOR_TIER_5,
info: pallet_referenda::TrackInfo {
name: s("head ambassador tier 5"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -158,11 +162,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::HEAD_AMBASSADOR_TIER_6,
pallet_referenda::TrackInfo {
name: "head ambassador tier 6",
},
Track {
id: constants::HEAD_AMBASSADOR_TIER_6,
info: pallet_referenda::TrackInfo {
name: s("head ambassador tier 6"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -180,11 +184,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::HEAD_AMBASSADOR_TIER_7,
pallet_referenda::TrackInfo {
name: "head ambassador tier 7",
},
Track {
id: constants::HEAD_AMBASSADOR_TIER_7,
info: pallet_referenda::TrackInfo {
name: s("head ambassador tier 7"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -202,11 +206,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::MASTER_AMBASSADOR_TIER_8,
pallet_referenda::TrackInfo {
name: "master ambassador tier 8",
},
Track {
id: constants::MASTER_AMBASSADOR_TIER_8,
info: pallet_referenda::TrackInfo {
name: s("master ambassador tier 8"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -224,11 +228,11 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
(
constants::MASTER_AMBASSADOR_TIER_9,
pallet_referenda::TrackInfo {
name: "master ambassador tier 9",
},
Track {
id: constants::MASTER_AMBASSADOR_TIER_9,
info: pallet_referenda::TrackInfo {
name: s("master ambassador tier 9"),
max_deciding: 10,
decision_deposit: 5 * DOLLARS,
prepare_period: 24 * HOURS,
Expand All @@ -246,9 +250,9 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
ceil: Perbill::from_percent(50),
},
},
),
},
];
&DATA[..]
DATA.iter().map(Borrowed)
}

/// Determine the voting track for the given `origin`.
Expand All @@ -259,7 +263,7 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
// It is important that this is not available in production!
let root: Self::RuntimeOrigin = frame_system::RawOrigin::Root.into();
if &root == id {
return Ok(constants::MASTER_AMBASSADOR_TIER_9)
return Ok(constants::MASTER_AMBASSADOR_TIER_9);
}
}

Expand Down
Loading

0 comments on commit 11126c5

Please sign in to comment.