@@ -203,140 +203,130 @@ pub fn validate_role_signing_key(role_data: &RoleData, report: &ProblemReport) {
203203
204204#[ cfg( test) ]
205205mod tests {
206+ use std:: str:: FromStr ;
206207
207- use catalyst_types:: problem_report:: ProblemReport ;
208- use pallas:: { codec:: utils:: Nullable , ledger:: traverse:: MultiEraBlock } ;
208+ use cardano_blockchain_types:: hashes:: Blake2b256Hash ;
209+ use pallas:: ledger:: traverse:: MultiEraBlock ;
210+ use uuid:: Uuid ;
209211
210212 use super :: * ;
211213 use crate :: cardano:: cip509:: Cip509 ;
212214
215+ // `conway_1.block` contains one transaction (index = 3) with the `Cip509` data.
213216 #[ test]
214217 fn block_1 ( ) {
215218 let block = hex:: decode ( include_str ! ( "../../test_data/cardano/conway_1.block" ) ) . unwrap ( ) ;
216219 let block = MultiEraBlock :: decode ( & block) . unwrap ( ) ;
217220
218- let registrations = Cip509 :: from_block ( & block) ;
219- // TODO: FIXME: Assert size, transaction indexes and check reports!
220- println ! ( "{registrations:?}" ) ;
221+ let mut registrations = Cip509 :: from_block ( & block) ;
222+ assert_eq ! ( 1 , registrations. len( ) ) ;
223+
224+ let registration = registrations. pop ( ) . unwrap ( ) ;
225+ assert ! (
226+ !registration. report( ) . is_problematic( ) ,
227+ "{:?}" ,
228+ registration. report( )
229+ ) ;
230+ assert ! ( registration. previous_transaction( ) . is_none( ) ) ;
231+ assert_eq ! ( registration. origin( ) , ( 77429134 . into( ) , 3 . into( ) ) ) ;
232+
233+ let ( purpose, metadata) = registration. consume ( ) . unwrap ( ) ;
234+ assert_eq ! (
235+ purpose,
236+ Uuid :: parse_str( "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c" ) . unwrap( )
237+ ) ;
238+ assert_eq ! ( 1 , metadata. role_data. len( ) ) ;
221239 }
222240
241+ // `conway_2.block` contains one transaction (index = 0) with the `Cip509` data. Also
242+ // this registration contains an invalid public key that isn't present in the transaction
243+ // witness set.
223244 #[ test]
224245 fn block_2 ( ) {
225246 let block = hex:: decode ( include_str ! ( "../../test_data/cardano/conway_2.block" ) ) . unwrap ( ) ;
226247 let block = MultiEraBlock :: decode ( & block) . unwrap ( ) ;
227248
228- let registrations = Cip509 :: from_block ( & block) ;
229- // TODO: FIXME: Assert size, transaction indexes and check reports!
230- println ! ( "{registrations:?}" ) ;
249+ let mut registrations = Cip509 :: from_block ( & block) ;
250+ assert_eq ! ( 1 , registrations. len( ) ) ;
251+
252+ let registration = registrations. pop ( ) . unwrap ( ) ;
253+ assert ! ( registration. report( ) . is_problematic( ) ) ;
254+ assert_eq ! ( registration. origin( ) , ( 77171632 . into( ) , 0 . into( ) ) ) ;
255+
256+ // The consume function must return the problem report contained within the registration.
257+ let report = registration. consume ( ) . unwrap_err ( ) ;
258+ assert ! ( report. is_problematic( ) ) ;
259+ let report = format ! ( "{report:?}" ) ;
260+ assert ! ( report. contains( "Public key hash not found in transaction witness set" ) ) ;
231261 }
232262
263+ // `conway_3.block` contains one transaction (index = 0) with the `Cip509` data.
233264 #[ test]
234265 fn block_3 ( ) {
235266 let block = hex:: decode ( include_str ! ( "../../test_data/cardano/conway_3.block" ) ) . unwrap ( ) ;
236267 let block = MultiEraBlock :: decode ( & block) . unwrap ( ) ;
237268
238- let registrations = Cip509 :: from_block ( & block) ;
239- // TODO: FIXME: Assert size, transaction indexes and check reports!
240- println ! ( "{registrations:?}" ) ;
241- }
242-
243- fn conway_1 ( ) -> Vec < u8 > {
244- hex:: decode ( include_str ! ( "../../test_data/cardano/conway_1.block" ) )
245- . expect ( "Failed to decode hex block." )
246- }
269+ let mut registrations = Cip509 :: from_block ( & block) ;
270+ assert_eq ! ( 1 , registrations. len( ) ) ;
247271
248- #[ test]
249- fn test_validate_txn_inputs_hash ( ) {
250- let conway_block_data = conway_1 ( ) ;
251- let multi_era_block =
252- MultiEraBlock :: decode ( & conway_block_data) . expect ( "Failed to decode MultiEraBlock" ) ;
253-
254- let transactions = multi_era_block. txs ( ) ;
255- // Forth transaction of this test data contains the CIP509 auxiliary data
256- let transaction_index = 3 ;
257- let tx = transactions
258- . get ( transaction_index)
259- . expect ( "Failed to get transaction index" ) ;
260-
261- let cip509 = Cip509 :: new ( & multi_era_block, transaction_index. into ( ) )
262- . unwrap ( )
263- . unwrap ( ) ;
272+ let registration = registrations. pop ( ) . unwrap ( ) ;
264273 assert ! (
265- !cip509. report( ) . is_problematic( ) ,
266- "Failed to decode Cip509: {:?}" ,
267- cip509. report( )
274+ !registration. report( ) . is_problematic( ) ,
275+ "{:?}" ,
276+ registration. report( )
277+ ) ;
278+ assert_eq ! (
279+ registration. previous_transaction( ) ,
280+ Some (
281+ Blake2b256Hash :: from_str(
282+ "4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2"
283+ )
284+ . unwrap( )
285+ )
268286 ) ;
287+ assert_eq ! ( registration. origin( ) , ( 77170639 . into( ) , 0 . into( ) ) ) ;
269288
270- let MultiEraTx :: Conway ( tx) = tx else {
271- panic ! ( "Unexpected transaction era" ) ;
272- } ;
273- let hash = cip509. txn_inputs_hash ( ) . unwrap ( ) ;
274- let report = ProblemReport :: new ( "validate_txn_inputs_hash test" ) ;
275- validate_txn_inputs_hash ( hash, tx, & report) ;
276- assert ! (
277- !report. is_problematic( ) ,
278- "validate_txn_inputs_hash failed: {report:?}"
289+ let ( purpose, metadata) = registration. consume ( ) . unwrap ( ) ;
290+ assert_eq ! (
291+ purpose,
292+ Uuid :: parse_str( "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c" ) . unwrap( )
279293 ) ;
294+ assert_eq ! ( 1 , metadata. role_data. len( ) ) ;
280295 }
281296
297+ // `conway_3.block` contains one transaction (index = 1) with the `Cip509` data.
282298 #[ test]
283- fn test_validate_aux ( ) {
284- let conway_block_data = conway_1 ( ) ;
285- let multi_era_block =
286- MultiEraBlock :: decode ( & conway_block_data) . expect ( "Failed to decode MultiEraBlock" ) ;
287-
288- let transactions = multi_era_block. txs ( ) ;
289- // Forth transaction of this test data contains the CIP509 auxiliary data
290- let tx = transactions
291- . get ( 3 )
292- . expect ( "Failed to get transaction index" ) ;
293- let MultiEraTx :: Conway ( tx) = tx else {
294- panic ! ( "Unexpected transaction era" ) ;
295- } ;
296- let auxiliary_data = match & tx. auxiliary_data {
297- Nullable :: Some ( v) => v. raw_cbor ( ) ,
298- _ => panic ! ( "Missing auxiliary data in transaction" ) ,
299- } ;
299+ fn block_4 ( ) {
300+ let block = hex:: decode ( include_str ! ( "../../test_data/cardano/conway_4.block" ) ) . unwrap ( ) ;
301+ let block = MultiEraBlock :: decode ( & block) . unwrap ( ) ;
300302
301- let report = ProblemReport :: new ( "Auxiliary data validation" ) ;
302- validate_aux (
303- auxiliary_data,
304- tx. transaction_body . auxiliary_data_hash . as_ref ( ) ,
305- & report,
303+ let mut registrations = Cip509 :: from_block ( & block) ;
304+ assert_eq ! ( 1 , registrations. len( ) ) ;
305+
306+ let registration = registrations. pop ( ) . unwrap ( ) ;
307+ assert ! (
308+ !registration. report( ) . is_problematic( ) ,
309+ "{:?}" ,
310+ registration. report( )
306311 ) ;
307- assert ! ( !report. is_problematic( ) , "validate_aux failed: {report:?}" ) ;
308- }
312+ assert_eq ! (
313+ registration. previous_transaction( ) ,
314+ Some (
315+ Blake2b256Hash :: from_str(
316+ "6695b9cac9230af5c8ee50747b1ca3c78a854d181c7e5c6c371de01b80274d31"
317+ )
318+ . unwrap( )
319+ )
320+ ) ;
321+ assert_eq ! ( registration. origin( ) , ( 77436369 . into( ) , 1 . into( ) ) ) ;
309322
310- // TODO: FIXME:
311- // #[test]
312- // fn test_validate_public_key_fail() {
313- // let conway_block_data = conway_2();
314- // let multi_era_block =
315- // MultiEraBlock::decode(&conway_block_data).expect("Failed to decode
316- // MultiEraBlock");
317- //
318- // let transactions = multi_era_block.txs();
319- // // First transaction of this test data contains the CIP509 auxiliary data
320- // let tx = transactions
321- // .first()
322- // .expect("Failed to get transaction index");
323- //
324- // let cip509 = Cip509::new(&multi_era_block, 0.into()).unwrap().unwrap();
325- // assert!(
326- // !cip509.report().is_problematic(),
327- // "Failed to decode Cip509: {:?}",
328- // cip509.report()
329- // );
330- //
331- // let report = ProblemReport::new("validate_stake_public_key");
332- // validate_stake_public_key(tx, cip509.certificate_uris(), &report);
333- // assert!(report.is_problematic());
334- // let report = format!("{report:?}");
335- // assert!(
336- // report.contains("Failed to compare public keys with witnesses"),
337- // "Unexpected problem report content: {report}"
338- // );
339- // }
323+ let ( purpose, metadata) = registration. consume ( ) . unwrap ( ) ;
324+ assert_eq ! (
325+ purpose,
326+ Uuid :: parse_str( "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c" ) . unwrap( )
327+ ) ;
328+ assert_eq ! ( 1 , metadata. role_data. len( ) ) ;
329+ }
340330
341331 #[ test]
342332 fn extract_stake_addresses_from_metadata ( ) {
0 commit comments