feat: Implement common traverse iterators (#119)
This commit is contained in:
parent
2a43dd1061
commit
e67da5e7ef
12 changed files with 378 additions and 316 deletions
|
|
@ -17,7 +17,7 @@ impl TransactionOutput {
|
|||
mod tests {
|
||||
use pallas_codec::minicbor;
|
||||
|
||||
use crate::alonzo::{Block, TransactionBodyComponent};
|
||||
use crate::alonzo::Block;
|
||||
|
||||
type BlockWrapper = (u16, Block);
|
||||
|
||||
|
|
@ -49,18 +49,14 @@ mod tests {
|
|||
assert!(block.transaction_bodies.len() > 0);
|
||||
|
||||
for tx in block.transaction_bodies.iter() {
|
||||
for component in tx.iter() {
|
||||
if let TransactionBodyComponent::Outputs(outputs) = component {
|
||||
for output in outputs.iter() {
|
||||
let addr_str = output.to_bech32_address("addr_test").unwrap();
|
||||
for output in tx.outputs.iter() {
|
||||
let addr_str = output.to_bech32_address("addr_test").unwrap();
|
||||
|
||||
assert!(
|
||||
KNOWN_ADDRESSES.contains(&addr_str.as_str()),
|
||||
"address {} not in known list",
|
||||
addr_str
|
||||
);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
KNOWN_ADDRESSES.contains(&addr_str.as_str()),
|
||||
"address {} not in known list",
|
||||
addr_str
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
use pallas_codec::minicbor::{bytes::ByteVec, data::Int, data::Tag, Decode, Encode};
|
||||
use pallas_crypto::hash::Hash;
|
||||
use std::ops::Deref;
|
||||
|
||||
use pallas_codec::utils::{AnyUInt, KeepRaw, KeyValuePairs, MaybeIndefArray};
|
||||
|
||||
|
|
@ -699,155 +698,52 @@ pub struct Update {
|
|||
pub epoch: Epoch,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum TransactionBodyComponent {
|
||||
Inputs(MaybeIndefArray<TransactionInput>),
|
||||
Outputs(MaybeIndefArray<TransactionOutput>),
|
||||
Fee(u64),
|
||||
Ttl(u64),
|
||||
Certificates(MaybeIndefArray<Certificate>),
|
||||
Withdrawals(KeyValuePairs<RewardAccount, Coin>),
|
||||
Update(Update),
|
||||
AuxiliaryDataHash(ByteVec),
|
||||
ValidityIntervalStart(u64),
|
||||
Mint(Multiasset<i64>),
|
||||
ScriptDataHash(Hash<32>),
|
||||
Collateral(MaybeIndefArray<TransactionInput>),
|
||||
RequiredSigners(MaybeIndefArray<AddrKeyhash>),
|
||||
NetworkId(NetworkId),
|
||||
}
|
||||
|
||||
impl<'b, C> minicbor::decode::Decode<'b, C> for TransactionBodyComponent {
|
||||
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
|
||||
let key: u32 = d.decode_with(ctx)?;
|
||||
|
||||
match key {
|
||||
0 => Ok(Self::Inputs(d.decode_with(ctx)?)),
|
||||
1 => Ok(Self::Outputs(d.decode_with(ctx)?)),
|
||||
2 => Ok(Self::Fee(d.decode_with(ctx)?)),
|
||||
3 => Ok(Self::Ttl(d.decode_with(ctx)?)),
|
||||
4 => Ok(Self::Certificates(d.decode_with(ctx)?)),
|
||||
5 => Ok(Self::Withdrawals(d.decode_with(ctx)?)),
|
||||
6 => Ok(Self::Update(d.decode_with(ctx)?)),
|
||||
7 => Ok(Self::AuxiliaryDataHash(d.decode_with(ctx)?)),
|
||||
8 => Ok(Self::ValidityIntervalStart(d.decode_with(ctx)?)),
|
||||
9 => Ok(Self::Mint(d.decode_with(ctx)?)),
|
||||
11 => Ok(Self::ScriptDataHash(d.decode_with(ctx)?)),
|
||||
13 => Ok(Self::Collateral(d.decode_with(ctx)?)),
|
||||
14 => Ok(Self::RequiredSigners(d.decode_with(ctx)?)),
|
||||
15 => Ok(Self::NetworkId(d.decode_with(ctx)?)),
|
||||
_ => Err(minicbor::decode::Error::message(
|
||||
"invalid map key for transaction body component",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> minicbor::encode::Encode<C> for TransactionBodyComponent {
|
||||
fn encode<W: minicbor::encode::Write>(
|
||||
&self,
|
||||
e: &mut minicbor::Encoder<W>,
|
||||
ctx: &mut C,
|
||||
) -> Result<(), minicbor::encode::Error<W::Error>> {
|
||||
match self {
|
||||
TransactionBodyComponent::Inputs(x) => {
|
||||
e.encode_with(0, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Outputs(x) => {
|
||||
e.encode_with(1, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Fee(x) => {
|
||||
e.encode_with(2, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Ttl(x) => {
|
||||
e.encode_with(3, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Certificates(x) => {
|
||||
e.encode_with(4, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Withdrawals(x) => {
|
||||
e.encode_with(5, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Update(x) => {
|
||||
e.encode_with(6, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::AuxiliaryDataHash(x) => {
|
||||
e.encode_with(7, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::ValidityIntervalStart(x) => {
|
||||
e.encode_with(8, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Mint(x) => {
|
||||
e.encode_with(9, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::ScriptDataHash(x) => {
|
||||
e.encode_with(11, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::Collateral(x) => {
|
||||
e.encode_with(13, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::RequiredSigners(x) => {
|
||||
e.encode_with(14, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
TransactionBodyComponent::NetworkId(x) => {
|
||||
e.encode_with(15, ctx)?;
|
||||
e.encode_with(x, ctx)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Can't derive encode for TransactionBody because it seems to require a very
|
||||
// particular order for each key in the map
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct TransactionBody(Vec<TransactionBodyComponent>);
|
||||
#[derive(Encode, Decode, Debug, PartialEq, Clone)]
|
||||
#[cbor(map)]
|
||||
pub struct TransactionBody {
|
||||
#[n(0)]
|
||||
pub inputs: MaybeIndefArray<TransactionInput>,
|
||||
|
||||
impl Deref for TransactionBody {
|
||||
type Target = Vec<TransactionBodyComponent>;
|
||||
#[n(1)]
|
||||
pub outputs: MaybeIndefArray<TransactionOutput>,
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
#[n(2)]
|
||||
pub fee: u64,
|
||||
|
||||
impl<'b, C> minicbor::decode::Decode<'b, C> for TransactionBody {
|
||||
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
|
||||
let len = d.map()?.unwrap_or_default();
|
||||
#[n(3)]
|
||||
pub ttl: Option<u64>,
|
||||
|
||||
let components: Result<_, _> = (0..len).map(|_| d.decode_with(ctx)).collect();
|
||||
#[n(4)]
|
||||
pub certificates: Option<MaybeIndefArray<Certificate>>,
|
||||
|
||||
Ok(Self(components?))
|
||||
}
|
||||
}
|
||||
#[n(5)]
|
||||
pub withdrawals: Option<KeyValuePairs<RewardAccount, Coin>>,
|
||||
|
||||
impl<C> minicbor::encode::Encode<C> for TransactionBody {
|
||||
fn encode<W: minicbor::encode::Write>(
|
||||
&self,
|
||||
e: &mut minicbor::Encoder<W>,
|
||||
ctx: &mut C,
|
||||
) -> Result<(), minicbor::encode::Error<W::Error>> {
|
||||
e.map(self.0.len() as u64)?;
|
||||
for component in &self.0 {
|
||||
e.encode_with(component, ctx)?;
|
||||
}
|
||||
#[n(6)]
|
||||
pub update: Option<Update>,
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[n(7)]
|
||||
pub auxiliary_data_hash: Option<ByteVec>,
|
||||
|
||||
#[n(8)]
|
||||
pub validity_interval_start: Option<u64>,
|
||||
|
||||
#[n(9)]
|
||||
pub mint: Option<Multiasset<i64>>,
|
||||
|
||||
#[n(11)]
|
||||
pub script_data_hash: Option<Hash<32>>,
|
||||
|
||||
#[n(13)]
|
||||
pub collateral: Option<MaybeIndefArray<TransactionInput>>,
|
||||
|
||||
#[n(14)]
|
||||
pub required_signers: Option<MaybeIndefArray<AddrKeyhash>>,
|
||||
|
||||
#[n(15)]
|
||||
pub network_id: Option<NetworkId>,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug, PartialEq, Clone)]
|
||||
|
|
@ -1440,7 +1336,7 @@ pub struct Block {
|
|||
/// This structure is analogous to [Block], but it allows to retrieve the
|
||||
/// original CBOR bytes for each structure that might require hashing. In this
|
||||
/// way, we make sure that the resulting hash matches what exists on-chain.
|
||||
#[derive(Encode, Decode, Debug, PartialEq)]
|
||||
#[derive(Encode, Decode, Debug, PartialEq, Clone)]
|
||||
pub struct MintedBlock<'b> {
|
||||
#[n(0)]
|
||||
pub header: KeepRaw<'b, Header>,
|
||||
|
|
@ -1473,7 +1369,7 @@ pub struct Tx {
|
|||
pub auxiliary_data: Option<AuxiliaryData>,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct MintedTx<'b> {
|
||||
#[b(0)]
|
||||
pub transaction_body: KeepRaw<'b, TransactionBody>,
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ pub type StakeholderId = Blake2b224;
|
|||
|
||||
pub type EpochId = u64;
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct SlotId {
|
||||
#[n(0)]
|
||||
pub epoch: EpochId,
|
||||
|
|
@ -407,7 +407,7 @@ pub type SscCert = (VssPubKey, EpochId, PubKey, Signature);
|
|||
// ssccerts = #6.258([* ssccert])
|
||||
pub type SscCerts = TagWrap<MaybeIndefArray<SscCert>, 258>;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Ssc {
|
||||
Variant0(SscComms, SscCerts),
|
||||
Variant1(SscOpens, SscCerts),
|
||||
|
|
@ -473,7 +473,7 @@ impl<C> minicbor::Encode<C> for Ssc {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SscProof {
|
||||
Variant0(ByronHash, ByronHash),
|
||||
Variant1(ByronHash, ByronHash),
|
||||
|
|
@ -543,7 +543,7 @@ impl<C> minicbor::Encode<C> for SscProof {
|
|||
|
||||
// Delegation
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[derive(Debug, Encode, Decode, Clone)]
|
||||
pub struct Dlg {
|
||||
#[n(0)]
|
||||
pub epoch: EpochId,
|
||||
|
|
@ -560,7 +560,7 @@ pub struct Dlg {
|
|||
|
||||
pub type DlgSig = (Dlg, Signature);
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[derive(Debug, Encode, Decode, Clone)]
|
||||
pub struct Lwdlg {
|
||||
#[n(0)]
|
||||
pub epoch_range: (EpochId, EpochId),
|
||||
|
|
@ -581,7 +581,7 @@ pub type LwdlgSig = (Lwdlg, Signature);
|
|||
|
||||
pub type BVer = (u16, u16, u8);
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TxFeePol {
|
||||
//[0, #6.24(bytes .cbor ([bigint, bigint]))]
|
||||
Variant0(CborWrap<(i64, i64)>),
|
||||
|
|
@ -628,7 +628,7 @@ impl<C> minicbor::Encode<C> for TxFeePol {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[derive(Debug, Encode, Decode, Clone)]
|
||||
pub struct BVerMod {
|
||||
#[n(0)]
|
||||
pub script_version: ZeroOrOneArray<u16>,
|
||||
|
|
@ -675,7 +675,7 @@ pub struct BVerMod {
|
|||
|
||||
pub type UpData = (ByronHash, ByronHash, ByronHash, ByronHash);
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[derive(Debug, Encode, Decode, Clone)]
|
||||
pub struct UpProp {
|
||||
#[n(0)]
|
||||
pub block_version: Option<BVer>,
|
||||
|
|
@ -701,7 +701,7 @@ pub struct UpProp {
|
|||
pub signature: Option<Signature>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[derive(Debug, Encode, Decode, Clone)]
|
||||
pub struct UpVote {
|
||||
#[n(0)]
|
||||
pub voter: PubKey,
|
||||
|
|
@ -716,7 +716,7 @@ pub struct UpVote {
|
|||
pub signature: Signature,
|
||||
}
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
#[derive(Debug, Encode, Decode, Clone)]
|
||||
pub struct Up {
|
||||
#[n(0)]
|
||||
pub proposal: ZeroOrOneArray<UpProp>,
|
||||
|
|
@ -729,7 +729,7 @@ pub struct Up {
|
|||
|
||||
pub type Difficulty = MaybeIndefArray<u64>;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BlockSig {
|
||||
Signature(Signature),
|
||||
LwdlgSig(LwdlgSig),
|
||||
|
|
@ -785,7 +785,7 @@ impl<C> minicbor::Encode<C> for BlockSig {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct BlockCons(
|
||||
#[n(0)] pub SlotId,
|
||||
#[n(1)] pub PubKey,
|
||||
|
|
@ -793,7 +793,7 @@ pub struct BlockCons(
|
|||
#[n(3)] pub BlockSig,
|
||||
);
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct BlockHeadEx {
|
||||
#[n(0)]
|
||||
pub block_version: BVer,
|
||||
|
|
@ -808,7 +808,7 @@ pub struct BlockHeadEx {
|
|||
pub extra_proof: ByronHash,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct BlockProof {
|
||||
#[n(0)]
|
||||
pub tx_proof: TxProof,
|
||||
|
|
@ -823,7 +823,7 @@ pub struct BlockProof {
|
|||
pub upd_proof: ByronHash,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct BlockHead {
|
||||
#[n(0)]
|
||||
pub protocol_magic: u32,
|
||||
|
|
@ -874,7 +874,7 @@ pub struct BlockBody {
|
|||
pub upd_payload: Up,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct MintedBlockBody<'b> {
|
||||
#[b(0)]
|
||||
pub tx_payload: MaybeIndefArray<MintedTxPayload<'b>>,
|
||||
|
|
@ -891,7 +891,7 @@ pub struct MintedBlockBody<'b> {
|
|||
|
||||
// Epoch Boundary Blocks
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct EbbCons {
|
||||
#[n(0)]
|
||||
pub epoch_id: EpochId,
|
||||
|
|
@ -900,7 +900,7 @@ pub struct EbbCons {
|
|||
pub difficulty: Difficulty,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct EbbHead {
|
||||
#[n(0)]
|
||||
pub protocol_magic: u32,
|
||||
|
|
@ -930,7 +930,7 @@ pub struct Block {
|
|||
pub extra: MaybeIndefArray<Attributes>,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct MintedBlock<'b> {
|
||||
#[b(0)]
|
||||
pub header: KeepRaw<'b, BlockHead>,
|
||||
|
|
@ -942,7 +942,7 @@ pub struct MintedBlock<'b> {
|
|||
pub extra: MaybeIndefArray<Attributes>,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
#[derive(Encode, Decode, Debug, Clone)]
|
||||
pub struct EbBlock {
|
||||
#[n(0)]
|
||||
pub header: EbbHead,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue