1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{fmt::Debug, str::FromStr};
use parity_scale_codec::Decode;
use sc_executor::NativeExecutionDispatch;
use sc_service::Configuration;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use crate::{
build_executor, ensure_matching_spec, extract_code, local_spec, state_machine_call_with_proof,
SharedParams, State, LOG_TARGET,
};
#[derive(Debug, Clone, clap::Parser)]
pub struct OnRuntimeUpgradeCmd {
#[clap(subcommand)]
pub state: State,
}
pub(crate) async fn on_runtime_upgrade<Block, ExecDispatch>(
shared: SharedParams,
command: OnRuntimeUpgradeCmd,
config: Configuration,
) -> sc_cli::Result<()>
where
Block: BlockT + serde::de::DeserializeOwned,
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
NumberFor<Block>: FromStr,
<NumberFor<Block> as FromStr>::Err: Debug,
ExecDispatch: NativeExecutionDispatch + 'static,
{
let executor = build_executor(&shared, &config);
let execution = shared.execution;
let ext = {
let builder = command.state.builder::<Block>()?.state_version(shared.state_version);
let (code_key, code) = extract_code(&config.chain_spec)?;
builder.inject_hashed_key_value(&[(code_key, code)]).build().await?
};
if let Some(uri) = command.state.live_uri() {
let (expected_spec_name, expected_spec_version, _) =
local_spec::<Block, ExecDispatch>(&ext, &executor);
ensure_matching_spec::<Block>(
uri,
expected_spec_name,
expected_spec_version,
shared.no_spec_name_check,
)
.await;
}
let (_, encoded_result) = state_machine_call_with_proof::<Block, ExecDispatch>(
&ext,
&executor,
execution,
"TryRuntime_on_runtime_upgrade",
&[],
Default::default(), )?;
let (weight, total_weight) = <(u64, u64) as Decode>::decode(&mut &*encoded_result)
.map_err(|e| format!("failed to decode output: {:?}", e))?;
log::info!(
target: LOG_TARGET,
"TryRuntime_on_runtime_upgrade executed without errors. Consumed weight = {}, total weight = {} ({})",
weight,
total_weight,
weight as f64 / total_weight.max(1) as f64
);
Ok(())
}