-
Notifications
You must be signed in to change notification settings - Fork 92
/
authsethelper.go
109 lines (89 loc) · 2.13 KB
/
authsethelper.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package testhelper
import (
. "github.com/FactomProject/factomd/electionsCore/messages"
. "github.com/FactomProject/factomd/electionsCore/primitives"
)
// AuthSetHelper will help testing authority sets. Add whatever functions you need to help you
// manage the authsets in tests
type AuthSetHelper struct {
feds int
auds int
AuthSet
sequenceCounter int
}
func NewAuthSetHelper(feds, auds int) *AuthSetHelper {
a := new(AuthSetHelper)
a.AuthSet = *NewAuthSet()
for i := 0; i < feds; i++ {
a.AddFed()
}
for i := 0; i < auds; i++ {
a.AddAudit()
}
return a
}
func (a *AuthSetHelper) NextIdentity() Identity {
a.sequenceCounter++
return NewIdentityFromInt(a.sequenceCounter)
}
func (a *AuthSetHelper) AddFed() Identity {
id := a.NextIdentity()
a.Add(id, 1)
return id
}
func (a *AuthSetHelper) AddAudit() Identity {
id := a.NextIdentity()
a.Add(id, 0)
return id
}
func (a *AuthSetHelper) GetAuthSet() AuthSet {
return a.AuthSet
}
func (a *AuthSetHelper) NewVoteFactory(vol VolunteerMessage) *VoteFactory {
return NewVoteFactory(a.GetFeds(), vol)
}
func (a *AuthSetHelper) Majority() int {
return (len(a.GetFeds()) / 2) + 1
}
type VoteFactory struct {
index int
FedList []Identity
Volunteer VolunteerMessage
}
func NewVoteFactory(feds []Identity, vol VolunteerMessage) *VoteFactory {
vf := new(VoteFactory)
vf.FedList = feds
vf.Volunteer = vol
return vf
}
func (v *VoteFactory) NextVote() VoteMessage {
vote := NewVoteMessage(v.Volunteer, v.NextFed())
v.index = v.index % len(v.FedList)
return vote
}
func (v *VoteFactory) VotesMapWithMajority() map[Identity]VoteMessage {
votes := make(map[Identity]VoteMessage)
for i := 0; i < v.Majority(); i++ {
vote := v.NextVote()
votes[vote.Signer] = vote
}
return votes
}
func (v *VoteFactory) VotesListWithMajority() []VoteMessage {
// List to map
var votes []VoteMessage
m := v.VotesMapWithMajority()
for _, v := range m {
votes = append(votes, v)
}
return votes
}
func (v *VoteFactory) NextFed() Identity {
f := v.FedList[v.index]
v.index++
v.index = v.index % len(v.FedList)
return f
}
func (v *VoteFactory) Majority() int {
return (len(v.FedList) / 2) + 1
}