-
Notifications
You must be signed in to change notification settings - Fork 22
/
proposer.go
342 lines (293 loc) · 10 KB
/
proposer.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package ssv
import (
"crypto/sha256"
"encoding/json"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
"github.com/pkg/errors"
"github.com/bloxapp/ssv-spec/qbft"
"github.com/bloxapp/ssv-spec/types"
)
type ProposerRunner struct {
BaseRunner *BaseRunner
// ProducesBlindedBlocks is true when the runner will only produce blinded blocks
ProducesBlindedBlocks bool
beacon BeaconNode
network Network
signer types.KeyManager
valCheck qbft.ProposedValueCheckF
}
func NewProposerRunner(
beaconNetwork types.BeaconNetwork,
share *types.Share,
qbftController *qbft.Controller,
beacon BeaconNode,
network Network,
signer types.KeyManager,
valCheck qbft.ProposedValueCheckF,
highestDecidedSlot phase0.Slot,
) Runner {
return &ProposerRunner{
BaseRunner: &BaseRunner{
BeaconRoleType: types.BNRoleProposer,
BeaconNetwork: beaconNetwork,
Share: share,
QBFTController: qbftController,
highestDecidedSlot: highestDecidedSlot,
},
beacon: beacon,
network: network,
signer: signer,
valCheck: valCheck,
}
}
func (r *ProposerRunner) StartNewDuty(duty *types.Duty) error {
return r.BaseRunner.baseStartNewDuty(r, duty)
}
// HasRunningDuty returns true if a duty is already running (StartNewDuty called and returned nil)
func (r *ProposerRunner) HasRunningDuty() bool {
return r.BaseRunner.hasRunningDuty()
}
func (r *ProposerRunner) ProcessPreConsensus(signedMsg *types.SignedPartialSignatureMessage) error {
quorum, roots, err := r.BaseRunner.basePreConsensusMsgProcessing(r, signedMsg)
if err != nil {
return errors.Wrap(err, "failed processing randao message")
}
// quorum returns true only once (first time quorum achieved)
if !quorum {
return nil
}
// only 1 root, verified in basePreConsensusMsgProcessing
root := roots[0]
// randao is relevant only for block proposals, no need to check type
fullSig, err := r.GetState().ReconstructBeaconSig(r.GetState().PreConsensusContainer, root, r.GetShare().ValidatorPubKey)
if err != nil {
return errors.Wrap(err, "could not reconstruct randao sig")
}
duty := r.GetState().StartingDuty
var ver spec.DataVersion
var obj ssz.Marshaler
if r.ProducesBlindedBlocks {
// get block data
obj, ver, err = r.GetBeaconNode().GetBlindedBeaconBlock(duty.Slot, r.GetShare().Graffiti, fullSig)
if err != nil {
return errors.Wrap(err, "failed to get Beacon block")
}
} else {
// get block data
obj, ver, err = r.GetBeaconNode().GetBeaconBlock(duty.Slot, r.GetShare().Graffiti, fullSig)
if err != nil {
return errors.Wrap(err, "failed to get Beacon block")
}
}
byts, err := obj.MarshalSSZ()
if err != nil {
return errors.Wrap(err, "could not marshal beacon block")
}
input := &types.ConsensusData{
Duty: *duty,
Version: ver,
DataSSZ: byts,
}
if err := r.BaseRunner.decide(r, input); err != nil {
return errors.Wrap(err, "can't start new duty runner instance for duty")
}
return nil
}
func (r *ProposerRunner) ProcessConsensus(signedMsg *qbft.SignedMessage) error {
decided, decidedValue, err := r.BaseRunner.baseConsensusMsgProcessing(r, signedMsg)
if err != nil {
return errors.Wrap(err, "failed processing consensus message")
}
// Decided returns true only once so if it is true it must be for the current running instance
if !decided {
return nil
}
// specific duty sig
var blkToSign ssz.HashRoot
if r.decidedBlindedBlock() {
_, blkToSign, err = decidedValue.GetBlindedBlockData()
if err != nil {
return errors.Wrap(err, "could not get blinded block data")
}
} else {
_, blkToSign, err = decidedValue.GetBlockData()
if err != nil {
return errors.Wrap(err, "could not get block data")
}
}
msg, err := r.BaseRunner.signBeaconObject(
r,
blkToSign,
decidedValue.Duty.Slot,
types.DomainProposer,
)
if err != nil {
return errors.Wrap(err, "failed signing attestation data")
}
postConsensusMsg := &types.PartialSignatureMessages{
Type: types.PostConsensusPartialSig,
Slot: decidedValue.Duty.Slot,
Messages: []*types.PartialSignatureMessage{msg},
}
postSignedMsg, err := r.BaseRunner.signPostConsensusMsg(r, postConsensusMsg)
if err != nil {
return errors.Wrap(err, "could not sign post consensus msg")
}
data, err := postSignedMsg.Encode()
if err != nil {
return errors.Wrap(err, "failed to encode post consensus signature msg")
}
msgToBroadcast := &types.SSVMessage{
MsgType: types.SSVPartialSignatureMsgType,
MsgID: types.NewMsgID(r.GetShare().DomainType, r.GetShare().ValidatorPubKey, r.BaseRunner.BeaconRoleType),
Data: data,
}
if err := r.GetNetwork().Broadcast(msgToBroadcast); err != nil {
return errors.Wrap(err, "can't broadcast partial post consensus sig")
}
return nil
}
func (r *ProposerRunner) ProcessPostConsensus(signedMsg *types.SignedPartialSignatureMessage) error {
quorum, roots, err := r.BaseRunner.basePostConsensusMsgProcessing(r, signedMsg)
if err != nil {
return errors.Wrap(err, "failed processing post consensus message")
}
if !quorum {
return nil
}
for _, root := range roots {
sig, err := r.GetState().ReconstructBeaconSig(r.GetState().PostConsensusContainer, root, r.GetShare().ValidatorPubKey)
if err != nil {
return errors.Wrap(err, "could not reconstruct post consensus signature")
}
specSig := phase0.BLSSignature{}
copy(specSig[:], sig)
if r.decidedBlindedBlock() {
vBlindedBlk, _, err := r.GetState().DecidedValue.GetBlindedBlockData()
if err != nil {
return errors.Wrap(err, "could not get blinded block")
}
if err := r.GetBeaconNode().SubmitBlindedBeaconBlock(vBlindedBlk, specSig); err != nil {
return errors.Wrap(err, "could not submit to Beacon chain reconstructed signed blinded Beacon block")
}
} else {
vBlk, _, err := r.GetState().DecidedValue.GetBlockData()
if err != nil {
return errors.Wrap(err, "could not get block")
}
if err := r.GetBeaconNode().SubmitBeaconBlock(vBlk, specSig); err != nil {
return errors.Wrap(err, "could not submit to Beacon chain reconstructed signed Beacon block")
}
}
}
r.GetState().Finished = true
return nil
}
// decidedBlindedBlock returns true if decided value has a blinded block, false if regular block
// WARNING!! should be called after decided only
func (r *ProposerRunner) decidedBlindedBlock() bool {
_, _, err := r.BaseRunner.State.DecidedValue.GetBlindedBlockData()
return err == nil
}
func (r *ProposerRunner) expectedPreConsensusRootsAndDomain() ([]ssz.HashRoot, phase0.DomainType, error) {
epoch := r.BaseRunner.BeaconNetwork.EstimatedEpochAtSlot(r.GetState().StartingDuty.Slot)
return []ssz.HashRoot{types.SSZUint64(epoch)}, types.DomainRandao, nil
}
// expectedPostConsensusRootsAndDomain an INTERNAL function, returns the expected post-consensus roots to sign
func (r *ProposerRunner) expectedPostConsensusRootsAndDomain() ([]ssz.HashRoot, phase0.DomainType, error) {
if r.decidedBlindedBlock() {
_, data, err := r.GetState().DecidedValue.GetBlindedBlockData()
if err != nil {
return nil, phase0.DomainType{}, errors.Wrap(err, "could not get blinded block data")
}
return []ssz.HashRoot{data}, types.DomainProposer, nil
}
_, data, err := r.GetState().DecidedValue.GetBlockData()
if err != nil {
return nil, phase0.DomainType{}, errors.Wrap(err, "could not get block data")
}
return []ssz.HashRoot{data}, types.DomainProposer, nil
}
// executeDuty steps:
// 1) sign a partial randao sig and wait for 2f+1 partial sigs from peers
// 2) reconstruct randao and send GetBeaconBlock to BN
// 3) start consensus on duty + block data
// 4) Once consensus decides, sign partial block and broadcast
// 5) collect 2f+1 partial sigs, reconstruct and broadcast valid block sig to the BN
func (r *ProposerRunner) executeDuty(duty *types.Duty) error {
// sign partial randao
epoch := r.GetBeaconNode().GetBeaconNetwork().EstimatedEpochAtSlot(duty.Slot)
msg, err := r.BaseRunner.signBeaconObject(r, types.SSZUint64(epoch), duty.Slot, types.DomainRandao)
if err != nil {
return errors.Wrap(err, "could not sign randao")
}
msgs := types.PartialSignatureMessages{
Type: types.RandaoPartialSig,
Slot: duty.Slot,
Messages: []*types.PartialSignatureMessage{msg},
}
// sign msg
signature, err := r.GetSigner().SignRoot(msgs, types.PartialSignatureType, r.GetShare().SharePubKey)
if err != nil {
return errors.Wrap(err, "could not sign randao msg")
}
signedPartialMsg := &types.SignedPartialSignatureMessage{
Message: msgs,
Signature: signature,
Signer: r.GetShare().OperatorID,
}
// broadcast
data, err := signedPartialMsg.Encode()
if err != nil {
return errors.Wrap(err, "failed to encode randao pre-consensus signature msg")
}
msgToBroadcast := &types.SSVMessage{
MsgType: types.SSVPartialSignatureMsgType,
MsgID: types.NewMsgID(r.GetShare().DomainType, r.GetShare().ValidatorPubKey, r.BaseRunner.BeaconRoleType),
Data: data,
}
if err := r.GetNetwork().Broadcast(msgToBroadcast); err != nil {
return errors.Wrap(err, "can't broadcast partial randao sig")
}
return nil
}
func (r *ProposerRunner) GetBaseRunner() *BaseRunner {
return r.BaseRunner
}
func (r *ProposerRunner) GetNetwork() Network {
return r.network
}
func (r *ProposerRunner) GetBeaconNode() BeaconNode {
return r.beacon
}
func (r *ProposerRunner) GetShare() *types.Share {
return r.BaseRunner.Share
}
func (r *ProposerRunner) GetState() *State {
return r.BaseRunner.State
}
func (r *ProposerRunner) GetValCheckF() qbft.ProposedValueCheckF {
return r.valCheck
}
func (r *ProposerRunner) GetSigner() types.KeyManager {
return r.signer
}
// Encode returns the encoded struct in bytes or error
func (r *ProposerRunner) Encode() ([]byte, error) {
return json.Marshal(r)
}
// Decode returns error if decoding failed
func (r *ProposerRunner) Decode(data []byte) error {
return json.Unmarshal(data, &r)
}
// GetRoot returns the root used for signing and verification
func (r *ProposerRunner) GetRoot() ([32]byte, error) {
marshaledRoot, err := r.Encode()
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode DutyRunnerState")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}