govote/voting/quorum/quorum.go

111 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-05-12 22:47:24 +00:00
package quorum
import (
"fmt"
"strings"
)
2024-05-14 08:26:48 +00:00
// Quorum defines a custom uint8 type for storing a quorum.
2024-05-12 22:47:24 +00:00
type Quorum uint8
2024-05-14 08:26:48 +00:00
// Supported quorums are:
2024-05-12 22:47:24 +00:00
const (
2024-05-13 08:45:38 +00:00
Simple Quorum = iota
OneFifth
OneQuarter
OneThird
OneHalf
TwoFifths
TwoThirds
ThreeQuarters
ThreeFifths
FourFifths
Unanimous
2024-05-12 22:47:24 +00:00
)
2024-05-14 08:26:48 +00:00
// ValidQuorums returns a []Quorum of all supported quorums.
2024-05-12 22:47:24 +00:00
func ValidQuorums() []Quorum {
2024-05-13 08:45:38 +00:00
return []Quorum{
Simple, Unanimous,
2024-05-13 08:45:38 +00:00
OneFifth, OneQuarter, OneThird, OneHalf,
TwoThirds, TwoFifths,
ThreeQuarters, ThreeFifths,
FourFifths,
}
2024-05-12 22:47:24 +00:00
}
2024-05-14 08:26:48 +00:00
// FromString takes a string and returns an initialized Quorum or an error if
// the string could not be parsed.
2024-05-12 22:47:24 +00:00
func FromString(s string) (Quorum, error) {
2024-05-13 08:45:38 +00:00
for _, q := range ValidQuorums() {
if strings.ToUpper(q.String()) == strings.ToUpper(s) {
return q, nil
}
}
return Simple, fmt.Errorf("inalid quorum: %s", s)
2024-05-12 22:47:24 +00:00
}
2024-05-14 08:26:48 +00:00
// String implements fmt.Stringer
2024-05-12 22:47:24 +00:00
func (q Quorum) String() string {
2024-05-13 08:45:38 +00:00
switch q {
case Simple:
return "SIMPLE"
case OneFifth:
return "1/5"
case OneQuarter:
return "1/4"
case OneThird:
return "1/3"
case OneHalf:
return "1/2"
case TwoThirds:
return "2/3"
case TwoFifths:
return "2/5"
case ThreeQuarters:
return "3/4"
case ThreeFifths:
return "3/5"
case FourFifths:
return "4/5"
case Unanimous:
return "ALL"
default:
panic("this code should never be reached")
}
2024-05-12 22:47:24 +00:00
}
2024-05-14 08:26:48 +00:00
// IsSatisfied takes the number of eligible and actual votes and returns a bool
// indicating if the quroum is satisfied or not.
2024-05-12 22:47:24 +00:00
func (q Quorum) IsSatisfied(possibleVotes, totalVotes int) bool {
2024-05-13 08:45:38 +00:00
if totalVotes == 0 {
return false
}
switch q {
case Simple:
return true
case OneFifth:
return totalVotes*5 >= possibleVotes
case OneQuarter:
return totalVotes*4 >= possibleVotes
case OneThird:
return totalVotes*3 >= possibleVotes
case OneHalf:
return totalVotes*2 >= possibleVotes
case TwoThirds:
return totalVotes*3 >= possibleVotes*2
case TwoFifths:
return totalVotes*5 >= possibleVotes*2
case ThreeQuarters:
return totalVotes*4 >= possibleVotes*3
case ThreeFifths:
return totalVotes*5 >= possibleVotes*3
case FourFifths:
return totalVotes*5 >= possibleVotes*4
case Unanimous:
return totalVotes >= possibleVotes
default:
panic("this code should never be reached")
2024-05-13 08:45:38 +00:00
}
2024-05-12 22:47:24 +00:00
}