From 333440b0febdad0094cde2e33e986ec6ff67d817 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Wed, 8 Jun 2022 09:46:02 -0300 Subject: [PATCH] fix: Add missing details in tx record --- src/mapper/collect.rs | 73 ++++++++++++++++++++++++++++++++++++++++--- src/mapper/map.rs | 22 ++++++++++++- src/mapper/shelley.rs | 2 +- src/model.rs | 15 ++++++--- 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/mapper/collect.rs b/src/mapper/collect.rs index ad5b93f5..9e1e7219 100644 --- a/src/mapper/collect.rs +++ b/src/mapper/collect.rs @@ -1,12 +1,16 @@ use pallas::ledger::primitives::{ - alonzo::{AuxiliaryData, Block, Multiasset, TransactionInput, TransactionOutput, Value}, + alonzo::{ + AuxiliaryData, Block, Multiasset, TransactionInput, TransactionOutput, + TransactionWitnessSet, Value, + }, ToHash, }; use crate::{ model::{ - MetadataRecord, MintRecord, OutputAssetRecord, TransactionRecord, TxInputRecord, - TxOutputRecord, + MetadataRecord, MintRecord, NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord, + PlutusRedeemerRecord, PlutusWitnessRecord, TransactionRecord, TxInputRecord, + TxOutputRecord, VKeyWitnessRecord, }, Error, }; @@ -77,6 +81,65 @@ impl EventWriter { } } + pub fn collect_vkey_witness_records( + &self, + witness_set: &TransactionWitnessSet, + ) -> Result, Error> { + match &witness_set.vkeywitness { + Some(all) => all.iter().map(|i| self.to_vkey_witness_record(i)).collect(), + None => Ok(vec![]), + } + } + + pub fn collect_native_witness_records( + &self, + witness_set: &TransactionWitnessSet, + ) -> Result, Error> { + match &witness_set.native_script { + Some(all) => all + .iter() + .map(|i| self.to_native_witness_record(i)) + .collect(), + None => Ok(vec![]), + } + } + + pub fn collect_plutus_witness_records( + &self, + witness_set: &TransactionWitnessSet, + ) -> Result, Error> { + match &witness_set.plutus_script { + Some(all) => all + .iter() + .map(|i| self.to_plutus_witness_record(i)) + .collect(), + None => Ok(vec![]), + } + } + + pub fn collect_plutus_redeemer_records( + &self, + witness_set: &TransactionWitnessSet, + ) -> Result, Error> { + match &witness_set.redeemer { + Some(all) => all + .iter() + .map(|i| self.to_plutus_redeemer_record(i)) + .collect(), + None => Ok(vec![]), + } + } + + pub fn collect_plutus_datum_records( + &self, + witness_set: &TransactionWitnessSet, + ) -> Result, Error> { + match &witness_set.plutus_data { + Some(all) => all.iter().map(|i| self.to_plutus_datum_record(i)).collect(), + None => Ok(vec![]), + } + } + pub fn collect_shelley_tx_records( &self, block: &Block, @@ -92,9 +155,11 @@ impl EventWriter { .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_transaction_record(tx, &tx_hash, aux_data) + self.to_transaction_record(tx, &tx_hash, aux_data, witness_set) }) .collect() } diff --git a/src/mapper/map.rs b/src/mapper/map.rs index b9ac0a1f..5ed68caa 100644 --- a/src/mapper/map.rs +++ b/src/mapper/map.rs @@ -5,7 +5,7 @@ use pallas::crypto::hash::Hash; use pallas::ledger::primitives::alonzo::{ self as alonzo, AuxiliaryData, Block, Certificate, InstantaneousRewardSource, InstantaneousRewardTarget, Metadatum, MetadatumLabel, Relay, TransactionInput, - TransactionOutput, Value, + TransactionOutput, TransactionWitnessSet, Value, }; use pallas::ledger::primitives::alonzo::{NetworkId, TransactionBody, TransactionBodyComponent}; use pallas::ledger::primitives::{ToCanonicalJson, ToHash}; @@ -17,6 +17,7 @@ use crate::model::{ BlockRecord, Era, EventData, MetadataRecord, MetadatumRendition, MintRecord, NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord, PlutusRedeemerRecord, PlutusWitnessRecord, StakeCredential, TransactionRecord, TxInputRecord, TxOutputRecord, + VKeyWitnessRecord, }; use crate::utils::time::TimeProvider; @@ -258,6 +259,16 @@ impl EventWriter { }) } + pub fn to_vkey_witness_record( + &self, + witness: &alonzo::VKeyWitness, + ) -> Result { + Ok(VKeyWitnessRecord { + vkey_hex: witness.vkey.to_hex(), + signature_hex: witness.signature.to_hex(), + }) + } + pub fn to_certificate_event(&self, certificate: &Certificate) -> EventData { match certificate { Certificate::StakeRegistration(credential) => EventData::StakeRegistration { @@ -330,6 +341,7 @@ impl EventWriter { body: &TransactionBody, tx_hash: &str, aux_data: Option<&AuxiliaryData>, + witness_set: Option<&TransactionWitnessSet>, ) -> Result { let mut record = TransactionRecord::default(); @@ -390,6 +402,14 @@ impl EventWriter { 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)?.into(); + record.native_witnesses = self.collect_native_witness_records(witnesses)?.into(); + record.plutus_witnesses = self.collect_plutus_witness_records(witnesses)?.into(); + record.plutus_redeemers = self.collect_plutus_redeemer_records(witnesses)?.into(); + record.plutus_data = self.collect_plutus_datum_records(witnesses)?.into(); + } } Ok(record) diff --git a/src/mapper/shelley.rs b/src/mapper/shelley.rs index c1eca4f5..d0a733bb 100644 --- a/src/mapper/shelley.rs +++ b/src/mapper/shelley.rs @@ -167,7 +167,7 @@ impl EventWriter { aux_data: Option<&AuxiliaryData>, witness_set: Option<&TransactionWitnessSet>, ) -> Result<(), Error> { - let record = self.to_transaction_record(tx, tx_hash, aux_data)?; + let record = self.to_transaction_record(tx, tx_hash, aux_data, witness_set)?; self.append_from(record.clone())?; diff --git a/src/model.rs b/src/model.rs index 085b3cb8..ec995770 100644 --- a/src/model.rs +++ b/src/model.rs @@ -160,6 +160,11 @@ pub struct TransactionRecord { pub inputs: Option>, pub outputs: Option>, pub mint: Option>, + pub vkey_witnesses: Option>, + pub native_witnesses: Option>, + pub plutus_witnesses: Option>, + pub plutus_redeemers: Option>, + pub plutus_data: Option>, } impl From for EventData { @@ -188,13 +193,13 @@ pub enum StakeCredential { Scripthash(String), } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct VKeyWitnessRecord { pub vkey_hex: String, pub signature_hex: String, } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct NativeWitnessRecord { pub policy_id: String, pub script_json: JsonValue, @@ -206,7 +211,7 @@ impl From for EventData { } } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct PlutusWitnessRecord { pub script_hash: String, pub script_hex: String, @@ -218,7 +223,7 @@ impl From for EventData { } } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct PlutusRedeemerRecord { pub purpose: String, pub ex_units_mem: u32, @@ -233,7 +238,7 @@ impl From for EventData { } } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct PlutusDatumRecord { pub datum_hash: String, pub plutus_data: JsonValue,