feat: introduce conway primitives (#290)
Co-authored-by: lisicky <lisicky@inbox.ru>
This commit is contained in:
parent
fb1cc54b0c
commit
bf973dff3c
19 changed files with 2419 additions and 34 deletions
|
|
@ -2,7 +2,7 @@ use std::borrow::Cow;
|
|||
|
||||
use pallas_codec::minicbor;
|
||||
use pallas_crypto::hash::Hash;
|
||||
use pallas_primitives::{alonzo, babbage, byron};
|
||||
use pallas_primitives::{alonzo, babbage, byron, conway};
|
||||
|
||||
use crate::{probe, support, Era, Error, MultiEraBlock, MultiEraHeader, MultiEraTx};
|
||||
|
||||
|
|
@ -58,6 +58,13 @@ impl<'b> MultiEraBlock<'b> {
|
|||
Ok(Self::Babbage(Box::new(block)))
|
||||
}
|
||||
|
||||
pub fn decode_conway(cbor: &'b [u8]) -> Result<Self, Error> {
|
||||
let (_, block): BlockWrapper<conway::MintedBlock> =
|
||||
minicbor::decode(cbor).map_err(Error::invalid_cbor)?;
|
||||
|
||||
Ok(Self::Conway(Box::new(block)))
|
||||
}
|
||||
|
||||
pub fn decode(cbor: &'b [u8]) -> Result<MultiEraBlock<'b>, Error> {
|
||||
match probe::block_era(cbor) {
|
||||
probe::Outcome::EpochBoundary => Self::decode_epoch_boundary(cbor),
|
||||
|
|
@ -68,6 +75,7 @@ impl<'b> MultiEraBlock<'b> {
|
|||
Era::Mary => Self::decode_mary(cbor),
|
||||
Era::Alonzo => Self::decode_alonzo(cbor),
|
||||
Era::Babbage => Self::decode_babbage(cbor),
|
||||
Era::Conway => Self::decode_conway(cbor),
|
||||
},
|
||||
probe::Outcome::Inconclusive => Err(Error::unknown_cbor(cbor)),
|
||||
}
|
||||
|
|
@ -83,6 +91,7 @@ impl<'b> MultiEraBlock<'b> {
|
|||
MultiEraHeader::AlonzoCompatible(Cow::Borrowed(&x.header))
|
||||
}
|
||||
MultiEraBlock::Babbage(x) => MultiEraHeader::Babbage(Cow::Borrowed(&x.header)),
|
||||
MultiEraBlock::Conway(x) => MultiEraHeader::Babbage(Cow::Borrowed(&x.header)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97,6 +106,7 @@ impl<'b> MultiEraBlock<'b> {
|
|||
MultiEraBlock::AlonzoCompatible(_, x) => *x,
|
||||
MultiEraBlock::Babbage(_) => Era::Babbage,
|
||||
MultiEraBlock::Byron(_) => Era::Byron,
|
||||
MultiEraBlock::Conway(_) => Era::Conway,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -123,6 +133,10 @@ impl<'b> MultiEraBlock<'b> {
|
|||
.into_iter()
|
||||
.map(|x| MultiEraTx::Byron(Box::new(Cow::Owned(x))))
|
||||
.collect(),
|
||||
MultiEraBlock::Conway(x) => support::clone_conway_txs(x)
|
||||
.into_iter()
|
||||
.map(|x| MultiEraTx::Conway(Box::new(Cow::Owned(x))))
|
||||
.collect(),
|
||||
MultiEraBlock::EpochBoundary(_) => vec![],
|
||||
}
|
||||
}
|
||||
|
|
@ -134,6 +148,7 @@ impl<'b> MultiEraBlock<'b> {
|
|||
MultiEraBlock::AlonzoCompatible(x, _) => x.transaction_bodies.is_empty(),
|
||||
MultiEraBlock::Babbage(x) => x.transaction_bodies.is_empty(),
|
||||
MultiEraBlock::Byron(x) => x.body.tx_payload.is_empty(),
|
||||
MultiEraBlock::Conway(x) => x.transaction_bodies.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -144,6 +159,7 @@ impl<'b> MultiEraBlock<'b> {
|
|||
MultiEraBlock::AlonzoCompatible(x, _) => x.transaction_bodies.len(),
|
||||
MultiEraBlock::Babbage(x) => x.transaction_bodies.len(),
|
||||
MultiEraBlock::Byron(x) => x.body.tx_payload.len(),
|
||||
MultiEraBlock::Conway(x) => x.transaction_bodies.len(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -154,33 +170,46 @@ impl<'b> MultiEraBlock<'b> {
|
|||
MultiEraBlock::AlonzoCompatible(x, _) => !x.auxiliary_data_set.is_empty(),
|
||||
MultiEraBlock::Babbage(x) => !x.auxiliary_data_set.is_empty(),
|
||||
MultiEraBlock::Byron(_) => false,
|
||||
MultiEraBlock::Conway(x) => !x.auxiliary_data_set.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_alonzo(&self) -> Option<&alonzo::MintedBlock> {
|
||||
match self {
|
||||
MultiEraBlock::EpochBoundary(_) => None,
|
||||
MultiEraBlock::AlonzoCompatible(x, _) => Some(x),
|
||||
MultiEraBlock::Babbage(_) => None,
|
||||
MultiEraBlock::Byron(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_babbage(&self) -> Option<&babbage::MintedBlock> {
|
||||
match self {
|
||||
MultiEraBlock::EpochBoundary(_) => None,
|
||||
MultiEraBlock::AlonzoCompatible(_, _) => None,
|
||||
MultiEraBlock::Babbage(x) => Some(x),
|
||||
MultiEraBlock::Byron(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_byron(&self) -> Option<&byron::MintedBlock> {
|
||||
match self {
|
||||
MultiEraBlock::EpochBoundary(_) => None,
|
||||
MultiEraBlock::AlonzoCompatible(_, _) => None,
|
||||
MultiEraBlock::Babbage(_) => None,
|
||||
MultiEraBlock::Byron(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_conway(&self) -> Option<&conway::MintedBlock> {
|
||||
match self {
|
||||
MultiEraBlock::Conway(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the size of the serialised block in bytes
|
||||
pub fn size(&self) -> usize {
|
||||
match self {
|
||||
MultiEraBlock::EpochBoundary(b) => minicbor::to_vec(b).unwrap().len(),
|
||||
MultiEraBlock::Byron(b) => minicbor::to_vec(b).unwrap().len(),
|
||||
MultiEraBlock::AlonzoCompatible(b, _) => minicbor::to_vec(b).unwrap().len(),
|
||||
MultiEraBlock::Babbage(b) => minicbor::to_vec(b).unwrap().len(),
|
||||
MultiEraBlock::Conway(b) => minicbor::to_vec(b).unwrap().len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use pallas_primitives::alonzo;
|
||||
use pallas_primitives::{alonzo, conway};
|
||||
|
||||
use crate::MultiEraCert;
|
||||
|
||||
|
|
@ -9,4 +9,11 @@ impl<'b> MultiEraCert<'b> {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_conway(&self) -> Option<&conway::Certificate> {
|
||||
match self {
|
||||
MultiEraCert::Conway(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ impl Era {
|
|||
Feature::CIP31 => self.ge(&Era::Babbage),
|
||||
Feature::CIP32 => self.ge(&Era::Babbage),
|
||||
Feature::CIP33 => self.ge(&Era::Babbage),
|
||||
Feature::CIP1694 => self.ge(&Era::Conway),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +32,7 @@ impl TryFrom<u16> for Era {
|
|||
4 => Ok(Era::Mary),
|
||||
5 => Ok(Era::Alonzo),
|
||||
6 => Ok(Era::Babbage),
|
||||
7 => Ok(Era::Conway),
|
||||
x => Err(crate::Error::UnknownEra(x)),
|
||||
}
|
||||
}
|
||||
|
|
@ -45,6 +47,7 @@ impl From<Era> for u16 {
|
|||
Era::Mary => 4,
|
||||
Era::Alonzo => 5,
|
||||
Era::Babbage => 6,
|
||||
Era::Conway => 7,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -58,6 +61,7 @@ impl Display for Era {
|
|||
Era::Mary => write!(f, "Mary"),
|
||||
Era::Alonzo => write!(f, "Alonzo"),
|
||||
Era::Babbage => write!(f, "Babbage"),
|
||||
Era::Conway => write!(f, "Conway"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{ComputeHash, OriginalHash};
|
||||
use pallas_codec::utils::KeepRaw;
|
||||
use pallas_crypto::hash::{Hash, Hasher};
|
||||
use pallas_primitives::{alonzo, babbage, byron};
|
||||
use pallas_primitives::{alonzo, babbage, byron, conway};
|
||||
|
||||
impl ComputeHash<32> for byron::EbbHead {
|
||||
fn compute_hash(&self) -> Hash<32> {
|
||||
|
|
@ -142,6 +142,32 @@ impl ComputeHash<32> for babbage::DatumOption {
|
|||
}
|
||||
}
|
||||
|
||||
// conway
|
||||
|
||||
impl ComputeHash<28> for conway::PlutusV3Script {
|
||||
fn compute_hash(&self) -> Hash<28> {
|
||||
Hasher::<224>::hash_tagged(&self.0, 3)
|
||||
}
|
||||
}
|
||||
|
||||
impl ComputeHash<32> for conway::TransactionBody {
|
||||
fn compute_hash(&self) -> Hash<32> {
|
||||
Hasher::<256>::hash_cbor(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl OriginalHash<32> for KeepRaw<'_, conway::TransactionBody> {
|
||||
fn original_hash(&self) -> pallas_crypto::hash::Hash<32> {
|
||||
Hasher::<256>::hash(self.raw_cbor())
|
||||
}
|
||||
}
|
||||
|
||||
impl OriginalHash<32> for KeepRaw<'_, conway::MintedTransactionBody<'_>> {
|
||||
fn original_hash(&self) -> pallas_crypto::hash::Hash<32> {
|
||||
Hasher::<256>::hash(self.raw_cbor())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{Era, MultiEraTx};
|
||||
|
|
@ -318,7 +344,7 @@ mod tests {
|
|||
fn tx_wits_plutus_v1_script_hashes_as_cli() {
|
||||
let tx_bytecode_hex = include_str!("../../test_data/scriptwit.tx");
|
||||
let bytecode = hex::decode(tx_bytecode_hex).unwrap();
|
||||
let tx = MultiEraTx::decode(Era::Babbage, &bytecode).unwrap();
|
||||
let tx = MultiEraTx::decode_for_era(Era::Babbage, &bytecode).unwrap();
|
||||
|
||||
let generated = tx
|
||||
.plutus_v1_scripts()
|
||||
|
|
@ -346,7 +372,7 @@ mod tests {
|
|||
|
||||
let tx_hex = include_str!("../../test_data/babbage1.tx");
|
||||
let tx_bytes = hex::decode(tx_hex).unwrap();
|
||||
let tx = MultiEraTx::decode(Era::Babbage, &tx_bytes).unwrap();
|
||||
let tx = MultiEraTx::decode_for_era(Era::Babbage, &tx_bytes).unwrap();
|
||||
let data = tx.plutus_data();
|
||||
|
||||
for (datum, expected_hash) in data.iter().zip(expected) {
|
||||
|
|
@ -360,7 +386,7 @@ mod tests {
|
|||
|
||||
let tx_hex = include_str!("../../test_data/babbage2.tx");
|
||||
let tx_bytes = hex::decode(tx_hex).unwrap();
|
||||
let tx = MultiEraTx::decode(Era::Babbage, &tx_bytes).unwrap();
|
||||
let tx = MultiEraTx::decode_for_era(Era::Babbage, &tx_bytes).unwrap();
|
||||
|
||||
for output in tx.outputs() {
|
||||
if let Some(MintedDatumOption::Data(datum)) = output.datum() {
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ mod tests {
|
|||
fn test_duplicate_consumed_inputs() {
|
||||
let tx_bytecode_hex = include_str!("../../test_data/duplicateinput.tx");
|
||||
let bytecode = hex::decode(tx_bytecode_hex).unwrap();
|
||||
let tx = MultiEraTx::decode(Era::Alonzo, &bytecode).unwrap();
|
||||
let tx = MultiEraTx::decode_for_era(Era::Alonzo, &bytecode).unwrap();
|
||||
|
||||
let expected_inputs = vec![
|
||||
"4d9cb6bf1c2e349f1bcd454a632d2b721d5badcf687220430c316588f39506ab#1",
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use thiserror::Error;
|
|||
|
||||
use pallas_codec::utils::{KeepRaw, KeyValuePairs};
|
||||
use pallas_crypto::hash::Hash;
|
||||
use pallas_primitives::{alonzo, babbage, byron};
|
||||
use pallas_primitives::{alonzo, babbage, byron, conway};
|
||||
|
||||
mod support;
|
||||
|
||||
|
|
@ -42,6 +42,7 @@ pub enum Era {
|
|||
Mary, // multi-assets
|
||||
Alonzo, // smart-contracts
|
||||
Babbage, // CIP-31/32/33
|
||||
Conway, // governance CIP-1694
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
|
|
@ -54,6 +55,7 @@ pub enum Feature {
|
|||
CIP31,
|
||||
CIP32,
|
||||
CIP33,
|
||||
CIP1694,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -71,6 +73,7 @@ pub enum MultiEraBlock<'b> {
|
|||
AlonzoCompatible(Box<alonzo::MintedBlock<'b>>, Era),
|
||||
Babbage(Box<babbage::MintedBlock<'b>>),
|
||||
Byron(Box<byron::MintedBlock<'b>>),
|
||||
Conway(Box<conway::MintedBlock<'b>>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -79,6 +82,7 @@ pub enum MultiEraTx<'b> {
|
|||
AlonzoCompatible(Box<Cow<'b, alonzo::MintedTx<'b>>>, Era),
|
||||
Babbage(Box<Cow<'b, babbage::MintedTx<'b>>>),
|
||||
Byron(Box<Cow<'b, byron::MintedTxPayload<'b>>>),
|
||||
Conway(Box<Cow<'b, conway::MintedTx<'b>>>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -101,6 +105,7 @@ pub enum MultiEraInput<'b> {
|
|||
pub enum MultiEraCert<'b> {
|
||||
NotApplicable,
|
||||
AlonzoCompatible(Box<Cow<'b, alonzo::Certificate>>),
|
||||
Conway(Box<Cow<'b, conway::Certificate>>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
|
|
@ -143,7 +148,6 @@ pub enum MultiEraWithdrawals<'b> {
|
|||
#[derive(Debug, Clone)]
|
||||
#[non_exhaustive]
|
||||
pub enum MultiEraUpdate<'b> {
|
||||
NotApplicable,
|
||||
AlonzoCompatible(Box<Cow<'b, alonzo::Update>>),
|
||||
Babbage(Box<Cow<'b, babbage::Update>>),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ impl<'b> MultiEraOutput<'b> {
|
|||
let tx = Box::new(Cow::Owned(tx));
|
||||
Ok(Self::AlonzoCompatible(tx))
|
||||
}
|
||||
Era::Babbage => {
|
||||
Era::Babbage | Era::Conway => {
|
||||
let tx = minicbor::decode(cbor)?;
|
||||
let tx = Box::new(Cow::Owned(tx));
|
||||
Ok(Self::Babbage(tx))
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ pub fn block_era(cbor: &[u8]) -> Outcome {
|
|||
4 => Outcome::Matched(Era::Mary),
|
||||
5 => Outcome::Matched(Era::Alonzo),
|
||||
6 => Outcome::Matched(Era::Babbage),
|
||||
7 => Outcome::Matched(Era::Conway),
|
||||
_ => Outcome::Inconclusive,
|
||||
},
|
||||
_ => Outcome::Inconclusive,
|
||||
|
|
@ -99,4 +100,24 @@ mod tests {
|
|||
|
||||
assert!(matches!(inference, Outcome::Matched(Era::Alonzo)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn babbage_block_detected() {
|
||||
let block_str = include_str!("../../test_data/babbage1.block");
|
||||
let bytes = hex::decode(block_str).unwrap();
|
||||
|
||||
let inference = block_era(bytes.as_slice());
|
||||
|
||||
assert!(matches!(inference, Outcome::Matched(Era::Babbage)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conway_block_detected() {
|
||||
let block_str = include_str!("../../test_data/conway1.artificial.block");
|
||||
let bytes = hex::decode(block_str).unwrap();
|
||||
|
||||
let inference = block_era(bytes.as_slice());
|
||||
|
||||
assert!(matches!(inference, Outcome::Matched(Era::Conway)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,10 @@ impl<'b> MultiEraTx<'b> {
|
|||
_ => 2,
|
||||
},
|
||||
MultiEraTx::Byron(_) => 0,
|
||||
MultiEraTx::Conway(x) => match &x.auxiliary_data {
|
||||
Nullable::Some(x) => x.raw_cbor().len(),
|
||||
_ => 2,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -22,6 +26,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
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(),
|
||||
MultiEraTx::Conway(x) => x.transaction_body.raw_cbor().len(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,6 +35,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
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(),
|
||||
MultiEraTx::Conway(x) => x.transaction_witness_set.raw_cbor().len(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -47,6 +53,7 @@ impl<'b> MultiEraBlock<'b> {
|
|||
MultiEraBlock::Babbage(x) => Some(x.header.header_body.block_body_size as usize),
|
||||
MultiEraBlock::EpochBoundary(_) => None,
|
||||
MultiEraBlock::Byron(_) => None,
|
||||
MultiEraBlock::Conway(x) => Some(x.header.header_body.block_body_size as usize),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! Internal supporting utilities
|
||||
|
||||
use pallas_primitives::{alonzo, babbage, byron};
|
||||
use pallas_primitives::{alonzo, babbage, byron, conway};
|
||||
|
||||
macro_rules! clone_tx_fn {
|
||||
($fn_name:ident, $era:tt) => {
|
||||
|
|
@ -40,6 +40,7 @@ macro_rules! clone_tx_fn {
|
|||
};
|
||||
}
|
||||
|
||||
clone_tx_fn!(conway_clone_tx_at, conway);
|
||||
clone_tx_fn!(babbage_clone_tx_at, babbage);
|
||||
clone_tx_fn!(alonzo_clone_tx_at, alonzo);
|
||||
|
||||
|
|
@ -57,6 +58,13 @@ pub fn clone_babbage_txs<'b>(block: &'b babbage::MintedBlock) -> Vec<babbage::Mi
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub fn clone_conway_txs<'b>(block: &'b conway::MintedBlock) -> Vec<conway::MintedTx<'b>> {
|
||||
(0..block.transaction_bodies.len())
|
||||
.step_by(1)
|
||||
.filter_map(|idx| conway_clone_tx_at(block, idx))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn clone_byron_txs<'b>(block: &'b byron::MintedBlock) -> Vec<byron::MintedTxPayload<'b>> {
|
||||
block.body.tx_payload.iter().cloned().collect()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,6 +131,9 @@ impl<'a> MultiEraBlock<'a> {
|
|||
MultiEraBlock::Babbage(x) => {
|
||||
genesis.absolute_slot_to_relative(x.header.header_body.slot)
|
||||
}
|
||||
MultiEraBlock::Conway(x) => {
|
||||
genesis.absolute_slot_to_relative(x.header.header_body.slot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@ use pallas_crypto::hash::Hash;
|
|||
use pallas_primitives::{
|
||||
alonzo,
|
||||
babbage::{self, NetworkId},
|
||||
byron,
|
||||
byron, conway,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Era, MultiEraCert, MultiEraInput, MultiEraMeta, MultiEraOutput, MultiEraPolicyAssets,
|
||||
MultiEraSigners, MultiEraTx, MultiEraUpdate, MultiEraWithdrawals, OriginalHash,
|
||||
MultiEraSigners, MultiEraTx, MultiEraUpdate, MultiEraWithdrawals, OriginalHash, Error
|
||||
};
|
||||
|
||||
impl<'b> MultiEraTx<'b> {
|
||||
|
|
@ -32,10 +32,11 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => minicbor::to_vec(x).unwrap(),
|
||||
MultiEraTx::Babbage(x) => minicbor::to_vec(x).unwrap(),
|
||||
MultiEraTx::Byron(x) => minicbor::to_vec(x).unwrap(),
|
||||
MultiEraTx::Conway(x) => minicbor::to_vec(x).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode(era: Era, cbor: &'b [u8]) -> Result<Self, minicbor::decode::Error> {
|
||||
pub fn decode_for_era(era: Era, cbor: &'b [u8]) -> Result<Self, minicbor::decode::Error> {
|
||||
match era {
|
||||
Era::Byron => {
|
||||
let tx = minicbor::decode(cbor)?;
|
||||
|
|
@ -52,6 +53,35 @@ impl<'b> MultiEraTx<'b> {
|
|||
let tx = Box::new(Cow::Owned(tx));
|
||||
Ok(MultiEraTx::Babbage(tx))
|
||||
}
|
||||
Era::Conway => {
|
||||
let tx = minicbor::decode(cbor)?;
|
||||
let tx = Box::new(Cow::Owned(tx));
|
||||
Ok(MultiEraTx::Conway(tx))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Try decode a transaction via every era's encoding format, starting with
|
||||
/// the most recent and returning on first success, or None if none are
|
||||
/// successful
|
||||
pub fn decode(cbor: &'b [u8]) -> Result<Self, Error> {
|
||||
if let Ok(tx) = minicbor::decode(cbor) {
|
||||
return Ok(MultiEraTx::Conway(Box::new(Cow::Owned(tx))))
|
||||
}
|
||||
|
||||
if let Ok(tx) = minicbor::decode(cbor) {
|
||||
return Ok(MultiEraTx::Babbage(Box::new(Cow::Owned(tx))))
|
||||
}
|
||||
|
||||
if let Ok(tx) = minicbor::decode(cbor) {
|
||||
// Shelley/Allegra/Mary/Alonzo will all decode to Alonzo
|
||||
return Ok(MultiEraTx::AlonzoCompatible(Box::new(Cow::Owned(tx)), Era::Alonzo))
|
||||
}
|
||||
|
||||
if let Ok(tx) = minicbor::decode(cbor) {
|
||||
Ok(MultiEraTx::Byron(Box::new(Cow::Owned(tx))))
|
||||
} else {
|
||||
Err(Error::unknown_cbor(cbor))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +90,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(_, era) => *era,
|
||||
MultiEraTx::Babbage(_) => Era::Babbage,
|
||||
MultiEraTx::Byron(_) => Era::Byron,
|
||||
MultiEraTx::Conway(_) => Era::Conway,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,6 +99,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.original_hash(),
|
||||
MultiEraTx::Babbage(x) => x.transaction_body.original_hash(),
|
||||
MultiEraTx::Byron(x) => x.transaction.original_hash(),
|
||||
MultiEraTx::Conway(x) => x.transaction_body.original_hash(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,6 +123,12 @@ impl<'b> MultiEraTx<'b> {
|
|||
.iter()
|
||||
.map(MultiEraOutput::from_byron)
|
||||
.collect(),
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.outputs
|
||||
.iter()
|
||||
.map(MultiEraOutput::from_babbage)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,13 +149,17 @@ impl<'b> MultiEraTx<'b> {
|
|||
.outputs
|
||||
.get(index)
|
||||
.map(MultiEraOutput::from_byron),
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.outputs
|
||||
.get(index)
|
||||
.map(MultiEraOutput::from_babbage),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the transaction inputs
|
||||
///
|
||||
/// NOTE: It is possible for this to return duplicates. See
|
||||
/// https://github.com/input-output-hk/cardano-ledger/commit/a342b74f5db3d3a75eae3e2abe358a169701b1e7
|
||||
/// NOTE: It is possible for this to return duplicates before some point in the chain history. See https://github.com/input-output-hk/cardano-ledger/commit/a342b74f5db3d3a75eae3e2abe358a169701b1e7
|
||||
pub fn inputs(&self) -> Vec<MultiEraInput> {
|
||||
match self {
|
||||
MultiEraTx::AlonzoCompatible(x, _) => x
|
||||
|
|
@ -138,6 +180,12 @@ impl<'b> MultiEraTx<'b> {
|
|||
.iter()
|
||||
.map(MultiEraInput::from_byron)
|
||||
.collect(),
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.inputs
|
||||
.iter()
|
||||
.map(MultiEraInput::from_alonzo_compatible)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -179,6 +227,13 @@ impl<'b> MultiEraTx<'b> {
|
|||
.map(|c| MultiEraCert::AlonzoCompatible(Box::new(Cow::Borrowed(c))))
|
||||
.collect(),
|
||||
MultiEraTx::Byron(_) => vec![],
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.certificates
|
||||
.iter()
|
||||
.flat_map(|c| c.iter())
|
||||
.map(|c| MultiEraCert::Conway(Box::new(Cow::Borrowed(c))))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -195,6 +250,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
.as_ref()
|
||||
.map(MultiEraUpdate::from_babbage),
|
||||
MultiEraTx::Byron(_) => None,
|
||||
MultiEraTx::Conway(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -215,6 +271,14 @@ impl<'b> MultiEraTx<'b> {
|
|||
.map(|(k, v)| MultiEraPolicyAssets::AlonzoCompatibleMint(k, v))
|
||||
.collect(),
|
||||
MultiEraTx::Byron(_) => vec![],
|
||||
// TODO: Is this still AlonzoCompatible? Zero vals not allowed or something
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.mint
|
||||
.iter()
|
||||
.flat_map(|x| x.iter())
|
||||
.map(|(k, v)| MultiEraPolicyAssets::AlonzoCompatibleMint(k, v))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -239,6 +303,13 @@ impl<'b> MultiEraTx<'b> {
|
|||
.map(MultiEraInput::from_alonzo_compatible)
|
||||
.collect(),
|
||||
MultiEraTx::Byron(_) => vec![],
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.collateral
|
||||
.iter()
|
||||
.flat_map(|x| x.iter())
|
||||
.map(MultiEraInput::from_alonzo_compatible)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -342,6 +413,11 @@ impl<'b> MultiEraTx<'b> {
|
|||
None => MultiEraWithdrawals::Empty,
|
||||
},
|
||||
MultiEraTx::Byron(_) => MultiEraWithdrawals::NotApplicable,
|
||||
// TODO: non empty still compatible?
|
||||
MultiEraTx::Conway(x) => match &x.transaction_body.withdrawals {
|
||||
Some(x) => MultiEraWithdrawals::AlonzoCompatible(x),
|
||||
None => MultiEraWithdrawals::Empty,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -350,6 +426,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => Some(x.transaction_body.fee),
|
||||
MultiEraTx::Babbage(x) => Some(x.transaction_body.fee),
|
||||
MultiEraTx::Byron(_) => None,
|
||||
MultiEraTx::Conway(x) => Some(x.transaction_body.fee),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -358,6 +435,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.ttl,
|
||||
MultiEraTx::Babbage(x) => x.transaction_body.ttl,
|
||||
MultiEraTx::Byron(_) => None,
|
||||
MultiEraTx::Conway(x) => x.transaction_body.ttl,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -372,6 +450,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.fee,
|
||||
MultiEraTx::Babbage(x) => x.transaction_body.fee,
|
||||
MultiEraTx::Byron(x) => crate::fees::compute_byron_fee(x, None),
|
||||
MultiEraTx::Conway(x) => x.transaction_body.fee,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -388,6 +467,11 @@ impl<'b> MultiEraTx<'b> {
|
|||
pallas_codec::utils::Nullable::Undefined => None,
|
||||
},
|
||||
MultiEraTx::Byron(_) => None,
|
||||
MultiEraTx::Conway(x) => match &x.auxiliary_data {
|
||||
pallas_codec::utils::Nullable::Some(x) => Some(x),
|
||||
pallas_codec::utils::Nullable::Null => None,
|
||||
pallas_codec::utils::Nullable::Undefined => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -423,6 +507,13 @@ impl<'b> MultiEraTx<'b> {
|
|||
.map(MultiEraSigners::AlonzoCompatible)
|
||||
.unwrap_or_default(),
|
||||
MultiEraTx::Byron(_) => MultiEraSigners::NotApplicable,
|
||||
// TODO: still compat?
|
||||
MultiEraTx::Conway(x) => x
|
||||
.transaction_body
|
||||
.required_signers
|
||||
.as_ref()
|
||||
.map(MultiEraSigners::AlonzoCompatible)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -431,6 +522,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.validity_interval_start,
|
||||
MultiEraTx::Babbage(x) => x.transaction_body.validity_interval_start,
|
||||
MultiEraTx::Byron(_) => None,
|
||||
MultiEraTx::Conway(x) => x.transaction_body.validity_interval_start,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -439,6 +531,7 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => x.transaction_body.network_id,
|
||||
MultiEraTx::Babbage(x) => x.transaction_body.network_id,
|
||||
MultiEraTx::Byron(_) => None,
|
||||
MultiEraTx::Conway(x) => x.transaction_body.network_id,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -447,30 +540,35 @@ impl<'b> MultiEraTx<'b> {
|
|||
MultiEraTx::AlonzoCompatible(x, _) => x.success,
|
||||
MultiEraTx::Babbage(x) => x.success,
|
||||
MultiEraTx::Byron(_) => true,
|
||||
MultiEraTx::Conway(x) => x.success,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_babbage(&self) -> Option<&babbage::MintedTx> {
|
||||
match self {
|
||||
MultiEraTx::AlonzoCompatible(_, _) => None,
|
||||
MultiEraTx::Babbage(x) => Some(x),
|
||||
MultiEraTx::Byron(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_alonzo(&self) -> Option<&alonzo::MintedTx> {
|
||||
match self {
|
||||
MultiEraTx::AlonzoCompatible(x, _) => Some(x),
|
||||
MultiEraTx::Babbage(_) => None,
|
||||
MultiEraTx::Byron(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_byron(&self) -> Option<&byron::MintedTxPayload> {
|
||||
match self {
|
||||
MultiEraTx::AlonzoCompatible(_, _) => None,
|
||||
MultiEraTx::Babbage(_) => None,
|
||||
MultiEraTx::Byron(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_conway(&self) -> Option<&conway::MintedTx> {
|
||||
match self {
|
||||
MultiEraTx::Conway(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue