Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back archival endpoint GetValidatorBalances with fallback #5620

Merged
merged 25 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
40b6ee4
Fix ListValidatorBalances
terencechain Apr 16, 2020
a4902e4
Fixed all the tests
terencechain Apr 16, 2020
3dd6c3f
Merge refs/heads/master into fix-list-balances
prylabs-bulldozer[bot] Apr 16, 2020
8b4e3e7
Use requestedEpoch as default
terencechain Apr 16, 2020
32f9ab1
Merge branch 'fix-list-balances' of github.com:prysmaticlabs/prysm in…
terencechain Apr 16, 2020
81fa1b8
Fixed a test
terencechain Apr 16, 2020
fe3ae95
Merge branch 'fix-list-balances' into validator-balances-fallback
terencechain Apr 25, 2020
8fc74c8
Add fallback
terencechain Apr 25, 2020
af46edc
Fixed test
terencechain Apr 25, 2020
b1d5088
Fixed a few more tests
terencechain Apr 25, 2020
85886a8
Addback benchmark
terencechain Apr 25, 2020
cfb2c81
Merge branch 'master' into validator-balances-fallback
terencechain Apr 25, 2020
2d1d43e
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 25, 2020
0a4c889
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 25, 2020
d0d3b42
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 26, 2020
94a51a5
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 26, 2020
7cf4bbe
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 26, 2020
9813df8
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 26, 2020
8ac42bb
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 26, 2020
c311f0d
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 26, 2020
cc08214
Victor's feedback
terencechain Apr 27, 2020
20243b8
Merge branch 'validator-balances-fallback' of github.com:prysmaticlab…
terencechain Apr 27, 2020
c9dafdd
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 27, 2020
804ab6a
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 27, 2020
9051ddb
Merge refs/heads/master into validator-balances-fallback
prylabs-bulldozer[bot] Apr 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions beacon-chain/rpc/beacon/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ func TestServer_ListIndexedAttestations_NewStateManagnmentDisabled(t *testing.T)
func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) {
params.OverrideBeaconConfig(params.MainnetConfig())
defer params.OverrideBeaconConfig(params.MinimalSpecConfig())

config := &featureconfig.Flags{
NewStateMgmt: true,
}
featureconfig.Init(config)

farazdagi marked this conversation as resolved.
Show resolved Hide resolved
cfg := assertNewStateMgmtIsEnabled()
defer featureconfig.Init(cfg)
db := dbTest.SetupDB(t)
Expand Down
129 changes: 129 additions & 0 deletions beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,135 @@ func (bs *Server) ListValidatorBalances(
req.PageSize, flags.Get().MaxPageSize)
}

if !featureconfig.Get().NewStateMgmt {
return bs.listValidatorsBalancesUsingOldArchival(ctx, req)
}

currentEpoch := helpers.SlotToEpoch(bs.GenesisTimeFetcher.CurrentSlot())
requestedEpoch := currentEpoch
switch q := req.QueryFilter.(type) {
case *ethpb.ListValidatorBalancesRequest_Epoch:
requestedEpoch = q.Epoch
case *ethpb.ListValidatorBalancesRequest_Genesis:
requestedEpoch = 0
default:
requestedEpoch = currentEpoch
}

if requestedEpoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
currentEpoch,
requestedEpoch,
)
}
res := make([]*ethpb.ValidatorBalances_Balance, 0)
filtered := map[uint64]bool{} // Track filtered validators to prevent duplication in the response.

requestedState, err := bs.StateGen.StateBySlot(ctx, helpers.StartSlot(requestedEpoch))
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get state")
}

validators := requestedState.Validators()
balances := requestedState.Balances()
balancesCount := len(balances)
for _, pubKey := range req.PublicKeys {
// Skip empty public key.
if len(pubKey) == 0 {
continue
}
pubkeyBytes := bytesutil.ToBytes48(pubKey)
index, ok := requestedState.ValidatorIndexByPubkey(pubkeyBytes)
if !ok {
return nil, status.Errorf(codes.NotFound, "Could not find validator index for public key %#x", pubkeyBytes)
}

filtered[index] = true

if int(index) >= len(balances) {
return nil, status.Errorf(codes.OutOfRange, "Validator index %d >= balance list %d",
index, len(balances))
}

res = append(res, &ethpb.ValidatorBalances_Balance{
PublicKey: pubKey,
Index: index,
Balance: balances[index],
})
balancesCount = len(res)
}

for _, index := range req.Indices {
if int(index) >= len(balances) {
return nil, status.Errorf(codes.OutOfRange, "Validator index %d >= balance list %d",
index, len(balances))
}

if !filtered[index] {
res = append(res, &ethpb.ValidatorBalances_Balance{
PublicKey: validators[index].PublicKey,
Index: index,
Balance: balances[index],
})
}
balancesCount = len(res)
}
// Depending on the indices and public keys given, results might not be sorted.
sort.Slice(res, func(i, j int) bool {
return res[i].Index < res[j].Index
})

// If there are no balances, we simply return a response specifying this.
// Otherwise, attempting to paginate 0 balances below would result in an error.
if balancesCount == 0 {
return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Balances: make([]*ethpb.ValidatorBalances_Balance, 0),
TotalSize: int32(0),
NextPageToken: strconv.Itoa(0),
}, nil
}

start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), balancesCount)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not paginate results: %v",
err,
)
}

if len(req.Indices) == 0 && len(req.PublicKeys) == 0 {
// Return everything.
for i := start; i < end; i++ {
pubkey := requestedState.PubkeyAtIndex(uint64(i))
res = append(res, &ethpb.ValidatorBalances_Balance{
PublicKey: pubkey[:],
Index: uint64(i),
Balance: balances[i],
})
}
return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Balances: res,
TotalSize: int32(balancesCount),
NextPageToken: nextPageToken,
}, nil
}

return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Balances: res[start:end],
TotalSize: int32(balancesCount),
NextPageToken: nextPageToken,
}, nil
}

func (bs *Server) listValidatorsBalancesUsingOldArchival(
ctx context.Context,
req *ethpb.ListValidatorBalancesRequest) (*ethpb.ValidatorBalances, error) {
res := make([]*ethpb.ValidatorBalances_Balance, 0)
filtered := map[uint64]bool{} // Track filtered validators to prevent duplication in the response.

Expand Down
Loading