vspd/cmd/vote-validator/results.go
Jamie Holdstock 8bb868a5a5
Add vote-validator tool. (#335)
vote-validator is a tool for VSP admins to verify that their vspd deployment
is voting correctly according to user preferences.
2022-11-18 15:05:38 -05:00

80 lines
1.6 KiB
Go

// 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
}