Skip to content

Commit

Permalink
Use spec to obtain fork version
Browse files Browse the repository at this point in the history
  • Loading branch information
Bez625 committed Jan 27, 2025
1 parent 251e60f commit cd3f071
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
3 changes: 0 additions & 3 deletions api/attestationdataopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package api

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

Expand All @@ -26,6 +25,4 @@ type AttestationDataOpts struct {
Slot phase0.Slot
// CommitteeIndex is the committee index for which the data is obtained.
CommitteeIndex phase0.CommitteeIndex
// Version is the version of the chain we are using. Used to control fork specific logic.
Version spec.DataVersion
}
35 changes: 30 additions & 5 deletions http/attestationdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

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

Expand Down Expand Up @@ -54,7 +53,7 @@ func (s *Service) AttestationData(ctx context.Context,
}
}

func (*Service) attestationDataFromJSON(_ context.Context,
func (s *Service) attestationDataFromJSON(ctx context.Context,
opts *api.AttestationDataOpts,
httpResponse *httpResponse,
) (
Expand All @@ -66,7 +65,7 @@ func (*Service) attestationDataFromJSON(_ context.Context,
return nil, err
}

if err := verifyAttestationData(opts, &data); err != nil {
if err := s.verifyAttestationData(ctx, opts, &data); err != nil {
return nil, err
}

Expand All @@ -76,16 +75,22 @@ func (*Service) attestationDataFromJSON(_ context.Context,
}, nil
}

func verifyAttestationData(opts *api.AttestationDataOpts, data *phase0.AttestationData) error {
func (s *Service) verifyAttestationData(ctx context.Context, opts *api.AttestationDataOpts, data *phase0.AttestationData) error {
if data.Slot != opts.Slot {
return errors.Join(
fmt.Errorf("attestation data for slot %d; expected %d", data.Slot, opts.Slot),
client.ErrInconsistentResult,
)
}

electraSlot, err := s.calculateElectraSlot(ctx)
if err != nil {
return errors.Join(errors.New("failed to calculate electra slot"), err)
}

// When in the electra era the data.Index is hardcoded to 0.
index := opts.CommitteeIndex
if opts.Version >= spec.DataVersionElectra {
if opts.Slot >= electraSlot {
index = 0
}
if data.Index != index {
Expand All @@ -97,3 +102,23 @@ func verifyAttestationData(opts *api.AttestationDataOpts, data *phase0.Attestati

return nil
}

func (s *Service) calculateElectraSlot(ctx context.Context) (phase0.Slot, error) {
response, err := s.Spec(ctx, &api.SpecOpts{})
if err != nil {
return 0, err
}
slotsPerEpoch, isCorrectType := response.Data["SLOTS_PER_EPOCH"].(uint64)
if !isCorrectType {
return 0, ErrIncorrectType
}

electraEpoch, isCorrectType := response.Data["ELECTRA_FORK_EPOCH"].(uint64)
if !isCorrectType {
return 0, ErrIncorrectType
}

electraSlot := phase0.Slot(slotsPerEpoch * electraEpoch)

return electraSlot, nil
}

0 comments on commit cd3f071

Please sign in to comment.