Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rust/cardano-chain-follower): update MultiEraBlock and MultiEraTx to use with WithRawAuxiliary suffix #68

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions rust/cardano-chain-follower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ license.workspace = true
workspace = true

[dependencies]
pallas = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" }
pallas-hardano = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" }
pallas-crypto = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" }
pallas = { version = "0.31.0", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "51c0aa752926ecebafbf7b4ba90f62f835b78805" }
pallas-hardano = { version = "0.31.0", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "51c0aa752926ecebafbf7b4ba90f62f835b78805" }
pallas-crypto = { version = "0.31.0", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "51c0aa752926ecebafbf7b4ba90f62f835b78805" }

# cspell: words licence
mithril-client = { version = "0.8.18", default-features = false, features = [
Expand Down
4 changes: 3 additions & 1 deletion rust/cardano-chain-follower/src/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,15 @@ impl ChainFollower {

#[cfg(test)]
mod tests {
use pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary;

use super::*;

fn mock_block() -> MultiEraBlock {
let raw_block = hex::decode(include_str!("./../test_data/shelley.block"))
.expect("Failed to decode hex block.");

let pallas_block = pallas::ledger::traverse::MultiEraBlock::decode(raw_block.as_slice())
let pallas_block = MultiEraBlockWithRawAuxiliary::decode(raw_block.as_slice())
.expect("cannot decode block");

let previous_point = Point::new(
Expand Down
10 changes: 5 additions & 5 deletions rust/cardano-chain-follower/src/metadata/cip36.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use ed25519_dalek::Verifier;
use minicbor::Decoder;
use pallas::ledger::traverse::MultiEraTx;
use pallas::ledger::traverse::MultiEraTxWithRawAuxiliary;
use tracing::debug;

use super::{
Expand Down Expand Up @@ -91,8 +91,8 @@ impl Cip36 {
/// Nothing. IF CIP36 Metadata is found it will be updated in `decoded_metadata`.
#[allow(clippy::too_many_lines)]
pub(crate) fn decode_and_validate(
decoded_metadata: &DecodedMetadata, slot: u64, txn: &MultiEraTx, raw_aux_data: &RawAuxData,
catalyst_strict: bool, chain: Network,
decoded_metadata: &DecodedMetadata, slot: u64, txn: &MultiEraTxWithRawAuxiliary,
raw_aux_data: &RawAuxData, catalyst_strict: bool, chain: Network,
) {
let k61284 = raw_aux_data.get_metadata(LABEL);
let k61285 = raw_aux_data.get_metadata(SIG_LABEL);
Expand Down Expand Up @@ -377,7 +377,7 @@ impl Cip36 {
/// Decode the Payment Address Metadata in the CIP36 Metadata map.
fn decode_payment_address(
&mut self, decoder: &mut Decoder, validation_report: &mut ValidationReport,
decoded_metadata: &DecodedMetadata, _txn: &MultiEraTx, chain: Network,
decoded_metadata: &DecodedMetadata, _txn: &MultiEraTxWithRawAuxiliary, chain: Network,
) -> Option<usize> {
let raw_address = match decoder.bytes() {
Ok(address) => address,
Expand Down Expand Up @@ -969,7 +969,7 @@ mod tests {
let mut cip36 = create_empty_cip36(false);
let mut decoder = Decoder::new(&hex_data);
let mut report = ValidationReport::new();
let multi_era_tx: *const MultiEraTx = std::ptr::null();
let multi_era_tx: *const MultiEraTxWithRawAuxiliary = std::ptr::null();
let multi_era_tx = unsafe { &*multi_era_tx };

let rc = cip36.decode_payment_address(
Expand Down
68 changes: 38 additions & 30 deletions rust/cardano-chain-follower/src/metadata/cip509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use pallas::{
minicbor::{Encode, Encoder},
utils::Bytes,
},
ledger::traverse::MultiEraTx,
ledger::traverse::MultiEraTxWithRawAuxiliary,
};
use rbac::{certs::C509Cert, role_data::RoleData};
use strum::FromRepr;
Expand Down Expand Up @@ -189,8 +189,8 @@ impl Cip509 {
///
/// 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,
txn_idx: usize,
decoded_metadata: &DecodedMetadata, txn: &MultiEraTxWithRawAuxiliary,
raw_aux_data: &RawAuxData, txn_idx: usize,
) {
// Get the CIP509 metadata if possible
let Some(k509) = raw_aux_data.get_metadata(LABEL) else {
Expand Down Expand Up @@ -297,13 +297,13 @@ impl Cip509 {
/// Transaction inputs hash validation.
/// Must exist and match the hash of the transaction inputs.
fn validate_txn_inputs_hash(
&self, txn: &MultiEraTx, validation_report: &mut ValidationReport,
&self, txn: &MultiEraTxWithRawAuxiliary, validation_report: &mut ValidationReport,
decoded_metadata: &DecodedMetadata,
) -> Option<bool> {
let mut buffer = Vec::new();
let mut e = Encoder::new(&mut buffer);
match txn {
MultiEraTx::AlonzoCompatible(tx, _) => {
MultiEraTxWithRawAuxiliary::AlonzoCompatible(tx, _) => {
let inputs = tx.transaction_body.inputs.clone();
if let Err(e) = e.array(inputs.len() as u64) {
self.validation_failure(
Expand All @@ -324,7 +324,7 @@ impl Cip509 {
}
}
},
MultiEraTx::Babbage(tx) => {
MultiEraTxWithRawAuxiliary::Babbage(tx) => {
let inputs = tx.transaction_body.inputs.clone();
if let Err(e) = e.array(inputs.len() as u64) {
self.validation_failure(
Expand All @@ -345,7 +345,7 @@ impl Cip509 {
}
}
},
MultiEraTx::Conway(tx) => {
MultiEraTxWithRawAuxiliary::Conway(tx) => {
let inputs = tx.transaction_body.inputs.clone();
if let Err(e) = e.array(inputs.len() as u64) {
self.validation_failure(
Expand Down Expand Up @@ -398,11 +398,11 @@ impl Cip509 {
/// Also log out the pre-computed hash where the validation signature (99) set to
/// zero.
fn validate_aux(
&mut self, txn: &MultiEraTx, validation_report: &mut ValidationReport,
&mut self, txn: &MultiEraTxWithRawAuxiliary, validation_report: &mut ValidationReport,
decoded_metadata: &DecodedMetadata,
) -> Option<bool> {
match txn {
MultiEraTx::AlonzoCompatible(tx, _) => {
MultiEraTxWithRawAuxiliary::AlonzoCompatible(tx, _) => {
if let pallas::codec::utils::Nullable::Some(a) = &tx.auxiliary_data {
let original_aux = a.raw_cbor();
let aux_data_hash =
Expand Down Expand Up @@ -432,7 +432,7 @@ impl Cip509 {
None
}
},
MultiEraTx::Babbage(tx) => {
MultiEraTxWithRawAuxiliary::Babbage(tx) => {
if let pallas::codec::utils::Nullable::Some(a) = &tx.auxiliary_data {
let original_aux = a.raw_cbor();
let aux_data_hash =
Expand Down Expand Up @@ -462,7 +462,7 @@ impl Cip509 {
None
}
},
MultiEraTx::Conway(tx) => {
MultiEraTxWithRawAuxiliary::Conway(tx) => {
if let pallas::codec::utils::Nullable::Some(a) = &tx.auxiliary_data {
let original_aux = a.raw_cbor();
let aux_data_hash =
Expand Down Expand Up @@ -535,12 +535,14 @@ impl Cip509 {
/// Validate the stake public key in the certificate with witness set in transaction.
#[allow(clippy::too_many_lines)]
fn validate_stake_public_key(
&self, txn: &MultiEraTx, validation_report: &mut ValidationReport,
&self, txn: &MultiEraTxWithRawAuxiliary, validation_report: &mut ValidationReport,
decoded_metadata: &DecodedMetadata, txn_idx: usize,
) -> Option<bool> {
let mut pk_addrs = Vec::new();
match txn {
MultiEraTx::AlonzoCompatible(..) | MultiEraTx::Babbage(_) | MultiEraTx::Conway(_) => {
MultiEraTxWithRawAuxiliary::AlonzoCompatible(..)
| MultiEraTxWithRawAuxiliary::Babbage(_)
| MultiEraTxWithRawAuxiliary::Conway(_) => {
// X509 certificate
if let Some(x509_certs) = &self.x509_chunks.0.x509_certs {
for cert in x509_certs {
Expand Down Expand Up @@ -740,7 +742,7 @@ impl Cip509 {
/// Validate the payment key
#[allow(clippy::too_many_lines)]
fn validate_payment_key(
&self, txn: &MultiEraTx, validation_report: &mut ValidationReport,
&self, txn: &MultiEraTxWithRawAuxiliary, validation_report: &mut ValidationReport,
decoded_metadata: &DecodedMetadata, txn_idx: usize, role_data: &RoleData,
) -> Option<bool> {
if let Some(payment_key) = role_data.payment_key {
Expand All @@ -754,7 +756,7 @@ impl Cip509 {
return None;
}
match txn {
MultiEraTx::AlonzoCompatible(tx, _) => {
MultiEraTxWithRawAuxiliary::AlonzoCompatible(tx, _) => {
// Handle negative payment keys (reference to tx output)
if payment_key < 0 {
let witness = match TxWitness::new(&[txn.clone()]) {
Expand Down Expand Up @@ -819,7 +821,7 @@ impl Cip509 {
}
return Some(true);
},
MultiEraTx::Babbage(tx) => {
MultiEraTxWithRawAuxiliary::Babbage(tx) => {
// Negative indicates reference to tx output
if payment_key < 0 {
let index = match decremented_index(payment_key.abs()) {
Expand Down Expand Up @@ -887,7 +889,7 @@ impl Cip509 {
}
return Some(true);
},
MultiEraTx::Conway(tx) => {
MultiEraTxWithRawAuxiliary::Conway(tx) => {
// Negative indicates reference to tx output
if payment_key < 0 {
let index = match decremented_index(payment_key.abs()) {
Expand Down Expand Up @@ -1025,7 +1027,7 @@ mod tests {
.expect("Failed to decode hex block.")
}

fn cip_509_aux_data(tx: &MultiEraTx<'_>) -> Vec<u8> {
fn cip_509_aux_data(tx: &pallas::ledger::traverse::MultiEraTxWithRawAuxiliary<'_>) -> Vec<u8> {
let raw_auxiliary_data = tx
.as_conway()
.unwrap()
Expand Down Expand Up @@ -1079,8 +1081,9 @@ mod tests {
let decoded_metadata = DecodedMetadata(DashMap::new());
let mut validation_report = ValidationReport::new();
let conway_block_data = conway_1();
let multi_era_block = pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");
let multi_era_block =
pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");

let transactions = multi_era_block.txs();
// Forth transaction of this test data contains the CIP509 auxiliary data
Expand All @@ -1101,8 +1104,9 @@ mod tests {
let decoded_metadata = DecodedMetadata(DashMap::new());
let mut validation_report = ValidationReport::new();
let conway_block_data = conway_1();
let multi_era_block = pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");
let multi_era_block =
pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");

let transactions = multi_era_block.txs();
// Forth transaction of this test data contains the CIP509 auxiliary data
Expand All @@ -1124,8 +1128,9 @@ mod tests {
let decoded_metadata = DecodedMetadata(DashMap::new());
let mut validation_report = ValidationReport::new();
let conway_block_data = conway_1();
let multi_era_block = pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");
let multi_era_block =
pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");

let transactions = multi_era_block.txs();
// Forth transaction of this test data contains the CIP509 auxiliary data
Expand All @@ -1147,8 +1152,9 @@ mod tests {
let decoded_metadata = DecodedMetadata(DashMap::new());
let mut validation_report = ValidationReport::new();
let conway_block_data = conway_1();
let multi_era_block = pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");
let multi_era_block =
pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");

let transactions = multi_era_block.txs();
// Forth transaction of this test data contains the CIP509 auxiliary data
Expand Down Expand Up @@ -1183,8 +1189,9 @@ mod tests {
let decoded_metadata = DecodedMetadata(DashMap::new());
let mut validation_report = ValidationReport::new();
let conway_block_data = conway_3();
let multi_era_block = pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");
let multi_era_block =
pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");

let transactions = multi_era_block.txs();
// First transaction of this test data contains the CIP509 auxiliary data
Expand Down Expand Up @@ -1220,8 +1227,9 @@ mod tests {
let decoded_metadata = DecodedMetadata(DashMap::new());
let mut validation_report = ValidationReport::new();
let conway_block_data = conway_2();
let multi_era_block = pallas::ledger::traverse::MultiEraBlock::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");
let multi_era_block =
pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary::decode(&conway_block_data)
.expect("Failed to decode MultiEraBlock");

let transactions = multi_era_block.txs();
// Forth transaction of this test data contains the CIP509 auxiliary data
Expand Down
9 changes: 5 additions & 4 deletions rust/cardano-chain-follower/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{fmt::Debug, sync::Arc};
use cip36::Cip36;
use cip509::Cip509;
use dashmap::DashMap;
use pallas::ledger::traverse::{MultiEraBlock, MultiEraTx};
use pallas::ledger::traverse::{MultiEraBlockWithRawAuxiliary, MultiEraTxWithRawAuxiliary};
use raw_aux_data::RawAuxData;
use tracing::error;

Expand Down Expand Up @@ -50,7 +50,8 @@ pub(crate) struct DecodedMetadata(DashMap<u64, Arc<DecodedMetadataItem>>);
impl DecodedMetadata {
/// Create new decoded metadata for a transaction.
fn new(
chain: Network, slot: u64, txn: &MultiEraTx, raw_aux_data: &RawAuxData, txn_idx: usize,
chain: Network, slot: u64, txn: &MultiEraTxWithRawAuxiliary, raw_aux_data: &RawAuxData,
txn_idx: usize,
) -> Self {
let decoded_metadata = Self(DashMap::new());

Expand Down Expand Up @@ -98,7 +99,7 @@ impl DecodedTransaction {
/// Insert another transaction worth of data into the Decoded Aux Data
fn insert(
&mut self, chain: Network, slot: u64, txn_idx: u32, cbor_data: &[u8],
transactions: &[MultiEraTx],
transactions: &[MultiEraTxWithRawAuxiliary],
) {
let txn_idx = usize_from_saturating(txn_idx);

Expand All @@ -115,7 +116,7 @@ impl DecodedTransaction {
}

/// Create a new `DecodedTransaction`.
pub(crate) fn new(chain: Network, block: &MultiEraBlock) -> Self {
pub(crate) fn new(chain: Network, block: &MultiEraBlockWithRawAuxiliary) -> Self {
let mut decoded_aux_data = DecodedTransaction {
raw: DashMap::new(),
decoded: DashMap::new(),
Expand Down
8 changes: 3 additions & 5 deletions rust/cardano-chain-follower/src/mithril_snapshot_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use logcall::logcall;
use pallas::ledger::traverse::MultiEraBlockWithRawAuxiliary;
use tokio::task;
use tracing::{debug, error};
use tracing_log::log;
Expand Down Expand Up @@ -85,8 +86,7 @@ impl MithrilSnapshotIterator {

match next {
Some(Ok(raw_block)) => {
let Ok(block) = pallas::ledger::traverse::MultiEraBlock::decode(&raw_block)
else {
let Ok(block) = MultiEraBlockWithRawAuxiliary::decode(&raw_block) else {
return None;
};

Expand Down Expand Up @@ -237,9 +237,7 @@ impl Iterator for MithrilSnapshotIteratorInner {

// We cannot fully decode this block because we don't know its previous point,
// So this MUST be the first block in iteration, so use it as the previous.
if let Ok(raw_decoded_block) =
pallas::ledger::traverse::MultiEraBlock::decode(&block)
{
if let Ok(raw_decoded_block) = MultiEraBlockWithRawAuxiliary::decode(&block) {
// debug!("Pre Previous update 2 : {:?}", self.previous);
self.previous =
Point::new(raw_decoded_block.slot(), raw_decoded_block.hash().to_vec());
Expand Down
Loading