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

Fix Attester Slashing Validation In Gossip #12295

Merged
merged 10 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions beacon-chain/sync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ go_library(
"//beacon-chain/core/signing:go_default_library",
"//beacon-chain/core/transition:go_default_library",
"//beacon-chain/core/transition/interop:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/db/filters:go_default_library",
"//beacon-chain/execution:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/sync/validate_attester_slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/blocks"
v "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/v4/container/slice"
"github.com/prysmaticlabs/prysm/v4/monitoring/tracing"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (s *Service) validateAttesterSlashing(ctx context.Context, pid peer.ID, msg
if err != nil {
return pubsub.ValidationIgnore, err
}
if err := blocks.VerifyAttesterSlashing(ctx, headState, slashing); err != nil {
if _, err := blocks.ProcessAttesterSlashing(ctx, headState, slashing, v.SlashValidator); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This mutates a beacon state and slashes a validator in vain, since the function VerifyAttesterSlashing anyway is called in a few other places (like RPC methods) and in particular is called within ProcessAttesterSlashing I believe that the right thing to do would be to perform the extra check within VerifyAttesterSlashing and if performance is an issue, this function can be refactored to return the validators that need to be slashed in the state (so that ProcessAttesterSlashing can use them)

return pubsub.ValidationReject, err
}

Expand Down
43 changes: 43 additions & 0 deletions beacon-chain/sync/validate_attester_slashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,49 @@ func TestValidateAttesterSlashing_ValidSlashing(t *testing.T) {
assert.NotNil(t, msg.ValidatorData, "Decoded message was not set on the message validator data")
}

func TestValidateAttesterSlashing_InvalidSlashing_WithdrawableEpoch(t *testing.T) {
p := p2ptest.NewTestP2P(t)
ctx := context.Background()

slashing, s := setupValidAttesterSlashing(t)
// Set all validators as withdrawn
vals := s.Validators()
for _, vv := range vals {
vv.WithdrawableEpoch = primitives.Epoch(1)
}
require.NoError(t, s.SetValidators(vals))

r := &Service{
cfg: &config{
p2p: p,
chain: &mock.ChainService{State: s, Genesis: time.Now()},
initialSync: &mockSync.Sync{IsSyncing: false},
},
seenAttesterSlashingCache: make(map[uint64]bool),
subHandler: newSubTopicHandler(),
}

buf := new(bytes.Buffer)
_, err := p.Encoding().EncodeGossip(buf, slashing)
require.NoError(t, err)

topic := p2p.GossipTypeMapping[reflect.TypeOf(slashing)]
d, err := r.currentForkDigest()
assert.NoError(t, err)
topic = r.addDigestToTopic(topic, d)
msg := &pubsub.Message{
Message: &pubsubpb.Message{
Data: buf.Bytes(),
Topic: &topic,
},
}
res, err := r.validateAttesterSlashing(ctx, "foobar", msg)
assert.ErrorContains(t, "unable to slash any validator", err)
invalid := res == pubsub.ValidationReject

assert.Equal(t, true, invalid, "Passed Validation")
}

func TestValidateAttesterSlashing_CanFilter(t *testing.T) {
p := p2ptest.NewTestP2P(t)
ctx := context.Background()
Expand Down