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

fix(MessageValidation): first slot proposals [stage] #1886

Open
wants to merge 5 commits into
base: stage
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions message/validation/common_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,18 @@ func (mv *messageValidator) validateBeaconDuty(
role spectypes.RunnerRole,
slot phase0.Slot,
indices []phase0.ValidatorIndex,
randao bool,
) error {
epoch := mv.netCfg.Beacon.EstimatedEpochAtSlot(slot)

// Rule: For a proposal duty message, we check if the validator is assigned to it
if role == spectypes.RoleProposer {
epoch := mv.netCfg.Beacon.EstimatedEpochAtSlot(slot)
if randao && mv.netCfg.Beacon.IsFirstSlotOfEpoch(slot) {
if !mv.dutyStore.Proposer.IsEpochSet(epoch) {
return nil // Accept message even if duties are not set
}
}

// Non-committee roles always have one validator index.
validatorIndex := indices[0]
if mv.dutyStore.Proposer.ValidatorDuty(epoch, slot, validatorIndex) == nil {
Expand All @@ -130,7 +138,7 @@ func (mv *messageValidator) validateBeaconDuty(

// Rule: For a sync committee aggregation duty message, we check if the validator is assigned to it
if role == spectypes.RoleSyncCommitteeContribution {
period := mv.netCfg.Beacon.EstimatedSyncCommitteePeriodAtEpoch(mv.netCfg.Beacon.EstimatedEpochAtSlot(slot))
period := mv.netCfg.Beacon.EstimatedSyncCommitteePeriodAtEpoch(epoch)
// Non-committee roles always have one validator index.
validatorIndex := indices[0]
if mv.dutyStore.SyncCommittee.Duty(period, validatorIndex) == nil {
Expand Down
2 changes: 1 addition & 1 deletion message/validation/consensus_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (mv *messageValidator) validateQBFTMessageByDutyLogic(
}

msgSlot := phase0.Slot(consensusMessage.Height)
if err := mv.validateBeaconDuty(role, msgSlot, validatorIndices); err != nil {
if err := mv.validateBeaconDuty(role, msgSlot, validatorIndices, false); err != nil {
return err
}

Expand Down
5 changes: 3 additions & 2 deletions message/validation/partial_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (mv *messageValidator) validatePartialSigMessagesByDutyLogic(
signerStateBySlot := state.GetOrCreate(signer)

// Rule: Height must not be "old". I.e., signer must not have already advanced to a later slot.
if signedSSVMessage.SSVMessage.MsgID.GetRoleType() != types.RoleCommittee { // Rule only for validator runners
if role != types.RoleCommittee { // Rule only for validator runners
maxSlot := signerStateBySlot.MaxSlot()
if maxSlot != 0 && maxSlot > partialSignatureMessages.Slot {
e := ErrSlotAlreadyAdvanced
Expand All @@ -155,7 +155,8 @@ func (mv *messageValidator) validatePartialSigMessagesByDutyLogic(
}
}

if err := mv.validateBeaconDuty(signedSSVMessage.SSVMessage.GetID().GetRoleType(), messageSlot, committeeInfo.indices); err != nil {
randao := partialSignatureMessages.Type == spectypes.RandaoPartialSig
if err := mv.validateBeaconDuty(role, messageSlot, committeeInfo.indices, randao); err != nil {
return err
}

Expand Down
88 changes: 88 additions & 0 deletions message/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

eth2apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/herumi/bls-eth-go-binary/bls"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pspb "github.com/libp2p/go-libp2p-pubsub/pb"
specqbft "github.com/ssvlabs/ssv-spec/qbft"
Expand Down Expand Up @@ -630,6 +631,67 @@ func Test_ValidateSSVMessage(t *testing.T) {
require.NoError(t, err)
})

t.Run("accept pre-consensus randao message when epoch duties are not set", func(t *testing.T) {
const epoch = 1
slot := netCfg.Beacon.FirstSlotAtEpoch(epoch)

ds := dutystore.New()

validator := New(netCfg, validatorStore, ds, signatureVerifier).(*messageValidator)

messages := generateRandaoMsg(ks.Shares[1], 1, epoch, slot)
encodedMessages, err := messages.Encode()
require.NoError(t, err)

dutyExecutorID := shares.active.ValidatorPubKey[:]
ssvMessage := &spectypes.SSVMessage{
MsgType: spectypes.SSVPartialSignatureMsgType,
MsgID: spectypes.NewMsgID(spectestingutils.TestingSSVDomainType, dutyExecutorID, spectypes.RoleProposer),
Data: encodedMessages,
}

signedSSVMessage := spectestingutils.SignedSSVMessageWithSigner(1, ks.OperatorKeys[1], ssvMessage)

receivedAt := netCfg.Beacon.GetSlotStartTime(slot)
topicID := commons.CommitteeTopicID(committeeID)[0]

require.False(t, ds.Proposer.IsEpochSet(epoch))

_, err = validator.handleSignedSSVMessage(signedSSVMessage, topicID, receivedAt)
require.NoError(t, err)
})

t.Run("reject pre-consensus randao message when epoch duties are set", func(t *testing.T) {
const epoch = 1
slot := netCfg.Beacon.FirstSlotAtEpoch(epoch)

ds := dutystore.New()
ds.Proposer.Set(epoch, make([]dutystore.StoreDuty[eth2apiv1.ProposerDuty], 0))

validator := New(netCfg, validatorStore, ds, signatureVerifier).(*messageValidator)

messages := generateRandaoMsg(ks.Shares[1], 1, epoch, slot)
encodedMessages, err := messages.Encode()
require.NoError(t, err)

dutyExecutorID := shares.active.ValidatorPubKey[:]
ssvMessage := &spectypes.SSVMessage{
MsgType: spectypes.SSVPartialSignatureMsgType,
MsgID: spectypes.NewMsgID(spectestingutils.TestingSSVDomainType, dutyExecutorID, spectypes.RoleProposer),
Data: encodedMessages,
}

signedSSVMessage := spectestingutils.SignedSSVMessageWithSigner(1, ks.OperatorKeys[1], ssvMessage)

receivedAt := netCfg.Beacon.GetSlotStartTime(slot)
topicID := commons.CommitteeTopicID(committeeID)[0]

require.True(t, ds.Proposer.IsEpochSet(epoch))

_, err = validator.handleSignedSSVMessage(signedSSVMessage, topicID, receivedAt)
require.ErrorContains(t, err, ErrNoDuty.Error())
})

//// Get error when receiving a message with over 13 partial signatures
t.Run("partial message too big", func(t *testing.T) {
slot := netCfg.Beacon.FirstSlotAtEpoch(1)
Expand Down Expand Up @@ -1878,3 +1940,29 @@ func generateMultiSignedMessage(

return signedSSVMessage
}

var generateRandaoMsg = func(
sk *bls.SecretKey,
id spectypes.OperatorID,
epoch phase0.Epoch,
slot phase0.Slot,
) *spectypes.PartialSignatureMessages {
signer := spectestingutils.NewTestingKeyManager()
beacon := spectestingutils.NewTestingBeaconNode()
d, _ := beacon.DomainData(epoch, spectypes.DomainRandao)
signed, root, _ := signer.SignBeaconObject(spectypes.SSZUint64(epoch), d, sk.GetPublicKey().Serialize(), spectypes.DomainRandao)

msgs := spectypes.PartialSignatureMessages{
Type: spectypes.RandaoPartialSig,
Slot: slot,
Messages: []*spectypes.PartialSignatureMessage{},
}
msgs.Messages = append(msgs.Messages, &spectypes.PartialSignatureMessage{
PartialSignature: signed[:],
SigningRoot: root,
Signer: id,
ValidatorIndex: spectestingutils.TestingValidatorIndex,
})

return &msgs
}
8 changes: 8 additions & 0 deletions operator/duties/dutystore/duties.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ func (d *Duties[D]) ResetEpoch(epoch phase0.Epoch) {

delete(d.m, epoch)
}

func (d *Duties[D]) IsEpochSet(epoch phase0.Epoch) bool {
d.mu.RLock()
defer d.mu.RUnlock()

_, exists := d.m[epoch]
return exists
}