1313// limitations under the License.
1414
1515use amaru_kernel:: {
16- Bound , EraHistory , EraParams , HEADER_HASH_SIZE , Hash , Nonce , Summary , cbor,
16+ Bound , EraHistory , EraParams , HEADER_HASH_SIZE , Hash , Nonce , Point , Summary , cbor,
1717 network:: NetworkName ,
1818} ;
19- use amaru_ouroboros_traits:: Nonces ;
2019use clap:: Parser ;
2120use std:: path:: { Path , PathBuf } ;
2221use tokio:: fs:: { self } ;
22+ use tracing:: { debug, info} ;
23+
24+ use crate :: cmd:: import_nonces:: InitialNonces ;
2325
2426#[ derive( Debug , Parser ) ]
2527pub struct Args {
2628 /// Path to the CBOR encoded ledger state snapshot as serialised by Haskell
2729 /// node.
28- #[ arg( long, value_name = "SNAPSHOT" , verbatim_doc_comment, num_args ( 0 .. ) ) ]
29- snapshot : Vec < PathBuf > ,
30+ #[ arg( long, value_name = "SNAPSHOT" , verbatim_doc_comment) ]
31+ snapshot : PathBuf ,
3032
3133 /// Directory to store converted snapshots into.
3234 ///
@@ -55,10 +57,7 @@ pub enum Error {
5557
5658pub ( crate ) async fn run ( args : Args ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
5759 let target_dir = args. target_dir . unwrap_or ( PathBuf :: from ( "." ) ) ;
58- for snapshot in args. snapshot {
59- convert_one_snapshot_file ( & target_dir, & snapshot, & args. network ) . await ?;
60- }
61-
60+ convert_one_snapshot_file ( & target_dir, & args. snapshot , & args. network ) . await ?;
6261 Ok ( ( ) )
6362}
6463
@@ -76,7 +75,12 @@ async fn convert_one_snapshot_file(
7675
7776 fs:: create_dir_all ( target_dir) . await ?;
7877
79- convert_snapshot_to ( snapshot, target_dir, network) . await
78+ let converted = convert_snapshot_to ( snapshot, target_dir, network) . await ?;
79+ info ! (
80+ "converted ledger state from {:?} to {:?}" ,
81+ snapshot, converted
82+ ) ;
83+ Ok ( converted)
8084}
8185
8286async fn convert_snapshot_to (
@@ -118,7 +122,8 @@ async fn convert_snapshot_to(
118122 slot_length : 1000 ,
119123 } ,
120124 } ) ;
121- let history = EraHistory :: new ( & eras, network. default_stability_window ( ) ) ;
125+
126+ let era_history = EraHistory :: new ( & eras, network. default_stability_window ( ) ) ;
122127
123128 // ledger state
124129 // https://github.com/abailly/ouroboros-consensus/blob/1508638f832772d21874e18e48b908fcb791cd49/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Ledger.hs#L736
@@ -160,7 +165,7 @@ async fn convert_snapshot_to(
160165 d. array ( ) ?;
161166 // NOTE: The encoding of an AnnTip is not consistent with the encoding of a Tip
162167 let tip_slot = d. u64 ( ) ?;
163- let _tip_hash : Hash < HEADER_HASH_SIZE > = d. decode ( ) ?;
168+ let tip_hash : Hash < HEADER_HASH_SIZE > = d. decode ( ) ?;
164169 let _tip_height = d. u64 ( ) ?;
165170
166171 // ChainDepState for Praos
@@ -210,26 +215,37 @@ async fn convert_snapshot_to(
210215 d. u8 ( ) ?;
211216 let active: Nonce = d. decode ( ) ?;
212217
213- d. array ( ) ?;
214- d. u8 ( ) ?;
215- let tail = d. decode ( ) ?;
218+ // lab nonce
219+ d. skip ( ) ?;
216220
217- // previous epoch nonce, can be defined or 0
218- // FIXME: do we use it?
221+ // last epoch nonce
219222 d. skip ( ) ?;
220223
221- let nonces = Nonces {
224+ let nonces = InitialNonces {
225+ at : Point :: Specific ( tip_slot, tip_hash. to_vec ( ) ) ,
222226 active,
223227 evolving,
224228 candidate,
225- epoch : history. slot_to_epoch ( tip_slot. into ( ) , tip_slot. into ( ) ) ?,
226- tail,
229+ tail : Hash :: new ( [ 0 ; 32 ] ) ,
227230 } ;
228231
229232 write_nonces ( target_dir, slot, hash, nonces) . await ?;
233+ write_era_history ( target_dir, slot, hash, & era_history) . await ?;
230234 write_ledger_snapshot ( target_dir, slot, hash, & bytes[ begin..end] ) . await
231235}
232236
237+ async fn write_era_history (
238+ target_dir : & Path ,
239+ slot : u64 ,
240+ hash : Hash < 32 > ,
241+ era_history : & EraHistory ,
242+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
243+ let target_path = target_dir. join ( format ! ( "history.{}.{}.json" , slot, hash) ) ;
244+ fs:: write ( & target_path, serde_json:: to_string ( era_history) ?) . await ?;
245+ debug ! ( "wrote era history {:?}" , target_path) ;
246+ Ok ( ( ) )
247+ }
248+
233249/// This is the number of past eras before the current era in the "standard" Cardano history, e.g
234250/// from Byron to Babbage. Bump this number when a hard fork happens.
235251pub const PAST_ERAS_NUMBER : i32 = 6 ;
@@ -287,10 +303,11 @@ async fn write_nonces(
287303 target_dir : & Path ,
288304 slot : u64 ,
289305 hash : Hash < HEADER_HASH_SIZE > ,
290- nonces : Nonces ,
306+ nonces : InitialNonces ,
291307) -> Result < ( ) , Box < dyn std:: error:: Error > > {
292308 let target_path = target_dir. join ( format ! ( "nonces.{}.{}.json" , slot, hash) ) ;
293309 fs:: write ( & target_path, serde_json:: to_string ( & nonces) ?) . await ?;
310+ debug ! ( "wrote nonces file {:?}" , target_path) ;
294311 Ok ( ( ) )
295312}
296313
@@ -302,6 +319,7 @@ async fn write_ledger_snapshot(
302319) -> Result < PathBuf , Box < dyn std:: error:: Error > > {
303320 let target_path = target_dir. join ( format ! ( "{}.{}.cbor" , slot, hash) ) ;
304321 fs:: write ( & target_path, ledger_data) . await ?;
322+ debug ! ( "wrote ledger snapshot {:?}" , target_path) ;
305323 Ok ( target_path)
306324}
307325
@@ -326,7 +344,7 @@ mod test {
326344 }
327345
328346 #[ tokio:: test]
329- async fn generates_converted_snapshots_in_given_target_dir ( ) {
347+ async fn generates_converted_snapshot_in_given_target_dir ( ) {
330348 let network = NetworkName :: Testnet ( 42 ) ;
331349 let tempdir = tempfile:: tempdir ( ) . unwrap ( ) ;
332350 let expected_paths = vec ! [
@@ -341,15 +359,19 @@ mod test {
341359 ) ,
342360 ] ;
343361
344- let args = super :: Args {
345- snapshot : dir_content ( Path :: new ( "tests/data/convert" ) ) . await . unwrap ( ) ,
346- target_dir : Some ( tempdir. path ( ) . to_path_buf ( ) ) ,
347- network,
348- } ;
362+ let snapshots = dir_content ( Path :: new ( "tests/data/convert" ) ) . await . unwrap ( ) ;
349363
350- run ( args)
351- . await
352- . expect ( "unexpected error in conversion test" ) ;
364+ for snapshot in snapshots {
365+ let args = super :: Args {
366+ snapshot,
367+ target_dir : Some ( tempdir. path ( ) . to_path_buf ( ) ) ,
368+ network,
369+ } ;
370+
371+ run ( args)
372+ . await
373+ . expect ( "unexpected error in conversion test" ) ;
374+ }
353375
354376 assert ! (
355377 expected_paths. iter( ) . all( |p| p. exists( ) ) ,
@@ -389,15 +411,19 @@ mod test {
389411 ) ,
390412 ] ;
391413
392- let args = super :: Args {
393- snapshot : dir_content ( Path :: new ( "tests/data/convert" ) ) . await . unwrap ( ) ,
394- target_dir : Some ( target_dir. clone ( ) ) ,
395- network,
396- } ;
414+ let snapshots = dir_content ( Path :: new ( "tests/data/convert" ) ) . await . unwrap ( ) ;
397415
398- run ( args)
399- . await
400- . expect ( "unexpected error in conversion test" ) ;
416+ for snapshot in snapshots {
417+ let args = super :: Args {
418+ snapshot,
419+ target_dir : Some ( target_dir. clone ( ) ) ,
420+ network,
421+ } ;
422+
423+ run ( args)
424+ . await
425+ . expect ( "unexpected error in conversion test" ) ;
426+ }
401427
402428 assert ! (
403429 expected_paths. iter( ) . all( |p| p. exists( ) ) ,
0 commit comments