Skip to content

Commit

Permalink
refactor validators bal
Browse files Browse the repository at this point in the history
Signed-off-by: nidhi-singh02 <trippin@berachain.com>
  • Loading branch information
nidhi-singh02 committed Sep 8, 2024
1 parent 05edaae commit 96daada
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions mod/node-api/handlers/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 96daada

Please sign in to comment.