Skip to content

Commit

Permalink
feat: Introduce 'traverse' library (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Jun 12, 2022
1 parent fe80ff7 commit 26da913
Show file tree
Hide file tree
Showing 20 changed files with 377 additions and 106 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"pallas-miniprotocols",
"pallas-crypto",
"pallas-primitives",
"pallas-traverse",
"pallas",
"examples/block-download",
"examples/block-decode",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ As already explained, _Pallas_ aims at being an expanding set of components. The
| Crates | Description |
| --------------------------------------- | ----------------------------------------------------------------------- |
| [pallas-primitives](/pallas-primitives) | Ledger primitives and cbor codec for the different Cardano eras |
| [pallas-traverse](/pallas-traverse) | Utilities to traverse over multi-era block data |
| pallas-ticking | Time passage implementation for consensus algorithm |
| pallas-applying | Logic for validating and applying new blocks and txs to the chain state |
| pallas-forecasting | Ledger forecasting algorithm to be used by the consensus layer |
Expand Down
27 changes: 9 additions & 18 deletions examples/block-decode/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pallas::ledger::primitives::{alonzo, byron, probing, Era};
use pallas::ledger::traverse::MultiEraBlock;

fn main() {
let blocks = vec![
Expand All @@ -10,23 +10,14 @@ fn main() {
];

for block_str in blocks.iter() {
let bytes = hex::decode(block_str).expect("invalid hex");
let cbor = hex::decode(block_str).expect("invalid hex");

match probing::probe_block_cbor_era(&bytes) {
probing::Outcome::Matched(era) => match era {
Era::Byron => {
let (_, block): (u16, byron::MainBlock) =
pallas::codec::minicbor::decode(&bytes).expect("invalid cbor");
println!("{:?}", block)
}
// we use alonzo for everything post-shelly since it's backward compatible
Era::Shelley | Era::Allegra | Era::Mary | Era::Alonzo => {
let (_, block): (u16, alonzo::Block) =
pallas::codec::minicbor::decode(&bytes).expect("invalid cbor");
println!("{:?}", block)
}
},
_ => println!("couldn't infer block era"),
};
let block = MultiEraBlock::decode(&cbor).expect("invalid cbor");

println!("{} {}", block.slot(), block.hash());

for tx in block.tx_iter() {
println!("{:?}", tx);
}
}
}
10 changes: 5 additions & 5 deletions pallas-codec/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ where
}

/// A struct that maintains a reference to whether a cbor array was indef or not
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum MaybeIndefArray<A> {
Def(Vec<A>),
Indef(Vec<A>),
Expand Down Expand Up @@ -186,7 +186,7 @@ where
/// transform key-value structures into an orderer vec of `properties`, where
/// each entry represents a a cbor-encodable variant of an attribute of the
/// struct.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub struct OrderPreservingProperties<P>(Vec<P>);

impl<P> Deref for OrderPreservingProperties<P> {
Expand Down Expand Up @@ -229,7 +229,7 @@ where
}

/// Wraps a struct so that it is encoded/decoded as a cbor bytes
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CborWrap<T>(pub T);

impl<'b, C, T> minicbor::Decode<'b, C> for CborWrap<T>
Expand Down Expand Up @@ -312,7 +312,7 @@ where
/// An empty map
///
/// don't ask me why, that's what the CDDL asks for.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct EmptyMap;

impl<'b, C> minicbor::decode::Decode<'b, C> for EmptyMap {
Expand Down Expand Up @@ -518,7 +518,7 @@ impl From<&AnyUInt> for u64 {
/// let confirm: (u16, u16) = minicbor::decode(keeper.raw_cbor()).unwrap();
/// assert_eq!(confirm, (456u16, 789u16));
/// ```
#[derive(Debug, PartialEq, PartialOrd)]
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct KeepRaw<'b, T> {
raw: &'b [u8],
inner: T,
Expand Down
Loading

0 comments on commit 26da913

Please sign in to comment.