-
Notifications
You must be signed in to change notification settings - Fork 14
/
test_helpers.go
89 lines (71 loc) · 1.8 KB
/
test_helpers.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
package pbft
type ValidatorKeyMock string
func (k ValidatorKeyMock) NodeID() NodeID {
return NodeID(k)
}
func (k ValidatorKeyMock) Sign(b []byte) ([]byte, error) {
return b, nil
}
type TransportStub struct {
Nodes []*Pbft
GossipFunc func(ft *TransportStub, msg *MessageReq) error
}
func (ft *TransportStub) Gossip(msg *MessageReq) error {
if ft.GossipFunc != nil {
return ft.GossipFunc(ft, msg)
}
for _, node := range ft.Nodes {
if msg.From != node.GetValidatorId() {
node.PushMessage(msg.Copy())
}
}
return nil
}
func NewValStringStub(nodes []NodeID, votingPowerMap map[NodeID]uint64) *ValStringStub {
return &ValStringStub{
Nodes: nodes,
VotingPowerMap: votingPowerMap,
}
}
type ValStringStub struct {
Nodes []NodeID
VotingPowerMap map[NodeID]uint64
}
func (v *ValStringStub) CalcProposer(round uint64) NodeID {
seed := uint64(0)
offset := 0
// add last proposer
seed = uint64(offset) + round
pick := seed % uint64(v.Len())
return (v.Nodes)[pick]
}
func (v *ValStringStub) Index(id NodeID) int {
for i, currentId := range v.Nodes {
if currentId == id {
return i
}
}
return -1
}
func (v *ValStringStub) Includes(id NodeID) bool {
for _, currentId := range v.Nodes {
if currentId == id {
return true
}
}
return false
}
func (v *ValStringStub) Len() int {
return len(v.Nodes)
}
func (v *ValStringStub) VotingPower() map[NodeID]uint64 {
return v.VotingPowerMap
}
// CreateEqualVotingPowerMap is a helper function which creates map with same weight for every validator id in the provided slice
func CreateEqualVotingPowerMap(validatorIds []NodeID) map[NodeID]uint64 {
weightedValidators := make(map[NodeID]uint64, len(validatorIds))
for _, validatorId := range validatorIds {
weightedValidators[validatorId] = 1
}
return weightedValidators
}