beacon-chain/rpc: use BalanceAtIndex instead of Balances to reduce memory copy (#13279)

* beacon-chain/rpc: use BalanceAtIndex instead of Balances

Signed-off-by: jsvisa <delweng@gmail.com>

* beacon-chain/rpc: stream use BalanceAtIndex is sufficient

Signed-off-by: jsvisa <delweng@gmail.com>

* beacon-chain/rpc: fix commit review

Signed-off-by: jsvisa <delweng@gmail.com>

* beacon-chain/rpc: http2 -> httputil

Signed-off-by: jsvisa <delweng@gmail.com>

---------

Signed-off-by: jsvisa <delweng@gmail.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
Delweng 2023-12-10 08:04:45 +08:00 committed by GitHub
parent bdfa06ed65
commit 0eb08a4f96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View File

@ -97,7 +97,6 @@ func (s *Server) GetValidators(w http.ResponseWriter, r *http.Request) {
return
}
epoch := slots.ToEpoch(st.Slot())
allBalances := st.Balances()
// Exit early if no matching validators were found or we don't want to further filter validators by status.
if len(readOnlyVals) == 0 || len(statuses) == 0 {
@ -108,11 +107,16 @@ func (s *Server) GetValidators(w http.ResponseWriter, r *http.Request) {
httputil.HandleError(w, "Could not get validator status: "+err.Error(), http.StatusInternalServerError)
return
}
if len(ids) == 0 {
containers[i] = valContainerFromReadOnlyVal(val, primitives.ValidatorIndex(i), allBalances[i], valStatus)
} else {
containers[i] = valContainerFromReadOnlyVal(val, ids[i], allBalances[ids[i]], valStatus)
id := primitives.ValidatorIndex(i)
if len(ids) > 0 {
id = ids[i]
}
balance, err := st.BalanceAtIndex(id)
if err != nil {
httputil.HandleError(w, "Could not get validator balance: "+err.Error(), http.StatusInternalServerError)
return
}
containers[i] = valContainerFromReadOnlyVal(val, id, balance, valStatus)
}
resp := &GetValidatorsResponse{
Data: containers,
@ -146,11 +150,16 @@ func (s *Server) GetValidators(w http.ResponseWriter, r *http.Request) {
}
if filteredStatuses[valStatus] || filteredStatuses[valSubStatus] {
var container *ValidatorContainer
if len(ids) == 0 {
container = valContainerFromReadOnlyVal(val, primitives.ValidatorIndex(i), allBalances[i], valSubStatus)
} else {
container = valContainerFromReadOnlyVal(val, ids[i], allBalances[ids[i]], valSubStatus)
id := primitives.ValidatorIndex(i)
if len(ids) > 0 {
id = ids[i]
}
balance, err := st.BalanceAtIndex(id)
if err != nil {
httputil.HandleError(w, "Could not get validator balance: "+err.Error(), http.StatusInternalServerError)
return
}
container = valContainerFromReadOnlyVal(val, id, balance, valSubStatus)
valContainers = append(valContainers, container)
}
}

View File

@ -325,7 +325,11 @@ func (is *infostream) generateValidatorInfo(
info.Status, info.TransitionTimestamp = is.calculateStatusAndTransition(validator, coreTime.CurrentEpoch(headState))
// Balance
info.Balance = headState.Balances()[info.Index]
if balance, err := headState.BalanceAtIndex(info.Index); err != nil {
return nil, err
} else {
info.Balance = balance
}
// Effective balance (for attesting states)
if info.Status == ethpb.ValidatorStatus_ACTIVE ||