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
18#![allow(clippy::result_large_err)]
19
20use std::{fmt::Debug, str::FromStr, time::Duration};
21
22use common::shared_parameters::SharedParams;
23use parity_scale_codec::DecodeAll;
24use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutor};
25use sp_core::{
26    offchain::{
27        testing::{TestOffchainExt, TestTransactionPoolExt},
28        OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
29    },
30    traits::ReadRuntimeVersionExt,
31};
32use sp_externalities::Extensions;
33use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
34use sp_runtime::traits::Block as BlockT;
35use sp_weights::Weight;
36
37pub mod commands;
38pub mod common;
39
40pub(crate) const LOG_TARGET: &str = "try-runtime::cli";
41
42/// Get the hash type of the generic `Block` from a `hash_str`.
43pub(crate) fn hash_of<Block: BlockT>(hash_str: &str) -> sc_cli::Result<Block::Hash>
44where
45    <Block::Hash as FromStr>::Err: Debug,
46{
47    hash_str
48        .parse::<<Block as BlockT>::Hash>()
49        .map_err(|e| format!("Could not parse block hash: {:?}", e).into())
50}
51
52pub struct RefTimeInfo {
53    pub used: Duration,
54    pub max: Duration,
55}
56
57impl TryFrom<Vec<u8>> for RefTimeInfo {
58    type Error = String;
59
60    /// try_from Vec encoded as (Weight, Weight) tuple
61    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
62        let (weight_used, weight_max) = <(Weight, Weight)>::decode_all(&mut &*value)
63            .map_err(|e| format!("failed to decode weight: {:?}", e))?;
64
65        Ok(RefTimeInfo {
66            // 1000 picoseconds == 1 nanosecond
67            used: Duration::from_nanos(weight_used.ref_time() / 1000),
68            max: Duration::from_nanos(weight_max.ref_time() / 1000),
69        })
70    }
71}
72
73/// Build all extensions that we typically use.
74pub(crate) fn full_extensions<H: HostFunctions>(wasm_executor: WasmExecutor<H>) -> Extensions {
75    let mut extensions = Extensions::default();
76    let (offchain, _offchain_state) = TestOffchainExt::new();
77    let (pool, _pool_state) = TestTransactionPoolExt::new();
78    let keystore = MemoryKeystore::new();
79    extensions.register(OffchainDbExt::new(offchain.clone()));
80    extensions.register(OffchainWorkerExt::new(offchain));
81    extensions.register(KeystoreExt::new(keystore));
82    extensions.register(TransactionPoolExt::new(pool));
83    extensions.register(ReadRuntimeVersionExt::new(wasm_executor));
84
85    extensions
86}
87
88pub(crate) fn rpc_err_handler(error: impl Debug) -> &'static str {
89    log::error!(target: LOG_TARGET, "rpc error: {:?}", error);
90    "rpc error."
91}