Skip to content

Commit 13362d0

Browse files
Update the registration chain tests
1 parent 7432ec1 commit 13362d0

File tree

3 files changed

+72
-125
lines changed

3 files changed

+72
-125
lines changed

rust/rbac-registration/src/cardano/cip509/validation.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ use pallas::{
3535

3636
use super::utils::cip19::compare_key_hash;
3737
use crate::{
38-
cardano::cip509::{
39-
types::TxInputHash, utils::cip19::extract_key_hash, Cip0134UriSet, LocalRefInt, RoleData,
40-
},
38+
cardano::cip509::{types::TxInputHash, Cip0134UriSet, LocalRefInt, RoleData},
4139
utils::hashing::{blake2b_128, blake2b_256},
4240
};
4341

@@ -176,7 +174,7 @@ fn extract_stake_addresses(uris: Option<&Cip0134UriSet>) -> Vec<VKeyHash> {
176174
.flat_map(|(_index, uris)| uris.iter())
177175
.filter_map(|uri| {
178176
if let Address::Stake(a) = uri.address() {
179-
extract_key_hash(a.payload().as_hash().as_ref())
177+
a.payload().as_hash().as_slice().try_into().ok()
180178
} else {
181179
None
182180
}
@@ -349,13 +347,7 @@ mod tests {
349347
else {
350348
panic!("Unexpected address type");
351349
};
352-
let hash = address
353-
.payload()
354-
.as_hash()
355-
.get(1..29)
356-
.unwrap()
357-
.try_into()
358-
.unwrap();
350+
let hash = address.payload().as_hash().as_ref().try_into().unwrap();
359351

360352
let addresses = extract_stake_addresses(cip509.certificate_uris());
361353
assert_eq!(1, addresses.len());

rust/rbac-registration/src/registration/cardano/mod.rs

Lines changed: 62 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use anyhow::bail;
66
use c509_certificate::c509::C509;
77
use cardano_blockchain_types::hashes::Blake2b256Hash;
88
use ed25519_dalek::VerifyingKey;
9-
use pallas::ledger::traverse::MultiEraTx;
109
use tracing::{error, warn};
1110
use uuid::Uuid;
1211
use x509_cert::certificate::Certificate as X509Certificate;
@@ -17,6 +16,7 @@ use crate::cardano::cip509::{
1716
};
1817

1918
/// Registration chains.
19+
#[derive(Debug)]
2020
pub 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)]
115112
struct 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)]
419412
mod 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
}

rust/rbac-registration/src/utils/test.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,34 @@ use cardano_blockchain_types::{MultiEraBlock, Network, Point};
66
/// with the `Cip509` data.
77
pub fn test_block_1() -> MultiEraBlock {
88
let data = hex::decode(include_str!("../test_data/cardano/conway_1.block")).unwrap();
9-
let previous = Point::fuzzy(0.into());
10-
block(data, previous)
9+
block(data)
1110
}
1211

1312
/// Returns the decoded `conway_2.block` block that contains one transaction (index = 0).
1413
/// This registration contains an invalid public key that isn't present in the transaction
1514
/// witness set.
1615
pub fn test_block_2() -> MultiEraBlock {
1716
let data = hex::decode(include_str!("../test_data/cardano/conway_2.block")).unwrap();
18-
let previous = Point::fuzzy(1.into());
19-
block(data, previous)
17+
block(data)
2018
}
2119

2220
/// Returns the decoded `conway_3.block` block that contains one transaction (index = 0)
2321
/// with the `Cip509` data.
2422
pub fn test_block_3() -> MultiEraBlock {
2523
let data = hex::decode(include_str!("../test_data/cardano/conway_3.block")).unwrap();
26-
let previous = Point::fuzzy(2.into());
27-
block(data, previous)
24+
block(data)
2825
}
2926

3027
/// Returns the decoded `conway_4.block` block that contains one transaction (index = 1)
3128
/// with the `Cip509` data.
3229
pub fn test_block_4() -> MultiEraBlock {
3330
let data = hex::decode(include_str!("../test_data/cardano/conway_4.block")).unwrap();
34-
let previous = Point::fuzzy(3.into());
35-
block(data, previous)
31+
block(data)
3632
}
3733

3834
/// Converts the given raw data to a block.
39-
fn block(data: Vec<u8>, previous: Point) -> MultiEraBlock {
35+
fn block(data: Vec<u8>) -> MultiEraBlock {
36+
// This point is used to bypass validation in the block constructor.
37+
let previous = Point::fuzzy(0.into());
4038
MultiEraBlock::new(Network::Preprod, data, &previous, 0.into()).unwrap()
4139
}

0 commit comments

Comments
 (0)