From 21bd98d80a76cded2386077718e073fbfc5c1434 Mon Sep 17 00:00:00 2001 From: Clark Alesna Date: Thu, 8 Dec 2022 04:39:40 +0800 Subject: [PATCH 1/3] Added code to expose collateral data --- src/mapper/babbage.rs | 16 ++++++++++++++++ src/model.rs | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/src/mapper/babbage.rs b/src/mapper/babbage.rs index 3da6b3d4..5f9996a0 100644 --- a/src/mapper/babbage.rs +++ b/src/mapper/babbage.rs @@ -65,6 +65,11 @@ impl EventWriter { } } + // Add Collateral Stuff + let collateral_inputs = &body.collateral.as_deref(); + record.collateral_input_count = collateral_inputs.iter().count(); + record.has_collateral_output = body.collateral_return.is_none(); + // TODO // TransactionBodyComponent::ScriptDataHash(_) // TransactionBodyComponent::RequiredSigners(_) @@ -74,6 +79,17 @@ impl EventWriter { record.outputs = outputs.into(); record.inputs = inputs.into(); + // transaction_details collateral stuff + record.collateral_inputs = + collateral_inputs.map(|inputs| self.collect_input_records(inputs)); + + record.collateral_output = body.collateral_return.as_ref().map(|output| match output { + TransactionOutput::Legacy(x) => self.to_legacy_output_record(x).unwrap(), + TransactionOutput::PostAlonzo(x) => { + self.to_post_alonzo_output_record(x).unwrap() + } + }); + record.metadata = match aux_data { Some(aux_data) => self.collect_metadata_records(aux_data)?.into(), None => None, diff --git a/src/model.rs b/src/model.rs index ede19ba6..d779a05e 100644 --- a/src/model.rs +++ b/src/model.rs @@ -160,6 +160,8 @@ pub struct TransactionRecord { pub validity_interval_start: Option, pub network_id: Option, pub input_count: usize, + pub collateral_input_count: usize, + pub has_collateral_output: bool, pub output_count: usize, pub mint_count: usize, pub total_output: u64, @@ -168,6 +170,8 @@ pub struct TransactionRecord { pub metadata: Option>, pub inputs: Option>, pub outputs: Option>, + pub collateral_inputs: Option>, + pub collateral_output: Option, pub mint: Option>, pub vkey_witnesses: Option>, pub native_witnesses: Option>, From d8a7343622db3561efdf79ee5cd958d9f89c0fed Mon Sep 17 00:00:00 2001 From: Clark Alesna Date: Thu, 8 Dec 2022 13:07:09 +0800 Subject: [PATCH 2/3] minor opsie --- src/mapper/babbage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mapper/babbage.rs b/src/mapper/babbage.rs index 5f9996a0..66db1c49 100644 --- a/src/mapper/babbage.rs +++ b/src/mapper/babbage.rs @@ -68,7 +68,7 @@ impl EventWriter { // Add Collateral Stuff let collateral_inputs = &body.collateral.as_deref(); record.collateral_input_count = collateral_inputs.iter().count(); - record.has_collateral_output = body.collateral_return.is_none(); + record.has_collateral_output = body.collateral_return.is_some(); // TODO // TransactionBodyComponent::ScriptDataHash(_) From bdf1b41f51b0fe249e633bb1cabe0cc635955642 Mon Sep 17 00:00:00 2001 From: Clark Alesna Date: Sun, 11 Dec 2022 17:15:47 +0800 Subject: [PATCH 3/3] Added Collateral Return event and modified Collateral Input event to match --- src/filters/fingerprint.rs | 15 +++++++---- src/filters/selection.rs | 2 +- src/mapper/babbage.rs | 31 ++++++++++++++++----- src/mapper/map.rs | 7 ----- src/mapper/shelley.rs | 12 ++++++--- src/model.rs | 52 +++++++++++++++++++++++++++++++++--- src/sinks/terminal/format.rs | 31 +++++++++++++-------- 7 files changed, 113 insertions(+), 37 deletions(-) diff --git a/src/filters/fingerprint.rs b/src/filters/fingerprint.rs index 291d6bdd..d1a720ac 100644 --- a/src/filters/fingerprint.rs +++ b/src/filters/fingerprint.rs @@ -122,6 +122,16 @@ fn build_fingerprint(event: &Event, seed: u32) -> Result { .with_prefix("utxo") .append_optional(&event.context.tx_hash)? .append_optional_to_string(&event.context.output_idx)?, + EventData::CollateralTxInput { .. } => b + .with_slot(&event.context.slot) + .with_prefix("colin") + .append_optional(&event.context.tx_hash)? + .append_optional_to_string(&event.context.input_idx)?, + EventData::CollateralTxOutput { .. } => b + .with_slot(&event.context.slot) + .with_prefix("colout") + .append_optional(&event.context.tx_hash)? + .append_optional_to_string(&event.context.output_idx)?, EventData::OutputAsset(OutputAssetRecord { policy, asset, .. }) => b .with_slot(&event.context.slot) .with_prefix("asst") @@ -140,11 +150,6 @@ fn build_fingerprint(event: &Event, seed: u32) -> Result { .append_optional(&event.context.tx_hash)? .append_slice(policy)? .append_slice(asset)?, - EventData::Collateral { tx_id, index } => b - .with_slot(&event.context.slot) - .with_prefix("coll") - .append_slice(tx_id)? - .append_to_string(index)?, EventData::NativeScript { policy_id, .. } => b .with_slot(&event.context.slot) .with_prefix("scpt") diff --git a/src/filters/selection.rs b/src/filters/selection.rs index 90c2ba1c..e5a4c1b2 100644 --- a/src/filters/selection.rs +++ b/src/filters/selection.rs @@ -8,7 +8,7 @@ use serde_json::Value as JsonValue; use crate::{ model::{ Event, EventData, MetadataRecord, MetadatumRendition, MintRecord, OutputAssetRecord, - TransactionRecord, TxOutputRecord, + TransactionRecord, TxOutputRecord }, pipelining::{new_inter_stage_channel, FilterProvider, PartialBootstrapResult, StageReceiver}, }; diff --git a/src/mapper/babbage.rs b/src/mapper/babbage.rs index 66db1c49..3bf7c627 100644 --- a/src/mapper/babbage.rs +++ b/src/mapper/babbage.rs @@ -8,7 +8,7 @@ use pallas::ledger::primitives::babbage::{ use pallas::crypto::hash::Hash; -use crate::model::{BlockRecord, Era, TransactionRecord}; +use crate::model::{BlockRecord, CollateralTxOutputRecord, Era, TransactionRecord}; use crate::utils::time::TimeProvider; use crate::{ model::{EventContext, EventData}, @@ -85,9 +85,7 @@ impl EventWriter { record.collateral_output = body.collateral_return.as_ref().map(|output| match output { TransactionOutput::Legacy(x) => self.to_legacy_output_record(x).unwrap(), - TransactionOutput::PostAlonzo(x) => { - self.to_post_alonzo_output_record(x).unwrap() - } + TransactionOutput::PostAlonzo(x) => self.to_post_alonzo_output_record(x).unwrap(), }); record.metadata = match aux_data { @@ -287,11 +285,14 @@ impl EventWriter { if let Some(collateral) = &tx.collateral { for (_idx, collateral) in collateral.iter().enumerate() { // TODO: collateral context? - - self.crawl_collateral(collateral)?; + self.crawl_collateral_input(collateral)?; } } + if let Some(collateral_return) = &tx.collateral_return { + self.crawl_collateral_output(collateral_return)?; + } + if let Some(mint) = &tx.mint { self.crawl_mints(mint)?; } @@ -379,4 +380,22 @@ impl EventWriter { let (_, block): (u16, MintedBlock) = pallas::codec::minicbor::decode(cbor)?; self.crawl_babbage_with_cbor(&block, cbor) } + + pub(crate) fn crawl_collateral_output( + &self, + collateral_return: &TransactionOutput, + ) -> Result<(), Error> { + match collateral_return { + TransactionOutput::Legacy(x) => { + let output: CollateralTxOutputRecord = + self.to_legacy_output_record(x).unwrap().into(); + self.append_from(output) + } + TransactionOutput::PostAlonzo(x) => { + let output: CollateralTxOutputRecord = + self.to_post_alonzo_output_record(x).unwrap().into(); + self.append_from(output) + } + } + } } diff --git a/src/mapper/map.rs b/src/mapper/map.rs index 378e7448..5ee89c81 100644 --- a/src/mapper/map.rs +++ b/src/mapper/map.rs @@ -363,13 +363,6 @@ impl EventWriter { } } - pub fn to_collateral_event(&self, collateral: &TransactionInput) -> EventData { - EventData::Collateral { - tx_id: collateral.transaction_id.to_hex(), - index: collateral.index, - } - } - pub fn to_tx_size( &self, body: &KeepRaw, diff --git a/src/mapper/shelley.rs b/src/mapper/shelley.rs index 2bf380cd..0e06481f 100644 --- a/src/mapper/shelley.rs +++ b/src/mapper/shelley.rs @@ -8,6 +8,7 @@ use pallas::ledger::primitives::alonzo::{ use pallas::crypto::hash::Hash; +use crate::model::CollateralTxInputRecord; use crate::{ model::{Era, EventContext, EventData}, Error, @@ -111,8 +112,13 @@ impl EventWriter { // more complex event goes here (eg: pool metadata?) } - pub(crate) fn crawl_collateral(&self, collateral: &TransactionInput) -> Result<(), Error> { - self.append(self.to_collateral_event(collateral)) + pub(crate) fn crawl_collateral_input( + &self, + collateral_input: &TransactionInput, + ) -> Result<(), Error> { + let input: CollateralTxInputRecord = + self.to_transaction_input_record(collateral_input).into(); + self.append_from(input) // TODO: should we have a collateral idx in context? // more complex event goes here (eg: ???) @@ -205,7 +211,7 @@ impl EventWriter { for (_idx, collateral) in collateral.iter().enumerate() { // TODO: collateral context? - self.crawl_collateral(collateral)?; + self.crawl_collateral_input(collateral)?; } } diff --git a/src/model.rs b/src/model.rs index d779a05e..a22b5081 100644 --- a/src/model.rs +++ b/src/model.rs @@ -105,6 +105,27 @@ impl From for EventData { } } +impl From for CollateralTxInputRecord { + fn from(x: TxInputRecord) -> Self { + CollateralTxInputRecord { + tx_id: x.tx_id, + index: x.index, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +pub struct CollateralTxInputRecord { + pub tx_id: String, + pub index: u64, +} + +impl From for EventData { + fn from(x: CollateralTxInputRecord) -> Self { + EventData::CollateralTxInput(x) + } +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct OutputAssetRecord { pub policy: String, @@ -133,6 +154,31 @@ impl From for EventData { } } +impl From for CollateralTxOutputRecord { + fn from(x: TxOutputRecord) -> Self { + CollateralTxOutputRecord { + address: x.address, + amount: x.amount, + assets: x.assets, + datum_hash: x.datum_hash, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub struct CollateralTxOutputRecord { + pub address: String, + pub amount: u64, + pub assets: Option>, + pub datum_hash: Option, +} + +impl From for EventData { + fn from(x: CollateralTxOutputRecord) -> Self { + EventData::CollateralTxOutput(x) + } +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct MintRecord { pub policy: String, @@ -296,6 +342,8 @@ pub enum EventData { TransactionEnd(TransactionRecord), TxInput(TxInputRecord), TxOutput(TxOutputRecord), + CollateralTxInput(CollateralTxInputRecord), + CollateralTxOutput(CollateralTxOutputRecord), OutputAsset(OutputAssetRecord), Metadata(MetadataRecord), @@ -312,10 +360,6 @@ pub enum EventData { CIP15Asset(CIP15AssetRecord), Mint(MintRecord), - Collateral { - tx_id: String, - index: u64, - }, NativeScript { policy_id: String, script: JsonValue, diff --git a/src/sinks/terminal/format.rs b/src/sinks/terminal/format.rs index bdfd1b9e..01f939ca 100644 --- a/src/sinks/terminal/format.rs +++ b/src/sinks/terminal/format.rs @@ -5,10 +5,10 @@ use unicode_truncate::UnicodeTruncateStr; use crate::{ model::{ - BlockRecord, CIP15AssetRecord, CIP25AssetRecord, Event, EventData, MetadataRecord, - MintRecord, NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord, - PlutusRedeemerRecord, PlutusWitnessRecord, TransactionRecord, TxInputRecord, - TxOutputRecord, VKeyWitnessRecord, + BlockRecord, CIP15AssetRecord, CIP25AssetRecord, CollateralTxInputRecord, + CollateralTxOutputRecord, Event, EventData, MetadataRecord, MintRecord, + NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord, PlutusRedeemerRecord, + PlutusWitnessRecord, TransactionRecord, TxInputRecord, TxOutputRecord, VKeyWitnessRecord, }, utils::Utils, }; @@ -129,6 +129,22 @@ impl LogLine { max_width, format!("{{ to: {}, amount: {} }}", address, amount), ), + EventData::CollateralTxInput(CollateralTxInputRecord { tx_id, index }) => LogLine::new_raw( + source, + "COLIN", + Color::Cyan, + max_width, + format!("{{ tx_id: {}, index: {} }}", tx_id, index), + ), + EventData::CollateralTxOutput(CollateralTxOutputRecord { + address, amount, .. + }) => LogLine::new_raw( + source, + "COLOUT", + Color::Cyan, + max_width, + format!("{{ to: {}, amount: {} }}", address, amount), + ), EventData::OutputAsset(OutputAssetRecord { policy, asset, @@ -315,13 +331,6 @@ impl LogLine { max_width, format!("{{ slot: {}, hash: {} }}", block_slot, block_hash), ), - EventData::Collateral { tx_id, index } => LogLine::new_raw( - source, - "COLLAT", - Color::Blue, - max_width, - format!("{{ tx_id: {}, index: {} }}", tx_id, index), - ), EventData::CIP25Asset(CIP25AssetRecord { policy, asset,