chore(traverse): Improve API ergonomics (#233)

This commit is contained in:
Santiago Carmuega 2023-03-13 19:19:40 +01:00 committed by GitHub
parent 8fa662b4de
commit c1446ebea5
7 changed files with 164 additions and 38 deletions

View file

@ -623,7 +623,9 @@ impl<C> minicbor::encode::Encode<C> for Certificate {
} }
} }
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] #[derive(
Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy,
)]
#[cbor(index_only)] #[cbor(index_only)]
pub enum NetworkId { pub enum NetworkId {
#[n(0)] #[n(0)]

View file

@ -26,13 +26,20 @@ impl<'b> MultiEraAsset<'b> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
pub fn policy_id(&self) -> Option<&Hash<28>> { pub fn policy(&self) -> Option<&Hash<28>> {
match self { match self {
Self::AlonzoCompatible(x, ..) => Some(*x), Self::AlonzoCompatible(x, ..) => Some(*x),
Self::Lovelace(_) => None, Self::Lovelace(_) => None,
} }
} }
pub fn name(&self) -> Option<&[u8]> {
match self {
Self::AlonzoCompatible(_, n, _) => Some(n.as_ref()),
Self::Lovelace(_) => None,
}
}
pub fn coin(&self) -> i64 { pub fn coin(&self) -> i64 {
match self { match self {
Self::AlonzoCompatible(_, _, x) => *x, Self::AlonzoCompatible(_, _, x) => *x,

View file

@ -77,6 +77,33 @@ impl<'b> MultiEraHeader<'b> {
} }
} }
pub fn previous_hash(&self) -> Option<Hash<32>> {
match self {
MultiEraHeader::AlonzoCompatible(x) => x.header_body.prev_hash,
MultiEraHeader::Babbage(x) => x.header_body.prev_hash,
MultiEraHeader::EpochBoundary(x) => Some(x.prev_block),
MultiEraHeader::Byron(x) => Some(x.prev_block),
}
}
pub fn vrf_vkey(&self) -> Option<&[u8]> {
match self {
MultiEraHeader::AlonzoCompatible(x) => Some(x.header_body.vrf_vkey.as_ref()),
MultiEraHeader::Babbage(x) => Some(x.header_body.vrf_vkey.as_ref()),
MultiEraHeader::EpochBoundary(_) => None,
MultiEraHeader::Byron(_) => None,
}
}
pub fn issuer_vkey(&self) -> Option<&[u8]> {
match self {
MultiEraHeader::AlonzoCompatible(x) => Some(x.header_body.issuer_vkey.as_ref()),
MultiEraHeader::Babbage(x) => Some(x.header_body.issuer_vkey.as_ref()),
MultiEraHeader::EpochBoundary(_) => None,
MultiEraHeader::Byron(_) => None,
}
}
pub fn leader_vrf_output(&self) -> Result<Vec<u8>, Error> { pub fn leader_vrf_output(&self) -> Result<Vec<u8>, Error> {
match self { match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Era::Byron)), MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Era::Byron)),

View file

@ -24,6 +24,7 @@ pub mod meta;
pub mod output; pub mod output;
pub mod probe; pub mod probe;
pub mod signers; pub mod signers;
pub mod size;
pub mod time; pub mod time;
pub mod tx; pub mod tx;
pub mod withdrawals; pub mod withdrawals;

View file

@ -0,0 +1,52 @@
use pallas_codec::utils::Nullable;
use crate::{MultiEraBlock, MultiEraTx};
impl<'b> MultiEraTx<'b> {
fn aux_data_size(&self) -> usize {
match self {
MultiEraTx::AlonzoCompatible(x, _) => match &x.auxiliary_data {
Nullable::Some(x) => x.raw_cbor().len(),
_ => 2,
},
MultiEraTx::Babbage(x) => match &x.auxiliary_data {
Nullable::Some(x) => x.raw_cbor().len(),
_ => 2,
},
MultiEraTx::Byron(_) => 0,
}
}
fn body_size(&self) -> usize {
match self {
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.raw_cbor().len(),
MultiEraTx::Babbage(x) => x.transaction_body.raw_cbor().len(),
MultiEraTx::Byron(x) => x.transaction.raw_cbor().len(),
}
}
fn witness_set_size(&self) -> usize {
match self {
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_witness_set.raw_cbor().len(),
MultiEraTx::Babbage(x) => x.transaction_witness_set.raw_cbor().len(),
MultiEraTx::Byron(x) => x.witness.raw_cbor().len(),
}
}
pub fn size(&self) -> usize {
self.body_size() + self.witness_set_size() + self.aux_data_size()
}
}
impl<'b> MultiEraBlock<'b> {
pub fn body_size(&self) -> Option<usize> {
match self {
MultiEraBlock::AlonzoCompatible(x, _) => {
Some(x.header.header_body.block_body_size as usize)
}
MultiEraBlock::Babbage(x) => Some(x.header.header_body.block_body_size as usize),
MultiEraBlock::EpochBoundary(_) => None,
MultiEraBlock::Byron(_) => None,
}
}
}

View file

@ -2,7 +2,11 @@ use std::{borrow::Cow, ops::Deref};
use pallas_codec::{minicbor, utils::KeepRaw}; use pallas_codec::{minicbor, utils::KeepRaw};
use pallas_crypto::hash::Hash; use pallas_crypto::hash::Hash;
use pallas_primitives::{alonzo, babbage, byron}; use pallas_primitives::{
alonzo,
babbage::{self, NetworkId},
byron,
};
use crate::{ use crate::{
Era, MultiEraAsset, MultiEraCert, MultiEraInput, MultiEraMeta, MultiEraOutput, MultiEraSigners, Era, MultiEraAsset, MultiEraCert, MultiEraInput, MultiEraMeta, MultiEraOutput, MultiEraSigners,
@ -305,6 +309,14 @@ impl<'b> MultiEraTx<'b> {
} }
} }
pub fn ttl(&self) -> Option<u64> {
match self {
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.ttl,
MultiEraTx::Babbage(x) => x.transaction_body.ttl,
MultiEraTx::Byron(_) => None,
}
}
/// Returns the fee or attempts to compute it /// Returns the fee or attempts to compute it
/// ///
/// If the fee is available as part of the tx data (post-byron), this /// If the fee is available as part of the tx data (post-byron), this
@ -370,6 +382,22 @@ impl<'b> MultiEraTx<'b> {
} }
} }
pub fn validity_start(&self) -> Option<u64> {
match self {
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.validity_interval_start,
MultiEraTx::Babbage(x) => x.transaction_body.validity_interval_start,
MultiEraTx::Byron(_) => None,
}
}
pub fn network_id(&self) -> Option<NetworkId> {
match self {
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.network_id,
MultiEraTx::Babbage(x) => x.transaction_body.network_id,
MultiEraTx::Byron(_) => None,
}
}
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
match self { match self {
MultiEraTx::AlonzoCompatible(x, _) => x.success, MultiEraTx::AlonzoCompatible(x, _) => x.success,

View file

@ -7,114 +7,123 @@ use pallas_primitives::{
use crate::MultiEraTx; use crate::MultiEraTx;
impl<'b> MultiEraTx<'b> { impl<'b> MultiEraTx<'b> {
pub fn vkey_witnesses(&self) -> Option<&[VKeyWitness]> { pub fn vkey_witnesses(&self) -> &[VKeyWitness] {
match self { match self {
Self::AlonzoCompatible(x, _) => x Self::AlonzoCompatible(x, _) => x
.transaction_witness_set .transaction_witness_set
.vkeywitness .vkeywitness
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
.unwrap_or(&[]),
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.vkeywitness .vkeywitness
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
_ => None, .unwrap_or(&[]),
_ => &[],
} }
} }
pub fn native_scripts(&self) -> Option<&[NativeScript]> { pub fn native_scripts(&self) -> &[NativeScript] {
match self { match self {
Self::AlonzoCompatible(x, _) => x Self::AlonzoCompatible(x, _) => x
.transaction_witness_set .transaction_witness_set
.native_script .native_script
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
.unwrap_or(&[]),
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.native_script .native_script
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
_ => None, .unwrap_or(&[]),
_ => &[],
} }
} }
pub fn bootstrap_witnesses(&self) -> Option<&[BootstrapWitness]> { pub fn bootstrap_witnesses(&self) -> &[BootstrapWitness] {
match self { match self {
Self::AlonzoCompatible(x, _) => x Self::AlonzoCompatible(x, _) => x
.transaction_witness_set .transaction_witness_set
.bootstrap_witness .bootstrap_witness
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
.unwrap_or(&[]),
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.bootstrap_witness .bootstrap_witness
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
_ => None, .unwrap_or(&[]),
_ => &[],
} }
} }
pub fn plutus_v1_scripts(&self) -> Vec<&alonzo::PlutusScript> { pub fn plutus_v1_scripts(&self) -> &[alonzo::PlutusScript] {
match self { match self {
Self::AlonzoCompatible(x, _) => x Self::AlonzoCompatible(x, _) => x
.transaction_witness_set .transaction_witness_set
.plutus_script .plutus_script
.iter() .as_ref()
.flatten() .map(|x| x.as_ref())
.collect(), .unwrap_or(&[]),
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.plutus_v1_script .plutus_v1_script
.iter() .as_ref()
.flatten() .map(|x| x.as_ref())
.collect(), .unwrap_or(&[]),
_ => vec![], _ => &[],
} }
} }
pub fn plutus_data(&self) -> Vec<&KeepRaw<'b, PlutusData>> { pub fn plutus_data(&self) -> &[KeepRaw<'b, PlutusData>] {
match self { match self {
Self::AlonzoCompatible(x, _) => x Self::AlonzoCompatible(x, _) => x
.transaction_witness_set .transaction_witness_set
.plutus_data .plutus_data
.iter() .as_ref()
.flatten() .map(|x| x.as_ref())
.collect(), .unwrap_or(&[]),
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.plutus_data .plutus_data
.iter() .as_ref()
.flatten() .map(|x| x.as_ref())
.collect(), .unwrap_or(&[]),
_ => std::iter::empty().collect(), _ => &[],
} }
} }
pub fn redeemers(&self) -> Option<&[Redeemer]> { pub fn redeemers(&self) -> &[Redeemer] {
match self { match self {
Self::AlonzoCompatible(x, _) => x Self::AlonzoCompatible(x, _) => x
.transaction_witness_set .transaction_witness_set
.redeemer .redeemer
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
.unwrap_or(&[]),
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.redeemer .redeemer
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
_ => None, .unwrap_or(&[]),
_ => &[],
} }
} }
pub fn plutus_v2_scripts(&self) -> Option<&[PlutusV2Script]> { pub fn plutus_v2_scripts(&self) -> &[PlutusV2Script] {
match self { match self {
Self::Babbage(x) => x Self::Babbage(x) => x
.transaction_witness_set .transaction_witness_set
.plutus_v2_script .plutus_v2_script
.as_ref() .as_ref()
.map(|x| x.as_ref()), .map(|x| x.as_ref())
_ => None, .unwrap_or(&[]),
_ => &[],
} }
} }
} }