rbuilder Documentation

repository·develop·Indexed 20 days ago

https://github.com/flashbots/rbuilder

A high-performance Ethereum MEV-Boost block builder written in Rust for live production and historical backtesting. Features include the Parallel Builder for sophisticated bundle merging, bid-scraper for relay bid monitoring, BuilderNet Rebalancer for automated fund management, and eth-sparse-mpt for optimized root hash calculations. Integrates with Reth for state access and supports testing via a dummy test-relay implementation.

Tokens
82.6K
Snippets
272
Records
338
Agent score
69%

What's inside rbuilder

  1. Overview of rbuilder

    develop

    rbuilder is an open-source, high-performance Ethereum MEV-Boost block builder implemented in Rust. It is designed for both production block building and research into block building algorithms.

    Key Features:

    • Multiple algorithms: Supports various block building strategies. The default ordering_builder.rs algorithm sorts orders by effective gas price or total profit and attempts execution.
    • Backtesting: Built-in support for testing building strategies against historical mempool data using mempool-dumpster.
    • Bundle merging: Automatically drops bundles targeting transactions already included in a pending block if they are marked in reverting_tx_hashes.
    • Smart nonce management: Handles nonce dependencies between bundles and transactions.
    • Reth Integration: Leverages the Reth Ethereum node for high-performance state access.
    • Reproducible builds: Supports deterministic builds via SOURCE_DATE_EPOCH.
  2. What is BuilderNet Rebalancer?

    develop
    BuilderNet Rebalancer is a standalone service designed to automate fund management for BuilderNet builders. It monitors balances for a configured set of accounts and initiates EOA-originated transfers to keep operational wallets (like fee payer accounts) topped up. This automation ensures that wallets used for bid adjustments maintain sufficient balances without manual intervention, while keeping transfers separate from builder profit in coinbase accounting.
  3. Use test-relay for local builder testing

    develop

    The test-relay crate provides a dummy MEV-boost relay implementation. It allows block builders to test their workflows without submitting to a production MEV-boost relay.

    To use test-relay, you must provide:

    • A real MEV-boost relay URL
    • A connection to a consensus layer node
    • (Optional) A validation endpoint to validate blocks

    It also exposes metrics, such as estimated slot auction winners among builders who submit to this relay.

  4. What is the Parallel Builder?

    develop

    The Parallel Builder is a block building algorithm designed to run key building components in parallel to enable more sophisticated bundle merging.

    Its primary mechanism is identifying groups of conflicting orders and resolving them independently and in parallel. This allows the system to pipeline the stages of orderflow intake, conflict resolution, and final block construction.

  5. Configure the start slot watchdog

    develop

    The rbuilder can be configured to use a watchdog to ensure the builder is actively processing slots. If watchdog_timeout is set in the BaseConfig, a watchdog thread is spawned. The builder must send a message for every slot it starts building; otherwise, the watchdog will trigger a process kill to prevent the service from hanging in an unhealthy state.

    Use create_start_slot_watchdog to initialize this mechanism using a BaseConfig and a CancellationToken.

    pub fn create_start_slot_watchdog(
        config: &BaseConfig,
        cancel: CancellationToken,
    ) -> std::io::Result<Option<flume::Sender<()>>>
  6. Configure publishers in bid-scraper

    develop

    Publishers are defined in the configuration file using the [[publishers]] array syntax. Each publisher requires a type and a unique name. The type determines which specific parameters are required for that publisher instance.

    [[publishers]]
    type = "relay-headers"
    name = "relay-headers-1"
  7. Understanding and mitigating reorg losses

    develop

    Block reorganizations (reorgs) on Ethereum can cause builders to lose funds. When a reorg occurs, transactions from the losing fork (including builder-generated transactions used to pay validators) are returned to the mempool. This can result in the builder paying a bid to a random validator without actually winning a block. This loss can occur even if the transaction provides no tip to the block.

    To mitigate these losses, the following strategies are being considered:

    1. Contract-based payments: Pay via a contract that verifies the coinbase is the builder's address and reverts if it is not. This requires gas analysis to balance security against the extra cost added to regular blocks.
    2. Reorg detection and nonce replacement: Detect reorgs and immediately post a transaction with the same nonce using the smallest possible gas tip. This attempts to lure builders into using the new transaction instead of the reorged one.
    3. Periodic fund withdrawal: Periodically move winnings from the builder's account to a separate location to limit the maximum potential loss in a single event.
  8. Configure building algorithms in rbuilder

    develop

    rbuilder supports multiple building algorithms, and each algorithm can be instantiated multiple times with its own unique set of parameters. Each algorithm instance must include a name and an algo type.

    Supported algorithms:

    • ordering-builder: Uses the OrderingBuildingAlgorithm.
    • parallel-builder: (Experimental) Uses the ParallelBuilder.

    To use environment variables for any parameter, prefix the value with env: followed by the variable name (e.g., env:MY_VAR_NAME).

    [[algorithms]]
    name = "my-ordering-algo"
    algo = "ordering-builder"
    discard_txs = true
    sorting = "mev-gas-price"
    failed_order_retries = 3
    drop_failed_orders = false
  9. Understand block sealing and bidding in LiveBuilder

    develop

    In the LiveBuilder dataflow, once blocks are filled with orders, they must be converted into bids. This involves two main steps:

    1. Bidding Decision: Deciding how much to bid for a block. Every block has a true block value (the MEV generated by the block).
      • Bidding 0 aims for maximum profit (the full true block value).
      • Bidding the true block value results in zero profit.
      • Bidding above the true block value results in a loss (subsidizing the block).
    2. Sealing: Inserting the final payout transaction for the validator and computing the root hash.

    To implement this, the rbuilder example uses BlockSealingBidderFactory as the UnfinishedBlockBuildingSinkFactory.

  10. How block multiplexing and best block selection works

    develop

    Because order cancellations can decrease the true block value of a block currently being built by an algorithm, rbuilder does not simply bid every block it receives. Instead, it selects the best available block across all running building algorithms.

    This is handled by the UnfinishedBlockBuildingSinkMuxer. It acts as an UnfinishedBlockBuildingSink that receives all generated blocks and forwards only the one with the highest true block value to the next stage of the pipeline.

    Example Scenario: If Algorithm A produces a block with value 4, but then an order is cancelled and its value drops to 2, the Muxer will switch to using a block from Algorithm B that has a value of 3.

  11. Configure Relay Poller publishers

    develop

    Relay pollers use HTTP polling to fetch data. There are two types:

    1. relay-headers: Polls /eth/v1/builder/header/ to get the current top bid. Requires a beacon_node_uri.
    2. relay-bids: Polls /relay/v1/data/bidtraces/builder_blocks_received to get all bids. No extra fields required.

    Common fields for all Relay Pollers:

    • eth_provider_uri: Endpoint for an EL client (e.g., ws://127.0.0.1:8545).
    • relays_file: Path to a JSON file containing relay mappings. Example format:
      {
        "flashbots": "https://0xac6e77dfe25ecd6110b8e780608cce0dab71fdd5ebea22a16c0205200f2f8e2e3ad3b71d3499c54ad14d6c21b41a37ae@boost-relay.flashbots.net",
        "happy relay": "https://happy.com"
      }
    • request_start_s: Delay in seconds to start querying in each slot.
    • request_interval_s: Frequency of queries in seconds.
    • time_offset_count: Total number of instances used for proportional request distribution.
    • time_offset_index: The specific index for this instance (between 0 and time_offset_count - 1).
  12. Core components of LiveBuilder

    develop

    The LiveBuilder struct is the central orchestrator of rbuilder. To instantiate it, you must provide three core components:

    1. blocks_source: An implementation of the SlotSource trait that provides the source of slots to build. This allows rbuilder to work in different contexts, such as L1 (consensus client slots) or L2 (sequencer slots).
    2. builders: A vector of objects implementing the BlockBuildingAlgorithm trait. Each builder takes a base block state and a stream of simulated orders to continuously generate and optimize blocks for maximum value.
    3. sink_factory: A factory implementing UnfinishedBlockBuildingSinkFactory used to create the destination for built blocks. This abstraction handles different environments like L1 (requiring bidding), L2 (no bidding), or testing environments.