guts-org/sdk
Client SDK for interacting with the GUTS network
Verified by quorum
Quorum size7 nodes
Network observers12 nodes
Last verified2 min ago
456 commits
5 branches
12 issues
3 pull requests
src/consensus/simplex.rs
use std::collections::HashMap;
use tokio::sync::mpsc;
use thiserror::Error;
/// Core consensus engine implementing Simplex BFT
pub struct ConsensusEngine {
/// Current view number
view: u64,
/// Validator set
validators: Vec<PublicKey>,
/// Pending transactions
mempool: Vec<Transaction>,
/// Finalized blocks
chain: Vec<Block>,
}
#[derive(Error, Debug)]
pub enum ConsensusError {
#[error("Invalid signature")]
InvalidSignature,
#[error("Quorum not reached")]
QuorumNotReached,
#[error("Block not found: {0}")]
BlockNotFound(u64),
}
impl ConsensusEngine {
/// Create a new consensus engine
pub fn new(validators: Vec<PublicKey>) -> Self {
Self {
view: 0,
validators,
mempool: Vec::new(),
chain: Vec::new(),
}
}
/// Process a new transaction
pub async fn submit_transaction(&mut self, tx: Transaction) -> Result<(), ConsensusError> {
// Verify transaction signature
if !tx.verify() {
return Err(ConsensusError::InvalidSignature);
}
self.mempool.push(tx);
Ok(())
}
/// Propose a new block
pub async fn propose_block(&mut self) -> Result<Block, ConsensusError> {
let transactions = self.mempool.drain(..).collect();
let block = Block::new(self.view, transactions);
Ok(block)
}
/// Finalize a block after reaching consensus
pub async fn finalize_block(&mut self, block: Block) -> Result<(), ConsensusError> {
self.chain.push(block);
self.view += 1;
Ok(())
}
}