Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
pull-requests: read
jobs:
golangci:
name: lint
Expand All @@ -32,7 +32,7 @@ jobs:
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
Expand Down
22 changes: 19 additions & 3 deletions pkg/beacon/checkpoints/majority/majority.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,28 @@ func New() *Decider {
}

func (m *Decider) Decide(checkpoints []*v1.Finality) (*v1.Finality, error) {
// Checkpoints with missing fields can't be keyed and don't count towards
// the majority threshold, matching how upstreams that fail to return
// finality at all are treated.
valid := make([]*v1.Finality, 0, len(checkpoints))

for _, checkpoint := range checkpoints {
if checkpoint == nil ||
checkpoint.Finalized == nil ||
checkpoint.Justified == nil ||
checkpoint.PreviousJustified == nil {
continue
}

valid = append(valid, checkpoint)
}

common := make(map[string]struct {
Finality *v1.Finality
Count int
})
}, len(valid))

for _, checkpoint := range checkpoints {
for _, checkpoint := range valid {
key := eth.RootAsString(checkpoint.Finalized.Root) + "-" +
eth.RootAsString(checkpoint.Justified.Root) + "-" +
eth.RootAsString(checkpoint.PreviousJustified.Root)
Expand All @@ -46,7 +62,7 @@ func (m *Decider) Decide(checkpoints []*v1.Finality) (*v1.Finality, error) {
}

for _, v := range common {
if v.Count > len(checkpoints)/2 {
if v.Count > len(valid)/2 {
return v.Finality, nil
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/beacon/checkpoints/majority/majority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,48 @@ func TestSplitMajority(t *testing.T) {
t.Errorf("Expected %v, got %v", ErrNoMajorityFound, err)
}
}

func TestNilFinalityIgnored(t *testing.T) {
payload := []*v1.Finality{
nil,
finalityA,
finalityA,
nil,
}

finality, err := majority.Decide(payload)
if err != nil {
t.Fatal(err)
}

if finality.Finalized.Root != finalityA.Finalized.Root {
t.Errorf("Expected %v, got %v", finalityA, finality)
}
}

func TestNilCheckpointFieldsIgnored(t *testing.T) {
payload := []*v1.Finality{
{Finalized: nil, Justified: checkpointB, PreviousJustified: checkpointB},
{Finalized: checkpointA, Justified: nil, PreviousJustified: checkpointB},
{Finalized: checkpointA, Justified: checkpointB, PreviousJustified: nil},
finalityA,
}

finality, err := majority.Decide(payload)
if err != nil {
t.Fatal(err)
}

if finality.Finalized.Root != finalityA.Finalized.Root {
t.Errorf("Expected %v, got %v", finalityA, finality)
}
}

func TestAllNilNoMajority(t *testing.T) {
payload := []*v1.Finality{nil, nil}

_, err := majority.Decide(payload)
if err != ErrNoMajorityFound {
t.Errorf("Expected %v, got %v", ErrNoMajorityFound, err)
}
}
9 changes: 9 additions & 0 deletions pkg/beacon/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,15 @@ func (d *Default) checkFinality(ctx context.Context) error {
continue
}

if finality == nil ||
finality.Finalized == nil ||
finality.Justified == nil ||
finality.PreviousJustified == nil {
d.log.Infof("Node %s returned incomplete finality", node.Config.Name)

continue
}

aggFinality = append(aggFinality, finality)
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/checkpointz/checkpointz.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ func (s *Server) Start(ctx context.Context) error {

router := httprouter.New()

router.PanicHandler = func(w http.ResponseWriter, r *http.Request, rcv any) {
s.log.
WithField("panic", rcv).
WithField("path", r.URL.Path).
Error("Recovered from panic while handling request")

if err := api.WriteErrorResponse(w, "internal server error", http.StatusInternalServerError); err != nil {
s.log.WithError(err).Error("Failed to write panic error response")
}
}

if err := s.http.Register(ctx, router); err != nil {
return err
}
Expand Down
Loading