Skip to content

Commit

Permalink
Update unit tests and some mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-ssvlabs committed Jan 23, 2025
1 parent 4928fc7 commit c1ce6d7
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 35 deletions.
2 changes: 2 additions & 0 deletions api/handlers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type validatorJSON struct {
Index phase0.ValidatorIndex `json:"index"`
Status string `json:"status"`
ActivationEpoch phase0.Epoch `json:"activation_epoch"`
ExitEpoch phase0.Epoch `json:"exit_epoch"`
Owner api.Hex `json:"owner"`
Committee []spectypes.OperatorID `json:"committee"`
Quorum uint64 `json:"quorum"`
Expand All @@ -187,6 +188,7 @@ func validatorFromShare(share *types.SSVShare) *validatorJSON {
v.Index = share.ValidatorIndex
v.Status = share.Status.String()
v.ActivationEpoch = share.ActivationEpoch
v.ExitEpoch = share.ExitEpoch

Check warning on line 191 in api/handlers/validators.go

View check run for this annotation

Codecov / codecov/patch

api/handlers/validators.go#L191

Added line #L191 was not covered by tests
}
return v
}
1 change: 1 addition & 0 deletions eth/eventhandler/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ func TestHandleBlockEventsStream(t *testing.T) {
require.NotNil(t, valShare)
valShare.ValidatorIndex = 1
valShare.ActivationEpoch = 0
valShare.ExitEpoch = 0
valShare.Status = eth2apiv1.ValidatorStateActiveOngoing
err := eh.nodeStorage.Shares().Save(nil, valShare)
require.NoError(t, err)
Expand Down
9 changes: 6 additions & 3 deletions message/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,12 +1811,14 @@ func generateShares(t *testing.T, ks *spectestingutils.TestKeySet, ns storage.St
require.NoError(t, ns.Shares().Save(nil, inactiveShare))

slot := netCfg.Beacon.EstimatedCurrentSlot()
epoch := netCfg.Beacon.EstimatedEpochAtSlot(slot)
activationEpoch := netCfg.Beacon.EstimatedEpochAtSlot(slot)
exitEpoch := phase0.Epoch(0)

nonUpdatedMetadataShare := &ssvtypes.SSVShare{
Share: *spectestingutils.TestingShare(ks, spectestingutils.TestingValidatorIndex),
Status: eth2apiv1.ValidatorStatePendingQueued,
ActivationEpoch: epoch,
ActivationEpoch: activationEpoch,
ExitEpoch: exitEpoch,
Liquidated: false,
}

Expand All @@ -1829,7 +1831,8 @@ func generateShares(t *testing.T, ks *spectestingutils.TestKeySet, ns storage.St
nonUpdatedMetadataNextEpochShare := &ssvtypes.SSVShare{
Share: *spectestingutils.TestingShare(ks, spectestingutils.TestingValidatorIndex),
Status: eth2apiv1.ValidatorStatePendingQueued,
ActivationEpoch: epoch + 1,
ActivationEpoch: activationEpoch + 1,
ExitEpoch: exitEpoch,
Liquidated: false,
}

Expand Down
1 change: 1 addition & 0 deletions operator/validator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ func (c *controller) startValidatorsForMetadata(_ context.Context, validators me
v.Share.ValidatorIndex = share.ValidatorIndex
v.Share.Status = share.Status
v.Share.ActivationEpoch = share.ActivationEpoch
v.Share.ExitEpoch = share.ExitEpoch

Check warning on line 559 in operator/validator/controller.go

View check run for this annotation

Codecov / codecov/patch

operator/validator/controller.go#L559

Added line #L559 was not covered by tests
started, err := c.startValidator(v)
if err != nil {
c.logger.Warn("could not start validator", zap.Error(err))
Expand Down
31 changes: 18 additions & 13 deletions operator/validator/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestNewController(t *testing.T) {
}

func TestSetupValidatorsExporter(t *testing.T) {
passedEpoch := phase0.Epoch(1)
activationEpoch, exitEpoch := phase0.Epoch(1), phase0.Epoch(0)
operators := buildOperators(t)

operatorDataStore := operatordatastore.New(buildOperatorData(0, "67Ce5c69260bd819B4e0AD13f4b873074D479811"))
Expand All @@ -116,7 +116,8 @@ func TestSetupValidatorsExporter(t *testing.T) {
ValidatorPubKey: spectypes.ValidatorPK(secretKey.GetPublicKey().Serialize()),
},
Status: 3, // ValidatorStatePendingInitialized
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
ExitEpoch: exitEpoch,
Liquidated: false,
},
{
Expand All @@ -125,7 +126,8 @@ func TestSetupValidatorsExporter(t *testing.T) {
ValidatorPubKey: spectypes.ValidatorPK(secretKey2.GetPublicKey().Serialize()),
},
Status: 3, // Some other status
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
ExitEpoch: exitEpoch,
Liquidated: false,
},
}
Expand Down Expand Up @@ -305,7 +307,7 @@ func TestSetupValidators(t *testing.T) {
logger := logging.TestLogger(t)

// Init global variables
passedEpoch := phase0.Epoch(1)
activationEpoch, exitEpoch := phase0.Epoch(1), phase0.Epoch(0)
operatorIds := []uint64{1, 2, 3, 4}
var validatorPublicKey phase0.BLSPubKey

Expand Down Expand Up @@ -337,7 +339,8 @@ func TestSetupValidators(t *testing.T) {
ValidatorPubKey: spectypes.ValidatorPK(validatorKey),
},
Status: 3, // ValidatorStateActiveOngoing
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
ExitEpoch: exitEpoch,
}

shareWithoutMetaData := &types.SSVShare{
Expand Down Expand Up @@ -367,7 +370,7 @@ func TestSetupValidators(t *testing.T) {
Status: 3,
Index: 2,
Validator: &phase0.Validator{
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
PublicKey: validatorPublicKey,
},
},
Expand Down Expand Up @@ -584,7 +587,7 @@ func TestGetValidatorStats(t *testing.T) {
ctrl := gomock.NewController(t)
sharesStorage := mocks.NewMockSharesStorage(ctrl)
bc := beacon.NewMockBeaconNode(ctrl)
passedEpoch := phase0.Epoch(1)
activationEpoch, exitEpoch := phase0.Epoch(1), phase0.Epoch(0)

netCfg := networkconfig.TestNetwork
bc.EXPECT().GetBeaconNetwork().Return(netCfg.Beacon.GetBeaconNetwork()).AnyTimes()
Expand All @@ -604,14 +607,16 @@ func TestGetValidatorStats(t *testing.T) {
Committee: operators,
},
Status: 3, // ValidatorStatePendingInitialized
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
ExitEpoch: exitEpoch,
},
{
Share: spectypes.Share{
Committee: operators[1:],
},
Status: 1, // Some other status
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
ExitEpoch: exitEpoch,
},
}

Expand Down Expand Up @@ -651,7 +656,7 @@ func TestGetValidatorStats(t *testing.T) {
Committee: operators,
},
Status: 3, // ValidatorStatePendingInitialized
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
},
}

Expand Down Expand Up @@ -683,7 +688,7 @@ func TestGetValidatorStats(t *testing.T) {
Committee: nil,
},
Status: 3, // ValidatorStatePendingInitialized
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
},
}

Expand Down Expand Up @@ -722,14 +727,14 @@ func TestGetValidatorStats(t *testing.T) {
Committee: operators,
},
Status: 3, // ValidatorStatePendingInitialized
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
},
{
Share: spectypes.Share{
Committee: operators,
},
Status: 1,
ActivationEpoch: passedEpoch,
ActivationEpoch: activationEpoch,
},
}

Expand Down
13 changes: 3 additions & 10 deletions operator/validator/metadata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,16 @@ func (s *Syncer) Sync(ctx context.Context, pubKeys []spectypes.ValidatorPK) (Val
return nil, fmt.Errorf("fetch metadata: %w", err)
}

s.logger.Debug("🆕 fetched validators metadata",
fields.Took(time.Since(fetchStart)),
zap.Int("metadatas", len(metadata)),
zap.Int("validators", len(pubKeys)),
)
logger := s.logger.With(zap.Int("metadatas", len(metadata)), zap.Int("validators", len(pubKeys)))
logger.Debug("🆕 fetched validators metadata", fields.Took(time.Since(fetchStart)))

updateStart := time.Now()
// TODO: Refactor share storage to support passing context.
if err := s.shareStorage.UpdateValidatorsMetadata(metadata); err != nil {
return metadata, fmt.Errorf("update metadata: %w", err)
}

s.logger.Debug("🆕 saved validators metadata",
fields.Took(time.Since(updateStart)),
zap.Int("metadatas", len(metadata)),
zap.Int("validators", len(pubKeys)),
)
logger.Debug("🆕 saved validators metadata", fields.Took(time.Since(updateStart)))

return metadata, nil
}
Expand Down
7 changes: 4 additions & 3 deletions operator/validator/metadata/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"fmt"
"math"
"testing"
"time"

Expand Down Expand Up @@ -31,8 +32,6 @@ const (

// This test is copied from validator controller (TestUpdateValidatorMetadata) with minor changes and may require further refactoring.
func TestUpdateValidatorMetadata(t *testing.T) {
passedEpoch := phase0.Epoch(1)

operatorIDs := []uint64{1, 2, 3, 4}
committee := make([]*spectypes.ShareMember, len(operatorIDs))
for i, id := range operatorIDs {
Expand All @@ -41,7 +40,7 @@ func TestUpdateValidatorMetadata(t *testing.T) {
committee[i] = &spectypes.ShareMember{Signer: id, SharePubKey: operatorKey}
}

validatorMetadata := &beacon.ValidatorMetadata{Index: 1, ActivationEpoch: passedEpoch, Status: eth2apiv1.ValidatorStateActiveOngoing}
validatorMetadata := &beacon.ValidatorMetadata{Index: 1, ActivationEpoch: phase0.Epoch(1), ExitEpoch: phase0.Epoch(math.MaxUint64), Status: eth2apiv1.ValidatorStateActiveOngoing}

pubKey := spectypes.ValidatorPK{0x1}

Expand Down Expand Up @@ -443,6 +442,7 @@ func TestSyncer_Stream(t *testing.T) {
},
Status: eth2apiv1.ValidatorStateActiveOngoing,
ActivationEpoch: 0,
ExitEpoch: 0,
Liquidated: false,
}

Expand Down Expand Up @@ -476,6 +476,7 @@ func TestSyncer_Stream(t *testing.T) {
Index: 1,
Status: eth2apiv1.ValidatorStateActiveOngoing,
ActivationEpoch: 0,
ExitEpoch: 0,
},
}

Expand Down
3 changes: 2 additions & 1 deletion protocol/v2/blockchain/beacon/validator_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func (m *ValidatorMetadata) Equals(other *ValidatorMetadata) bool {
m.Status == other.Status &&
m.Index == other.Index &&
m.Balance == other.Balance &&
m.ActivationEpoch == other.ActivationEpoch
m.ActivationEpoch == other.ActivationEpoch &&
m.ExitEpoch == other.ExitEpoch
}

// Pending returns true if the validator is pending
Expand Down
10 changes: 5 additions & 5 deletions registry/storage/shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,7 @@ func TestSharesStorage(t *testing.T) {

for _, share := range persistedValidatorShares {
require.NoError(t, storage.Shares.UpdateValidatorsMetadata(map[spectypes.ValidatorPK]*beaconprotocol.ValidatorMetadata{
share.ValidatorPubKey: {
Index: 3,
Status: 2,
ActivationEpoch: 4,
},
share.ValidatorPubKey: {},
}))
}
})
Expand Down Expand Up @@ -354,6 +350,7 @@ func TestValidatorStoreThroughSharesStorage(t *testing.T) {
Status: eth2apiv1.ValidatorStateActiveOngoing,
Index: 3,
ActivationEpoch: 5,
ExitEpoch: 10,
}

// Update the share with new metadata
Expand All @@ -370,6 +367,7 @@ func TestValidatorStoreThroughSharesStorage(t *testing.T) {
Status: updatedShare.Status,
Index: updatedShare.ValidatorIndex,
ActivationEpoch: updatedShare.ActivationEpoch,
ExitEpoch: updatedShare.ExitEpoch,
}, "Validator metadata should be updated in validator store")

// Remove the share via SharesStorage
Expand Down Expand Up @@ -465,6 +463,7 @@ func TestSharesStorage_HighContentionConcurrency(t *testing.T) {
Status: updatedShare2.Status,
Index: updatedShare2.ValidatorIndex,
ActivationEpoch: updatedShare2.ActivationEpoch,
ExitEpoch: updatedShare2.ExitEpoch,
},
}))
case "remove1":
Expand Down Expand Up @@ -664,6 +663,7 @@ func fakeParticipatingShare(index phase0.ValidatorIndex, pk spectypes.ValidatorP
},
Status: eth2apiv1.ValidatorStateActiveOngoing,
ActivationEpoch: 4,
ExitEpoch: 0,
OwnerAddress: common.HexToAddress("0xFeedB14D8b2C76FdF808C29818b06b830E8C2c0e"),
Liquidated: false,
}
Expand Down
7 changes: 7 additions & 0 deletions registry/storage/validatorstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var share1 = &ssvtypes.SSVShare{
Graffiti: []byte("example"),
},
ActivationEpoch: 100,
ExitEpoch: 0,
Status: eth2apiv1.ValidatorStatePendingQueued,
OwnerAddress: common.HexToAddress("0x12345"),
Liquidated: false,
Expand All @@ -47,6 +48,7 @@ var share2 = &ssvtypes.SSVShare{
Graffiti: []byte("test"),
},
ActivationEpoch: 200,
ExitEpoch: 0,
Status: eth2apiv1.ValidatorStatePendingQueued,
OwnerAddress: common.HexToAddress("0x67890"),
Liquidated: false,
Expand All @@ -62,6 +64,7 @@ var updatedShare2 = &ssvtypes.SSVShare{
Graffiti: []byte("test"),
},
ActivationEpoch: 200,
ExitEpoch: 250,
Status: eth2apiv1.ValidatorStatePendingQueued,
OwnerAddress: common.HexToAddress("0x67890"),
Liquidated: true,
Expand Down Expand Up @@ -782,6 +785,7 @@ func TestValidatorStore_HandlingDifferentStatuses(t *testing.T) {
Graffiti: []byte("status_test"),
},
ActivationEpoch: 300,
ExitEpoch: 0,
Status: eth2apiv1.ValidatorStateActiveOngoing,
OwnerAddress: common.HexToAddress("0xabcde"),
Liquidated: false,
Expand Down Expand Up @@ -836,6 +840,7 @@ func TestValidatorStore_AddRemoveBulkShares(t *testing.T) {
Graffiti: []byte("bulk_add"),
},
ActivationEpoch: phase0.Epoch(i),
ExitEpoch: 0,
Status: eth2apiv1.ValidatorStatePendingQueued,
OwnerAddress: common.HexToAddress(fmt.Sprintf("0x%x", i)),
Liquidated: false,
Expand Down Expand Up @@ -923,6 +928,7 @@ func TestValidatorStore_InvalidCommitteeHandling(t *testing.T) {
Graffiti: []byte("invalid_committee"),
},
ActivationEpoch: 500,
ExitEpoch: 0,
Status: eth2apiv1.ValidatorStatePendingQueued,
OwnerAddress: common.HexToAddress("0xdeadbeef"),
Liquidated: false,
Expand Down Expand Up @@ -1063,6 +1069,7 @@ func TestValidatorStore_HandleDuplicateSharesAdded(t *testing.T) {
Graffiti: []byte("duplicate_test"),
},
ActivationEpoch: 100,
ExitEpoch: 0,
Status: eth2apiv1.ValidatorStatePendingQueued,
OwnerAddress: common.HexToAddress("0x12345"),
Liquidated: false,
Expand Down

0 comments on commit c1ce6d7

Please sign in to comment.