-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathresults.go
79 lines (67 loc) · 1.55 KB
/
results.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
)
type results struct {
badVotes []*votedTicket
noPreferences []*votedTicket
wrongVersion []*votedTicket
}
func (r *results) writeFile(path string) (bool, error) {
if len(r.badVotes) == 0 &&
len(r.noPreferences) == 0 &&
len(r.wrongVersion) == 0 {
return false, nil
}
// Open a log file.
f, err := os.Create(path)
if err != nil {
return false, fmt.Errorf("opening log file failed: %w", err)
}
write := func(f *os.File, str string, a ...any) {
_, err := f.WriteString(fmt.Sprintf(str+"\n", a...))
if err != nil {
f.Close()
panic(fmt.Sprintf("writing to log file failed: %v", err))
}
}
if len(r.badVotes) > 0 {
write(f, "Tickets with bad votes:")
for _, t := range r.badVotes {
write(f,
"Hash: %s VoteHeight: %d ExpectedVote: %v ActualVote: %v",
t.ticket.Hash, t.voteHeight, t.ticket.VoteChoices, t.vote,
)
}
write(f, "\n")
}
if len(r.wrongVersion) > 0 {
write(f, "Tickets with the wrong vote version:")
for _, t := range r.wrongVersion {
write(f,
"Hash: %s",
t.ticket.Hash,
)
}
write(f, "\n")
}
if len(r.noPreferences) > 0 {
write(f, "Tickets with no user set vote preferences:")
for _, t := range r.noPreferences {
write(f,
"Hash: %s",
t.ticket.Hash,
)
}
write(f, "\n")
}
err = f.Close()
if err != nil {
return false, fmt.Errorf("closing log file failed: %w", err)
}
return true, nil
}