From 0eb08a4f9630ea931e91cd5468e2767fbab6c01f Mon Sep 17 00:00:00 2001 From: Delweng Date: Sun, 10 Dec 2023 08:04:45 +0800 Subject: [PATCH] beacon-chain/rpc: use BalanceAtIndex instead of Balances to reduce memory copy (#13279) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * beacon-chain/rpc: use BalanceAtIndex instead of Balances Signed-off-by: jsvisa * beacon-chain/rpc: stream use BalanceAtIndex is sufficient Signed-off-by: jsvisa * beacon-chain/rpc: fix commit review Signed-off-by: jsvisa * beacon-chain/rpc: http2 -> httputil Signed-off-by: jsvisa --------- Signed-off-by: jsvisa Co-authored-by: Radosław Kapka --- .../rpc/eth/beacon/handlers_validator.go | 27 ++++++++++++------- .../v1alpha1/beacon/validators_stream.go | 6 ++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/handlers_validator.go b/beacon-chain/rpc/eth/beacon/handlers_validator.go index fcf541def..06b708fa8 100644 --- a/beacon-chain/rpc/eth/beacon/handlers_validator.go +++ b/beacon-chain/rpc/eth/beacon/handlers_validator.go @@ -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) } } diff --git a/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_stream.go b/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_stream.go index 6f2ef4480..dbb903406 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_stream.go +++ b/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_stream.go @@ -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 ||