Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/bcast: make lighthouse submit attestations idempotent #813

Merged
merged 4 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions core/bcast/bcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package bcast

import (
"context"
"strings"
"time"

eth2client "github.com/attestantio/go-eth2-client"
Expand Down Expand Up @@ -81,6 +82,12 @@ func (b Broadcaster) Broadcast(ctx context.Context, duty core.Duty,
}

err = b.eth2Cl.SubmitAttestations(ctx, []*eth2p0.Attestation{&att.Attestation})
if err != nil && (strings.Contains(err.Error(), "PriorAttestationKnown") ||
strings.Contains(err.Error(), "No peers on libp2p topic")) {
Copy link
Contributor Author

@corverroos corverroos Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Lighthouse and Nimbus aren't idempotent, so just swallow this non-issue.
// See reference github.com/attestantio/[email protected]/multi/submitattestations.go:38
err = nil
}
if err == nil {
log.Info(ctx, "Attestation successfully submitted to beacon node",
z.Any("delay", b.delayFunc(duty.Slot)),
Expand Down
26 changes: 20 additions & 6 deletions core/bcast/bcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/stretchr/testify/require"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/core/bcast"
"github.com/obolnetwork/charon/testutil"
Expand All @@ -31,27 +32,40 @@ import (

func TestBroadcastAttestation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

mock, err := beaconmock.New()
require.NoError(t, err)

aggData := core.Attestation{Attestation: *testutil.RandomAttestation()}

// Assert output and cancel context
// Assert output and return lighthouse and nimbus known error on duplicates
var submitted int
mock.SubmitAttestationsFunc = func(ctx context.Context, attestations []*eth2p0.Attestation) error {
require.Len(t, attestations, 1)
require.Equal(t, aggData.Attestation, *attestations[0])
cancel()

return ctx.Err()
submitted++
if submitted > 2 {
// Non-idempotent error returned by lighthouse but swallowed by bcast.
return errors.New("Verification: PriorAttestationKnown")
} else if submitted > 1 {
// Non-idempotent error returned by nimbus but swallowed by bcast.
return errors.New(`"message":"No peers on libp2p topic"`)
}

return nil
}

bcaster, err := bcast.New(ctx, mock)
require.NoError(t, err)

err = bcaster.Broadcast(ctx, core.Duty{Type: core.DutyAttester}, "", aggData)
require.ErrorIs(t, err, context.Canceled)

<-ctx.Done()
require.NoError(t, err)
err = bcaster.Broadcast(ctx, core.Duty{Type: core.DutyAttester}, "", aggData)
require.NoError(t, err)
err = bcaster.Broadcast(ctx, core.Duty{Type: core.DutyAttester}, "", aggData)
require.NoError(t, err)
}

func TestBroadcastBeaconBlock(t *testing.T) {
Expand Down