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
+174 -40
View File
@@ -17,12 +17,14 @@
//! Scheduler pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::benchmarks;
use frame_support::{ensure, traits::OnInitialize};
use frame_support::{
ensure,
traits::{OnInitialize, PreimageProvider, PreimageRecipient},
};
use frame_system::RawOrigin;
use sp_runtime::traits::Hash;
use sp_std::{prelude::*, vec};
use crate::Pallet as Scheduler;
@@ -30,37 +32,184 @@ use frame_system::Pallet as System;
const BLOCK_NUMBER: u32 = 2;
// Add `n` named items to the schedule
fn fill_schedule<T: Config>(when: T::BlockNumber, n: u32) -> Result<(), &'static str> {
// Essentially a no-op call.
let call = frame_system::Call::set_storage { items: vec![] };
/// Add `n` named items to the schedule.
///
/// For `resolved`:
/// - `None`: aborted (hash without preimage)
/// - `Some(true)`: hash resolves into call if possible, plain call otherwise
/// - `Some(false)`: plain call
fn fill_schedule<T: Config>(
when: T::BlockNumber,
n: u32,
periodic: bool,
named: bool,
resolved: Option<bool>,
) -> Result<(), &'static str> {
for i in 0..n {
// Named schedule is strictly heavier than anonymous
Scheduler::<T>::do_schedule_named(
i.encode(),
DispatchTime::At(when),
// Add periodicity
Some((T::BlockNumber::one(), 100)),
// HARD_DEADLINE priority means it gets executed no matter what
0,
frame_system::RawOrigin::Root.into(),
call.clone().into(),
)?;
let (call, hash) = call_and_hash::<T>(i);
let call_or_hash = match resolved {
Some(true) => {
T::PreimageProvider::note_preimage(call.encode().try_into().unwrap());
if T::PreimageProvider::have_preimage(&hash) {
CallOrHashOf::<T>::Hash(hash)
} else {
call.into()
}
},
Some(false) => call.into(),
None => CallOrHashOf::<T>::Hash(hash),
};
let period = match periodic {
true => Some(((i + 100).into(), 100)),
false => None,
};
let t = DispatchTime::At(when);
let origin = frame_system::RawOrigin::Root.into();
if named {
Scheduler::<T>::do_schedule_named(i.encode(), t, period, 0, origin, call_or_hash)?;
} else {
Scheduler::<T>::do_schedule(t, period, 0, origin, call_or_hash)?;
}
}
ensure!(Agenda::<T>::get(when).len() == n as usize, "didn't fill schedule");
Ok(())
}
fn call_and_hash<T: Config>(i: u32) -> (<T as Config>::Call, T::Hash) {
// Essentially a no-op call.
let call: <T as Config>::Call = frame_system::Call::remark { remark: i.encode() }.into();
let hash = T::Hashing::hash_of(&call);
(call, hash)
}
benchmarks! {
on_initialize_periodic_named_resolved {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, true, true, Some(true))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s * 2);
for i in 0..s {
assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
}
}
on_initialize_named_resolved {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, false, true, Some(true))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s * 2);
assert!(Agenda::<T>::iter().count() == 0);
}
on_initialize_periodic_resolved {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, true, false, Some(true))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s * 2);
for i in 0..s {
assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
}
}
on_initialize_resolved {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, false, false, Some(true))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s * 2);
assert!(Agenda::<T>::iter().count() == 0);
}
on_initialize_named_aborted {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, false, true, None)?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), 0);
if let Some(delay) = T::NoPreimagePostponement::get() {
assert_eq!(Agenda::<T>::get(when + delay).len(), s as usize);
} else {
assert!(Agenda::<T>::iter().count() == 0);
}
}
on_initialize_aborted {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, false, false, None)?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), 0);
if let Some(delay) = T::NoPreimagePostponement::get() {
assert_eq!(Agenda::<T>::get(when + delay).len(), s as usize);
} else {
assert!(Agenda::<T>::iter().count() == 0);
}
}
on_initialize_periodic_named {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, true, true, Some(false))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s);
for i in 0..s {
assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
}
}
on_initialize_periodic {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, true, false, Some(false))?;
}: { Scheduler::<T>::on_initialize(when); }
verify {
assert_eq!(System::<T>::event_count(), s);
for i in 0..s {
assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
}
}
on_initialize_named {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, false, true, Some(false))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s);
assert!(Agenda::<T>::iter().count() == 0);
}
on_initialize {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s, false, false, Some(false))?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s);
assert!(Agenda::<T>::iter().count() == 0);
}
schedule {
let s in 0 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
let periodic = Some((T::BlockNumber::one(), 100));
let priority = 0;
// Essentially a no-op call.
let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());
let inner_call = frame_system::Call::set_storage { items: vec![] }.into();
let call = Box::new(CallOrHashOf::<T>::Value(inner_call));
fill_schedule::<T>(when, s)?;
fill_schedule::<T>(when, s, true, true, Some(false))?;
}: _(RawOrigin::Root, when, periodic, priority, call)
verify {
ensure!(
@@ -73,7 +222,7 @@ benchmarks! {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s)?;
fill_schedule::<T>(when, s, true, true, Some(false))?;
assert_eq!(Agenda::<T>::get(when).len(), s as usize);
}: _(RawOrigin::Root, when, 0)
verify {
@@ -95,9 +244,10 @@ benchmarks! {
let periodic = Some((T::BlockNumber::one(), 100));
let priority = 0;
// Essentially a no-op call.
let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());
let inner_call = frame_system::Call::set_storage { items: vec![] }.into();
let call = Box::new(CallOrHashOf::<T>::Value(inner_call));
fill_schedule::<T>(when, s)?;
fill_schedule::<T>(when, s, true, true, Some(false))?;
}: _(RawOrigin::Root, id, when, periodic, priority, call)
verify {
ensure!(
@@ -110,7 +260,7 @@ benchmarks! {
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s)?;
fill_schedule::<T>(when, s, true, true, Some(false))?;
}: _(RawOrigin::Root, 0.encode())
verify {
ensure!(
@@ -124,21 +274,5 @@ benchmarks! {
);
}
// TODO [#7141]: Make this more complex and flexible so it can be used in automation.
#[extra]
on_initialize {
let s in 0 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s)?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
verify {
assert_eq!(System::<T>::event_count(), s);
// Next block should have all the schedules again
ensure!(
Agenda::<T>::get(when + T::BlockNumber::one()).len() == s as usize,
"didn't append schedule"
);
}
impl_benchmark_test_suite!(Scheduler, crate::tests::new_test_ext(), crate::tests::Test);
impl_benchmark_test_suite!(Scheduler, crate::mock::new_test_ext(), crate::mock::Test);
}