From 5608aaa67b9ff2e5ee41280c6bd447fe4531139b Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Sat, 24 Aug 2024 17:41:36 +0100 Subject: [PATCH] Allow filtering of attestationpool by committee index. --- api/attestationpoolopts.go | 11 ++++++++--- http/attestationpool.go | 16 +++++++++++++--- http/http.go | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/attestationpoolopts.go b/api/attestationpoolopts.go index 57349fd0..9994e109 100644 --- a/api/attestationpoolopts.go +++ b/api/attestationpoolopts.go @@ -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 @@ -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 } diff --git a/http/attestationpool.go b/http/attestationpool.go index 8ec46e90..d9d968ef 100644 --- a/http/attestationpool.go +++ b/http/attestationpool.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "strings" client "github.com/attestantio/go-eth2-client" "github.com/attestantio/go-eth2-client/api" @@ -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 } @@ -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 diff --git a/http/http.go b/http/http.go index 109fb801..9a899e2a 100644 --- a/http/http.go +++ b/http/http.go @@ -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) {