Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions rust/rbac-registration/src/cardano/cip509/cip509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,18 +551,17 @@ mod tests {

#[test]
fn new() {
let block = test::block_1();
let index = TxnIndex::from(3);
let res = Cip509::new(&block, index, &[])
let data = test::block_1();
let res = Cip509::new(&data.block, data.txn_index, &[])
.expect("Failed to get Cip509")
.expect("There must be Cip509 in block");
assert!(!res.report.is_problematic(), "{:?}", res.report);
}

#[test]
fn from_block() {
let block = test::block_1();
let res = Cip509::from_block(&block, &[]);
let data = test::block_1();
let res = Cip509::from_block(&data.block, &[]);
assert_eq!(1, res.len());
let cip509 = res.first().unwrap();
assert!(!cip509.report.is_problematic(), "{:?}", cip509.report);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ mod tests {

#[test]
fn set_new() {
let block = test::block_1();
let cip509 = Cip509::new(&block, 3.into(), &[]).unwrap().unwrap();
let data = test::block_1();
let cip509 = Cip509::new(&data.block, data.txn_index, &[])
.unwrap()
.unwrap();
assert!(
!cip509.report().is_problematic(),
"Failed to decode Cip509: {:?}",
Expand All @@ -288,7 +290,7 @@ mod tests {
// cSpell:disable
assert_eq!(
uri.uri(),
"web+cardano://addr/stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr"
format!("web+cardano://addr/{}", data.stake_addr.unwrap())
);
// cSpell:enable
let Address::Stake(address) = uri.address() else {
Expand Down
80 changes: 31 additions & 49 deletions rust/rbac-registration/src/cardano/cip509/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ mod tests {

#[test]
fn block_1() {
let block = test::block_1();
let data = test::block_1();

let mut registrations = Cip509::from_block(&block, &[]);
let mut registrations = Cip509::from_block(&data.block, &[]);
assert_eq!(1, registrations.len());

let registration = registrations.pop().unwrap();
Expand All @@ -271,30 +271,27 @@ mod tests {
assert!(registration.previous_transaction().is_none());

let origin = registration.origin();
assert_eq!(origin.txn_index(), 3.into());
assert_eq!(origin.point().slot_or_default(), 77_429_134.into());
assert_eq!(origin.txn_index(), data.txn_index);
assert_eq!(origin.point().slot_or_default(), data.slot);

let (purpose, metadata, _) = registration.consume().unwrap();
assert_eq!(
purpose,
Uuid::parse_str("ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c").unwrap()
);
assert_eq!(purpose, Uuid::parse_str(&data.purpose).unwrap());
assert_eq!(1, metadata.role_data.len());
}

#[test]
fn block_2() {
let block = test::block_2();
let data = test::block_2();

let mut registrations = Cip509::from_block(&block, &[]);
let mut registrations = Cip509::from_block(&data.block, &[]);
assert_eq!(1, registrations.len());

let registration = registrations.pop().unwrap();
assert!(registration.report().is_problematic());

let origin = registration.origin();
assert_eq!(origin.txn_index(), 0.into());
assert_eq!(origin.point().slot_or_default(), 77_171_632.into());
assert_eq!(origin.txn_index(), data.txn_index);
assert_eq!(origin.point().slot_or_default(), data.slot);

// The consume function must return the problem report contained within the registration.
let report = registration.consume().unwrap_err();
Expand All @@ -305,44 +302,35 @@ mod tests {

#[test]
fn block_3() {
let block = test::block_3();
let data = test::block_3();

let mut registrations = Cip509::from_block(&block, &[]);
let mut registrations = Cip509::from_block(&data.block, &[]);
assert_eq!(1, registrations.len());

let registration = registrations.pop().unwrap();
assert!(
!registration.report().is_problematic(),
"{:?}",
registration.report()
);
assert!(registration.report().is_problematic());

assert_eq!(
registration.previous_transaction(),
Some(
Blake2b256Hash::from_str(
"4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2"
)
.unwrap()
)
Some(Blake2b256Hash::from_str(data.prv_hash.unwrap().as_str()).unwrap())
);

let origin = registration.origin();
assert_eq!(origin.txn_index(), 0.into());
assert_eq!(origin.point().slot_or_default(), 77_170_639.into());
assert_eq!(origin.txn_index(), data.txn_index);
assert_eq!(origin.point().slot_or_default(), data.slot);

let (purpose, metadata, _) = registration.consume().unwrap();
assert_eq!(
purpose,
Uuid::parse_str("ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c").unwrap()
);
assert_eq!(1, metadata.role_data.len());
let report = registration.consume().unwrap_err();
assert!(report.is_problematic());
let report = format!("{report:?}");
assert!(report
.contains("Role payment key reference index (1) is not found in transaction outputs"));
}

#[test]
fn block_4() {
let block = test::block_4();
let data = test::block_4();

let mut registrations = Cip509::from_block(&block, &[]);
let mut registrations = Cip509::from_block(&data.block, &[]);
assert_eq!(1, registrations.len());

let registration = registrations.pop().unwrap();
Expand All @@ -353,30 +341,24 @@ mod tests {
);
assert_eq!(
registration.previous_transaction(),
Some(
Blake2b256Hash::from_str(
"6695b9cac9230af5c8ee50747b1ca3c78a854d181c7e5c6c371de01b80274d31"
)
.unwrap()
)
Some(Blake2b256Hash::from_str(data.prv_hash.unwrap().as_str()).unwrap())
);

let origin = registration.origin();
assert_eq!(origin.txn_index(), 1.into());
assert_eq!(origin.point().slot_or_default(), 77_436_369.into());
assert_eq!(origin.txn_index(), data.txn_index);
assert_eq!(origin.point().slot_or_default(), data.slot);

let (purpose, metadata, _) = registration.consume().unwrap();
assert_eq!(
purpose,
Uuid::parse_str("ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c").unwrap()
);
assert_eq!(purpose, Uuid::parse_str(&data.purpose).unwrap());
assert_eq!(1, metadata.role_data.len());
}

#[test]
fn extract_stake_addresses_from_metadata() {
let block = test::block_1();
let cip509 = Cip509::new(&block, 3.into(), &[]).unwrap().unwrap();
let data = test::block_1();
let cip509 = Cip509::new(&data.block, data.txn_index, &[])
.unwrap()
.unwrap();
assert!(
!cip509.report().is_problematic(),
"Failed to decode Cip509: {:?}",
Expand Down
12 changes: 6 additions & 6 deletions rust/rbac-registration/src/cardano/cip509/x509_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ mod tests {
let mut report = ProblemReport::new("X509Chunks");
// We don't care about actual values in the context, all we want is to check the decoding
// of differently compressed data.
let block = test::block_3();
let transactions = block.txs();
let data = test::block_3();
let transactions = data.block.txs();
let MultiEraTx::Conway(txn) = transactions.first().unwrap() else {
panic!("Unexpected transaction type");
};
Expand Down Expand Up @@ -170,8 +170,8 @@ mod tests {
let mut report = ProblemReport::new("X509Chunks");
// We don't care about actual values in the context, all we want is to check the decoding
// of differently compressed data.
let block = test::block_3();
let transactions = block.txs();
let data = test::block_3();
let transactions = data.block.txs();
let MultiEraTx::Conway(txn) = transactions.first().unwrap() else {
panic!("Unexpected transaction type");
};
Expand Down Expand Up @@ -203,8 +203,8 @@ mod tests {
let mut report = ProblemReport::new("X509Chunks");
// We don't care about actual values in the context, all we want is to check the decoding
// of differently compressed data.
let block = test::block_3();
let transactions = block.txs();
let data = test::block_3();
let transactions = data.block.txs();
let MultiEraTx::Conway(txn) = transactions.first().unwrap() else {
panic!("Unexpected transaction type");
};
Expand Down
38 changes: 24 additions & 14 deletions rust/rbac-registration/src/registration/cardano/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ impl RegistrationChainInner {
// Previous transaction ID in the CIP509 should equal to the current transaction ID
// or else it is not a part of the chain
if prv_tx_id == self.current_tx_id_hash {
new_inner.current_tx_id_hash = prv_tx_id;
// Update the current transaction ID hash
new_inner.current_tx_id_hash = cip509.txn_hash();
} else {
bail!("Invalid previous transaction ID, not a part of this registration chain");
}
Expand Down Expand Up @@ -415,8 +416,10 @@ mod test {

#[test]
fn multiple_registrations() {
let block = test::block_1();
let registration = Cip509::new(&block, 3.into(), &[]).unwrap().unwrap();
let data = test::block_1();
let registration = Cip509::new(&data.block, data.txn_index, &[])
.unwrap()
.unwrap();
assert!(
!registration.report().is_problematic(),
"{:#?}",
Expand All @@ -425,18 +428,17 @@ mod test {

// Create a chain with the first registration.
let chain = RegistrationChain::new(registration).unwrap();
assert_eq!(chain.purpose(), &[Uuid::parse_str(
"ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c"
)
.unwrap()]);
assert_eq!(chain.purpose(), &[Uuid::parse_str(&data.purpose).unwrap()]);
assert_eq!(1, chain.x509_certs().len());
let origin = &chain.x509_certs().get(&0).unwrap().0;
assert_eq!(origin.point().slot_or_default(), 77_429_134.into());
assert_eq!(origin.txn_index(), 3.into());
assert_eq!(origin.point().slot_or_default(), data.slot);
assert_eq!(origin.txn_index(), data.txn_index);

// Try to add an invalid registration.
let block = test::block_2();
let registration = Cip509::new(&block, 0.into(), &[]).unwrap().unwrap();
let data = test::block_2();
let registration = Cip509::new(&data.block, data.txn_index, &[])
.unwrap()
.unwrap();
assert!(registration.report().is_problematic());

let error = chain.update(registration).unwrap_err();
Expand All @@ -448,13 +450,21 @@ mod test {
);

// Add the second registration.
let block = test::block_4();
let registration = Cip509::new(&block, 1.into(), &[]).unwrap().unwrap();
let data = test::block_4();
let registration = Cip509::new(&data.block, data.txn_index, &[])
.unwrap()
.unwrap();
assert!(
!registration.report().is_problematic(),
"{:#?}",
registration.report()
);
chain.update(registration).unwrap();
let update = chain.update(registration).unwrap();

// Current tx hash should updated to RBAC data in block 4
assert_eq!(update.current_tx_id_hash().to_string(), data.tx_hash);
assert!(update
.role_data()
.contains_key(&RoleNumber::from(data.role)));
}
}
Loading
Loading