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: Implement Babbage compatibility #351

Merged
merged 3 commits into from
Jul 2, 2022
Merged
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
346 changes: 346 additions & 0 deletions src/mapper/babbage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
use pallas::codec::utils::KeepRaw;
use pallas::ledger::primitives::ToHash;

use pallas::ledger::primitives::babbage::{
AuxiliaryData, MintedBlock, NetworkId, PostAlonzoTransactionOutput, TransactionBody,
TransactionOutput, TransactionWitnessSet,
};

use pallas::crypto::hash::Hash;

use crate::model::{BlockRecord, Era, TransactionRecord};
use crate::utils::time::TimeProvider;
use crate::{
model::{EventContext, EventData},
Error,
};

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

impl EventWriter {
pub fn to_babbage_transaction_record(
&self,
body: &TransactionBody,
tx_hash: &str,
aux_data: Option<&KeepRaw<AuxiliaryData>>,
witness_set: Option<&KeepRaw<TransactionWitnessSet>>,
) -> Result<TransactionRecord, Error> {
let mut record = TransactionRecord::default();

record.hash.push_str(tx_hash);

record.fee = body.fee;
record.ttl = body.ttl;
record.validity_interval_start = body.validity_interval_start;

record.network_id = body.network_id.as_ref().map(|x| match x {
NetworkId::One => 1,
NetworkId::Two => 2,
});

let outputs = self.collect_any_output_records(&body.outputs)?;
record.output_count = outputs.len();
record.total_output = outputs.iter().map(|o| o.amount).sum();

let inputs = self.collect_input_records(&body.inputs);
record.input_count = inputs.len();

if let Some(mint) = &body.mint {
let mints = self.collect_mint_records(mint);
record.mint_count = mints.len();

if self.config.include_transaction_details {
record.mint = mints.into();
}
}

// TODO
// TransactionBodyComponent::ScriptDataHash(_)
// TransactionBodyComponent::RequiredSigners(_)
// TransactionBodyComponent::AuxiliaryDataHash(_)

if self.config.include_transaction_details {
record.outputs = outputs.into();
record.inputs = inputs.into();

record.metadata = match aux_data {
Some(aux_data) => self.collect_metadata_records(aux_data)?.into(),
None => None,
};

if let Some(witnesses) = witness_set {
record.vkey_witnesses = self
.collect_vkey_witness_records(&witnesses.vkeywitness)?
.into();

record.native_witnesses = self
.collect_native_witness_records(&witnesses.native_script)?
.into();

record.plutus_witnesses = self
.collect_plutus_v1_witness_records(&witnesses.plutus_v1_script)?
.into();

record.plutus_redeemers = self
.collect_plutus_redeemer_records(&witnesses.redeemer)?
.into();

record.plutus_data = self
.collect_plutus_datum_records(&witnesses.plutus_data)?
.into();
}
}

Ok(record)
}

pub fn to_babbage_block_record(
&self,
source: &MintedBlock,
hash: &Hash<32>,
cbor: &[u8],
) -> Result<BlockRecord, Error> {
let relative_epoch = self
.utils
.time
.as_ref()
.map(|time| time.absolute_slot_to_relative(source.header.header_body.slot));

let mut record = BlockRecord {
era: Era::Babbage,
body_size: source.header.header_body.block_body_size as usize,
issuer_vkey: source.header.header_body.issuer_vkey.to_hex(),
tx_count: source.transaction_bodies.len(),
hash: hex::encode(hash),
number: source.header.header_body.block_number,
slot: source.header.header_body.slot,
epoch: relative_epoch.map(|(epoch, _)| epoch),
epoch_slot: relative_epoch.map(|(_, epoch_slot)| epoch_slot),
previous_hash: hex::encode(source.header.header_body.prev_hash),
cbor_hex: match self.config.include_block_cbor {
true => hex::encode(cbor).into(),
false => None,
},
transactions: None,
};

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

Ok(record)
}

pub fn collect_babbage_tx_records(
&self,
block: &MintedBlock,
) -> 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 witness_set = block.transaction_witness_sets.get(idx);

let tx_hash = tx.to_hash().to_hex();

self.to_babbage_transaction_record(tx, &tx_hash, aux_data, witness_set)
})
.collect()
}

fn crawl_post_alonzo_output(&self, output: &PostAlonzoTransactionOutput) -> Result<(), Error> {
let record = self.to_post_alonzo_output_record(output)?;
self.append(record.into())?;

let child = &self.child_writer(EventContext {
output_address: self
.utils
.bech32
.encode_address(output.address.as_slice())?
.into(),
..EventContext::default()
});

child.crawl_transaction_output_amount(&output.value)?;

Ok(())
}

fn crawl_babbage_transaction_output(&self, output: &TransactionOutput) -> Result<(), Error> {
match output {
TransactionOutput::Legacy(x) => self.crawl_legacy_output(x),
TransactionOutput::PostAlonzo(x) => self.crawl_post_alonzo_output(x),
}
}

fn crawl_babbage_witness_set(&self, witness_set: &TransactionWitnessSet) -> Result<(), Error> {
if let Some(native) = &witness_set.native_script {
for script in native.iter() {
self.append_from(self.to_native_witness_record(script)?)?;
}
}

if let Some(plutus) = &witness_set.plutus_v1_script {
for script in plutus.iter() {
self.append_from(self.to_plutus_v1_witness_record(script)?)?;
}
}

if let Some(redeemers) = &witness_set.redeemer {
for redeemer in redeemers.iter() {
self.append_from(self.to_plutus_redeemer_record(redeemer)?)?;
}
}

if let Some(datums) = &witness_set.plutus_data {
for datum in datums.iter() {
self.append_from(self.to_plutus_datum_record(datum)?)?;
}
}

Ok(())
}

fn crawl_babbage_transaction(
&self,
tx: &TransactionBody,
tx_hash: &str,
aux_data: Option<&KeepRaw<AuxiliaryData>>,
witness_set: Option<&KeepRaw<TransactionWitnessSet>>,
) -> Result<(), Error> {
let record = self.to_babbage_transaction_record(tx, tx_hash, aux_data, witness_set)?;

self.append_from(record.clone())?;

for (idx, input) in tx.inputs.iter().enumerate() {
let child = self.child_writer(EventContext {
input_idx: Some(idx),
..EventContext::default()
});

child.crawl_transaction_input(input)?;
}

for (idx, output) in tx.outputs.iter().enumerate() {
let child = self.child_writer(EventContext {
output_idx: Some(idx),
..EventContext::default()
});

child.crawl_babbage_transaction_output(output)?;
}

if let Some(certs) = &tx.certificates {
for (idx, cert) in certs.iter().enumerate() {
let child = self.child_writer(EventContext {
certificate_idx: Some(idx),
..EventContext::default()
});

child.crawl_certificate(cert)?;
}
}

if let Some(collateral) = &tx.collateral {
for (_idx, collateral) in collateral.iter().enumerate() {
// TODO: collateral context?

self.crawl_collateral(collateral)?;
}
}

if let Some(mint) = &tx.mint {
self.crawl_mints(mint)?;
}

if let Some(aux_data) = aux_data {
self.crawl_auxdata(aux_data)?;
}

if let Some(witness_set) = witness_set {
self.crawl_babbage_witness_set(witness_set)?;
}

if self.config.include_transaction_end_events {
self.append(EventData::TransactionEnd(record))?;
}

Ok(())
}

fn crawl_babbage_block(
&self,
block: &MintedBlock,
hash: &Hash<32>,
cbor: &[u8],
) -> Result<(), Error> {
let record = self.to_babbage_block_record(block, hash, cbor)?;

self.append(EventData::Block(record.clone()))?;

for (idx, tx) in block.transaction_bodies.iter().enumerate() {
let aux_data = block
.auxiliary_data_set
.iter()
.find(|(k, _)| *k == (idx as u32))
.map(|(_, v)| v);

let witness_set = block.transaction_witness_sets.get(idx);

let tx_hash = tx.to_hash().to_hex();

let child = self.child_writer(EventContext {
tx_idx: Some(idx),
tx_hash: Some(tx_hash.to_owned()),
..EventContext::default()
});

child.crawl_babbage_transaction(tx, &tx_hash, aux_data, witness_set)?;
}

if self.config.include_block_end_events {
self.append(EventData::BlockEnd(record))?;
}

Ok(())
}

/// Mapper entry-point for decoded Babbage blocks
///
/// Entry-point to start crawling a blocks for events. Meant to be used when
/// we already have a decoded block (for example, N2C). The raw CBOR is also
/// passed through in case we need to attach it to outbound events.
pub fn crawl_babbage_with_cbor<'b>(
&self,
block: &'b MintedBlock<'b>,
cbor: &'b [u8],
) -> Result<(), Error> {
let hash = block.header.to_hash();

let child = self.child_writer(EventContext {
block_hash: Some(hex::encode(&hash)),
block_number: Some(block.header.header_body.block_number),
slot: Some(block.header.header_body.slot),
timestamp: self.compute_timestamp(block.header.header_body.slot),
..EventContext::default()
});

child.crawl_babbage_block(block, &hash, cbor)
}

/// Mapper entry-point for raw Babbage cbor blocks
///
/// Entry-point to start crawling a blocks for events. Meant to be used when
/// we haven't decoded the CBOR yet (for example, N2N).
pub fn crawl_from_babbage_cbor(&self, cbor: &[u8]) -> Result<(), Error> {
let (_, block): (u16, MintedBlock) = pallas::codec::minicbor::decode(cbor)?;
self.crawl_babbage_with_cbor(&block, cbor)
}
}
Loading