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 Nov 10, 2023
1 parent b04cd26 commit e1a2699
Show file tree
Hide file tree
Showing 11 changed files with 524 additions and 456 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

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

/// Referendum `TrackId` type.
pub type TrackId = u16;
Expand Down Expand Up @@ -46,13 +49,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 +75,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 +97,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 +119,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 +141,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 +163,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 +185,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 +207,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 +229,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 +251,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 Down
Loading

0 comments on commit e1a2699

Please sign in to comment.