summaryrefslogtreecommitdiff
path: root/internal/vote/vote.go
blob: 74078486603d13b0d2554fc0dcaf1267fecb0823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package vote

import (
	"log"
	"strings"
	"time"

	"codeberg.org/snonux/gorum/internal/config"
)

const Expiry = 20 * time.Second

type Vote struct {
	FromID    string
	IDs       []string
	ExpiresAt time.Time
}

func New(conf config.Config, message string) Vote {
	var (
		fromID string
		ids    []string
	)

	for _, id := range strings.Split(strings.TrimSpace(message), " ") {
		if !conf.IsNode(id) {
			log.Println("vote: is not a node, excluding from the vote", id)
			continue
		}
		if fromID == "" {
			fromID = id
			continue
		}
		ids = append(ids, id)
	}

	return Vote{fromID, ids, time.Now().Add(Expiry)}
}

func (v Vote) Expired() bool {
	now := time.Now()
	return now.After(v.ExpiresAt) || now.Equal(v.ExpiresAt)
}