Skip to content

Commit 67fdb65

Browse files
Chris Dukesclaude
authored andcommitted
✨ Add minimum quorum configuration to consensus protocols
- Byzantine: minQuorum = Math.floor(2*n/3)+1 (handles 33% malicious nodes) - Raft: minQuorum = Math.floor(n/2)+1 (majority consensus) - Gossip: minQuorum = configurable threshold (default 0.51) - CRDT: no quorum needed (eventual consistency) New Features: - Created complete Raft consensus protocol implementation - Added getMinQuorum() and hasQuorum() methods to all protocols - Updated consensus index with quorum calculation utilities - Enhanced Byzantine consensus with proper quorum validation - Added configurable quorum threshold for gossip protocol 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 5de4d56 commit 67fdb65

File tree

6 files changed

+904
-10
lines changed

6 files changed

+904
-10
lines changed

src/protocols/a2a/consensus/byzantine-consensus.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class ByzantineConsensus extends EventEmitter {
5151
private state: ConsensusState;
5252
private messageLog: ConsensusMessage[] = [];
5353
private faultThreshold: number;
54+
private minQuorum: number; // Byzantine: Math.floor(2*n/3)+1
5455
private timeoutDuration: number = 30000; // 30 seconds
5556
private viewChangeTimeout: ReturnType<typeof setTimeout> | null = null;
5657
private performance: {
@@ -66,6 +67,7 @@ export class ByzantineConsensus extends EventEmitter {
6667
) {
6768
super();
6869
this.faultThreshold = Math.floor((totalAgents - 1) / 3);
70+
this.minQuorum = Math.floor((2 * totalAgents) / 3) + 1; // Byzantine: 2f+1 where f is fault threshold
6971
this.state = this.initializeState();
7072
this.performance = {
7173
consensusRounds: 0,
@@ -473,7 +475,21 @@ export class ByzantineConsensus extends EventEmitter {
473475
const maliciousCount = Array.from(this.agents.values())
474476
.filter(a => a.isMalicious && this.state.activeAgents.has(a.id)).length;
475477

476-
return maliciousCount <= this.faultThreshold && activeCount >= 3 * this.faultThreshold + 1;
478+
return maliciousCount <= this.faultThreshold && activeCount >= this.minQuorum;
479+
}
480+
481+
/**
482+
* Get minimum quorum size for Byzantine consensus
483+
*/
484+
public getMinQuorum(): number {
485+
return this.minQuorum;
486+
}
487+
488+
/**
489+
* Check if we have sufficient nodes for quorum
490+
*/
491+
public hasQuorum(): boolean {
492+
return this.state.activeAgents.size >= this.minQuorum;
477493
}
478494

479495
/**

src/protocols/a2a/consensus/index.ts

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* Byzantine Fault-Tolerant Consensus System for Agent-to-Agent Communication
2+
* Consensus Protocols for Agent-to-Agent Communication
33
*
4-
* This module provides a comprehensive Byzantine consensus system that can handle
5-
* up to 33% malicious agents while maintaining correctness and security.
4+
* This module provides comprehensive consensus protocols for distributed systems:
65
*
76
* Features:
87
* - PBFT (Practical Byzantine Fault Tolerance) consensus algorithm
8+
* - Raft consensus protocol for leader-based consensus
99
* - Advanced voting mechanisms (weighted, quadratic, liquid democracy)
1010
* - Sophisticated malicious agent detection and isolation
1111
* - State machine replication with conflict resolution
@@ -14,6 +14,12 @@
1414
* - Integration with A2A security framework
1515
* - Comprehensive fault injection testing
1616
*
17+
* Quorum Requirements:
18+
* - Byzantine: Math.floor(2*n/3) + 1 (handles up to 33% malicious nodes)
19+
* - Raft: Math.floor(n/2) + 1 (majority consensus)
20+
* - Gossip: Configurable threshold (default 51%)
21+
* - CRDT: No quorum needed (eventual consistency)
22+
*
1723
* @author AI Assistant
1824
* @version 1.0.0
1925
*/
@@ -28,6 +34,15 @@ export {
2834
default as ByzantineConsensusDefault
2935
} from './byzantine-consensus';
3036

37+
export {
38+
RaftConsensus,
39+
RaftNode,
40+
LogEntry,
41+
RaftMessage,
42+
RaftState,
43+
default as RaftConsensusDefault
44+
} from './raft-consensus';
45+
3146
export {
3247
VotingMechanisms,
3348
Vote,
@@ -92,6 +107,44 @@ export {
92107
} from './consensus-security-integration';
93108

94109
// Utility types and constants
110+
export const QUORUM_CALCULATIONS = {
111+
/**
112+
* Calculate Byzantine consensus quorum (2f + 1)
113+
* @param totalAgents Total number of agents in the system
114+
* @returns Minimum quorum size for Byzantine consensus
115+
*/
116+
calculateByzantineQuorum: (totalAgents: number): number => {
117+
return Math.floor((2 * totalAgents) / 3) + 1;
118+
},
119+
120+
/**
121+
* Calculate Raft consensus quorum (majority)
122+
* @param totalAgents Total number of agents in the system
123+
* @returns Minimum quorum size for Raft consensus
124+
*/
125+
calculateRaftQuorum: (totalAgents: number): number => {
126+
return Math.floor(totalAgents / 2) + 1;
127+
},
128+
129+
/**
130+
* Calculate gossip quorum based on threshold
131+
* @param totalAgents Total number of agents in the system
132+
* @param threshold Percentage threshold (0-1, default 0.51)
133+
* @returns Minimum quorum size for gossip consensus
134+
*/
135+
calculateGossipQuorum: (totalAgents: number, threshold: number = 0.51): number => {
136+
return Math.ceil(totalAgents * threshold);
137+
},
138+
139+
/**
140+
* CRDT doesn't require quorum (eventual consistency)
141+
* @returns Always 1 (any single node can make progress)
142+
*/
143+
calculateCRDTQuorum: (): number => {
144+
return 1;
145+
}
146+
};
147+
95148
export const BYZANTINE_FAULT_TOLERANCE = {
96149
/**
97150
* Calculate the maximum number of faulty agents that can be tolerated
@@ -160,6 +213,28 @@ export const VOTING_TYPES = {
160213
STAKE_WEIGHTED: 'stake-weighted' as const
161214
};
162215

216+
/**
217+
* Factory function to create a Raft consensus system
218+
*/
219+
export function createRaftConsensusSystem(
220+
nodeId: string,
221+
totalNodes: number = 3
222+
): {
223+
consensus: RaftConsensus;
224+
quorumSize: number;
225+
hasQuorum: boolean;
226+
} {
227+
const consensus = new RaftConsensus(nodeId, totalNodes);
228+
const quorumSize = QUORUM_CALCULATIONS.calculateRaftQuorum(totalNodes);
229+
const hasQuorum = consensus.hasQuorum();
230+
231+
return {
232+
consensus,
233+
quorumSize,
234+
hasQuorum
235+
};
236+
}
237+
163238
/**
164239
* Factory function to create a complete Byzantine consensus system
165240
* with all components properly integrated

0 commit comments

Comments
 (0)