FRAME: Revamp Preimage pallet to use Consideration (#1363)

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>
This commit is contained in:
Gavin Wood
2023-09-17 12:19:18 +01:00
committed by GitHub
parent 11d1a3955e
commit d787269cdf
25 changed files with 789 additions and 348 deletions
+9
View File
@@ -138,6 +138,15 @@ impl From<Pays> for PostDispatchInfo {
}
}
impl From<bool> for Pays {
fn from(b: bool) -> Self {
match b {
true => Self::Yes,
false => Self::No,
}
}
}
/// A generalized group of dispatch types.
///
/// NOTE whenever upgrading the enum make sure to also update
+2 -2
View File
@@ -90,8 +90,8 @@ pub use hooks::{
pub mod schedule;
mod storage;
pub use storage::{
Consideration, Footprint, Incrementable, Instance, PartialStorageInfoTrait, StorageInfo,
StorageInfoTrait, StorageInstance, TrackedStorageKey, WhitelistedStorageKeys,
Consideration, Footprint, Incrementable, Instance, LinearStoragePrice, PartialStorageInfoTrait,
StorageInfo, StorageInfoTrait, StorageInstance, TrackedStorageKey, WhitelistedStorageKeys,
};
mod dispatch;
+39 -1
View File
@@ -18,11 +18,13 @@
//! Traits for encoding data related to pallet's storage items.
use codec::{Encode, FullCodec, MaxEncodedLen};
use core::marker::PhantomData;
use impl_trait_for_tuples::impl_for_tuples;
use scale_info::TypeInfo;
pub use sp_core::storage::TrackedStorageKey;
use sp_core::Get;
use sp_runtime::{
traits::{Member, Saturating},
traits::{Convert, Member, Saturating},
DispatchError, RuntimeDebug,
};
use sp_std::{collections::btree_set::BTreeSet, prelude::*};
@@ -152,6 +154,20 @@ impl Footprint {
}
}
/// A storage price that increases linearly with the number of elements and their size.
pub struct LinearStoragePrice<Base, Slope, Balance>(PhantomData<(Base, Slope, Balance)>);
impl<Base, Slope, Balance> Convert<Footprint, Balance> for LinearStoragePrice<Base, Slope, Balance>
where
Base: Get<Balance>,
Slope: Get<Balance>,
Balance: From<u64> + sp_runtime::Saturating,
{
fn convert(a: Footprint) -> Balance {
let s: Balance = (a.count.saturating_mul(a.size)).into();
s.saturating_mul(Slope::get()).saturating_add(Base::get())
}
}
/// Some sort of cost taken from account temporarily in order to offset the cost to the chain of
/// holding some data [`Footprint`] in state.
///
@@ -239,3 +255,25 @@ where
}
impl_incrementable!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
#[cfg(test)]
mod tests {
use super::*;
use sp_core::ConstU64;
#[test]
fn linear_storage_price_works() {
type Linear = LinearStoragePrice<ConstU64<7>, ConstU64<3>, u64>;
let p = |count, size| Linear::convert(Footprint { count, size });
assert_eq!(p(0, 0), 7);
assert_eq!(p(0, 1), 7);
assert_eq!(p(1, 0), 7);
assert_eq!(p(1, 1), 10);
assert_eq!(p(8, 1), 31);
assert_eq!(p(1, 8), 31);
assert_eq!(p(u64::MAX, u64::MAX), u64::MAX);
}
}