mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 07:37:57 +00:00
* Starting * closer * Compiles! * comments * Create seperate mock * Remove changes to test env * Fix step calculation * Add host function * Add runtime api * compiles * Update to use offchain timestamp * Gives a result * added some CLI wip * make generic * Update instance * Remove CLI stuff * Remove last cli stuff * undo more changes * Update benchmarks * Update Cargo.lock * remove test * Move loop out of runtime * Benchmarking externalities * Benchmarking state * Implemented commit * Make CLI work, move loop back into runtime * Wipe resets to genesis * Speedup benchmarks * Use enum to select extrinsic within pallet * CLI controls which module and extrinsic to call * Select a pallet with cli * Add steps and repeats to cli * Output as CSV format * Introduce benchmark pallet * Append bench * Use Results * fix merge * Clear Identity benchmark * Bench request judgment and cancel request * Add final benchmarks * Fix CSV output * Start cleaning up for PR * Bump numbers in `wasmtime` integration tests. * More docs * Add rockdb feature to bench * Fix formatting issues * Add test feature to bench * Add test feature to bench * Add rocksdb feature flag * Update bench.rs Co-authored-by: Arkadiy Paronyan <arkady.paronyan@gmail.com> Co-authored-by: Gavin Wood <github@gavwood.com>
This commit is contained in:
Generated
+1
@@ -5654,6 +5654,7 @@ dependencies = [
|
||||
"parity-util-mem",
|
||||
"parking_lot 0.10.0",
|
||||
"quickcheck",
|
||||
"rand 0.7.3",
|
||||
"sc-client",
|
||||
"sc-client-api",
|
||||
"sc-executor",
|
||||
|
||||
@@ -30,12 +30,14 @@ use sp_core::u32_trait::{_1, _2, _3, _4};
|
||||
use node_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, Moment, Signature};
|
||||
use sp_api::impl_runtime_apis;
|
||||
use sp_runtime::{
|
||||
Permill, Perbill, Percent, ApplyExtrinsicResult, impl_opaque_keys, generic, create_runtime_str
|
||||
Permill, Perbill, Percent, ApplyExtrinsicResult, impl_opaque_keys, generic, create_runtime_str,
|
||||
BenchmarkResults,
|
||||
};
|
||||
use sp_runtime::curve::PiecewiseLinear;
|
||||
use sp_runtime::transaction_validity::TransactionValidity;
|
||||
use sp_runtime::traits::{
|
||||
self, BlakeTwo256, Block as BlockT, StaticLookup, SaturatedConversion, ConvertInto, OpaqueKeys,
|
||||
self, BlakeTwo256, Block as BlockT, StaticLookup, SaturatedConversion,
|
||||
ConvertInto, OpaqueKeys, Benchmarking,
|
||||
};
|
||||
use sp_version::RuntimeVersion;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
@@ -804,6 +806,25 @@ impl_runtime_apis! {
|
||||
SessionKeys::decode_into_raw_public_keys(&encoded)
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Benchmark<Block> for Runtime {
|
||||
fn dispatch_benchmark(module: Vec<u8>, extrinsic: Vec<u8>, steps: u32, repeat: u32)
|
||||
-> Option<Vec<BenchmarkResults>>
|
||||
{
|
||||
match module.as_slice() {
|
||||
b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat).ok(),
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
pub trait Benchmark
|
||||
{
|
||||
fn dispatch_benchmark(module: Vec<u8>, extrinsic: Vec<u8>, steps: u32, repeat: u32)
|
||||
-> Option<Vec<BenchmarkResults>>;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -58,6 +58,7 @@ use params::{
|
||||
pub use params::{
|
||||
SharedParams, ImportParams, ExecutionStrategy, Subcommand, RunCmd, BuildSpecCmd,
|
||||
ExportBlocksCmd, ImportBlocksCmd, CheckBlockCmd, PurgeChainCmd, RevertCmd,
|
||||
BenchmarkCmd,
|
||||
};
|
||||
pub use traits::GetSharedParams;
|
||||
use app_dirs::{AppInfo, AppDataType};
|
||||
|
||||
@@ -845,6 +845,49 @@ pub struct PurgeChainCmd {
|
||||
pub shared_params: SharedParams,
|
||||
}
|
||||
|
||||
/// The `benchmark` command used to benchmark FRAME Pallets.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct BenchmarkCmd {
|
||||
/// Select a FRAME Pallet to benchmark.
|
||||
#[structopt(short, long)]
|
||||
pub pallet: String,
|
||||
|
||||
/// Select an extrinsic to benchmark.
|
||||
#[structopt(short, long)]
|
||||
pub extrinsic: String,
|
||||
|
||||
/// Select how many samples we should take across the variable components.
|
||||
#[structopt(short, long, default_value = "1")]
|
||||
pub steps: u32,
|
||||
|
||||
/// Select how many repetitions of this benchmark should run.
|
||||
#[structopt(short, long, default_value = "1")]
|
||||
pub repeat: u32,
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[structopt(flatten)]
|
||||
pub shared_params: SharedParams,
|
||||
|
||||
/// The execution strategy that should be used for benchmarks
|
||||
#[structopt(
|
||||
long = "execution",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
)]
|
||||
pub execution: Option<ExecutionStrategy>,
|
||||
|
||||
/// Method for executing Wasm runtime code.
|
||||
#[structopt(
|
||||
long = "wasm-execution",
|
||||
value_name = "METHOD",
|
||||
possible_values = &WasmExecutionMethod::enabled_variants(),
|
||||
case_insensitive = true,
|
||||
default_value = "Interpreted"
|
||||
)]
|
||||
pub wasm_method: WasmExecutionMethod,
|
||||
}
|
||||
|
||||
/// All core commands that are provided by default.
|
||||
///
|
||||
/// The core commands are split into multiple subcommands and `Run` is the default subcommand. From
|
||||
@@ -869,6 +912,9 @@ pub enum Subcommand {
|
||||
|
||||
/// Remove the whole chain data.
|
||||
PurgeChain(PurgeChainCmd),
|
||||
|
||||
/// Run runtime benchmarks.
|
||||
Benchmark(BenchmarkCmd),
|
||||
}
|
||||
|
||||
impl Subcommand {
|
||||
@@ -883,6 +929,7 @@ impl Subcommand {
|
||||
CheckBlock(params) => ¶ms.shared_params,
|
||||
Revert(params) => ¶ms.shared_params,
|
||||
PurgeChain(params) => ¶ms.shared_params,
|
||||
Benchmark(params) => ¶ms.shared_params,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -909,6 +956,7 @@ impl Subcommand {
|
||||
Subcommand::ImportBlocks(cmd) => cmd.run(config, builder),
|
||||
Subcommand::CheckBlock(cmd) => cmd.run(config, builder),
|
||||
Subcommand::PurgeChain(cmd) => cmd.run(config),
|
||||
Subcommand::Benchmark(cmd) => cmd.run(config, builder),
|
||||
Subcommand::Revert(cmd) => cmd.run(config, builder),
|
||||
}
|
||||
}
|
||||
@@ -1186,3 +1234,32 @@ impl RevertCmd {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl BenchmarkCmd {
|
||||
/// Runs the command and benchmarks the chain.
|
||||
pub fn run<G, E, B, BC, BB>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
_builder: B,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
BC: ServiceBuilderCommand<Block = BB> + Unpin,
|
||||
BB: sp_runtime::traits::Block + Debug,
|
||||
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
|
||||
<BB as BlockT>::Hash: std::str::FromStr,
|
||||
{
|
||||
let spec = config.chain_spec.expect("chain_spec is always Some");
|
||||
let execution_strategy = self.execution.unwrap_or(ExecutionStrategy::Native).into();
|
||||
let wasm_method = self.wasm_method.into();
|
||||
let pallet = self.pallet;
|
||||
let extrinsic = self.extrinsic;
|
||||
let steps = self.steps;
|
||||
let repeat = self.repeat;
|
||||
sc_service::chain_ops::benchmark_runtime::<BB, BC::NativeDispatch, _, _>(spec, execution_strategy, wasm_method, pallet, extrinsic, steps, repeat)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ license = "GPL-3.0"
|
||||
[dependencies]
|
||||
parking_lot = "0.10.0"
|
||||
log = "0.4.8"
|
||||
rand = "0.7"
|
||||
kvdb = "0.4.0"
|
||||
kvdb-rocksdb = { version = "0.5", optional = true }
|
||||
kvdb-memorydb = "0.4.0"
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! State backend that's useful for benchmarking
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::path::PathBuf;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use rand::Rng;
|
||||
|
||||
use hash_db::{Prefix, Hasher};
|
||||
use sp_trie::{MemoryDB, prefixed_key};
|
||||
use sp_core::storage::ChildInfo;
|
||||
use sp_runtime::traits::{Block as BlockT, HasherFor};
|
||||
use sp_runtime::Storage;
|
||||
use sp_state_machine::{DBValue, backend::Backend as StateBackend};
|
||||
use kvdb::{KeyValueDB, DBTransaction};
|
||||
use kvdb_rocksdb::{Database, DatabaseConfig};
|
||||
|
||||
type DbState<B> = sp_state_machine::TrieBackend<
|
||||
Arc<dyn sp_state_machine::Storage<HasherFor<B>>>, HasherFor<B>
|
||||
>;
|
||||
|
||||
struct StorageDb<Block: BlockT> {
|
||||
db: Arc<dyn KeyValueDB>,
|
||||
_block: std::marker::PhantomData<Block>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HasherFor<Block>> for StorageDb<Block> {
|
||||
fn get(&self, key: &Block::Hash, prefix: Prefix) -> Result<Option<DBValue>, String> {
|
||||
let key = prefixed_key::<HasherFor<Block>>(key, prefix);
|
||||
self.db.get(0, &key)
|
||||
.map_err(|e| format!("Database backend error: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
/// State that manages the backend database reference. Allows runtime to control the database.
|
||||
pub struct BenchmarkingState<B: BlockT> {
|
||||
path: PathBuf,
|
||||
root: Cell<B::Hash>,
|
||||
state: RefCell<Option<DbState<B>>>,
|
||||
db: Cell<Option<Arc<dyn KeyValueDB>>>,
|
||||
genesis: <DbState<B> as StateBackend<HasherFor<B>>>::Transaction,
|
||||
}
|
||||
|
||||
impl<B: BlockT> BenchmarkingState<B> {
|
||||
/// Create a new instance that creates a database in a temporary dir.
|
||||
pub fn new(genesis: Storage) -> Result<Self, String> {
|
||||
let temp_dir = PathBuf::from(std::env::temp_dir());
|
||||
let name: String = rand::thread_rng().sample_iter(&rand::distributions::Alphanumeric).take(10).collect();
|
||||
let path = temp_dir.join(&name);
|
||||
|
||||
let mut root = B::Hash::default();
|
||||
let mut mdb = MemoryDB::<HasherFor<B>>::default();
|
||||
sp_state_machine::TrieDBMut::<HasherFor<B>>::new(&mut mdb, &mut root);
|
||||
|
||||
std::fs::create_dir(&path).map_err(|_| String::from("Error creating temp dir"))?;
|
||||
let mut state = BenchmarkingState {
|
||||
state: RefCell::new(None),
|
||||
db: Cell::new(None),
|
||||
path,
|
||||
root: Cell::new(root),
|
||||
genesis: Default::default(),
|
||||
};
|
||||
|
||||
state.reopen()?;
|
||||
let child_delta = genesis.children.into_iter().map(|(storage_key, child_content)| (
|
||||
storage_key,
|
||||
child_content.data.into_iter().map(|(k, v)| (k, Some(v))),
|
||||
child_content.child_info
|
||||
));
|
||||
let (root, transaction) = state.state.borrow_mut().as_mut().unwrap().full_storage_root(
|
||||
genesis.top.into_iter().map(|(k, v)| (k, Some(v))),
|
||||
child_delta,
|
||||
);
|
||||
state.genesis = transaction.clone();
|
||||
state.commit(root, transaction)?;
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
fn reopen(&self) -> Result<(), String> {
|
||||
*self.state.borrow_mut() = None;
|
||||
self.db.set(None);
|
||||
let db_config = DatabaseConfig::with_columns(1);
|
||||
let path = self.path.to_str()
|
||||
.ok_or_else(|| String::from("Invalid database path"))?;
|
||||
let db = Arc::new(Database::open(&db_config, &path).map_err(|e| format!("Error opening database: {:?}", e))?);
|
||||
self.db.set(Some(db.clone()));
|
||||
let storage_db = Arc::new(StorageDb::<B> { db, _block: Default::default() });
|
||||
*self.state.borrow_mut() = Some(DbState::<B>::new(storage_db, self.root.get()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn kill(&self) -> Result<(), String> {
|
||||
self.db.set(None);
|
||||
*self.state.borrow_mut() = None;
|
||||
let mut root = B::Hash::default();
|
||||
let mut mdb = MemoryDB::<HasherFor<B>>::default();
|
||||
sp_state_machine::TrieDBMut::<HasherFor<B>>::new(&mut mdb, &mut root);
|
||||
self.root.set(root);
|
||||
|
||||
std::fs::remove_dir_all(&self.path).map_err(|_| "Error removing database dir".into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: BlockT> Drop for BenchmarkingState<B> {
|
||||
fn drop(&mut self) {
|
||||
self.kill().ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn state_err() -> String {
|
||||
"State is not open".into()
|
||||
}
|
||||
|
||||
impl<B: BlockT> StateBackend<HasherFor<B>> for BenchmarkingState<B> {
|
||||
type Error = <DbState<B> as StateBackend<HasherFor<B>>>::Error;
|
||||
type Transaction = <DbState<B> as StateBackend<HasherFor<B>>>::Transaction;
|
||||
type TrieBackendStorage = <DbState<B> as StateBackend<HasherFor<B>>>::TrieBackendStorage;
|
||||
|
||||
fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.storage(key)
|
||||
}
|
||||
|
||||
fn storage_hash(&self, key: &[u8]) -> Result<Option<B::Hash>, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.storage_hash(key)
|
||||
}
|
||||
|
||||
fn child_storage(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
key: &[u8],
|
||||
) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.child_storage(storage_key, child_info, key)
|
||||
}
|
||||
|
||||
fn exists_storage(&self, key: &[u8]) -> Result<bool, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.exists_storage(key)
|
||||
}
|
||||
|
||||
fn exists_child_storage(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
key: &[u8],
|
||||
) -> Result<bool, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.exists_child_storage(storage_key, child_info, key)
|
||||
}
|
||||
|
||||
fn next_storage_key(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.next_storage_key(key)
|
||||
}
|
||||
|
||||
fn next_child_storage_key(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
key: &[u8],
|
||||
) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.next_child_storage_key(storage_key, child_info, key)
|
||||
}
|
||||
|
||||
fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], f: F) {
|
||||
if let Some(ref state) = *self.state.borrow() {
|
||||
state.for_keys_with_prefix(prefix, f)
|
||||
}
|
||||
}
|
||||
|
||||
fn for_key_values_with_prefix<F: FnMut(&[u8], &[u8])>(&self, prefix: &[u8], f: F) {
|
||||
if let Some(ref state) = *self.state.borrow() {
|
||||
state.for_key_values_with_prefix(prefix, f)
|
||||
}
|
||||
}
|
||||
|
||||
fn for_keys_in_child_storage<F: FnMut(&[u8])>(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
f: F,
|
||||
) {
|
||||
if let Some(ref state) = *self.state.borrow() {
|
||||
state.for_keys_in_child_storage(storage_key, child_info, f)
|
||||
}
|
||||
}
|
||||
|
||||
fn for_child_keys_with_prefix<F: FnMut(&[u8])>(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
prefix: &[u8],
|
||||
f: F,
|
||||
) {
|
||||
if let Some(ref state) = *self.state.borrow() {
|
||||
state.for_child_keys_with_prefix(storage_key, child_info, prefix, f)
|
||||
}
|
||||
}
|
||||
|
||||
fn storage_root<I>(&self, delta: I) -> (B::Hash, Self::Transaction) where
|
||||
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
|
||||
{
|
||||
self.state.borrow().as_ref().map_or(Default::default(), |s| s.storage_root(delta))
|
||||
}
|
||||
|
||||
fn child_storage_root<I>(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
delta: I,
|
||||
) -> (B::Hash, bool, Self::Transaction) where
|
||||
I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>,
|
||||
{
|
||||
self.state.borrow().as_ref().map_or(Default::default(), |s| s.child_storage_root(storage_key, child_info, delta))
|
||||
}
|
||||
|
||||
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {
|
||||
self.state.borrow().as_ref().map_or(Default::default(), |s| s.pairs())
|
||||
}
|
||||
|
||||
fn keys(&self, prefix: &[u8]) -> Vec<Vec<u8>> {
|
||||
self.state.borrow().as_ref().map_or(Default::default(), |s| s.keys(prefix))
|
||||
}
|
||||
|
||||
fn child_keys(
|
||||
&self,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
prefix: &[u8],
|
||||
) -> Vec<Vec<u8>> {
|
||||
self.state.borrow().as_ref().map_or(Default::default(), |s| s.child_keys(storage_key, child_info, prefix))
|
||||
}
|
||||
|
||||
fn as_trie_backend(&mut self)
|
||||
-> Option<&sp_state_machine::TrieBackend<Self::TrieBackendStorage, HasherFor<B>>>
|
||||
{
|
||||
None
|
||||
}
|
||||
|
||||
fn commit(&self, storage_root: <HasherFor<B> as Hasher>::Out, mut transaction: Self::Transaction)
|
||||
-> Result<(), Self::Error>
|
||||
{
|
||||
if let Some(db) = self.db.take() {
|
||||
let mut db_transaction = DBTransaction::new();
|
||||
|
||||
for (key, (val, rc)) in transaction.drain() {
|
||||
if rc > 0 {
|
||||
db_transaction.put(0, &key, &val);
|
||||
} else if rc < 0 {
|
||||
db_transaction.delete(0, &key);
|
||||
}
|
||||
}
|
||||
db.write(db_transaction).map_err(|_| String::from("Error committing transaction"))?;
|
||||
self.root.set(storage_root);
|
||||
} else {
|
||||
return Err("Trying to commit to a closed db".into())
|
||||
}
|
||||
self.reopen()
|
||||
}
|
||||
|
||||
fn wipe(&self) -> Result<(), Self::Error> {
|
||||
self.kill()?;
|
||||
self.reopen()?;
|
||||
self.commit(self.root.get(), self.genesis.clone())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> std::fmt::Debug for BenchmarkingState<Block> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "DB at {:?}", self.path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
pub mod light;
|
||||
pub mod offchain;
|
||||
|
||||
#[cfg(any(feature = "kvdb-rocksdb", test))]
|
||||
pub mod bench;
|
||||
|
||||
mod children;
|
||||
mod cache;
|
||||
mod changes_tries_storage;
|
||||
@@ -80,6 +83,9 @@ use crate::stats::StateUsageStats;
|
||||
use log::{trace, debug, warn};
|
||||
pub use sc_state_db::PruningMode;
|
||||
|
||||
#[cfg(any(feature = "kvdb-rocksdb", test))]
|
||||
pub use bench::BenchmarkingState;
|
||||
|
||||
#[cfg(feature = "test-helpers")]
|
||||
use sc_client::in_mem::Backend as InMemoryBackend;
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ fn call_not_existing_function(wasm_method: WasmExecutionMethod) {
|
||||
#[cfg(feature = "wasmtime")]
|
||||
WasmExecutionMethod::Compiled => assert_eq!(
|
||||
&format!("{:?}", e),
|
||||
"Other(\"call to undefined external function with index 68\")"
|
||||
"Other(\"call to undefined external function with index 71\")"
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,7 @@ fn call_yet_another_not_existing_function(wasm_method: WasmExecutionMethod) {
|
||||
#[cfg(feature = "wasmtime")]
|
||||
WasmExecutionMethod::Compiled => assert_eq!(
|
||||
&format!("{:?}", e),
|
||||
"Other(\"call to undefined external function with index 69\")"
|
||||
"Other(\"call to undefined external function with index 72\")"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -673,6 +673,8 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
|
||||
pub trait ServiceBuilderCommand {
|
||||
/// Block type this API operates on.
|
||||
type Block: BlockT;
|
||||
/// Native execution dispatch required by some commands.
|
||||
type NativeDispatch: NativeExecutionDispatch + 'static;
|
||||
/// Starts the process of importing blocks.
|
||||
fn import_blocks(
|
||||
self,
|
||||
|
||||
@@ -22,14 +22,20 @@ use crate::error::Error;
|
||||
use sc_chain_spec::{ChainSpec, RuntimeGenesis, Extension};
|
||||
use log::{warn, info};
|
||||
use futures::{future, prelude::*};
|
||||
use sp_runtime::traits::{
|
||||
Block as BlockT, NumberFor, One, Zero, Header, SaturatedConversion
|
||||
use sp_runtime::{
|
||||
BuildStorage, BenchmarkResults,
|
||||
traits::{
|
||||
Block as BlockT, NumberFor, One, Zero, Header, SaturatedConversion
|
||||
}
|
||||
};
|
||||
use sp_runtime::generic::{BlockId, SignedBlock};
|
||||
use codec::{Decode, Encode, IoReader};
|
||||
use sc_client::Client;
|
||||
use sc_client::{Client, ExecutionStrategy, StateMachine, LocalCallExecutor};
|
||||
#[cfg(feature = "rocksdb")]
|
||||
use sc_client_db::BenchmarkingState;
|
||||
use sp_consensus::import_queue::{IncomingBlock, Link, BlockImportError, BlockImportResult, ImportQueue};
|
||||
use sp_consensus::BlockOrigin;
|
||||
use sc_executor::{NativeExecutor, NativeExecutionDispatch, WasmExecutionMethod};
|
||||
|
||||
use std::{io::{Read, Write, Seek}, pin::Pin};
|
||||
|
||||
@@ -43,21 +49,76 @@ pub fn build_spec<G, E>(spec: ChainSpec<G, E>, raw: bool) -> error::Result<Strin
|
||||
Ok(spec.to_json(raw)?)
|
||||
}
|
||||
|
||||
/// Run runtime benchmarks.
|
||||
#[cfg(feature = "rocksdb")]
|
||||
pub fn benchmark_runtime<TBl, TExecDisp, G, E> (
|
||||
spec: ChainSpec<G, E>,
|
||||
strategy: ExecutionStrategy,
|
||||
wasm_method: WasmExecutionMethod,
|
||||
pallet: String,
|
||||
extrinsic: String,
|
||||
steps: u32,
|
||||
repeat: u32,
|
||||
) -> error::Result<()> where
|
||||
TBl: BlockT,
|
||||
TExecDisp: NativeExecutionDispatch + 'static,
|
||||
G: RuntimeGenesis,
|
||||
E: Extension,
|
||||
{
|
||||
let genesis_storage = spec.build_storage()?;
|
||||
let mut changes = Default::default();
|
||||
let state = BenchmarkingState::<TBl>::new(genesis_storage)?;
|
||||
let executor = NativeExecutor::<TExecDisp>::new(
|
||||
wasm_method,
|
||||
None, // heap pages
|
||||
);
|
||||
let result = StateMachine::<_, _, NumberFor<TBl>, _>::new(
|
||||
&state,
|
||||
None,
|
||||
&mut changes,
|
||||
&executor,
|
||||
"Benchmark_dispatch_benchmark",
|
||||
&(&pallet, &extrinsic, steps, repeat).encode(),
|
||||
Default::default(),
|
||||
).execute(strategy).map_err(|e| format!("Error executing runtime benchmark: {:?}", e))?;
|
||||
let results = <Option<Vec<BenchmarkResults>> as Decode>::decode(&mut &result[..]).unwrap_or(None);
|
||||
if let Some(results) = results {
|
||||
// Print benchmark metadata
|
||||
println!("Pallet: {:?}, Extrinsic: {:?}, Steps: {:?}, Repeat: {:?}", pallet, extrinsic, steps, repeat);
|
||||
// Print the table header
|
||||
results[0].0.iter().for_each(|param| print!("{:?},", param.0));
|
||||
print!("time\n");
|
||||
// Print the values
|
||||
results.iter().for_each(|result| {
|
||||
let parameters = &result.0;
|
||||
parameters.iter().for_each(|param| print!("{:?},", param.1));
|
||||
print!("{:?}\n", result.1);
|
||||
});
|
||||
info!("Done.");
|
||||
} else {
|
||||
info!("No Results.");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
impl<
|
||||
TBl, TRtApi, TGen, TCSExt, TBackend,
|
||||
TExec, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP,
|
||||
TExecDisp, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP,
|
||||
TExPool, TRpc, Backend
|
||||
> ServiceBuilderCommand for ServiceBuilder<
|
||||
TBl, TRtApi, TGen, TCSExt, Client<TBackend, TExec, TBl, TRtApi>,
|
||||
TBl, TRtApi, TGen, TCSExt,
|
||||
Client<TBackend, LocalCallExecutor<TBackend, NativeExecutor<TExecDisp>>, TBl, TRtApi>,
|
||||
TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TExPool, TRpc, Backend
|
||||
> where
|
||||
TBl: BlockT,
|
||||
TBackend: 'static + sc_client_api::backend::Backend<TBl> + Send,
|
||||
TExec: 'static + sc_client::CallExecutor<TBl> + Send + Sync + Clone,
|
||||
TExecDisp: 'static + NativeExecutionDispatch,
|
||||
TImpQu: 'static + ImportQueue<TBl>,
|
||||
TRtApi: 'static + Send + Sync,
|
||||
{
|
||||
type Block = TBl;
|
||||
type NativeDispatch = TExecDisp;
|
||||
|
||||
fn import_blocks(
|
||||
self,
|
||||
|
||||
@@ -65,6 +65,7 @@ pub use sp_transaction_pool::{TransactionPool, InPoolTransaction, error::IntoPoo
|
||||
pub use sc_transaction_pool::txpool::Options as TransactionPoolOptions;
|
||||
pub use sc_client::FinalityNotifications;
|
||||
pub use sc_rpc::Metadata as RpcMetadata;
|
||||
pub use sc_executor::NativeExecutionDispatch;
|
||||
#[doc(hidden)]
|
||||
pub use std::{ops::Deref, result::Result, sync::Arc};
|
||||
#[doc(hidden)]
|
||||
|
||||
@@ -104,4 +104,4 @@ pub use crate::{
|
||||
},
|
||||
leaves::LeafSet,
|
||||
};
|
||||
pub use sp_state_machine::{ExecutionStrategy, StorageProof};
|
||||
pub use sp_state_machine::{ExecutionStrategy, StorageProof, StateMachine};
|
||||
|
||||
@@ -0,0 +1,630 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Identity pallet benchmarking.
|
||||
|
||||
use super::*;
|
||||
|
||||
use frame_system::RawOrigin;
|
||||
use sp_io::hashing::blake2_256;
|
||||
use sp_runtime::{BenchmarkResults, BenchmarkParameter};
|
||||
use sp_runtime::traits::{Bounded, Benchmarking, BenchmarkingSetup, Dispatchable};
|
||||
|
||||
use crate::Module as Identity;
|
||||
|
||||
// The maximum number of identity registrars we will test.
|
||||
const MAX_REGISTRARS: u32 = 50;
|
||||
|
||||
// Support Functions
|
||||
fn account<T: Trait>(name: &'static str, index: u32) -> T::AccountId {
|
||||
let entropy = (name, index).using_encoded(blake2_256);
|
||||
T::AccountId::decode(&mut &entropy[..]).unwrap_or_default()
|
||||
}
|
||||
|
||||
// Adds `r` registrars to the Identity Pallet. These registrars will have set fees and fields.
|
||||
fn add_registrars<T: Trait>(r: u32) -> Result<(), &'static str> {
|
||||
for i in 0..r {
|
||||
let _ = T::Currency::make_free_balance_be(&account::<T>("registrar", i), BalanceOf::<T>::max_value());
|
||||
Identity::<T>::add_registrar(RawOrigin::Root.into(), account::<T>("registrar", i))?;
|
||||
Identity::<T>::set_fee(RawOrigin::Signed(account::<T>("registrar", i)).into(), i.into(), 10.into())?;
|
||||
let fields = IdentityFields(
|
||||
IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot
|
||||
| IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter
|
||||
);
|
||||
Identity::<T>::set_fields(RawOrigin::Signed(account::<T>("registrar", i)).into(), i.into(), fields)?;
|
||||
}
|
||||
|
||||
assert_eq!(Registrars::<T>::get().len(), r as usize);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Adds `s` sub-accounts to the identity of `who`. Each wil have 32 bytes of raw data added to it.
|
||||
// This additionally returns the vector of sub-accounts to it can be modified if needed.
|
||||
fn add_sub_accounts<T: Trait>(who: T::AccountId, s: u32) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
|
||||
let mut subs = Vec::new();
|
||||
let who_origin = RawOrigin::Signed(who.clone());
|
||||
let data = Data::Raw(vec![0; 32]);
|
||||
|
||||
for i in 0..s {
|
||||
let sub_account = account::<T>("sub", i);
|
||||
subs.push((sub_account, data.clone()));
|
||||
}
|
||||
Identity::<T>::set_subs(who_origin.into(), subs.clone())?;
|
||||
|
||||
return Ok(subs)
|
||||
}
|
||||
|
||||
// This creates an `IdentityInfo` object with `num_fields` extra fields.
|
||||
// All data is pre-populated with some arbitrary bytes.
|
||||
fn create_identity_info<T: Trait>(num_fields: u32) -> IdentityInfo {
|
||||
let data = Data::Raw(vec![0; 32]);
|
||||
|
||||
let info = IdentityInfo {
|
||||
additional: vec![(data.clone(), data.clone()); num_fields as usize],
|
||||
display: data.clone(),
|
||||
legal: data.clone(),
|
||||
web: data.clone(),
|
||||
riot: data.clone(),
|
||||
email: data.clone(),
|
||||
pgp_fingerprint: Some([0; 20]),
|
||||
image: data.clone(),
|
||||
twitter: data.clone(),
|
||||
};
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
// Benchmark `add_registrar` extrinsic.
|
||||
struct AddRegistrar;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for AddRegistrar {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// Add r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Return the `add_registrar` r + 1 call
|
||||
Ok((crate::Call::<T>::add_registrar(account::<T>("registrar", r + 1)), RawOrigin::Root))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `set_identity` extrinsic.
|
||||
struct SetIdentity;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for SetIdentity {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
// Additional Field Count
|
||||
(BenchmarkParameter::X, 1, T::MaxAdditionalFields::get())
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// Add r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// The target user
|
||||
let caller = account::<T>("caller", r);
|
||||
let caller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(caller.clone());
|
||||
let caller_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(caller.clone()).into();
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Add an initial identity
|
||||
let initial_info = benchmarking::create_identity_info::<T>(1);
|
||||
Identity::<T>::set_identity(caller_origin.clone(), initial_info)?;
|
||||
|
||||
// User requests judgement from all the registrars, and they approve
|
||||
for i in 0..r {
|
||||
Identity::<T>::request_judgement(caller_origin.clone(), i, 10.into())?;
|
||||
Identity::<T>::provide_judgement(
|
||||
RawOrigin::Signed(account::<T>("registrar", i)).into(),
|
||||
i,
|
||||
caller_lookup.clone(),
|
||||
Judgement::Reasonable
|
||||
)?;
|
||||
}
|
||||
|
||||
// Create identity info with x additional fields
|
||||
let x = components.iter().find(|&c| c.0 == BenchmarkParameter::X).unwrap().1;
|
||||
// 32 byte data that we reuse below
|
||||
let info = benchmarking::create_identity_info::<T>(x);
|
||||
|
||||
// Return the `set_identity` call
|
||||
Ok((crate::Call::<T>::set_identity(info), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `set_subs` extrinsic.
|
||||
struct SetSubs;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for SetSubs {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Subs Count
|
||||
(BenchmarkParameter::S, 1, T::MaxSubAccounts::get()),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// Generic data to be used.
|
||||
let data = Data::Raw(vec![0; 32]);
|
||||
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let caller_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(caller.clone()).into();
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Create their main identity
|
||||
let info = benchmarking::create_identity_info::<T>(1);
|
||||
Identity::<T>::set_identity(caller_origin.clone(), info)?;
|
||||
|
||||
// Give them s many sub accounts
|
||||
let s = components.iter().find(|&c| c.0 == BenchmarkParameter::S).unwrap().1;
|
||||
let mut subs = add_sub_accounts::<T>(caller.clone(), s)?;
|
||||
|
||||
// Create an s+1 sub account to add
|
||||
subs.push((account::<T>("sub", s+1), data));
|
||||
|
||||
// Return the `set_subs` call
|
||||
Ok((crate::Call::<T>::set_subs(subs), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `clear_identity` extrinsic.
|
||||
struct ClearIdentity;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for ClearIdentity {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
// Subs Count
|
||||
(BenchmarkParameter::S, 1, T::MaxSubAccounts::get()),
|
||||
// Additional Field Count
|
||||
(BenchmarkParameter::X, 1, T::MaxAdditionalFields::get()),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let caller_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(caller.clone()).into();
|
||||
let caller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(caller.clone());
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Create their main identity with x additional fields
|
||||
let x = components.iter().find(|&c| c.0 == BenchmarkParameter::X).unwrap().1;
|
||||
let info = benchmarking::create_identity_info::<T>(x);
|
||||
Identity::<T>::set_identity(caller_origin.clone(), info)?;
|
||||
|
||||
// Give them s many sub accounts
|
||||
let s = components.iter().find(|&c| c.0 == BenchmarkParameter::S).unwrap().1;
|
||||
let _ = benchmarking::add_sub_accounts::<T>(caller.clone(), s)?;
|
||||
|
||||
// User requests judgement from all the registrars, and they approve
|
||||
for i in 0..r {
|
||||
Identity::<T>::request_judgement(caller_origin.clone(), i, 10.into())?;
|
||||
Identity::<T>::provide_judgement(
|
||||
RawOrigin::Signed(account::<T>("registrar", i)).into(),
|
||||
i,
|
||||
caller_lookup.clone(),
|
||||
Judgement::Reasonable
|
||||
)?;
|
||||
}
|
||||
|
||||
// Return the `clear_identity` call
|
||||
Ok((crate::Call::<T>::clear_identity(), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `request_judgement` extrinsic.
|
||||
struct RequestJudgement;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for RequestJudgement {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
// Additional Field Count
|
||||
(BenchmarkParameter::X, 1, T::MaxAdditionalFields::get()),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let caller_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(caller.clone()).into();
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Create their main identity with x additional fields
|
||||
let x = components.iter().find(|&c| c.0 == BenchmarkParameter::X).unwrap().1;
|
||||
let info = benchmarking::create_identity_info::<T>(x);
|
||||
Identity::<T>::set_identity(caller_origin.clone(), info)?;
|
||||
|
||||
// Return the `request_judgement` call
|
||||
Ok((crate::Call::<T>::request_judgement(r-1, 10.into()), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `cancel_request` extrinsic.
|
||||
struct CancelRequest;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for CancelRequest {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
// Additional Field Count
|
||||
(BenchmarkParameter::X, 1, T::MaxAdditionalFields::get()),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let caller_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(caller.clone()).into();
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Create their main identity with x additional fields
|
||||
let x = components.iter().find(|&c| c.0 == BenchmarkParameter::X).unwrap().1;
|
||||
let info = benchmarking::create_identity_info::<T>(x);
|
||||
Identity::<T>::set_identity(caller_origin.clone(), info)?;
|
||||
|
||||
// Request judgement
|
||||
Identity::<T>::request_judgement(caller_origin.clone(), r-1, 10.into())?;
|
||||
|
||||
Ok((crate::Call::<T>::cancel_request(r-1), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `set_fee` extrinsic.
|
||||
struct SetFee;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for SetFee {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Add caller as registrar
|
||||
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
|
||||
|
||||
// Return `set_fee` call
|
||||
Ok((crate::Call::<T>::set_fee(r, 10.into()), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `set_account_id` extrinsic.
|
||||
struct SetAccountId;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for SetAccountId {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Add caller as registrar
|
||||
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
|
||||
|
||||
// Return `set_account_id` call
|
||||
Ok((crate::Call::<T>::set_account_id(r, account::<T>("new", 0)), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `set_fields` extrinsic.
|
||||
struct SetFields;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for SetFields {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Add caller as registrar
|
||||
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
|
||||
|
||||
let fields = IdentityFields(
|
||||
IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot
|
||||
| IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter
|
||||
);
|
||||
|
||||
// Return `set_account_id` call
|
||||
Ok((crate::Call::<T>::set_fields(r, fields), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `provide_judgement` extrinsic.
|
||||
struct ProvideJudgement;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for ProvideJudgement {
|
||||
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
// Additional Field Count
|
||||
(BenchmarkParameter::X, 1, T::MaxAdditionalFields::get()),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// Add r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// The user
|
||||
let user = account::<T>("user", r);
|
||||
let user_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(user.clone()).into();
|
||||
let user_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(user.clone());
|
||||
let _ = T::Currency::make_free_balance_be(&user, BalanceOf::<T>::max_value());
|
||||
|
||||
// Create their main identity with x additional fields
|
||||
let x = components.iter().find(|&c| c.0 == BenchmarkParameter::X).unwrap().1;
|
||||
let info = benchmarking::create_identity_info::<T>(x);
|
||||
Identity::<T>::set_identity(user_origin.clone(), info)?;
|
||||
|
||||
// The caller registrar
|
||||
let caller = account::<T>("caller", r);
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Add caller as registrar
|
||||
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
|
||||
|
||||
// User requests judgement from caller registrar
|
||||
Identity::<T>::request_judgement(user_origin.clone(), r, 10.into())?;
|
||||
|
||||
// Return `provide_judgement` call
|
||||
Ok((crate::Call::<T>::provide_judgement(
|
||||
r,
|
||||
user_lookup.clone(),
|
||||
Judgement::Reasonable
|
||||
), RawOrigin::Signed(caller)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark `kill_identity` extrinsic.
|
||||
struct KillIdentity;
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for KillIdentity {
|
||||
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
vec![
|
||||
// Registrar Count
|
||||
(BenchmarkParameter::R, 1, MAX_REGISTRARS),
|
||||
// Subs Count
|
||||
(BenchmarkParameter::S, 1, T::MaxSubAccounts::get()),
|
||||
// Additional Field Count
|
||||
(BenchmarkParameter::X, 1, T::MaxAdditionalFields::get()),
|
||||
]
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
// The target user
|
||||
let caller = account::<T>("caller", 0);
|
||||
let caller_origin: <T as frame_system::Trait>::Origin = RawOrigin::Signed(caller.clone()).into();
|
||||
let caller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(caller.clone());
|
||||
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
|
||||
// Register r registrars
|
||||
let r = components.iter().find(|&c| c.0 == BenchmarkParameter::R).unwrap().1;
|
||||
benchmarking::add_registrars::<T>(r)?;
|
||||
|
||||
// Create their main identity with x additional fields
|
||||
let x = components.iter().find(|&c| c.0 == BenchmarkParameter::X).unwrap().1;
|
||||
let info = benchmarking::create_identity_info::<T>(x);
|
||||
Identity::<T>::set_identity(caller_origin.clone(), info)?;
|
||||
|
||||
// Give them s many sub accounts
|
||||
let s = components.iter().find(|&c| c.0 == BenchmarkParameter::S).unwrap().1;
|
||||
let _ = benchmarking::add_sub_accounts::<T>(caller.clone(), s)?;
|
||||
|
||||
// User requests judgement from all the registrars, and they approve
|
||||
for i in 0..r {
|
||||
Identity::<T>::request_judgement(caller_origin.clone(), i, 10.into())?;
|
||||
Identity::<T>::provide_judgement(
|
||||
RawOrigin::Signed(account::<T>("registrar", i)).into(),
|
||||
i,
|
||||
caller_lookup.clone(),
|
||||
Judgement::Reasonable
|
||||
)?;
|
||||
}
|
||||
|
||||
// Return the `kill_identity` call
|
||||
Ok((crate::Call::<T>::kill_identity(caller_lookup), RawOrigin::Root))
|
||||
}
|
||||
}
|
||||
|
||||
// The list of available benchmarks for this pallet.
|
||||
enum SelectedBenchmark {
|
||||
AddRegistrar,
|
||||
SetIdentity,
|
||||
SetSubs,
|
||||
ClearIdentity,
|
||||
RequestJudgement,
|
||||
CancelRequest,
|
||||
SetFee,
|
||||
SetAccountId,
|
||||
SetFields,
|
||||
ProvideJudgement,
|
||||
KillIdentity,
|
||||
}
|
||||
|
||||
// Allow us to select a benchmark from the list of available benchmarks.
|
||||
|
||||
impl<T: Trait> BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>> for SelectedBenchmark {
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)> {
|
||||
match self {
|
||||
Self::AddRegistrar => <AddRegistrar as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&AddRegistrar),
|
||||
Self::SetIdentity => <SetIdentity as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&SetIdentity),
|
||||
Self::SetSubs => <SetSubs as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&SetSubs),
|
||||
Self::ClearIdentity => <ClearIdentity as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&ClearIdentity),
|
||||
Self::RequestJudgement => <RequestJudgement as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&RequestJudgement),
|
||||
Self::CancelRequest => <CancelRequest as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&CancelRequest),
|
||||
Self::SetFee => <SetFee as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&SetFee),
|
||||
Self::SetAccountId => <SetAccountId as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&SetAccountId),
|
||||
Self::SetFields => <SetFields as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&SetFields),
|
||||
Self::ProvideJudgement => <ProvideJudgement as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&ProvideJudgement),
|
||||
Self::KillIdentity => <KillIdentity as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&KillIdentity),
|
||||
}
|
||||
}
|
||||
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)])
|
||||
-> Result<(crate::Call<T>, RawOrigin<T::AccountId>), &'static str>
|
||||
{
|
||||
match self {
|
||||
Self::AddRegistrar => <AddRegistrar as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&AddRegistrar, components),
|
||||
Self::SetIdentity => <SetIdentity as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&SetIdentity, components),
|
||||
Self::SetSubs => <SetSubs as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&SetSubs, components),
|
||||
Self::ClearIdentity => <ClearIdentity as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&ClearIdentity, components),
|
||||
Self::RequestJudgement => <RequestJudgement as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&RequestJudgement, components),
|
||||
Self::CancelRequest => <CancelRequest as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&CancelRequest, components),
|
||||
Self::SetFee => <SetFee as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&SetFee, components),
|
||||
Self::SetAccountId => <SetAccountId as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&SetAccountId, components),
|
||||
Self::SetFields => <SetFields as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&SetFields, components),
|
||||
Self::ProvideJudgement => <ProvideJudgement as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&ProvideJudgement, components),
|
||||
Self::KillIdentity => <KillIdentity as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&KillIdentity, components),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> Benchmarking<BenchmarkResults> for Module<T> {
|
||||
fn run_benchmark(extrinsic: Vec<u8>, steps: u32, repeat: u32) -> Result<Vec<BenchmarkResults>, &'static str> {
|
||||
// Map the input to the selected benchmark.
|
||||
let selected_benchmark = match extrinsic.as_slice() {
|
||||
b"add_registrar" => SelectedBenchmark::AddRegistrar,
|
||||
b"set_identity" => SelectedBenchmark::SetIdentity,
|
||||
b"set_subs" => SelectedBenchmark::SetSubs,
|
||||
b"clear_identity" => SelectedBenchmark::ClearIdentity,
|
||||
b"request_judgement" => SelectedBenchmark::RequestJudgement,
|
||||
b"cancel_request" => SelectedBenchmark::CancelRequest,
|
||||
b"set_fee" => SelectedBenchmark::SetFee,
|
||||
b"set_account_id" => SelectedBenchmark::SetAccountId,
|
||||
b"set_fields" => SelectedBenchmark::SetFields,
|
||||
b"provide_judgement" => SelectedBenchmark::ProvideJudgement,
|
||||
b"kill_identity" => SelectedBenchmark::KillIdentity,
|
||||
_ => return Err("Could not find extrinsic."),
|
||||
};
|
||||
|
||||
// Warm up the DB
|
||||
sp_io::benchmarking::commit_db();
|
||||
sp_io::benchmarking::wipe_db();
|
||||
|
||||
// first one is set_identity.
|
||||
let components = <SelectedBenchmark as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&selected_benchmark);
|
||||
// results go here
|
||||
let mut results: Vec<BenchmarkResults> = Vec::new();
|
||||
// Select the component we will be benchmarking. Each component will be benchmarked.
|
||||
for (name, low, high) in components.iter() {
|
||||
// Create up to `STEPS` steps for that component between high and low.
|
||||
let step_size = ((high - low) / steps).max(1);
|
||||
let num_of_steps = (high - low) / step_size;
|
||||
for s in 0..num_of_steps {
|
||||
// This is the value we will be testing for component `name`
|
||||
let component_value = low + step_size * s;
|
||||
|
||||
// Select the mid value for all the other components.
|
||||
let c: Vec<(BenchmarkParameter, u32)> = components.iter()
|
||||
.map(|(n, l, h)|
|
||||
(*n, if n == name { component_value } else { (h - l) / 2 + l })
|
||||
).collect();
|
||||
|
||||
// Run the benchmark `repeat` times.
|
||||
for _ in 0..repeat {
|
||||
// Set up the externalities environment for the setup we want to benchmark.
|
||||
let (call, caller) = <SelectedBenchmark as BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::instance(&selected_benchmark, &c)?;
|
||||
// Commit the externalities to the database, flushing the DB cache.
|
||||
// This will enable worst case scenario for reading from the database.
|
||||
sp_io::benchmarking::commit_db();
|
||||
// Run the benchmark.
|
||||
let start = sp_io::benchmarking::current_time();
|
||||
call.dispatch(caller.into())?;
|
||||
let finish = sp_io::benchmarking::current_time();
|
||||
let elapsed = finish - start;
|
||||
results.push((c.clone(), elapsed));
|
||||
// Wipe the DB back to the genesis state.
|
||||
sp_io::benchmarking::wipe_db();
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(results);
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,8 @@ use frame_support::{
|
||||
};
|
||||
use frame_system::{self as system, ensure_signed, ensure_root};
|
||||
|
||||
pub mod benchmarking;
|
||||
|
||||
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
|
||||
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
|
||||
|
||||
@@ -880,7 +882,7 @@ mod tests {
|
||||
use sp_runtime::traits::BadOrigin;
|
||||
use frame_support::{
|
||||
assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight,
|
||||
ord_parameter_types
|
||||
ord_parameter_types,
|
||||
};
|
||||
use sp_core::H256;
|
||||
use frame_system::EnsureSignedBy;
|
||||
|
||||
@@ -202,6 +202,14 @@ pub trait Externalities: ExtensionStore {
|
||||
///
|
||||
/// Returns the SCALE encoded hash.
|
||||
fn storage_changes_root(&mut self, parent: &[u8]) -> Result<Option<Vec<u8>>, ()>;
|
||||
|
||||
fn wipe(&mut self) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn commit(&mut self) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension for the [`Externalities`] trait.
|
||||
|
||||
@@ -771,6 +771,30 @@ pub trait Logging {
|
||||
}
|
||||
}
|
||||
|
||||
/// Interface that provides functions for benchmarking the runtime.
|
||||
#[runtime_interface]
|
||||
pub trait Benchmarking {
|
||||
/// Get the number of nanoseconds passed since the UNIX epoch
|
||||
///
|
||||
/// WARNING! This is a non-deterministic call. Do not use this within
|
||||
/// consensus critical logic.
|
||||
fn current_time() -> u128 {
|
||||
std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.expect("Unix time doesn't go backwards; qed")
|
||||
.as_nanos()
|
||||
}
|
||||
|
||||
/// Reset the trie database to the genesis state.
|
||||
fn wipe_db(&mut self) {
|
||||
self.wipe()
|
||||
}
|
||||
|
||||
/// Commit pending storage changes to the trie database and clear the database cache.
|
||||
fn commit_db(&mut self) {
|
||||
self.commit()
|
||||
}
|
||||
}
|
||||
|
||||
/// Wasm-only interface that provides functions for interacting with the sandbox.
|
||||
#[runtime_interface(wasm_only)]
|
||||
pub trait Sandbox {
|
||||
@@ -923,6 +947,7 @@ pub type SubstrateHostFunctions = (
|
||||
logging::HostFunctions,
|
||||
sandbox::HostFunctions,
|
||||
crate::trie::HostFunctions,
|
||||
benchmarking::HostFunctions,
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -685,6 +685,18 @@ pub fn print(print: impl traits::Printable) {
|
||||
print.print();
|
||||
}
|
||||
|
||||
/// An alphabet of possible parameters to use for benchmarking.
|
||||
#[derive(Encode, Decode, Clone, Copy, PartialEq, Debug)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum BenchmarkParameter {
|
||||
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
|
||||
}
|
||||
|
||||
/// Results from running benchmarks on a FRAME pallet.
|
||||
/// Contains duration of the function call in nanoseconds along with the benchmark parameters
|
||||
/// used for that benchmark result.
|
||||
pub type BenchmarkResults = (Vec<(BenchmarkParameter, u32)>, u128);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -24,6 +24,7 @@ use std::fmt::Display;
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
||||
use sp_core::{self, Hasher, Blake2Hasher, TypeId, RuntimeDebug};
|
||||
use crate::BenchmarkParameter;
|
||||
use crate::codec::{Codec, Encode, Decode};
|
||||
use crate::transaction_validity::{
|
||||
ValidTransaction, TransactionValidity, TransactionValidityError, UnknownTransaction,
|
||||
@@ -1328,6 +1329,26 @@ pub trait BlockIdTo<Block: self::Block> {
|
||||
) -> Result<Option<NumberFor<Block>>, Self::Error>;
|
||||
}
|
||||
|
||||
/// The pallet benchmarking trait.
|
||||
pub trait Benchmarking<T> {
|
||||
/// Run the benchmarks for this pallet.
|
||||
///
|
||||
/// Parameters
|
||||
/// - `extrinsic`: The name of extrinsic function you want to benchmark encoded as bytes.
|
||||
/// - `steps`: The number of sample points you want to take across the range of parameters.
|
||||
/// - `repeat`: The number of times you want to repeat a benchmark.
|
||||
fn run_benchmark(extrinsic: Vec<u8>, steps: u32, repeat: u32) -> Result<Vec<T>, &'static str>;
|
||||
}
|
||||
|
||||
/// The required setup for creating a benchmark.
|
||||
pub trait BenchmarkingSetup<T, Call, RawOrigin> {
|
||||
/// Return the components and their ranges which should be tested in this benchmark.
|
||||
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)>;
|
||||
|
||||
/// Set up the storage, and prepare a call and caller to test in a single run of the benchmark.
|
||||
fn instance(&self, components: &[(BenchmarkParameter, u32)]) -> Result<(Call, RawOrigin), &'static str>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -213,6 +213,16 @@ pub trait Backend<H: Hasher>: std::fmt::Debug {
|
||||
fn usage_info(&self) -> UsageInfo {
|
||||
UsageInfo::empty()
|
||||
}
|
||||
|
||||
/// Wipe the state database.
|
||||
fn wipe(&self) -> Result<(), Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
/// Commit given transaction to storage.
|
||||
fn commit(&self, _storage_root: H::Out, _transaction: Self::Transaction) -> Result<(), Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Backend<H>, H: Hasher> Backend<H> for &'a T {
|
||||
|
||||
@@ -583,6 +583,25 @@ where
|
||||
|
||||
root.map(|r| r.map(|o| o.encode()))
|
||||
}
|
||||
|
||||
fn wipe(&mut self) {
|
||||
self.overlay.discard_prospective();
|
||||
self.overlay.drain_storage_changes(&self.backend, None, Default::default(), self.storage_transaction_cache)
|
||||
.expect(EXT_NOT_ALLOWED_TO_FAIL);
|
||||
self.storage_transaction_cache.reset();
|
||||
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL)
|
||||
}
|
||||
|
||||
fn commit(&mut self) {
|
||||
self.overlay.commit_prospective();
|
||||
let changes = self.overlay.drain_storage_changes(&self.backend, None, Default::default(), self.storage_transaction_cache)
|
||||
.expect(EXT_NOT_ALLOWED_TO_FAIL);
|
||||
self.backend.commit(
|
||||
changes.transaction_storage_root,
|
||||
changes.transaction,
|
||||
).expect(EXT_NOT_ALLOWED_TO_FAIL);
|
||||
self.storage_transaction_cache.reset();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, H, B, N> sp_externalities::ExtensionStore for Ext<'a, H, N, B>
|
||||
|
||||
@@ -428,15 +428,18 @@ impl OverlayedChanges {
|
||||
///
|
||||
/// Panics:
|
||||
/// Will panic if there are any uncommitted prospective changes.
|
||||
pub fn into_committed(self) -> (
|
||||
fn drain_committed(&mut self) -> (
|
||||
impl Iterator<Item=(StorageKey, Option<StorageValue>)>,
|
||||
impl Iterator<Item=(StorageKey, (impl Iterator<Item=(StorageKey, Option<StorageValue>)>, OwnedChildInfo))>,
|
||||
){
|
||||
) {
|
||||
assert!(self.prospective.is_empty());
|
||||
(
|
||||
self.committed.top.into_iter().map(|(k, v)| (k, v.value)),
|
||||
self.committed.children.into_iter()
|
||||
.map(|(sk, (v, ci))| (sk, (v.into_iter().map(|(k, v)| (k, v.value)), ci)))
|
||||
std::mem::replace(&mut self.committed.top, Default::default())
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k, v.value)),
|
||||
std::mem::replace(&mut self.committed.children, Default::default())
|
||||
.into_iter()
|
||||
.map(|(sk, (v, ci))| (sk, (v.into_iter().map(|(k, v)| (k, v.value)), ci))),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -444,11 +447,22 @@ impl OverlayedChanges {
|
||||
pub fn into_storage_changes<
|
||||
B: Backend<H>, H: Hasher, N: BlockNumber
|
||||
>(
|
||||
self,
|
||||
mut self,
|
||||
backend: &B,
|
||||
changes_trie_state: Option<&ChangesTrieState<H, N>>,
|
||||
parent_hash: H::Out,
|
||||
mut cache: StorageTransactionCache<B::Transaction, H, N>,
|
||||
) -> Result<StorageChanges<B::Transaction, H, N>, String> where H::Out: Ord + Encode + 'static {
|
||||
self.drain_storage_changes(backend, changes_trie_state, parent_hash, &mut cache)
|
||||
}
|
||||
|
||||
/// Drain all changes into a [`StorageChanges`] instance. Leave empty overlay in place.
|
||||
pub fn drain_storage_changes<B: Backend<H>, H: Hasher, N: BlockNumber>(
|
||||
&mut self,
|
||||
backend: &B,
|
||||
changes_trie_state: Option<&ChangesTrieState<H, N>>,
|
||||
parent_hash: H::Out,
|
||||
mut cache: &mut StorageTransactionCache<B::Transaction, H, N>,
|
||||
) -> Result<StorageChanges<B::Transaction, H, N>, String> where H::Out: Ord + Encode + 'static {
|
||||
// If the transaction does not exist, we generate it.
|
||||
if cache.transaction.is_none() {
|
||||
@@ -474,7 +488,7 @@ impl OverlayedChanges {
|
||||
.take()
|
||||
.expect("Changes trie transaction was generated by `changes_trie_root`; qed");
|
||||
|
||||
let (main_storage_changes, child_storage_changes) = self.into_committed();
|
||||
let (main_storage_changes, child_storage_changes) = self.drain_committed();
|
||||
|
||||
Ok(StorageChanges {
|
||||
main_storage_changes: main_storage_changes.collect(),
|
||||
|
||||
Reference in New Issue
Block a user