Skip to content

Commit e020907

Browse files
Revert "SSZ support for submitAttestationsV2 (#15422)" (#15426)
This reverts commit 9927cea.
1 parent 9927cea commit e020907

File tree

4 files changed

+77
-367
lines changed

4 files changed

+77
-367
lines changed

beacon-chain/rpc/endpoints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ func (s *Service) beaconEndpoints(
694694
template: "/eth/v2/beacon/pool/attestations",
695695
name: namespace + ".SubmitAttestationsV2",
696696
middleware: []middleware.Middleware{
697-
middleware.ContentTypeHandler([]string{api.JsonMediaType, api.OctetStreamMediaType}),
697+
middleware.ContentTypeHandler([]string{api.JsonMediaType}),
698698
middleware.AcceptHeaderHandler([]string{api.JsonMediaType}),
699699
},
700700
handler: server.SubmitAttestationsV2,

beacon-chain/rpc/eth/beacon/handlers_pool.go

Lines changed: 73 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,18 @@ func (s *Server) SubmitAttestations(w http.ResponseWriter, r *http.Request) {
183183
ctx, span := trace.StartSpan(r.Context(), "beacon.SubmitAttestations")
184184
defer span.End()
185185

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)
187198
if err != nil {
188199
httputil.HandleError(w, err.Error(), http.StatusBadRequest)
189200
return
@@ -225,13 +236,24 @@ func (s *Server) SubmitAttestationsV2(w http.ResponseWriter, r *http.Request) {
225236
return
226237
}
227238

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+
228250
var attFailures []*server.IndexedVerificationFailure
229251
var failedBroadcasts []string
230252

231253
if v >= version.Electra {
232-
attFailures, failedBroadcasts, err = s.handleAttestationsElectra(ctx, r)
254+
attFailures, failedBroadcasts, err = s.handleAttestationsElectra(ctx, req.Data)
233255
} else {
234-
attFailures, failedBroadcasts, err = s.handleAttestations(ctx, r)
256+
attFailures, failedBroadcasts, err = s.handleAttestations(ctx, req.Data)
235257
}
236258
if err != nil {
237259
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) {
259281

260282
func (s *Server) handleAttestationsElectra(
261283
ctx context.Context,
262-
r *http.Request,
284+
data json.RawMessage,
263285
) (attFailures []*server.IndexedVerificationFailure, failedBroadcasts []string, err error) {
264-
var validAttestations []*eth.SingleAttestation
286+
var sourceAttestations []*structs.SingleAttestation
265287
currentEpoch := slots.ToEpoch(s.TimeFetcher.CurrentSlot())
266288
if currentEpoch < params.BeaconConfig().ElectraForkEpoch {
267289
return nil, nil, errors.Errorf("electra attestations have not been enabled, current epoch %d enabled epoch %d", currentEpoch, params.BeaconConfig().ElectraForkEpoch)
268290
}
269291

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+
}
313295

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+
}
317299

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
320309
}
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
339316
}
317+
validAttestations = append(validAttestations, att)
340318
}
341319

342320
for i, singleAtt := range validAttestations {
@@ -384,83 +362,39 @@ func (s *Server) handleAttestationsElectra(
384362
return attFailures, failedBroadcasts, nil
385363
}
386364

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
389367

390368
if slots.ToEpoch(s.TimeFetcher.CurrentSlot()) >= params.BeaconConfig().ElectraForkEpoch {
391369
return nil, nil, errors.New("old attestation format, only electra attestations should be sent")
392370
}
393371

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+
}
437375

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+
}
441379

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
444389
}
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
463396
}
397+
validAttestations = append(validAttestations, att)
464398
}
465399

466400
for i, att := range validAttestations {

0 commit comments

Comments
 (0)