/
suffrage.go
71 lines (59 loc) · 1.36 KB
/
suffrage.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
package base
import (
"github.com/spikeekips/mitum/util"
)
type Suffrage interface {
util.Initializer
Name() string
NumberOfActing() uint
Acting(Height, Round) (ActingSuffrage, error)
IsInside(Address) bool
IsActing(Height, Round, Address /* node address */) (bool, error)
IsProposer(Height, Round, Address /* node address */) (bool, error)
Nodes() []Address
Verbose() string
}
type ActingSuffrage struct {
height Height
round Round
proposer Address
nodes map[Address]struct{}
nodeList []Address
}
func NewActingSuffrage(height Height, round Round, proposer Address, selected []Address) ActingSuffrage {
nodes := map[Address]struct{}{}
for _, n := range selected {
nodes[n] = struct{}{}
}
return ActingSuffrage{
height: height,
round: round,
proposer: proposer,
nodes: nodes,
nodeList: selected,
}
}
func (as ActingSuffrage) Height() Height {
return as.height
}
func (as ActingSuffrage) Round() Round {
return as.round
}
func (as ActingSuffrage) Nodes() []Address {
return as.nodeList
}
func (as ActingSuffrage) Exists(node Address) bool {
_, found := as.nodes[node]
return found
}
func (as ActingSuffrage) Proposer() Address {
return as.proposer
}
func NumberOfFaultyNodes(n uint, threshold float64) int {
if n < 1 {
return 0
} else if threshold >= 100 {
return 0
}
return int(float64(n) - float64(n)*(threshold/100))
}