libs5-go/protocol/protocol.go

29 lines
509 B
Go
Raw Normal View History

2024-01-06 11:33:46 +00:00
package protocol
2024-01-06 15:54:03 +00:00
import (
"crypto/rand"
"math"
)
2024-01-06 11:33:46 +00:00
func GenerateChallenge() []byte {
challenge := make([]byte, 32)
_, err := rand.Read(challenge)
if err != nil {
panic(err)
}
return challenge
}
2024-01-06 15:54:03 +00:00
func CalculateNodeScore(goodResponses, badResponses int) float64 {
totalVotes := goodResponses + badResponses
if totalVotes == 0 {
return 0.5
}
average := float64(goodResponses) / float64(totalVotes)
score := average - (average-0.5)*math.Pow(2, -math.Log(float64(totalVotes+1)))
return score
}