Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olegshmuelov committed Nov 28, 2024
1 parent 1d29d4d commit ce0b3ff
Showing 1 changed file with 88 additions and 0 deletions.
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
}

0 comments on commit ce0b3ff

Please sign in to comment.