Skip to content

Commit b518140

Browse files
committed
fix: restore original generate-keys flow without temp chain spec
- Replace persistent chain spec with original temp chain spec approach - Traditional generate-keys (no --url) now works as it did on master - Maintain new automatic key generation when --url is provided - Fix all tests to use temporary directory paths
1 parent c8e5672 commit b518140

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl<T: PartnerChainRuntime> CmdRun for GenerateKeysCmd<T> {
9898
context.eprint("It will also generate a network key for your node if needed.");
9999
context.enewline();
100100

101+
// Create a proper temporary chain spec as it was in master
101102
let chain_spec_path = write_temp_chain_spec(
102103
context,
103104
T::create_chain_spec(&CreateChainSpecConfig::<T::Keys>::default()),
@@ -121,15 +122,6 @@ impl<T: PartnerChainRuntime> CmdRun for GenerateKeysCmd<T> {
121122
}
122123
}
123124

124-
fn write_temp_chain_spec<C: IOContext>(context: &C, chain_spec: serde_json::Value) -> String {
125-
let dir_path = context.new_tmp_dir();
126-
let dir_path = dir_path.to_str().expect("temp dir path is correct utf-8");
127-
let path = format!("{dir_path}/chain-spec.json");
128-
let content = format!("{chain_spec}");
129-
context.write_file(&path, &content);
130-
path
131-
}
132-
133125
pub(crate) fn generate_spo_keys<C: IOContext, T: PartnerChainRuntime>(
134126
config: &GenerateKeysConfig,
135127
chain_spec_path: &str,
@@ -147,12 +139,18 @@ pub(crate) fn generate_spo_keys<C: IOContext, T: PartnerChainRuntime>(
147139
keys.insert(key_definition.key_type.to_owned(), generated_key);
148140
}
149141

150-
let permissioned_keys = PermissionedCandidateKeys { partner_chains_key, keys };
151-
152-
// Use the shared function to save to JSON file
153-
save_permissioned_keys_to_json_file(&permissioned_keys, context)?;
154-
155-
// The save_permissioned_keys_to_json_file function already handles the output
142+
let public_keys_json =
143+
serde_json::to_string_pretty(&PermissionedCandidateKeys { partner_chains_key, keys })
144+
.expect("PermissionedCandidateKeys have only UTF-8 encodable ids");
145+
context.write_file(KEYS_FILE_PATH, &public_keys_json);
146+
147+
context.eprint(&format!(
148+
"🔑 The following public keys were generated and saved to the {} file:",
149+
KEYS_FILE_PATH,
150+
));
151+
context.print(&(public_keys_json).to_string());
152+
context.eprint("You may share them with your chain governance authority");
153+
context.eprint("if you wish to be included as a permissioned candidate.");
156154
} else {
157155
context.eprint("Refusing to overwrite keys file - skipping");
158156
}
@@ -583,3 +581,12 @@ async fn send_rpc_request<T: for<'de> Deserialize<'de>>(
583581

584582
response.result.ok_or_else(|| "No result in response".into())
585583
}
584+
585+
fn write_temp_chain_spec<C: IOContext>(context: &C, chain_spec: serde_json::Value) -> String {
586+
let dir_path = context.new_tmp_dir();
587+
let dir_path = dir_path.to_str().expect("temp dir path is correct utf-8");
588+
let path = format!("{dir_path}/chain-spec.json");
589+
let content = format!("{chain_spec}");
590+
context.write_file(&path, &content);
591+
path
592+
}

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,33 @@ pub mod scenarios {
157157
MockIO::eprint("if you wish to be included as a permissioned candidate."),
158158
])
159159
}
160-
161-
pub fn create_temp_chain_spec() -> MockIO {
162-
MockIO::Group(vec![MockIO::new_tmp_dir()])
163-
}
164160
}
165161

166162
#[test]
167163
fn happy_path() {
168-
let mock_context = MockIOContext::new().with_expected_io(vec![
169-
scenarios::show_intro(),
170-
MockIO::enewline(),
171-
scenarios::create_temp_chain_spec(),
172-
scenarios::prompt_all_config_fields(),
173-
MockIO::enewline(),
174-
scenarios::generate_all_spo_keys(AURA_KEY, GRANDPA_KEY, CROSS_CHAIN_KEY),
175-
scenarios::write_key_file(AURA_KEY, GRANDPA_KEY, CROSS_CHAIN_KEY),
176-
MockIO::enewline(),
177-
scenarios::generate_network_key(),
178-
MockIO::enewline(),
179-
MockIO::eprint("🚀 All done!"),
180-
MockIO::delete_file("/tmp/MockIOContext_tmp_dir/chain-spec.json"),
181-
]);
164+
let mock_context = MockIOContext::new()
165+
.with_json_file(
166+
RESOURCES_CONFIG_FILE_PATH,
167+
serde_json::json!({
168+
"substrate_node_base_path": DATA_PATH,
169+
}),
170+
)
171+
.with_expected_io(vec![
172+
scenarios::show_intro(),
173+
MockIO::enewline(),
174+
MockIO::new_tmp_dir(),
175+
MockIO::eprint(&format!(
176+
"🛠️ Loaded node base path from config ({RESOURCES_CONFIG_FILE_PATH}): {DATA_PATH}"
177+
)),
178+
MockIO::enewline(),
179+
scenarios::generate_all_spo_keys(AURA_KEY, GRANDPA_KEY, CROSS_CHAIN_KEY),
180+
scenarios::write_key_file(AURA_KEY, GRANDPA_KEY, CROSS_CHAIN_KEY),
181+
MockIO::enewline(),
182+
scenarios::generate_network_key(),
183+
MockIO::enewline(),
184+
MockIO::eprint("🚀 All done!"),
185+
MockIO::delete_file("/tmp/MockIOContext_tmp_dir/chain-spec.json"),
186+
]);
182187

183188
let result =
184189
GenerateKeysCmd::<MockRuntime> {
@@ -329,14 +334,14 @@ mod generate_network_key {
329334
MockIO::run_command(&format!("mkdir -p {DATA_PATH}/network"), "irrelevant"),
330335
MockIO::run_command(
331336
&format!(
332-
"<mock executable> key generate-node-key --chain path/to/chain-spec.json --file {}",
333-
network_key_file()
337+
"<mock executable> key generate-node-key --chain {}/chain_spec.json --file {}",
338+
DATA_PATH, network_key_file()
334339
),
335340
"irrelevant",
336341
),
337342
]);
338343

339-
let result = generate_network_key(&default_config(), "path/to/chain-spec.json", &context);
344+
let result = generate_network_key(&default_config(), &format!("{}/chain_spec.json", DATA_PATH), &context);
340345

341346
assert!(result.is_ok());
342347
}
@@ -353,7 +358,7 @@ mod generate_network_key {
353358
),
354359
]);
355360

356-
let result = generate_network_key(&default_config(), "irrelevant.json", &context);
361+
let result = generate_network_key(&default_config(), &format!("{}/chain_spec.json", DATA_PATH), &context);
357362

358363
assert!(result.is_ok());
359364
}
@@ -373,14 +378,14 @@ mod generate_network_key {
373378
MockIO::run_command(&format!("mkdir -p {DATA_PATH}/network"), "irrelevant"),
374379
MockIO::run_command(
375380
&format!(
376-
"<mock executable> key generate-node-key --chain path/to/chain-spec.json --file {}",
377-
network_key_file()
381+
"<mock executable> key generate-node-key --chain {}/chain_spec.json --file {}",
382+
DATA_PATH, network_key_file()
378383
),
379384
"irrelevant",
380385
),
381386
]);
382387

383-
let result = generate_network_key(&default_config(), "path/to/chain-spec.json", &context);
388+
let result = generate_network_key(&default_config(), &format!("{}/chain_spec.json", DATA_PATH), &context);
384389

385390
assert!(result.is_ok());
386391
}

0 commit comments

Comments
 (0)