Pallas

repository·main·Indexed 42 days ago

https://github.com/txpipe/pallas

A collection of Rust-native building blocks for the Cardano blockchain ecosystem. Pallas re-implements Ouroboros and Cardano logic, providing a foundational layer for developing blockchain applications such as nodes, wallets, and explorers. It includes crates for address parsing (pallas-addresses), CBOR and Plutus Core encoding (pallas-codec), genesis file parsing (pallas-configs), and support for the Leios overlay (CIP-0164).

Tokens
120.7K
Snippets
357
Records
518
Agent score
51%

What's inside Pallas

  1. Overview of pallas-crypto primitives

    main

    The pallas-crypto crate provides the following cryptographic primitives required for the Cardano protocol:

    • hash: Provides Hash<N> and Hasher<N> implementations using Blake2b. The generic N is the digest size in bits.
    • key::ed25519: Supports regular and extended Ed25519 key pairs, including signing and verification.
    • kes: Implements Key Evolving Signature (KES) primitives used by block producers.
    • nonce: Provides helpers for epoch and chain-nonce evolution.
    • memsec: Provides secure-memory utilities to ensure key material is wiped from memory.
  2. Overview of Pallas Network architecture

    main

    Pallas Network is an async, tokio-backed implementation of the Ouroboros networking stack. It uses a multiplexer for segment-level transport and manages state machines for various Cardano mini-protocols.

    Key components include:

    Facades

    Opinionated client/server bundles organized by role. Each facade owns a multiplexer and provides access to relevant mini-protocols:

    • N2N (Node-to-Node): PeerClient / PeerServer
    • N2C (Node-to-Client): NodeClient / NodeServer
    • DMQ (Data Management Queue): DmqClient / DmqServer

    Multiplexer

    Handles segment-level transport via components like RunningPlexer, Bearer, VersionTable, and VersionNumber.

    Mini-protocols

    Includes implementations for all Ouroboros protocols:

    • handshake
    • chainsync
    • blockfetch
    • txsubmission
    • localstate
    • localtxsubmission
    • txmonitor
    • keepalive
    • peersharing
    • localmsgnotification
    • localmsgsubmission
  3. Validate Cardano transactions with Pallas Validate

    main

    Pallas Validate provides Phase-1 (structural/rule-based) and optionally Phase-2 (Plutus script execution) validation of Cardano transactions against live ledger rules. It is designed for clients that need to locally reject ill-formed or unenforceable transactions before submission, or for replaying historical chains to confirm protocol conformance.

    Validation Phases

    • Phase-1: Structural and rule-based validation. It is available by default and is organized into modules per era: byron, shelley_ma, alonzo, babbage, and conway.
    • Phase-2: Plutus script execution validation. This phase is gated behind the phase2 cargo feature.

    Core Components

    The validators use shared input types found in the utils module:

    • Environment: Contains protocol parameters.
    • UTxOs: The set of Unspent Transaction Outputs.
    • CertState: Certificate state.
    • MultiEraProtocolParameters: Era-specific parameters.
    • ValidationError / ValidationResult: Unified types for reporting validation outcomes.
  4. Available modules and data structures in pallas-configs

    main

    The pallas-configs crate provides era-specific modules and shared cost model views:

    • Era Modules: byron, shelley, alonzo, and conway. Each module exposes a GenesisFile struct (or equivalent) and a from_file function for loading configuration files.
    • Cost Models: The cost_models module provides typed views over Plutus cost-model tables, which are shared across different eras.
  5. Use pallas-hardano storage and display modules

    main

    The pallas-hardano crate provides interoperability with Haskell Cardano node artifacts through two main modules:

    storage::immutable

    Provides readers over the node's chunk, primary, and secondary index files. Key entry points include:

    • read_blocks: Iterates through blocks in the immutable directory.
    • read_blocks_from_point: Reads blocks starting from a specific point.
    • get_tip: Retrieves the current tip of the chain.

    display

    Provides pretty-printing helpers for the structures returned by the storage modules.

  6. Use pallas-codec for CBOR and Plutus Core encoding

    main

    The pallas-codec crate provides the encoding foundation for the Pallas workspace. It primarily consists of a re-exported version of minicbor for CBOR operations and a Rust port of the Plutus Core flat format.

    While most users will interact with these encoding capabilities transitively through higher-level crates like pallas-primitives, pallas-traverse, or pallas-txbuilder, you can use pallas-codec directly for custom CBOR serialization or Plutus Core script handling.

    use pallas_codec::minicbor;
    
    #[derive(minicbor::Encode, minicbor::Decode)]
    struct Pair(#[n(0)] u64, #[n(1)] String);
    
    let bytes = minicbor::to_vec(Pair(1, "hi".into()))?;
    let back: Pair = minicbor::decode(&bytes)?;
  7. What is the TxSubmission miniprotocol?

    main

    The TxSubmission miniprotocol allows a client holding transactions to announce them to a server interested in those transactions. This is primarily used for propagating transactions across node mempools in a peer-to-peer fashion until they reach a node ready to mint a block.

    Unlike traditional download protocols, TxSubmission is designed for two-way congestion control:

    1. A node with transactions is not forced to initiate connections to everyone.
    2. A node interested in transactions can selectively choose what to download.

    The protocol operates as a state machine where the client initiates the connection and moves the protocol from an Init state to an Idle state. Once in Idle, the server can request transaction IDs or full transaction bodies.

  8. Understand Alonzo Phase-1 Transaction and Block Definitions

    main

    The Alonzo phase-1 validation rules use a specific set of mathematical definitions and notations to describe the state and structure of blocks and transactions.

    Block Definitions

    • Block: The set of all possible Alonzo blocks. The term block refers to the specific block currently being validated.
    • txs(block): The set of transactions contained within a block.
    • blockExUnits(block): The total memory and execution step units for a block, calculated as the sum of txExUnits(txWits(tx)) for all transactions in the block.

    Transaction (Tx) Definitions

    A transaction is composed of a transaction body (TxBody) and a witness set (txWits).

    Transaction Body (TxBody)

    • txIns(txBody): The set of non-collateral inputs.
    • collateral(txBody): The set of collateral inputs.
    • txOuts(txBody): The list of transaction outputs (TxOut).
    • txValidityInterval(txBody): A tuple (Slot, Slot) representing the lower and upper bounds of validity.
    • requiredSigners(txBody): The set of KeyHash values required for Plutus script execution.
    • fee(txBody): The transaction fee in lovelace.
    • minted(txBody): The multi-asset value minted or burned.
    • txSize(txBody): The serialized size of the transaction in bytes.
    • txAuxDatHash(txBody): The hash of the auxiliary data.

    Transaction Outputs (TxOut)

    A TxOut is defined as Addr x Value x DatumHash.

    • getValue(txOut): Returns the Value contained in the output.
    • isADAOnly(Value): Returns true if the value contains only ADA assets.
    • balance(P(TxOut)): Returns the sum of all assets in a set of outputs.
    • outputEntrySize(txOut): The serialized size of the output in bytes.
  9. Compare Sum6Kes and SumCompact6Kes for Cardano compatibility

    main

    For compatibility with the Cardano blockchain, this library provides two implementations of Sum6Kes:

    1. Sum6Kes: The standard implementation used by Cardano.
    2. SumCompact6Kes: An optimized implementation that provides an asymptotic halving of the signature size compared to the standard version.

    Interoperability is maintained via test vectors generated using Cardano's code, located in the ./data directory of the repository.

  10. Understand the `Address` data model

    main

    The pallas_addresses crate organizes address data into a hierarchy of types:

    • Address (Enum): The top-level container that dispatches to one of three variants: Byron, Shelley, or Stake.
    • Specific Address Types:
      • ByronAddress
      • ShelleyAddress
      • StakeAddress
    • Structural Components (Shelley/Stake):
      • ShelleyPaymentPart and ShelleyDelegationPart: The constituent parts of a Shelley address.
      • StakePayload and Pointer: Components used in stake address construction.
    • Network: An enum representing the network context (Mainnet, Testnet, or Other(u8)) extracted from the address header.
    • Submodules:
      • byron: Contains Byron-specific structures and CBOR helpers.
      • varuint: Provides the variable-length integer codec used by stake pointers.
  11. Identify eras and features using Era and Feature

    main

    Pallas Traverse provides two main discriminators to understand the context of the data you are traversing:

    1. Era: Used to determine exactly which era a piece of data belongs to.
    2. Feature: Used to check if a specific era supports certain capabilities, such as multi-assets, smart contracts, or CIP-1694.
  12. How StagingTransaction and BuiltTransaction work together

    main

    The transaction construction lifecycle in Pallas TxBuilder follows a two-stage pattern:

    1. StagingTransaction: This is the mutable, in-progress state of your transaction. It serves as the primary entry point for configuration. You use it to collect inputs, outputs, minting requirements, scripts, datums, redeemers, fees, network IDs, and validity intervals.
    2. BuiltTransaction: Once configuration is complete, you call a finalization method (like build_conway_raw()) which consumes or transforms the staging state into a BuiltTransaction. This object represents the finalized, encoded transaction body. It provides the .sign(&signer) method to apply signatures and exposes the raw CBOR bytes via the .tx_bytes field.