From 46abe420ea681e39a5067d44addac571be129d54 Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sat, 2 Nov 2024 12:11:06 +0100 Subject: [PATCH 1/2] Update Pallet Referenda to support relay chain Block Number Provider --- substrate/frame/referenda/src/benchmarking.rs | 12 ++++--- substrate/frame/referenda/src/lib.rs | 32 +++++++++++-------- substrate/frame/referenda/src/mock.rs | 1 + 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/substrate/frame/referenda/src/benchmarking.rs b/substrate/frame/referenda/src/benchmarking.rs index 67ac82787d31..e24a97c51bf3 100644 --- a/substrate/frame/referenda/src/benchmarking.rs +++ b/substrate/frame/referenda/src/benchmarking.rs @@ -33,6 +33,10 @@ use sp_runtime::traits::Bounded as ArithBounded; const SEED: u32 = 0; +fn set_block_number, I: 'static>(n: BlockNumberFor) { + >::BlockNumberProvider::set_block_number(n); +} + fn assert_last_event, I: 'static>(generic_event: >::RuntimeEvent) { frame_system::Pallet::::assert_last_event(generic_event.into()); } @@ -151,25 +155,25 @@ fn make_failing, I: 'static>(index: ReferendumIndex) { fn skip_prepare_period, I: 'static>(index: ReferendumIndex) { let status = Referenda::::ensure_ongoing(index).unwrap(); let prepare_period_over = status.submitted + info::(index).prepare_period; - frame_system::Pallet::::set_block_number(prepare_period_over); + set_block_number::(prepare_period_over); } fn skip_decision_period, I: 'static>(index: ReferendumIndex) { let status = Referenda::::ensure_ongoing(index).unwrap(); let decision_period_over = status.deciding.unwrap().since + info::(index).decision_period; - frame_system::Pallet::::set_block_number(decision_period_over); + set_block_number::(decision_period_over); } fn skip_confirm_period, I: 'static>(index: ReferendumIndex) { let status = Referenda::::ensure_ongoing(index).unwrap(); let confirm_period_over = status.deciding.unwrap().confirming.unwrap(); - frame_system::Pallet::::set_block_number(confirm_period_over); + set_block_number::(confirm_period_over); } fn skip_timeout_period, I: 'static>(index: ReferendumIndex) { let status = Referenda::::ensure_ongoing(index).unwrap(); let timeout_period_over = status.submitted + T::UndecidingTimeout::get(); - frame_system::Pallet::::set_block_number(timeout_period_over); + set_block_number::(timeout_period_over); } fn alarm_time, I: 'static>( diff --git a/substrate/frame/referenda/src/lib.rs b/substrate/frame/referenda/src/lib.rs index e72dd7f11cbb..ce3d911d0b1a 100644 --- a/substrate/frame/referenda/src/lib.rs +++ b/substrate/frame/referenda/src/lib.rs @@ -105,6 +105,7 @@ pub use self::{ }, weights::WeightInfo, }; +use sp_runtime::traits::BlockNumberProvider; pub use alloc::vec::Vec; #[cfg(test)] @@ -239,6 +240,9 @@ pub mod pallet { /// The preimage provider. type Preimages: QueryPreimage + StorePreimage; + + /// Provider for the block number. Normally this is the `frame_system` pallet. + type BlockNumberProvider: BlockNumberProvider>; } /// The next free referendum index, aka the number of referenda started so far. @@ -485,7 +489,7 @@ pub mod pallet { *x += 1; r }); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let nudge_call = T::Preimages::bound(CallOf::::from(Call::nudge_referendum { index }))?; let status = ReferendumStatus { @@ -527,7 +531,7 @@ pub mod pallet { let track = Self::track(status.track).ok_or(Error::::NoTrack)?; status.decision_deposit = Some(Self::take_deposit(who.clone(), track.decision_deposit)?); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let (info, _, branch) = Self::service_referendum(now, index, status); ReferendumInfoFor::::insert(index, info); let e = @@ -584,7 +588,7 @@ pub mod pallet { Self::note_one_fewer_deciding(status.track); Self::deposit_event(Event::::Cancelled { index, tally: status.tally }); let info = ReferendumInfo::Cancelled( - frame_system::Pallet::::block_number(), + T::BlockNumberProvider::current_block_number(), Some(status.submission_deposit), status.decision_deposit, ); @@ -611,7 +615,7 @@ pub mod pallet { Self::slash_deposit(Some(status.submission_deposit.clone())); Self::slash_deposit(status.decision_deposit.clone()); Self::do_clear_metadata(index); - let info = ReferendumInfo::Killed(frame_system::Pallet::::block_number()); + let info = ReferendumInfo::Killed(T::BlockNumberProvider::current_block_number()); ReferendumInfoFor::::insert(index, info); Ok(()) } @@ -627,7 +631,7 @@ pub mod pallet { index: ReferendumIndex, ) -> DispatchResultWithPostInfo { ensure_root(origin)?; - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let mut status = Self::ensure_ongoing(index)?; // This is our wake-up, so we can disregard the alarm. status.alarm = None; @@ -658,7 +662,7 @@ pub mod pallet { let mut track_queue = TrackQueue::::get(track); let branch = if let Some((index, mut status)) = Self::next_for_deciding(&mut track_queue) { - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let (maybe_alarm, branch) = Self::begin_deciding(&mut status, index, now, track_info); if let Some(set_alarm) = maybe_alarm { @@ -758,7 +762,7 @@ impl, I: 'static> Polling for Pallet { match ReferendumInfoFor::::get(index) { Some(ReferendumInfo::Ongoing(mut status)) => { let result = f(PollStatus::Ongoing(&mut status.tally, status.track)); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); Self::ensure_alarm_at(&mut status, index, now + One::one()); ReferendumInfoFor::::insert(index, ReferendumInfo::Ongoing(status)); result @@ -778,7 +782,7 @@ impl, I: 'static> Polling for Pallet { match ReferendumInfoFor::::get(index) { Some(ReferendumInfo::Ongoing(mut status)) => { let result = f(PollStatus::Ongoing(&mut status.tally, status.track))?; - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); Self::ensure_alarm_at(&mut status, index, now + One::one()); ReferendumInfoFor::::insert(index, ReferendumInfo::Ongoing(status)); Ok(result) @@ -800,7 +804,7 @@ impl, I: 'static> Polling for Pallet { *x += 1; r }); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let dummy_account_id = codec::Decode::decode(&mut sp_runtime::traits::TrailingZeroInput::new(&b"dummy"[..])) .expect("infinite length input; no invalid inputs for type; qed"); @@ -828,7 +832,7 @@ impl, I: 'static> Polling for Pallet { let mut status = Self::ensure_ongoing(index).map_err(|_| ())?; Self::ensure_no_alarm(&mut status); Self::note_one_fewer_deciding(status.track); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let info = if approved { ReferendumInfo::Approved(now, Some(status.submission_deposit), status.decision_deposit) } else { @@ -868,7 +872,7 @@ impl, I: 'static> Pallet { ReferendumInfo::Ongoing(status) => { let track = Self::track(status.track).ok_or(Error::::NoTrack)?; let elapsed = if let Some(deciding) = status.deciding { - frame_system::Pallet::::block_number().saturating_sub(deciding.since) + T::BlockNumberProvider::current_block_number().saturating_sub(deciding.since) } else { Zero::zero() }; @@ -893,7 +897,7 @@ impl, I: 'static> Pallet { origin: PalletsOriginOf, call: BoundedCallOf, ) { - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); // Earliest allowed block is always at minimum the next block. let earliest_allowed = now.saturating_add(track.min_enactment_period.max(One::one())); let desired = desired.evaluate(now); @@ -931,7 +935,7 @@ impl, I: 'static> Pallet { result.is_ok(), "Unable to schedule a new alarm at #{:?} (now: #{:?}), scheduler error: `{:?}`", when, - frame_system::Pallet::::block_number(), + T::BlockNumberProvider::current_block_number(), result.unwrap_err(), ); result.ok().map(|x| (when, x)) @@ -1023,7 +1027,7 @@ impl, I: 'static> Pallet { /// overall more efficient), however the weights become rather less easy to measure. fn note_one_fewer_deciding(track: TrackIdOf) { // Set an alarm call for the next block to nudge the track along. - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let next_block = now + One::one(); let call = match T::Preimages::bound(CallOf::::from(Call::one_fewer_deciding { track, diff --git a/substrate/frame/referenda/src/mock.rs b/substrate/frame/referenda/src/mock.rs index c96a50af8658..3f2c331ef861 100644 --- a/substrate/frame/referenda/src/mock.rs +++ b/substrate/frame/referenda/src/mock.rs @@ -206,6 +206,7 @@ impl Config for Test { type AlarmInterval = AlarmInterval; type Tracks = TestTracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } pub struct ExtBuilder {} From fd92baadb101f4589c907f29d08d8784db6ce4aa Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sat, 2 Nov 2024 12:38:13 +0100 Subject: [PATCH 2/2] Include BlockNumberProvider for all runtime configs PR doc --- .../collectives-westend/src/ambassador/mod.rs | 1 + .../collectives-westend/src/fellowship/mod.rs | 1 + .../runtime/rococo/src/governance/fellowship.rs | 1 + polkadot/runtime/rococo/src/governance/mod.rs | 1 + polkadot/runtime/westend/src/governance/mod.rs | 1 + prdoc/pr_6338.prdoc | 14 ++++++++++++++ substrate/bin/node/runtime/src/lib.rs | 2 ++ 7 files changed, 21 insertions(+) create mode 100644 prdoc/pr_6338.prdoc diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/ambassador/mod.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/ambassador/mod.rs index a052a9d3800c..8b0842697237 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/ambassador/mod.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/ambassador/mod.rs @@ -154,6 +154,7 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = AlarmInterval; type Tracks = tracks::TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } parameter_types! { diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/fellowship/mod.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/fellowship/mod.rs index 1e8212cf6ac2..e699f2eeaf43 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/fellowship/mod.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/fellowship/mod.rs @@ -106,6 +106,7 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = ConstU32<1>; type Tracks = tracks::TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = crate::System; } pub type FellowshipCollectiveInstance = pallet_ranked_collective::Instance1; diff --git a/polkadot/runtime/rococo/src/governance/fellowship.rs b/polkadot/runtime/rococo/src/governance/fellowship.rs index 27a58a0eebd1..231defab6aa5 100644 --- a/polkadot/runtime/rococo/src/governance/fellowship.rs +++ b/polkadot/runtime/rococo/src/governance/fellowship.rs @@ -308,6 +308,7 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = AlarmInterval; type Tracks = TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } pub type FellowshipCollectiveInstance = pallet_ranked_collective::Instance1; diff --git a/polkadot/runtime/rococo/src/governance/mod.rs b/polkadot/runtime/rococo/src/governance/mod.rs index ef2adf60753d..2be549be29ed 100644 --- a/polkadot/runtime/rococo/src/governance/mod.rs +++ b/polkadot/runtime/rococo/src/governance/mod.rs @@ -90,4 +90,5 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = AlarmInterval; type Tracks = TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } diff --git a/polkadot/runtime/westend/src/governance/mod.rs b/polkadot/runtime/westend/src/governance/mod.rs index d027f788d71f..abc25ebaa470 100644 --- a/polkadot/runtime/westend/src/governance/mod.rs +++ b/polkadot/runtime/westend/src/governance/mod.rs @@ -94,4 +94,5 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = AlarmInterval; type Tracks = TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } diff --git a/prdoc/pr_6338.prdoc b/prdoc/pr_6338.prdoc new file mode 100644 index 000000000000..7ea43f924d0d --- /dev/null +++ b/prdoc/pr_6338.prdoc @@ -0,0 +1,14 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Update Referenda to Support Block Number Provider + +doc: + - audience: Runtime Dev + description: | + This PR makes the referenda pallet uses the relay chain as a block provider for a parachain on a regular schedule. + To migrate existing referenda implementations, simply add `type BlockNumberProvider = System` to have the same behavior as before. + +crates: +- name: pallet-referenda + bump: minor \ No newline at end of file diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 7712d8ba9540..49fc360e399f 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -1005,6 +1005,7 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = AlarmInterval; type Tracks = TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } impl pallet_referenda::Config for Runtime { @@ -1025,6 +1026,7 @@ impl pallet_referenda::Config for Runtime { type AlarmInterval = AlarmInterval; type Tracks = TracksInfo; type Preimages = Preimage; + type BlockNumberProvider = System; } impl pallet_ranked_collective::Config for Runtime {