@@ -8,7 +8,10 @@ use std::{
8
8
use anyhow:: { Context , anyhow} ;
9
9
use clap:: { Parser , ValueEnum } ;
10
10
11
- use mithril_client:: MithrilResult ;
11
+ use mithril_client:: {
12
+ MithrilError , MithrilResult ,
13
+ common:: { CardanoNetwork , MagicId , PREPROD_MAGIC_ID , PREVIEW_MAGIC_ID } ,
14
+ } ;
12
15
13
16
use crate :: utils:: {
14
17
ArchiveUnpacker , GitHubReleaseRetriever , HttpDownloader , ReqwestGitHubApiClient ,
@@ -34,10 +37,6 @@ const SNAPSHOT_CONVERTER_CONFIG_FILE: &str = "config.json";
34
37
const LEDGER_DIR : & str = "ledger" ;
35
38
const PROTOCOL_MAGIC_ID_FILE : & str = "protocolMagicId" ;
36
39
37
- const MAINNET_MAGIC_ID : u32 = 764824073 ;
38
- const PREPROD_MAGIC_ID : u32 = 1 ;
39
- const PREVIEW_MAGIC_ID : u32 = 2 ;
40
-
41
40
const CONVERSION_FALLBACK_LIMIT : usize = 2 ;
42
41
43
42
#[ derive( Debug , Clone , ValueEnum , Eq , PartialEq ) ]
@@ -58,13 +57,13 @@ impl fmt::Display for UTxOHDFlavor {
58
57
}
59
58
60
59
#[ derive( Debug , Clone , ValueEnum , Eq , PartialEq ) ]
61
- enum CardanoNetwork {
60
+ enum CardanoNetworkCliArg {
62
61
Preview ,
63
62
Preprod ,
64
63
Mainnet ,
65
64
}
66
65
67
- impl fmt:: Display for CardanoNetwork {
66
+ impl fmt:: Display for CardanoNetworkCliArg {
68
67
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
69
68
match self {
70
69
Self :: Preview => write ! ( f, "preview" ) ,
@@ -74,6 +73,23 @@ impl fmt::Display for CardanoNetwork {
74
73
}
75
74
}
76
75
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
+
77
93
#[ cfg_attr( test, mockall:: automock) ]
78
94
trait SnapshotConverter {
79
95
fn convert ( & self , input_path : & Path , output_path : & Path ) -> MithrilResult < ( ) > ;
@@ -133,7 +149,7 @@ pub struct SnapshotConverterCommand {
133
149
since = "0.12.12" ,
134
150
note = "optional: automatically detected from the protocolMagicId file"
135
151
) ]
136
- cardano_network : Option < CardanoNetwork > ,
152
+ cardano_network : Option < CardanoNetworkCliArg > ,
137
153
138
154
/// UTxO-HD flavor to convert the ledger snapshot to (`Legacy` or `LMDB`).
139
155
#[ clap( long) ]
@@ -279,7 +295,7 @@ impl SnapshotConverterCommand {
279
295
work_dir : & Path ,
280
296
db_dir : & Path ,
281
297
distribution_dir : & Path ,
282
- cardano_network : & CardanoNetwork ,
298
+ cardano_network : & CardanoNetworkCliArg ,
283
299
utxo_hd_flavor : & UTxOHDFlavor ,
284
300
commit : bool ,
285
301
) -> MithrilResult < ( ) > {
@@ -374,7 +390,7 @@ impl SnapshotConverterCommand {
374
390
375
391
fn get_snapshot_converter_config_path (
376
392
distribution_dir : & Path ,
377
- network : & CardanoNetwork ,
393
+ network : & CardanoNetworkCliArg ,
378
394
) -> PathBuf {
379
395
distribution_dir
380
396
. join ( SNAPSHOT_CONVERTER_CONFIG_DIR )
@@ -520,25 +536,21 @@ impl SnapshotConverterCommand {
520
536
Ok ( ( ) )
521
537
}
522
538
523
- fn detect_cardano_network ( db_dir : & Path ) -> MithrilResult < CardanoNetwork > {
539
+ fn detect_cardano_network ( db_dir : & Path ) -> MithrilResult < CardanoNetworkCliArg > {
524
540
let magic_id_path = db_dir. join ( PROTOCOL_MAGIC_ID_FILE ) ;
525
541
let content = std:: fs:: read_to_string ( & magic_id_path) . with_context ( || {
526
542
format ! (
527
543
"Failed to read protocolMagicId file: {}" ,
528
544
magic_id_path. display( )
529
545
)
530
546
} ) ?;
531
- let id: u32 = content
547
+ let id: MagicId = content
532
548
. trim ( )
533
549
. parse ( )
534
550
. with_context ( || format ! ( "Invalid protocolMagicId value: '{}'" , content. trim( ) ) ) ?;
551
+ let network = CardanoNetwork :: from ( id) ;
535
552
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)
542
554
}
543
555
}
544
556
@@ -727,7 +739,7 @@ mod tests {
727
739
#[ test]
728
740
fn returns_config_path_for_mainnet ( ) {
729
741
let distribution_dir = PathBuf :: from ( "/path/to/distribution" ) ;
730
- let network = CardanoNetwork :: Mainnet ;
742
+ let network = CardanoNetworkCliArg :: Mainnet ;
731
743
732
744
let config_path = SnapshotConverterCommand :: get_snapshot_converter_config_path (
733
745
& distribution_dir,
@@ -746,7 +758,7 @@ mod tests {
746
758
#[ test]
747
759
fn returns_config_path_for_preprod ( ) {
748
760
let distribution_dir = PathBuf :: from ( "/path/to/distribution" ) ;
749
- let network = CardanoNetwork :: Preprod ;
761
+ let network = CardanoNetworkCliArg :: Preprod ;
750
762
751
763
let config_path = SnapshotConverterCommand :: get_snapshot_converter_config_path (
752
764
& distribution_dir,
@@ -765,7 +777,7 @@ mod tests {
765
777
#[ test]
766
778
fn returns_config_path_for_preview ( ) {
767
779
let distribution_dir = PathBuf :: from ( "/path/to/distribution" ) ;
768
- let network = CardanoNetwork :: Preview ;
780
+ let network = CardanoNetworkCliArg :: Preview ;
769
781
770
782
let config_path = SnapshotConverterCommand :: get_snapshot_converter_config_path (
771
783
& distribution_dir,
@@ -973,9 +985,11 @@ mod tests {
973
985
}
974
986
975
987
mod detect_cardano_network {
988
+ use mithril_client:: common:: MAINNET_MAGIC_ID ;
989
+
976
990
use super :: * ;
977
991
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 {
979
993
let file_path = db_dir. join ( PROTOCOL_MAGIC_ID_FILE ) ;
980
994
std:: fs:: write ( & file_path, magic_id. to_string ( ) ) . unwrap ( ) ;
981
995
@@ -989,7 +1003,7 @@ mod tests {
989
1003
990
1004
let network = SnapshotConverterCommand :: detect_cardano_network ( & db_dir) . unwrap ( ) ;
991
1005
992
- assert_eq ! ( network, CardanoNetwork :: Mainnet ) ;
1006
+ assert_eq ! ( network, CardanoNetworkCliArg :: Mainnet ) ;
993
1007
}
994
1008
995
1009
#[ test]
@@ -999,7 +1013,7 @@ mod tests {
999
1013
1000
1014
let network = SnapshotConverterCommand :: detect_cardano_network ( & db_dir) . unwrap ( ) ;
1001
1015
1002
- assert_eq ! ( network, CardanoNetwork :: Preprod ) ;
1016
+ assert_eq ! ( network, CardanoNetworkCliArg :: Preprod ) ;
1003
1017
}
1004
1018
1005
1019
#[ test]
@@ -1009,7 +1023,7 @@ mod tests {
1009
1023
1010
1024
let network = SnapshotConverterCommand :: detect_cardano_network ( & db_dir) . unwrap ( ) ;
1011
1025
1012
- assert_eq ! ( network, CardanoNetwork :: Preview ) ;
1026
+ assert_eq ! ( network, CardanoNetworkCliArg :: Preview ) ;
1013
1027
}
1014
1028
1015
1029
#[ test]
0 commit comments