Skip to content

Commit a02af3e

Browse files
committed
improve performance and nice to have empty slices checks
1 parent 633919d commit a02af3e

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

internal/adapters/beacon/beacon.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
v1 "github.com/attestantio/go-eth2-client/api/v1"
1212
"github.com/dappnode/validator-tracker/internal/application/domain"
1313
"github.com/dappnode/validator-tracker/internal/application/ports"
14+
"github.com/dappnode/validator-tracker/internal/logger"
1415
"github.com/rs/zerolog"
1516

1617
"github.com/attestantio/go-eth2-client/api"
@@ -59,12 +60,15 @@ func (b *beaconAttestantClient) GetJustifiedEpoch(ctx context.Context) (domain.E
5960
return domain.Epoch(finality.Data.Justified.Epoch), nil
6061
}
6162

62-
// internal/adapters/beaconchain_adapter.go
6363
func (b *beaconAttestantClient) GetValidatorDutiesBatch(ctx context.Context, epoch domain.Epoch, validatorIndices []domain.ValidatorIndex) ([]domain.ValidatorDuty, error) {
64-
// Convert to phase0.ValidatorIndex
65-
var indices []phase0.ValidatorIndex
66-
for _, idx := range validatorIndices {
67-
indices = append(indices, phase0.ValidatorIndex(idx))
64+
if len(validatorIndices) == 0 {
65+
logger.Debug("Called GetValidatorDutiesBatch with no validator indices, returning empty slice. Nothing to check.")
66+
return nil, nil
67+
}
68+
69+
indices := make([]phase0.ValidatorIndex, len(validatorIndices))
70+
for i, idx := range validatorIndices {
71+
indices[i] = phase0.ValidatorIndex(idx)
6872
}
6973

7074
duties, err := b.client.AttesterDuties(ctx, &api.AttesterDutiesOpts{
@@ -82,7 +86,7 @@ func (b *beaconAttestantClient) GetValidatorDutiesBatch(ctx context.Context, epo
8286
Slot: domain.Slot(d.Slot),
8387
CommitteeIndex: domain.CommitteeIndex(d.CommitteeIndex),
8488
ValidatorCommitteeIdx: d.ValidatorCommitteeIndex,
85-
ValidatorIndex: domain.ValidatorIndex(d.ValidatorIndex), // new field
89+
ValidatorIndex: domain.ValidatorIndex(d.ValidatorIndex),
8690
})
8791
}
8892

@@ -152,6 +156,11 @@ func (b *beaconAttestantClient) GetBlockAttestations(ctx context.Context, slot d
152156
}
153157

154158
func (b *beaconAttestantClient) GetValidatorIndicesByPubkeys(ctx context.Context, pubkeys []string) ([]domain.ValidatorIndex, error) {
159+
if len(pubkeys) == 0 {
160+
logger.Debug("Called GetValidatorIndicesByPubkeys with no pubkeys, nothing to check")
161+
return nil, nil
162+
}
163+
155164
var beaconPubkeys []phase0.BLSPubKey
156165

157166
// Convert hex pubkeys to BLS pubkeys
@@ -196,9 +205,14 @@ func (b *beaconAttestantClient) GetValidatorIndicesByPubkeys(ctx context.Context
196205

197206
// GetProposerDuties retrieves proposer duties for the given epoch and validator indices.
198207
func (b *beaconAttestantClient) GetProposerDuties(ctx context.Context, epoch domain.Epoch, indices []domain.ValidatorIndex) ([]domain.ProposerDuty, error) {
199-
var beaconIndices []phase0.ValidatorIndex
200-
for _, idx := range indices {
201-
beaconIndices = append(beaconIndices, phase0.ValidatorIndex(idx))
208+
if len(indices) == 0 {
209+
logger.Debug("Called GetProposerDuties with no validator indices, returning empty slice. Nothing to check.")
210+
return nil, nil
211+
}
212+
213+
beaconIndices := make([]phase0.ValidatorIndex, len(indices))
214+
for i, idx := range indices {
215+
beaconIndices[i] = phase0.ValidatorIndex(idx)
202216
}
203217

204218
resp, err := b.client.ProposerDuties(ctx, &api.ProposerDutiesOpts{
@@ -236,10 +250,14 @@ func (b *beaconAttestantClient) DidProposeBlock(ctx context.Context, slot domain
236250
}
237251

238252
func (b *beaconAttestantClient) GetValidatorsLiveness(ctx context.Context, epoch domain.Epoch, indices []domain.ValidatorIndex) (map[domain.ValidatorIndex]bool, error) {
239-
// Convert to phase0.ValidatorIndex
240-
var beaconIndices []phase0.ValidatorIndex
241-
for _, idx := range indices {
242-
beaconIndices = append(beaconIndices, phase0.ValidatorIndex(idx))
253+
if len(indices) == 0 {
254+
logger.Debug("Called GetValidatorsLiveness with no validator indices, returning empty map. Nothing to check.")
255+
return map[domain.ValidatorIndex]bool{}, nil
256+
}
257+
258+
beaconIndices := make([]phase0.ValidatorIndex, len(indices))
259+
for i, idx := range indices {
260+
beaconIndices[i] = phase0.ValidatorIndex(idx)
243261
}
244262

245263
liveness, err := b.client.ValidatorLiveness(ctx, &api.ValidatorLivenessOpts{
@@ -260,6 +278,7 @@ func (b *beaconAttestantClient) GetValidatorsLiveness(ctx context.Context, epoch
260278
// GetSlashedValidators retrieves the indices of slashed validators in the justified state.
261279
func (b *beaconAttestantClient) GetSlashedValidators(ctx context.Context, indices []domain.ValidatorIndex) ([]domain.ValidatorIndex, error) {
262280
if len(indices) == 0 {
281+
logger.Debug("Called GetSlashedValidators with no validator indices, returning empty slice. Nothing to check.")
263282
// No validators to check; return immediately. Nice to have
264283
return nil, nil
265284
}

0 commit comments

Comments
 (0)