Skip to content

Commit

Permalink
Revert "Fix ListValidatorBalances for v0.11 (#5458)"
Browse files Browse the repository at this point in the history
This reverts commit 3763a8c.
  • Loading branch information
terencechain committed Apr 16, 2020
1 parent 28733f2 commit 1a5ab73
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 119 deletions.
64 changes: 42 additions & 22 deletions beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,59 @@ func (bs *Server) ListValidatorBalances(
req.PageSize, flags.Get().MaxPageSize)
}

currentEpoch := helpers.SlotToEpoch(bs.GenesisTimeFetcher.CurrentSlot())
requestedEpoch := currentEpoch
res := make([]*ethpb.ValidatorBalances_Balance, 0)
filtered := map[uint64]bool{} // Track filtered validators to prevent duplication in the response.

headState, err := bs.HeadFetcher.HeadState(ctx)
if err != nil {
return nil, status.Error(codes.Internal, "Could not get head state")
}

var requestingGenesis bool
var epoch uint64
switch q := req.QueryFilter.(type) {
case *ethpb.ListValidatorBalancesRequest_Epoch:
requestedEpoch = q.Epoch
epoch = q.Epoch
case *ethpb.ListValidatorBalancesRequest_Genesis:
requestedEpoch = 0
requestingGenesis = q.Genesis
default:
requestedEpoch = currentEpoch
epoch = helpers.CurrentEpoch(headState)
}

if requestedEpoch > currentEpoch {
var balances []uint64
validators := headState.Validators()
if requestingGenesis || epoch < helpers.CurrentEpoch(headState) {
balances, err = bs.BeaconDB.ArchivedBalances(ctx, epoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve balances for epoch %d", epoch)
}
if balances == nil {
return nil, status.Errorf(
codes.NotFound,
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
0,
)
}
} else if epoch == helpers.CurrentEpoch(headState) {
balances = headState.Balances()
} else {
// Otherwise, we are requesting data from the future and we return an error.
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
currentEpoch,
requestedEpoch,
helpers.CurrentEpoch(headState),
epoch,
)
}
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)
index, ok := headState.ValidatorIndexByPubkey(pubkeyBytes)
if !ok {
return nil, status.Errorf(codes.NotFound, "Could not find validator index for public key %#x", pubkeyBytes)
}
Expand All @@ -88,6 +104,10 @@ func (bs *Server) ListValidatorBalances(

for _, index := range req.Indices {
if int(index) >= len(balances) {
if epoch <= helpers.CurrentEpoch(headState) {
return nil, status.Errorf(codes.OutOfRange, "Validator index %d does not exist in historical balances",
index)
}
return nil, status.Errorf(codes.OutOfRange, "Validator index %d >= balance list %d",
index, len(balances))
}
Expand All @@ -110,7 +130,7 @@ func (bs *Server) ListValidatorBalances(
// Otherwise, attempting to paginate 0 balances below would result in an error.
if balancesCount == 0 {
return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Epoch: epoch,
Balances: make([]*ethpb.ValidatorBalances_Balance, 0),
TotalSize: int32(0),
NextPageToken: strconv.Itoa(0),
Expand All @@ -129,23 +149,23 @@ func (bs *Server) ListValidatorBalances(
if len(req.Indices) == 0 && len(req.PublicKeys) == 0 {
// Return everything.
for i := start; i < end; i++ {
pubkey := requestedState.PubkeyAtIndex(uint64(i))
pubkey := headState.PubkeyAtIndex(uint64(i))
res = append(res, &ethpb.ValidatorBalances_Balance{
PublicKey: pubkey[:],
Index: uint64(i),
Balance: balances[i],
})
}
return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Epoch: epoch,
Balances: res,
TotalSize: int32(balancesCount),
NextPageToken: nextPageToken,
}, nil
}

return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Epoch: epoch,
Balances: res[start:end],
TotalSize: int32(balancesCount),
NextPageToken: nextPageToken,
Expand Down
Loading

0 comments on commit 1a5ab73

Please sign in to comment.