-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhelpers.go
More file actions
236 lines (197 loc) · 7.14 KB
/
Copy pathhelpers.go
File metadata and controls
236 lines (197 loc) · 7.14 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package consensus
// TODO: Split this file into multiple helpers (e.g. signatures.go, hotstuff_helpers.go, etc...)
import (
"encoding/base64"
"log"
typesCons "github.com/pokt-network/pocket/consensus/types"
"github.com/pokt-network/pocket/shared/codec"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"google.golang.org/protobuf/proto"
)
// These constants and variables are wrappers around the autogenerated protobuf types and were
// added to simply make the code in the `consensus` module more readable.
const (
NewRound = typesCons.HotstuffStep_HOTSTUFF_STEP_NEWROUND
Prepare = typesCons.HotstuffStep_HOTSTUFF_STEP_PREPARE
PreCommit = typesCons.HotstuffStep_HOTSTUFF_STEP_PRECOMMIT
Commit = typesCons.HotstuffStep_HOTSTUFF_STEP_COMMIT
Decide = typesCons.HotstuffStep_HOTSTUFF_STEP_DECIDE
Propose = typesCons.HotstuffMessageType_HOTSTUFF_MESSAGE_PROPOSE
Vote = typesCons.HotstuffMessageType_HOTSTUFF_MESSAGE_VOTE
ByzantineThreshold = float64(2) / float64(3)
HotstuffMessageContentType = "consensus.HotstuffMessage"
UtilityMessageContentType = "consensus.UtilityMessage"
)
var (
HotstuffSteps = [...]typesCons.HotstuffStep{NewRound, Prepare, PreCommit, Commit, Decide}
)
// ** Hotstuff Helpers ** //
// IMPROVE: Avoid having the `ConsensusModule` be a receiver of this; making it more functional.
// TODO: Add unit tests for all quorumCert creation & validation logic...
func (m *consensusModule) getQuorumCertificate(height uint64, step typesCons.HotstuffStep, round uint64) (*typesCons.QuorumCertificate, error) {
var pss []*typesCons.PartialSignature
for _, msg := range m.messagePool[step] {
if msg.GetPartialSignature() == nil {
m.nodeLog(typesCons.WarnMissingPartialSig(msg))
continue
}
if msg.GetHeight() != height || msg.GetStep() != step || msg.GetRound() != round {
m.nodeLog(typesCons.WarnUnexpectedMessageInPool(msg, height, step, round))
continue
}
ps := msg.GetPartialSignature()
if ps.Signature == nil || len(ps.Address) == 0 {
m.nodeLog(typesCons.WarnIncompletePartialSig(ps, msg))
continue
}
pss = append(pss, msg.GetPartialSignature())
}
if err := m.isOptimisticThresholdMet(len(pss)); err != nil {
return nil, err
}
thresholdSig, err := getThresholdSignature(pss)
if err != nil {
return nil, err
}
return &typesCons.QuorumCertificate{
Height: height,
Step: step,
Round: round,
Block: m.Block,
ThresholdSignature: thresholdSig,
}, nil
}
func (m *consensusModule) findHighQC(msgs []*typesCons.HotstuffMessage) (qc *typesCons.QuorumCertificate) {
for _, m := range msgs {
if m.GetQuorumCertificate() == nil {
continue
}
// TODO: Make sure to validate the "highest QC" first and add tests
if qc == nil || m.GetQuorumCertificate().Height > qc.Height {
qc = m.GetQuorumCertificate()
}
}
return
}
func getThresholdSignature(partialSigs []*typesCons.PartialSignature) (*typesCons.ThresholdSignature, error) {
thresholdSig := new(typesCons.ThresholdSignature)
thresholdSig.Signatures = make([]*typesCons.PartialSignature, len(partialSigs))
copy(thresholdSig.Signatures, partialSigs)
return thresholdSig, nil
}
func isSignatureValid(msg *typesCons.HotstuffMessage, pubKeyString string, signature []byte) bool {
pubKey, err := cryptoPocket.NewPublicKey(pubKeyString)
if err != nil {
log.Println("[WARN] Error getting PublicKey from bytes:", err)
return false
}
bytesToVerify, err := getSignableBytes(msg)
if err != nil {
log.Println("[WARN] Error getting bytes to verify:", err)
return false
}
return pubKey.Verify(bytesToVerify, signature)
}
func (m *consensusModule) didReceiveEnoughMessageForStep(step typesCons.HotstuffStep) error {
return m.isOptimisticThresholdMet(len(m.messagePool[step]))
}
func (m *consensusModule) isOptimisticThresholdMet(n int) error {
numValidators := len(m.validatorMap)
if !(float64(n) > ByzantineThreshold*float64(numValidators)) {
return typesCons.ErrByzantineThresholdCheck(n, ByzantineThreshold*float64(numValidators))
}
return nil
}
func (m *consensusModule) resetForNewHeight() {
m.Round = 0
m.Block = nil
m.highPrepareQC = nil
m.lockedQC = nil
}
func protoHash(m proto.Message) string {
b, err := codec.GetCodec().Marshal(m)
if err != nil {
log.Fatalf("Could not marshal proto message: %v", err)
}
return base64.StdEncoding.EncodeToString(b)
}
/*** P2P Helpers ***/
func (m *consensusModule) sendToNode(msg *typesCons.HotstuffMessage) {
// TODO(olshansky): This can happen due to a race condition with the pacemaker.
if m.LeaderId == nil {
m.nodeLogError(typesCons.ErrNilLeaderId.Error(), nil)
return
}
m.nodeLog(typesCons.SendingMessage(msg, *m.LeaderId))
anyConsensusMessage, err := codec.GetCodec().ToAny(msg)
if err != nil {
m.nodeLogError(typesCons.ErrCreateConsensusMessage.Error(), err)
return
}
if err := m.GetBus().GetP2PModule().Send(cryptoPocket.AddressFromString(m.idToValAddrMap[*m.LeaderId]), anyConsensusMessage); err != nil {
m.nodeLogError(typesCons.ErrSendMessage.Error(), err)
return
}
}
func (m *consensusModule) broadcastToNodes(msg *typesCons.HotstuffMessage) {
m.nodeLog(typesCons.BroadcastingMessage(msg))
anyConsensusMessage, err := codec.GetCodec().ToAny(msg)
if err != nil {
m.nodeLogError(typesCons.ErrCreateConsensusMessage.Error(), err)
return
}
if err := m.GetBus().GetP2PModule().Broadcast(anyConsensusMessage); err != nil {
m.nodeLogError(typesCons.ErrBroadcastMessage.Error(), err)
return
}
}
/*** Persistence Helpers ***/
// TECHDEBT: Integrate this with the `persistence` module or a real mempool.
func (m *consensusModule) clearMessagesPool() {
for _, step := range HotstuffSteps {
m.messagePool[step] = make([]*typesCons.HotstuffMessage, 0)
}
}
/*** Leader Election Helpers ***/
func (m *consensusModule) isLeaderUnknown() bool {
return m.LeaderId == nil
}
func (m *consensusModule) isLeader() bool {
return m.LeaderId != nil && *m.LeaderId == m.nodeId
}
func (m *consensusModule) isReplica() bool {
return !m.isLeader()
}
func (m *consensusModule) clearLeader() {
m.logPrefix = DefaultLogPrefix
m.LeaderId = nil
}
func (m *consensusModule) electNextLeader(message *typesCons.HotstuffMessage) error {
leaderId, err := m.leaderElectionMod.ElectNextLeader(message)
if err != nil || leaderId == 0 {
m.nodeLogError(typesCons.ErrLeaderElection(message).Error(), err)
m.clearLeader()
return err
}
m.LeaderId = &leaderId
if m.isLeader() {
m.setLogPrefix("LEADER")
m.nodeLog(typesCons.ElectedSelfAsNewLeader(m.idToValAddrMap[*m.LeaderId], *m.LeaderId, m.Height, m.Round))
} else {
m.setLogPrefix("REPLICA")
m.nodeLog(typesCons.ElectedNewLeader(m.idToValAddrMap[*m.LeaderId], *m.LeaderId, m.Height, m.Round))
}
return nil
}
/*** General Infrastructure Helpers ***/
// TODO(#164): Remove this once we have a proper logging system.
func (m *consensusModule) nodeLog(s string) {
log.Printf("[%s][%d] %s\n", m.logPrefix, m.nodeId, s)
}
// TODO(#164): Remove this once we have a proper logging system.
func (m *consensusModule) nodeLogError(s string, err error) {
log.Printf("[ERROR][%s][%d] %s: %v\n", m.logPrefix, m.nodeId, s, err)
}
func (m *consensusModule) setLogPrefix(logPrefix string) {
m.logPrefix = logPrefix
}