forked from ObolNetwork/charon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsigneddata.go
403 lines (346 loc) · 11.2 KB
/
unsigneddata.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright © 2022-2024 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1
package core
import (
"encoding/json"
eth2api "github.com/attestantio/go-eth2-client/api"
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2bellatrix "github.com/attestantio/go-eth2-client/api/v1/bellatrix"
eth2capella "github.com/attestantio/go-eth2-client/api/v1/capella"
eth2deneb "github.com/attestantio/go-eth2-client/api/v1/deneb"
eth2spec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/capella"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/eth2util"
)
var (
_ UnsignedData = AttestationData{}
_ UnsignedData = AggregatedAttestation{}
_ UnsignedData = VersionedProposal{}
_ UnsignedData = SyncContribution{}
// Some types also support SSZ marshalling and unmarshalling.
_ ssz.Marshaler = AttestationData{}
_ ssz.Marshaler = AggregatedAttestation{}
_ ssz.Marshaler = VersionedProposal{}
_ ssz.Marshaler = SyncContribution{}
_ ssz.Unmarshaler = new(AttestationData)
_ ssz.Unmarshaler = new(AggregatedAttestation)
_ ssz.Unmarshaler = new(VersionedProposal)
_ ssz.Unmarshaler = new(SyncContribution)
)
// AttestationData wraps the eth2 attestation data and adds the original duty.
// The original duty allows mapping the partial signed response from the VC
// back to the validator pubkey via the aggregation bits field.
type AttestationData struct {
Data eth2p0.AttestationData
Duty eth2v1.AttesterDuty
}
func (a AttestationData) Clone() (UnsignedData, error) {
var resp AttestationData
err := cloneJSONMarshaler(a, &resp)
if err != nil {
return nil, errors.Wrap(err, "clone attestation")
}
return resp, nil
}
func (a AttestationData) MarshalJSON() ([]byte, error) {
resp, err := json.Marshal(attestationDataJSON{
Data: &a.Data,
Duty: &a.Duty,
})
if err != nil {
return nil, errors.Wrap(err, "marshal attestation")
}
return resp, nil
}
func (a *AttestationData) UnmarshalJSON(data []byte) error {
var att attestationDataJSON
if err := json.Unmarshal(data, &att); err != nil {
return errors.Wrap(err, "unmarshal attestation")
}
a.Data = *att.Data
a.Duty = *att.Duty
return nil
}
type attestationDataJSON struct {
Data *eth2p0.AttestationData `json:"attestation_data"`
Duty *eth2v1.AttesterDuty `json:"attestation_duty"`
}
// NewAggregatedAttestation returns a new aggregated attestation.
func NewAggregatedAttestation(att *eth2p0.Attestation) AggregatedAttestation {
return AggregatedAttestation{Attestation: *att}
}
// AggregatedAttestation wraps un unsigned aggregated attestation and implements the UnsignedData interface.
type AggregatedAttestation struct {
eth2p0.Attestation
}
func (a AggregatedAttestation) Clone() (UnsignedData, error) {
var resp AggregatedAttestation
err := cloneJSONMarshaler(a, &resp)
if err != nil {
return nil, errors.Wrap(err, "clone aggregated attestation")
}
return resp, nil
}
func (a AggregatedAttestation) MarshalJSON() ([]byte, error) {
return a.Attestation.MarshalJSON()
}
func (a *AggregatedAttestation) UnmarshalJSON(input []byte) error {
var att eth2p0.Attestation
if err := json.Unmarshal(input, &att); err != nil {
return errors.Wrap(err, "unmarshal aggregated attestation")
}
*a = AggregatedAttestation{Attestation: att}
return nil
}
func (a AggregatedAttestation) MarshalSSZ() ([]byte, error) {
return a.Attestation.MarshalSSZ()
}
func (a AggregatedAttestation) MarshalSSZTo(dst []byte) ([]byte, error) {
return a.Attestation.MarshalSSZTo(dst)
}
func (a AggregatedAttestation) SizeSSZ() int {
return a.Attestation.SizeSSZ()
}
func (a *AggregatedAttestation) UnmarshalSSZ(b []byte) error {
return a.Attestation.UnmarshalSSZ(b)
}
// NewVersionedProposal validates and returns a new wrapped VersionedProposal.
func NewVersionedProposal(proposal *eth2api.VersionedProposal) (VersionedProposal, error) {
switch proposal.Version {
case eth2spec.DataVersionPhase0:
if proposal.Phase0 == nil {
return VersionedProposal{}, errors.New("no phase0 block")
}
case eth2spec.DataVersionAltair:
if proposal.Altair == nil {
return VersionedProposal{}, errors.New("no altair block")
}
case eth2spec.DataVersionBellatrix:
if proposal.Bellatrix == nil && !proposal.Blinded {
return VersionedProposal{}, errors.New("no bellatrix block")
}
if proposal.BellatrixBlinded == nil && proposal.Blinded {
return VersionedProposal{}, errors.New("no bellatrix blinded block")
}
case eth2spec.DataVersionCapella:
if proposal.Capella == nil && !proposal.Blinded {
return VersionedProposal{}, errors.New("no capella block")
}
if proposal.CapellaBlinded == nil && proposal.Blinded {
return VersionedProposal{}, errors.New("no capella blinded block")
}
case eth2spec.DataVersionDeneb:
if proposal.Deneb == nil && !proposal.Blinded {
return VersionedProposal{}, errors.New("no deneb block")
}
if proposal.DenebBlinded == nil && proposal.Blinded {
return VersionedProposal{}, errors.New("no deneb blinded block")
}
default:
return VersionedProposal{}, errors.New("unknown version")
}
return VersionedProposal{VersionedProposal: *proposal}, nil
}
// VersionedProposal wraps the eth2 versioned proposal and implements UnsignedData.
type VersionedProposal struct {
eth2api.VersionedProposal
}
func (p VersionedProposal) Clone() (UnsignedData, error) {
var resp VersionedProposal
err := cloneJSONMarshaler(p, &resp)
if err != nil {
return nil, errors.Wrap(err, "clone block")
}
return resp, nil
}
func (p VersionedProposal) MarshalJSON() ([]byte, error) {
var marshaller json.Marshaler
switch p.Version {
// No block nil checks since `NewVersionedProposal` assumed.
case eth2spec.DataVersionPhase0:
marshaller = p.Phase0
case eth2spec.DataVersionAltair:
marshaller = p.Altair
case eth2spec.DataVersionBellatrix:
if p.Blinded {
marshaller = p.BellatrixBlinded
} else {
marshaller = p.Bellatrix
}
case eth2spec.DataVersionCapella:
if p.Blinded {
marshaller = p.CapellaBlinded
} else {
marshaller = p.Capella
}
case eth2spec.DataVersionDeneb:
if p.Blinded {
marshaller = p.DenebBlinded
} else {
marshaller = p.Deneb
}
default:
return nil, errors.New("unknown version")
}
block, err := marshaller.MarshalJSON()
if err != nil {
return nil, errors.Wrap(err, "marshal block")
}
version, err := eth2util.DataVersionFromETH2(p.Version)
if err != nil {
return nil, errors.Wrap(err, "convert version")
}
resp, err := json.Marshal(versionedRawBlockJSON{
Version: version,
Block: block,
Blinded: p.Blinded,
})
if err != nil {
return nil, errors.Wrap(err, "marshal wrapper")
}
return resp, nil
}
func (p *VersionedProposal) UnmarshalJSON(input []byte) error {
var raw versionedRawBlockJSON
if err := json.Unmarshal(input, &raw); err != nil {
return errors.Wrap(err, "unmarshal block")
}
resp := eth2api.VersionedProposal{
Version: raw.Version.ToETH2(),
Blinded: raw.Blinded,
}
switch resp.Version {
case eth2spec.DataVersionPhase0:
if raw.Blinded {
return errors.New("phase0 block cannot be blinded")
}
block := new(eth2p0.BeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal phase0")
}
resp.Phase0 = block
case eth2spec.DataVersionAltair:
if raw.Blinded {
return errors.New("altair block cannot be blinded")
}
block := new(altair.BeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal altair")
}
resp.Altair = block
case eth2spec.DataVersionBellatrix:
if raw.Blinded {
block := new(eth2bellatrix.BlindedBeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal bellatrix blinded")
}
resp.BellatrixBlinded = block
} else {
block := new(bellatrix.BeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal bellatrix")
}
resp.Bellatrix = block
}
case eth2spec.DataVersionCapella:
if raw.Blinded {
block := new(eth2capella.BlindedBeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal capella blinded")
}
resp.CapellaBlinded = block
} else {
block := new(capella.BeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal capella")
}
resp.Capella = block
}
case eth2spec.DataVersionDeneb:
if raw.Blinded {
block := new(eth2deneb.BlindedBeaconBlock)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal deneb blinded")
}
resp.DenebBlinded = block
} else {
block := new(eth2deneb.BlockContents)
if err := json.Unmarshal(raw.Block, &block); err != nil {
return errors.Wrap(err, "unmarshal deneb")
}
resp.Deneb = block
}
default:
return errors.New("unknown version")
}
*p = VersionedProposal{VersionedProposal: resp}
return nil
}
// NewSyncContribution returns a new SyncContribution.
func NewSyncContribution(c *altair.SyncCommitteeContribution) SyncContribution {
return SyncContribution{SyncCommitteeContribution: *c}
}
type SyncContribution struct {
altair.SyncCommitteeContribution
}
func (s SyncContribution) Clone() (UnsignedData, error) {
var resp SyncContribution
err := cloneJSONMarshaler(s, &resp)
if err != nil {
return nil, errors.Wrap(err, "clone sync contribution")
}
return resp, err
}
func (s SyncContribution) MarshalJSON() ([]byte, error) {
return s.SyncCommitteeContribution.MarshalJSON()
}
func (s *SyncContribution) UnmarshalJSON(input []byte) error {
return s.SyncCommitteeContribution.UnmarshalJSON(input)
}
func (s SyncContribution) MarshalSSZ() ([]byte, error) {
return s.SyncCommitteeContribution.MarshalSSZ()
}
func (s SyncContribution) MarshalSSZTo(dst []byte) ([]byte, error) {
return s.SyncCommitteeContribution.MarshalSSZTo(dst)
}
func (s SyncContribution) SizeSSZ() int {
return s.SyncCommitteeContribution.SizeSSZ()
}
func (s *SyncContribution) UnmarshalSSZ(b []byte) error {
return s.SyncCommitteeContribution.UnmarshalSSZ(b)
}
// unmarshalUnsignedData returns an instantiated unsigned data based on the duty type.
func unmarshalUnsignedData(typ DutyType, data []byte) (UnsignedData, error) {
switch typ {
case DutyAttester:
var resp AttestationData
if err := unmarshal(data, &resp); err != nil {
return nil, errors.Wrap(err, "unmarshal attestation data")
}
return resp, nil
case DutyProposer:
var resp VersionedProposal
if err := unmarshal(data, &resp); err != nil {
return nil, errors.Wrap(err, "unmarshal proposal")
}
return resp, nil
case DutyAggregator:
var resp AggregatedAttestation
if err := unmarshal(data, &resp); err != nil {
return nil, errors.Wrap(err, "unmarshal aggregated attestation")
}
return resp, nil
case DutySyncContribution:
var resp SyncContribution
if err := unmarshal(data, &resp); err != nil {
return nil, errors.Wrap(err, "unmarshal sync contribution")
}
return resp, nil
default:
return nil, errors.New("unsupported unsigned data duty type")
}
}