Preimage registrar and Scheduler integration (#10356)

* initial idea

* more

* fix compile

* add clear and request logic

* improve some docs

* Add and implement trait

* continuing to improve

* refcount type

* infallible system preimage upload

* fmt

* fix requests

* Make it simple

* Make it simple

* Formatting

* Initial draft

* request when scheduled

* Docs

* Scheduler good

* Scheduler good

* Scheduler tests working

* Add new files

* Missing stuff

* Repotting, add weights.

* Add some tests to preimage pallet

* More tests

* Fix benchmarks

* preimage benchmarks

* All preimage benchmarks

* Tidy cargo

* Update weights.rs

* Allow hash provision in benchmarks

* Initial work on new benchmarks for Scheduler

* Tests working, refactor looks good

* Tests for new Scheduler functionality

* Use real weight, make tests work with runtimes without Preimage

* Rename

* Update benchmarks

* Formatting

* Formatting

* Fix weird formatting

* Update frame/preimage/src/lib.rs

* Fix try-runtime build

* Fixes

* Fixes

* Update frame/support/src/traits/tokens/currency.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/support/src/traits/tokens/currency/reservable.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/support/src/traits/tokens/imbalance.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/preimage/src/mock.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/scheduler/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/preimage/src/lib.rs

* Fixes

* Fixes

* Formatting

* Fixes

* Fixes

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_scheduler --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/scheduler/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_preimage --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/preimage/src/weights.rs --template=./.maintain/frame-weight-template.hbs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
Gavin Wood
2021-12-11 15:55:23 +01:00
committed by GitHub
parent f6f58f95e1
commit 5e50e0bc2c
24 changed files with 3592 additions and 1287 deletions
@@ -0,0 +1,161 @@
// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Preimage pallet benchmarking.
use super::*;
use frame_benchmarking::{account, benchmarks, whitelist_account};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
use sp_std::{prelude::*, vec};
use crate::Pallet as Preimage;
const SEED: u32 = 0;
fn funded_account<T: Config>(name: &'static str, index: u32) -> T::AccountId {
let caller: T::AccountId = account(name, index, SEED);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
caller
}
fn preimage_and_hash<T: Config>() -> (Vec<u8>, T::Hash) {
sized_preimage_and_hash::<T>(T::MaxSize::get())
}
fn sized_preimage_and_hash<T: Config>(size: u32) -> (Vec<u8>, T::Hash) {
let mut preimage = vec![];
preimage.resize(size as usize, 0);
let hash = <T as frame_system::Config>::Hashing::hash(&preimage[..]);
(preimage, hash)
}
benchmarks! {
// Expensive note - will reserve.
note_preimage {
let s in 0 .. T::MaxSize::get();
let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);
let (preimage, hash) = sized_preimage_and_hash::<T>(s);
}: _(RawOrigin::Signed(caller), preimage)
verify {
assert!(Preimage::<T>::have_preimage(&hash));
}
// Cheap note - will not reserve since it was requested.
note_requested_preimage {
let s in 0 .. T::MaxSize::get();
let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);
let (preimage, hash) = sized_preimage_and_hash::<T>(s);
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
}: note_preimage(RawOrigin::Signed(caller), preimage)
verify {
assert!(Preimage::<T>::have_preimage(&hash));
}
// Cheap note - will not reserve since it's the manager.
note_no_deposit_preimage {
let s in 0 .. T::MaxSize::get();
let (preimage, hash) = sized_preimage_and_hash::<T>(s);
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
}: note_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), preimage)
verify {
assert!(Preimage::<T>::have_preimage(&hash));
}
// Expensive unnote - will unreserve.
unnote_preimage {
let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);
let (preimage, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::note_preimage(RawOrigin::Signed(caller.clone()).into(), preimage));
}: _(RawOrigin::Signed(caller), hash.clone())
verify {
assert!(!Preimage::<T>::have_preimage(&hash));
}
// Cheap unnote - will not unreserve since there's no deposit held.
unnote_no_deposit_preimage {
let (preimage, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::note_preimage(T::ManagerOrigin::successful_origin(), preimage));
}: unnote_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), hash.clone())
verify {
assert!(!Preimage::<T>::have_preimage(&hash));
}
// Expensive request - will unreserve the noter's deposit.
request_preimage {
let (preimage, hash) = preimage_and_hash::<T>();
let noter = funded_account::<T>("noter", 0);
whitelist_account!(noter);
assert_ok!(Preimage::<T>::note_preimage(RawOrigin::Signed(noter).into(), preimage));
}: _<T::Origin>(T::ManagerOrigin::successful_origin(), hash)
verify {
assert_eq!(StatusFor::<T>::get(&hash), Some(RequestStatus::Requested(1)));
}
// Cheap request - would unreserve the deposit but none was held.
request_no_deposit_preimage {
let (preimage, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::note_preimage(T::ManagerOrigin::successful_origin(), preimage));
}: request_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), hash)
verify {
assert_eq!(StatusFor::<T>::get(&hash), Some(RequestStatus::Requested(1)));
}
// Cheap request - the preimage is not yet noted, so deposit to unreserve.
request_unnoted_preimage {
let (_, hash) = preimage_and_hash::<T>();
}: request_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), hash)
verify {
assert_eq!(StatusFor::<T>::get(&hash), Some(RequestStatus::Requested(1)));
}
// Cheap request - the preimage is already requested, so just a counter bump.
request_requested_preimage {
let (_, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
}: request_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), hash)
verify {
assert_eq!(StatusFor::<T>::get(&hash), Some(RequestStatus::Requested(2)));
}
// Expensive unrequest - last reference and it's noted, so will destroy the preimage.
unrequest_preimage {
let (preimage, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
assert_ok!(Preimage::<T>::note_preimage(T::ManagerOrigin::successful_origin(), preimage));
}: _<T::Origin>(T::ManagerOrigin::successful_origin(), hash.clone())
verify {
assert_eq!(StatusFor::<T>::get(&hash), None);
}
// Cheap unrequest - last reference, but it's not noted.
unrequest_unnoted_preimage {
let (_, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
}: unrequest_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), hash.clone())
verify {
assert_eq!(StatusFor::<T>::get(&hash), None);
}
// Cheap unrequest - not the last reference.
unrequest_multi_referenced_preimage {
let (_, hash) = preimage_and_hash::<T>();
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
assert_ok!(Preimage::<T>::request_preimage(T::ManagerOrigin::successful_origin(), hash.clone()));
}: unrequest_preimage<T::Origin>(T::ManagerOrigin::successful_origin(), hash.clone())
verify {
assert_eq!(StatusFor::<T>::get(&hash), Some(RequestStatus::Requested(1)));
}
impl_benchmark_test_suite!(Preimage, crate::mock::new_test_ext(), crate::mock::Test);
}
+347
View File
@@ -0,0 +1,347 @@
// This file is part of Substrate.
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Preimage Pallet
//!
//! - [`Config`]
//! - [`Call`]
//!
//! ## Overview
//!
//! The Preimage pallet allows for the users and the runtime to store the preimage
//! of a hash on chain. This can be used by other pallets where storing and managing
//! large byte-blobs.
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
use sp_runtime::traits::{BadOrigin, Hash, Saturating};
use sp_std::{convert::TryFrom, prelude::*};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
ensure,
pallet_prelude::Get,
traits::{Currency, PreimageProvider, PreimageRecipient, ReservableCurrency},
weights::Pays,
BoundedVec,
};
use scale_info::TypeInfo;
use weights::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use pallet::*;
/// A type to note whether a preimage is owned by a user or the system.
#[derive(Clone, Eq, PartialEq, Encode, Decode, TypeInfo, MaxEncodedLen, RuntimeDebug)]
pub enum RequestStatus<AccountId, Balance> {
/// The associated preimage has not yet been requested by the system. The given deposit (if
/// some) is being held until either it becomes requested or the user retracts the primage.
Unrequested(Option<(AccountId, Balance)>),
/// There are a non-zero number of outstanding requests for this hash by this chain. If there
/// is a preimage registered, then it may be removed iff this counter becomes zero.
Requested(u32),
}
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The Weight information for this pallet.
type WeightInfo: weights::WeightInfo;
/// Currency type for this pallet.
type Currency: ReservableCurrency<Self::AccountId>;
/// An origin that can request a preimage be placed on-chain without a deposit or fee, or
/// manage existing preimages.
type ManagerOrigin: EnsureOrigin<Self::Origin>;
/// Max size allowed for a preimage.
type MaxSize: Get<u32>;
/// The base deposit for placing a preimage on chain.
type BaseDeposit: Get<BalanceOf<Self>>;
/// The per-byte deposit for placing a preimage on chain.
type ByteDeposit: Get<BalanceOf<Self>>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A preimage has been noted.
Noted { hash: T::Hash },
/// A preimage has been requested.
Requested { hash: T::Hash },
/// A preimage has ben cleared.
Cleared { hash: T::Hash },
}
#[pallet::error]
pub enum Error<T> {
/// Preimage is too large to store on-chain.
TooLarge,
/// Preimage has already been noted on-chain.
AlreadyNoted,
/// The user is not authorized to perform this action.
NotAuthorized,
/// The preimage cannot be removed since it has not yet been noted.
NotNoted,
/// A preimage may not be removed when there are outstanding requests.
Requested,
/// The preimage request cannot be removed since no outstanding requests exist.
NotRequested,
}
/// The request status of a given hash.
#[pallet::storage]
pub(super) type StatusFor<T: Config> =
StorageMap<_, Identity, T::Hash, RequestStatus<T::AccountId, BalanceOf<T>>>;
/// The preimages stored by this pallet.
#[pallet::storage]
pub(super) type PreimageFor<T: Config> =
StorageMap<_, Identity, T::Hash, BoundedVec<u8, T::MaxSize>>;
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Register a preimage on-chain.
///
/// If the preimage was previously requested, no fees or deposits are taken for providing
/// the preimage. Otherwise, a deposit is taken proportional to the size of the preimage.
#[pallet::weight(T::WeightInfo::note_preimage(bytes.len() as u32))]
pub fn note_preimage(origin: OriginFor<T>, bytes: Vec<u8>) -> DispatchResultWithPostInfo {
// We accept a signed origin which will pay a deposit, or a root origin where a deposit
// is not taken.
let maybe_sender = Self::ensure_signed_or_manager(origin)?;
let bounded_vec =
BoundedVec::<u8, T::MaxSize>::try_from(bytes).map_err(|()| Error::<T>::TooLarge)?;
let system_requested = Self::note_bytes(bounded_vec, maybe_sender.as_ref())?;
if system_requested || maybe_sender.is_none() {
Ok(Pays::No.into())
} else {
Ok(().into())
}
}
/// Clear an unrequested preimage from the runtime storage.
#[pallet::weight(T::WeightInfo::unnote_preimage())]
pub fn unnote_preimage(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
let maybe_sender = Self::ensure_signed_or_manager(origin)?;
Self::do_unnote_preimage(&hash, maybe_sender)
}
/// Request a preimage be uploaded to the chain without paying any fees or deposits.
///
/// If the preimage requests has already been provided on-chain, we unreserve any deposit
/// a user may have paid, and take the control of the preimage out of their hands.
#[pallet::weight(T::WeightInfo::request_preimage())]
pub fn request_preimage(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
T::ManagerOrigin::ensure_origin(origin)?;
Self::do_request_preimage(&hash);
Ok(())
}
/// Clear a previously made request for a preimage.
///
/// NOTE: THIS MUST NOT BE CALLED ON `hash` MORE TIMES THAN `request_preimage`.
#[pallet::weight(T::WeightInfo::unrequest_preimage())]
pub fn unrequest_preimage(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
T::ManagerOrigin::ensure_origin(origin)?;
Self::do_unrequest_preimage(&hash)
}
}
}
impl<T: Config> Pallet<T> {
/// Ensure that the origin is either the `ManagerOrigin` or a signed origin.
fn ensure_signed_or_manager(origin: T::Origin) -> Result<Option<T::AccountId>, BadOrigin> {
if T::ManagerOrigin::ensure_origin(origin.clone()).is_ok() {
return Ok(None)
}
let who = ensure_signed(origin)?;
Ok(Some(who))
}
/// Store some preimage on chain.
///
/// We verify that the preimage is within the bounds of what the pallet supports.
///
/// If the preimage was requested to be uploaded, then the user pays no deposits or tx fees.
fn note_bytes(
preimage: BoundedVec<u8, T::MaxSize>,
maybe_depositor: Option<&T::AccountId>,
) -> Result<bool, DispatchError> {
let hash = T::Hashing::hash(&preimage);
ensure!(!PreimageFor::<T>::contains_key(hash), Error::<T>::AlreadyNoted);
// We take a deposit only if there is a provided depositor, and the preimage was not
// previously requested. This also allows the tx to pay no fee.
let was_requested = match (StatusFor::<T>::get(hash), maybe_depositor) {
(Some(RequestStatus::Requested(..)), _) => true,
(Some(RequestStatus::Unrequested(..)), _) => Err(Error::<T>::AlreadyNoted)?,
(None, None) => {
StatusFor::<T>::insert(hash, RequestStatus::Unrequested(None));
false
},
(None, Some(depositor)) => {
let length = preimage.len() as u32;
let deposit = T::BaseDeposit::get()
.saturating_add(T::ByteDeposit::get().saturating_mul(length.into()));
T::Currency::reserve(depositor, deposit)?;
let status = RequestStatus::Unrequested(Some((depositor.clone(), deposit)));
StatusFor::<T>::insert(hash, status);
false
},
};
PreimageFor::<T>::insert(hash, preimage);
Self::deposit_event(Event::Noted { hash });
Ok(was_requested)
}
// This function will add a hash to the list of requested preimages.
//
// If the preimage already exists before the request is made, the deposit for the preimage is
// returned to the user, and removed from their management.
fn do_request_preimage(hash: &T::Hash) {
let count = StatusFor::<T>::get(hash).map_or(1, |x| match x {
RequestStatus::Requested(mut count) => {
count.saturating_inc();
count
},
RequestStatus::Unrequested(None) => 1,
RequestStatus::Unrequested(Some((owner, deposit))) => {
// Return the deposit - the preimage now has outstanding requests.
T::Currency::unreserve(&owner, deposit);
1
},
});
StatusFor::<T>::insert(hash, RequestStatus::Requested(count));
if count == 1 {
Self::deposit_event(Event::Requested { hash: hash.clone() });
}
}
// Clear a preimage from the storage of the chain, returning any deposit that may be reserved.
//
// If `maybe_owner` is provided, we verify that it is the correct owner before clearing the
// data.
fn do_unnote_preimage(
hash: &T::Hash,
maybe_check_owner: Option<T::AccountId>,
) -> DispatchResult {
match StatusFor::<T>::get(hash).ok_or(Error::<T>::NotNoted)? {
RequestStatus::Unrequested(Some((owner, deposit))) => {
ensure!(
maybe_check_owner.map_or(true, |c| &c == &owner),
Error::<T>::NotAuthorized
);
T::Currency::unreserve(&owner, deposit);
},
RequestStatus::Unrequested(None) => {
ensure!(maybe_check_owner.is_none(), Error::<T>::NotAuthorized);
},
RequestStatus::Requested(_) => Err(Error::<T>::Requested)?,
}
StatusFor::<T>::remove(hash);
PreimageFor::<T>::remove(hash);
Self::deposit_event(Event::Cleared { hash: hash.clone() });
Ok(())
}
/// Clear a preimage request.
fn do_unrequest_preimage(hash: &T::Hash) -> DispatchResult {
match StatusFor::<T>::get(hash).ok_or(Error::<T>::NotRequested)? {
RequestStatus::Requested(mut count) if count > 1 => {
count.saturating_dec();
StatusFor::<T>::insert(hash, RequestStatus::Requested(count));
},
RequestStatus::Requested(count) => {
debug_assert!(count == 1, "preimage request counter at zero?");
PreimageFor::<T>::remove(hash);
StatusFor::<T>::remove(hash);
Self::deposit_event(Event::Cleared { hash: hash.clone() });
},
RequestStatus::Unrequested(_) => Err(Error::<T>::NotRequested)?,
}
Ok(())
}
}
impl<T: Config> PreimageProvider<T::Hash> for Pallet<T> {
fn have_preimage(hash: &T::Hash) -> bool {
PreimageFor::<T>::contains_key(hash)
}
fn preimage_requested(hash: &T::Hash) -> bool {
matches!(StatusFor::<T>::get(hash), Some(RequestStatus::Requested(..)))
}
fn get_preimage(hash: &T::Hash) -> Option<Vec<u8>> {
PreimageFor::<T>::get(hash).map(|preimage| preimage.to_vec())
}
fn request_preimage(hash: &T::Hash) {
Self::do_request_preimage(hash)
}
fn unrequest_preimage(hash: &T::Hash) {
let res = Self::do_unrequest_preimage(hash);
debug_assert!(res.is_ok(), "do_unrequest_preimage failed - counter underflow?");
}
}
impl<T: Config> PreimageRecipient<T::Hash> for Pallet<T> {
type MaxSize = T::MaxSize;
fn note_preimage(bytes: BoundedVec<u8, Self::MaxSize>) {
// We don't really care if this fails, since that's only the case if someone else has
// already noted it.
let _ = Self::note_bytes(bytes, None);
}
fn unnote_preimage(hash: &T::Hash) {
// Should never fail if authorization check is skipped.
let res = Self::do_unnote_preimage(hash, None);
debug_assert!(res.is_ok(), "unnote_preimage failed - request outstanding?");
}
}
+131
View File
@@ -0,0 +1,131 @@
// This file is part of Substrate.
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Scheduler test environment.
use super::*;
use crate as pallet_preimage;
use frame_support::{
ord_parameter_types, parameter_types, traits::Everything, weights::constants::RocksDbWeight,
};
use frame_system::EnsureSignedBy;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
Perbill,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system,
Balances: pallet_balances,
Preimage: pallet_preimage,
}
);
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(2_000_000_000_000);
}
impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = RocksDbWeight;
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
parameter_types! {
pub const ExistentialDeposit: u64 = 5;
pub const MaxReserves: u32 = 50;
}
impl pallet_balances::Config for Test {
type Balance = u64;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type MaxLocks = ();
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
}
parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * BlockWeights::get().max_block;
pub const MaxScheduledPerBlock: u32 = 10;
pub const MaxSize: u32 = 1024;
pub const BaseDeposit: u64 = 2;
pub const ByteDeposit: u64 = 1;
}
ord_parameter_types! {
pub const One: u64 = 1;
}
impl Config for Test {
type WeightInfo = ();
type Event = Event;
type Currency = Balances;
type ManagerOrigin = EnsureSignedBy<One, u64>;
type MaxSize = MaxSize;
type BaseDeposit = BaseDeposit;
type ByteDeposit = ByteDeposit;
}
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
let balances = pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100)],
};
balances.assimilate_storage(&mut t).unwrap();
t.into()
}
pub fn hashed(data: impl AsRef<[u8]>) -> H256 {
BlakeTwo256::hash(data.as_ref())
}
+233
View File
@@ -0,0 +1,233 @@
// This file is part of Substrate.
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Scheduler tests.
use super::*;
use crate::mock::*;
use frame_support::{assert_noop, assert_ok};
use pallet_balances::Error as BalancesError;
#[test]
fn user_note_preimage_works() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_eq!(Balances::reserved_balance(2), 3);
assert_eq!(Balances::free_balance(2), 97);
let h = hashed([1]);
assert!(Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
assert_noop!(
Preimage::note_preimage(Origin::signed(2), vec![1]),
Error::<Test>::AlreadyNoted
);
assert_noop!(
Preimage::note_preimage(Origin::signed(0), vec![2]),
BalancesError::<Test>::InsufficientBalance
);
});
}
#[test]
fn manager_note_preimage_works() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::free_balance(1), 100);
let h = hashed([1]);
assert!(Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
assert_noop!(
Preimage::note_preimage(Origin::signed(1), vec![1]),
Error::<Test>::AlreadyNoted
);
});
}
#[test]
fn user_unnote_preimage_works() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_noop!(
Preimage::unnote_preimage(Origin::signed(3), hashed([1])),
Error::<Test>::NotAuthorized
);
assert_noop!(
Preimage::unnote_preimage(Origin::signed(2), hashed([2])),
Error::<Test>::NotNoted
);
assert_ok!(Preimage::unnote_preimage(Origin::signed(2), hashed([1])));
assert_noop!(
Preimage::unnote_preimage(Origin::signed(2), hashed([1])),
Error::<Test>::NotNoted
);
let h = hashed([1]);
assert!(!Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), None);
});
}
#[test]
fn manager_unnote_preimage_works() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
assert_ok!(Preimage::unnote_preimage(Origin::signed(1), hashed([1])));
assert_noop!(
Preimage::unnote_preimage(Origin::signed(1), hashed([1])),
Error::<Test>::NotNoted
);
let h = hashed([1]);
assert!(!Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), None);
});
}
#[test]
fn manager_unnote_user_preimage_works() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_noop!(
Preimage::unnote_preimage(Origin::signed(3), hashed([1])),
Error::<Test>::NotAuthorized
);
assert_noop!(
Preimage::unnote_preimage(Origin::signed(2), hashed([2])),
Error::<Test>::NotNoted
);
assert_ok!(Preimage::unnote_preimage(Origin::signed(1), hashed([1])));
let h = hashed([1]);
assert!(!Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), None);
});
}
#[test]
fn requested_then_noted_preimage_cannot_be_unnoted() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_noop!(
Preimage::unnote_preimage(Origin::signed(1), hashed([1])),
Error::<Test>::Requested
);
let h = hashed([1]);
assert!(Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
});
}
#[test]
fn request_note_order_makes_no_difference() {
let one_way = new_test_ext().execute_with(|| {
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
(
StatusFor::<Test>::iter().collect::<Vec<_>>(),
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
)
});
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
let other_way = (
StatusFor::<Test>::iter().collect::<Vec<_>>(),
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
);
assert_eq!(one_way, other_way);
});
}
#[test]
fn requested_then_user_noted_preimage_is_free() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_eq!(Balances::reserved_balance(2), 0);
assert_eq!(Balances::free_balance(2), 100);
let h = hashed([1]);
assert!(Preimage::have_preimage(&h));
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
});
}
#[test]
fn request_user_note_order_makes_no_difference() {
let one_way = new_test_ext().execute_with(|| {
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
(
StatusFor::<Test>::iter().collect::<Vec<_>>(),
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
)
});
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
let other_way = (
StatusFor::<Test>::iter().collect::<Vec<_>>(),
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
);
assert_eq!(one_way, other_way);
});
}
#[test]
fn unrequest_preimage_works() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_noop!(
Preimage::unrequest_preimage(Origin::signed(1), hashed([2])),
Error::<Test>::NotRequested
);
assert_ok!(Preimage::unrequest_preimage(Origin::signed(1), hashed([1])));
assert!(Preimage::have_preimage(&hashed([1])));
assert_ok!(Preimage::unrequest_preimage(Origin::signed(1), hashed([1])));
assert_noop!(
Preimage::unrequest_preimage(Origin::signed(1), hashed([1])),
Error::<Test>::NotRequested
);
});
}
#[test]
fn user_noted_then_requested_preimage_is_refunded_once_only() {
new_test_ext().execute_with(|| {
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1; 3]));
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
assert_ok!(Preimage::unrequest_preimage(Origin::signed(1), hashed([1])));
// Still have reserve from `vec[1; 3]`.
assert_eq!(Balances::reserved_balance(2), 5);
assert_eq!(Balances::free_balance(2), 95);
});
}
+238
View File
@@ -0,0 +1,238 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Autogenerated weights for pallet_preimage
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-12-10, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128
// Executed Command:
// target/release/substrate
// benchmark
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=pallet_preimage
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./frame/preimage/src/weights.rs
// --template=./.maintain/frame-weight-template.hbs
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_preimage.
pub trait WeightInfo {
fn note_preimage(s: u32, ) -> Weight;
fn note_requested_preimage(s: u32, ) -> Weight;
fn note_no_deposit_preimage(s: u32, ) -> Weight;
fn unnote_preimage() -> Weight;
fn unnote_no_deposit_preimage() -> Weight;
fn request_preimage() -> Weight;
fn request_no_deposit_preimage() -> Weight;
fn request_unnoted_preimage() -> Weight;
fn request_requested_preimage() -> Weight;
fn unrequest_preimage() -> Weight;
fn unrequest_unnoted_preimage() -> Weight;
fn unrequest_multi_referenced_preimage() -> Weight;
}
/// Weights for pallet_preimage using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
fn note_preimage(s: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:0)
fn note_requested_preimage(s: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:0)
fn note_no_deposit_preimage(s: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unnote_preimage() -> Weight {
(60_560_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unnote_no_deposit_preimage() -> Weight {
(37_575_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_preimage() -> Weight {
(56_868_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_no_deposit_preimage() -> Weight {
(37_058_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_unnoted_preimage() -> Weight {
(21_500_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_requested_preimage() -> Weight {
(7_798_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unrequest_preimage() -> Weight {
(37_771_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unrequest_unnoted_preimage() -> Weight {
(22_913_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn unrequest_multi_referenced_preimage() -> Weight {
(7_608_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
}
// For backwards compatibility and tests
impl WeightInfo for () {
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:1)
fn note_preimage(s: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:0)
fn note_requested_preimage(s: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Preimage PreimageFor (r:1 w:1)
// Storage: Preimage StatusFor (r:1 w:0)
fn note_no_deposit_preimage(s: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unnote_preimage() -> Weight {
(60_560_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unnote_no_deposit_preimage() -> Weight {
(37_575_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_preimage() -> Weight {
(56_868_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_no_deposit_preimage() -> Weight {
(37_058_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_unnoted_preimage() -> Weight {
(21_500_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn request_requested_preimage() -> Weight {
(7_798_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unrequest_preimage() -> Weight {
(37_771_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
// Storage: Preimage PreimageFor (r:0 w:1)
fn unrequest_unnoted_preimage() -> Weight {
(22_913_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Preimage StatusFor (r:1 w:1)
fn unrequest_multi_referenced_preimage() -> Weight {
(7_608_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
}