Skip to content

Commit 20e731e

Browse files
Validation tests
1 parent 60a0dba commit 20e731e

File tree

2 files changed

+162
-69
lines changed

2 files changed

+162
-69
lines changed

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
use anyhow::anyhow;
66
use cardano_blockchain_types::{
77
hashes::{Blake2b256Hash, BLAKE_2B256_SIZE},
8-
MultiEraBlock, Slot, TxnIndex,
8+
Slot, TxnIndex,
99
};
1010
use catalyst_types::problem_report::ProblemReport;
1111
use minicbor::{
1212
decode::{self},
1313
Decode, Decoder,
1414
};
15-
use pallas::{codec::utils::Nullable, ledger::traverse::MultiEraTx};
15+
use pallas::{
16+
codec::utils::Nullable,
17+
ledger::traverse::{MultiEraBlock, MultiEraTx},
18+
};
1619
use strum_macros::FromRepr;
1720
use tracing::warn;
1821
use uuid::Uuid;
@@ -100,7 +103,6 @@ impl Cip509 {
100103
/// the `Cip509` structure contains fully or partially decoded data.
101104
pub fn new(block: &MultiEraBlock, index: TxnIndex) -> Result<Option<Self>, anyhow::Error> {
102105
// Find the transaction and decode the relevant data.
103-
let block = block.decode();
104106
let transactions = block.txs();
105107
let transaction = transactions.get(usize::from(index)).ok_or_else(|| {
106108
anyhow!(
@@ -168,8 +170,7 @@ impl Cip509 {
168170
pub fn from_block(block: &MultiEraBlock) -> Vec<Self> {
169171
let mut result = Vec::new();
170172

171-
let decoded_block = block.decode();
172-
for index in 0..decoded_block.tx_count() {
173+
for index in 0..block.tx_count() {
173174
let index = TxnIndex::from(index);
174175
match Self::new(block, index) {
175176
Ok(Some(v)) => result.push(v),
@@ -178,7 +179,7 @@ impl Cip509 {
178179
Err(e) => {
179180
warn!(
180181
"Unable to extract Cip509 from the {} block {index:?} transaction: {e:?}",
181-
decoded_block.slot()
182+
block.slot()
182183
);
183184
},
184185
}
@@ -236,10 +237,15 @@ impl Cip509 {
236237
(self.slot, self.transaction_index)
237238
}
238239

240+
/// Returns URIs contained in both x509 and c509 certificates of `Cip509` metadata.
239241
pub fn certificate_uris(&self) -> Option<&Cip0134UriSet> {
240242
self.metadata.as_ref().map(|m| &m.certificate_uris)
241243
}
242244

245+
pub fn txn_inputs_hash(&self) -> Option<&TxInputHash> {
246+
self.txn_inputs_hash.as_ref()
247+
}
248+
243249
/// Returns `Cip509` fields consuming the structure if it was successfully decoded and
244250
/// validated otherwise return the problem report that contains all the encountered
245251
/// issues.
@@ -461,3 +467,30 @@ fn decode_validation_signature(
461467
},
462468
}
463469
}
470+
471+
#[cfg(test)]
472+
mod tests {
473+
use super::*;
474+
475+
#[test]
476+
fn new() {
477+
let block = hex::decode(include_str!("../../test_data/cardano/conway_1.block"))
478+
.expect("Failed to decode hex block.");
479+
let block = MultiEraBlock::decode(&block).unwrap();
480+
let index = TxnIndex::from(3);
481+
let res = Cip509::new(&block, index)
482+
.expect("Failed to get Cip509")
483+
.expect("There must be Cip509 in block");
484+
assert!(!res.report.is_problematic());
485+
}
486+
487+
#[test]
488+
fn from_block() {
489+
let block = hex::decode(include_str!("../../test_data/cardano/conway_1.block"))
490+
.expect("Failed to decode hex block.");
491+
let block = MultiEraBlock::decode(&block).unwrap();
492+
let res = Cip509::from_block(&block);
493+
assert_eq!(1, res.len());
494+
assert!(!res.first().unwrap().report.is_problematic());
495+
}
496+
}

0 commit comments

Comments
 (0)