// Copyright 2017-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 .
//! Chain utilities.
use crate::error;
use crate::builder::{ServiceBuilderCommand, ServiceBuilder};
use crate::error::Error;
use sc_chain_spec::{ChainSpec, RuntimeGenesis, Extension};
use log::{warn, info};
use futures::{future, prelude::*};
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, 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};
use sc_network::message;
/// Build a chain spec json
pub fn build_spec(spec: ChainSpec, raw: bool) -> error::Result where
G: RuntimeGenesis,
E: Extension,
{
Ok(spec.to_json(raw)?)
}
/// Run runtime benchmarks.
#[cfg(feature = "rocksdb")]
pub fn benchmark_runtime (
spec: ChainSpec,
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::::new(genesis_storage)?;
let executor = NativeExecutor::::new(
wasm_method,
None, // heap pages
);
let result = StateMachine::<_, _, NumberFor, _>::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 =