Skip to content

Commit

Permalink
FRAME: Revamp Preimage pallet to use Consideration (#1363)
Browse files Browse the repository at this point in the history
Make Preimage pallet use Consideration instead of handling deposits
directly.

Other half of paritytech/substrate#13666.
Depends/based on #1361.

Script for the lazy migration that should be run manually once:
[migrate-preimage-lazy.py](https://github.com/ggwpez/substrate-scripts/blob/master/migrate-preimage-lazy.py).

## TODO

- [x] Migration code.

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: command-bot <>
Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 17, 2023
1 parent 11d1a39 commit d787269
Show file tree
Hide file tree
Showing 25 changed files with 787 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ use frame_support::{
construct_runtime,
dispatch::DispatchClass,
parameter_types,
traits::{ConstBool, ConstU16, ConstU32, ConstU64, ConstU8, EitherOfDiverse, InstanceFilter},
traits::{
fungible::HoldConsideration, ConstBool, ConstU16, ConstU32, ConstU64, ConstU8,
EitherOfDiverse, InstanceFilter, LinearStoragePrice,
},
weights::{ConstantMultiplier, Weight},
PalletId,
};
Expand Down Expand Up @@ -209,7 +212,7 @@ impl pallet_balances::Config for Runtime {
type ReserveIdentifier = [u8; 8];
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
}

Expand Down Expand Up @@ -545,15 +548,20 @@ impl pallet_scheduler::Config for Runtime {
parameter_types! {
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}

impl pallet_preimage::Config for Runtime {
type WeightInfo = weights::pallet_preimage::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type BaseDeposit = PreimageBaseDeposit;
type ByteDeposit = PreimageByteDeposit;
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
}

// Create the runtime by composing the FRAME pallets that were previously configured.
Expand Down Expand Up @@ -589,7 +597,7 @@ construct_runtime!(
Utility: pallet_utility::{Pallet, Call, Event} = 40,
Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 41,
Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 42,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 43,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>, HoldReason} = 43,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 44,

// The main stage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_preimage`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_preimage::WeightInfo for WeightInfo<T> {
fn ensure_updated(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `193 + n * (91 ±0)`
// Estimated: `3593 + n * (2566 ±0)`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 3593)
// Standard Error: 13_720
.saturating_add(Weight::from_parts(17_309_199, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes(1_u64))
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(n.into())))
.saturating_add(Weight::from_parts(0, 2566).saturating_mul(n.into()))
}

/// Storage: `Preimage::StatusFor` (r:1 w:1)
/// Proof: `Preimage::StatusFor` (`max_values`: None, `max_size`: Some(91), added: 2566, mode: `MaxEncodedLen`)
/// Storage: `Preimage::PreimageFor` (r:0 w:1)
Expand Down
16 changes: 11 additions & 5 deletions polkadot/runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ use frame_election_provider_support::{
use frame_support::{
construct_runtime, parameter_types,
traits::{
ConstU32, Contains, EitherOf, EitherOfDiverse, InstanceFilter, KeyOwnerProofSystem,
PrivilegeCmp, ProcessMessage, ProcessMessageError, StorageMapShim, WithdrawReasons,
fungible::HoldConsideration, ConstU32, Contains, EitherOf, EitherOfDiverse, InstanceFilter,
KeyOwnerProofSystem, LinearStoragePrice, PrivilegeCmp, ProcessMessage, ProcessMessageError,
StorageMapShim, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter},
PalletId,
Expand Down Expand Up @@ -239,15 +240,20 @@ impl pallet_scheduler::Config for Runtime {
parameter_types! {
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}

impl pallet_preimage::Config for Runtime {
type WeightInfo = weights::pallet_preimage::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type BaseDeposit = PreimageBaseDeposit;
type ByteDeposit = PreimageByteDeposit;
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
}

parameter_types! {
Expand Down Expand Up @@ -1560,7 +1566,7 @@ construct_runtime! {
Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 31,

// Preimage registrar.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 32,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>, HoldReason} = 32,

// Bounties modules.
Bounties: pallet_bounties::{Pallet, Call, Storage, Event<T>} = 35,
Expand Down
15 changes: 15 additions & 0 deletions polkadot/runtime/kusama/src/weights/pallet_preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_preimage`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_preimage::WeightInfo for WeightInfo<T> {
fn ensure_updated(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `193 + n * (91 ±0)`
// Estimated: `3593 + n * (2566 ±0)`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 3593)
// Standard Error: 13_720
.saturating_add(Weight::from_parts(17_309_199, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes(1_u64))
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(n.into())))
.saturating_add(Weight::from_parts(0, 2566).saturating_mul(n.into()))
}

/// Storage: Preimage StatusFor (r:1 w:1)
/// Proof: Preimage StatusFor (max_values: None, max_size: Some(91), added: 2566, mode: MaxEncodedLen)
/// Storage: Preimage PreimageFor (r:0 w:1)
Expand Down
19 changes: 12 additions & 7 deletions polkadot/runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ use frame_election_provider_support::{
use frame_support::{
construct_runtime, parameter_types,
traits::{
ConstU32, Contains, EitherOf, EitherOfDiverse, InstanceFilter, KeyOwnerProofSystem,
PrivilegeCmp, ProcessMessage, ProcessMessageError, WithdrawReasons,
fungible::HoldConsideration, ConstU32, Contains, EitherOf, EitherOfDiverse, InstanceFilter,
KeyOwnerProofSystem, LinearStoragePrice, PrivilegeCmp, ProcessMessage, ProcessMessageError,
WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter},
PalletId,
Expand Down Expand Up @@ -221,18 +222,22 @@ impl pallet_scheduler::Config for Runtime {
}

parameter_types! {
pub const PreimageMaxSize: u32 = 4096 * 1024;
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}

impl pallet_preimage::Config for Runtime {
type WeightInfo = weights::pallet_preimage::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type BaseDeposit = PreimageBaseDeposit;
type ByteDeposit = PreimageByteDeposit;
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
}

parameter_types! {
Expand Down Expand Up @@ -297,7 +302,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = weights::pallet_balances::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
}

Expand Down Expand Up @@ -1326,7 +1331,7 @@ construct_runtime! {
// Basic stuff; balances is uncallable initially.
System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>} = 0,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 1,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 10,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>, HoldReason} = 10,

// Babe must be before session.
Babe: pallet_babe::{Pallet, Call, Storage, Config<T>, ValidateUnsigned} = 2,
Expand Down
15 changes: 15 additions & 0 deletions polkadot/runtime/polkadot/src/weights/pallet_preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_preimage`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_preimage::WeightInfo for WeightInfo<T> {
fn ensure_updated(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `193 + n * (91 ±0)`
// Estimated: `3593 + n * (2566 ±0)`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 3593)
// Standard Error: 13_720
.saturating_add(Weight::from_parts(17_309_199, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes(1_u64))
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(n.into())))
.saturating_add(Weight::from_parts(0, 2566).saturating_mul(n.into()))
}

/// Storage: Preimage StatusFor (r:1 w:1)
/// Proof: Preimage StatusFor (max_values: None, max_size: Some(91), added: 2566, mode: MaxEncodedLen)
/// Storage: Preimage PreimageFor (r:0 w:1)
Expand Down
16 changes: 11 additions & 5 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ use beefy_primitives::{
use frame_support::{
construct_runtime, parameter_types,
traits::{
Contains, EitherOfDiverse, InstanceFilter, KeyOwnerProofSystem, LockIdentifier,
PrivilegeCmp, ProcessMessage, ProcessMessageError, StorageMapShim, WithdrawReasons,
fungible::HoldConsideration, Contains, EitherOfDiverse, InstanceFilter,
KeyOwnerProofSystem, LinearStoragePrice, LockIdentifier, PrivilegeCmp, ProcessMessage,
ProcessMessageError, StorageMapShim, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter},
PalletId,
Expand Down Expand Up @@ -224,15 +225,20 @@ impl pallet_scheduler::Config for Runtime {
parameter_types! {
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}

impl pallet_preimage::Config for Runtime {
type WeightInfo = weights::pallet_preimage::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type BaseDeposit = PreimageBaseDeposit;
type ByteDeposit = PreimageByteDeposit;
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
}

parameter_types! {
Expand Down Expand Up @@ -1449,7 +1455,7 @@ construct_runtime! {
Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 31,

// Preimage registrar.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 32,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>, HoldReason} = 32,

// Bounties modules.
Bounties: pallet_bounties::{Pallet, Call, Storage, Event<T>} = 35,
Expand Down
15 changes: 15 additions & 0 deletions polkadot/runtime/rococo/src/weights/pallet_preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_preimage`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_preimage::WeightInfo for WeightInfo<T> {
fn ensure_updated(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `193 + n * (91 ±0)`
// Estimated: `3593 + n * (2566 ±0)`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 3593)
// Standard Error: 13_720
.saturating_add(Weight::from_parts(17_309_199, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes(1_u64))
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(n.into())))
.saturating_add(Weight::from_parts(0, 2566).saturating_mul(n.into()))
}

/// Storage: Preimage StatusFor (r:1 w:1)
/// Proof: Preimage StatusFor (max_values: None, max_size: Some(91), added: 2566, mode: MaxEncodedLen)
/// Storage: Preimage PreimageFor (r:0 w:1)
Expand Down
18 changes: 11 additions & 7 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use frame_election_provider_support::{bounds::ElectionBoundsBuilder, onchain, Se
use frame_support::{
construct_runtime, parameter_types,
traits::{
ConstU32, InstanceFilter, KeyOwnerProofSystem, ProcessMessage, ProcessMessageError,
WithdrawReasons,
fungible::HoldConsideration, ConstU32, InstanceFilter, KeyOwnerProofSystem,
LinearStoragePrice, ProcessMessage, ProcessMessageError, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter},
PalletId,
Expand Down Expand Up @@ -193,18 +193,22 @@ impl pallet_scheduler::Config for Runtime {
}

parameter_types! {
pub const PreimageMaxSize: u32 = 4096 * 1024;
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}

impl pallet_preimage::Config for Runtime {
type WeightInfo = weights::pallet_preimage::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type BaseDeposit = PreimageBaseDeposit;
type ByteDeposit = PreimageByteDeposit;
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
}

parameter_types! {
Expand Down Expand Up @@ -268,7 +272,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = weights::pallet_balances::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
type FreezeIdentifier = ();
type MaxHolds = ConstU32<0>;
type MaxHolds = ConstU32<1>;
type MaxFreezes = ConstU32<0>;
}

Expand Down Expand Up @@ -1311,7 +1315,7 @@ construct_runtime! {
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 20,

// Preimage registrar.
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 28,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>, HoldReason} = 28,

// Sudo.
Sudo: pallet_sudo::{Pallet, Call, Storage, Event<T>, Config<T>} = 21,
Expand Down
15 changes: 15 additions & 0 deletions polkadot/runtime/westend/src/weights/pallet_preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_preimage`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_preimage::WeightInfo for WeightInfo<T> {
fn ensure_updated(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `193 + n * (91 ±0)`
// Estimated: `3593 + n * (2566 ±0)`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 3593)
// Standard Error: 13_720
.saturating_add(Weight::from_parts(17_309_199, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(n.into())))
.saturating_add(T::DbWeight::get().writes(1_u64))
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(n.into())))
.saturating_add(Weight::from_parts(0, 2566).saturating_mul(n.into()))
}

/// Storage: Preimage StatusFor (r:1 w:1)
/// Proof: Preimage StatusFor (max_values: None, max_size: Some(91), added: 2566, mode: MaxEncodedLen)
/// Storage: Preimage PreimageFor (r:0 w:1)
Expand Down
Loading

0 comments on commit d787269

Please sign in to comment.