Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoSantoss committed Feb 5, 2025
1 parent 914866a commit 0d157f6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/eth2wrap/eth2wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion app/eth2wrap/eth2wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -199,6 +198,7 @@ func TestFallback(t *testing.T) {
calledMu.Lock()
primaryCalled[i] = true
calledMu.Unlock()

return returnValue, primaryErr
}
primaryClients[i] = cl
Expand All @@ -214,6 +214,7 @@ func TestFallback(t *testing.T) {
calledMu.Lock()
fallbackCalled[i] = true
calledMu.Unlock()

return returnValue, fallbackErr
}
fallbackClients[i] = cl
Expand Down
22 changes: 12 additions & 10 deletions app/eth2wrap/valcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand All @@ -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
}

Expand All @@ -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")
}
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions app/eth2wrap/valcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -138,7 +137,7 @@ func TestGetBySlot(t *testing.T) {
},
}, nil
default:
return nil, nil
return beaconmock.ValidatorSet{}, nil
}
}

Expand All @@ -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)
Expand Down

0 comments on commit 0d157f6

Please sign in to comment.