diff --git a/substrate/frame/referenda/src/lib.rs b/substrate/frame/referenda/src/lib.rs index e616056c3022..ad2dcb349e69 100644 --- a/substrate/frame/referenda/src/lib.rs +++ b/substrate/frame/referenda/src/lib.rs @@ -64,7 +64,7 @@ #![recursion_limit = "256"] #![cfg_attr(not(feature = "std"), no_std)] -use codec::{Codec, Encode}; +use codec::{Codec, Decode, Encode}; use frame_support::{ dispatch::DispatchResult, ensure, @@ -74,7 +74,7 @@ use frame_support::{ DispatchTime, }, Currency, LockIdentifier, OnUnbalanced, OriginTrait, PollStatus, Polling, QueryPreimage, - ReservableCurrency, StorePreimage, VoteTally, + Randomness, ReservableCurrency, StorePreimage, VoteTally, }, BoundedVec, }; @@ -84,7 +84,7 @@ use sp_runtime::{ traits::{AtLeast32BitUnsigned, Bounded, Dispatchable, One, Saturating, Zero}, DispatchError, Perbill, }; -use sp_std::{fmt::Debug, prelude::*}; +use sp_std::{fmt::Debug, ops::Bound::Included, prelude::*}; mod branch; pub mod migration; @@ -142,6 +142,7 @@ pub mod pallet { use super::*; use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg}; use frame_system::pallet_prelude::*; + use sp_std::collections::btree_map::BTreeMap; /// The in-code storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); @@ -174,6 +175,8 @@ pub mod pallet { PalletsOriginOf, Hasher = Self::Hashing, >; + /// Something that provides randomness in the runtime. + type Randomness: Randomness>; /// Currency type for this pallet. type Currency: ReservableCurrency; // Origins and unbalances. @@ -247,6 +250,17 @@ pub mod pallet { pub type ReferendumInfoFor, I: 'static = ()> = StorageMap<_, Blake2_128Concat, ReferendumIndex, ReferendumInfoOf>; + /// State change within confirm period. + #[pallet::storage] + #[pallet::unbounded] + pub type PassingStatusInConfirmPeriod, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + ReferendumIndex, + BTreeMap, bool>, + ValueQuery, + >; + /// The sorted list of referenda ready to be decided but not yet being decided, ordered by /// conviction-weighted approvals. /// @@ -726,6 +740,19 @@ pub mod pallet { } } +#[inline] +fn is_finalizing, I: 'static>(status: &ReferendumStatusOf) -> bool { + status + .clone() + .deciding + .map(|d| { + d.confirming + .map(|x| frame_system::Pallet::::block_number() >= x) + .unwrap_or(false) + }) + .unwrap_or(false) +} + impl, I: 'static> Polling for Pallet { type Index = ReferendumIndex; type Votes = VotesOf; @@ -742,6 +769,9 @@ impl, I: 'static> Polling for Pallet { ) -> R { match ReferendumInfoFor::::get(index) { Some(ReferendumInfo::Ongoing(mut status)) => { + if is_finalizing(&status) { + return f(PollStatus::None); + } let result = f(PollStatus::Ongoing(&mut status.tally, status.track)); let now = frame_system::Pallet::::block_number(); Self::ensure_alarm_at(&mut status, index, now + One::one()); @@ -762,6 +792,9 @@ impl, I: 'static> Polling for Pallet { ) -> Result { match ReferendumInfoFor::::get(index) { Some(ReferendumInfo::Ongoing(mut status)) => { + if is_finalizing(&status) { + return f(PollStatus::None); + } let result = f(PollStatus::Ongoing(&mut status.tally, status.track))?; let now = frame_system::Pallet::::block_number(); Self::ensure_alarm_at(&mut status, index, now + One::one()); @@ -1052,11 +1085,23 @@ impl, I: 'static> Pallet { } } + fn insert_or_update_passing_status_on_confirm( + index: ReferendumIndex, + when: BlockNumberFor, + state: bool, + ) { + PassingStatusInConfirmPeriod::::mutate(index, |confirm_status| { + confirm_status.insert(when, state); + }); + } + /// Advance the state of a referendum, which comes down to: /// - If it's ready to be decided, start deciding; /// - If it's not ready to be decided and non-deciding timeout has passed, fail; - /// - If it's ongoing and passing, ensure confirming; if at end of confirmation period, pass. - /// - If it's ongoing and not passing, stop confirming; if it has reached end time, fail. + /// - If it's ongoing and passing, ensure confirming; if at end of confirmation period, decide + /// by "candle". + /// - If it's ongoing and not passing, stop confirming; if it has reached end time, decide by + /// "candle". /// /// Weight will be a bit different depending on what it does, but it's designed so as not to /// differ dramatically, especially if `MaxQueue` is kept small. In particular _there are no @@ -1156,29 +1201,35 @@ impl, I: 'static> Pallet { &track.min_approval, status.track, ); + branch = if is_passing { match deciding.confirming { Some(t) if now >= t => { - // Passed! - Self::ensure_no_alarm(&mut status); - Self::note_one_fewer_deciding(status.track); - let (desired, call) = (status.enactment, status.proposal); - Self::schedule_enactment(index, track, desired, status.origin, call); - Self::deposit_event(Event::::Confirmed { + // Ongoing is now Finished. From now on, it'll be impossible to modify the + // poll. The passing status will be now decided by the auction, and the + // information stored under `PassingStatusInConfirmPeriod`. + + // Sent to Auction only after confirm period finishes. Whether it will pass + // or not depends on the status history when confirming, and a random point + // in time within confirm period. + + Self::insert_or_update_passing_status_on_confirm( + index, + now.saturating_less_one(), + is_passing, + ); + return Self::confirm_by_candle(now, index, status); + }, + Some(_) => { + // We don't care if failing within confirm period. + // Report the change of state, and continue. + Self::insert_or_update_passing_status_on_confirm( index, - tally: status.tally, - }); - return ( - ReferendumInfo::Approved( - now, - Some(status.submission_deposit), - status.decision_deposit, - ), - true, - ServiceBranch::Approved, - ) + now.saturating_less_one(), + is_passing, + ); + ServiceBranch::ContinueConfirming }, - Some(_) => ServiceBranch::ContinueConfirming, None => { // Start confirming dirty = true; @@ -1188,29 +1239,62 @@ impl, I: 'static> Pallet { }, } } else { - if now >= deciding.since.saturating_add(track.decision_period) { - // Failed! - Self::ensure_no_alarm(&mut status); - Self::note_one_fewer_deciding(status.track); - Self::deposit_event(Event::::Rejected { index, tally: status.tally }); - return ( - ReferendumInfo::Rejected( - now, - Some(status.submission_deposit), - status.decision_deposit, - ), - true, - ServiceBranch::Rejected, - ) - } - if deciding.confirming.is_some() { - // Stop confirming - dirty = true; - deciding.confirming = None; - Self::deposit_event(Event::::ConfirmAborted { index }); - ServiceBranch::EndConfirming - } else { - ServiceBranch::ContinueNotConfirming + match deciding.confirming { + Some(_) if now <= deciding.since.saturating_add(track.decision_period) => { + // Stop confirming + dirty = true; + deciding.confirming = None; + Self::deposit_event(Event::::ConfirmAborted { index }); + ServiceBranch::EndConfirming + }, + Some(t) if now >= t => { + // Ongoing is now Finished. From now on, it'll be impossible to modify the + // poll. The passing status will be now decided by the auction, and the + // information stored under `PassingStatusInConfirmPeriod`. + + // Sent to Auction only after confirm period finishes. Whether it will pass + // or not depends on the status history when confirming, and a random point + // in time within confirm period. + Self::insert_or_update_passing_status_on_confirm( + index, + now.saturating_less_one(), + is_passing, + ); + return Self::confirm_by_candle(now, index, status); + }, + Some(_) => { + // We don't care if failing within confirm period. + // Report the change of state, and continue. + Self::insert_or_update_passing_status_on_confirm( + index, + now.saturating_less_one(), + is_passing, + ); + ServiceBranch::ContinueConfirming + }, + None => { + if now >= deciding.since.saturating_add(track.decision_period) { + // Exceeded decision period without having passed in first place. + // Failed! + Self::ensure_no_alarm(&mut status); + Self::note_one_fewer_deciding(status.track); + Self::deposit_event(Event::::Rejected { + index, + tally: status.tally, + }); + return ( + ReferendumInfo::Rejected( + now, + Some(status.submission_deposit), + status.decision_deposit, + ), + true, + ServiceBranch::Rejected, + ); + } else { + ServiceBranch::ContinueNotConfirming + } + }, } }; alarm = Self::decision_time(deciding, &status.tally, status.track, track); @@ -1225,6 +1309,94 @@ impl, I: 'static> Pallet { (ReferendumInfo::Ongoing(status), dirty_alarm || dirty, branch) } + /// Find "candle" moment within the confirm period to decide whether the referendum is + /// confirmed or not. + /// + /// The "candle": passing or failing of a referendum is ultimately decided as a candle auction + /// where, given a random point in time (defined as `t`), the definitive status of the the + /// referendum is decided by the last status registered before `t`. + fn confirm_by_candle( + now: BlockNumberFor, + index: ReferendumIndex, + mut status: ReferendumStatusOf, + ) -> (ReferendumInfoOf, bool, ServiceBranch) { + // Note: it'd be weird to come up to this point and yet not have a track + let track = match Self::track(status.clone().track) { + Some(x) => x, + None => return (ReferendumInfo::Ongoing(status), false, ServiceBranch::Fail), + }; + + let confirming_until = status + .clone() + .deciding + .expect("called after having deciding, we have deciding.since time; qed") + .confirming + .expect("called after finished confirming, we have a confirming_until time; qed"); + + let confirming_since = confirming_until.saturating_sub(track.confirm_period); + + let (raw_offset, known_since) = T::Randomness::random(&b"confirm_auction"[..]); + + // Do not use random until made sure random seed is not known before confirm ends + if known_since <= confirming_until { + Self::ensure_alarm_at(&mut status, index, now.saturating_plus_one()); + return (ReferendumInfo::Ongoing(status), false, ServiceBranch::ContinueConfirming); + } else { + Self::ensure_no_alarm(&mut status); + } + + let raw_offset_block_number = >::decode(&mut raw_offset.as_ref()) + .expect("secure hashes should always be bigger than the block number; qed"); + + let candle_block_number = if confirming_since <= raw_offset_block_number + && raw_offset_block_number < confirming_until + { + raw_offset_block_number + } else { + confirming_since.saturating_add(raw_offset_block_number % track.confirm_period) + }; + + let statuses = PassingStatusInConfirmPeriod::::get(index); + let statuses = + statuses.range((Included(&confirming_since), Included(&candle_block_number))); + let (_, is_confirmed) = statuses.last().unwrap_or((&now, &true)); + + // Cleanup passing status + PassingStatusInConfirmPeriod::::remove(index); + + if *is_confirmed { + // Passed! + Self::ensure_no_alarm(&mut status); + Self::note_one_fewer_deciding(status.track); + let (desired, call) = (status.enactment, status.proposal); + Self::schedule_enactment(index, track, desired, status.origin, call); + Self::deposit_event(Event::::Confirmed { index, tally: status.tally }); + ( + ReferendumInfo::Approved( + now, + Some(status.submission_deposit), + status.decision_deposit, + ), + true, + ServiceBranch::Approved, + ) + } else { + // Failed! + Self::ensure_no_alarm(&mut status); + Self::note_one_fewer_deciding(status.track); + Self::deposit_event(Event::::Rejected { index, tally: status.tally }); + return ( + ReferendumInfo::Rejected( + now, + Some(status.submission_deposit), + status.decision_deposit, + ), + true, + ServiceBranch::Rejected, + ); + } + } + /// Determine the point at which a referendum will be accepted, move into confirmation with the /// given `tally` or end with rejection (whichever happens sooner). fn decision_time( diff --git a/substrate/frame/referenda/src/mock.rs b/substrate/frame/referenda/src/mock.rs index 135476d7cb13..35068fb61a2b 100644 --- a/substrate/frame/referenda/src/mock.rs +++ b/substrate/frame/referenda/src/mock.rs @@ -21,7 +21,7 @@ use super::*; use crate as pallet_referenda; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ - assert_ok, derive_impl, ord_parameter_types, parameter_types, + assert_ok, derive_impl, ord_parameter_types, parameter_types, storage_alias, traits::{ ConstU32, ConstU64, Contains, EqualPrivilegeOnly, OnInitialize, OriginTrait, Polling, SortedMembers, @@ -29,6 +29,7 @@ use frame_support::{ weights::Weight, }; use frame_system::{EnsureRoot, EnsureSignedBy}; +use sp_core::H256; use sp_runtime::{ traits::{BlakeTwo256, Hash}, BuildStorage, DispatchResult, Perbill, @@ -118,12 +119,44 @@ impl SortedMembers for OneToFive { fn add(_m: &u64) {} } +#[storage_alias] +pub type TestRandomnessBlockNumberOffset, I: 'static> = + StorageValue, BlockNumberFor>; + +pub struct TestRandomness; + +impl TestRandomness { + pub fn place_candle() { + TestRandomnessBlockNumberOffset::::set(Some(System::block_number())); + } + + fn hash_256_like_for(n: BlockNumberFor) -> [u8; 32] { + let mut encoded = n.encode(); + while encoded.len() < 32 { + encoded.push(0); + } + + encoded.try_into().expect("added bytes until 32") + } +} + +impl frame_support::traits::Randomness> for TestRandomness { + fn random(_: &[u8]) -> (H256, BlockNumberFor) { + let block_number = TestRandomnessBlockNumberOffset::::get() + .unwrap_or(System::block_number().saturating_less_one()); + + let block_hash: H256 = Self::hash_256_like_for(block_number).into(); + + (block_hash, System::block_number()) + } +} + pub struct TestTracksInfo; impl TracksInfo for TestTracksInfo { type Id = u8; type RuntimeOrigin = ::PalletsOrigin; fn tracks() -> &'static [(Self::Id, TrackInfo)] { - static DATA: [(u8, TrackInfo); 2] = [ + static DATA: [(u8, TrackInfo); 3] = [ ( 0u8, TrackInfo { @@ -168,6 +201,28 @@ impl TracksInfo for TestTracksInfo { }, }, ), + ( + 2u8, + TrackInfo { + name: "signed by 100", + max_deciding: 1, + decision_deposit: 1, + prepare_period: 1, + decision_period: 14, + confirm_period: 7, + min_enactment_period: 2, + min_approval: Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(50), + ceil: Perbill::from_percent(100), + }, + min_support: Curve::LinearDecreasing { + length: Perbill::from_percent(50), + floor: Perbill::from_percent(0), + ceil: Perbill::from_percent(100), + }, + }, + ), ]; &DATA[..] } @@ -176,6 +231,7 @@ impl TracksInfo for TestTracksInfo { match system_origin { frame_system::RawOrigin::Root => Ok(0), frame_system::RawOrigin::None => Ok(1), + frame_system::RawOrigin::Signed(100) => Ok(2), _ => Err(()), } } else { @@ -190,6 +246,7 @@ impl Config for Test { type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; type Scheduler = Scheduler; + type Randomness = TestRandomness; type Currency = pallet_balances::Pallet; type SubmitOrigin = frame_system::EnsureSigned; type CancelOrigin = EnsureSignedBy; @@ -215,7 +272,7 @@ impl Default for ExtBuilder { impl ExtBuilder { pub fn build(self) -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); - let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100), (6, 100)]; + let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100), (6, 100), (100, 100)]; pallet_balances::GenesisConfig:: { balances } .assimilate_storage(&mut t) .unwrap(); @@ -293,6 +350,11 @@ pub fn set_balance_proposal_bounded(value: u64) -> BoundedCallOf { ::bound(c).unwrap() } +pub fn transfer_balance_proposal_bounded(value: u64) -> BoundedCallOf { + let c = RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive { dest: 42, value }); + ::bound(c).unwrap() +} + #[allow(dead_code)] pub fn propose_set_balance(who: u64, value: u64, delay: u64) -> DispatchResult { Referenda::submit( @@ -422,13 +484,47 @@ pub enum RefState { } impl RefState { + /// Create default referendum: + /// + /// - **Track**: _Root_ (`0`) + /// - **Call**: `Balances::force_set_balance(42, 1)` pub fn create(self) -> ReferendumIndex { + Self::create_with_params( + self, + frame_support::dispatch::RawOrigin::Root.into(), + set_balance_proposal_bounded(1), + ) + } + + /// Create referendum with long duration: + /// + /// - **Track**: _Signed by 100_ (`2`) + /// - **Call**: `Balances::transfer_keep_alive(42, 10)` + pub fn create_long(self) -> ReferendumIndex { + Self::create_with_params( + self, + frame_support::dispatch::RawOrigin::Signed(100).into(), + transfer_balance_proposal_bounded(10), + ) + } + + pub fn create_with_params( + self, + origin: PalletsOriginOf, + call: BoundedCallOf, + ) -> ReferendumIndex { assert_ok!(Referenda::submit( RuntimeOrigin::signed(1), - Box::new(frame_support::dispatch::RawOrigin::Root.into()), - set_balance_proposal_bounded(1), + Box::new(origin.clone()), + call, DispatchTime::At(10), )); + + let track_id = TestTracksInfo::track_for(&origin) + .expect("track should exist as the ref was successfully submitted; qed"); + let tracks = TestTracksInfo::tracks(); + let (_, track) = &tracks[track_id as usize]; + assert_ok!(Referenda::place_decision_deposit(RuntimeOrigin::signed(2), 0)); if matches!(self, RefState::Confirming { immediate: true }) { set_tally(0, 100, 0); @@ -442,7 +538,7 @@ impl RefState { run_to(System::block_number() + 1); } if matches!(self, RefState::Confirming { .. }) { - assert_eq!(confirming_until(index), System::block_number() + 2); + assert_eq!(confirming_until(index), System::block_number() + track.confirm_period); } if matches!(self, RefState::Passing) { set_tally(0, 100, 99); diff --git a/substrate/frame/referenda/src/tests.rs b/substrate/frame/referenda/src/tests.rs index 8f51136de0bf..bb9ffe4a0dc9 100644 --- a/substrate/frame/referenda/src/tests.rs +++ b/substrate/frame/referenda/src/tests.rs @@ -30,7 +30,7 @@ fn params_should_work() { ExtBuilder::default().build_and_execute(|| { assert_eq!(ReferendumCount::::get(), 0); assert_eq!(Balances::free_balance(42), 0); - assert_eq!(Balances::total_issuance(), 600); + assert_eq!(Balances::total_issuance(), 700); }); } @@ -57,15 +57,15 @@ fn basic_happy_path_works() { set_tally(0, 100, 0); run_to(7); assert_eq!(confirming_until(0), 9); - run_to(9); - // #8: Should be confirmed & ended. - assert_eq!(approved_since(0), 9); + run_to(10); + // #10: Should confirm via auction. + assert_eq!(approved_since(0), 10); assert_ok!(Referenda::refund_decision_deposit(RuntimeOrigin::signed(2), 0)); - run_to(12); - // #9: Should not yet be enacted. - assert_eq!(Balances::free_balance(&42), 0); run_to(13); - // #10: Proposal should be executed. + // #13: Should not yet be enacted. + assert_eq!(Balances::free_balance(&42), 0); + run_to(14); + // #14: Proposal should be executed. assert_eq!(Balances::free_balance(&42), 1); }); } @@ -89,8 +89,8 @@ fn confirm_then_reconfirm_with_elapsed_trigger_works() { set_tally(r, 100, 99); run_to(8); assert_eq!(deciding_and_failing_since(r), 5); - run_to(11); - assert_eq!(approved_since(r), 11); + run_to(12); + assert_eq!(approved_since(r), 12); }); } @@ -103,8 +103,8 @@ fn instaconfirm_then_reconfirm_with_elapsed_trigger_works() { set_tally(r, 100, 99); run_to(7); assert_eq!(deciding_and_failing_since(r), 5); - run_to(11); - assert_eq!(approved_since(r), 11); + run_to(12); + assert_eq!(approved_since(r), 12); }); } @@ -121,8 +121,8 @@ fn instaconfirm_then_reconfirm_with_voting_trigger_works() { set_tally(r, 100, 0); run_to(9); assert_eq!(confirming_until(r), 11); - run_to(11); - assert_eq!(approved_since(r), 11); + run_to(12); + assert_eq!(approved_since(r), 12); }); } @@ -132,21 +132,22 @@ fn voting_should_extend_for_late_confirmation() { let r = Passing.create(); run_to(10); assert_eq!(confirming_until(r), 11); - run_to(11); - assert_eq!(approved_since(r), 11); + run_to(12); + assert_eq!(approved_since(r), 12); }); } #[test] -fn should_instafail_during_extension_confirmation() { +fn should_fails_during_extension_confirmation_if_on_candle() { ExtBuilder::default().build_and_execute(|| { let r = Passing.create(); run_to(10); assert_eq!(confirming_until(r), 11); - // Should insta-fail since it's now past the normal voting time. + TestRandomness::place_candle(); + // Should record failure. I'll fail anyways set_tally(r, 100, 101); - run_to(11); - assert_eq!(rejected_since(r), 11); + run_to(12); + assert_eq!(rejected_since(r), 12); }); } @@ -165,6 +166,41 @@ fn confirming_then_fail_works() { }); } +// TODO: Add a new test to decide using candle method +// Note: use a deterministic (i.e. manually set the seed) +// method to be able to accurately determine which is +// the "winner" block +#[test] +fn confirming_decided_by_candle() { + ExtBuilder::default().build_and_execute(|| { + let r = Passing.create_long(); + // Normally ends at 5 + 4 (voting period) = 9. + assert_eq!(deciding_and_failing_since(r), 2); + run_to(16); + assert_eq!(confirming_until(r), 23); + run_to(19); + TestRandomness::place_candle(); + run_to(20); + set_tally(r, 100, 101); + run_to(24); + assert_eq!(approved_since(r), 24); + }); + + ExtBuilder::default().build_and_execute(|| { + let r = Passing.create_long(); + // Normally ends at 5 + 4 (voting period) = 9. + assert_eq!(deciding_and_failing_since(r), 2); + run_to(16); + assert_eq!(confirming_until(r), 23); + run_to(20); + set_tally(r, 100, 101); + run_to(21); + TestRandomness::place_candle(); + run_to(24); + assert_eq!(rejected_since(r), 24); + }); +} + #[test] fn queueing_works() { ExtBuilder::default().build_and_execute(|| { @@ -231,35 +267,34 @@ fn queueing_works() { println!("{:?}", Vec::<_>::from(TrackQueue::::get(0))); // Let confirmation period end. - run_to(9); + run_to(10); // #4 should have been confirmed. - assert_eq!(approved_since(4), 9); + assert_eq!(approved_since(4), 10); // On to the next block to select the new referendum - run_to(10); + run_to(11); // #1 (the one with the most approvals) should now be being decided. - assert_eq!(deciding_since(1), 10); + assert_eq!(deciding_since(1), 11); // Let it end unsuccessfully. - run_to(14); - assert_eq!(rejected_since(1), 14); + run_to(15); + assert_eq!(rejected_since(1), 15); // Service queue. - run_to(15); + run_to(16); // #2 should now be being decided. It will (barely) pass. - assert_eq!(deciding_and_failing_since(2), 15); + assert_eq!(deciding_and_failing_since(2), 16); // #2 moves into confirming at the last moment with a 50% approval. - run_to(19); - assert_eq!(confirming_until(2), 21); + run_to(20); + assert_eq!(confirming_until(2), 22); // #2 gets approved. - run_to(21); - assert_eq!(approved_since(2), 21); + run_to(23); + assert_eq!(approved_since(2), 23); // The final one has since timed out. - run_to(22); assert_eq!(timed_out_since(3), 22); }); }