pallas/pallas-traverse/src/probe.rs
Matthias Benkort 969d5612b7
refactor: Re-organize and clean-up pallas-primitives (#523)
* Re-organize and clean-up pallas-primitives

  Namely:

  - Move _common_ (i.e. era-independent) types and structures up to the
    `lib` module; to be shared across all eras. If any of those deviate
    in a subsequent era, it is easy to bring them down and define new
    types from the point of divergence onward. This simplifies the scope
    of each era-specific module and make them slightly easier to
    navigate.

    Note that, each era module still re-export all of the common types
    that's relevant to that particular era. So technically, this
    reorganization doesn't really change anything for callers/users of
    the library.

  - Rename `Scripthash` to `ScriptHash`. Before this commit, both
    actually existed as `ScriptHash` was introduced with the Conway era.
    Yet, they refer to the same thing, so the duplication is simply
    confusing.

  - Rename `One` / `Two` constructors for `NetworkId` to `Testnet` and
    `Mainnet` respectively. Also defined idiomatic `From` & `TryFrom`
    implementation for conversion to and from `u8`. This is a lot let
    confusing!

  - Generalize `PlutusScript` with a constant generic, to avoid
    repetition for each plutus script generated for specific version.
    Note that a distinction is still _necessary_ if we want to provie
    out-of-the-box serialisers for Plutus scripts, which are serialised
    with a tag prefix depending on the language. All else apart, they
    are strictly similar types.

  - Rename `CostMdls` to `CostModels`. Because, common.

  - Rename `plutus_script` to `plutus_v1_script` in the Alonzo's witness
    set, for consistency with other eras.

* Fix ordering of ScriptHash variants.

  This is an odd one. See the note.

* Bump minicbor to v0.25.1

* Add aliases with deprecation warnings to various fields and types.

* revert renaming plutus_script to plutus_v1_script in Alonzo witness

  See https://github.com/txpipe/pallas/pull/523#discussion_r1807329742
2024-10-22 08:57:21 -03:00

123 lines
3.5 KiB
Rust

//! Lightweight inspection of block data without full CBOR decoding
use pallas_codec::minicbor::{data::Token, decode::Tokenizer};
use crate::Era;
#[derive(Debug)]
pub enum Outcome {
Matched(Era),
EpochBoundary,
Inconclusive,
}
// Executes a very lightweight inspection of the initial tokens of the CBOR
// block payload to extract the tag of the block wrapper which defines the era
// of the contained bytes.
pub fn block_era(cbor: &[u8]) -> Outcome {
let mut tokenizer = Tokenizer::new(cbor);
if !matches!(tokenizer.next(), Some(Ok(Token::Array(2)))) {
return Outcome::Inconclusive;
}
match tokenizer.next() {
Some(Ok(Token::U8(variant))) => match variant {
0 => Outcome::EpochBoundary,
1 => Outcome::Matched(Era::Byron),
2 => Outcome::Matched(Era::Shelley),
3 => Outcome::Matched(Era::Allegra),
4 => Outcome::Matched(Era::Mary),
5 => Outcome::Matched(Era::Alonzo),
6 => Outcome::Matched(Era::Babbage),
7 => Outcome::Matched(Era::Conway),
_ => Outcome::Inconclusive,
},
_ => Outcome::Inconclusive,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn genesis_block_detected() {
let block_str = include_str!("../../test_data/genesis.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
assert!(matches!(inference, Outcome::EpochBoundary));
}
#[test]
fn byron_block_detected() {
let block_str = include_str!("../../test_data/byron1.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
assert!(matches!(inference, Outcome::Matched(Era::Byron)));
}
#[test]
fn shelley_block_detected() {
let block_str = include_str!("../../test_data/shelley1.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
assert!(matches!(inference, Outcome::Matched(Era::Shelley)));
}
#[test]
fn allegra_block_detected() {
let block_str = include_str!("../../test_data/allegra1.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
assert!(matches!(inference, Outcome::Matched(Era::Allegra)));
}
#[test]
fn mary_block_detected() {
let block_str = include_str!("../../test_data/mary1.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
assert!(matches!(inference, Outcome::Matched(Era::Mary)));
}
#[test]
fn alonzo_block_detected() {
let block_str = include_str!("../../test_data/alonzo1.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
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.block");
let bytes = hex::decode(block_str).unwrap();
let inference = block_era(bytes.as_slice());
assert!(matches!(inference, Outcome::Matched(Era::Conway)));
}
}