-
Notifications
You must be signed in to change notification settings - Fork 84
/
proposal.go
270 lines (236 loc) · 8.52 KB
/
proposal.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package instance
import (
"bytes"
specqbft "github.com/bloxapp/ssv-spec/qbft"
spectypes "github.com/bloxapp/ssv-spec/types"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/bloxapp/ssv/logging/fields"
"github.com/bloxapp/ssv/protocol/v2/qbft"
"github.com/bloxapp/ssv/protocol/v2/types"
)
// uponProposal process proposal message
// Assumes proposal message is valid!
func (i *Instance) uponProposal(logger *zap.Logger, signedProposal *specqbft.SignedMessage, proposeMsgContainer *specqbft.MsgContainer) error {
addedMsg, err := proposeMsgContainer.AddFirstMsgForSignerAndRound(signedProposal)
if err != nil {
return errors.Wrap(err, "could not add proposal msg to container")
}
if !addedMsg {
return nil // uponProposal was already called
}
logger.Debug("📬 got proposal message",
fields.Round(i.State.Round),
zap.Any("proposal-signers", signedProposal.Signers))
newRound := signedProposal.Message.Round
i.State.ProposalAcceptedForCurrentRound = signedProposal
// A future justified proposal should bump us into future round and reset timer
if signedProposal.Message.Round > i.State.Round {
i.config.GetTimer().TimeoutForRound(signedProposal.Message.Round)
}
i.bumpToRound(newRound)
i.metrics.EndStageProposal()
// value root
r, err := specqbft.HashDataRoot(signedProposal.FullData)
if err != nil {
return errors.Wrap(err, "could not hash input data")
}
prepare, err := CreatePrepare(i.State, i.config, newRound, r)
if err != nil {
return errors.Wrap(err, "could not create prepare msg")
}
logger.Debug("📢 got proposal, broadcasting prepare message",
fields.Round(i.State.Round),
zap.Any("proposal-signers", signedProposal.Signers),
zap.Any("prepare-signers", prepare.Signers))
if err := i.Broadcast(logger, prepare); err != nil {
return errors.Wrap(err, "failed to broadcast prepare message")
}
return nil
}
func isValidProposal(
state *specqbft.State,
config qbft.IConfig,
signedProposal *specqbft.SignedMessage,
valCheck specqbft.ProposedValueCheckF,
operators []*spectypes.Operator,
) error {
if signedProposal.Message.MsgType != specqbft.ProposalMsgType {
return errors.New("msg type is not proposal")
}
if signedProposal.Message.Height != state.Height {
return errors.New("wrong msg height")
}
if len(signedProposal.GetSigners()) != 1 {
return errors.New("msg allows 1 signer")
}
if err := types.VerifyByOperators(signedProposal.Signature, signedProposal, config.GetSignatureDomainType(), spectypes.QBFTSignatureType, operators); err != nil {
return errors.Wrap(err, "msg signature invalid")
}
if !signedProposal.MatchedSigners([]spectypes.OperatorID{proposer(state, config, signedProposal.Message.Round)}) {
return errors.New("proposal leader invalid")
}
if err := signedProposal.Validate(); err != nil {
return errors.Wrap(err, "proposal invalid")
}
// verify full data integrity
r, err := specqbft.HashDataRoot(signedProposal.FullData)
if err != nil {
return errors.Wrap(err, "could not hash input data")
}
if !bytes.Equal(signedProposal.Message.Root[:], r[:]) {
return errors.New("H(data) != root")
}
// get justifications
roundChangeJustification, _ := signedProposal.Message.GetRoundChangeJustifications() // no need to check error, checked on signedProposal.Validate()
prepareJustification, _ := signedProposal.Message.GetPrepareJustifications() // no need to check error, checked on signedProposal.Validate()
if err := isProposalJustification(
state,
config,
roundChangeJustification,
prepareJustification,
state.Height,
signedProposal.Message.Round,
signedProposal.FullData,
valCheck,
); err != nil {
return errors.Wrap(err, "proposal not justified")
}
if (state.ProposalAcceptedForCurrentRound == nil && signedProposal.Message.Round == state.Round) ||
signedProposal.Message.Round > state.Round {
return nil
}
return errors.New("proposal is not valid with current state")
}
// isProposalJustification returns nil if the proposal and round change messages are valid and justify a proposal message for the provided round, value and leader
func isProposalJustification(
state *specqbft.State,
config qbft.IConfig,
roundChangeMsgs []*specqbft.SignedMessage,
prepareMsgs []*specqbft.SignedMessage,
height specqbft.Height,
round specqbft.Round,
fullData []byte,
valCheck specqbft.ProposedValueCheckF,
) error {
if err := valCheck(fullData); err != nil {
return errors.Wrap(err, "proposal fullData invalid")
}
if round == specqbft.FirstRound {
return nil
} else {
// check all round changes are valid for height and round
// no quorum, duplicate signers, invalid still has quorum, invalid no quorum
// prepared
for _, rc := range roundChangeMsgs {
if err := validRoundChangeForData(state, config, rc, height, round, fullData); err != nil {
return errors.Wrap(err, "change round msg not valid")
}
}
// check there is a quorum
if !specqbft.HasQuorum(state.Share, roundChangeMsgs) {
return errors.New("change round has no quorum")
}
// previouslyPreparedF returns true if any on the round change messages have a prepared round and fullData
previouslyPrepared, err := func(rcMsgs []*specqbft.SignedMessage) (bool, error) {
for _, rc := range rcMsgs {
if rc.Message.RoundChangePrepared() {
return true, nil
}
}
return false, nil
}(roundChangeMsgs)
if err != nil {
return errors.Wrap(err, "could not calculate if previously prepared")
}
if !previouslyPrepared {
return nil
} else {
// check prepare quorum
if !specqbft.HasQuorum(state.Share, prepareMsgs) {
return errors.New("prepares has no quorum")
}
// get a round change data for which there is a justification for the highest previously prepared round
rcm, err := highestPrepared(roundChangeMsgs)
if err != nil {
return errors.Wrap(err, "could not get highest prepared")
}
if rcm == nil {
return errors.New("no highest prepared")
}
// proposed fullData must equal highest prepared fullData
r, err := specqbft.HashDataRoot(fullData)
if err != nil {
return errors.Wrap(err, "could not hash input data")
}
if !bytes.Equal(r[:], rcm.Message.Root[:]) {
return errors.New("proposed data doesn't match highest prepared")
}
// validate each prepare message against the highest previously prepared fullData and round
for _, pm := range prepareMsgs {
if err := validSignedPrepareForHeightRoundAndRoot(
config,
pm,
height,
rcm.Message.DataRound,
rcm.Message.Root,
state.Share.Committee,
); err != nil {
return errors.New("signed prepare not valid")
}
}
return nil
}
}
}
func proposer(state *specqbft.State, config qbft.IConfig, round specqbft.Round) spectypes.OperatorID {
// TODO - https://github.com/ConsenSys/qbft-formal-spec-and-verification/blob/29ae5a44551466453a84d4d17b9e083ecf189d97/dafny/spec/L1/node_auxiliary_functions.dfy#L304-L323
return config.GetProposerF()(state, round)
}
// CreateProposal
/**
Proposal(
signProposal(
UnsignedProposal(
|current.blockchain|,
newRound,
digest(block)),
current.id),
block,
extractSignedRoundChanges(roundChanges),
extractSignedPrepares(prepares));
*/
func CreateProposal(state *specqbft.State, config qbft.IConfig, fullData []byte, roundChanges, prepares []*specqbft.SignedMessage) (*specqbft.SignedMessage, error) {
r, err := specqbft.HashDataRoot(fullData)
if err != nil {
return nil, errors.Wrap(err, "could not hash input data")
}
roundChangesData, err := specqbft.MarshalJustifications(roundChanges)
if err != nil {
return nil, errors.Wrap(err, "could not marshal justifications")
}
preparesData, err := specqbft.MarshalJustifications(prepares)
if err != nil {
return nil, errors.Wrap(err, "could not marshal justifications")
}
msg := &specqbft.Message{
MsgType: specqbft.ProposalMsgType,
Height: state.Height,
Round: state.Round,
Identifier: state.ID,
Root: r,
RoundChangeJustification: roundChangesData,
PrepareJustification: preparesData,
}
sig, err := config.GetSigner().SignRoot(msg, spectypes.QBFTSignatureType, state.Share.SharePubKey)
if err != nil {
return nil, errors.Wrap(err, "failed signing prepare msg")
}
signedMsg := &specqbft.SignedMessage{
Signature: sig,
Signers: []spectypes.OperatorID{state.Share.OperatorID},
Message: *msg,
FullData: fullData,
}
return signedMsg, nil
}