Skip to content

Commit b3a3eb4

Browse files
committed
node-data: better error message on failure to migrate consensus keys to new file format
1 parent 27f6696 commit b3a3eb4

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

node-data/src/bls.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,42 @@ fn read_from_file(
200200

201201
if file_format_is_old {
202202
info!("Your consensus keys are in the old format. Migrating to the new format and saving the old file as {}.old", path.display());
203-
save_old_file(&path)?;
204-
let keys_filename = path
205-
.file_name()
206-
.expect("keys file should have a name")
207-
.to_str()
208-
.expect("keys file should be a valid string");
209-
let keys_file_dir = path
210-
.parent()
211-
.expect("keys file should have a parent directory");
212-
let temp_keys_name = format!("{}_new", keys_filename);
213-
save_consensus_keys(keys_file_dir, &temp_keys_name, &pk, &sk, pwd)?;
214-
fs::rename(
215-
keys_file_dir.join(&temp_keys_name).with_extension("keys"),
216-
&path,
217-
)?;
218-
fs::remove_file(
219-
keys_file_dir.join(temp_keys_name).with_extension("cpk"),
220-
)
221-
.expect("The new cpk file should be deleted");
203+
migrate_file_to_new_format(&path, &pk, &sk, pwd).map_err(|e| {
204+
anyhow::anyhow!(
205+
"failed to migrate consensus keys to the new format: {e}"
206+
)
207+
})?;
222208
}
223209

224210
Ok((pk, sk))
225211
}
226212

213+
fn migrate_file_to_new_format(
214+
path: &Path,
215+
pk: &BlsPublicKey,
216+
sk: &BlsSecretKey,
217+
pwd: &str,
218+
) -> Result<(), ConsensusKeysError> {
219+
save_old_file(path)?;
220+
let keys_filename = path
221+
.file_name()
222+
.expect("keys file should have a name")
223+
.to_str()
224+
.expect("keys file should be a valid string");
225+
let keys_file_dir = path
226+
.parent()
227+
.expect("keys file should have a parent directory");
228+
let temp_keys_name = format!("{}_new", keys_filename);
229+
save_consensus_keys(keys_file_dir, &temp_keys_name, pk, sk, pwd)?;
230+
fs::rename(
231+
keys_file_dir.join(&temp_keys_name).with_extension("keys"),
232+
path,
233+
)?;
234+
fs::remove_file(keys_file_dir.join(temp_keys_name).with_extension("cpk"))
235+
.expect("The new cpk file should be deleted");
236+
Ok(())
237+
}
238+
227239
fn save_old_file(path: &Path) -> Result<(), ConsensusKeysError> {
228240
let old_path = path.with_extension("keys.old");
229241
fs::copy(path, old_path)?;

0 commit comments

Comments
 (0)