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

Slasher: Fixes double votes false positive and attester slashings duplication. #13596

Merged
merged 5 commits into from
Feb 12, 2024
Merged
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
8 changes: 4 additions & 4 deletions beacon-chain/db/slasherkv/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func (s *Store) CheckAttesterDoubleVotes(

// Build the proof of double vote.
slashAtt := &slashertypes.AttesterDoubleVote{
ValidatorIndex: primitives.ValidatorIndex(valIdx),
Target: attToProcess.IndexedAttestation.Data.Target.Epoch,
PrevAttestationWrapper: existingAttRecord,
AttestationWrapper: attToProcess,
ValidatorIndex: primitives.ValidatorIndex(valIdx),
Target: attToProcess.IndexedAttestation.Data.Target.Epoch,
Wrapper_1: existingAttRecord,
Wrapper_2: attToProcess,
}

localDoubleVotes = append(localDoubleVotes, slashAtt)
Expand Down
36 changes: 18 additions & 18 deletions beacon-chain/db/slasherkv/slasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,28 @@ func TestStore_CheckAttesterDoubleVotes(t *testing.T) {

wanted := []*slashertypes.AttesterDoubleVote{
{
ValidatorIndex: 0,
Target: 3,
PrevAttestationWrapper: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{1}),
AttestationWrapper: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{2}),
ValidatorIndex: 0,
Target: 3,
Wrapper_1: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{1}),
Wrapper_2: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{2}),
},
{
ValidatorIndex: 1,
Target: 3,
PrevAttestationWrapper: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{1}),
AttestationWrapper: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{2}),
ValidatorIndex: 1,
Target: 3,
Wrapper_1: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{1}),
Wrapper_2: createAttestationWrapper(2, 3, []uint64{0, 1}, []byte{2}),
},
{
ValidatorIndex: 2,
Target: 4,
PrevAttestationWrapper: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{3}),
AttestationWrapper: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{4}),
ValidatorIndex: 2,
Target: 4,
Wrapper_1: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{3}),
Wrapper_2: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{4}),
},
{
ValidatorIndex: 3,
Target: 4,
PrevAttestationWrapper: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{3}),
AttestationWrapper: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{4}),
ValidatorIndex: 3,
Target: 4,
Wrapper_1: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{3}),
Wrapper_2: createAttestationWrapper(3, 4, []uint64{2, 3}, []byte{4}),
},
}
doubleVotes, err := beaconDB.CheckAttesterDoubleVotes(ctx, slashableAtts)
Expand All @@ -126,8 +126,8 @@ func TestStore_CheckAttesterDoubleVotes(t *testing.T) {
for i, double := range doubleVotes {
require.DeepEqual(t, wanted[i].ValidatorIndex, double.ValidatorIndex)
require.DeepEqual(t, wanted[i].Target, double.Target)
require.DeepEqual(t, wanted[i].PrevAttestationWrapper, double.PrevAttestationWrapper)
require.DeepEqual(t, wanted[i].AttestationWrapper, double.AttestationWrapper)
require.DeepEqual(t, wanted[i].Wrapper_1, double.Wrapper_1)
require.DeepEqual(t, wanted[i].Wrapper_2, double.Wrapper_2)
}
}

Expand Down
63 changes: 43 additions & 20 deletions beacon-chain/slasher/chunks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slasher

import (
"bytes"
"context"
"fmt"
"math"
Expand Down Expand Up @@ -186,10 +187,10 @@ func (m *MinSpanChunksSlice) CheckSlashable(
ctx context.Context,
slasherDB db.SlasherDatabase,
validatorIdx primitives.ValidatorIndex,
attestation *slashertypes.IndexedAttestationWrapper,
incomingAttWrapper *slashertypes.IndexedAttestationWrapper,
) (*ethpb.AttesterSlashing, error) {
sourceEpoch := attestation.IndexedAttestation.Data.Source.Epoch
targetEpoch := attestation.IndexedAttestation.Data.Target.Epoch
sourceEpoch := incomingAttWrapper.IndexedAttestation.Data.Source.Epoch
targetEpoch := incomingAttWrapper.IndexedAttestation.Data.Target.Epoch

minTarget, err := chunkDataAtEpoch(m.params, m.data, validatorIdx, sourceEpoch)
if err != nil {
Expand All @@ -204,12 +205,12 @@ func (m *MinSpanChunksSlice) CheckSlashable(
}

// The incoming attestation surrounds an existing one.
existingAttRecord, err := slasherDB.AttestationRecordForValidator(ctx, validatorIdx, minTarget)
existingAttWrapper, err := slasherDB.AttestationRecordForValidator(ctx, validatorIdx, minTarget)
if err != nil {
return nil, errors.Wrapf(err, "could not get existing attestation record at target %d", minTarget)
}

if existingAttRecord == nil {
if existingAttWrapper == nil {
// This case should normally not happen. If this happen, it means we previously
// recorded in our min/max DB an distance corresponding to an attestaiton, but WITHOUT
// recording the attestation itself. As a consequence, we say there is no surrounding vote,
Expand All @@ -223,7 +224,7 @@ func (m *MinSpanChunksSlice) CheckSlashable(
return nil, nil
}

if existingAttRecord.IndexedAttestation.Data.Source.Epoch <= sourceEpoch {
if existingAttWrapper.IndexedAttestation.Data.Source.Epoch <= sourceEpoch {
// This case should normally not happen, since if we have targetEpoch > minTarget,
// then there is at least one attestation we surround.
// However, it can happens if we have multiple attestation with the same target
Expand All @@ -233,10 +234,21 @@ func (m *MinSpanChunksSlice) CheckSlashable(
}

surroundingVotesTotal.Inc()
return &ethpb.AttesterSlashing{
Attestation_1: attestation.IndexedAttestation,
Attestation_2: existingAttRecord.IndexedAttestation,
}, nil

slashing := &ethpb.AttesterSlashing{
Attestation_1: existingAttWrapper.IndexedAttestation,
Attestation_2: incomingAttWrapper.IndexedAttestation,
}

// Ensure the attestation with the lower data root is the first attestation.
if bytes.Compare(existingAttWrapper.DataRoot[:], incomingAttWrapper.DataRoot[:]) > 0 {
slashing = &ethpb.AttesterSlashing{
Attestation_1: incomingAttWrapper.IndexedAttestation,
Attestation_2: existingAttWrapper.IndexedAttestation,
}
}

return slashing, nil
}

// CheckSlashable takes in a validator index and an incoming attestation
Expand All @@ -254,10 +266,10 @@ func (m *MaxSpanChunksSlice) CheckSlashable(
ctx context.Context,
slasherDB db.SlasherDatabase,
validatorIdx primitives.ValidatorIndex,
attestation *slashertypes.IndexedAttestationWrapper,
incomingAttWrapper *slashertypes.IndexedAttestationWrapper,
) (*ethpb.AttesterSlashing, error) {
sourceEpoch := attestation.IndexedAttestation.Data.Source.Epoch
targetEpoch := attestation.IndexedAttestation.Data.Target.Epoch
sourceEpoch := incomingAttWrapper.IndexedAttestation.Data.Source.Epoch
targetEpoch := incomingAttWrapper.IndexedAttestation.Data.Target.Epoch

maxTarget, err := chunkDataAtEpoch(m.params, m.data, validatorIdx, sourceEpoch)
if err != nil {
Expand All @@ -272,12 +284,12 @@ func (m *MaxSpanChunksSlice) CheckSlashable(
}

// The incoming attestation is surrounded by an existing one.
existingAttRecord, err := slasherDB.AttestationRecordForValidator(ctx, validatorIdx, maxTarget)
existingAttWrapper, err := slasherDB.AttestationRecordForValidator(ctx, validatorIdx, maxTarget)
if err != nil {
return nil, errors.Wrapf(err, "could not get existing attestation record at target %d", maxTarget)
}

if existingAttRecord == nil {
if existingAttWrapper == nil {
// This case should normally not happen. If this happen, it means we previously
// recorded in our min/max DB an distance corresponding to an attestaiton, but WITHOUT
// recording the attestation itself. As a consequence, we say there is no surrounded vote,
Expand All @@ -291,7 +303,7 @@ func (m *MaxSpanChunksSlice) CheckSlashable(
return nil, nil
}

if existingAttRecord.IndexedAttestation.Data.Source.Epoch >= sourceEpoch {
if existingAttWrapper.IndexedAttestation.Data.Source.Epoch >= sourceEpoch {
// This case should normally not happen, since if we have targetEpoch < maxTarget,
// then there is at least one attestation that surrounds us.
// However, it can happens if we have multiple attestation with the same target
Expand All @@ -301,10 +313,21 @@ func (m *MaxSpanChunksSlice) CheckSlashable(
}

surroundedVotesTotal.Inc()
return &ethpb.AttesterSlashing{
Attestation_1: existingAttRecord.IndexedAttestation,
Attestation_2: attestation.IndexedAttestation,
}, nil

slashing := &ethpb.AttesterSlashing{
Attestation_1: existingAttWrapper.IndexedAttestation,
Attestation_2: incomingAttWrapper.IndexedAttestation,
}

// Ensure the attestation with the lower data root is the first attestation.
if bytes.Compare(existingAttWrapper.DataRoot[:], incomingAttWrapper.DataRoot[:]) > 0 {
slashing = &ethpb.AttesterSlashing{
Attestation_1: incomingAttWrapper.IndexedAttestation,
Attestation_2: existingAttWrapper.IndexedAttestation,
}
}

return slashing, nil
}

// Update a min span chunk for a validator index starting at the current epoch, e_c, then updating
Expand Down
Loading
Loading