vrf-pbft 0.1.0

A Rust implementation of VRF-enhanced PBFT consensus protocol
Documentation
use vrf_pbft::{hex, Config, ConsensusEngine, Node};

fn main() {
    let config = Config::new(1); // f=1, needs 4 nodes
    let mut engine = ConsensusEngine::new(config);

    for i in 1..=4 {
        let node = Node::new(i, 1000).expect("failed to create node");
        engine.add_node(node);
    }

    println!("Running VRF-PBFT with 4 nodes (f=1)...\n");

    // VRF selection is probabilistic, so retry until a round succeeds
    loop {
        match engine.run_round() {
            Ok(block) => {
                println!("Consensus reached on round {}!", engine.round());
                println!("  Proposer:  node {}", block.proposer);
                println!("  Seed:      {}", block.seed);
                println!("  Prev hash: {}...", &hex(&block.prev_hash)[..16]);
                println!("  Hash:      {}...", &hex(&block.hash())[..16]);
                println!("\nMessages exchanged: {}", engine.messages().len());
                break;
            }
            Err(vrf_pbft::Error::NoProposer(r)) => {
                println!("Round {}: no proposer elected, retrying...", r);
            }
            Err(vrf_pbft::Error::InsufficientVotes { needed, got }) => {
                println!(
                    "Round {}: not enough votes ({}/{}), retrying...",
                    engine.round(),
                    got,
                    needed
                );
            }
            Err(e) => {
                eprintln!("Fatal error: {}", e);
                std::process::exit(1);
            }
        }
    }
}