Malachite BFT Consensus Engine

repository·main·Indexed 19 days ago

https://github.com/circlefin/malachite

A high-performance, flexible BFT consensus engine written in Rust that provides a library-based implementation of the Tendermint algorithm. It features a coroutine-based effect system in the malachitebft-core-consensus crate to decouple core logic from environment side effects, and includes components for node discovery, peer connection management, and a Validator Proof Protocol for cryptographic identity verification.

Tokens
145.3K
Snippets
340
Records
562
Agent score
65%

What's inside Malachite

  1. Overview of the Malachite Quint Specification

    main

    The Malachite Quint Specification is a formal specification of the Malachite consensus engine. It is organized into three distinct layers to facilitate both implementation and verification:

    1. Functional Layer: Captures the core logic of the consensus engine, including consensus logic, the vote keeper, and the driver.
    2. State Machine Layer: Captures the execution of Tendermint consensus within a distributed setting. This layer is designed for both simulation and the generation of random traces used in model-based testing.
    3. Runs and Tests: Provides documentation for interesting consensus scenarios and serves as the testing suite for the functional layer.

    Developers can use the domain-specific language (DSL) provided in the specification to compose complex runs and scenarios.

  2. Overview of Malachite Consensus

    main

    Malachite core libraries provide a Rust implementation of the Tendermint consensus algorithm. The implementation is verified against an executable specification written in Quint using model-based testing to ensure the code strictly adheres to the protocol specification.

    The consensus specification is organized into several key areas:

    • Protocol Overview: Summary of Tendermint consensus operations and components at the protocol level.
    • Algorithm Pseudo-code: A direct implementation of the algorithm described in the Tendermint paper.
    • Byzantine Misbehavior: Definitions and detection mechanisms for various types of misbehavior by Byzantine processes that could lead to system disagreement.
    • Implementation Design: An overview of how the Tendermint algorithm is specifically designed and structured within the Malachite codebase.
  3. What is Malachite?

    main

    Malachite is a flexible, high-performance Byzantine-fault tolerant (BFT) consensus engine implemented in Rust. It provides a state-of-the-art implementation of the Tendermint BFT consensus algorithm, designed to be used as a library for building decentralized systems such as sequencers, social networks, and Layer-1 blockchains.

    Note: Malachite is currently in alpha and has not been externally audited. Use at your own risk.

  4. Understand the Malachite network layer

    main

    Malachite implements its node-to-node communication primitives using the libp2p networking library. The network layer is primarily composed of two functional components:

    1. Peer Discovery Protocol: Handles how nodes find each other within the network.
    2. Gossip Communication Protocol: Handles the propagation of messages and data across the network using a gossip pattern.

    For specific implementation details and usage of these protocols, refer to the discovery and gossip documentation sections.

  5. What is the Malachite ValueSync Protocol?

    main

    ValueSync is a synchronization protocol designed to ensure long-term stability in Malachite nodes. It addresses the problem of nodes falling behind the consensus height due to extended downtime or network disconnections.

    Without ValueSync, if too many nodes fall behind and cannot catch up, the network may lack sufficient synchronized validators to make progress. ValueSync allows nodes to quickly obtain missing data (certificates and values) from other nodes to catch up to the current consensus height.

  6. What is the Proof-of-Validator (PoV) protocol?

    main

    The Proof-of-Validator (PoV) protocol is an independent module that allows validator nodes to cryptographically prove their status to other peers in the network. This enables nodes to distinguish between validators and non-validator peers, which is critical for consensus-related operations.

    Key use cases for the PoV protocol include:

    • Debugging and Observability: Classifying peers to improve operational visibility.
    • Connection Prioritization: Prioritizing connections with validators to ensure reliable delivery of consensus messages.
    • Mesh Formation Optimization: Prioritizing validators when constructing gossipsub's mesh overlay.
  7. What is Accountable Tendermint?

    main

    Accountable Tendermint is a variation of the Tendermint consensus algorithm designed to ensure the detection of amnesia attacks in scenarios where agreement is violated (i.e., two correct processes decide on different values).

    While standard Tendermint can detect double votes, it cannot always identify who misbehaved during amnesia attacks. Accountable Tendermint provides an [Accountability] guarantee: if there are fewer than 2f + 1 Byzantine faulty processes out of 3f + 1 total processes, and agreement is violated, a correct node can collect sufficient data to identify at least f + 1 Byzantine processes.

  8. Customize consensus behavior with the Middleware system

    main

    The Middleware trait (found in malachitebft-test) allows you to intercept and modify consensus behavior at key points. This is primarily used for fault injection and testing different network conditions.

    Available hooks in the Middleware trait include:

    • get_validator_set(): Override the validator set for a specific height.
    • get_timeouts(): Override consensus timeouts.
    • on_propose_value(): Intercept or modify incoming proposals.
    • get_validity(): Control decisions regarding proposal validity.
    • on_commit(): Hook into the commit process.

    Built-in middleware variants include DefaultMiddleware, RotateValidators, EpochValidators, and RotateEpochValidators.

  9. Precommit step details

    main

    The precommit step determines if a round has succeeded:

    • Commitment: If a process observed an agreement on value $v$ during the prevote step, it broadcasts a PRECOMMIT message for id(v). Otherwise, it broadcasts nil.
    • Success: If a super-majority of processes broadcast PRECOMMIT for the same value $v$, the height is finalized with that value.
    • Failure: If conflicting information is received or a timeout occurs, the round fails and the process moves to the next round.
  10. Understand the Consensus Write-Ahead Log (WAL) and Crash-Recovery Model

    main

    Malachite follows a crash-recovery failure model. To ensure a recovering process is indistinguishable from a process that simply paused, Malachite uses a Write-Ahead Log (WAL).

    A WAL is an append-only registry (typically a file) where all relevant inputs are logged to persistent storage before they are applied to the consensus state machine.

    Key Requirements for Correctness:

    1. Determinism: The consensus implementation must be deterministic. Given the same initial state and sequence of inputs, the state and outputs must always be identical. If the implementation is non-deterministic, replaying the WAL could lead to equivocation (producing different outputs for the same input), which makes the process Byzantine and slashable.
    2. Logging: All relevant inputs processed during ordinary execution must be logged to persistent storage.
    3. Replay: Upon recovery, the process must retrieve and sequentially execute (replay) the logged inputs from the WAL to reconstruct the state as it existed before the crash.

    Risks of Improper Recovery:

    If state is not properly recovered (e.g., due to 'Amnesia'), a process might:

    • Forget promises: Such as a lock on a specific value in a consensus round.
    • Equivocate: Emit conflicting messages (e.g., a Prevote for a new value after having previously committed to a different state) because it lost its previous context.