Skip to content

Commit e9ae86f

Browse files
talentdev219R.RajeshkumarsimonssoAlex Sedighi
committed
Remove without_storage_info for the allocations pallet V2 (#634)
* remove without_storage_info Signed-off-by: R.Rajeshkumar <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Fredrik Simonsson <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Fredrik Simonsson <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Fredrik Simonsson <[email protected]> * Fix to CI/CD build try-runtime error Signed-off-by: R.Rajeshkumar <[email protected]> * Fix to CI/CD build try-runtime error (#637) * chore: prepare for releasing 2.0.21 (#638) * remove without_storage_info Signed-off-by: R.Rajeshkumar <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Fredrik Simonsson <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Fredrik Simonsson <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Fredrik Simonsson <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Alex Sedighi <[email protected]> * Update pallets/allocations/src/lib.rs Co-authored-by: Alex Sedighi <[email protected]> * intake review suggestions Signed-off-by: R.Rajeshkumar <[email protected]> Co-authored-by: R.Rajeshkumar <[email protected]> Co-authored-by: Fredrik Simonsson <[email protected]> Co-authored-by: Alex Sedighi <[email protected]>
1 parent 3356c9f commit e9ae86f

File tree

8 files changed

+307
-89
lines changed

8 files changed

+307
-89
lines changed

Diff for: Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: node/src/chain_spec.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*
2-
* This file is part of the Nodle Chain distributed at https://github.com/NodleCode/chain
3-
* Copyright (C) 2020-2022 Nodle International
4-
*
5-
* This program is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU General Public License as published by
7-
* the Free Software Foundation, either version 3 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* This program is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License
16-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
*/
2+
* This file is part of the Nodle Chain distributed at https://github.com/NodleCode/chain
3+
* Copyright (C) 2020-2022 Nodle International
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
1818

1919
// clippy complains about ChainSpecGroup which we cannot modify
2020
#![allow(clippy::derive_partial_eq_without_eq)]

Diff for: pallets/allocations/Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ std = [
1313
"frame-support/std",
1414
"frame-system/std",
1515
"pallet-balances/std",
16+
"pallet-membership/std",
1617
"sp-io/std",
1718
"sp-runtime/std",
1819
"sp-std/std",
@@ -22,9 +23,11 @@ runtime-benchmarks = [
2223
"frame-system/runtime-benchmarks",
2324
"frame-support/runtime-benchmarks",
2425
]
26+
try-runtime = ["frame-support/try-runtime"]
2527

2628
[dependencies]
27-
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive",] }
29+
log = { version = "0.4.14", default-features = false }
30+
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
2831
serde = { version = "1.0.136", optional = true, features = ["derive"] }
2932
scale-info = { version = "2.0.1", default-features = false, features = [ "derive",] }
3033
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.20" }
@@ -37,4 +40,6 @@ sp-std = { git = "https://github.com/paritytech/substrate", default-features = f
3740
support = { path = "../../support" }
3841

3942
[dev-dependencies]
40-
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.20" }
43+
sp-tracing = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.20" }
44+
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.20" }
45+
pallet-membership = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.20" }

Diff for: pallets/allocations/src/benchmarking.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
//! Amendments pallet benchmarks
2222
2323
use super::*;
24+
use crate::BalanceOf;
25+
#[cfg(test)]
2426
use crate::Pallet as Allocations;
2527
use frame_benchmarking::{account, benchmarks};
26-
use frame_support::BoundedVec;
28+
use frame_support::{traits::ConstU32, BoundedVec};
2729
use frame_system::RawOrigin;
2830
use sp_std::prelude::*;
2931

32+
pub type MaxMembers = ConstU32<10>;
33+
3034
const SEED: u32 = 0;
3135

3236
pub struct BenchmarkConfig<T: Config> {
@@ -35,13 +39,16 @@ pub struct BenchmarkConfig<T: Config> {
3539
}
3640

3741
fn make_benchmark_config<T: Config>() -> BenchmarkConfig<T> {
38-
let grantee = account("grantee", 0, SEED);
39-
let oracle = account("oracle", 0, SEED);
42+
let grantee: T::AccountId = account("grantee", 0, SEED);
43+
let oracle: T::AccountId = account("oracle", 0, SEED);
44+
45+
let mut members = <BenchmarkOracles<T>>::get();
46+
assert!(members.try_push(oracle.clone()).is_ok());
47+
<BenchmarkOracles<T>>::put(&members);
4048

4149
BenchmarkConfig { grantee, oracle }
4250
}
4351

44-
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
4552
fn make_batch<T: Config>(b: u32) -> BoundedVec<(T::AccountId, BalanceOf<T>), T::MaxAllocs> {
4653
let mut ret = BoundedVec::with_bounded_capacity(b as usize);
4754

@@ -55,8 +62,6 @@ fn make_batch<T: Config>(b: u32) -> BoundedVec<(T::AccountId, BalanceOf<T>), T::
5562
benchmarks! {
5663
allocate {
5764
let config = make_benchmark_config::<T>();
58-
59-
Pallet::<T>::initialize_members(&[config.oracle.clone()]);
6065
}: _(RawOrigin::Signed(config.oracle.clone()), config.grantee.clone(), T::ExistentialDeposit::get() * 10u32.into(), vec![])
6166

6267
batch {
@@ -65,7 +70,6 @@ benchmarks! {
6570
let config = make_benchmark_config::<T>();
6671
let batch_arg = make_batch::<T>(b);
6772

68-
Pallet::<T>::initialize_members(&[config.oracle.clone()]);
6973
}: _(RawOrigin::Signed(config.oracle.clone()), batch_arg)
7074

7175
impl_benchmark_test_suite!(

Diff for: pallets/allocations/src/lib.rs

+58-30
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,32 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818
#![cfg_attr(not(feature = "std"), no_std)]
19+
1920
#[cfg(feature = "runtime-benchmarks")]
2021
mod benchmarking;
22+
2123
#[cfg(test)]
2224
mod tests;
2325

26+
mod migrations;
27+
28+
use codec::{Decode, Encode};
2429
use frame_support::{
25-
dispatch::Weight,
2630
ensure,
27-
migration::remove_storage_prefix,
28-
traits::{tokens::ExistenceRequirement, ChangeMembers, Currency, Get, InitializeMembers},
29-
transactional, PalletId,
31+
pallet_prelude::MaxEncodedLen,
32+
traits::{tokens::ExistenceRequirement, Contains, Currency, Get},
33+
transactional, BoundedVec, PalletId,
3034
};
31-
use frame_system::ensure_signed;
32-
use sp_std::prelude::*;
33-
use support::WithAccountId;
3435

36+
use frame_system::ensure_signed;
37+
use scale_info::TypeInfo;
38+
use sp_runtime::traits::AccountIdConversion;
3539
use sp_runtime::{
36-
traits::{AccountIdConversion, CheckedAdd, Saturating, Zero},
37-
DispatchResult, Perbill,
40+
traits::{CheckedAdd, Saturating, Zero},
41+
DispatchResult, Perbill, RuntimeDebug,
3842
};
43+
use sp_std::prelude::*;
44+
use support::WithAccountId;
3945

4046
pub mod weights;
4147
pub use weights::WeightInfo;
@@ -44,10 +50,26 @@ pub use pallet::*;
4450

4551
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
4652

53+
// A value placed in storage that represents the current version of the Allocations storage.
54+
// This value is used by the `on_runtime_upgrade` logic to determine whether we run storage
55+
// migration logic. This should match directly with the semantic versions of the Rust crate.
56+
#[derive(Encode, Decode, MaxEncodedLen, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
57+
enum Releases {
58+
V0_0_0Legacy, // To handle Legacy version
59+
V2_0_21,
60+
}
61+
62+
impl Default for Releases {
63+
fn default() -> Self {
64+
Releases::V0_0_0Legacy
65+
}
66+
}
67+
4768
#[frame_support::pallet]
4869
pub mod pallet {
4970
use super::*;
5071
use frame_support::pallet_prelude::*;
72+
use frame_support::traits::OnRuntimeUpgrade;
5173
use frame_system::pallet_prelude::*;
5274

5375
#[pallet::config]
@@ -71,20 +93,30 @@ pub mod pallet {
7193
#[pallet::constant]
7294
type MaxAllocs: Get<u32>;
7395

96+
type OracleMembers: Contains<Self::AccountId>;
97+
7498
/// Weight information for extrinsics in this pallet.
7599
type WeightInfo: WeightInfo;
76100
}
77101

78102
#[pallet::pallet]
79103
#[pallet::generate_store(pub(super) trait Store)]
80-
#[pallet::without_storage_info]
81104
pub struct Pallet<T>(PhantomData<T>);
82105

83106
#[pallet::hooks]
84107
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
85-
fn on_runtime_upgrade() -> Weight {
86-
remove_storage_prefix(<Pallet<T>>::name().as_bytes(), b"CoinsConsumed", b"");
87-
T::DbWeight::get().writes(1)
108+
#[cfg(feature = "try-runtime")]
109+
fn pre_upgrade() -> Result<(), &'static str> {
110+
migrations::v1::MigrateToBoundedOracles::<T>::pre_upgrade()
111+
}
112+
113+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
114+
migrations::v1::MigrateToBoundedOracles::<T>::on_runtime_upgrade()
115+
}
116+
117+
#[cfg(feature = "try-runtime")]
118+
fn post_upgrade() -> Result<(), &'static str> {
119+
migrations::v1::MigrateToBoundedOracles::<T>::post_upgrade()
88120
}
89121
}
90122

@@ -152,8 +184,8 @@ pub mod pallet {
152184
/// Can only be called by an oracle, trigger a token mint and dispatch to
153185
/// `amount`, minus protocol fees
154186
#[pallet::weight(
155-
<T as pallet::Config>::WeightInfo::allocate()
156-
)]
187+
<T as pallet::Config>::WeightInfo::allocate()
188+
)]
157189
// we add the `transactional` modifier here in the event that one of the
158190
// transfers fail. the code itself should already prevent this but we add
159191
// this as an additional guarantee.
@@ -185,13 +217,21 @@ pub mod pallet {
185217
}
186218

187219
#[pallet::storage]
188-
#[pallet::getter(fn oracles)]
189-
pub type Oracles<T: Config> = StorageValue<_, Vec<T::AccountId>, ValueQuery>;
220+
pub(crate) type StorageVersion<T: Config> = StorageValue<_, Releases, ValueQuery>;
221+
222+
#[cfg(feature = "runtime-benchmarks")]
223+
#[pallet::storage]
224+
#[pallet::getter(fn benchmark_oracles)]
225+
pub type BenchmarkOracles<T: Config> =
226+
StorageValue<_, BoundedVec<T::AccountId, benchmarking::MaxMembers>, ValueQuery>;
190227
}
191228

192229
impl<T: Config> Pallet<T> {
193230
pub fn is_oracle(who: T::AccountId) -> bool {
194-
Self::oracles().contains(&who)
231+
#[cfg(feature = "runtime-benchmarks")]
232+
return T::OracleMembers::contains(&who) || Self::benchmark_oracles().contains(&who);
233+
#[cfg(not(feature = "runtime-benchmarks"))]
234+
return T::OracleMembers::contains(&who);
195235
}
196236

197237
fn ensure_oracle(origin: T::Origin) -> DispatchResult {
@@ -200,15 +240,3 @@ impl<T: Config> Pallet<T> {
200240
Ok(())
201241
}
202242
}
203-
204-
impl<T: Config> ChangeMembers<T::AccountId> for Pallet<T> {
205-
fn change_members_sorted(_incoming: &[T::AccountId], _outgoing: &[T::AccountId], new: &[T::AccountId]) {
206-
<Oracles<T>>::put(new);
207-
}
208-
}
209-
210-
impl<T: Config> InitializeMembers<T::AccountId> for Pallet<T> {
211-
fn initialize_members(init: &[T::AccountId]) {
212-
<Oracles<T>>::put(init);
213-
}
214-
}

0 commit comments

Comments
 (0)