@@ -6,7 +6,6 @@ use anyhow::bail;
66use c509_certificate:: c509:: C509 ;
77use cardano_blockchain_types:: hashes:: Blake2b256Hash ;
88use ed25519_dalek:: VerifyingKey ;
9- use pallas:: ledger:: traverse:: MultiEraTx ;
109use tracing:: { error, warn} ;
1110use uuid:: Uuid ;
1211use x509_cert:: certificate:: Certificate as X509Certificate ;
@@ -17,6 +16,7 @@ use crate::cardano::cip509::{
1716} ;
1817
1918/// Registration chains.
19+ #[ derive( Debug ) ]
2020pub struct RegistrationChain {
2121 /// Inner part of the registration chain.
2222 inner : Arc < RegistrationChainInner > ,
@@ -28,15 +28,12 @@ impl RegistrationChain {
2828 ///
2929 /// # Arguments
3030 /// - `cip509` - The CIP509.
31- /// - `tracking_payment_keys` - The list of payment keys to track.
32- /// - `point` - The point (slot) of the transaction.
33- /// - `tx_idx` - The transaction index.
3431 ///
3532 /// # Errors
3633 ///
3734 /// Returns an error if data is invalid
38- pub fn new ( txn : & MultiEraTx , cip509 : Cip509 ) -> anyhow:: Result < Self > {
39- let inner = RegistrationChainInner :: new ( cip509, txn ) ?;
35+ pub fn new ( cip509 : Cip509 ) -> anyhow:: Result < Self > {
36+ let inner = RegistrationChainInner :: new ( cip509) ?;
4037
4138 Ok ( Self {
4239 inner : Arc :: new ( inner) ,
@@ -111,7 +108,7 @@ impl RegistrationChain {
111108}
112109
113110/// Inner structure of registration chain.
114- #[ derive( Clone ) ]
111+ #[ derive( Debug , Clone ) ]
115112struct RegistrationChainInner {
116113 /// The current transaction ID hash (32 bytes)
117114 current_tx_id_hash : Blake2b256Hash ,
@@ -143,21 +140,18 @@ impl RegistrationChainInner {
143140 ///
144141 /// # Arguments
145142 /// - `cip509` - The CIP509.
146- /// - `tracking_payment_keys` - The list of payment keys to track.
147- /// - `point` - The point (slot) of the transaction.
148- /// - `tx_idx` - The transaction index.
149- /// - `txn` - The transaction.
150143 ///
151144 /// # Errors
152145 ///
153146 /// Returns an error if data is invalid
154- fn new ( cip509 : Cip509 , txn : & MultiEraTx ) -> anyhow:: Result < Self > {
147+ fn new ( cip509 : Cip509 ) -> anyhow:: Result < Self > {
155148 // Should be chain root, return immediately if not
156149 if cip509. previous_transaction ( ) . is_some ( ) {
157150 bail ! ( "Invalid chain root, previous transaction ID should be None." ) ;
158151 }
159152
160153 let point_tx_idx = cip509. origin ( ) . clone ( ) ;
154+ let current_tx_id_hash = cip509. txn_hash ( ) ;
161155 let ( purpose, registration, payment_history) = match cip509. consume ( ) {
162156 Ok ( v) => v,
163157 Err ( e) => {
@@ -168,23 +162,22 @@ impl RegistrationChainInner {
168162 } ;
169163
170164 let purpose = vec ! [ purpose] ;
171-
172165 let certificate_uris = registration. certificate_uris ;
173- let x509_cert_map = chain_root_x509_certs ( registration. x509_certs , & point_tx_idx) ;
174- let c509_cert_map = chain_root_c509_certs ( registration. c509_certs , & point_tx_idx) ;
175- let public_key_map = chain_root_public_keys ( registration. pub_keys , & point_tx_idx) ;
166+ let x509_certs = chain_root_x509_certs ( registration. x509_certs , & point_tx_idx) ;
167+ let c509_certs = chain_root_c509_certs ( registration. c509_certs , & point_tx_idx) ;
168+ let simple_keys = chain_root_public_keys ( registration. pub_keys , & point_tx_idx) ;
176169 let revocations = revocations_list ( registration. revocation_list , & point_tx_idx) ;
177- let role_data_map = chain_root_role_data ( registration. role_data , & point_tx_idx) ;
170+ let role_data = chain_root_role_data ( registration. role_data , & point_tx_idx) ;
178171
179172 Ok ( Self {
180173 purpose,
181- current_tx_id_hash : txn . hash ( ) . into ( ) ,
182- x509_certs : x509_cert_map ,
183- c509_certs : c509_cert_map ,
174+ current_tx_id_hash,
175+ x509_certs,
176+ c509_certs,
184177 certificate_uris,
185- simple_keys : public_key_map ,
178+ simple_keys,
186179 revocations,
187- role_data : role_data_map ,
180+ role_data,
188181 payment_history,
189182 } )
190183 }
@@ -417,87 +410,51 @@ fn update_role_data(
417410
418411#[ cfg( test) ]
419412mod test {
420- // TODO: FIXME:
421- // let block = test_block_1();
422- //
423- // TODO: FIXME: chain from 4 blocks, one with error!
424- // use super::*;
425- //
426- //
427- // fn conway_1() -> Vec<u8> {
428- // hex::decode(include_str!("../../test_data/cardano/conway_1.block"))
429- // .expect("Failed to decode hex block.")
430- // }
431- //
432- // fn conway_4() -> Vec<u8> {
433- // hex::decode(include_str!("../../test_data/cardano/conway_4.block"))
434- // .expect("Failed to decode hex block.")
435- // }
436-
437- // // TODO: FIXME:
438- // #[test]
439- // fn test_new_and_update_registration() {
440- // let conway_block_data_1 = conway_1();
441- // let point_1 = Point::new(
442- // 77_429_134,
443- // hex::decode("
444- // 62483f96613b4c48acd28de482eb735522ac180df61766bdb476a7bf83e7bb98")
445- // .unwrap(), );
446- // let multi_era_block_1 =
447- // pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data_1)
448- // .expect("Failed to decode MultiEraBlock");
449- //
450- // let cip509_1 = Cip509::new(&multi_era_block_1, 3.into())
451- // .expect("Failed to decode Cip509")
452- // .unwrap();
453- // assert!(
454- // !cip509_1.report().is_problematic(),
455- // "Failed to decode Cip509: {:?}",
456- // cip509_1.report()
457- // );
458- //
459- // let tracking_payment_keys = vec![];
460- //
461- // // TODO: FIXME: The transaction shouldn't be used here.
462- // let transactions_1 = multi_era_block_1.txs();
463- // // Forth transaction of this test data contains the CIP509 auxiliary data
464- // let tx_1 = transactions_1
465- // .get(3)
466- // .expect("Failed to get transaction index");
467- // let registration_chain =
468- // RegistrationChain::new(point_1.clone(), &tracking_payment_keys, 3, tx_1,
469- // cip509_1); // Able to add chain root to the registration chain
470- // assert!(registration_chain.is_ok());
471- //
472- // let conway_block_data_4 = conway_4();
473- // let point_4 = Point::new(
474- // 77_436_369,
475- // hex::decode("
476- // b174fc697126f05046b847d47e60d66cbedaf25240027f9c07f27150889aac24")
477- // .unwrap(), );
478- //
479- // let multi_era_block_4 =
480- // pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data_4)
481- // .expect("Failed to decode MultiEraBlock");
482- //
483- // let cip509 = Cip509::new(&multi_era_block_4, 1.into()).unwrap().unwrap();
484- // assert!(
485- // !cip509.report().is_problematic(),
486- // "Failed to decode Cip509: {:?}",
487- // cip509.report()
488- // );
489- //
490- // // TODO: FIXME: The transaction shouldn't be used here.
491- // let transactions_4 = multi_era_block_4.txs();
492- // // Second transaction of this test data contains the CIP509 auxiliary data
493- // let tx = transactions_4
494- // .get(1)
495- // .expect("Failed to get transaction index");
496- //
497- // // Update the registration chain
498- // assert!(registration_chain
499- // .unwrap()
500- // .update(point_4.clone(), 1, tx, cip509)
501- // .is_ok());
502- // }
413+ use super :: * ;
414+ use crate :: utils:: test:: { test_block_1, test_block_2, test_block_4} ;
415+
416+ #[ test]
417+ fn multiple_registrations ( ) {
418+ let block = test_block_1 ( ) ;
419+ let registration = Cip509 :: new ( & block, 3 . into ( ) , & [ ] ) . unwrap ( ) . unwrap ( ) ;
420+ assert ! (
421+ !registration. report( ) . is_problematic( ) ,
422+ "{:#?}" ,
423+ registration. report( )
424+ ) ;
425+
426+ // Create a chain with the first registration.
427+ let chain = RegistrationChain :: new ( registration) . unwrap ( ) ;
428+ assert_eq ! ( chain. purpose( ) , & [ Uuid :: parse_str(
429+ "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c"
430+ )
431+ . unwrap( ) ] ) ;
432+ assert_eq ! ( 1 , chain. x509_certs( ) . len( ) ) ;
433+ let origin = & chain. x509_certs ( ) . get ( & 0 ) . unwrap ( ) . 0 ;
434+ assert_eq ! ( origin. point( ) . slot_or_default( ) , 77429134 . into( ) ) ;
435+ assert_eq ! ( origin. txn_index( ) , 3 . into( ) ) ;
436+
437+ // Try to add an invalid registration.
438+ let block = test_block_2 ( ) ;
439+ let registration = Cip509 :: new ( & block, 0 . into ( ) , & [ ] ) . unwrap ( ) . unwrap ( ) ;
440+ assert ! ( registration. report( ) . is_problematic( ) ) ;
441+
442+ let error = chain. update ( registration) . unwrap_err ( ) ;
443+ let error = format ! ( "{error:?}" ) ;
444+ assert ! (
445+ error. contains( "Invalid previous transaction ID" ) ,
446+ "{}" ,
447+ error
448+ ) ;
449+
450+ // Add the second registration.
451+ let block = test_block_4 ( ) ;
452+ let registration = Cip509 :: new ( & block, 1 . into ( ) , & [ ] ) . unwrap ( ) . unwrap ( ) ;
453+ assert ! (
454+ !registration. report( ) . is_problematic( ) ,
455+ "{:#?}" ,
456+ registration. report( )
457+ ) ;
458+ chain. update ( registration) . unwrap ( ) ;
459+ }
503460}
0 commit comments