Skip to content

Commit

Permalink
Use state instead of multiple validators requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Dec 23, 2023
1 parent a05485e commit 46f0e74
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.19.8
- more efficient fetching for large numbers of validators

0.19.7:
- add endpoint metrics for prometheus

Expand Down
4 changes: 2 additions & 2 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "go-eth2-client/0.19.7")
req.Header.Set("User-Agent", "go-eth2-client/0.19.8")
}

resp, err := s.client.Do(req)
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *Service) post2(ctx context.Context,
req.Header.Set(k, v)
}
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "go-eth2-client/0.19.7")
req.Header.Set("User-Agent", "go-eth2-client/0.19.8")
}

resp, err := s.client.Do(req)
Expand Down
29 changes: 29 additions & 0 deletions http/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func (s *Service) Validators(ctx context.Context,
return s.validatorsFromState(ctx, opts)
}

if len(opts.Indices) > indexChunkSizes["default"]*2 || len(opts.PubKeys) > pubKeyChunkSizes["default"]*2 {
// Request is for multiple pages of validators; fetch from state.
return s.validatorsFromState(ctx, opts)
}

if len(opts.Indices) > s.indexChunkSize(ctx) || len(opts.PubKeys) > s.pubKeyChunkSize(ctx) {
return s.chunkedValidators(ctx, opts)
}
Expand Down Expand Up @@ -217,9 +222,33 @@ func (s *Service) validatorsFromState(ctx context.Context,
return nil, err
}

// Provide map of required pubkeys or indices.
indices := make(map[phase0.ValidatorIndex]struct{})
for _, index := range opts.Indices {
indices[index] = struct{}{}
}
pubkeys := make(map[phase0.BLSPubKey]struct{})
for _, pubkey := range opts.PubKeys {
pubkeys[pubkey] = struct{}{}
}

res := make(map[phase0.ValidatorIndex]*apiv1.Validator, len(validators))
for i, validator := range validators {
if len(pubkeys) > 0 {
if _, exists := pubkeys[validator.PublicKey]; !exists {
// We want specific public keys, and this isn't one of them. Ignore.
continue
}
}

index := phase0.ValidatorIndex(i)
if len(indices) > 0 {
if _, exists := indices[index]; !exists {
// We want specific indices, and this isn't one of them. Ignore.
continue
}
}

state := apiv1.ValidatorToState(validator, &balances[i], epoch, farFutureEpoch)
res[index] = &apiv1.Validator{
Index: index,
Expand Down

0 comments on commit 46f0e74

Please sign in to comment.