From 0d157f62e0233b3a78d2bd3af73e809289149f52 Mon Sep 17 00:00:00 2001 From: Diogo Santos Date: Wed, 5 Feb 2025 13:49:49 +0000 Subject: [PATCH] formatting --- app/eth2wrap/eth2wrap.go | 1 + app/eth2wrap/eth2wrap_test.go | 3 ++- app/eth2wrap/valcache.go | 22 ++++++++++++---------- app/eth2wrap/valcache_test.go | 4 ++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/eth2wrap/eth2wrap.go b/app/eth2wrap/eth2wrap.go index 50d1f62ed..8d8cb5bc4 100644 --- a/app/eth2wrap/eth2wrap.go +++ b/app/eth2wrap/eth2wrap.go @@ -167,6 +167,7 @@ func provide[O any](ctx context.Context, clients []Client, fallbacks []Client, if bestSelector != nil { bestSelector.Increment(res.Input.client.Address()) } + return res.Output, nil } diff --git a/app/eth2wrap/eth2wrap_test.go b/app/eth2wrap/eth2wrap_test.go index 527096821..0aa31f066 100644 --- a/app/eth2wrap/eth2wrap_test.go +++ b/app/eth2wrap/eth2wrap_test.go @@ -179,7 +179,6 @@ func TestFallback(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var calledMu sync.Mutex primaryCalled := make([]bool, len(tt.primaryErrs)) fallbackCalled := make([]bool, len(tt.fallbackErrs)) @@ -199,6 +198,7 @@ func TestFallback(t *testing.T) { calledMu.Lock() primaryCalled[i] = true calledMu.Unlock() + return returnValue, primaryErr } primaryClients[i] = cl @@ -214,6 +214,7 @@ func TestFallback(t *testing.T) { calledMu.Lock() fallbackCalled[i] = true calledMu.Unlock() + return returnValue, fallbackErr } fallbackClients[i] = cl diff --git a/app/eth2wrap/valcache.go b/app/eth2wrap/valcache.go index 0f1b84189..55c23ab82 100644 --- a/app/eth2wrap/valcache.go +++ b/app/eth2wrap/valcache.go @@ -17,6 +17,11 @@ import ( "github.com/obolnetwork/charon/app/z" ) +var ( + maxRetries = 20 + retryDelay = 100 * time.Millisecond +) + // ActiveValidators is a map of active validator indices to pubkeys. type ActiveValidators map[eth2p0.ValidatorIndex]eth2p0.BLSPubKey @@ -139,7 +144,6 @@ func (c *ValidatorCache) Get(ctx context.Context) (ActiveValidators, CompleteVal // GetBySlot fetches active and complete validator by slot populating the cache. func (c *ValidatorCache) GetBySlot(ctx context.Context, slot uint64) (ActiveValidators, CompleteValidators, error) { - c.mu.Lock() defer c.mu.Unlock() @@ -150,12 +154,10 @@ func (c *ValidatorCache) GetBySlot(ctx context.Context, slot uint64) (ActiveVali var eth2Resp *eth2api.Response[map[eth2p0.ValidatorIndex]*eth2v1.Validator] var err error - maxRetries := 20 - retryDelay := 100 * time.Millisecond for retryCount := 0; retryCount < maxRetries; retryCount++ { eth2Resp, err = c.eth2Cl.Validators(ctx, opts) - if err == nil { + if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { break } @@ -170,10 +172,10 @@ func (c *ValidatorCache) GetBySlot(ctx context.Context, slot uint64) (ActiveVali return nil, nil, wrapError(ctx, err, "Failed to fetch validators by slot after maximum retries") } - vals := eth2Resp.Data + complete := eth2Resp.Data - resp := make(ActiveValidators) - for _, val := range vals { + active := make(ActiveValidators) + for _, val := range complete { if val == nil || val.Validator == nil { return nil, nil, errors.New("validator data cannot be nil") } @@ -182,11 +184,11 @@ func (c *ValidatorCache) GetBySlot(ctx context.Context, slot uint64) (ActiveVali continue } - resp[val.Index] = val.Validator.PublicKey + active[val.Index] = val.Validator.PublicKey } - c.active = resp + c.active = active c.complete = eth2Resp.Data - return resp, eth2Resp.Data, nil + return active, eth2Resp.Data, nil } diff --git a/app/eth2wrap/valcache_test.go b/app/eth2wrap/valcache_test.go index 43daaab42..4ae65ce66 100644 --- a/app/eth2wrap/valcache_test.go +++ b/app/eth2wrap/valcache_test.go @@ -90,7 +90,6 @@ func TestValidatorCache(t *testing.T) { } func TestGetBySlot(t *testing.T) { - // Create a mock client. eth2Cl, err := beaconmock.New() require.NoError(t, err) @@ -138,7 +137,7 @@ func TestGetBySlot(t *testing.T) { }, }, nil default: - return nil, nil + return beaconmock.ValidatorSet{}, nil } } @@ -148,6 +147,7 @@ func TestGetBySlot(t *testing.T) { active, complete, err := valCache.GetBySlot(ctx, 1) require.NoError(t, err) require.Len(t, active, 1) + require.Equal(t, pubkeys[1], active[1]) require.Len(t, complete, 2) active, complete, err = valCache.GetBySlot(ctx, 2)