Skip to content

Commit

Permalink
Update electra attestation and submitattestations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bez625 committed Jan 9, 2025
1 parent faf577a commit ab4175b
Show file tree
Hide file tree
Showing 16 changed files with 365 additions and 293 deletions.
75 changes: 66 additions & 9 deletions http/submitattestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,92 @@ import (
"context"
"encoding/json"
"errors"
"strings"

client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/attestantio/go-eth2-client/spec"
)

// SubmitAttestations submits attestations.
func (s *Service) SubmitAttestations(ctx context.Context, attestations []*phase0.Attestation) error {
// SubmitAttestations submits versioned attestations.
func (s *Service) SubmitAttestations(ctx context.Context, opts *api.SubmitAttestationsOpts) error {
if err := s.assertIsSynced(ctx); err != nil {
return err
}
if opts == nil {
return client.ErrNoOptions
}
if len(opts.Attestations) == 0 {
return errors.Join(errors.New("no attestations supplied"), client.ErrInvalidOptions)
}
attestations := opts.Attestations
unversionedAttestations, err := createUnversionedAttestations(attestations)
if err != nil {
return err
}

specJSON, err := json.Marshal(attestations)
specJSON, err := json.Marshal(unversionedAttestations)
if err != nil {
return errors.Join(errors.New("failed to marshal JSON"), err)
}

endpoint := "/eth/v1/beacon/pool/attestations"
endpoint := "/eth/v2/beacon/pool/attestations"
query := ""

if _, err := s.post(ctx,
headers := make(map[string]string)
headers["Eth-Consensus-Version"] = strings.ToLower(attestations[0].Version.String())
if _, err = s.post(ctx,
endpoint,
query,
&api.CommonOpts{},
&opts.Common,
bytes.NewReader(specJSON),
ContentTypeJSON,
map[string]string{},
headers,
); err != nil {
return errors.Join(errors.New("failed to submit beacon attestations"), err)
return errors.Join(errors.New("failed to submit versioned beacon attestations"), err)
}

return nil
}

func createUnversionedAttestations(attestations []*spec.VersionedAttestation) ([]any, error) {
var version *spec.DataVersion
var unversionedAttestations []any

for i := range attestations {
if attestations[i] == nil {
return nil, errors.Join(errors.New("nil attestation version supplied"), client.ErrInvalidOptions)
}

// Ensure consistent versioning.
if version == nil {
version = &attestations[i].Version
} else if *version != attestations[i].Version {
return nil, errors.Join(errors.New("attestations must all be of the same version"), client.ErrInvalidOptions)
}

// Append to unversionedAttestations.
switch attestations[i].Version {
case spec.DataVersionPhase0:
unversionedAttestations = append(unversionedAttestations, attestations[i].Phase0)
case spec.DataVersionAltair:
unversionedAttestations = append(unversionedAttestations, attestations[i].Altair)
case spec.DataVersionBellatrix:
unversionedAttestations = append(unversionedAttestations, attestations[i].Bellatrix)
case spec.DataVersionCapella:
unversionedAttestations = append(unversionedAttestations, attestations[i].Capella)
case spec.DataVersionDeneb:
unversionedAttestations = append(unversionedAttestations, attestations[i].Deneb)
case spec.DataVersionElectra:
singleAttestation, err := attestations[i].Electra.ToSingleAttestation()
if err != nil {
return nil, errors.Join(errors.New("failed to convert attestation to single attestation"), err)
}
unversionedAttestations = append(unversionedAttestations, singleAttestation)
default:
return nil, errors.Join(errors.New("unknown attestation version"), client.ErrInvalidOptions)
}
}

return unversionedAttestations, nil
}
9 changes: 8 additions & 1 deletion http/submitattestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package http_test

import (
"context"
"github.com/attestantio/go-eth2-client/spec"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -79,7 +80,13 @@ func TestSubmitAttestations(t *testing.T) {
}),
}

err = service.(client.AttestationsSubmitter).SubmitAttestations(ctx, []*phase0.Attestation{attestation})
versionedAttestations := []*spec.VersionedAttestation{
{Version: spec.DataVersionPhase0, Phase0: attestation},
}
opts := &api.SubmitAttestationsOpts{
Attestations: versionedAttestations,
}
err = service.(client.AttestationsSubmitter).SubmitAttestations(ctx, opts)
switch {
case test.err != "":
require.ErrorContains(t, err, test.err)
Expand Down
93 changes: 0 additions & 93 deletions http/submitversionedattestations.go

This file was deleted.

8 changes: 1 addition & 7 deletions mock/submitattestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ import (
"context"

"github.com/attestantio/go-eth2-client/api"
spec "github.com/attestantio/go-eth2-client/spec/phase0"
)

// SubmitAttestations submits attestations.
func (*Service) SubmitAttestations(_ context.Context, _ []*spec.Attestation) error {
return nil
}

// SubmitVersionedAttestations submits versioned attestations.
func (*Service) SubmitVersionedAttestations(_ context.Context, _ *api.SubmitAttestationsOpts) error {
func (*Service) SubmitAttestations(_ context.Context, _ *api.SubmitAttestationsOpts) error {
return nil
}
6 changes: 3 additions & 3 deletions multi/submitattestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
"strings"

consensusclient "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/attestantio/go-eth2-client/api"
)

// SubmitAttestations submits attestations.
func (s *Service) SubmitAttestations(ctx context.Context,
attestations []*phase0.Attestation,
opts *api.SubmitAttestationsOpts,
) error {
_, err := s.doCall(ctx, func(ctx context.Context, client consensusclient.Service) (any, error) {
err := client.(consensusclient.AttestationsSubmitter).SubmitAttestations(ctx, attestations)
err := client.(consensusclient.AttestationsSubmitter).SubmitAttestations(ctx, opts)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion multi/submitattestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package multi_test

import (
"context"
"github.com/attestantio/go-eth2-client/api"
"github.com/attestantio/go-eth2-client/spec"
"testing"

consensusclient "github.com/attestantio/go-eth2-client"
Expand Down Expand Up @@ -50,8 +52,14 @@ func TestSubmitAttestations(t *testing.T) {
)
require.NoError(t, err)

versionedAttestations := []*spec.VersionedAttestation{
{Version: spec.DataVersionPhase0, Phase0: &phase0.Attestation{}},
}
opts := &api.SubmitAttestationsOpts{
Attestations: versionedAttestations,
}
for i := 0; i < 128; i++ {
err := multiClient.(consensusclient.AttestationsSubmitter).SubmitAttestations(ctx, []*phase0.Attestation{})
err := multiClient.(consensusclient.AttestationsSubmitter).SubmitAttestations(ctx, opts)
require.NoError(t, err)
}
// At this point we expect mock 3 to be in active (unless probability hates us).
Expand Down
62 changes: 0 additions & 62 deletions multi/submitversionedattestations.go

This file was deleted.

Loading

0 comments on commit ab4175b

Please sign in to comment.