Skip to content

Commit

Permalink
refactor: utxo handling and streamline byte decoding
Browse files Browse the repository at this point in the history
- add a new function `inspect` in `codec.rs` to decode `r` instances from bytes
- remove the `inspect` function from `pallas_observer.rs` and use the `inspect` function from `codec.rs` instead for decoding bytes
- refactor replacement of `values` with `utxo` in `get_datum_bytes`, `inspect_datum`, and `serialize_datum` functions
- change `map_datums` function to process `transaction.utxo` instead of `utxo.utxo`
  • Loading branch information
falcucci committed Dec 21, 2023
1 parent 227c607 commit 09f1404
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
8 changes: 8 additions & 0 deletions mithril-common/src/chain_observer/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ impl ToCanonicalJson for Datum {
self.0.to_json()
}
}

/// Inspects the given bytes and returns a decoded `R` instance.
pub fn inspect<R>(inner: Vec<u8>) -> R
where
for<'b> R: pallas_codec::minicbor::Decode<'b, ()>,
{
minicbor::decode(&inner).unwrap()
}
34 changes: 14 additions & 20 deletions mithril-common/src/chain_observer/pallas_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::entities::StakeDistribution;
use crate::CardanoNetwork;
use crate::{entities::Epoch, StdResult};

use super::codec::inspect;
use super::model::{Datum, Datums};
use super::CardanoCliChainObserver;

Expand Down Expand Up @@ -83,42 +84,35 @@ impl PallasChainObserver {
Ok(epoch)
}

/// Inspects the given bytes and returns a decoded `R` instance.
fn inspect<R>(&self, inner: Vec<u8>) -> R
where
for<'b> R: pallas_codec::minicbor::Decode<'b, ()>,
{
minicbor::decode(&inner).unwrap()
}

/// Returns inline datum bytes from the given `Values` instance.
fn get_datum_bytes(&self, values: &Values) -> Vec<u8> {
let bytes = values.inline_datum.as_ref().unwrap().1.clone();
fn get_datum_bytes(&self, utxo: &Values) -> Vec<u8> {
let bytes = utxo.inline_datum.as_ref().unwrap().1.clone();
let bytes = CborWrap(bytes).to_vec();
self.inspect(bytes)
inspect(bytes)
}

/// Returns inline datums from the given `Values` instance.
fn inspect_datum(&self, values: &Values) -> Datum {
let datum = self.get_datum_bytes(values);
fn inspect_datum(&self, utxo: &Values) -> Datum {
let datum = self.get_datum_bytes(utxo);

self.inspect(datum)
inspect(datum)
}

/// Serializes datum to `TxDatum` instance.
fn serialize_datum(&self, values: &Values) -> TxDatum {
let datum = self.inspect_datum(values);
fn serialize_datum(&self, utxo: &Values) -> TxDatum {
let datum = self.inspect_datum(utxo);
let serialized = serde_json::to_string(&datum.to_json()).expect("Failed to serialize");

TxDatum(serialized)
}

/// Maps the given `UTxOByAddress` instance to Datums.
fn map_datums(&self, utxo: UTxOByAddress) -> Datums {
utxo.utxo
fn map_datums(&self, transaction: UTxOByAddress) -> Datums {
transaction
.utxo
.iter()
.filter(|(_, values)| values.inline_datum.is_some())
.map(|(_, values)| self.serialize_datum(values))
.filter(|(_, utxo)| utxo.inline_datum.is_some())
.map(|(_, utxo)| self.serialize_datum(utxo))
.collect()
}

Expand Down

0 comments on commit 09f1404

Please sign in to comment.