Skip to content

Commit

Permalink
Add call to fetch beacon block blobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Jun 12, 2023
1 parent 9d82b29 commit bfb037b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
51 changes: 51 additions & 0 deletions http/beaconblockblobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright © 2023 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
"context"
"encoding/json"
"fmt"
"sort"

"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/pkg/errors"
)

type beaconBlockBlobsJSON struct {
Data []*deneb.BlobSidecar `json:"data"`
}

// BeaconBlockBlobs fetches the blobs given a block ID.
func (s *Service) BeaconBlockBlobs(ctx context.Context, blockID string) ([]*deneb.BlobSidecar, error) {
respBodyReader, err := s.get(ctx, fmt.Sprintf("/eth/v1/beacon/blob_sidecars/%s", blockID))
if err != nil {
return nil, errors.Wrap(err, "failed to request blobs")
}
if respBodyReader == nil {
return nil, nil
}

var resp beaconBlockBlobsJSON
if err := json.NewDecoder(respBodyReader).Decode(&resp); err != nil {
return nil, errors.Wrap(err, "failed to parse blobs")
}

// Data is not guaranteed to be returned in indx order, so fix that.
sort.Slice(resp.Data, func(i int, j int) bool {
return resp.Data[i].Index < resp.Data[j].Index
})

return resp.Data, nil
}
39 changes: 39 additions & 0 deletions multi/beaconblockblobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2023 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package multi

import (
"context"

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

// BeaconBlockBlobs fetches the blobs given a block ID.
func (s *Service) BeaconBlockBlobs(ctx context.Context, blockID string) ([]*deneb.BlobSidecar, error) {
res, err := s.doCall(ctx, func(ctx context.Context, client consensusclient.Service) (interface{}, error) {
beaconBlockBlobs, err := client.(consensusclient.BeaconBlockBlobsProvider).BeaconBlockBlobs(ctx, blockID)
if err != nil {
return nil, err
}
return beaconBlockBlobs, nil
}, nil)
if err != nil {
return nil, err
}
if res == nil {
return nil, nil
}
return res.([]*deneb.BlobSidecar), nil
}
7 changes: 7 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

Expand Down Expand Up @@ -113,6 +114,12 @@ type SignedBeaconBlockProvider interface {
SignedBeaconBlock(ctx context.Context, blockID string) (*spec.VersionedSignedBeaconBlock, error)
}

// BeaconBlockBlobsProvider is the interface for providing blobs for a given beacon block.
type BeaconBlockBlobsProvider interface {
// BeaconBlockBlobs fetches the blobs given a block ID.
BeaconBlockBlobs(ctx context.Context, blockID string) ([]*deneb.BlobSidecar, error)
}

// BeaconCommitteesProvider is the interface for providing beacon committees.
type BeaconCommitteesProvider interface {
// BeaconCommittees fetches all beacon committees for the epoch at the given state.
Expand Down
13 changes: 13 additions & 0 deletions testclients/erroring.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

Expand Down Expand Up @@ -677,6 +678,18 @@ func (s *Erroring) SignedBeaconBlock(ctx context.Context, blockID string) (*spec
return next.SignedBeaconBlock(ctx, blockID)
}

// BeaconBlockBlobs fetches the blobs given a block ID.
func (s *Erroring) BeaconBlockBlobs(ctx context.Context, blockID string) ([]*deneb.BlobSidecar, error) {
if err := s.maybeError(ctx); err != nil {
return nil, err
}
next, isNext := s.next.(consensusclient.BeaconBlockBlobsProvider)
if !isNext {
return nil, fmt.Errorf("%s@%s does not support this call", s.next.Name(), s.next.Address())
}
return next.BeaconBlockBlobs(ctx, blockID)
}

// BeaconStateRoot fetches a beacon state root given a state ID.
func (s *Erroring) BeaconStateRoot(ctx context.Context, stateID string) (*phase0.Root, error) {
if err := s.maybeError(ctx); err != nil {
Expand Down
13 changes: 12 additions & 1 deletion testclients/sleepy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021, 2022 Attestant Limited.
// Copyright © 2021 - 2023 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 @@ -24,6 +24,7 @@ import (
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

Expand Down Expand Up @@ -455,3 +456,13 @@ func (s *Sleepy) GenesisTime(ctx context.Context) (time.Time, error) {
}
return next.GenesisTime(ctx)
}

// BeaconBlockBlobs fetches the blobs given a block ID.
func (s *Sleepy) BeaconBlockBlobs(ctx context.Context, blockID string) ([]*deneb.BlobSidecar, error) {
s.sleep(ctx)
next, isNext := s.next.(consensusclient.BeaconBlockBlobsProvider)
if !isNext {
return []*deneb.BlobSidecar{}, errors.New("next does not support this call")
}
return next.BeaconBlockBlobs(ctx, blockID)
}

0 comments on commit bfb037b

Please sign in to comment.