Skip to content

Commit

Permalink
Handle empty blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Jan 17, 2024
1 parent 6ad8e7a commit faf3c8a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.35.1:
- fix output for various commands that may encounter an empty slot

1.35.0:
- support Deneb
- add start and end dates for eth1votes period
Expand Down
30 changes: 20 additions & 10 deletions cmd/attester/inclusion/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"context"
"fmt"
"net/http"

eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
Expand Down Expand Up @@ -66,6 +67,11 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
Block: fmt.Sprintf("%d", slot),
})
if err != nil {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
// No block for this slot, that's fine.
continue
}
return nil, errors.Wrap(err, "failed to obtain block")
}
block := blockResponse.Data
Expand Down Expand Up @@ -129,13 +135,15 @@ func calcHeadCorrect(ctx context.Context, data *dataIn, attestation *phase0.Atte
Block: fmt.Sprintf("%d", slot),
})
if err != nil {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
// No block.
slot--
continue
}

return false, err
}
if response.Data == nil {
// No block.
slot--
continue
}
if !response.Data.Canonical {
// Not canonical.
slot--
Expand All @@ -153,13 +161,15 @@ func calcTargetCorrect(ctx context.Context, data *dataIn, attestation *phase0.At
Block: fmt.Sprintf("%d", slot),
})
if err != nil {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
// No block.
slot--
continue
}

return false, err
}
if response.Data == nil {
// No block.
slot--
continue
}
if !response.Data.Canonical {
// Not canonical.
slot--
Expand Down
5 changes: 4 additions & 1 deletion cmd/block/info/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -80,12 +81,14 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
})
if err != nil {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == 404 {
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
if data.quiet {
os.Exit(1)
}

return nil, errors.New("empty beacon block")
}

return nil, errors.Wrap(err, "failed to obtain beacon block")
}
block := blockResponse.Data
Expand Down
7 changes: 7 additions & 0 deletions cmd/epoch/summary/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package epochsummary
import (
"context"
"fmt"
"net/http"
"sort"

eth2client "github.com/attestantio/go-eth2-client"
Expand Down Expand Up @@ -411,6 +412,12 @@ func (c *command) fetchBlock(ctx context.Context,
Block: blockID,
})
if err != nil {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
// No block for this slot, that's okay.
return nil, nil
}

return nil, errors.Wrap(err, "failed to fetch block")
}
block = blockResponse.Data
Expand Down
9 changes: 8 additions & 1 deletion cmd/synccommittee/inclusion/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package inclusion
import (
"context"
"fmt"
"net/http"

eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
Expand Down Expand Up @@ -76,8 +77,14 @@ func (c *command) process(ctx context.Context) error {
Block: fmt.Sprintf("%d", slot),
})
if err != nil {
return err
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
c.inclusions = append(c.inclusions, 0)
continue
}
return errors.Wrap(err, "failed to obtain beacon block")
}

block := blockResponse.Data
if block == nil {
c.inclusions = append(c.inclusions, 0)
Expand Down
13 changes: 12 additions & 1 deletion cmd/validator/summary/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package validatorsummary
import (
"context"
"fmt"
"net/http"
"sort"

eth2client "github.com/attestantio/go-eth2-client"
Expand Down Expand Up @@ -96,6 +97,11 @@ func (c *command) processProposerDuties(ctx context.Context) error {
Block: fmt.Sprintf("%d", duty.Slot),
})
if err != nil {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
return nil
}

return errors.Wrap(err, fmt.Sprintf("failed to obtain block for slot %d", duty.Slot))
}
block := blockResponse.Data
Expand Down Expand Up @@ -224,7 +230,12 @@ func (c *command) processAttesterDutiesSlot(ctx context.Context,
Block: fmt.Sprintf("%d", slot),
})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to obtain block for slot %d", slot))
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
return nil
}

return errors.Wrap(err, "failed to obtain beacon block")
}
block := blockResponse.Data
attestations, err := block.Attestations()
Expand Down
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2019 - 2023 Weald Technology Trading.
// Copyright © 2019 - 2024 Weald Technology Trading.
// 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,7 +24,7 @@ import (

// ReleaseVersion is the release version of the codebase.
// Usually overridden by tag names when building binaries.
var ReleaseVersion = "local build (latest release 1.35.0)"
var ReleaseVersion = "local build (latest release 1.35.1)"

// versionCmd represents the version command.
var versionCmd = &cobra.Command{
Expand Down
15 changes: 10 additions & 5 deletions util/beaconheadercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ package util

import (
"context"
"errors"
"fmt"
"net/http"

eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
Expand Down Expand Up @@ -53,18 +55,21 @@ func (b *BeaconBlockHeaderCache) Fetch(ctx context.Context,
if !exists {
response, err := b.beaconBlockHeadersProvider.BeaconBlockHeader(ctx, &api.BeaconBlockHeaderOpts{Block: fmt.Sprintf("%d", slot)})
if err != nil {
return nil, err
}
if response.Data == nil {
entry = &beaconBlockHeaderEntry{
present: false,
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
entry = &beaconBlockHeaderEntry{
present: false,
}
} else {
return nil, err
}
} else {
entry = &beaconBlockHeaderEntry{
present: true,
value: response.Data,
}
}

b.entries[slot] = entry
}
return entry.value, nil
Expand Down

0 comments on commit faf3c8a

Please sign in to comment.