forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.go
385 lines (372 loc) · 14.2 KB
/
factory.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package blocks
import (
"fmt"
"math/big"
"github.com/pkg/errors"
"github.com/stratisproject/prysm-stratis/consensus-types/interfaces"
enginev1 "github.com/stratisproject/prysm-stratis/proto/engine/v1"
eth "github.com/stratisproject/prysm-stratis/proto/prysm/v1alpha1"
"github.com/stratisproject/prysm-stratis/runtime/version"
)
var (
// ErrUnsupportedSignedBeaconBlock is returned when the struct type is not a supported signed
// beacon block type.
ErrUnsupportedSignedBeaconBlock = errors.New("unsupported signed beacon block")
// errUnsupportedBeaconBlock is returned when the struct type is not a supported beacon block
// type.
errUnsupportedBeaconBlock = errors.New("unsupported beacon block")
// errUnsupportedBeaconBlockBody is returned when the struct type is not a supported beacon block body
// type.
errUnsupportedBeaconBlockBody = errors.New("unsupported beacon block body")
// ErrNilObject is returned in a constructor when the underlying object is nil.
ErrNilObject = errors.New("received nil object")
// ErrNilSignedBeaconBlock is returned when a nil signed beacon block is received.
ErrNilSignedBeaconBlock = errors.New("signed beacon block can't be nil")
// ErrNilBeaconBlock is returned when a nil beacon block is received.
ErrNilBeaconBlock = errors.New("beacon block can't be nil")
errNonBlindedSignedBeaconBlock = errors.New("can only build signed beacon block from blinded format")
)
// NewSignedBeaconBlock creates a signed beacon block from a protobuf signed beacon block.
func NewSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) {
switch b := i.(type) {
case nil:
return nil, ErrNilObject
case *eth.GenericSignedBeaconBlock_Phase0:
return initSignedBlockFromProtoPhase0(b.Phase0)
case *eth.SignedBeaconBlock:
return initSignedBlockFromProtoPhase0(b)
case *eth.GenericSignedBeaconBlock_Altair:
return initSignedBlockFromProtoAltair(b.Altair)
case *eth.SignedBeaconBlockAltair:
return initSignedBlockFromProtoAltair(b)
case *eth.GenericSignedBeaconBlock_Bellatrix:
return initSignedBlockFromProtoBellatrix(b.Bellatrix)
case *eth.SignedBeaconBlockBellatrix:
return initSignedBlockFromProtoBellatrix(b)
case *eth.GenericSignedBeaconBlock_BlindedBellatrix:
return initBlindedSignedBlockFromProtoBellatrix(b.BlindedBellatrix)
case *eth.SignedBlindedBeaconBlockBellatrix:
return initBlindedSignedBlockFromProtoBellatrix(b)
case *eth.GenericSignedBeaconBlock_Capella:
return initSignedBlockFromProtoCapella(b.Capella)
case *eth.SignedBeaconBlockCapella:
return initSignedBlockFromProtoCapella(b)
case *eth.GenericSignedBeaconBlock_BlindedCapella:
return initBlindedSignedBlockFromProtoCapella(b.BlindedCapella)
case *eth.SignedBlindedBeaconBlockCapella:
return initBlindedSignedBlockFromProtoCapella(b)
case *eth.GenericSignedBeaconBlock_Deneb:
return initSignedBlockFromProtoDeneb(b.Deneb.Block)
case *eth.SignedBeaconBlockDeneb:
return initSignedBlockFromProtoDeneb(b)
case *eth.SignedBlindedBeaconBlockDeneb:
return initBlindedSignedBlockFromProtoDeneb(b)
case *eth.GenericSignedBeaconBlock_BlindedDeneb:
return initBlindedSignedBlockFromProtoDeneb(b.BlindedDeneb)
default:
return nil, errors.Wrapf(ErrUnsupportedSignedBeaconBlock, "unable to create block from type %T", i)
}
}
// NewBeaconBlock creates a beacon block from a protobuf beacon block.
func NewBeaconBlock(i interface{}) (interfaces.ReadOnlyBeaconBlock, error) {
switch b := i.(type) {
case nil:
return nil, ErrNilObject
case *eth.GenericBeaconBlock_Phase0:
return initBlockFromProtoPhase0(b.Phase0)
case *eth.BeaconBlock:
return initBlockFromProtoPhase0(b)
case *eth.GenericBeaconBlock_Altair:
return initBlockFromProtoAltair(b.Altair)
case *eth.BeaconBlockAltair:
return initBlockFromProtoAltair(b)
case *eth.GenericBeaconBlock_Bellatrix:
return initBlockFromProtoBellatrix(b.Bellatrix)
case *eth.BeaconBlockBellatrix:
return initBlockFromProtoBellatrix(b)
case *eth.GenericBeaconBlock_BlindedBellatrix:
return initBlindedBlockFromProtoBellatrix(b.BlindedBellatrix)
case *eth.BlindedBeaconBlockBellatrix:
return initBlindedBlockFromProtoBellatrix(b)
case *eth.GenericBeaconBlock_Capella:
return initBlockFromProtoCapella(b.Capella)
case *eth.BeaconBlockCapella:
return initBlockFromProtoCapella(b)
case *eth.GenericBeaconBlock_BlindedCapella:
return initBlindedBlockFromProtoCapella(b.BlindedCapella)
case *eth.BlindedBeaconBlockCapella:
return initBlindedBlockFromProtoCapella(b)
case *eth.GenericBeaconBlock_Deneb:
return initBlockFromProtoDeneb(b.Deneb.Block)
case *eth.BeaconBlockDeneb:
return initBlockFromProtoDeneb(b)
case *eth.BlindedBeaconBlockDeneb:
return initBlindedBlockFromProtoDeneb(b)
case *eth.GenericBeaconBlock_BlindedDeneb:
return initBlindedBlockFromProtoDeneb(b.BlindedDeneb)
default:
return nil, errors.Wrapf(errUnsupportedBeaconBlock, "unable to create block from type %T", i)
}
}
// NewBeaconBlockBody creates a beacon block body from a protobuf beacon block body.
func NewBeaconBlockBody(i interface{}) (interfaces.ReadOnlyBeaconBlockBody, error) {
switch b := i.(type) {
case nil:
return nil, ErrNilObject
case *eth.BeaconBlockBody:
return initBlockBodyFromProtoPhase0(b)
case *eth.BeaconBlockBodyAltair:
return initBlockBodyFromProtoAltair(b)
case *eth.BeaconBlockBodyBellatrix:
return initBlockBodyFromProtoBellatrix(b)
case *eth.BlindedBeaconBlockBodyBellatrix:
return initBlindedBlockBodyFromProtoBellatrix(b)
case *eth.BeaconBlockBodyCapella:
return initBlockBodyFromProtoCapella(b)
case *eth.BlindedBeaconBlockBodyCapella:
return initBlindedBlockBodyFromProtoCapella(b)
case *eth.BeaconBlockBodyDeneb:
return initBlockBodyFromProtoDeneb(b)
case *eth.BlindedBeaconBlockBodyDeneb:
return initBlindedBlockBodyFromProtoDeneb(b)
default:
return nil, errors.Wrapf(errUnsupportedBeaconBlockBody, "unable to create block body from type %T", i)
}
}
// BuildSignedBeaconBlock assembles a block.ReadOnlySignedBeaconBlock interface compatible struct from a
// given beacon block and the appropriate signature. This method may be used to easily create a
// signed beacon block.
func BuildSignedBeaconBlock(blk interfaces.ReadOnlyBeaconBlock, signature []byte) (interfaces.SignedBeaconBlock, error) {
pb, err := blk.Proto()
if err != nil {
return nil, err
}
switch blk.Version() {
case version.Phase0:
pb, ok := pb.(*eth.BeaconBlock)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBeaconBlock{Block: pb, Signature: signature})
case version.Altair:
pb, ok := pb.(*eth.BeaconBlockAltair)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBeaconBlockAltair{Block: pb, Signature: signature})
case version.Bellatrix:
if blk.IsBlinded() {
pb, ok := pb.(*eth.BlindedBeaconBlockBellatrix)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBlindedBeaconBlockBellatrix{Block: pb, Signature: signature})
}
pb, ok := pb.(*eth.BeaconBlockBellatrix)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBeaconBlockBellatrix{Block: pb, Signature: signature})
case version.Capella:
if blk.IsBlinded() {
pb, ok := pb.(*eth.BlindedBeaconBlockCapella)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBlindedBeaconBlockCapella{Block: pb, Signature: signature})
}
pb, ok := pb.(*eth.BeaconBlockCapella)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBeaconBlockCapella{Block: pb, Signature: signature})
case version.Deneb:
if blk.IsBlinded() {
pb, ok := pb.(*eth.BlindedBeaconBlockDeneb)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBlindedBeaconBlockDeneb{Message: pb, Signature: signature})
}
pb, ok := pb.(*eth.BeaconBlockDeneb)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(ð.SignedBeaconBlockDeneb{Block: pb, Signature: signature})
default:
return nil, errUnsupportedBeaconBlock
}
}
// BuildSignedBeaconBlockFromExecutionPayload takes a signed, blinded beacon block and converts into
// a full, signed beacon block by specifying an execution payload.
func BuildSignedBeaconBlockFromExecutionPayload(
blk interfaces.ReadOnlySignedBeaconBlock, payload interface{},
) (interfaces.SignedBeaconBlock, error) {
if err := BeaconBlockIsNil(blk); err != nil {
return nil, err
}
if !blk.IsBlinded() {
return nil, errNonBlindedSignedBeaconBlock
}
b := blk.Block()
payloadHeader, err := b.Body().Execution()
if err != nil {
return nil, errors.Wrap(err, "could not get execution payload header")
}
var wrappedPayload interfaces.ExecutionData
var wrapErr error
switch p := payload.(type) {
case *enginev1.ExecutionPayload:
wrappedPayload, wrapErr = WrappedExecutionPayload(p)
case *enginev1.ExecutionPayloadCapella:
wrappedPayload, wrapErr = WrappedExecutionPayloadCapella(p, big.NewInt(0))
case *enginev1.ExecutionPayloadDeneb:
wrappedPayload, wrapErr = WrappedExecutionPayloadDeneb(p, big.NewInt(0))
default:
return nil, fmt.Errorf("%T is not a type of execution payload", p)
}
if wrapErr != nil {
return nil, wrapErr
}
empty, err := IsEmptyExecutionData(wrappedPayload)
if err != nil {
return nil, err
}
if !empty {
payloadRoot, err := wrappedPayload.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "could not hash tree root execution payload")
}
payloadHeaderRoot, err := payloadHeader.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "could not hash tree root payload header")
}
if payloadRoot != payloadHeaderRoot {
return nil, fmt.Errorf(
"payload %#x and header %#x roots do not match",
payloadRoot,
payloadHeaderRoot,
)
}
}
syncAgg, err := b.Body().SyncAggregate()
if err != nil {
return nil, errors.Wrap(err, "could not get sync aggregate from block body")
}
parentRoot := b.ParentRoot()
stateRoot := b.StateRoot()
randaoReveal := b.Body().RandaoReveal()
graffiti := b.Body().Graffiti()
sig := blk.Signature()
var fullBlock interface{}
switch p := payload.(type) {
case *enginev1.ExecutionPayload:
fullBlock = ð.SignedBeaconBlockBellatrix{
Block: ð.BeaconBlockBellatrix{
Slot: b.Slot(),
ProposerIndex: b.ProposerIndex(),
ParentRoot: parentRoot[:],
StateRoot: stateRoot[:],
Body: ð.BeaconBlockBodyBellatrix{
RandaoReveal: randaoReveal[:],
Eth1Data: b.Body().Eth1Data(),
Graffiti: graffiti[:],
ProposerSlashings: b.Body().ProposerSlashings(),
AttesterSlashings: b.Body().AttesterSlashings(),
Attestations: b.Body().Attestations(),
Deposits: b.Body().Deposits(),
VoluntaryExits: b.Body().VoluntaryExits(),
SyncAggregate: syncAgg,
ExecutionPayload: p,
},
},
Signature: sig[:],
}
case *enginev1.ExecutionPayloadCapella:
blsToExecutionChanges, err := b.Body().BLSToExecutionChanges()
if err != nil {
return nil, err
}
fullBlock = ð.SignedBeaconBlockCapella{
Block: ð.BeaconBlockCapella{
Slot: b.Slot(),
ProposerIndex: b.ProposerIndex(),
ParentRoot: parentRoot[:],
StateRoot: stateRoot[:],
Body: ð.BeaconBlockBodyCapella{
RandaoReveal: randaoReveal[:],
Eth1Data: b.Body().Eth1Data(),
Graffiti: graffiti[:],
ProposerSlashings: b.Body().ProposerSlashings(),
AttesterSlashings: b.Body().AttesterSlashings(),
Attestations: b.Body().Attestations(),
Deposits: b.Body().Deposits(),
VoluntaryExits: b.Body().VoluntaryExits(),
SyncAggregate: syncAgg,
ExecutionPayload: p,
BlsToExecutionChanges: blsToExecutionChanges,
},
},
Signature: sig[:],
}
case *enginev1.ExecutionPayloadDeneb:
blsToExecutionChanges, err := b.Body().BLSToExecutionChanges()
if err != nil {
return nil, err
}
commitments, err := b.Body().BlobKzgCommitments()
if err != nil {
return nil, err
}
fullBlock = ð.SignedBeaconBlockDeneb{
Block: ð.BeaconBlockDeneb{
Slot: b.Slot(),
ProposerIndex: b.ProposerIndex(),
ParentRoot: parentRoot[:],
StateRoot: stateRoot[:],
Body: ð.BeaconBlockBodyDeneb{
RandaoReveal: randaoReveal[:],
Eth1Data: b.Body().Eth1Data(),
Graffiti: graffiti[:],
ProposerSlashings: b.Body().ProposerSlashings(),
AttesterSlashings: b.Body().AttesterSlashings(),
Attestations: b.Body().Attestations(),
Deposits: b.Body().Deposits(),
VoluntaryExits: b.Body().VoluntaryExits(),
SyncAggregate: syncAgg,
ExecutionPayload: p,
BlsToExecutionChanges: blsToExecutionChanges,
BlobKzgCommitments: commitments,
},
},
Signature: sig[:],
}
default:
return nil, fmt.Errorf("%T is not a type of execution payload", p)
}
return NewSignedBeaconBlock(fullBlock)
}
// BeaconBlockContainerToSignedBeaconBlock converts BeaconBlockContainer (API response) to a SignedBeaconBlock.
// This is particularly useful for using the values from API calls.
func BeaconBlockContainerToSignedBeaconBlock(obj *eth.BeaconBlockContainer) (interfaces.ReadOnlySignedBeaconBlock, error) {
switch obj.Block.(type) {
case *eth.BeaconBlockContainer_BlindedDenebBlock:
return NewSignedBeaconBlock(obj.GetBlindedDenebBlock())
case *eth.BeaconBlockContainer_DenebBlock:
return NewSignedBeaconBlock(obj.GetDenebBlock())
case *eth.BeaconBlockContainer_BlindedCapellaBlock:
return NewSignedBeaconBlock(obj.GetBlindedCapellaBlock())
case *eth.BeaconBlockContainer_CapellaBlock:
return NewSignedBeaconBlock(obj.GetCapellaBlock())
case *eth.BeaconBlockContainer_BlindedBellatrixBlock:
return NewSignedBeaconBlock(obj.GetBlindedBellatrixBlock())
case *eth.BeaconBlockContainer_BellatrixBlock:
return NewSignedBeaconBlock(obj.GetBellatrixBlock())
case *eth.BeaconBlockContainer_AltairBlock:
return NewSignedBeaconBlock(obj.GetAltairBlock())
case *eth.BeaconBlockContainer_Phase0Block:
return NewSignedBeaconBlock(obj.GetPhase0Block())
default:
return nil, errors.New("container block type not recognized")
}
}