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

Broadcast Slashing on equivocation #14693

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
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: 4 additions & 0 deletions beacon-chain/blockchain/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ChainService struct {
InitSyncBlockRoots map[[32]byte]bool
DB db.Database
State state.BeaconState
HeadStateErr error
Block interfaces.ReadOnlySignedBeaconBlock
VerifyBlkDescendantErr error
stateNotifier statefeed.Notifier
Expand Down Expand Up @@ -360,6 +361,9 @@ func (s *ChainService) HeadState(context.Context) (state.BeaconState, error) {

// HeadStateReadOnly mocks HeadStateReadOnly method in chain service.
func (s *ChainService) HeadStateReadOnly(context.Context) (state.ReadOnlyBeaconState, error) {
if s.HeadStateErr != nil {
return nil, s.HeadStateErr
}
return s.State, nil
}

Expand Down
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
71 changes: 71 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,69 @@ func getBlockFields(b interfaces.ReadOnlySignedBeaconBlock) logrus.Fields {
"version": b.Block().Version(),
}
}

// detectAndBroadcastEquivocation checks if the given block is an equivocating block by comparing it with
// the head block when a duplicate slot/proposer index is detected. If an equivocation is found, it creates
// and broadcasts a proposer slashing object and adds it to the slashing pool.
func (s *Service) detectAndBroadcastEquivocation(ctx context.Context, blk interfaces.ReadOnlySignedBeaconBlock) error {
// If this proposer/slot combo is seen before, it means this is a potential equivocating block
slot := blk.Block().Slot()
proposerIndex := blk.Block().ProposerIndex()

if !s.hasSeenBlockIndexSlot(slot, proposerIndex) {
return nil
}

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

// Compare signatures since they must be different for equivocation
sig1 := blk.Signature()
headBlock, err := s.cfg.chain.HeadBlock(ctx)
if err != nil {
return errors.Wrap(err, "could not get head block")
}

sig2 := headBlock.Signature()

// Different signatures indicate equivocation
if sig1 == sig2 {
return nil
}

header1, err := blk.Header()
if err != nil {
return errors.Wrap(err, "could not get header from new block")
}
header2, err := headBlock.Header()
if err != nil {
return errors.Wrap(err, "could not get header from head block")
}

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

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

// 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
}
218 changes: 218 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,220 @@ func Test_validateDenebBeaconBlock(t *testing.T) {
require.NoError(t, err)
require.ErrorIs(t, validateDenebBeaconBlock(bdb.Block()), errRejectCommitmentLen)
}

func TestDetectAndBroadcastEquivocation_NoEquivocation(t *testing.T) {
// db := dbtest.SetupDB(t)
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(),
}
r := &Service{
cfg: &config{
p2p: p,
chain: chainService,
slashingPool: &slashingsmock.PoolMock{},
},
seenBlockCache: lruwrpr.New(10),
}

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

err = r.detectAndBroadcastEquivocation(ctx, signedBlock)
require.NoError(t, err)
}

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

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

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

signedHeadBlock, err := blocks.NewSignedBeaconBlock(headBlock)
require.NoError(t, err)

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

// Mark block as seen
r.setSeenBlockIndexSlot(newBlock.Block.Slot, newBlock.Block.ProposerIndex)

signedNewBlock, err := blocks.NewSignedBeaconBlock(newBlock)
require.NoError(t, err)

err = r.detectAndBroadcastEquivocation(ctx, signedNewBlock)
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 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

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

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

// Mark block as seen
r.setSeenBlockIndexSlot(block.Block.Slot, block.Block.ProposerIndex)

// 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")
}

func TestDetectAndBroadcastEquivocation_DifferentSignatures(t *testing.T) {
// db := dbtest.SetupDB(t)
p := p2ptest.NewTestP2P(t)
ctx := context.Background()
beaconState, privKeys := util.DeterministicGenesisState(t, 100)

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

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

signedHeadBlock, err := blocks.NewSignedBeaconBlock(headBlock)
require.NoError(t, err)

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

// Mark block as seen
r.setSeenBlockIndexSlot(newBlock.Block.Slot, newBlock.Block.ProposerIndex)

signedNewBlock, err := blocks.NewSignedBeaconBlock(newBlock)
require.NoError(t, err)

err = r.detectAndBroadcastEquivocation(ctx, signedNewBlock)
require.NoError(t, err)

// Verify slashing was inserted
require.Equal(t, 1, len(slashingPool.PendingPropSlashings))
}

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

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

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

chainService := &mock.ChainService{
State: nil,
Block: signedBlock,
HeadStateErr: errors.New("could not get head state"),
}
r := &Service{
cfg: &config{
p2p: p,
chain: chainService,
slashingPool: &slashingsmock.PoolMock{},
},
seenBlockCache: lruwrpr.New(10),
}

// Mark block as seen to trigger equivocation check
r.setSeenBlockIndexSlot(block.Block.Slot, block.Block.ProposerIndex)

err = r.detectAndBroadcastEquivocation(ctx, signedBlock)
require.ErrorContains(t, "could not get head state", err)
}
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