Skip to content

Commit

Permalink
fix(rust/cardano-chain-follower): Use CIP509 from rbac-registration (
Browse files Browse the repository at this point in the history
…#96)

* fix: remove Cip509 from chain follower

Signed-off-by: bkioshn <[email protected]>

* feat: add rbac-registration lib to chain-follower

Signed-off-by: bkioshn <[email protected]>

* fix: modify cip509 metadatum

Signed-off-by: bkioshn <[email protected]>

* fix: remove unused function

Signed-off-by: bkioshn <[email protected]>

* fix: remove unused transaction index

Signed-off-by: bkioshn <[email protected]>

* fix: remove unused dependencies

Signed-off-by: bkioshn <[email protected]>

* fix: add getter function

Signed-off-by: bkioshn <[email protected]>

* fix: remove label

Signed-off-by: bkioshn <[email protected]>

* test: test new rbac-reg branch

Signed-off-by: bkioshn <[email protected]>

* fix: remove getter (for consistency)

Signed-off-by: bkioshn <[email protected]>

* fix: update rbac-registration to tag v0.0.8

Signed-off-by: bkioshn <[email protected]>

---------

Signed-off-by: bkioshn <[email protected]>
  • Loading branch information
bkioshn authored Dec 6, 2024
1 parent 1747c61 commit e5a2fd1
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 2,242 deletions.
7 changes: 1 addition & 6 deletions rust/cardano-chain-follower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mithril-client = { version = "0.8.18", default-features = false, features = [
"num-integer-backend",
] }

c509-certificate = { version = "0.0.3", git = "https://github.com/input-output-hk/catalyst-libs.git" , tag = "v0.0.3" }
rbac-registration = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.8" }

thiserror = "1.0.64"
tokio = { version = "1.40.0", features = [
Expand Down Expand Up @@ -53,9 +53,7 @@ mimalloc = { version = "0.1.43", optional = true }
memx = "0.1.32"
fmmap = { version = "0.3.3", features = ["sync", "tokio-async"] }
minicbor = { version = "0.25.1", features = ["alloc", "derive", "half"] }
brotli = "7.0.0"
zstd = "0.13.2"
x509-cert = "0.2.5"
ed25519-dalek = "2.1.1"
blake2b_simd = "1.0.2"
num-traits = "0.2.19"
Expand All @@ -65,9 +63,6 @@ ureq = { version = "2.10.1", features = ["native-certs"] }
http = "1.1.0"
hickory-resolver = { version = "0.24.1", features = ["dns-over-rustls"] }
moka = { version = "0.12.8", features = ["sync"] }
der-parser = "9.0.0"
regex = "1.11.0"
bech32 = "0.11.0"

[dev-dependencies]
hex = "0.4.3"
Expand Down
11 changes: 3 additions & 8 deletions rust/cardano-chain-follower/examples/follow_chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#[cfg(feature = "mimalloc")]
use mimalloc::MiMalloc;
use rbac_registration::cardano::cip509;

/// Use Mimalloc for the global allocator.
#[cfg(feature = "mimalloc")]
Expand Down Expand Up @@ -329,10 +330,7 @@ fn update_biggest_aux_data(
None => 0,
};

let raw_size_cip509 = match chain_update
.data
.txn_raw_metadata(tx_idx, Metadata::cip509::LABEL)
{
let raw_size_cip509 = match chain_update.data.txn_raw_metadata(tx_idx, cip509::LABEL) {
Some(raw) => raw.len(),
None => 0,
};
Expand Down Expand Up @@ -368,10 +366,7 @@ fn log_bad_cip36_info(chain_update: &ChainUpdate, network: Network, tx_idx: usiz

/// Helper function for logging CIP509 validation.
fn log_cip509_info(chain_update: &ChainUpdate, block_num: u64, network: Network, tx_idx: usize) {
if let Some(decoded_metadata) = chain_update
.data
.txn_metadata(tx_idx, Metadata::cip509::LABEL)
{
if let Some(decoded_metadata) = chain_update.data.txn_metadata(tx_idx, cip509::LABEL) {
info!("Block Number {}", block_num);

if let Metadata::DecodedMetadataValues::Cip509(cip509) = &decoded_metadata.value {
Expand Down
80 changes: 80 additions & 0 deletions rust/cardano-chain-follower/src/metadata/cip509.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! Cardano Improvement Proposal 509 (CIP-509) metadata module.
//! Doc Reference: <https://github.com/input-output-hk/catalyst-CIPs/tree/x509-envelope-metadata/CIP-XXXX>
//! CDDL Reference: <https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/x509-envelope.cddl>
use std::sync::Arc;

use minicbor::{Decode, Decoder};
use pallas::ledger::traverse::MultiEraTx;
use rbac_registration::cardano::cip509::{Cip509 as RbacRegCip509, Cip509Validation, LABEL};

use super::{
DecodedMetadata, DecodedMetadataItem, DecodedMetadataValues, RawAuxData, ValidationReport,
};

/// CIP509 metadatum.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Cip509 {
/// CIP509 data.
pub cip509: RbacRegCip509,
/// Validation value, not a part of CIP509, justs storing validity of the data.
pub validation: Cip509Validation,
}

impl Cip509 {
/// Decode and validate CIP509 Metadata
///
/// # Returns
///
/// Nothing. IF CIP509 Metadata is found it will be updated in `decoded_metadata`.
pub(crate) fn decode_and_validate(
decoded_metadata: &DecodedMetadata, txn: &MultiEraTx, raw_aux_data: &RawAuxData,
) {
// Get the CIP509 metadata if possible
let Some(k509) = raw_aux_data.get_metadata(LABEL) else {
return;
};

let mut validation_report = ValidationReport::new();
let mut decoder = Decoder::new(k509.as_slice());

let cip509 = match RbacRegCip509::decode(&mut decoder, &mut ()) {
Ok(metadata) => metadata,
Err(e) => {
Cip509::default().validation_failure(
&format!("Failed to decode CIP509 metadata: {e}"),
&mut validation_report,
decoded_metadata,
);
return;
},
};

// Validate the decoded metadata
let validation = cip509.validate(txn, &mut validation_report);

// Create a Cip509 struct and insert it into decoded_metadata
decoded_metadata.0.insert(
LABEL,
Arc::new(DecodedMetadataItem {
value: DecodedMetadataValues::Cip509(Arc::new(Cip509 { cip509, validation })),
report: validation_report.clone(),
}),
);
}

/// Handle validation failure.
fn validation_failure(
&self, reason: &str, validation_report: &mut ValidationReport,
decoded_metadata: &DecodedMetadata,
) {
validation_report.push(reason.into());
decoded_metadata.0.insert(
LABEL,
Arc::new(DecodedMetadataItem {
value: DecodedMetadataValues::Cip509(Arc::new(self.clone()).clone()),
report: validation_report.clone(),
}),
);
}
}
215 changes: 0 additions & 215 deletions rust/cardano-chain-follower/src/metadata/cip509/decode_helper.rs

This file was deleted.

Loading

0 comments on commit e5a2fd1

Please sign in to comment.