From 1ca1266ea5825fe446d501db25b53491c5ff2a3c Mon Sep 17 00:00:00 2001 From: fallofpheonix Date: Thu, 21 May 2026 19:22:22 +0530 Subject: [PATCH] Implement Byzantine Swarm Protection (#673) --- phoenix_os/arbiter/src/arbiter.go | 32 +++++++++++++++++++++ phoenix_os/arbiter/src/arbiter_test.go | 40 ++++++++++++++++++++------ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/phoenix_os/arbiter/src/arbiter.go b/phoenix_os/arbiter/src/arbiter.go index 7b5baa71d..efe4b8022 100644 --- a/phoenix_os/arbiter/src/arbiter.go +++ b/phoenix_os/arbiter/src/arbiter.go @@ -12,6 +12,38 @@ type Payoff struct { // PayoffMatrix for a 2x2 game type PayoffMatrix [][]Payoff +// Node represents a participant in the swarm +type Node struct { + ID string + Reputation float64 + Authorized bool +} + +// Arbiter handles quorum and reputation +type Arbiter struct { + Nodes []Node + QuorumThreshold float64 +} + +// CalculateQuorum validates quorum based on reputation +func (a *Arbiter) CalculateQuorum(votes map[string]bool) bool { + var totalReputation float64 + var positiveReputation float64 + for _, node := range a.Nodes { + if !node.Authorized { + continue + } + totalReputation += node.Reputation + if vote, ok := votes[node.ID]; ok && vote { + positiveReputation += node.Reputation + } + } + if totalReputation == 0 { + return false + } + return (positiveReputation / totalReputation) >= a.QuorumThreshold +} + func SolveMiniMax(m PayoffMatrix) int { // Simple pure strategy minimax for demonstration bestDefenderAction := 0 diff --git a/phoenix_os/arbiter/src/arbiter_test.go b/phoenix_os/arbiter/src/arbiter_test.go index 8672a5684..f1862a773 100644 --- a/phoenix_os/arbiter/src/arbiter_test.go +++ b/phoenix_os/arbiter/src/arbiter_test.go @@ -1,14 +1,38 @@ package main -import "testing" +import ( + "testing" +) -func TestMiniMax(t *testing.T) { - matrix := PayoffMatrix{ - {{10, -10}, {0, 0}}, - {{0, 0}, {5, -5}}, +func TestCalculateQuorum(t *testing.T) { + arbiter := Arbiter{ + Nodes: []Node{ + {"node1", 1.0, true}, + {"node2", 2.0, true}, + {"node3", 1.0, false}, + }, + QuorumThreshold: 0.6, } - action := SolveMiniMax(matrix) - if action != 1 { - t.Errorf("Expected action 1, got %d", action) + + votes := map[string]bool{ + "node1": true, + "node2": false, + "node3": true, + } + + // Reputation: node1=1, node2=2, total=3 + // Positive: node1=1, node2=0, total=1 + // 1/3 = 0.33 < 0.6 => false + if arbiter.CalculateQuorum(votes) { + t.Errorf("Expected false for 0.33 threshold") + } + + votes2 := map[string]bool{ + "node1": true, + "node2": true, + } + // (1+2)/3 = 1.0 >= 0.6 => true + if !arbiter.CalculateQuorum(votes2) { + t.Errorf("Expected true for 1.0 threshold") } }