Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 200088e

Browse files
muharemgavofyork
andauthored
Refund referendum submission deposit (#12788)
* optinal submission deposit and migration * refund submission deposit call, test, bench * try runtime fixes * assert for bench * Only refund cancelled/approved referenda deposits * update storage version Co-authored-by: Gav <[email protected]>
1 parent 0d153c9 commit 200088e

File tree

8 files changed

+593
-241
lines changed

8 files changed

+593
-241
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frame/referenda/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ frame-system = { version = "4.0.0-dev", default-features = false, path = "../sys
2626
sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
2727
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
2828
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }
29+
log = { version = "0.4.17", default-features = false }
2930

3031
[dev-dependencies]
3132
assert_matches = { version = "1.5" }

frame/referenda/src/benchmarking.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ benchmarks_instance_pallet! {
264264
assert_matches!(ReferendumInfoFor::<T, I>::get(index), Some(ReferendumInfo::Cancelled(_, _, None)));
265265
}
266266

267+
refund_submission_deposit {
268+
let (origin, index) = create_referendum::<T, I>();
269+
let caller = frame_system::ensure_signed(origin.clone()).unwrap();
270+
let balance = T::Currency::free_balance(&caller);
271+
assert_ok!(Referenda::<T, I>::cancel(T::CancelOrigin::successful_origin(), index));
272+
assert_matches!(ReferendumInfoFor::<T, I>::get(index), Some(ReferendumInfo::Cancelled(_, Some(_), _)));
273+
}: _<T::RuntimeOrigin>(origin, index)
274+
verify {
275+
assert_matches!(ReferendumInfoFor::<T, I>::get(index), Some(ReferendumInfo::Cancelled(_, None, _)));
276+
let new_balance = T::Currency::free_balance(&caller);
277+
assert!(new_balance > balance);
278+
}
279+
267280
cancel {
268281
let (_origin, index) = create_referendum::<T, I>();
269282
place_deposit::<T, I>(index);

frame/referenda/src/lib.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use sp_runtime::{
8585
use sp_std::{fmt::Debug, prelude::*};
8686

8787
mod branch;
88+
pub mod migration;
8889
mod types;
8990
pub mod weights;
9091

@@ -140,8 +141,12 @@ pub mod pallet {
140141
use frame_support::pallet_prelude::*;
141142
use frame_system::pallet_prelude::*;
142143

144+
/// The current storage version.
145+
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
146+
143147
#[pallet::pallet]
144148
#[pallet::generate_store(pub(super) trait Store)]
149+
#[pallet::storage_version(STORAGE_VERSION)]
145150
pub struct Pallet<T, I = ()>(_);
146151

147152
#[pallet::config]
@@ -342,6 +347,15 @@ pub mod pallet {
342347
/// The final tally of votes in this referendum.
343348
tally: T::Tally,
344349
},
350+
/// The submission deposit has been refunded.
351+
SubmissionDepositRefunded {
352+
/// Index of the referendum.
353+
index: ReferendumIndex,
354+
/// The account who placed the deposit.
355+
who: T::AccountId,
356+
/// The amount placed by the account.
357+
amount: BalanceOf<T, I>,
358+
},
345359
}
346360

347361
#[pallet::error]
@@ -368,6 +382,8 @@ pub mod pallet {
368382
NoPermission,
369383
/// The deposit cannot be refunded since none was made.
370384
NoDeposit,
385+
/// The referendum status is invalid for this operation.
386+
BadStatus,
371387
}
372388

373389
#[pallet::call]
@@ -495,7 +511,7 @@ pub mod pallet {
495511
Self::deposit_event(Event::<T, I>::Cancelled { index, tally: status.tally });
496512
let info = ReferendumInfo::Cancelled(
497513
frame_system::Pallet::<T>::block_number(),
498-
status.submission_deposit,
514+
Some(status.submission_deposit),
499515
status.decision_deposit,
500516
);
501517
ReferendumInfoFor::<T, I>::insert(index, info);
@@ -579,6 +595,36 @@ pub mod pallet {
579595
};
580596
Ok(Some(branch.weight::<T, I>()).into())
581597
}
598+
599+
/// Refund the Submission Deposit for a closed referendum back to the depositor.
600+
///
601+
/// - `origin`: must be `Signed` or `Root`.
602+
/// - `index`: The index of a closed referendum whose Submission Deposit has not yet been
603+
/// refunded.
604+
///
605+
/// Emits `SubmissionDepositRefunded`.
606+
#[pallet::weight(T::WeightInfo::refund_submission_deposit())]
607+
pub fn refund_submission_deposit(
608+
origin: OriginFor<T>,
609+
index: ReferendumIndex,
610+
) -> DispatchResult {
611+
ensure_signed_or_root(origin)?;
612+
let mut info =
613+
ReferendumInfoFor::<T, I>::get(index).ok_or(Error::<T, I>::BadReferendum)?;
614+
let deposit = info
615+
.take_submission_deposit()
616+
.map_err(|_| Error::<T, I>::BadStatus)?
617+
.ok_or(Error::<T, I>::NoDeposit)?;
618+
Self::refund_deposit(Some(deposit.clone()));
619+
ReferendumInfoFor::<T, I>::insert(index, info);
620+
let e = Event::<T, I>::SubmissionDepositRefunded {
621+
index,
622+
who: deposit.who,
623+
amount: deposit.amount,
624+
};
625+
Self::deposit_event(e);
626+
Ok(())
627+
}
582628
}
583629
}
584630

@@ -671,9 +717,9 @@ impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {
671717
Self::note_one_fewer_deciding(status.track);
672718
let now = frame_system::Pallet::<T>::block_number();
673719
let info = if approved {
674-
ReferendumInfo::Approved(now, status.submission_deposit, status.decision_deposit)
720+
ReferendumInfo::Approved(now, Some(status.submission_deposit), status.decision_deposit)
675721
} else {
676-
ReferendumInfo::Rejected(now, status.submission_deposit, status.decision_deposit)
722+
ReferendumInfo::Rejected(now, Some(status.submission_deposit), status.decision_deposit)
677723
};
678724
ReferendumInfoFor::<T, I>::insert(index, info);
679725
Ok(())
@@ -995,7 +1041,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
9951041
return (
9961042
ReferendumInfo::TimedOut(
9971043
now,
998-
status.submission_deposit,
1044+
Some(status.submission_deposit),
9991045
status.decision_deposit,
10001046
),
10011047
true,
@@ -1027,7 +1073,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
10271073
return (
10281074
ReferendumInfo::Approved(
10291075
now,
1030-
status.submission_deposit,
1076+
Some(status.submission_deposit),
10311077
status.decision_deposit,
10321078
),
10331079
true,
@@ -1052,7 +1098,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
10521098
return (
10531099
ReferendumInfo::Rejected(
10541100
now,
1055-
status.submission_deposit,
1101+
Some(status.submission_deposit),
10561102
status.decision_deposit,
10571103
),
10581104
true,

frame/referenda/src/migration.rs

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! Storage migrations for the referenda pallet.
19+
20+
use super::*;
21+
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
22+
use frame_support::{pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade};
23+
use log;
24+
25+
/// Initial version of storage types.
26+
pub mod v0 {
27+
use super::*;
28+
// ReferendumStatus and its dependency types referenced from the latest version while staying
29+
// unchanged. [`super::test::referendum_status_v0()`] checks its immutability between v0 and
30+
// latest version.
31+
#[cfg(test)]
32+
pub(super) use super::{ReferendumStatus, ReferendumStatusOf};
33+
34+
pub type ReferendumInfoOf<T, I> = ReferendumInfo<
35+
TrackIdOf<T, I>,
36+
PalletsOriginOf<T>,
37+
<T as frame_system::Config>::BlockNumber,
38+
BoundedCallOf<T, I>,
39+
BalanceOf<T, I>,
40+
TallyOf<T, I>,
41+
<T as frame_system::Config>::AccountId,
42+
ScheduleAddressOf<T, I>,
43+
>;
44+
45+
/// Info regarding a referendum, present or past.
46+
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
47+
pub enum ReferendumInfo<
48+
TrackId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
49+
RuntimeOrigin: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
50+
Moment: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone + EncodeLike,
51+
Call: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
52+
Balance: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
53+
Tally: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
54+
AccountId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
55+
ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
56+
> {
57+
/// Referendum has been submitted and is being voted on.
58+
Ongoing(
59+
ReferendumStatus<
60+
TrackId,
61+
RuntimeOrigin,
62+
Moment,
63+
Call,
64+
Balance,
65+
Tally,
66+
AccountId,
67+
ScheduleAddress,
68+
>,
69+
),
70+
/// Referendum finished with approval. Submission deposit is held.
71+
Approved(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
72+
/// Referendum finished with rejection. Submission deposit is held.
73+
Rejected(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
74+
/// Referendum finished with cancellation. Submission deposit is held.
75+
Cancelled(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
76+
/// Referendum finished and was never decided. Submission deposit is held.
77+
TimedOut(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
78+
/// Referendum finished with a kill.
79+
Killed(Moment),
80+
}
81+
82+
#[storage_alias]
83+
pub type ReferendumInfoFor<T: Config<I>, I: 'static> =
84+
StorageMap<Pallet<T, I>, Blake2_128Concat, ReferendumIndex, ReferendumInfoOf<T, I>>;
85+
}
86+
87+
pub mod v1 {
88+
use super::*;
89+
90+
/// The log target.
91+
const TARGET: &'static str = "runtime::democracy::migration::v1";
92+
93+
/// Transforms a submission deposit of ReferendumInfo(Approved|Rejected|Cancelled|TimedOut) to
94+
/// optional value, making it refundable.
95+
pub struct MigrateV0ToV1<T, I = ()>(PhantomData<(T, I)>);
96+
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for MigrateV0ToV1<T, I> {
97+
#[cfg(feature = "try-runtime")]
98+
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
99+
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
100+
assert_eq!(onchain_version, 0, "migration from version 0 to 1.");
101+
let referendum_count = v0::ReferendumInfoFor::<T, I>::iter().count();
102+
log::info!(
103+
target: TARGET,
104+
"pre-upgrade state contains '{}' referendums.",
105+
referendum_count
106+
);
107+
Ok((referendum_count as u32).encode())
108+
}
109+
110+
fn on_runtime_upgrade() -> Weight {
111+
let current_version = Pallet::<T, I>::current_storage_version();
112+
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
113+
let mut weight = T::DbWeight::get().reads(1);
114+
log::info!(
115+
target: TARGET,
116+
"running migration with current storage version {:?} / onchain {:?}.",
117+
current_version,
118+
onchain_version
119+
);
120+
if onchain_version != 0 {
121+
log::warn!(target: TARGET, "skipping migration from v0 to v1.");
122+
return weight
123+
}
124+
v0::ReferendumInfoFor::<T, I>::iter().for_each(|(key, value)| {
125+
let maybe_new_value = match value {
126+
v0::ReferendumInfo::Ongoing(_) | v0::ReferendumInfo::Killed(_) => None,
127+
v0::ReferendumInfo::Approved(e, s, d) =>
128+
Some(ReferendumInfo::Approved(e, Some(s), d)),
129+
v0::ReferendumInfo::Rejected(e, s, d) =>
130+
Some(ReferendumInfo::Rejected(e, Some(s), d)),
131+
v0::ReferendumInfo::Cancelled(e, s, d) =>
132+
Some(ReferendumInfo::Cancelled(e, Some(s), d)),
133+
v0::ReferendumInfo::TimedOut(e, s, d) =>
134+
Some(ReferendumInfo::TimedOut(e, Some(s), d)),
135+
};
136+
if let Some(new_value) = maybe_new_value {
137+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
138+
log::info!(target: TARGET, "migrating referendum #{:?}", &key);
139+
ReferendumInfoFor::<T, I>::insert(key, new_value);
140+
} else {
141+
weight.saturating_accrue(T::DbWeight::get().reads(1));
142+
}
143+
});
144+
StorageVersion::new(1).put::<Pallet<T, I>>();
145+
weight.saturating_accrue(T::DbWeight::get().writes(1));
146+
weight
147+
}
148+
149+
#[cfg(feature = "try-runtime")]
150+
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
151+
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
152+
assert_eq!(onchain_version, 1, "must upgrade from version 0 to 1.");
153+
let pre_referendum_count: u32 = Decode::decode(&mut &state[..])
154+
.expect("failed to decode the state from pre-upgrade.");
155+
let post_referendum_count = ReferendumInfoFor::<T, I>::iter().count() as u32;
156+
assert_eq!(
157+
post_referendum_count, pre_referendum_count,
158+
"must migrate all referendums."
159+
);
160+
log::info!(target: TARGET, "migrated all referendums.");
161+
Ok(())
162+
}
163+
}
164+
}
165+
166+
#[cfg(test)]
167+
pub mod test {
168+
use super::*;
169+
use crate::mock::{Test as T, *};
170+
use core::str::FromStr;
171+
172+
// create referendum status v0.
173+
fn create_status_v0() -> v0::ReferendumStatusOf<T, ()> {
174+
let origin: OriginCaller = frame_system::RawOrigin::Root.into();
175+
let track = <T as Config<()>>::Tracks::track_for(&origin).unwrap();
176+
v0::ReferendumStatusOf::<T, ()> {
177+
track,
178+
in_queue: true,
179+
origin,
180+
proposal: set_balance_proposal_bounded(1),
181+
enactment: DispatchTime::At(1),
182+
tally: TallyOf::<T, ()>::new(track),
183+
submission_deposit: Deposit { who: 1, amount: 10 },
184+
submitted: 1,
185+
decision_deposit: None,
186+
alarm: None,
187+
deciding: None,
188+
}
189+
}
190+
191+
#[test]
192+
pub fn referendum_status_v0() {
193+
// make sure the bytes of the encoded referendum v0 is decodable.
194+
let ongoing_encoded = sp_core::Bytes::from_str("0x00000000013001012a000000000000000400000100000000000000010000000000000001000000000000000a00000000000000000000000000000000000100").unwrap();
195+
let ongoing_dec = v0::ReferendumInfoOf::<T, ()>::decode(&mut &*ongoing_encoded).unwrap();
196+
let ongoing = v0::ReferendumInfoOf::<T, ()>::Ongoing(create_status_v0());
197+
assert_eq!(ongoing, ongoing_dec);
198+
}
199+
200+
#[test]
201+
fn migration_v0_to_v1_works() {
202+
new_test_ext().execute_with(|| {
203+
// create and insert into the storage an ongoing referendum v0.
204+
let status_v0 = create_status_v0();
205+
let ongoing_v0 = v0::ReferendumInfoOf::<T, ()>::Ongoing(status_v0.clone());
206+
v0::ReferendumInfoFor::<T, ()>::insert(2, ongoing_v0);
207+
// create and insert into the storage an approved referendum v0.
208+
let approved_v0 = v0::ReferendumInfoOf::<T, ()>::Approved(
209+
123,
210+
Deposit { who: 1, amount: 10 },
211+
Some(Deposit { who: 2, amount: 20 }),
212+
);
213+
v0::ReferendumInfoFor::<T, ()>::insert(5, approved_v0);
214+
// run migration from v0 to v1.
215+
v1::MigrateV0ToV1::<T, ()>::on_runtime_upgrade();
216+
// fetch and assert migrated into v1 the ongoing referendum.
217+
let ongoing_v1 = ReferendumInfoFor::<T, ()>::get(2).unwrap();
218+
// referendum status schema is the same for v0 and v1.
219+
assert_eq!(ReferendumInfoOf::<T, ()>::Ongoing(status_v0), ongoing_v1);
220+
// fetch and assert migrated into v1 the approved referendum.
221+
let approved_v1 = ReferendumInfoFor::<T, ()>::get(5).unwrap();
222+
assert_eq!(
223+
approved_v1,
224+
ReferendumInfoOf::<T, ()>::Approved(
225+
123,
226+
Some(Deposit { who: 1, amount: 10 }),
227+
Some(Deposit { who: 2, amount: 20 })
228+
)
229+
);
230+
});
231+
}
232+
}

0 commit comments

Comments
 (0)