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

fix: Add missing details in Tx record #321

Merged
merged 1 commit into from
Jun 8, 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
73 changes: 69 additions & 4 deletions src/mapper/collect.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -77,6 +81,65 @@ impl EventWriter {
}
}

pub fn collect_vkey_witness_records(
&self,
witness_set: &TransactionWitnessSet,
) -> Result<Vec<VKeyWitnessRecord>, 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<Vec<NativeWitnessRecord>, 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<Vec<PlutusWitnessRecord>, 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<Vec<PlutusRedeemerRecord>, 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<Vec<PlutusDatumRecord>, 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,
Expand All @@ -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()
}
Expand Down
22 changes: 21 additions & 1 deletion src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -258,6 +259,16 @@ impl EventWriter {
})
}

pub fn to_vkey_witness_record(
&self,
witness: &alonzo::VKeyWitness,
) -> Result<VKeyWitnessRecord, crate::Error> {
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 {
Expand Down Expand Up @@ -330,6 +341,7 @@ impl EventWriter {
body: &TransactionBody,
tx_hash: &str,
aux_data: Option<&AuxiliaryData>,
witness_set: Option<&TransactionWitnessSet>,
) -> Result<TransactionRecord, Error> {
let mut record = TransactionRecord::default();

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/mapper/shelley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;

Expand Down
15 changes: 10 additions & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ pub struct TransactionRecord {
pub inputs: Option<Vec<TxInputRecord>>,
pub outputs: Option<Vec<TxOutputRecord>>,
pub mint: Option<Vec<MintRecord>>,
pub vkey_witnesses: Option<Vec<VKeyWitnessRecord>>,
pub native_witnesses: Option<Vec<NativeWitnessRecord>>,
pub plutus_witnesses: Option<Vec<PlutusWitnessRecord>>,
pub plutus_redeemers: Option<Vec<PlutusRedeemerRecord>>,
pub plutus_data: Option<Vec<PlutusDatumRecord>>,
}

impl From<TransactionRecord> for EventData {
Expand Down Expand Up @@ -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,
Expand All @@ -206,7 +211,7 @@ impl From<NativeWitnessRecord> 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,
Expand All @@ -218,7 +223,7 @@ impl From<PlutusWitnessRecord> 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,
Expand All @@ -233,7 +238,7 @@ impl From<PlutusRedeemerRecord> 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,
Expand Down