try_runtime_core/
lib.rs

1// This file is part of try-runtime-cli.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use std::{fmt::Debug, str::FromStr, time::Duration};
19
20use common::shared_parameters::SharedParams;
21use parity_scale_codec::DecodeAll;
22use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutor};
23use sp_core::{
24    offchain::{
25        testing::{TestOffchainExt, TestTransactionPoolExt},
26        OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
27    },
28    traits::ReadRuntimeVersionExt,
29};
30use sp_externalities::Extensions;
31use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
32use sp_runtime::traits::Block as BlockT;
33use sp_weights::Weight;
34
35pub mod commands;
36pub mod common;
37
38pub(crate) const LOG_TARGET: &str = "try-runtime::cli";
39
40/// Get the hash type of the generic `Block` from a `hash_str`.
41pub(crate) fn hash_of<Block: BlockT>(hash_str: &str) -> sc_cli::Result<Block::Hash>
42where
43    <Block::Hash as FromStr>::Err: Debug,
44{
45    hash_str
46        .parse::<<Block as BlockT>::Hash>()
47        .map_err(|e| format!("Could not parse block hash: {:?}", e).into())
48}
49
50pub struct RefTimeInfo {
51    pub used: Duration,
52    pub max: Duration,
53}
54
55impl TryFrom<Vec<u8>> for RefTimeInfo {
56    type Error = String;
57
58    /// try_from Vec encoded as (Weight, Weight) tuple
59    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
60        let (weight_used, weight_max) = <(Weight, Weight)>::decode_all(&mut &*value)
61            .map_err(|e| format!("failed to decode weight: {:?}", e))?;
62
63        Ok(RefTimeInfo {
64            // 1000 picoseconds == 1 nanosecond
65            used: Duration::from_nanos(weight_used.ref_time() / 1000),
66            max: Duration::from_nanos(weight_max.ref_time() / 1000),
67        })
68    }
69}
70
71/// Build all extensions that we typically use.
72pub(crate) fn full_extensions<H: HostFunctions>(wasm_executor: WasmExecutor<H>) -> Extensions {
73    let mut extensions = Extensions::default();
74    let (offchain, _offchain_state) = TestOffchainExt::new();
75    let (pool, _pool_state) = TestTransactionPoolExt::new();
76    let keystore = MemoryKeystore::new();
77    extensions.register(OffchainDbExt::new(offchain.clone()));
78    extensions.register(OffchainWorkerExt::new(offchain));
79    extensions.register(KeystoreExt::new(keystore));
80    extensions.register(TransactionPoolExt::new(pool));
81    extensions.register(ReadRuntimeVersionExt::new(wasm_executor));
82
83    extensions
84}
85
86pub(crate) fn rpc_err_handler(error: impl Debug) -> &'static str {
87    log::error!(target: LOG_TARGET, "rpc error: {:?}", error);
88    "rpc error."
89}