Skip to content

Commit d10cd47

Browse files
authored
Support for generic keys in start-node
1 parent 9c92fec commit d10cd47

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

demo/node/src/cli.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use clap::command;
22
use partner_chains_cli::{AURA, GRANDPA, KeyDefinition};
33
use partner_chains_demo_runtime::opaque::SessionKeys;
4-
use partner_chains_node_commands::{
5-
PartnerChainRuntime, PartnerChainsSubcommand, RuntimeTypeWrapper,
6-
};
4+
use partner_chains_node_commands::{PartnerChainRuntime, PartnerChainsSubcommand};
75
use sc_cli::RunCmd;
86
use sp_runtime::AccountId32;
97

@@ -18,9 +16,6 @@ pub struct Cli {
1816

1917
#[derive(Debug, Clone)]
2018
pub struct WizardBindings;
21-
impl RuntimeTypeWrapper for WizardBindings {
22-
type Runtime = partner_chains_demo_runtime::Runtime;
23-
}
2419

2520
impl PartnerChainRuntime for WizardBindings {
2621
type Keys = SessionKeys;

toolkit/cli/node-commands/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use cli_commands::registration_signatures::RegistrationSignaturesCmd;
1212
use frame_support::sp_runtime::traits::NumberFor;
1313
use parity_scale_codec::{Decode, Encode};
1414
use partner_chains_cli::DefaultCmdRunContext;
15-
pub use partner_chains_cli::{PartnerChainRuntime, RuntimeTypeWrapper};
15+
pub use partner_chains_cli::PartnerChainRuntime;
1616
use partner_chains_smart_contracts_commands::SmartContractsCmd;
1717
use sc_cli::{CliConfiguration, SharedParams, SubstrateCli};
1818
use sc_service::TaskManager;

toolkit/partner-chains-cli/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use io::*;
3030
pub use keystore::{AURA, CROSS_CHAIN, GRANDPA, KeyDefinition};
3131
use partner_chains_cardano_offchain::await_tx::FixedDelayRetries;
3232
pub use permissioned_candidates::ParsedPermissionedCandidatesKeys;
33-
pub use runtime_bindings::{PartnerChainRuntime, RuntimeTypeWrapper};
33+
pub use runtime_bindings::PartnerChainRuntime;
3434
use std::time::Duration;
3535

3636
#[derive(Clone, Debug, clap::Parser)]
@@ -65,7 +65,7 @@ pub enum Command<T: PartnerChainRuntime + Send + Sync> {
6565
CreateChainSpec(create_chain_spec::CreateChainSpecCmd<T>),
6666
/// Wizard for starting a substrate node in the environment set up by `generate-keys`,
6767
/// `prepare-config`, and `create-chain-spec`. It also assists in setting the `resources configuration`.
68-
StartNode(start_node::StartNodeCmd),
68+
StartNode(start_node::StartNodeCmd<T>),
6969
/// The first step of registering as a committee candidate. Registration is split into three steps to allow the user to use their cold keys on a cold machine.
7070
Register1(register::register1::Register1Cmd),
7171
/// The second step of registering as a committee candidate, using cold keys.

toolkit/partner-chains-cli/src/runtime_bindings.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
use crate::{CreateChainSpecConfig, keystore::KeyDefinition};
22
use authority_selection_inherents::MaybeFromCandidateKeys;
33

4-
/// Trait wrapping Substrate runtime type. Should be implemented for the runtime of the node.
5-
pub trait RuntimeTypeWrapper {
6-
/// Substrate runtime type.
7-
type Runtime;
8-
}
9-
104
/// Trait defining Partner Chain governance related types.
115
pub trait PartnerChainRuntime {
126
/// User keys type

toolkit/partner-chains-cli/src/start_node/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ use crate::io::IOContext;
55
use crate::keystore::*;
66
use crate::{config::config_fields, *};
77
use serde::Deserialize;
8+
use std::marker::PhantomData;
89

910
#[cfg(test)]
1011
mod tests;
1112

1213
#[derive(Clone, Debug, clap::Parser)]
13-
pub struct StartNodeCmd {
14+
pub struct StartNodeCmd<T: PartnerChainRuntime> {
1415
#[arg(long)]
1516
silent: bool,
17+
#[clap(skip)]
18+
_phantom: PhantomData<T>,
1619
}
1720

1821
pub struct StartNodeConfig {
@@ -37,11 +40,11 @@ pub struct StartNodeChainConfig {
3740
pub bootnodes: Vec<String>,
3841
}
3942

40-
impl CmdRun for StartNodeCmd {
43+
impl<T: PartnerChainRuntime> CmdRun for StartNodeCmd<T> {
4144
fn run<C: IOContext>(&self, context: &C) -> anyhow::Result<()> {
4245
let config = StartNodeConfig::load(context);
4346

44-
if !check_keystore(&config, context)? || !check_chain_spec(context) {
47+
if !check_keystore::<C, T>(&config, context)? || !check_chain_spec(context) {
4548
return Ok(());
4649
}
4750

@@ -56,7 +59,7 @@ impl CmdRun for StartNodeCmd {
5659
return Ok(());
5760
}
5861

59-
start_node(config, chain_config, &db_connection_string, context)?;
62+
start_node::<C, T>(config, chain_config, &db_connection_string, context)?;
6063

6164
Ok(())
6265
}
@@ -117,11 +120,15 @@ fn check_chain_spec<C: IOContext>(context: &C) -> bool {
117120
}
118121
}
119122

120-
fn check_keystore<C: IOContext>(config: &StartNodeConfig, context: &C) -> anyhow::Result<bool> {
123+
fn check_keystore<C: IOContext, T: PartnerChainRuntime>(
124+
config: &StartNodeConfig,
125+
context: &C,
126+
) -> anyhow::Result<bool> {
121127
let existing_keys = context.list_directory(&config.keystore_path())?.unwrap_or_default();
122-
Ok(key_present(&AURA, &existing_keys, context)
123-
&& key_present(&GRANDPA, &existing_keys, context)
124-
&& key_present(&CROSS_CHAIN, &existing_keys, context))
128+
Ok(key_present(&CROSS_CHAIN, &existing_keys, context)
129+
&& T::key_definitions()
130+
.iter()
131+
.all(|key_def| key_present(key_def, &existing_keys, context)))
125132
}
126133

127134
fn key_present<C: IOContext>(key: &KeyDefinition, existing_keys: &[String], context: &C) -> bool {
@@ -136,7 +143,7 @@ fn key_present<C: IOContext>(key: &KeyDefinition, existing_keys: &[String], cont
136143
}
137144
}
138145

139-
pub fn start_node<C: IOContext>(
146+
pub fn start_node<C: IOContext, T: PartnerChainRuntime>(
140147
StartNodeConfig { substrate_node_base_path }: StartNodeConfig,
141148
StartNodeChainConfig {
142149
cardano:

toolkit/partner-chains-cli/src/start_node/tests.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::tests::{CHAIN_CONFIG_FILE_PATH, MockIO, MockIOContext, RESOURCES_CONFIG_FILE_PATH};
1+
use crate::tests::{
2+
CHAIN_CONFIG_FILE_PATH, MockIO, MockIOContext, RESOURCES_CONFIG_FILE_PATH, runtime::MockRuntime,
3+
};
24

35
use super::*;
46

@@ -10,9 +12,9 @@ const DB_CONNECTION_STRING: &str =
1012
fn keystore_path() -> String {
1113
format!("{DATA_PATH}/keystore")
1214
}
13-
const GRANDPA_PREFIX: &str = "6772616e"; // "gran" in hex
15+
const ED25_PREFIX: &str = "65643235"; // "ed25" in hex
1416
const CROSS_CHAIN_PREFIX: &str = "63726368"; // "crch" in hex
15-
const AURA_PREFIX: &str = "61757261"; // "aura" in hex
17+
const SR25_PREFIX: &str = "73723235"; // "sr25" in hex
1618

1719
fn default_config() -> StartNodeConfig {
1820
StartNodeConfig { substrate_node_base_path: DATA_PATH.into() }
@@ -107,8 +109,8 @@ fn happy_path() {
107109
format!(
108110
"{CROSS_CHAIN_PREFIX}020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1"
109111
),
110-
format!("{AURA_PREFIX}aura-key"),
111-
format!("{GRANDPA_PREFIX}grandpa-key"),
112+
format!("{SR25_PREFIX}sr25-key"),
113+
format!("{ED25_PREFIX}ed25-key"),
112114
];
113115

114116
let context = MockIOContext::new()
@@ -127,7 +129,7 @@ fn happy_path() {
127129
MockIO::run_command(&default_chain_config_run_command(), "irrelevant")
128130
]);
129131

130-
let result = StartNodeCmd { silent: false }.run(&context);
132+
let result = StartNodeCmd::<MockRuntime> { silent: false, _phantom: PhantomData }.run(&context);
131133

132134
result.expect("should succeed");
133135
verify_json!(context, RESOURCES_CONFIG_FILE_PATH, expected_final_resources_config_json());
@@ -169,15 +171,15 @@ mod check_keystore {
169171
fn passes_when_all_present() {
170172
let keystore_files = vec![
171173
format!("{CROSS_CHAIN_PREFIX}cross-chain-key"),
172-
format!("{AURA_PREFIX}aura-key"),
173-
format!("{GRANDPA_PREFIX}grandpa-key"),
174+
format!("{SR25_PREFIX}sr25-key"),
175+
format!("{ED25_PREFIX}ed25-key"),
174176
];
175177
#[rustfmt::skip]
176178
let context = MockIOContext::new().with_expected_io(vec![
177179
MockIO::list_dir(&keystore_path(), Some(keystore_files.clone())),
178180
]);
179181

180-
let result = check_keystore(&default_config(), &context);
182+
let result = check_keystore::<MockIOContext, MockRuntime>(&default_config(), &context);
181183

182184
result.expect("should succeed");
183185
}
@@ -186,16 +188,16 @@ mod check_keystore {
186188
fn fails_when_one_is_missing() {
187189
let keystore_files = vec![
188190
format!("{CROSS_CHAIN_PREFIX}cross-chain-key"),
189-
format!("{GRANDPA_PREFIX}grandpa-key"),
191+
format!("{ED25_PREFIX}grandpa-key"),
190192
];
191193
let context = MockIOContext::new().with_expected_io(vec![
192194
MockIO::list_dir(&keystore_path(), Some(keystore_files.clone())),
193195
MockIO::eprint(
194-
"⚠️ AURA key is missing from the keystore. Please run generate-keys wizard first.",
196+
"⚠️ TestAuraLike key is missing from the keystore. Please run generate-keys wizard first.",
195197
),
196198
]);
197199

198-
let result = check_keystore(&default_config(), &context);
200+
let result = check_keystore::<MockIOContext, MockRuntime>(&default_config(), &context);
199201
let result = result.expect("should succeed");
200202
assert!(!result);
201203
}

0 commit comments

Comments
 (0)