superblock

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2025 License: GPL-3.0 Imports: 26 Imported by: 0

README

Superblock Construction Protocol (SBCP)

This package implements the Superblock Construction Protocol (SBCP), a system designed to coordinate the creation of " superblocks" that bundle transactions from multiple independent rollups. Its primary goal is to enable synchronous composability and atomic cross-chain transactions in a multi-rollup environment.

The protocol is orchestrated by a central node, the Shared Publisher (SP), which manages a slot-based timeline. Participating rollup Sequencers follow the SP's timeline to build and submit their individual L2 blocks, which are then aggregated into a final superblock.

For a detailed formal specification of the protocol, please see Superblock Construction Protocol.

Core Concepts

  • Shared Publisher (SP): The central coordinator node that orchestrates the entire protocol. The logic for the SP is primarily contained in coordinator.go.
  • Sequencer: A node representing a participating rollup chain. It builds L2 blocks according to the SP's signals. The logic for a sequencer is contained in the sequencer/ sub-package.
  • Slot: A fixed-duration time interval (12 seconds, aligned with Ethereum slots) during which transactions are collected and a superblock is proposed. The slot/ package manages this timing and the associated state machine.
  • Superblock: A block that contains a collection of L2 blocks from different rollups for a specific slot, along with metadata about included cross-chain transactions.
  • Cross-Chain Transaction (XT): A transaction involving operations on multiple rollups, which must be executed atomically.
  • Synchronous Composability Protocol (SCP): The underlying consensus mechanism (a 2-Phase Commit protocol) used to agree on the outcome (commit or abort) of a cross-chain transaction across all participating sequencers. The core logic for this is in the x/consensus package.

Architecture Deep Dive

The x/superblock package is organized into sub-packages, each with a distinct responsibility.

  • / (Root):

    • coordinator.go: The heart of the Shared Publisher. It drives the slot lifecycle, orchestrates SCP instances for cross-chain transactions, and ultimately builds the final superblock.
    • interfaces.go: Defines the core interfaces for the SP and slot management.
  • adapter/:

    • A crucial bridge package. publisher_wrapper.go wraps a generic P2P publisher (from x/publisher) and injects the SBCP logic, turning a standard node into a Shared Publisher.
  • handlers/:

    • Implements a Chain of Responsibility pattern for processing incoming P2P messages. This allows for clean separation of message-handling logic specific to SBCP.
  • sequencer/:

    • Contains the complete logic for a participating Sequencer node. This includes its own state machine ( Waiting -> Building-Free -> Building-Locked -> Submission), a block builder, and logic for following the SP's commands.
  • slot/:

    • manager.go: A clean, focused time manager for calculating slot boundaries and progress.
    • state_machine.go: Implements the SP's core state machine (StartingFreeLockedSealing), which governs the protocol's execution phases within a slot.
  • store/, queue/, wal/:

    • These packages define interfaces for persistence and data management. They provide in-memory implementations for testing, with the expectation that production-level backends will be implemented.
  • l1/ & registry/:

    • Handle interaction with the L1 blockchain. l1/ is responsible for publishing the final superblock. registry/ is responsible for discovering and tracking the set of active rollups.

Message and Consensus Handling

The architecture uses a layered approach to handle network messages and consensus, which allows for both a simple standalone mode and the full SBCP mode.

  1. Consensus Engine (x/consensus): Provides the core, reusable 2-Phase Commit (2PC) logic. It is not a network handler itself but a library that is driven by a consumer and reports outcomes via callbacks.
  2. Base Publisher (x/publisher): Provides a basic publisher that can run a simple 2PC for cross-chain transactions. It has its own simple handler for Vote and XTRequest messages.
  3. SBCP Adapter (x/superblock/adapter): This is the key to enabling SBCP mode. It wraps the base publisher and intercepts the entire flow:
    • Message Interception: It replaces the transport's message handler with its own HandlerChain. This chain processes SBCP-specific messages (like L2Block) first.
    • Fallback: Any message not handled by the SBCP chain (like a Vote) is passed down to the base publisher's original handler, which then feeds it to the consensus engine.
    • Callback Interception: It overrides the consensus engine's decision callback. This allows the SBCP layer to be notified of a transaction's outcome, so it can update its own slot state machine accordingly (e.g., moving from Locked back to Free).

This powerful composition pattern allows the superblock module to add its complex, stateful logic on top of a simpler, generic consensus and publisher layer.

Protocol Flow (Happy Path)

A single slot follows the sequence defined in the protocol specification:

  1. Slot Start: The SP's Coordinator begins a new slot and broadcasts a StartSlot message.
  2. Block Building: Sequencers receive StartSlot, transition to the Building-Free state, and begin constructing their L2 blocks.
  3. Cross-Chain Transaction (XT): a. An XTRequest is submitted to the SP, which places it in a priority queue. b. The SP takes a request from the queue, transitions its state to Locked, and initiates SCP by broadcasting a StartSC message. c. Sequencers transition to Building-Locked, validate their part of the XT, and send a Vote to the SP. d. The SP gathers votes and broadcasts a Decided message. e. Sequencers commit or abort the XT in their draft blocks and transition back to Building-Free.
  4. Seal Request: At the seal cutover point (e.g., 8/12 seconds), the SP transitions to Sealing and broadcasts a RequestSeal message with the list of all included XTs for the slot.
  5. L2 Block Submission: Sequencers transition to Submission, finalize their L2 blocks, and send the completed L2Block to the SP.
  6. Superblock Creation: The SP gathers all L2 blocks, validates them, assembles the final Superblock, and publishes it to the L1 chain.
  7. Next Slot: The SP transitions back to StartingSlot to begin the cycle anew.

Refactoring and Future Work

This package is under active development. Key areas for improvement include:

  • Production-Grade Dependencies: The current implementation relies on in-memory mocks for storage, queuing, and L1 interaction. These must be replaced with robust, production-ready implementations (e.g., using a database like BadgerDB and a real Ethereum client).
  • Decoupling via Dependency Injection: The adapter/publisher_wrapper.go needs to be refactored to accept its dependencies via interfaces, rather than instantiating them directly. This is the highest-priority task to make the system configurable and testable.
  • Coordinator Refactoring: The main Coordinator is a "God object" that should be broken down into smaller, more focused components (e.g., SlotLifecycleManager, SuperblockBuilder) to improve maintainability and testability.
  • Unify Handling Logic: The current mix of a HandlerChain, a fallback handler, and intercepted callbacks is functional but complex. This could be refactored into a single, unified message router at the adapter level to make the flow of control more explicit.
  • Comprehensive Testing: A full suite of unit and integration tests is required to ensure the protocol's correctness and stability.
  • WAL Recovery: The Write-Ahead Log (WAL) recovery path needs to be fully implemented to ensure the system can recover gracefully from a crash, as outlined in the protocol specification.
  • Parallelism Optimizations: The protocol specification describes opportunities for advanced parallelism (e.g., processing local transactions during the Building-Locked state). This is a significant future optimization.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Slot   slot.Config  `mapstructure:"slot"   yaml:"slot"`
	Queue  queue.Config `mapstructure:"queue"  yaml:"queue"`
	Store  store.Config `mapstructure:"store"  yaml:"store"`
	WAL    wal.Config   `mapstructure:"wal"    yaml:"wal"`
	L1     l1.Config    `mapstructure:"l1"     yaml:"l1"`
	Proofs ProofsConfig `mapstructure:"proofs" yaml:"proofs"`

	// Coordinator-level settings
	MaxConcurrentSlots     int           `mapstructure:"max_concurrent_slots"     yaml:"max_concurrent_slots"`
	BlockValidationTimeout time.Duration `mapstructure:"block_validation_timeout" yaml:"block_validation_timeout"`
}

Config aggregates configuration for all SBCP components

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for production deployment

type Coordinator

type Coordinator struct {
	// contains filtered or unexported fields
}

Coordinator orchestrates the Superblock Construction Protocol (SBCP) by managing slot-based execution, cross-chain transactions, and L2 block assembly

func NewCoordinator

func NewCoordinator(
	config Config,
	log zerolog.Logger,
	metrics prometheus.Registerer,
	registryService registry.Service,
	l2BlockStore store.L2BlockStore,
	superblockStore store.SuperblockStore,
	xtQueue queue.XTRequestQueue,
	l1Publisher l1.Publisher,
	walManager wal.Manager,
	consensusCoord consensus.Coordinator,
	transport transport.Server,
	collector apicollector.Service,
	prover proofs.ProverClient,
) *Coordinator

func (*Coordinator) Consensus

func (c *Coordinator) Consensus() consensus.Coordinator

Consensus returns the underlying consensus coordinator.

func (*Coordinator) GetActiveTransactions

func (c *Coordinator) GetActiveTransactions() []*pb.XtID

func (*Coordinator) GetCurrentSlot

func (c *Coordinator) GetCurrentSlot() uint64

func (*Coordinator) GetSlotState

func (c *Coordinator) GetSlotState() slot.State

func (*Coordinator) GetStats

func (c *Coordinator) GetStats() map[string]interface{}

func (*Coordinator) HandleL2Block

func (c *Coordinator) HandleL2Block(ctx context.Context, from string, l2Block *pb.L2Block) error

HandleL2Block is an exported wrapper to process incoming L2 block messages.

func (*Coordinator) Logger

func (c *Coordinator) Logger() *zerolog.Logger

Logger exposes the coordinator's logger for external packages (e.g., handlers).

func (*Coordinator) Start

func (c *Coordinator) Start(ctx context.Context) error

func (*Coordinator) StartSCPForAdapter

func (c *Coordinator) StartSCPForAdapter(ctx context.Context, xtReq *pb.XTRequest, xtID []byte, from string) error

StartSCPForAdapter allows adapter layer to initiate SCP with a constructed request.

func (*Coordinator) StateMachine

func (c *Coordinator) StateMachine() *slot.StateMachine

StateMachine exposes the slot state machine for external observers.

func (*Coordinator) Stop

func (c *Coordinator) Stop(ctx context.Context) error

func (*Coordinator) SubmitXTRequest

func (c *Coordinator) SubmitXTRequest(ctx context.Context, from string, request *pb.XTRequest) error

func (*Coordinator) Transport

func (c *Coordinator) Transport() transport.Server

Transport exposes the transport server for broadcasting messages from adapters.

type ProofsConfig

type ProofsConfig struct {
	Enabled bool `mapstructure:"enabled" yaml:"enabled"`

	Collector struct {
		RequireAllChains bool          `mapstructure:"require_all_chains" yaml:"require_all_chains"`
		WaitTimeout      time.Duration `mapstructure:"wait_timeout" yaml:"wait_timeout"`
		RequiredChainIDs []uint32      `mapstructure:"required_chain_ids" yaml:"required_chain_ids"`
	} `mapstructure:"collector" yaml:"collector"`

	Prover struct {
		BaseURL      string        `mapstructure:"base_url" yaml:"base_url"`
		PollInterval time.Duration `mapstructure:"poll_interval" yaml:"poll_interval"`
		ProofType    string        `mapstructure:"proof_type" yaml:"proof_type"`
	} `mapstructure:"prover" yaml:"prover"`

	// If false, SP may fall back to publishing without a proof on prover failure.
	RequireProof bool `mapstructure:"require_proof" yaml:"require_proof"`
}

ProofsConfig controls the optional proof pipeline (collection → proving → L1 publish with proof).

func DefaultProofsConfig

func DefaultProofsConfig() ProofsConfig

DefaultProofsConfig returns sensible defaults.

type SlotExecution

type SlotExecution struct {
	Slot                 uint64                            `json:"slot"`
	State                slot.State                        `json:"state"`
	StartTime            time.Time                         `json:"start_time"`
	NextSuperblockNumber uint64                            `json:"next_superblock_number"`
	LastSuperblockHash   []byte                            `json:"last_superblock_hash"`
	ActiveRollups        [][]byte                          `json:"active_rollups"`     // Rollups participating in slot
	ReceivedL2Blocks     map[string]*pb.L2Block            `json:"received_l2_blocks"` // Key: chainID
	SCPInstances         map[string]*slot.SCPInstance      `json:"scp_instances"`      // Key: xtID
	L2BlockRequests      map[string]*pb.L2BlockRequest     `json:"l2_block_requests"`  // Key: chainID
	AttemptedRequests    map[string]*queue.QueuedXTRequest `json:"attempted_requests"` // xtID hex -> queued request
}

SlotExecution tracks the execution state for a single slot in the SBCP

Directories

Path Synopsis
l1
tx
Package queue.
Package queue.
Package store.
Package store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL