-
Notifications
You must be signed in to change notification settings - Fork 84
/
commit.go
191 lines (163 loc) · 5.85 KB
/
commit.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
package instance
import (
"bytes"
"sort"
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"
)
// UponCommit returns true if a quorum of commit messages was received.
// Assumes commit message is valid!
func (i *Instance) UponCommit(logger *zap.Logger, signedCommit *specqbft.SignedMessage, commitMsgContainer *specqbft.MsgContainer) (bool, []byte, *specqbft.SignedMessage, error) {
addMsg, err := commitMsgContainer.AddFirstMsgForSignerAndRound(signedCommit)
if err != nil {
return false, nil, nil, errors.Wrap(err, "could not add commit msg to container")
}
if !addMsg {
return false, nil, nil, nil // UponCommit was already called
}
logger.Debug("📬 got commit message",
fields.Round(i.State.Round),
zap.Any("commit-signers", signedCommit.Signers))
// calculate commit quorum and act upon it
quorum, commitMsgs, err := commitQuorumForRoundRoot(i.State, commitMsgContainer, signedCommit.Message.Root, signedCommit.Message.Round)
if err != nil {
return false, nil, nil, errors.Wrap(err, "could not calculate commit quorum")
}
if quorum {
fullData := i.State.ProposalAcceptedForCurrentRound.FullData /* must have value there, checked on validateCommit */
agg, err := aggregateCommitMsgs(commitMsgs, fullData)
if err != nil {
return false, nil, nil, errors.Wrap(err, "could not aggregate commit msgs")
}
logger.Debug("🎯 got commit quorum",
fields.Round(i.State.Round),
zap.Any("commit-signers", signedCommit.Signers),
zap.Any("agg-signers", agg.Signers))
i.metrics.EndStageCommit()
return true, fullData, agg, nil
}
return false, nil, nil, nil
}
// returns true if there is a quorum for the current round for this provided value
func commitQuorumForRoundRoot(state *specqbft.State, commitMsgContainer *specqbft.MsgContainer, root [32]byte, round specqbft.Round) (bool, []*specqbft.SignedMessage, error) {
signers, msgs := commitMsgContainer.LongestUniqueSignersForRoundAndRoot(round, root)
return state.Share.HasQuorum(len(signers)), msgs, nil
}
func aggregateCommitMsgs(msgs []*specqbft.SignedMessage, fullData []byte) (*specqbft.SignedMessage, error) {
if len(msgs) == 0 {
return nil, errors.New("can't aggregate zero commit msgs")
}
var ret *specqbft.SignedMessage
for _, m := range msgs {
if ret == nil {
ret = m.DeepCopy()
} else {
if err := ret.Aggregate(m); err != nil {
return nil, errors.Wrap(err, "could not aggregate commit msg")
}
}
}
ret.FullData = fullData
// TODO: REWRITE THIS!
sort.Slice(ret.Signers, func(i, j int) bool {
return ret.Signers[i] < ret.Signers[j]
})
return ret, nil
}
// didSendCommitForHeightAndRound returns true if sent commit msg for specific Height and round
/**
!exists m :: && m in current.messagesReceived
&& m.Commit?
&& var uPayload := m.commitPayload.unsignedPayload;
&& uPayload.Height == |current.blockchain|
&& uPayload.round == current.round
&& recoverSignedCommitAuthor(m.commitPayload) == current.id
*/
func didSendCommitForHeightAndRound(state *specqbft.State, commitMsgContainer *specqbft.MsgContainer) bool {
for _, msg := range commitMsgContainer.MessagesForRound(state.Round) {
if msg.MatchedSigners([]spectypes.OperatorID{state.Share.OperatorID}) {
return true
}
}
return false
}
// CreateCommit
/**
Commit(
signCommit(
UnsignedCommit(
|current.blockchain|,
current.round,
signHash(hashBlockForCommitSeal(proposedBlock), current.id),
digest(proposedBlock)),
current.id
)
);
*/
func CreateCommit(state *specqbft.State, config qbft.IConfig, root [32]byte) (*specqbft.SignedMessage, error) {
msg := &specqbft.Message{
MsgType: specqbft.CommitMsgType,
Height: state.Height,
Round: state.Round,
Identifier: state.ID,
Root: root,
}
sig, err := config.GetSigner().SignRoot(msg, spectypes.QBFTSignatureType, state.Share.SharePubKey)
if err != nil {
return nil, errors.Wrap(err, "failed signing commit msg")
}
signedMsg := &specqbft.SignedMessage{
Signature: sig,
Signers: []spectypes.OperatorID{state.Share.OperatorID},
Message: *msg,
}
return signedMsg, nil
}
func BaseCommitValidation(
config qbft.IConfig,
signedCommit *specqbft.SignedMessage,
height specqbft.Height,
operators []*spectypes.Operator,
) error {
if signedCommit.Message.MsgType != specqbft.CommitMsgType {
return errors.New("commit msg type is wrong")
}
if signedCommit.Message.Height != height {
return errors.New("wrong msg height")
}
if err := signedCommit.Validate(); err != nil {
return errors.Wrap(err, "signed commit invalid")
}
// verify signature
if err := types.VerifyByOperators(signedCommit.Signature, signedCommit, config.GetSignatureDomainType(), spectypes.QBFTSignatureType, operators); err != nil {
return errors.Wrap(err, "msg signature invalid")
}
return nil
}
func validateCommit(
config qbft.IConfig,
signedCommit *specqbft.SignedMessage,
height specqbft.Height,
round specqbft.Round,
proposedMsg *specqbft.SignedMessage,
operators []*spectypes.Operator,
) error {
if err := BaseCommitValidation(config, signedCommit, height, operators); err != nil {
return err
}
if len(signedCommit.Signers) != 1 {
return errors.New("msg allows 1 signer")
}
if signedCommit.Message.Round != round {
return errors.New("wrong msg round")
}
if !bytes.Equal(proposedMsg.Message.Root[:], signedCommit.Message.Root[:]) {
return errors.New("proposed data mistmatch")
}
return nil
}