-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
199 lines (151 loc) · 5.03 KB
/
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
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
package messages
import (
"bytes"
"github.com/Woop-Chain/go-ibft/messages/proto"
)
type CommittedSeal struct {
Signer []byte
Signature []byte
}
// ExtractCommittedSeals extracts the committed seals from the passed in messages
func ExtractCommittedSeals(commitMessages []*proto.Message) []*CommittedSeal {
committedSeals := make([]*CommittedSeal, 0)
for _, commitMessage := range commitMessages {
if commitMessage.Type != proto.MessageType_COMMIT {
continue
}
committedSeals = append(committedSeals, ExtractCommittedSeal(commitMessage))
}
return committedSeals
}
// ExtractCommittedSeal extracts the committed seal from the passed in message
func ExtractCommittedSeal(commitMessage *proto.Message) *CommittedSeal {
commitData, _ := commitMessage.Payload.(*proto.Message_CommitData)
return &CommittedSeal{
Signer: commitMessage.From,
Signature: commitData.CommitData.CommittedSeal,
}
}
// ExtractCommitHash extracts the commit proposal hash from the passed in message
func ExtractCommitHash(commitMessage *proto.Message) []byte {
if commitMessage.Type != proto.MessageType_COMMIT {
return nil
}
commitData, _ := commitMessage.Payload.(*proto.Message_CommitData)
return commitData.CommitData.ProposalHash
}
// ExtractProposal extracts the proposal from the passed in message
func ExtractProposal(proposalMessage *proto.Message) []byte {
if proposalMessage.Type != proto.MessageType_PREPREPARE {
return nil
}
preprepareData, _ := proposalMessage.Payload.(*proto.Message_PreprepareData)
return preprepareData.PreprepareData.Proposal
}
// ExtractProposalHash extracts the proposal hash from the passed in message
func ExtractProposalHash(proposalMessage *proto.Message) []byte {
if proposalMessage.Type != proto.MessageType_PREPREPARE {
return nil
}
preprepareData, _ := proposalMessage.Payload.(*proto.Message_PreprepareData)
return preprepareData.PreprepareData.ProposalHash
}
// ExtractRoundChangeCertificate extracts the RCC from the passed in message
func ExtractRoundChangeCertificate(proposalMessage *proto.Message) *proto.RoundChangeCertificate {
if proposalMessage.Type != proto.MessageType_PREPREPARE {
return nil
}
preprepareData, _ := proposalMessage.Payload.(*proto.Message_PreprepareData)
return preprepareData.PreprepareData.Certificate
}
// ExtractPrepareHash extracts the prepare proposal hash from the passed in message
func ExtractPrepareHash(prepareMessage *proto.Message) []byte {
if prepareMessage.Type != proto.MessageType_PREPARE {
return nil
}
prepareData, _ := prepareMessage.Payload.(*proto.Message_PrepareData)
return prepareData.PrepareData.ProposalHash
}
// ExtractLatestPC extracts the latest PC from the passed in message
func ExtractLatestPC(roundChangeMessage *proto.Message) *proto.PreparedCertificate {
if roundChangeMessage.Type != proto.MessageType_ROUND_CHANGE {
return nil
}
rcData, _ := roundChangeMessage.Payload.(*proto.Message_RoundChangeData)
return rcData.RoundChangeData.LatestPreparedCertificate
}
// ExtractLastPreparedProposedBlock extracts the latest prepared proposed block from the passed in message
func ExtractLastPreparedProposedBlock(roundChangeMessage *proto.Message) []byte {
if roundChangeMessage.Type != proto.MessageType_ROUND_CHANGE {
return nil
}
rcData, _ := roundChangeMessage.Payload.(*proto.Message_RoundChangeData)
return rcData.RoundChangeData.LastPreparedProposedBlock
}
// HasUniqueSenders checks if the messages have unique senders
func HasUniqueSenders(messages []*proto.Message) bool {
if len(messages) < 1 {
return false
}
senderMap := make(map[string]struct{})
for _, message := range messages {
key := string(message.From)
if _, exists := senderMap[key]; exists {
return false
}
senderMap[key] = struct{}{}
}
return true
}
// HaveSameProposalHash checks if the messages have the same proposal hash
func HaveSameProposalHash(messages []*proto.Message) bool {
if len(messages) < 1 {
return false
}
var hash []byte = nil
for _, message := range messages {
var extractedHash []byte
switch message.Type {
case proto.MessageType_PREPREPARE:
extractedHash = ExtractProposalHash(message)
case proto.MessageType_PREPARE:
extractedHash = ExtractPrepareHash(message)
default:
return false
}
if hash == nil {
// No previous hash for comparison,
// set the first one as the reference, as
// all of them need to be the same anyway
hash = extractedHash
}
if !bytes.Equal(hash, extractedHash) {
return false
}
}
return true
}
// AllHaveLowerRound checks if all messages have the same round
func AllHaveLowerRound(messages []*proto.Message, round uint64) bool {
if len(messages) < 1 {
return false
}
for _, message := range messages {
if message.View.Round >= round {
return false
}
}
return true
}
// AllHaveSameHeight checks if all messages have the same height
func AllHaveSameHeight(messages []*proto.Message, height uint64) bool {
if len(messages) < 1 {
return false
}
for _, message := range messages {
if message.View.Height != height {
return false
}
}
return true
}