Skip to content

Commit

Permalink
implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
olegshmuelov committed Dec 3, 2024
1 parent 7f928b8 commit 38d3290
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions eth/eventhandler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
// b64 encrypted key length is 256
const encryptedKeyLength = 256

// contractParticipationDelay is the number of epochs after contract registration or reactivation
// in which the validator can start participating.
const contractParticipationDelay phase0.Epoch = 1

var (
ErrOperatorPubkeyAlreadyExists = fmt.Errorf("operator public key already exists")
ErrOperatorIDAlreadyExists = fmt.Errorf("operator ID already exists")
Expand Down Expand Up @@ -243,6 +247,12 @@ func (eh *EventHandler) handleShareCreation(
if err := eh.keyManager.AddShare(shareSecret); err != nil {
return nil, fmt.Errorf("could not add share secret to key manager: %w", err)
}

// Set the minimum participation epoch to match slashing protection.
// Note: The current epoch can differ from the epoch set in slashing protection
// due to the passage of time between saving slashing protection data and setting
// the minimum participation epoch
share.SetMinParticipationEpoch(eh.networkConfig.Beacon.EstimatedCurrentEpoch() + contractParticipationDelay)
}

// Save share.
Expand Down Expand Up @@ -424,6 +434,12 @@ func (eh *EventHandler) handleClusterReactivated(txn basedb.Txn, event *contract
if err := eh.keyManager.(ekm.StorageProvider).BumpSlashingProtection(share.SharePubKey); err != nil {
return nil, fmt.Errorf("could not bump slashing protection: %w", err)
}

// Set the minimum participation epoch to match slashing protection.
// Note: The current epoch can differ from the epoch set in slashing protection
// due to the passage of time between saving slashing protection data and setting
// the minimum participation epoch
share.SetMinParticipationEpoch(eh.networkConfig.Beacon.EstimatedCurrentEpoch() + contractParticipationDelay)
}

if len(enabledPubKeys) > 0 {
Expand Down
17 changes: 17 additions & 0 deletions operator/duties/attester.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ func (h *AttesterHandler) toGenesisSpecDuty(duty *eth2apiv1.AttesterDuty, role g

func (h *AttesterHandler) shouldExecute(duty *eth2apiv1.AttesterDuty) bool {
currentSlot := h.network.Beacon.EstimatedCurrentSlot()
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(currentSlot)

v, exists := h.validatorProvider.Validator(duty.PubKey[:])
if !exists {
h.logger.Warn("validator not found", fields.Validator(duty.PubKey[:]))
return false
}

if v.MinParticipationEpoch() > currentEpoch {
h.logger.Debug("validator not yet participating",
fields.Validator(duty.PubKey[:]),
zap.Uint64("min_participation_epoch", uint64(v.MinParticipationEpoch())),
zap.Uint64("current_epoch", uint64(currentEpoch)),
)
return false
}

// execute task if slot already began and not pass 1 epoch
var attestationPropagationSlotRange = phase0.Slot(h.network.Beacon.SlotsPerEpoch())
if currentSlot >= duty.Slot && currentSlot-duty.Slot <= attestationPropagationSlotRange {
Expand Down
1 change: 1 addition & 0 deletions operator/duties/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type ExecutionClient interface {
type ValidatorProvider interface {
ParticipatingValidators(epoch phase0.Epoch) []*types.SSVShare
SelfParticipatingValidators(epoch phase0.Epoch) []*types.SSVShare
Validator(pubKey []byte) (*types.SSVShare, bool)
}

// ValidatorController represents the component that controls validators via the scheduler
Expand Down
15 changes: 15 additions & 0 deletions operator/duties/scheduler_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions operator/duties/sync_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ func (h *SyncCommitteeHandler) toSpecDuty(duty *eth2apiv1.SyncCommitteeDuty, slo

func (h *SyncCommitteeHandler) shouldExecute(duty *eth2apiv1.SyncCommitteeDuty, slot phase0.Slot) bool {
currentSlot := h.network.Beacon.EstimatedCurrentSlot()
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(currentSlot)

v, exists := h.validatorProvider.Validator(duty.PubKey[:])
if !exists {
h.logger.Warn("validator not found", fields.Validator(duty.PubKey[:]))
return false
}

if v.MinParticipationEpoch() > currentEpoch {
h.logger.Debug("validator not yet participating",
fields.Validator(duty.PubKey[:]),
zap.Uint64("min_participation_epoch", uint64(v.MinParticipationEpoch())),
zap.Uint64("current_epoch", uint64(currentEpoch)),
)
return false
}

// execute task if slot already began and not pass 1 slot
if currentSlot == slot {
return true
Expand Down
14 changes: 14 additions & 0 deletions protocol/v2/types/ssvshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type SSVShare struct {
Metadata

committeeID atomic.Pointer[spectypes.CommitteeID]

// minParticipationEpoch is the epoch at which the validator can start participating.
// This is set on registration and on every reactivation.
//
// TODO: this is not persistent yet, so we should assume zero values are already participating for now.
minParticipationEpoch phase0.Epoch
}

// BelongsToOperator checks whether the share belongs to operator.
Expand Down Expand Up @@ -56,6 +62,14 @@ func (s *SSVShare) IsParticipating(epoch phase0.Epoch) bool {
return !s.Liquidated && s.IsAttesting(epoch)
}

func (s *SSVShare) SetMinParticipationEpoch(epoch phase0.Epoch) {
s.minParticipationEpoch = epoch
}

func (s *SSVShare) MinParticipationEpoch() phase0.Epoch {
return s.minParticipationEpoch
}

func (s *SSVShare) SetFeeRecipient(feeRecipient bellatrix.ExecutionAddress) {
s.FeeRecipientAddress = feeRecipient
}
Expand Down

0 comments on commit 38d3290

Please sign in to comment.