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

feat: Add option to include tx details in block events #231

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 26 additions & 2 deletions src/mapper/byron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::model::{BlockRecord, Era, EventData, TransactionRecord, TxInputRecord
use crate::{model::EventContext, Error};

use pallas::crypto::hash::Hash;
use pallas::ledger::primitives::byron::MainBlock;
use pallas::ledger::primitives::{byron, Fragment};

impl EventWriter {
Expand Down Expand Up @@ -98,6 +99,21 @@ impl EventWriter {
Ok(record)
}

pub fn collect_byron_tx_records(
&self,
block: &MainBlock,
) -> Result<Vec<TransactionRecord>, Error> {
block
.body
.tx_payload
.iter()
.map(|tx| {
let tx_hash = tx.transaction.to_hash().to_string();
self.to_byron_transaction_record(&tx, &tx_hash)
})
.collect()
}

fn crawl_byron_transaction(
&self,
source: &byron::TxPayload,
Expand Down Expand Up @@ -142,7 +158,7 @@ impl EventWriter {
hash: &Hash<32>,
cbor: &[u8],
) -> Result<BlockRecord, Error> {
Ok(BlockRecord {
let mut record = BlockRecord {
era: Era::Byron,
body_size: cbor.len() as usize,
issuer_vkey: source.header.consensus_data.1.to_hex(),
Expand All @@ -157,7 +173,14 @@ impl EventWriter {
true => hex::encode(cbor).into(),
false => None,
},
})
transactions: None,
};

if self.config.include_block_details {
record.transactions = Some(self.collect_byron_tx_records(&source)?);
}

Ok(record)
}

fn crawl_byron_main_block(
Expand Down Expand Up @@ -210,6 +233,7 @@ impl EventWriter {
true => hex::encode(cbor).into(),
false => None,
},
transactions: None,
})
}

Expand Down
31 changes: 28 additions & 3 deletions src/mapper/collect.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use pallas::ledger::primitives::alonzo::{
AuxiliaryData, Multiasset, TransactionInput, TransactionOutput, Value,
crypto, AuxiliaryData, Block, Multiasset, TransactionInput, TransactionOutput, Value,
};

use crate::{
model::{MetadataRecord, MintRecord, OutputAssetRecord, TxInputRecord, TxOutputRecord},
model::{
MetadataRecord, MintRecord, OutputAssetRecord, TransactionRecord, TxInputRecord,
TxOutputRecord,
},
Error,
};

use super::EventWriter;
use super::{map::ToHex, shelley, EventWriter};

impl EventWriter {
pub fn collect_input_records(&self, source: &[TransactionInput]) -> Vec<TxInputRecord> {
Expand Down Expand Up @@ -72,4 +75,26 @@ impl EventWriter {
None => Ok(vec![]),
}
}

pub fn collect_shelley_tx_records(
&self,
block: &Block,
) -> Result<Vec<TransactionRecord>, Error> {
block
.transaction_bodies
.iter()
.enumerate()
.map(|(idx, tx)| {
let aux_data = block
.auxiliary_data_set
.iter()
.find(|(k, _)| *k == (idx as u32))
.map(|(_, v)| v);

let tx_hash = crypto::hash_transaction(tx).to_hex();

self.to_transaction_record(&tx, &tx_hash, aux_data)
})
.collect()
}
}
11 changes: 9 additions & 2 deletions src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl EventWriter {
.as_ref()
.map(|time| time.absolute_slot_to_relative(source.header.header_body.slot));

Ok(BlockRecord {
let mut record = BlockRecord {
era,
body_size: source.header.header_body.block_body_size as usize,
issuer_vkey: source.header.header_body.issuer_vkey.to_hex(),
Expand All @@ -381,7 +381,14 @@ impl EventWriter {
true => hex::encode(cbor).into(),
false => None,
},
})
transactions: None,
};

if self.config.include_block_details {
record.transactions = Some(self.collect_shelley_tx_records(source)?);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

}

Ok(record)
}

pub(crate) fn append_rollback_event(&self, point: &Point) -> Result<(), Error> {
Expand Down
3 changes: 3 additions & 0 deletions src/mapper/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct Config {
#[serde(default)]
pub include_transaction_end_events: bool,

#[serde(default)]
pub include_block_details: bool,

#[serde(default)]
pub include_block_cbor: bool,

Expand Down
17 changes: 9 additions & 8 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum Era {
Alonzo,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MetadatumRendition {
MapJson(JsonValue),
Expand All @@ -44,7 +44,7 @@ impl Display for MetadatumRendition {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct MetadataRecord {
pub label: String,

Expand Down Expand Up @@ -76,7 +76,7 @@ impl From<CIP25AssetRecord> for EventData {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct TxInputRecord {
pub tx_id: String,
pub index: u64,
Expand All @@ -88,7 +88,7 @@ impl From<TxInputRecord> for EventData {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct OutputAssetRecord {
pub policy: String,
pub asset: String,
Expand All @@ -101,7 +101,7 @@ impl From<OutputAssetRecord> for EventData {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TxOutputRecord {
pub address: String,
pub amount: u64,
Expand All @@ -114,7 +114,7 @@ impl From<TxOutputRecord> for EventData {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct MintRecord {
pub policy: String,
pub asset: String,
Expand All @@ -127,7 +127,7 @@ impl From<MintRecord> for EventData {
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct TransactionRecord {
pub hash: String,
pub fee: u64,
Expand Down Expand Up @@ -172,7 +172,7 @@ pub enum StakeCredential {
Scripthash(String),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct BlockRecord {
pub era: Era,
pub epoch: Option<u64>,
Expand All @@ -185,6 +185,7 @@ pub struct BlockRecord {
pub number: u64,
pub previous_hash: String,
pub cbor_hex: Option<String>,
pub transactions: Option<Vec<TransactionRecord>>,
}

impl From<BlockRecord> for EventData {
Expand Down