Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moshe-blox committed Dec 3, 2024
2 parents ba06e7e + 492f99d commit 3ab3aba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
31 changes: 21 additions & 10 deletions message/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/ssvlabs/ssv/registry/storage/mocks"
"github.com/ssvlabs/ssv/storage/basedb"
"github.com/ssvlabs/ssv/storage/kv"
"github.com/ssvlabs/ssv/utils"
)

func Test_ValidateSSVMessage(t *testing.T) {
Expand Down Expand Up @@ -632,27 +633,32 @@ func Test_ValidateSSVMessage(t *testing.T) {
})

t.Run("accept pre-consensus randao message when epoch duties are not set", func(t *testing.T) {
currentSlot := &utils.SlotValue{}
mockNetworkConfig := networkconfig.NetworkConfig{
Beacon: utils.SetupMockBeaconNetwork(t, currentSlot),
}

const epoch = 1
slot := netCfg.Beacon.FirstSlotAtEpoch(epoch)
currentSlot.SetSlot(netCfg.Beacon.FirstSlotAtEpoch(epoch))

ds := dutystore.New()

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

messages := generateRandaoMsg(ks.Shares[1], 1, epoch, slot)
messages := generateRandaoMsg(ks.Shares[1], 1, epoch, currentSlot.GetSlot())
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),
MsgID: spectypes.NewMsgID(mockNetworkConfig.DomainType(), dutyExecutorID, spectypes.RoleProposer),
Data: encodedMessages,
}

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

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

require.False(t, ds.Proposer.IsEpochSet(epoch))
Expand All @@ -662,28 +668,33 @@ func Test_ValidateSSVMessage(t *testing.T) {
})

t.Run("reject pre-consensus randao message when epoch duties are set", func(t *testing.T) {
currentSlot := &utils.SlotValue{}
mockNetworkConfig := networkconfig.NetworkConfig{
Beacon: utils.SetupMockBeaconNetwork(t, currentSlot),
}

const epoch = 1
slot := netCfg.Beacon.FirstSlotAtEpoch(epoch)
currentSlot.SetSlot(mockNetworkConfig.Beacon.FirstSlotAtEpoch(epoch))

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

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

messages := generateRandaoMsg(ks.Shares[1], 1, epoch, slot)
messages := generateRandaoMsg(ks.Shares[1], 1, epoch, currentSlot.GetSlot())
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),
MsgID: spectypes.NewMsgID(mockNetworkConfig.DomainType(), dutyExecutorID, spectypes.RoleProposer),
Data: encodedMessages,
}

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

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

require.True(t, ds.Proposer.IsEpochSet(epoch))
Expand Down
37 changes: 27 additions & 10 deletions utils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"sync"
"testing"
"time"

"github.com/attestantio/go-eth2-client/spec/phase0"
spectypes "github.com/ssvlabs/ssv-spec/types"
Expand Down Expand Up @@ -38,25 +39,41 @@ func SetupMockBeaconNetwork(t *testing.T, currentSlot *SlotValue) *mocknetwork.M
}

beaconNetwork := spectypes.HoleskyNetwork // it must be something known by ekm

mockBeaconNetwork := mocknetwork.NewMockBeaconNetwork(ctrl)
mockBeaconNetwork.EXPECT().GetBeaconNetwork().Return(beaconNetwork).AnyTimes()

mockBeaconNetwork.EXPECT().EstimatedCurrentEpoch().DoAndReturn(
func() phase0.Epoch {
return phase0.Epoch(currentSlot.GetSlot() / 32)
},
).AnyTimes()

mockBeaconNetwork.EXPECT().GetBeaconNetwork().Return(beaconNetwork).AnyTimes()
mockBeaconNetwork.EXPECT().SlotsPerEpoch().Return(beaconNetwork.SlotsPerEpoch()).AnyTimes()
mockBeaconNetwork.EXPECT().EstimatedCurrentSlot().DoAndReturn(
func() phase0.Slot {
return currentSlot.GetSlot()
},
).AnyTimes()

mockBeaconNetwork.EXPECT().EstimatedCurrentEpoch().DoAndReturn(
func() phase0.Epoch {
return phase0.Epoch(uint64(currentSlot.GetSlot()) / beaconNetwork.SlotsPerEpoch())
},
).AnyTimes()
mockBeaconNetwork.EXPECT().EstimatedEpochAtSlot(gomock.Any()).DoAndReturn(
func(slot phase0.Slot) phase0.Epoch {
return phase0.Epoch(slot / 32)
return beaconNetwork.EstimatedEpochAtSlot(slot)
},
).AnyTimes()
mockBeaconNetwork.EXPECT().FirstSlotAtEpoch(gomock.Any()).DoAndReturn(
func(epoch phase0.Epoch) phase0.Slot {
return beaconNetwork.FirstSlotAtEpoch(epoch)
},
).AnyTimes()
mockBeaconNetwork.EXPECT().IsFirstSlotOfEpoch(gomock.Any()).DoAndReturn(
func(slot phase0.Slot) bool {
return uint64(slot)%mockBeaconNetwork.SlotsPerEpoch() == 0
},
).AnyTimes()
mockBeaconNetwork.EXPECT().GetSlotStartTime(gomock.Any()).DoAndReturn(
func(slot phase0.Slot) time.Time {
timeSinceGenesisStart := int64(uint64(slot) * uint64(beaconNetwork.SlotDurationSec().Seconds())) // #nosec G115
minGenesisTime := int64(mockBeaconNetwork.GetBeaconNetwork().MinGenesisTime()) // #nosec G115
start := time.Unix(minGenesisTime+timeSinceGenesisStart, 0)
return start
},
).AnyTimes()

Expand Down

0 comments on commit 3ab3aba

Please sign in to comment.