-
Notifications
You must be signed in to change notification settings - Fork 178
/
static.go
120 lines (99 loc) · 3.61 KB
/
static.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
110
111
112
113
114
115
116
117
118
119
120
package committees
import (
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
)
// NewStaticCommittee returns a new committee with a static participant set.
func NewStaticCommittee(participants flow.IdentityList, myID flow.Identifier, dkgParticipants map[flow.Identifier]flow.DKGParticipant, dkgGroupKey crypto.PublicKey) (*Static, error) {
return NewStaticCommitteeWithDKG(participants, myID, staticDKG{
dkgParticipants: dkgParticipants,
dkgGroupKey: dkgGroupKey,
})
}
// NewStaticCommitteeWithDKG returns a new committee with a static participant set.
func NewStaticCommitteeWithDKG(participants flow.IdentityList, myID flow.Identifier, dkg protocol.DKG) (*Static, error) {
valid := flow.IsIdentityListCanonical(participants)
if !valid {
return nil, fmt.Errorf("participants %v is not in Canonical order", participants)
}
static := &Static{
participants: participants,
myID: myID,
dkg: dkg,
}
return static, nil
}
// Static represents a committee with a static participant set. It is used for
// bootstrapping purposes.
type Static struct {
participants flow.IdentityList
myID flow.Identifier
dkg protocol.DKG
}
func (s Static) IdentitiesByBlock(_ flow.Identifier) (flow.IdentityList, error) {
return s.participants, nil
}
func (s Static) IdentityByBlock(_ flow.Identifier, participantID flow.Identifier) (*flow.Identity, error) {
identity, ok := s.participants.ByNodeID(participantID)
if !ok {
return nil, model.NewInvalidSignerErrorf("unknown participant %x", participantID)
}
return identity, nil
}
func (s Static) IdentitiesByEpoch(_ uint64) (flow.IdentityList, error) {
return s.participants, nil
}
func (s Static) IdentityByEpoch(_ uint64, participantID flow.Identifier) (*flow.Identity, error) {
identity, ok := s.participants.ByNodeID(participantID)
if !ok {
return nil, model.NewInvalidSignerErrorf("unknown participant %x", participantID)
}
return identity, nil
}
func (s Static) LeaderForView(_ uint64) (flow.Identifier, error) {
return flow.ZeroID, fmt.Errorf("invalid for static committee")
}
func (s Static) QuorumThresholdForView(_ uint64) (uint64, error) {
return WeightThresholdToBuildQC(s.participants.TotalWeight()), nil
}
func (s Static) TimeoutThresholdForView(_ uint64) (uint64, error) {
return WeightThresholdToTimeout(s.participants.TotalWeight()), nil
}
func (s Static) Self() flow.Identifier {
return s.myID
}
func (s Static) DKG(_ uint64) (hotstuff.DKG, error) {
return s.dkg, nil
}
type staticDKG struct {
dkgParticipants map[flow.Identifier]flow.DKGParticipant
dkgGroupKey crypto.PublicKey
}
func (s staticDKG) Size() uint {
return uint(len(s.dkgParticipants))
}
func (s staticDKG) GroupKey() crypto.PublicKey {
return s.dkgGroupKey
}
// Index returns the index for the given node. Error Returns:
// protocol.IdentityNotFoundError if nodeID is not a valid DKG participant.
func (s staticDKG) Index(nodeID flow.Identifier) (uint, error) {
participant, ok := s.dkgParticipants[nodeID]
if !ok {
return 0, protocol.IdentityNotFoundError{NodeID: nodeID}
}
return participant.Index, nil
}
// KeyShare returns the public key share for the given node. Error Returns:
// protocol.IdentityNotFoundError if nodeID is not a valid DKG participant.
func (s staticDKG) KeyShare(nodeID flow.Identifier) (crypto.PublicKey, error) {
participant, ok := s.dkgParticipants[nodeID]
if !ok {
return nil, protocol.IdentityNotFoundError{NodeID: nodeID}
}
return participant.KeyShare, nil
}