Files
prysm/validator/client/beacon-api/attestation_data.go
james-prysm cf94ccbf72 node fallback cleanup (#16316)
**What type of PR is this?**

 Other

**What does this PR do? Why is it needed?**

Follow up to https://github.com/OffchainLabs/prysm/pull/16215 this pr
improves logging, fixes stuttering in package naming, adds additional
unit tests, and deduplicates fallback node code.

**Which issues(s) does this PR fix?**

fixes a potential race if reconnecting to the same host very quickly
which has a stale connection still.

**Other notes for review**

**Acknowledgements**

- [x] I have read
[CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md).
- [x] I have included a uniquely named [changelog fragment
file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd).
- [x] I have added a description with sufficient context for reviewers
to understand this PR.
- [x] I have tested that my changes work as expected and I added a
testing plan to the PR description (if applicable).
2026-02-04 15:59:42 +00:00

97 lines
3.2 KiB
Go

package beacon_api
import (
"context"
"net/url"
"strconv"
"github.com/OffchainLabs/prysm/v7/api/apiutil"
"github.com/OffchainLabs/prysm/v7/api/server/structs"
fieldparams "github.com/OffchainLabs/prysm/v7/config/fieldparams"
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/pkg/errors"
)
func (c *beaconApiValidatorClient) attestationData(
ctx context.Context,
reqSlot primitives.Slot,
reqCommitteeIndex primitives.CommitteeIndex,
) (*ethpb.AttestationData, error) {
params := url.Values{}
params.Add("slot", strconv.FormatUint(uint64(reqSlot), 10))
params.Add("committee_index", strconv.FormatUint(uint64(reqCommitteeIndex), 10))
query := apiutil.BuildURL("/eth/v1/validator/attestation_data", params)
produceAttestationDataResponseJson := structs.GetAttestationDataResponse{}
if err := c.handler.Get(ctx, query, &produceAttestationDataResponseJson); err != nil {
return nil, err
}
if produceAttestationDataResponseJson.Data == nil {
return nil, errors.New("attestation data is nil")
}
attestationData := produceAttestationDataResponseJson.Data
committeeIndex, err := strconv.ParseUint(attestationData.CommitteeIndex, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse attestation committee index: %s", attestationData.CommitteeIndex)
}
beaconBlockRoot, err := bytesutil.DecodeHexWithLength(attestationData.BeaconBlockRoot, fieldparams.RootLength)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode beacon block root: %s", attestationData.BeaconBlockRoot)
}
slot, err := strconv.ParseUint(attestationData.Slot, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse attestation slot: %s", attestationData.Slot)
}
if attestationData.Source == nil {
return nil, errors.New("attestation source is nil")
}
sourceEpoch, err := strconv.ParseUint(attestationData.Source.Epoch, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse attestation source epoch: %s", attestationData.Source.Epoch)
}
sourceRoot, err := bytesutil.DecodeHexWithLength(attestationData.Source.Root, fieldparams.RootLength)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode attestation source root: %s", attestationData.Source.Root)
}
if attestationData.Target == nil {
return nil, errors.New("attestation target is nil")
}
targetEpoch, err := strconv.ParseUint(attestationData.Target.Epoch, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse attestation target epoch: %s", attestationData.Target.Epoch)
}
targetRoot, err := bytesutil.DecodeHexWithLength(attestationData.Target.Root, fieldparams.RootLength)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode attestation target root: %s", attestationData.Target.Root)
}
response := &ethpb.AttestationData{
BeaconBlockRoot: beaconBlockRoot,
CommitteeIndex: primitives.CommitteeIndex(committeeIndex),
Slot: primitives.Slot(slot),
Source: &ethpb.Checkpoint{
Epoch: primitives.Epoch(sourceEpoch),
Root: sourceRoot,
},
Target: &ethpb.Checkpoint{
Epoch: primitives.Epoch(targetEpoch),
Root: targetRoot,
},
}
return response, nil
}