Search Apps Documentation Source Content File Folder Download Copy Actions Download

vote_set.gno

1.77 Kb ยท 104 lines
  1package groups
  2
  3import (
  4	"time"
  5
  6	"gno.land/p/archive/rat"
  7)
  8
  9//----------------------------------------
 10// VoteSet
 11
 12type VoteSet interface {
 13	// number of present votes in set.
 14	Size() int
 15	// add or update vote for voter.
 16	SetVote(voter address, value string) error
 17	// count the number of votes for value.
 18	CountVotes(value string) int
 19}
 20
 21//----------------------------------------
 22// VoteList
 23
 24type Vote struct {
 25	Voter address
 26	Value string
 27}
 28
 29type VoteList []Vote
 30
 31func NewVoteList() *VoteList {
 32	return &VoteList{}
 33}
 34
 35func (vlist *VoteList) Size() int {
 36	return len(*vlist)
 37}
 38
 39func (vlist *VoteList) SetVote(voter address, value string) error {
 40	// TODO optimize with binary algorithm
 41	for i, vote := range *vlist {
 42		if vote.Voter == voter {
 43			// update vote
 44			(*vlist)[i] = Vote{
 45				Voter: voter,
 46				Value: value,
 47			}
 48			return nil
 49		}
 50	}
 51	*vlist = append(*vlist, Vote{
 52		Voter: voter,
 53		Value: value,
 54	})
 55	return nil
 56}
 57
 58func (vlist *VoteList) CountVotes(target string) int {
 59	// TODO optimize with binary algorithm
 60	var count int
 61	for _, vote := range *vlist {
 62		if vote.Value == target {
 63			count++
 64		}
 65	}
 66	return count
 67}
 68
 69//----------------------------------------
 70// Committee
 71
 72type Committee struct {
 73	Quorum    rat.Rat
 74	Threshold rat.Rat
 75	Addresses []address
 76}
 77
 78//----------------------------------------
 79// VoteSession
 80// NOTE: this seems a bit too formal and
 81// complicated vs what might be possible;
 82// something simpler, more informal.
 83
 84type SessionStatus int
 85
 86const (
 87	SessionNew SessionStatus = iota
 88	SessionStarted
 89	SessionCompleted
 90	SessionCanceled
 91)
 92
 93type VoteSession struct {
 94	Name      string
 95	Creator   address
 96	Body      string
 97	Start     time.Time
 98	Deadline  time.Time
 99	Status    SessionStatus
100	Committee *Committee
101	Votes     VoteSet
102	Choices   []string
103	Result    string
104}