55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package consensus
|
|
|
|
import "log"
|
|
|
|
// CheckQuorum verifica se 2/3 dos validadores (por quantidade) assinaram.
|
|
func CheckQuorum(votes map[string]string, totalValidators int) (bool, string) {
|
|
voteCounts := make(map[string]int)
|
|
for _, hash := range votes {
|
|
voteCounts[hash]++
|
|
}
|
|
quorum := (2 * totalValidators) / 3
|
|
for hash, count := range voteCounts {
|
|
if count > quorum {
|
|
return true, hash
|
|
}
|
|
}
|
|
return false, ""
|
|
}
|
|
|
|
// CheckQuorumWeighted verifica se votos representam 2/3 do stake total considerando peers online.
|
|
func CheckQuorumWeighted(votes map[string]string, valSet *ValidatorSet, liveness *LivenessMonitor) (bool, string) {
|
|
totalStake := uint64(0)
|
|
for _, val := range valSet.Validators {
|
|
if liveness.IsAlive(val.Address) {
|
|
totalStake += val.Stake
|
|
}
|
|
}
|
|
required := (2 * totalStake) / 3
|
|
log.Printf("🔍 Total stake online: %d | Quórum necessário: %d\n", totalStake, required)
|
|
|
|
weighted := make(map[string]uint64)
|
|
for validator, hash := range votes {
|
|
val, ok := valSet.ValidatorByAddress(validator)
|
|
if !ok {
|
|
log.Printf("⚠️ Validador %s não encontrado no conjunto\n", validator)
|
|
continue
|
|
}
|
|
if !liveness.IsAlive(val.Address) {
|
|
log.Printf("⚠️ Validador %s está OFFLINE\n", val.Address)
|
|
continue
|
|
}
|
|
log.Printf("✅ Voto de %s para hash %s com %d tokens\n", val.Address, hash, val.Stake)
|
|
weighted[hash] += val.Stake
|
|
}
|
|
|
|
for hash, sum := range weighted {
|
|
log.Printf("📊 Hash %s recebeu %d tokens\n", hash, sum)
|
|
if sum > required {
|
|
log.Printf("🎯 Quórum atingido para hash %s\n", hash)
|
|
return true, hash
|
|
}
|
|
}
|
|
log.Println("❌ Quórum NÃO atingido")
|
|
return false, ""
|
|
} |