Skip to content

Commit

Permalink
Allow filtering of attestationpool by committee index.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Aug 24, 2024
1 parent 490d07a commit 5608aaa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
11 changes: 8 additions & 3 deletions api/attestationpoolopts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Attestant Limited.
// Copyright © 2023, 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -19,6 +19,11 @@ import "github.com/attestantio/go-eth2-client/spec/phase0"
type AttestationPoolOpts struct {
Common CommonOpts

// Slot is the slot for which the data is obtained.
Slot phase0.Slot
// Slot is the slot for which the data is obtained. If not present then
// data for all slots will be obtained.
Slot *phase0.Slot

// CommmitteeIndex is the committee index for which the data is obtained.
// If not present then data for all committee indices will be obtained.
CommitteeIndex *phase0.CommitteeIndex
}
16 changes: 13 additions & 3 deletions http/attestationpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"strings"

client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
Expand All @@ -39,8 +40,14 @@ func (s *Service) AttestationPool(ctx context.Context,
}

endpoint := "/eth/v1/beacon/pool/attestations"
query := fmt.Sprintf("slot=%d", opts.Slot)
httpResponse, err := s.get(ctx, endpoint, query, &opts.Common, false)
queryItems := make([]string, 0)
if opts.Slot != nil {
queryItems = append(queryItems, fmt.Sprintf("slot=%d", *opts.Slot))
}
if opts.CommitteeIndex != nil {
queryItems = append(queryItems, fmt.Sprintf("committee_index=%d", *opts.CommitteeIndex))
}
httpResponse, err := s.get(ctx, endpoint, strings.Join(queryItems, "&"), &opts.Common, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -77,9 +84,12 @@ func (*Service) attestationPoolFromJSON(_ context.Context,

func verifyAttestationPool(opts *api.AttestationPoolOpts, data []*phase0.Attestation) error {
for _, datum := range data {
if datum.Data.Slot != opts.Slot {
if opts.Slot != nil && datum.Data.Slot != *opts.Slot {
return errors.New("attestation data not for requested slot")
}
if opts.CommitteeIndex != nil && datum.Data.Index != *opts.CommitteeIndex {
return errors.New("attestation data not for requested committee index")
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

// defaultUserAgent is sent with requests if no other user agent has been supplied.
const defaultUserAgent = "go-eth2-client/0.21.10"
const defaultUserAgent = "go-eth2-client/0.21.11"

// post sends an HTTP post request and returns the body.
func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io.Reader, error) {
Expand Down

0 comments on commit 5608aaa

Please sign in to comment.