Skip to content

Commit

Permalink
Convert Phase0 slashing to Electra slashings at the fork (#14844)
Browse files Browse the repository at this point in the history
* EIP-7549: slasher

* update chunks and detection

* update tests

* encode+decode

* timer

* test fixes

* testing the timer

* Decouple pool from service

* update mock

* cleanup

* make review easier

* comments and changelog
  • Loading branch information
rkapka authored Jan 31, 2025
1 parent 4a487ba commit 1069da1
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 254 deletions.
15 changes: 15 additions & 0 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ func registerServices(cliCtx *cli.Context, beacon *BeaconNode, synchronizer *sta
return errors.Wrap(err, "could not register sync service")
}

log.Debugln("Registering Slashing Pool Service")
if err := beacon.registerSlashingPoolService(); err != nil {
return errors.Wrap(err, "could not register slashing pool service")
}

log.Debugln("Registering Slasher Service")
if err := beacon.registerSlasherService(); err != nil {
return errors.Wrap(err, "could not register slasher service")
Expand Down Expand Up @@ -724,6 +729,16 @@ func (b *BeaconNode) registerAttestationPool() error {
return b.services.RegisterService(s)
}

func (b *BeaconNode) registerSlashingPoolService() error {
var chainService *blockchain.Service
if err := b.services.FetchService(&chainService); err != nil {
return err
}

s := slashings.NewPoolService(b.ctx, b.slashingsPool, slashings.WithElectraTimer(b.clockWaiter, chainService.CurrentSlot))
return b.services.RegisterService(s)
}

func (b *BeaconNode) registerBlockchainService(fc forkchoice.ForkChoicer, gs *startup.ClockSynchronizer, syncComplete chan struct{}) error {
var web3Service *execution.Service
if err := b.services.FetchService(&web3Service); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/operations/slashings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"log.go",
"metrics.go",
"service.go",
"service_new.go",
"types.go",
],
importpath = "github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/slashings",
Expand All @@ -19,13 +20,15 @@ go_library(
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/time:go_default_library",
"//beacon-chain/startup:go_default_library",
"//beacon-chain/state:go_default_library",
"//config/params:go_default_library",
"//consensus-types/primitives:go_default_library",
"//container/slice:go_default_library",
"//monitoring/tracing/trace:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",
"//time/slots:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
Expand All @@ -39,18 +42,21 @@ go_test(
size = "small",
srcs = [
"service_attester_test.go",
"service_new_test.go",
"service_proposer_test.go",
"service_test.go",
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/operations/slashings/mock:go_default_library",
"//beacon-chain/startup:go_default_library",
"//beacon-chain/state:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types/primitives:go_default_library",
"//crypto/bls:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",
"//testing/assert:go_default_library",
"//testing/require:go_default_library",
"//testing/util:go_default_library",
Expand Down
3 changes: 3 additions & 0 deletions beacon-chain/operations/slashings/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (m *PoolMock) InsertProposerSlashing(_ context.Context, _ state.ReadOnlyBea
return nil
}

// ConvertToElectra --
func (*PoolMock) ConvertToElectra() {}

// MarkIncludedAttesterSlashing --
func (*PoolMock) MarkIncludedAttesterSlashing(_ ethpb.AttSlashing) {
panic("implement me")
Expand Down
30 changes: 28 additions & 2 deletions beacon-chain/operations/slashings/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/time"
coretime "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/time"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
Expand Down Expand Up @@ -270,6 +270,32 @@ func (p *Pool) MarkIncludedProposerSlashing(ps *ethpb.ProposerSlashing) {
numProposerSlashingsIncluded.Inc()
}

// ConvertToElectra converts all Phase0 attester slashings to Electra attester slashings.
// This functionality is needed at the time of the Electra fork.
func (p *Pool) ConvertToElectra() {
p.lock.Lock()
defer p.lock.Unlock()

for _, pas := range p.pendingAttesterSlashing {
if pas.attesterSlashing.Version() == version.Phase0 {
first := pas.attesterSlashing.FirstAttestation()
second := pas.attesterSlashing.SecondAttestation()
pas.attesterSlashing = &ethpb.AttesterSlashingElectra{
Attestation_1: &ethpb.IndexedAttestationElectra{
AttestingIndices: first.GetAttestingIndices(),
Data: first.GetData(),
Signature: first.GetSignature(),
},
Attestation_2: &ethpb.IndexedAttestationElectra{
AttestingIndices: second.GetAttestingIndices(),
Data: second.GetData(),
Signature: second.GetSignature(),
},
}
}
}
}

// this function checks a few items about a validator before proceeding with inserting
// a proposer/attester slashing into the pool. First, it checks if the validator
// has been recently included in the pool, then it checks if the validator is slashable.
Expand All @@ -291,7 +317,7 @@ func (p *Pool) validatorSlashingPreconditionCheck(
return false, err
}
// Checking if the validator is slashable.
if !helpers.IsSlashableValidatorUsingTrie(validator, time.CurrentEpoch(state)) {
if !helpers.IsSlashableValidatorUsingTrie(validator, coretime.CurrentEpoch(state)) {
return false, nil
}
return true, nil
Expand Down
Loading

0 comments on commit 1069da1

Please sign in to comment.