Skip to content

Commit 027a5a5

Browse files
Stanislav TkachStanislav Tkach
authored andcommitted
Add the 'assert_valid' function for the test data
1 parent b9a8be2 commit 027a5a5

File tree

6 files changed

+78
-78
lines changed

6 files changed

+78
-78
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ impl Cip509 {
195195
}
196196

197197
/// Returns all role numbers present in this `Cip509` instance.
198+
#[must_use]
198199
pub fn all_roles(&self) -> Vec<RoleNumber> {
199200
if let Some(metadata) = &self.metadata {
200201
metadata.role_data.keys().copied().collect()
@@ -204,36 +205,43 @@ impl Cip509 {
204205
}
205206

206207
/// Returns a role data for the given role if it is present.
208+
#[must_use]
207209
pub fn role_data(&self, role: RoleNumber) -> Option<&RoleData> {
208210
self.metadata.as_ref().and_then(|m| m.role_data.get(&role))
209211
}
210212

211213
/// Returns a hash of the previous transaction.
214+
#[must_use]
212215
pub fn previous_transaction(&self) -> Option<Blake2b256Hash> {
213216
self.prv_tx_id
214217
}
215218

216-
/// Returns a problem report
219+
/// Returns a problem report.
220+
#[must_use]
217221
pub fn report(&self) -> &ProblemReport {
218222
&self.report
219223
}
220224

221225
/// Returns a point and a transaction index where this data is originating from.
226+
#[must_use]
222227
pub fn origin(&self) -> &PointTxnIdx {
223228
&self.origin
224229
}
225230

226231
/// Returns a hash of the transaction where this data is originating from.
232+
#[must_use]
227233
pub fn txn_hash(&self) -> Blake2b256Hash {
228234
self.txn_hash
229235
}
230236

231237
/// Returns URIs contained in both x509 and c509 certificates of `Cip509` metadata.
238+
#[must_use]
232239
pub fn certificate_uris(&self) -> Option<&Cip0134UriSet> {
233240
self.metadata.as_ref().map(|m| &m.certificate_uris)
234241
}
235242

236243
/// Returns a transaction inputs hash.
244+
#[must_use]
237245
pub fn txn_inputs_hash(&self) -> Option<&TxInputHash> {
238246
self.txn_inputs_hash.as_ref()
239247
}
@@ -556,6 +564,7 @@ mod tests {
556564
.expect("Failed to get Cip509")
557565
.expect("There must be Cip509 in block");
558566
assert!(!res.report.is_problematic(), "{:?}", res.report);
567+
data.assert_valid(&res);
559568
}
560569

561570
#[test]
@@ -565,5 +574,6 @@ mod tests {
565574
assert_eq!(1, res.len());
566575
let cip509 = res.first().unwrap();
567576
assert!(!cip509.report.is_problematic(), "{:?}", cip509.report);
577+
data.assert_valid(cip509);
568578
}
569579
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct RoleData {
3131

3232
impl RoleData {
3333
/// Create an instance of role data.
34+
#[must_use]
3435
pub fn new(data: CborRoleData, txn: &conway::MintedTx, report: &ProblemReport) -> Self {
3536
let payment_key = if data.number == Some(RoleNumber::ROLE_0) && data.payment_key.is_none() {
3637
report.other(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct Cip0134UriSetInner {
4040

4141
impl Cip0134UriSet {
4242
/// Creates a new `Cip0134UriSet` instance from the given certificates.
43+
#[must_use]
4344
pub fn new(
4445
x509_certs: &[X509DerCert], c509_certs: &[C509Cert], report: &ProblemReport,
4546
) -> Self {
@@ -287,12 +288,10 @@ mod tests {
287288
assert_eq!(uris.len(), 1);
288289

289290
let uri = uris.first().unwrap();
290-
// cSpell:disable
291291
assert_eq!(
292292
uri.uri(),
293293
format!("web+cardano://addr/{}", data.stake_addr.unwrap())
294294
);
295-
// cSpell:enable
296295
let Address::Stake(address) = uri.address() else {
297296
panic!("Unexpected address type");
298297
};

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

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,6 @@ fn check_key_offset<T>(
248248

249249
#[cfg(test)]
250250
mod tests {
251-
use std::str::FromStr;
252-
253-
use uuid::Uuid;
254-
255251
use super::*;
256252
use crate::{cardano::cip509::Cip509, utils::test};
257253

@@ -263,20 +259,7 @@ mod tests {
263259
assert_eq!(1, registrations.len());
264260

265261
let registration = registrations.pop().unwrap();
266-
assert!(
267-
!registration.report().is_problematic(),
268-
"{:?}",
269-
registration.report()
270-
);
271-
assert!(registration.previous_transaction().is_none());
272-
273-
let origin = registration.origin();
274-
assert_eq!(origin.txn_index(), data.txn_index);
275-
assert_eq!(origin.point().slot_or_default(), data.slot);
276-
277-
let (purpose, metadata, _) = registration.consume().unwrap();
278-
assert_eq!(purpose, Uuid::parse_str(&data.purpose).unwrap());
279-
assert_eq!(1, metadata.role_data.len());
262+
data.assert_valid(&registration);
280263
}
281264

282265
#[test]
@@ -310,10 +293,7 @@ mod tests {
310293
let registration = registrations.pop().unwrap();
311294
assert!(registration.report().is_problematic());
312295

313-
assert_eq!(
314-
registration.previous_transaction(),
315-
Some(Blake2b256Hash::from_str(data.prv_hash.unwrap().as_str()).unwrap())
316-
);
296+
assert_eq!(registration.previous_transaction(), data.prv_hash);
317297

318298
let origin = registration.origin();
319299
assert_eq!(origin.txn_index(), data.txn_index);
@@ -334,23 +314,7 @@ mod tests {
334314
assert_eq!(1, registrations.len());
335315

336316
let registration = registrations.pop().unwrap();
337-
assert!(
338-
!registration.report().is_problematic(),
339-
"{:?}",
340-
registration.report()
341-
);
342-
assert_eq!(
343-
registration.previous_transaction(),
344-
Some(Blake2b256Hash::from_str(data.prv_hash.unwrap().as_str()).unwrap())
345-
);
346-
347-
let origin = registration.origin();
348-
assert_eq!(origin.txn_index(), data.txn_index);
349-
assert_eq!(origin.point().slot_or_default(), data.slot);
350-
351-
let (purpose, metadata, _) = registration.consume().unwrap();
352-
assert_eq!(purpose, Uuid::parse_str(&data.purpose).unwrap());
353-
assert_eq!(1, metadata.role_data.len());
317+
data.assert_valid(&registration);
354318
}
355319

356320
#[test]

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,11 @@ mod test {
420420
let registration = Cip509::new(&data.block, data.txn_index, &[])
421421
.unwrap()
422422
.unwrap();
423-
assert!(
424-
!registration.report().is_problematic(),
425-
"{:#?}",
426-
registration.report()
427-
);
423+
data.assert_valid(&registration);
428424

429425
// Create a chain with the first registration.
430426
let chain = RegistrationChain::new(registration).unwrap();
431-
assert_eq!(chain.purpose(), &[Uuid::parse_str(&data.purpose).unwrap()]);
427+
assert_eq!(chain.purpose(), &[data.purpose]);
432428
assert_eq!(1, chain.x509_certs().len());
433429
let origin = &chain.x509_certs().get(&0).unwrap().0;
434430
assert_eq!(origin.point().slot_or_default(), data.slot);
@@ -454,17 +450,11 @@ mod test {
454450
let registration = Cip509::new(&data.block, data.txn_index, &[])
455451
.unwrap()
456452
.unwrap();
457-
assert!(
458-
!registration.report().is_problematic(),
459-
"{:#?}",
460-
registration.report()
461-
);
453+
data.assert_valid(&registration);
462454
let update = chain.update(registration).unwrap();
463455

464-
// Current tx hash should updated to RBAC data in block 4
465-
assert_eq!(update.current_tx_id_hash().to_string(), data.tx_hash);
466-
assert!(update
467-
.role_data()
468-
.contains_key(&RoleNumber::from(data.role)));
456+
// Current tx hash should be equal to the hash from block 4.
457+
assert_eq!(update.current_tx_id_hash(), data.txn_hash);
458+
assert!(update.role_data().contains_key(&data.role));
469459
}
470460
}

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

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// cspell: words stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr
44

55
use cardano_blockchain_types::{MultiEraBlock, Network, Point, Slot, TxnIndex};
6+
use catalyst_types::hashes::Blake2b256Hash;
7+
use uuid::Uuid;
8+
9+
use crate::cardano::cip509::{Cip509, RoleNumber};
610

711
/// Test data expected from block.
812
#[allow(dead_code)]
@@ -12,19 +16,37 @@ pub struct BlockTestData {
1216
/// Slot number.
1317
pub slot: Slot,
1418
/// Role.
15-
pub role: u8,
19+
pub role: RoleNumber,
1620
/// Transaction index.
1721
pub txn_index: TxnIndex,
1822
/// Transaction hash.
19-
pub tx_hash: String,
23+
pub txn_hash: Blake2b256Hash,
2024
/// Previous hash.
21-
pub prv_hash: Option<String>,
25+
pub prv_hash: Option<Blake2b256Hash>,
2226
/// Purpose.
23-
pub purpose: String,
27+
pub purpose: Uuid,
2428
/// Stake address.
2529
pub stake_addr: Option<String>,
2630
}
2731

32+
impl BlockTestData {
33+
/// Asserts that the problem report doesn't contain errors and all fields have
34+
/// expected values.
35+
#[track_caller]
36+
pub fn assert_valid(&self, cip509: &Cip509) {
37+
assert!(!cip509.report().is_problematic(), "{:?}", cip509.report());
38+
39+
let origin = cip509.origin();
40+
assert_eq!(origin.txn_index(), self.txn_index);
41+
assert_eq!(origin.point().as_fuzzy(), Point::fuzzy(self.slot));
42+
assert!(cip509.role_data(self.role).is_some());
43+
assert_eq!(cip509.txn_hash(), self.txn_hash);
44+
assert_eq!(cip509.previous_transaction(), self.prv_hash);
45+
let (purpose, ..) = cip509.clone().consume().unwrap();
46+
assert_eq!(purpose, self.purpose);
47+
}
48+
}
49+
2850
/// Returns the decoded `conway_1.block` block that contains 6 transaction
2951
/// Slot number: `81_440_954`, Block number: `3_096_080`
3052
/// Tx hash: 0x22469cb214ad2c95265630f5c26b96166ea618722b38401d55ecf68a1fd893ec
@@ -40,11 +62,13 @@ pub fn block_1() -> BlockTestData {
4062
BlockTestData {
4163
block: block(data),
4264
slot: 81_440_954.into(),
43-
role: 0,
65+
role: 0.into(),
4466
txn_index: 2.into(),
45-
tx_hash: "22469cb214ad2c95265630f5c26b96166ea618722b38401d55ecf68a1fd893ec".to_string(),
67+
txn_hash: "22469cb214ad2c95265630f5c26b96166ea618722b38401d55ecf68a1fd893ec"
68+
.parse()
69+
.unwrap(),
4670
prv_hash: None,
47-
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".to_string(),
71+
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".parse().unwrap(),
4872
stake_addr: Some(
4973
"stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr".to_string(),
5074
),
@@ -67,13 +91,17 @@ pub fn block_2() -> BlockTestData {
6791
BlockTestData {
6892
block: block(data),
6993
slot: 77_171_632.into(),
70-
role: 0,
94+
role: 0.into(),
7195
txn_index: 0.into(),
72-
tx_hash: "337d35026efaa48b5ee092d38419e102add1b535364799eb8adec8ac6d573b79".to_string(),
96+
txn_hash: "337d35026efaa48b5ee092d38419e102add1b535364799eb8adec8ac6d573b79"
97+
.parse()
98+
.unwrap(),
7399
prv_hash: Some(
74-
"4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2".to_string(),
100+
"4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2"
101+
.parse()
102+
.unwrap(),
75103
),
76-
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".to_string(),
104+
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".parse().unwrap(),
77105
stake_addr: None,
78106
}
79107
}
@@ -94,15 +122,21 @@ pub fn block_3() -> BlockTestData {
94122
BlockTestData {
95123
block: block(data),
96124
slot: 77_170_639.into(),
97-
role: 0,
125+
role: 0.into(),
98126
txn_index: 0.into(),
99-
tx_hash: "0fda4c9f86e763fecd33f57d8f93540b1598c0a0e539dd996c48052ce94bab80".to_string(),
127+
txn_hash: "0fda4c9f86e763fecd33f57d8f93540b1598c0a0e539dd996c48052ce94bab80"
128+
.parse()
129+
.unwrap(),
100130
prv_hash: Some(
101-
"4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2".to_string(),
131+
"4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2"
132+
.parse()
133+
.unwrap(),
102134
),
103-
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".to_string(),
135+
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".parse().unwrap(),
104136
stake_addr: Some(
105-
"stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr".to_string(),
137+
"stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr"
138+
.parse()
139+
.unwrap(),
106140
),
107141
}
108142
}
@@ -122,11 +156,13 @@ pub fn block_4() -> BlockTestData {
122156
BlockTestData {
123157
block: block(data),
124158
slot: 81_441_637.into(),
125-
role: 4,
159+
role: 4.into(),
126160
txn_index: 4.into(),
127-
tx_hash: "730cc97496b2aec16af58d27284deae2b3347e4c4c5e9ac97458e80131ccd575".to_string(),
128-
prv_hash: Some(block_1().tx_hash),
129-
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".to_string(),
161+
txn_hash: "730cc97496b2aec16af58d27284deae2b3347e4c4c5e9ac97458e80131ccd575"
162+
.parse()
163+
.unwrap(),
164+
prv_hash: Some(block_1().txn_hash),
165+
purpose: "ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c".parse().unwrap(),
130166
stake_addr: Some(
131167
"stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr".to_string(),
132168
),

0 commit comments

Comments
 (0)