canopy

consensus.go - Blockchain Synchronization and Consensus Management

This file contains the high-level functionality for maintaining agreement on blocks in the Canopy blockchain. It handles synchronization with peers and manages consensus messaging between validators.

Overview

The consensus.go file is responsible for:

Core Components

Synchronization System

The synchronization system allows nodes to catch up with the current blockchain state. When a node falls behind or joins the network, it:

  1. Polls peers to determine the current maximum height and VDF iterations
  2. Requests blocks one at a time from peers
  3. Verifies each block by checking if the certificate was signed by a 2/3 majority
  4. Commits valid blocks to the state machine
  5. Continues until it reaches the maximum peer height

The system includes safeguards against malicious peers by tracking reputation and validating VDF iterations to prevent unsafe forks.

Consensus Message Routing

This component handles the routing of consensus messages between validators in a committee. It:

The system distinguishes between sending to self (internal routing) and sending to peers (via P2P network).

Block Request Handling

This component manages block requests from syncing peers:

It includes reputation management to penalize peers that exceed rate limits or send invalid requests.

Committee Management

The system maintains connections with committee members:

Status Reporting

Provides detailed information about the current consensus state:

This information is useful for monitoring and debugging the consensus process.

Processes

Blockchain Synchronization Process

flowchart TD
    A[Start Sync] --> B{Single Node Network?}
    B -->|Yes| C[Finish Syncing]
    B -->|No| D[Poll Max Height]
    D --> E{Syncing Done?}
    E -->|Yes| C
    E -->|No| F[Request Block]
    F --> G{Block Received?}
    G -->|Yes| H[Handle Peer Block]
    G -->|No| I[Timeout]
    H --> J{Block Valid?}
    J -->|Yes| K[Update Height]
    J -->|No| L[Slash Reputation]
    K --> E
    L --> D
    I --> D

Consensus Message Flow

flowchart LR
    A[Receive Consensus Message] --> B{Node Syncing?}
    B -->|Yes| C[Discard Message]
    B -->|No| D[Unmarshal Message]
    D --> E[Route to Consensus Module]
    E --> F{Error?}
    F -->|Yes| G[Slash Reputation]
    F -->|No| H[Process Message]

Security Features

The file implements several security measures:

These security features help maintain the integrity of the blockchain by protecting against various attack vectors and ensuring that only valid blocks are added to the chain.