Skip to content

Commit 8886fe3

Browse files
committed
refactor: use the CardanoNetwork and magic IDs constants from mithril-client library in the tools utxo-hd snapshot-converter command in the client CLI
1 parent 902299b commit 8886fe3

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

mithril-client-cli/src/commands/tools/snapshot_converter.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use std::{
88
use anyhow::{Context, anyhow};
99
use clap::{Parser, ValueEnum};
1010

11-
use mithril_client::MithrilResult;
11+
use mithril_client::{
12+
MithrilError, MithrilResult,
13+
common::{CardanoNetwork, MagicId, PREPROD_MAGIC_ID, PREVIEW_MAGIC_ID},
14+
};
1215

1316
use crate::utils::{
1417
ArchiveUnpacker, GitHubReleaseRetriever, HttpDownloader, ReqwestGitHubApiClient,
@@ -34,10 +37,6 @@ const SNAPSHOT_CONVERTER_CONFIG_FILE: &str = "config.json";
3437
const LEDGER_DIR: &str = "ledger";
3538
const PROTOCOL_MAGIC_ID_FILE: &str = "protocolMagicId";
3639

37-
const MAINNET_MAGIC_ID: u32 = 764824073;
38-
const PREPROD_MAGIC_ID: u32 = 1;
39-
const PREVIEW_MAGIC_ID: u32 = 2;
40-
4140
const CONVERSION_FALLBACK_LIMIT: usize = 2;
4241

4342
#[derive(Debug, Clone, ValueEnum, Eq, PartialEq)]
@@ -58,13 +57,13 @@ impl fmt::Display for UTxOHDFlavor {
5857
}
5958

6059
#[derive(Debug, Clone, ValueEnum, Eq, PartialEq)]
61-
enum CardanoNetwork {
60+
enum CardanoNetworkCliArg {
6261
Preview,
6362
Preprod,
6463
Mainnet,
6564
}
6665

67-
impl fmt::Display for CardanoNetwork {
66+
impl fmt::Display for CardanoNetworkCliArg {
6867
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6968
match self {
7069
Self::Preview => write!(f, "preview"),
@@ -74,6 +73,23 @@ impl fmt::Display for CardanoNetwork {
7473
}
7574
}
7675

76+
impl TryFrom<CardanoNetwork> for CardanoNetworkCliArg {
77+
type Error = MithrilError;
78+
79+
fn try_from(network: CardanoNetwork) -> Result<Self, Self::Error> {
80+
match network {
81+
CardanoNetwork::MainNet => Ok(Self::Mainnet),
82+
CardanoNetwork::TestNet(magic_id) => match magic_id {
83+
PREVIEW_MAGIC_ID => Ok(Self::Preview),
84+
PREPROD_MAGIC_ID => Ok(Self::Preprod),
85+
_ => Err(anyhow!(
86+
"Cardano network not supported for ledger state snapshot conversion: {network:?}",
87+
)),
88+
},
89+
}
90+
}
91+
}
92+
7793
#[cfg_attr(test, mockall::automock)]
7894
trait SnapshotConverter {
7995
fn convert(&self, input_path: &Path, output_path: &Path) -> MithrilResult<()>;
@@ -133,7 +149,7 @@ pub struct SnapshotConverterCommand {
133149
since = "0.12.12",
134150
note = "optional: automatically detected from the protocolMagicId file"
135151
)]
136-
cardano_network: Option<CardanoNetwork>,
152+
cardano_network: Option<CardanoNetworkCliArg>,
137153

138154
/// UTxO-HD flavor to convert the ledger snapshot to (`Legacy` or `LMDB`).
139155
#[clap(long)]
@@ -279,7 +295,7 @@ impl SnapshotConverterCommand {
279295
work_dir: &Path,
280296
db_dir: &Path,
281297
distribution_dir: &Path,
282-
cardano_network: &CardanoNetwork,
298+
cardano_network: &CardanoNetworkCliArg,
283299
utxo_hd_flavor: &UTxOHDFlavor,
284300
commit: bool,
285301
) -> MithrilResult<()> {
@@ -374,7 +390,7 @@ impl SnapshotConverterCommand {
374390

375391
fn get_snapshot_converter_config_path(
376392
distribution_dir: &Path,
377-
network: &CardanoNetwork,
393+
network: &CardanoNetworkCliArg,
378394
) -> PathBuf {
379395
distribution_dir
380396
.join(SNAPSHOT_CONVERTER_CONFIG_DIR)
@@ -520,25 +536,21 @@ impl SnapshotConverterCommand {
520536
Ok(())
521537
}
522538

523-
fn detect_cardano_network(db_dir: &Path) -> MithrilResult<CardanoNetwork> {
539+
fn detect_cardano_network(db_dir: &Path) -> MithrilResult<CardanoNetworkCliArg> {
524540
let magic_id_path = db_dir.join(PROTOCOL_MAGIC_ID_FILE);
525541
let content = std::fs::read_to_string(&magic_id_path).with_context(|| {
526542
format!(
527543
"Failed to read protocolMagicId file: {}",
528544
magic_id_path.display()
529545
)
530546
})?;
531-
let id: u32 = content
547+
let id: MagicId = content
532548
.trim()
533549
.parse()
534550
.with_context(|| format!("Invalid protocolMagicId value: '{}'", content.trim()))?;
551+
let network = CardanoNetwork::from(id);
535552

536-
match id {
537-
MAINNET_MAGIC_ID => Ok(CardanoNetwork::Mainnet),
538-
PREPROD_MAGIC_ID => Ok(CardanoNetwork::Preprod),
539-
PREVIEW_MAGIC_ID => Ok(CardanoNetwork::Preview),
540-
_ => Err(anyhow!("Unknown protocolMagicId value: '{}'", id)),
541-
}
553+
CardanoNetworkCliArg::try_from(network)
542554
}
543555
}
544556

@@ -727,7 +739,7 @@ mod tests {
727739
#[test]
728740
fn returns_config_path_for_mainnet() {
729741
let distribution_dir = PathBuf::from("/path/to/distribution");
730-
let network = CardanoNetwork::Mainnet;
742+
let network = CardanoNetworkCliArg::Mainnet;
731743

732744
let config_path = SnapshotConverterCommand::get_snapshot_converter_config_path(
733745
&distribution_dir,
@@ -746,7 +758,7 @@ mod tests {
746758
#[test]
747759
fn returns_config_path_for_preprod() {
748760
let distribution_dir = PathBuf::from("/path/to/distribution");
749-
let network = CardanoNetwork::Preprod;
761+
let network = CardanoNetworkCliArg::Preprod;
750762

751763
let config_path = SnapshotConverterCommand::get_snapshot_converter_config_path(
752764
&distribution_dir,
@@ -765,7 +777,7 @@ mod tests {
765777
#[test]
766778
fn returns_config_path_for_preview() {
767779
let distribution_dir = PathBuf::from("/path/to/distribution");
768-
let network = CardanoNetwork::Preview;
780+
let network = CardanoNetworkCliArg::Preview;
769781

770782
let config_path = SnapshotConverterCommand::get_snapshot_converter_config_path(
771783
&distribution_dir,
@@ -973,9 +985,11 @@ mod tests {
973985
}
974986

975987
mod detect_cardano_network {
988+
use mithril_client::common::MAINNET_MAGIC_ID;
989+
976990
use super::*;
977991

978-
fn create_protocol_magic_id_file(db_dir: &Path, magic_id: u32) -> PathBuf {
992+
fn create_protocol_magic_id_file(db_dir: &Path, magic_id: MagicId) -> PathBuf {
979993
let file_path = db_dir.join(PROTOCOL_MAGIC_ID_FILE);
980994
std::fs::write(&file_path, magic_id.to_string()).unwrap();
981995

@@ -989,7 +1003,7 @@ mod tests {
9891003

9901004
let network = SnapshotConverterCommand::detect_cardano_network(&db_dir).unwrap();
9911005

992-
assert_eq!(network, CardanoNetwork::Mainnet);
1006+
assert_eq!(network, CardanoNetworkCliArg::Mainnet);
9931007
}
9941008

9951009
#[test]
@@ -999,7 +1013,7 @@ mod tests {
9991013

10001014
let network = SnapshotConverterCommand::detect_cardano_network(&db_dir).unwrap();
10011015

1002-
assert_eq!(network, CardanoNetwork::Preprod);
1016+
assert_eq!(network, CardanoNetworkCliArg::Preprod);
10031017
}
10041018

10051019
#[test]
@@ -1009,7 +1023,7 @@ mod tests {
10091023

10101024
let network = SnapshotConverterCommand::detect_cardano_network(&db_dir).unwrap();
10111025

1012-
assert_eq!(network, CardanoNetwork::Preview);
1026+
assert_eq!(network, CardanoNetworkCliArg::Preview);
10131027
}
10141028

10151029
#[test]

mithril-client/src/type_alias.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ pub mod common {
7070
pub use mithril_common::entities::{
7171
AncillaryLocation, BlockHash, BlockNumber, CardanoDbBeacon, CardanoNetwork, ChainPoint,
7272
CompressionAlgorithm, DigestLocation, Epoch, ImmutableFileNumber, ImmutablesLocation,
73-
MultiFilesUri, ProtocolMessage, ProtocolMessagePartKey, ProtocolParameters,
74-
SignedEntityType, SlotNumber, StakeDistribution, SupportedEra, TemplateUri,
75-
TransactionHash,
73+
MAINNET_MAGIC_ID, MagicId, MultiFilesUri, PREPROD_MAGIC_ID, PREVIEW_MAGIC_ID,
74+
ProtocolMessage, ProtocolMessagePartKey, ProtocolParameters, SignedEntityType, SlotNumber,
75+
StakeDistribution, SupportedEra, TemplateUri, TransactionHash,
7676
};
7777
pub use mithril_common::messages::{
7878
AncillaryMessagePart, DigestsMessagePart, ImmutablesMessagePart,

mithril-common/src/entities/cardano_network.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ use thiserror::Error;
44

55
use crate::{MagicId, StdResult};
66

7-
const MAINNET_MAGIC_ID: MagicId = 764824073;
8-
const PREPROD_MAGIC_ID: MagicId = 1;
9-
const PREVIEW_MAGIC_ID: MagicId = 2;
7+
/// Mainnet magic ID
8+
pub const MAINNET_MAGIC_ID: MagicId = 764824073;
9+
/// Preprod magic ID
10+
pub const PREPROD_MAGIC_ID: MagicId = 1;
11+
/// Preview magic ID
12+
pub const PREVIEW_MAGIC_ID: MagicId = 2;
13+
/// Devnet magic ID
1014
pub(crate) const DEVNET_MAGIC_ID: MagicId = 42;
1115

1216
#[derive(Error, Debug)]

mithril-common/src/entities/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub use cardano_database::{
4040
ImmutablesLocations,
4141
};
4242
pub use cardano_db_beacon::CardanoDbBeacon;
43-
pub use cardano_network::CardanoNetwork;
4443
pub(crate) use cardano_network::DEVNET_MAGIC_ID;
44+
pub use cardano_network::{CardanoNetwork, MAINNET_MAGIC_ID, PREPROD_MAGIC_ID, PREVIEW_MAGIC_ID};
4545
pub use cardano_stake_distribution::CardanoStakeDistribution;
4646
pub use cardano_transaction::{CardanoTransaction, TransactionHash};
4747
pub use cardano_transactions_set_proof::CardanoTransactionsSetProof;

0 commit comments

Comments
 (0)