Skip to content

Broadcast Proposer Slashing on equivocation #14693

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

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
682677c
Add equivocation detection logic; broadcast slashing immediately on e…
shyam-patel-kira Dec 6, 2024
4861bed
nit: comments
shyam-patel-kira Dec 6, 2024
98f0bc3
Merge branch 'develop' into broadcast-equivocating-blocks
shyam-patel-kira Dec 6, 2024
8858503
Merge branch 'prysmaticlabs:develop' into broadcast-equivocating-blocks
shyam-patel-kira Jan 25, 2025
8d7270e
move equivocation detection to validateBeaconBlockPubSub
shyam-patel-kira Jan 25, 2025
f14cacb
include broadcasting logic within the helper function
shyam-patel-kira Jan 25, 2025
a3797e6
fix lint
shyam-patel-kira Jan 26, 2025
1bfc38e
Add unit tests for equivocation detection
shyam-patel-kira Jan 26, 2025
e915e42
remove comment that are not required
shyam-patel-kira Jan 26, 2025
b8989d3
Add changelog file
shyam-patel-kira Jan 26, 2025
bfa3b9a
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Jan 30, 2025
b9b9cde
Add descriptive comment for detectAndBroadcastEquivocation
shyam-patel-kira Jan 30, 2025
9609bef
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Jan 30, 2025
cdb8935
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Feb 2, 2025
95cb35d
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Feb 3, 2025
7acef84
Merge branch 'broadcast-equivocating-blocks' of https://github.com/sh…
shyam-patel-kira Feb 3, 2025
462f569
use head block instead of block cache for equivocation detection
shyam-patel-kira Feb 3, 2025
1709546
add more equivocation unit tests; update a mock to include HeadState …
shyam-patel-kira Feb 3, 2025
bdec36f
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Feb 6, 2025
d80fb4b
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Feb 7, 2025
38026c4
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Feb 9, 2025
0f174be
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Feb 10, 2025
e6ce574
update the order of the checks
shyam-patel-kira Feb 10, 2025
603073a
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Feb 12, 2025
5b71bba
move slashing before state fetch; update Tests
shyam-patel-kira Feb 12, 2025
32d16e5
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Feb 12, 2025
8da810f
update changelog
shyam-patel-kira Feb 12, 2025
e4a0852
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Feb 20, 2025
a67e289
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Feb 23, 2025
11ea758
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Mar 10, 2025
800918e
use verifyProposerSlashing to verify and reject block; remove verifyS…
shyam-patel-kira Mar 10, 2025
c877071
Update changelog
shyam-patel-kira Mar 10, 2025
4923fd7
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Mar 11, 2025
a09f18b
nit: cleaner error check
shyam-patel-kira Mar 11, 2025
b83ca3f
nit: clean up
shyam-patel-kira Mar 11, 2025
c59a186
revert code logic; update string check; add a unit test
shyam-patel-kira Mar 13, 2025
fc542d6
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Apr 1, 2025
b333d36
improve errors; merge tests
shyam-patel-kira Apr 1, 2025
6438f2c
Merge branch 'develop' into broadcast-equivocating-blocks
rkapka Apr 2, 2025
5c184fc
Update a unit test
shyam-patel-kira Apr 7, 2025
73b1d11
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Apr 7, 2025
f3f28eb
Merge branch 'broadcast-equivocating-blocks' of https://github.com/sh…
shyam-patel-kira Apr 7, 2025
0b7c01f
Merge branch 'broadcast-equivocating-blocks' of https://github.com/sh…
shyam-patel-kira Apr 14, 2025
455e68c
resolve merge conflicts; pull v6
shyam-patel-kira Apr 22, 2025
9b4a826
fix lint
shyam-patel-kira Apr 22, 2025
713f658
Merge branch 'develop' into broadcast-equivocating-blocks
shyam-patel-kira Apr 24, 2025
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 @@ -211,6 +211,7 @@ go_test(
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/blstoexec:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/operations/slashings/mock:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/p2p/encoder:go_default_library",
"//beacon-chain/p2p/peers:go_default_library",
Expand Down
77 changes: 77 additions & 0 deletions beacon-chain/sync/validate_beacon_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v5/monitoring/tracing"
"github.com/prysmaticlabs/prysm/v5/monitoring/tracing/trace"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
prysmTime "github.com/prysmaticlabs/prysm/v5/time"
"github.com/prysmaticlabs/prysm/v5/time/slots"
Expand Down Expand Up @@ -99,6 +100,10 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms

// Verify the block is the first block received for the proposer for the slot.
if s.hasSeenBlockIndexSlot(blk.Block().Slot(), blk.Block().ProposerIndex()) {
// Attempt to detect and broadcast equivocation before ignoring
if err := s.detectAndBroadcastEquivocation(ctx, blk); err != nil {
log.WithError(err).Debug("Could not detect/broadcast equivocation")
}
return pubsub.ValidationIgnore, nil
}

Expand Down Expand Up @@ -456,3 +461,75 @@ func getBlockFields(b interfaces.ReadOnlySignedBeaconBlock) logrus.Fields {
"version": b.Block().Version(),
}
}

// detectAndBroadcastEquivocation checks if the given block is an equivocating block (i.e. a different block
// with the same slot and proposer index but different signature than one we've already seen). If an equivocation
// is detected, it creates a proposer slashing object and broadcasts it to the network before adding it to the
// slashing pool.
func (s *Service) detectAndBroadcastEquivocation(ctx context.Context, blk interfaces.ReadOnlySignedBeaconBlock) error {
// If we've seen this proposer/slot combo before, it means this is an equivocating block
slot := blk.Block().Slot()
proposerIndex := blk.Block().ProposerIndex()

// Get the head state for block verification
headState, err := s.cfg.chain.HeadStateReadOnly(ctx)
if err != nil {
return err
}

sig1 := blk.Signature()
s.seenBlockLock.RLock()
// Create a unique cache key by combining slot and proposer index
// Convert slot and proposer index to 32 bytes and combine them into a single byte slice
b := append(bytesutil.Bytes32(uint64(slot)), bytesutil.Bytes32(uint64(proposerIndex))...)
existingBlock, isSeen := s.seenBlockCache.Get(string(b))
s.seenBlockLock.RUnlock()

if !isSeen {
return nil
}

// Type assert to get the existing block
existingSignedBlock, ok := existingBlock.(interfaces.ReadOnlySignedBeaconBlock)
if !ok {
return errors.New("invalid type assertion for existing block")
}

sig2 := existingSignedBlock.Signature()

// Different signatures indicate equivocation
if sig1 != sig2 {
header1, err := blk.Header()
if err != nil {
return err
}
header2, err := existingSignedBlock.Header()
if err != nil {
return err
}

slashing := &ethpb.ProposerSlashing{
Header_1: header1,
Header_2: header2,
}

// Verify the proposer slashing is valid
if err := blocks.VerifyProposerSlashing(headState, slashing); err != nil {
return err
}

// Broadcast if verification passes
if !features.Get().DisableBroadcastSlashings {
if err := s.cfg.p2p.Broadcast(ctx, slashing); err != nil {
return errors.Wrap(err, "could not broadcast slashing object")
}
}

// Also insert into slashing pool
if err := s.cfg.slashingPool.InsertProposerSlashing(ctx, headState, slashing); err != nil {
return errors.Wrap(err, "could not insert proposer slashing into pool")
}
}

return nil
}
134 changes: 134 additions & 0 deletions beacon-chain/sync/validate_beacon_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
dbtest "github.com/prysmaticlabs/prysm/v5/beacon-chain/db/testing"
doublylinkedtree "github.com/prysmaticlabs/prysm/v5/beacon-chain/forkchoice/doubly-linked-tree"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/attestations"
slashingsmock "github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/slashings/mock"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p"
p2ptest "github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/testing"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/startup"
Expand Down Expand Up @@ -1495,3 +1496,136 @@ func Test_validateDenebBeaconBlock(t *testing.T) {
require.NoError(t, err)
require.ErrorIs(t, validateDenebBeaconBlock(bdb.Block()), errRejectCommitmentLen)
}

func TestDetectAndBroadcastEquivocation_NoEquivocation(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you merge all these tests into one, using t.Run for each test case? That way you can set up things like p := p2ptest.NewTestP2P(t) only once, at the start of the test, and reduce test times.

Pseudocode:

func TestDetectAndBroadcastEquivocation() {
    p := p2ptest.NewTestP2P(t)
    (...)
    t.Run("no equivocation", ...
    t.Run("equivocation detected", ...
}

p := p2ptest.NewTestP2P(t)
ctx := context.Background()
beaconState, privKeys := util.DeterministicGenesisState(t, 100)

block := util.NewBeaconBlock()
block.Block.Slot = 1
block.Block.ProposerIndex = 0

sig, err := signing.ComputeDomainAndSign(beaconState, 0, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[0])
require.NoError(t, err)
block.Signature = sig

chainService := &mock.ChainService{
State: beaconState,
Genesis: time.Now(),
}

slashingPool := &slashingsmock.PoolMock{}
r := &Service{
cfg: &config{
p2p: p,
chain: chainService,
slashingPool: slashingPool,
},
seenBlockCache: lruwrpr.New(10),
}

b, err := blocks.NewSignedBeaconBlock(block)
require.NoError(t, err)

err = r.detectAndBroadcastEquivocation(ctx, b)
assert.NoError(t, err)
assert.Equal(t, 0, len(slashingPool.PendingPropSlashings), "Expected no slashings to be inserted")
}

func TestDetectAndBroadcastEquivocation_EquivocationDetected(t *testing.T) {
p := p2ptest.NewTestP2P(t)
ctx := context.Background()
beaconState, privKeys := util.DeterministicGenesisState(t, 100)

// Create first block
block1 := util.NewBeaconBlock()
block1.Block.Slot = 1
block1.Block.ProposerIndex = 0
block1.Block.ParentRoot = bytesutil.PadTo([]byte("parent1"), 32)
sig1, err := signing.ComputeDomainAndSign(beaconState, 0, block1.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[0])
require.NoError(t, err)
block1.Signature = sig1

// Create second block with same slot/proposer but different contents
block2 := util.NewBeaconBlock()
block2.Block.Slot = 1
block2.Block.ProposerIndex = 0
block2.Block.ParentRoot = bytesutil.PadTo([]byte("parent2"), 32)
sig2, err := signing.ComputeDomainAndSign(beaconState, 0, block2.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[0])
require.NoError(t, err)
block2.Signature = sig2

slashingPool := &slashingsmock.PoolMock{}
chainService := &mock.ChainService{
State: beaconState,
Genesis: time.Now(),
}
r := &Service{
cfg: &config{
p2p: p,
chain: chainService,
slashingPool: slashingPool,
},
seenBlockCache: lruwrpr.New(10),
}

// Cache first block
r.setSeenBlockIndexSlot(block1.Block.Slot, block1.Block.ProposerIndex)
b := append(bytesutil.Bytes32(uint64(block1.Block.Slot)), bytesutil.Bytes32(uint64(block1.Block.ProposerIndex))...)
signedBlock1, err := blocks.NewSignedBeaconBlock(block1)
require.NoError(t, err)
r.seenBlockCache.Add(string(b), signedBlock1)

// Process second block which should trigger equivocation
signedBlock2, err := blocks.NewSignedBeaconBlock(block2)
require.NoError(t, err)
err = r.detectAndBroadcastEquivocation(ctx, signedBlock2)
require.NoError(t, err)

// Verify slashing was inserted
require.Equal(t, 1, len(slashingPool.PendingPropSlashings), "Expected a slashing to be inserted")
slashing := slashingPool.PendingPropSlashings[0]
assert.Equal(t, primitives.ValidatorIndex(0), slashing.Header_1.Header.ProposerIndex, "Wrong proposer index")
assert.Equal(t, primitives.Slot(1), slashing.Header_1.Header.Slot, "Wrong slot")
}

func TestDetectAndBroadcastEquivocation_SameSignature(t *testing.T) {
p := p2ptest.NewTestP2P(t)
ctx := context.Background()
beaconState, privKeys := util.DeterministicGenesisState(t, 100)

// Create a block
block := util.NewBeaconBlock()
block.Block.Slot = 1
block.Block.ProposerIndex = 0
sig, err := signing.ComputeDomainAndSign(beaconState, 0, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[0])
require.NoError(t, err)
block.Signature = sig

slashingPool := &slashingsmock.PoolMock{}
chainService := &mock.ChainService{
State: beaconState,
Genesis: time.Now(),
}
r := &Service{
cfg: &config{
p2p: p,
chain: chainService,
slashingPool: slashingPool,
},
seenBlockCache: lruwrpr.New(10),
}

// Cache the block
r.setSeenBlockIndexSlot(block.Block.Slot, block.Block.ProposerIndex)
b := append(bytesutil.Bytes32(uint64(block.Block.Slot)), bytesutil.Bytes32(uint64(block.Block.ProposerIndex))...)
signedBlock, err := blocks.NewSignedBeaconBlock(block)
require.NoError(t, err)
r.seenBlockCache.Add(string(b), signedBlock)

// Try to process the same block again
err = r.detectAndBroadcastEquivocation(ctx, signedBlock)
require.NoError(t, err)
assert.Equal(t, 0, len(slashingPool.PendingPropSlashings), "Expected no slashings for same signature")
}
5 changes: 5 additions & 0 deletions changelog/kira_broadcast_slashings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Added
- Added broadcast of slashing events (proposer and attester slashings) upon detection of equivocation.

### Updated
- Updated attestation pool logic to prioritize slashings over regular attestations during block production.
Loading