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

Added collateral return event and modified collateral input event to match #496

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/filters/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ fn build_fingerprint(event: &Event, seed: u32) -> Result<String, Error> {
.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")
Expand All @@ -140,11 +150,6 @@ fn build_fingerprint(event: &Event, seed: u32) -> Result<String, Error> {
.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")
Expand Down
31 changes: 25 additions & 6 deletions src/mapper/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -288,11 +286,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)?;
}
Expand Down Expand Up @@ -380,4 +381,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)
}
}
}
}
7 changes: 0 additions & 7 deletions src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionBody>,
Expand Down
12 changes: 9 additions & 3 deletions src/mapper/shelley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: ???)
Expand Down Expand Up @@ -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)?;
}
}

Expand Down
52 changes: 48 additions & 4 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ impl From<TxInputRecord> for EventData {
}
}

impl From<TxInputRecord> 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<CollateralTxInputRecord> 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,
Expand Down Expand Up @@ -133,6 +154,31 @@ impl From<TxOutputRecord> for EventData {
}
}

impl From<TxOutputRecord> 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<Vec<OutputAssetRecord>>,
pub datum_hash: Option<String>,
}

impl From<CollateralTxOutputRecord> 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,
Expand Down Expand Up @@ -297,6 +343,8 @@ pub enum EventData {
TransactionEnd(TransactionRecord),
TxInput(TxInputRecord),
TxOutput(TxOutputRecord),
CollateralTxInput(CollateralTxInputRecord),
CollateralTxOutput(CollateralTxOutputRecord),
OutputAsset(OutputAssetRecord),
Metadata(MetadataRecord),

Expand All @@ -313,10 +361,6 @@ pub enum EventData {
CIP15Asset(CIP15AssetRecord),

Mint(MintRecord),
Collateral {
tx_id: String,
index: u64,
},
NativeScript {
policy_id: String,
script: JsonValue,
Expand Down
31 changes: 20 additions & 11 deletions src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down