Celestia Node

repository·main·Indexed 21 days ago

https://github.com/celestiaorg/celestia-node

A Golang implementation of Celestia's Data Availability (DA) node types, specifically Bridge and Light nodes. It enables Data Availability Sampling (DAS) by bridging the consensus network to the DA network. The repository includes a Go client library for interacting with the network in Read-Only or Full Client modes, allowing users to query headers, retrieve blobs, and submit data to the network.

Tokens
40.1K
Snippets
132
Records
176
Agent score
76%

What's inside celestia-node

  1. What is the SHREX protocol?

    main

    SHREX (Share Exchange) is the data availability protocol for the Celestia network. It is designed to enable efficient peer-to-peer data exchange, specifically allowing light nodes to perform data availability sampling without downloading entire blocks.

    SHREX is not a single mechanism but a suite of four coordinated components:

    1. Client/Server Protocol: A request-response mechanism used to retrieve specific data from peers.
    2. ShrEx/Sub Protocol: A push-based notification system that announces the availability of new blocks.
    3. Peer Discovery: A DHT-based mechanism used to find peers with specific capabilities.
    4. Peer Manager: A central coordination component that manages peer pools, selects optimal peers for requests, and validates notifications.
  2. Overview of Celestia Node services

    main

    The Celestia Node is built around a central Node data structure that manages various modular services. Services are processes for data retrieval, sharing, or storage that can be started and stopped on the core node.

    Services available to Light Nodes

    • ExtendedHeaderService: Handles retrieving, broadcasting (ExtendedHeaderSub), and storing ExtendedHeaders via ExtendedHeaderExchange.
    • FraudProofService (optional): Handles retrieving BadEncodingFraudProofs and StateFraudProofs via FraudProofSub and FraudProofStore.
    • ShareService: Handles retrieving shares via sampling or namespace via ShareExchange and ShareStore.
    • StateService (optional): Handles retrieving state for a block height or account via StateExchange.
    • TransactionService (optional): A server handling endpoints like /submit_tx via SubmitTx.

    Services available to Full Nodes

    Full nodes include all light node services with the following specialized implementations:

    • ExtendedHeaderService: Adds generation and verification (ExtendedHeaderVerification) capabilities.
    • FraudProofService: Adds FraudProofGeneration.
    • ShareService: Uses ShareExchange but does not use a separate ShareStore (as it stores full blocks instead).
    • BlockService: Manages BlockErasureCoding, NewBlockEventSubscription (from Celestia Core), BlockExchange (between full nodes), and BlockStore.
    • StateService & TransactionService: Same as light nodes.
  3. Compare Celestia Light Nodes and Full Nodes

    main

    A Celestia Node can be initialized in two modes, each providing different capabilities and services:

    Light Node

    Designed for Data Availability (DA) sampling. It performs the following:

    • Verifies ExtendedHeaders.
    • Propagates relevant block information (ExtendedHeaders and BadEncodingFraudProofs) to peers.
    • Performs and serves sampling and SharesByNamespace requests.
    • Requests State to retrieve AccountBalance for transaction submission.

    Full Node

    Includes all light node capabilities plus additional services for block processing and storage:

    • Receives "raw" (un-erasure coded) blocks from a Celestia Core node via NewBlockEvents (using the /block RPC endpoint).
    • Performs erasure coding and verifies erasure coding.
    • Generates ExtendedHeaders (containing the raw block header, DataAvailabilityHeader (DAH), and ValidatorSet) and serves them to the network.
    • Can optionally request raw blocks from other full nodes.
  4. Understand the ShrEx/Sub Protocol

    main

    The ShrEx/Sub (Share Exchange/Subscribe) protocol is a push-based notification system used to disseminate new block availability information across the Celestia data availability network. It implements a publish-subscribe pattern to allow nodes (specifically the Peer Manager) to build pools of reliable peers for data access.

    Key Characteristics

    • Pattern: Publish-Subscribe (PubSub).
    • Transport: Built on libp2p's FloodSub router (rather than GossipSub) to ensure notifications reach all connected peers without the risk of isolation caused by GossipSub's mesh pruning.
    • Topic ID: /eds-sub/0.0.1
    • Message Format: Protocol Buffers (protobuf) with a fixed 40-byte payload.

    Message Schema

    Every notification contains the following fields:

    • data_hash: bytes[32] (the root hash)
    • height: uint64 (the block height)
  5. How Bad Encoding Fraud Proofs (BEFP) work

    main

    A Bad Encoding Fraud Proof (BEFP) is generated by a Full Node when it detects ErrByzantineData from the rsmt2d library during block reparation (i.e., when recovered data does not match its respective row/column roots).

    When a BEFP is detected:

    1. The Full Node generates a BadEncodingProof containing the Height, the Index of the failed row/column, the Axis (ROW or COL), and the Shares (including their MerkleProof).
    2. The Full Node broadcasts this proof to the network via a Broadcaster using libp2p pubsub.
    3. Light and Full nodes subscribe to the BEFP topic to receive and verify these proofs.
    4. Upon receiving a valid BEFP, nodes must stop dependent services: DAS, Syncer, and SubmitTx.
    5. Valid BEFPs are stored on disk using the FraudStore interface under the fraud/badEncodingProof path.
    // Example of the BadEncodingProof structure
    type BadEncodingProof struct {
        Height uint64
        Shares []*ShareWithProof
        Index  uint8
        Axis   rsmt2d.Axis
    }
  6. Understand SHREX node types and roles

    main

    The SHREX protocol defines two primary node types with distinct responsibilities:

    Light Nodes (LN)

    Light nodes are consumers of data. Their responsibilities include:

    • Subscribing to ShrEx/Sub notifications to learn about new blocks.
    • Using Peer Discovery (DHT) to find bridge nodes.
    • Using the SHREX Client to request specific data.
    • Note: Light nodes do not advertise themselves or serve data to others.

    Bridge Nodes (BN)

    Bridge nodes are providers of data. Their responsibilities include:

    • Acting as ShrEx/Sub Publishers to announce new block availability.
    • Using the Discovery Service to advertise their presence.
    • Acting as a SHREX Server to serve requested data.
    • Storing block data (both recent and historical).
    • Advertising: Bridge nodes advertise themselves using the full tag (or the archival tag if the pruner is disabled).
    ┌─────────────────────────────────────────────────────────────┐
    │                        Light Node                           │
    ├─────────────────────────────────────────────────────────────┤
    │                                                             │
    │  ┌────────────────────────────────────────────────────┐     │
    │  │              Peer Manager                          │     │
    │  │  - Maintains validated peer pools                  │     │
    │  │  - Selects optimal peers for requests              │     │
    │  │  - Validates ShrEx/Sub notifications               │     │
    │  └──────┬──────────────────────┬──────────────────┬───┘     │
    │         │                      │                  │         │
    │         │                      │                  │         │
    │  ┌──────▼────────┐    ┌────────▼────────┐  ┌─────▼──────┐   │
    │  │  ShrEx/Sub    │    │   Discovery     │  │  SHREX     │   │
    │  │  Subscriber   │    │   Service       │  │  Client    │   │
    │  │               │    │                 │  │            │   │
    │  └───────────────┘    └─────────────────┘  └────────────┘   │
    │                                                             │
    └─────────────────────────────────────────────────────────────┘
                                  │
                                  │ Network
                                  │
    ┌─────────────────────────────────────────────────────────────┐
    │                    Bridge Node                              │
    ├─────────────────────────────────────────────────────────────┤
    │                                                             │
    │  ┌────────────────┐    ┌──────────────┐  ┌──────────────┐   │
    │  │  ShrEx/Sub     │    │  Discovery   │  │   SHREX      │   │
    │  │  Publisher     │    │  Service     │  │   Server     │   │
    │  │                │    │              │  │              │   │
    │  └────────────────┘    └──────────────┘  └──────────────┘   │
    └─────────────────────────────────────────────────────────────┘
  7. How the Peer Manager coordinates peer selection in SHREX

    main

    The Peer Manager is the central coordination component for peer selection in the SHREX protocol. It manages two types of peer sources to ensure efficient data retrieval:

    1. ShrEx/Sub Notifications: Peers announcing specific data availability via pubsub. These are placed into Data Hash Pools and must be validated against headers before being promoted.
    2. Discovery Service: Peers found via DHT-based discovery. These are added directly to the Discovered Nodes Pool.

    Peer Selection Priority

    When requesting data, the manager follows this priority order:

    1. First Priority: Peers from validated data-hash-specific pools (highest confidence).
    2. Second Priority: Peers from the general Discovered Nodes Pool.
    3. Blocking: If no peers are available, the system waits subject to a context timeout.

    Peer Pool Lifecycle

    • Data Hash Pools: Created when a ShrEx/Sub notification arrives for a specific hash. Peers are added to these pools and held until a matching header is received via the Header Subscription. Once validated, peers are promoted to the Discovered Nodes Pool.
    • Discovered Nodes Pool: A general pool containing peers from the Discovery service and promoted peers from validated Data Hash Pools.
    /* Peer Selection Priority Logic */
    1. Validated data-hash-specific pools
    2. Discovered nodes pool
    3. Blocking (subject to context timeout)
  8. Understand the role of StateService and TxSub

    main

    The StateService manages state interactions required for transaction submission:

    • Responsibilities: Fetching account balances, preparing transactions, and propagating them via TxSub.
    • Node Interactions:
      • Bridge Nodes: Listen to TxSub and relay transactions into the celestia-core mempool.
      • Light and Full Nodes: Can publish transactions to TxSub, but they do not need to listen to it.
  9. Choose between Upload and Submit flows in Fibre

    main

    The Fibre API provides two distinct ways to handle data submission, allowing callers to choose the level of control required for their workflow:

    1. Staged Upload flow: A multi-step process for uploading data.
    2. Full Submit flow: A single-step process for submitting data.

    These flows are exposed explicitly through the Fibre module rather than being hidden within blob-module options.

  10. How bad encoding fraud proofs work

    main

    Fraud proofs are used to maintain network integrity regarding data encoding:

    • Full Nodes: Generate proofs within ShareService during block reconstruction. If fraud is detected, the node broadcasts the proof to the FraudSub gossip network and halts.
    • Light Nodes: Listen to the FraudSub network for these proofs. They verify the proof against the relevant header hash. If the proof is valid, the light node must immediately halt all operations. If invalid, it continues normally.
  11. Understand the Celestia Node modular API design

    main

    The celestia-node API is designed to be modular rather than monolithic. Instead of a single large service, functionality is segregated into independent, categorized modules. This design ensures that all node types (e.g., Full, Light, Bridge) implement a unified set of APIs, with differences appearing only in specific module implementations (like FullAvailability vs LightAvailability).

    Key design principles:

    • Module-centric: APIs are grouped into logical modules (e.g., HeaderModule, StateModule).
    • Unified: All node types share the same API surface.
    • Embeddable: The API is designed as a library that can be constructed into an application, rather than a rigid framework.
    • Language-agnostic: Interfaces are designed to be easily implemented via RPC in other languages.
  12. Understand the StateService and StateAccessor abstractions

    main

    The celestia-node uses two primary abstractions to manage interaction with the Celestia network state and transaction submission:

    1. StateService: A high-level service that manages RPC endpoints. It acts as the entry point for users to access state-related methods.
    2. StateAccessor: An interface that defines how the node interacts with celestia-core to retrieve account information and submit transactions.

    Both light and full nodes run a StateService. The StateAccessor interface allows for different underlying implementations (CORE, P2P, or LOCAL) without changing the high-level service logic.

    type StateService struct {
       accessor StateAccessor
    }
    
    type StateAccessor interface {
       // Balance retrieves the Celestia coin balance for the node's account/signer.
       Balance(ctx context.Context) (*Balance, error)
       // BalanceForAddress retrieves the Celestia coin balance for the given address.
       BalanceForAddress(ctx context.Context, addr types.AccAddress) (*Balance, error)
       // SubmitTx submits the given transaction/message and blocks until included in a block.
       SubmitTx(ctx context.Context, tx Tx) (*TxResponse, error)
    }