@@ -183,7 +183,18 @@ func (s *Server) SubmitAttestations(w http.ResponseWriter, r *http.Request) {
183
183
ctx , span := trace .StartSpan (r .Context (), "beacon.SubmitAttestations" )
184
184
defer span .End ()
185
185
186
- attFailures , failedBroadcasts , err := s .handleAttestations (ctx , r )
186
+ var req structs.SubmitAttestationsRequest
187
+ err := json .NewDecoder (r .Body ).Decode (& req .Data )
188
+ switch {
189
+ case errors .Is (err , io .EOF ):
190
+ httputil .HandleError (w , "No data submitted" , http .StatusBadRequest )
191
+ return
192
+ case err != nil :
193
+ httputil .HandleError (w , "Could not decode request body: " + err .Error (), http .StatusBadRequest )
194
+ return
195
+ }
196
+
197
+ attFailures , failedBroadcasts , err := s .handleAttestations (ctx , req .Data )
187
198
if err != nil {
188
199
httputil .HandleError (w , err .Error (), http .StatusBadRequest )
189
200
return
@@ -225,13 +236,24 @@ func (s *Server) SubmitAttestationsV2(w http.ResponseWriter, r *http.Request) {
225
236
return
226
237
}
227
238
239
+ var req structs.SubmitAttestationsRequest
240
+ err = json .NewDecoder (r .Body ).Decode (& req .Data )
241
+ switch {
242
+ case errors .Is (err , io .EOF ):
243
+ httputil .HandleError (w , "No data submitted" , http .StatusBadRequest )
244
+ return
245
+ case err != nil :
246
+ httputil .HandleError (w , "Could not decode request body: " + err .Error (), http .StatusBadRequest )
247
+ return
248
+ }
249
+
228
250
var attFailures []* server.IndexedVerificationFailure
229
251
var failedBroadcasts []string
230
252
231
253
if v >= version .Electra {
232
- attFailures , failedBroadcasts , err = s .handleAttestationsElectra (ctx , r )
254
+ attFailures , failedBroadcasts , err = s .handleAttestationsElectra (ctx , req . Data )
233
255
} else {
234
- attFailures , failedBroadcasts , err = s .handleAttestations (ctx , r )
256
+ attFailures , failedBroadcasts , err = s .handleAttestations (ctx , req . Data )
235
257
}
236
258
if err != nil {
237
259
httputil .HandleError (w , fmt .Sprintf ("Failed to handle attestations: %v" , err ), http .StatusBadRequest )
@@ -259,84 +281,40 @@ func (s *Server) SubmitAttestationsV2(w http.ResponseWriter, r *http.Request) {
259
281
260
282
func (s * Server ) handleAttestationsElectra (
261
283
ctx context.Context ,
262
- r * http. Request ,
284
+ data json. RawMessage ,
263
285
) (attFailures []* server.IndexedVerificationFailure , failedBroadcasts []string , err error ) {
264
- var validAttestations []* eth .SingleAttestation
286
+ var sourceAttestations []* structs .SingleAttestation
265
287
currentEpoch := slots .ToEpoch (s .TimeFetcher .CurrentSlot ())
266
288
if currentEpoch < params .BeaconConfig ().ElectraForkEpoch {
267
289
return nil , nil , errors .Errorf ("electra attestations have not been enabled, current epoch %d enabled epoch %d" , currentEpoch , params .BeaconConfig ().ElectraForkEpoch )
268
290
}
269
291
270
- if httputil .IsRequestSsz (r ) {
271
- sszLen := (& eth.SingleAttestation {}).SizeSSZ ()
272
- body , err := readRequestBody (r )
273
- if err != nil {
274
- return nil , nil , errors .Wrap (err , "could not read request body" )
275
- }
276
- if len (body ) < sszLen {
277
- return nil , nil , errors .New ("no data submitted" )
278
- }
279
-
280
- i := 0
281
- for len (body ) >= sszLen {
282
- var sourceAtt eth.SingleAttestation
283
- if err := sourceAtt .UnmarshalSSZ (body [:sszLen ]); err != nil {
284
- return nil , nil , errors .Wrap (err , "could not unmarshal ssz attestation" )
285
- }
286
- body = body [sszLen :]
287
-
288
- if _ , err = bls .SignatureFromBytes (sourceAtt .Signature ); err != nil {
289
- attFailures = append (attFailures , & server.IndexedVerificationFailure {
290
- Index : i ,
291
- Message : "Incorrect attestation signature: " + err .Error (),
292
- })
293
- i ++
294
- continue
295
- }
296
- validAttestations = append (validAttestations , & sourceAtt )
297
- i ++
298
- }
299
-
300
- if len (body ) > 0 {
301
- return nil , nil , errors .New ("could not decode request body, extra bytes found" )
302
- }
303
- } else {
304
- var sourceAttestations []* structs.SingleAttestation
305
- var req structs.SubmitAttestationsRequest
306
- err = json .NewDecoder (r .Body ).Decode (& req .Data )
307
- switch {
308
- case errors .Is (err , io .EOF ):
309
- return nil , nil , errors .New ("no data submitted" )
310
- case err != nil :
311
- return nil , nil , errors .Wrap (err , "could not decode request body" )
312
- }
292
+ if err = json .Unmarshal (data , & sourceAttestations ); err != nil {
293
+ return nil , nil , errors .Wrap (err , "failed to unmarshal attestation" )
294
+ }
313
295
314
- if err = json . Unmarshal ( req . Data , & sourceAttestations ); err != nil {
315
- return nil , nil , errors .Wrap ( err , "failed to unmarshal attestation " )
316
- }
296
+ if len ( sourceAttestations ) == 0 {
297
+ return nil , nil , errors .New ( "no data submitted " )
298
+ }
317
299
318
- if len (sourceAttestations ) == 0 {
319
- return nil , nil , errors .New ("no data submitted" )
300
+ var validAttestations []* eth.SingleAttestation
301
+ for i , sourceAtt := range sourceAttestations {
302
+ att , err := sourceAtt .ToConsensus ()
303
+ if err != nil {
304
+ attFailures = append (attFailures , & server.IndexedVerificationFailure {
305
+ Index : i ,
306
+ Message : "Could not convert request attestation to consensus attestation: " + err .Error (),
307
+ })
308
+ continue
320
309
}
321
-
322
- for i , sourceAtt := range sourceAttestations {
323
- att , err := sourceAtt .ToConsensus ()
324
- if err != nil {
325
- attFailures = append (attFailures , & server.IndexedVerificationFailure {
326
- Index : i ,
327
- Message : "Could not convert request attestation to consensus attestation: " + err .Error (),
328
- })
329
- continue
330
- }
331
- if _ , err = bls .SignatureFromBytes (att .Signature ); err != nil {
332
- attFailures = append (attFailures , & server.IndexedVerificationFailure {
333
- Index : i ,
334
- Message : "Incorrect attestation signature: " + err .Error (),
335
- })
336
- continue
337
- }
338
- validAttestations = append (validAttestations , att )
310
+ if _ , err = bls .SignatureFromBytes (att .Signature ); err != nil {
311
+ attFailures = append (attFailures , & server.IndexedVerificationFailure {
312
+ Index : i ,
313
+ Message : "Incorrect attestation signature: " + err .Error (),
314
+ })
315
+ continue
339
316
}
317
+ validAttestations = append (validAttestations , att )
340
318
}
341
319
342
320
for i , singleAtt := range validAttestations {
@@ -384,83 +362,39 @@ func (s *Server) handleAttestationsElectra(
384
362
return attFailures , failedBroadcasts , nil
385
363
}
386
364
387
- func (s * Server ) handleAttestations (ctx context.Context , r * http. Request ) (attFailures []* server.IndexedVerificationFailure , failedBroadcasts []string , err error ) {
388
- var validAttestations []* eth .Attestation
365
+ func (s * Server ) handleAttestations (ctx context.Context , data json. RawMessage ) (attFailures []* server.IndexedVerificationFailure , failedBroadcasts []string , err error ) {
366
+ var sourceAttestations []* structs .Attestation
389
367
390
368
if slots .ToEpoch (s .TimeFetcher .CurrentSlot ()) >= params .BeaconConfig ().ElectraForkEpoch {
391
369
return nil , nil , errors .New ("old attestation format, only electra attestations should be sent" )
392
370
}
393
371
394
- if httputil .IsRequestSsz (r ) {
395
- body , err := readRequestBody (r )
396
- if err != nil {
397
- return nil , nil , errors .Wrap (err , "could not read request body" )
398
- }
399
- if len (body ) == 0 {
400
- return nil , nil , errors .New ("no data submitted" )
401
- }
402
-
403
- i := 0
404
- for len (body ) > 0 {
405
- // Since AggregationBits in Attestation is variable length, the length of the Attestation object will be added as the first byte, per SSZ spec.
406
- sszLen := 1 + int (body [0 ])
407
- var sourceAtt eth.Attestation
408
- if err := sourceAtt .UnmarshalSSZ (body [:sszLen ]); err != nil {
409
- return nil , nil , errors .Wrap (err , "could not unmarshal ssz attestation" )
410
- }
411
- body = body [sszLen :]
412
-
413
- if _ , err = bls .SignatureFromBytes (sourceAtt .Signature ); err != nil {
414
- attFailures = append (attFailures , & server.IndexedVerificationFailure {
415
- Index : i ,
416
- Message : "Incorrect attestation signature: " + err .Error (),
417
- })
418
- } else {
419
- validAttestations = append (validAttestations , & sourceAtt )
420
- }
421
- i ++
422
- }
423
-
424
- if len (body ) > 0 {
425
- return nil , nil , errors .New ("could not decode request body, extra bytes found" )
426
- }
427
- } else {
428
- var sourceAttestations []* structs.Attestation
429
- var req structs.SubmitAttestationsRequest
430
- err = json .NewDecoder (r .Body ).Decode (& req .Data )
431
- switch {
432
- case errors .Is (err , io .EOF ):
433
- return nil , nil , errors .New ("no data submitted" )
434
- case err != nil :
435
- return nil , nil , errors .Wrap (err , "could not decode request body" )
436
- }
372
+ if err = json .Unmarshal (data , & sourceAttestations ); err != nil {
373
+ return nil , nil , errors .Wrap (err , "failed to unmarshal attestation" )
374
+ }
437
375
438
- if err = json . Unmarshal ( req . Data , & sourceAttestations ); err != nil {
439
- return nil , nil , errors .Wrap ( err , "failed to unmarshal attestation " )
440
- }
376
+ if len ( sourceAttestations ) == 0 {
377
+ return nil , nil , errors .New ( "no data submitted " )
378
+ }
441
379
442
- if len (sourceAttestations ) == 0 {
443
- return nil , nil , errors .New ("no data submitted" )
380
+ var validAttestations []* eth.Attestation
381
+ for i , sourceAtt := range sourceAttestations {
382
+ att , err := sourceAtt .ToConsensus ()
383
+ if err != nil {
384
+ attFailures = append (attFailures , & server.IndexedVerificationFailure {
385
+ Index : i ,
386
+ Message : "Could not convert request attestation to consensus attestation: " + err .Error (),
387
+ })
388
+ continue
444
389
}
445
-
446
- for i , sourceAtt := range sourceAttestations {
447
- att , err := sourceAtt .ToConsensus ()
448
- if err != nil {
449
- attFailures = append (attFailures , & server.IndexedVerificationFailure {
450
- Index : i ,
451
- Message : "Could not convert request attestation to consensus attestation: " + err .Error (),
452
- })
453
- continue
454
- }
455
- if _ , err = bls .SignatureFromBytes (att .Signature ); err != nil {
456
- attFailures = append (attFailures , & server.IndexedVerificationFailure {
457
- Index : i ,
458
- Message : "Incorrect attestation signature: " + err .Error (),
459
- })
460
- continue
461
- }
462
- validAttestations = append (validAttestations , att )
390
+ if _ , err = bls .SignatureFromBytes (att .Signature ); err != nil {
391
+ attFailures = append (attFailures , & server.IndexedVerificationFailure {
392
+ Index : i ,
393
+ Message : "Incorrect attestation signature: " + err .Error (),
394
+ })
395
+ continue
463
396
}
397
+ validAttestations = append (validAttestations , att )
464
398
}
465
399
466
400
for i , att := range validAttestations {
0 commit comments