From 96daada7205ed2976ed85bfc2fc57783f03f376b Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Sun, 8 Sep 2024 13:24:11 +0530 Subject: [PATCH] refactor validators bal Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/beacon/validators.go | 49 ++++++++++------------ 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/mod/node-api/handlers/beacon/validators.go b/mod/node-api/handlers/beacon/validators.go index 263c9f21f2..68b34d6146 100644 --- a/mod/node-api/handlers/beacon/validators.go +++ b/mod/node-api/handlers/beacon/validators.go @@ -156,35 +156,27 @@ func convertValidator[ValidatorT any]( } func convertHexFields(v *CustomValidator) error { - var err error - v.EffectiveBalance, err = hexToDecimalString(v.EffectiveBalance) - if err != nil { - return errors.Wrap(err, "failed to convert effective balance") - } - v.ActivationEligibilityEpoch, err = hexToDecimalString( - v.ActivationEligibilityEpoch, - ) - if err != nil { - return errors.Wrap(err, "failed to convert activation eligibility epoch") - } - v.ActivationEpoch, err = hexToDecimalString(v.ActivationEpoch) - if err != nil { - return errors.Wrap(err, "failed to convert activation epoch") - } - v.ExitEpoch, err = hexToDecimalString(v.ExitEpoch) - if err != nil { - return errors.Wrap(err, "failed to convert exit epoch") + fields := []*string{ + &v.EffectiveBalance, + &v.ActivationEligibilityEpoch, + &v.ActivationEpoch, + &v.ExitEpoch, + &v.WithdrawableEpoch, } - v.WithdrawableEpoch, err = hexToDecimalString(v.WithdrawableEpoch) - if err != nil { - return errors.Wrap(err, "failed to convert withdrawable epoch") + + for _, field := range fields { + converted, err := hexToDecimalString(*field) + if err != nil { + return errors.Wrap(err, "failed to convert hex field") + } + *field = converted } + return nil } func hexToDecimalString(hexStr string) (string, error) { hexStr = strings.TrimPrefix(hexStr, "0x") - // Convert hex string to uint64 value, err := strconv.ParseUint(hexStr, 16, 64) if err != nil { @@ -247,32 +239,33 @@ func (h *Handler[_, ContextT, _, _]) GetStateValidatorBalances( func (h *Handler[_, ContextT, _, _]) PostStateValidatorBalances( c ContextT, ) (any, error) { + var ids []string if err := c.Bind(&ids); err != nil { - return nil, errors.Wrapf(errors.New("err in func bind"), "err %v", err) + return nil, types.ErrInvalidRequest } + // TODO: Find a way to pass the state_id from request. + // Currently only head is supported. req := beacontypes.PostValidatorBalancesRequest{ StateIDRequest: types.StateIDRequest{StateID: "head"}, IDs: ids, } if err := c.Validate(&req); err != nil { - return nil, errors.Wrapf(errors.New("err in validate"), "err %v", err) + return nil, types.ErrInvalidRequest } slot, err := utils.SlotFromStateID(req.StateID, h.backend) if err != nil { - return nil, errors.Wrapf( - errors.New("err in getting slot "), - " slot req err %v %v %v", req, slot, err) + return nil, errors.Wrapf(err, "err getting slot for req %v ", req) } h.Logger().Info("PostStateValidatorBalances", "slot", slot, "req", req) balances, err := h.backend.ValidatorBalancesByIDs(slot, req.IDs) if err != nil { - return nil, errors.Wrapf(errors.New("err in backend "), "err %v", err) + return nil, errors.Wrap(err, "err in backend") } return beacontypes.ValidatorResponse{ ExecutionOptimistic: false, // stubbed