-
Notifications
You must be signed in to change notification settings - Fork 997
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
Refactor RPC to Fully Utilize Ethereum APIs #4243
Changes from 28 commits
4bdb03a
b87e9a4
db691c0
635a50c
303b8fc
3ca03e5
0a027ea
6029165
cf2aa25
7a9ee57
84569fc
d4addeb
e1faae9
3a8773e
7676da9
e9739b5
5445617
2b864fd
5edc4f2
92e8673
f1899ec
3605a1a
8ef3910
d66b3d0
1af1890
511a357
1d79aaf
d48eebe
3f69c78
64c0328
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/core/state" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/core/validators" | ||
"github.com/prysmaticlabs/prysm/shared/bytesutil" | ||
"github.com/prysmaticlabs/prysm/shared/pagination" | ||
|
@@ -187,18 +188,24 @@ func (bs *Server) ListValidators( | |
requestedEpoch = q.Epoch | ||
} | ||
|
||
vals := headState.Validators | ||
validatorList := make([]*ethpb.Validators_ValidatorContainer, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementing: prysmaticlabs/ethereumapis#75 (including validator indices in listvalidators response) |
||
for i := 0; i < len(headState.Validators); i++ { | ||
validatorList = append(validatorList, ðpb.Validators_ValidatorContainer{ | ||
Index: uint64(i), | ||
Validator: headState.Validators[i], | ||
}) | ||
} | ||
if requestedEpoch < currentEpoch { | ||
stopIdx := len(vals) | ||
for idx, val := range vals { | ||
stopIdx := len(validatorList) | ||
for idx, item := range validatorList { | ||
// The first time we see a validator with an activation epoch > the requested epoch, | ||
// we know this validator is from the future relative to what the request wants. | ||
if val.ActivationEpoch > requestedEpoch { | ||
if item.Validator.ActivationEpoch > requestedEpoch { | ||
stopIdx = idx | ||
break | ||
} | ||
} | ||
vals = vals[:stopIdx] | ||
validatorList = validatorList[:stopIdx] | ||
} else if requestedEpoch > currentEpoch { | ||
// Otherwise, we are requesting data from the future and we return an error. | ||
return nil, status.Errorf( | ||
|
@@ -210,12 +217,12 @@ func (bs *Server) ListValidators( | |
} | ||
|
||
// Filter active validators if the request specifies it. | ||
res := vals | ||
res := validatorList | ||
if req.Active { | ||
filteredValidators := make([]*ethpb.Validator, 0) | ||
for _, val := range vals { | ||
if helpers.IsActiveValidator(val, requestedEpoch) { | ||
filteredValidators = append(filteredValidators, val) | ||
filteredValidators := make([]*ethpb.Validators_ValidatorContainer, 0) | ||
for _, item := range validatorList { | ||
if helpers.IsActiveValidator(item.Validator, requestedEpoch) { | ||
filteredValidators = append(filteredValidators, item) | ||
} | ||
} | ||
res = filteredValidators | ||
|
@@ -226,7 +233,7 @@ func (bs *Server) ListValidators( | |
// Otherwise, attempting to paginate 0 validators below would result in an error. | ||
if validatorCount == 0 { | ||
return ðpb.Validators{ | ||
Validators: make([]*ethpb.Validator, 0), | ||
ValidatorList: make([]*ethpb.Validators_ValidatorContainer, 0), | ||
TotalSize: int32(0), | ||
NextPageToken: strconv.Itoa(0), | ||
}, nil | ||
|
@@ -242,7 +249,7 @@ func (bs *Server) ListValidators( | |
} | ||
|
||
return ðpb.Validators{ | ||
Validators: res[start:end], | ||
ValidatorList: res[start:end], | ||
TotalSize: int32(validatorCount), | ||
NextPageToken: nextPageToken, | ||
}, nil | ||
|
@@ -537,3 +544,53 @@ func (bs *Server) GetValidatorQueue( | |
ExitPublicKeys: exitQueueKeys, | ||
}, nil | ||
} | ||
|
||
// GetValidatorPerformance reports the validator's latest balance along with other important metrics on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moves this method to the beacon RPC as it makes more sense to have it there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No new logic |
||
// rewards and penalties throughout its lifecycle in the beacon chain. | ||
func (bs *Server) GetValidatorPerformance( | ||
ctx context.Context, req *ethpb.ValidatorPerformanceRequest, | ||
) (*ethpb.ValidatorPerformanceResponse, error) { | ||
headState, err := bs.HeadFetcher.HeadState(ctx) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, "Could not get head state") | ||
} | ||
|
||
// Advance state with empty transitions up to the requested epoch start slot. | ||
if req.Slot > headState.Slot { | ||
headState, err = state.ProcessSlots(ctx, headState, req.Slot) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not process slots up to %d: %v", req.Slot, err) | ||
} | ||
} | ||
|
||
balances := make([]uint64, len(req.PublicKeys)) | ||
missingValidators := make([][]byte, 0) | ||
for i, key := range req.PublicKeys { | ||
index, ok, err := bs.BeaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(key)) | ||
if err != nil || !ok { | ||
missingValidators = append(missingValidators, key) | ||
balances[i] = 0 | ||
continue | ||
} | ||
balances[i] = headState.Balances[index] | ||
} | ||
|
||
activeCount, err := helpers.ActiveValidatorCount(headState, helpers.SlotToEpoch(req.Slot)) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not retrieve active validator count: %v", err) | ||
} | ||
|
||
totalActiveBalance, err := helpers.TotalActiveBalance(headState) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not retrieve total active balance: %v", err) | ||
} | ||
|
||
avgBalance := float32(totalActiveBalance / activeCount) | ||
return ðpb.ValidatorPerformanceResponse{ | ||
Balances: balances, | ||
AverageActiveValidatorBalance: avgBalance, | ||
MissingValidators: missingValidators, | ||
TotalValidators: uint64(len(headState.Validators)), | ||
TotalActiveValidators: uint64(activeCount), | ||
}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to review, just changing argument types