diff --git a/node-api/backend/getters.go b/node-api/backend/getters.go index 732de98c52..61d308f59f 100644 --- a/node-api/backend/getters.go +++ b/node-api/backend/getters.go @@ -28,9 +28,11 @@ import ( "cosmossdk.io/log" cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service" datypes "github.com/berachain/beacon-kit/da/types" + "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/common" "github.com/berachain/beacon-kit/primitives/math" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/version" ) @@ -52,7 +54,8 @@ func (b *Backend) StateAndSlotFromHeight(height int64) (ReadOnlyBeaconState, mat return nil, 0, fmt.Errorf("failed loading genesis state: %w", err) } case errors.Is(err, cometbft.ErrAppNotReady): - return nil, 0, cometbft.ErrAppNotReady + // chain not ready, like when genesis time is set in the future + return nil, 0, middleware.ErrNotFound default: return nil, 0, fmt.Errorf("unable to check whether app is ready: %w", err) } @@ -71,6 +74,14 @@ func (b *Backend) StateAndSlotFromHeight(height int64) (ReadOnlyBeaconState, mat height = max(0, height) // CreateQueryContext uses 0 to pick latest height. queryCtx, err := b.node.CreateQueryContext(height, false) if err != nil { + if errors.Is(err, cometbft.ErrAppNotReady) { + // chain not ready, like when genesis time is set in the future + return nil, 0, middleware.ErrNotFound + } + if errors.Is(err, sdkerrors.ErrInvalidHeight) { + // height requested too high + return nil, 0, middleware.ErrNotFound + } return nil, 0, fmt.Errorf("CreateQueryContext failed: %w", err) } st := b.sb.StateFromContext(queryCtx) diff --git a/node-api/handlers/beacon/blob_test.go b/node-api/handlers/beacon/blob_test.go index e46995a8b2..ee3c74913d 100644 --- a/node-api/handlers/beacon/blob_test.go +++ b/node-api/handlers/beacon/blob_test.go @@ -35,8 +35,8 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/common" "github.com/berachain/beacon-kit/primitives/math" @@ -77,7 +77,7 @@ func TestGetBlobSidecars(t *testing.T) { inputs: func() beacontypes.GetBlobSidecarsRequest { return beacontypes.GetBlobSidecarsRequest{ BlockIDRequest: handlertypes.BlockIDRequest{ - BlockID: utils.StateIDHead, + BlockID: mapping.StateIDHead, }, Indices: nil, } diff --git a/node-api/handlers/beacon/blobs.go b/node-api/handlers/beacon/blobs.go index 6d07c01f9d..55953a5a2a 100644 --- a/node-api/handlers/beacon/blobs.go +++ b/node-api/handlers/beacon/blobs.go @@ -26,6 +26,7 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers" "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/primitives/math" ) @@ -43,13 +44,13 @@ func (h *Handler) GetBlobSidecars(c handlers.Context) (any, error) { } // Map requested blockID to slot - slotID, err := utils.BlockIDToHeight(req.BlockID, h.backend) + slotID, err := mapping.BlockIDToHeight(req.BlockID, h.backend) if err != nil { return nil, err } var slot math.Slot - if slotID == utils.Head { + if slotID == mapping.Head { latestHeight, _ := h.backend.GetSyncData() if latestHeight < 0 { return nil, errors.New("invalid negative block height") diff --git a/node-api/handlers/beacon/genesis.go b/node-api/handlers/beacon/genesis.go index 7f508ff076..c9613d990c 100644 --- a/node-api/handlers/beacon/genesis.go +++ b/node-api/handlers/beacon/genesis.go @@ -21,23 +21,16 @@ package beacon import ( - "errors" "fmt" - cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service" "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" ) func (h *Handler) GetGenesis(handlers.Context) (any, error) { - st, _, err := h.backend.StateAndSlotFromHeight(utils.Genesis) + st, _, err := h.backend.StateAndSlotFromHeight(mapping.Genesis) if err != nil { - if errors.Is(err, cometbft.ErrAppNotReady) { - // chain not ready, like when genesis time is set in the future - return nil, handlertypes.ErrNotFound - } return nil, fmt.Errorf("failed to get state from genesis: %w", err) } diff --git a/node-api/handlers/beacon/genesis_test.go b/node-api/handlers/beacon/genesis_test.go index 5fcf26b164..81d4b1d941 100644 --- a/node-api/handlers/beacon/genesis_test.go +++ b/node-api/handlers/beacon/genesis_test.go @@ -32,13 +32,11 @@ import ( "github.com/berachain/beacon-kit/chain" "github.com/berachain/beacon-kit/config/spec" ctypes "github.com/berachain/beacon-kit/consensus-types/types" - cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service" beaconlog "github.com/berachain/beacon-kit/log" "github.com/berachain/beacon-kit/log/noop" "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - "github.com/berachain/beacon-kit/node-api/handlers/types" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/node-core/components/metrics" "github.com/berachain/beacon-kit/primitives/common" @@ -104,12 +102,12 @@ func TestGetGenesis(t *testing.T) { { name: "genesis not ready", setMockExpectations: func(b *mocks.Backend) { - b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, 0, cometbft.ErrAppNotReady) + b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, 0, middleware.ErrNotFound) }, check: func(t *testing.T, res any, err error) { t.Helper() - require.ErrorIs(t, err, types.ErrNotFound) + require.ErrorIs(t, err, middleware.ErrNotFound) require.Nil(t, res) }, }, diff --git a/node-api/handlers/beacon/header.go b/node-api/handlers/beacon/header.go index a7b729ecc6..dcbc81d19c 100644 --- a/node-api/handlers/beacon/header.go +++ b/node-api/handlers/beacon/header.go @@ -27,8 +27,9 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/math" ) @@ -44,7 +45,7 @@ func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) { case len(req.Slot) == 0 && len(req.ParentRoot) == 0: // no parameter specified, pick chain HEAD // by requesting special height -1. - height := utils.Head + height := mapping.Head return h.makeBlockHeaderResponse(height, true /*resultsInList*/) case len(req.Slot) != 0 && len(req.ParentRoot) == 0: @@ -54,17 +55,17 @@ func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) { return nil, fmt.Errorf("failed retrieving slot from input parameters: %w", errSlot) } if slot > stdmath.MaxInt64 { // appease linters - return 0, fmt.Errorf("%w: slot %d", utils.ErrFailedMappingHeightTooHigh, slot) + return 0, fmt.Errorf("%w: slot %d", mapping.ErrFailedMappingHeightTooHigh, slot) } return h.makeBlockHeaderResponse(int64(slot), true /*resultsInList*/) //#nosec: G115 // safe case len(req.Slot) == 0 && len(req.ParentRoot) != 0: - parentHeight, errParent := utils.BlockIDToHeight(req.ParentRoot, h.backend) + parentHeight, errParent := mapping.BlockIDToHeight(req.ParentRoot, h.backend) if errParent != nil { - return nil, fmt.Errorf("%w, failed retrieving parent root with error: %w", handlertypes.ErrNotFound, errParent) + return nil, fmt.Errorf("%w, failed retrieving parent root with error: %w", middleware.ErrNotFound, errParent) } - if parentHeight == utils.Head { - return nil, fmt.Errorf("%w, requested header of tip's child", handlertypes.ErrNotFound) + if parentHeight == mapping.Head { + return nil, fmt.Errorf("%w, requested header of tip's child", middleware.ErrNotFound) } height := parentHeight + 1 return h.makeBlockHeaderResponse(height, true /*resultsInList*/) @@ -72,14 +73,14 @@ func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) { default: var ( slot, errSlot = math.U64FromString(req.Slot) - parentSlot, errParent = utils.BlockIDToHeight(req.ParentRoot, h.backend) + parentSlot, errParent = mapping.BlockIDToHeight(req.ParentRoot, h.backend) ) if err := errors.Join(errSlot, errParent); err != nil { return nil, err } if slot > stdmath.MaxInt64 { // appease linters - return 0, fmt.Errorf("%w: slot %d", utils.ErrFailedMappingHeightTooHigh, slot) + return 0, fmt.Errorf("%w: slot %d", mapping.ErrFailedMappingHeightTooHigh, slot) } height := int64(slot) //#nosec: G115 // safe if height != parentSlot+1 { @@ -94,7 +95,7 @@ func (h *Handler) GetBlockHeaderByID(c handlers.Context) (any, error) { if err != nil { return nil, err } - slot, err := utils.BlockIDToHeight(req.BlockID, h.backend) + slot, err := mapping.BlockIDToHeight(req.BlockID, h.backend) if err != nil { return nil, fmt.Errorf("failed retrieving slot from block ID %s: %w", req.BlockID, err) } @@ -104,7 +105,7 @@ func (h *Handler) GetBlockHeaderByID(c handlers.Context) (any, error) { func (h *Handler) makeBlockHeaderResponse(height int64, resultsInList bool) (any, error) { st, _, err := h.backend.StateAndSlotFromHeight(height) if err != nil { - return nil, fmt.Errorf("%w: failed to get state from height %d, %s", handlertypes.ErrNotFound, height, err.Error()) + return nil, fmt.Errorf("failed to get state from height %d, %w", height, err) } // Return after updating the state root in the block header. header, err := st.GetLatestBlockHeader() diff --git a/node-api/handlers/beacon/header_test.go b/node-api/handlers/beacon/header_test.go index 7df6b3219e..f2c30d0fbc 100644 --- a/node-api/handlers/beacon/header_test.go +++ b/node-api/handlers/beacon/header_test.go @@ -172,7 +172,7 @@ func TestGetBlockHeaders(t *testing.T) { }, check: func(t *testing.T, _ common.Root, _ any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrInvalidRequest) + require.ErrorIs(t, err, middleware.ErrInvalidRequest) }, }, { @@ -187,14 +187,14 @@ func TestGetBlockHeaders(t *testing.T) { }, setMockExpectations: func(t *testing.T, b *mocks.Backend) common.Root { t.Helper() - b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), errTestHeaderNotFound) + b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), middleware.ErrNotFound) return common.Root{} }, check: func(t *testing.T, _ common.Root, _ any, err error) { t.Helper() // Implicitly ensuring that 404 error code is returned // (see responseFromError implementation) - require.ErrorIs(t, err, handlertypes.ErrNotFound) + require.ErrorIs(t, err, middleware.ErrNotFound) }, }, { @@ -250,7 +250,7 @@ func TestGetBlockHeaders(t *testing.T) { }, check: func(t *testing.T, _ common.Root, _ any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrInvalidRequest) + require.ErrorIs(t, err, middleware.ErrInvalidRequest) }, }, { @@ -270,7 +270,7 @@ func TestGetBlockHeaders(t *testing.T) { t.Helper() // Implicitly ensuring that 404 error code is returned // (see responseFromError implementation) - require.ErrorIs(t, err, handlertypes.ErrNotFound) + require.ErrorIs(t, err, middleware.ErrNotFound) }, }, { @@ -444,7 +444,7 @@ func TestGetBlockHeaderByID(t *testing.T) { }, check: func(t *testing.T, _ common.Root, _ any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrInvalidRequest) + require.ErrorIs(t, err, middleware.ErrInvalidRequest) }, }, { @@ -458,7 +458,7 @@ func TestGetBlockHeaderByID(t *testing.T) { }, setMockExpectations: func(t *testing.T, b *mocks.Backend) common.Root { t.Helper() - b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), errTestHeaderNotFound) + b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), middleware.ErrNotFound) return common.Root{} }, check: func(t *testing.T, _ common.Root, _ any, err error) { @@ -466,7 +466,7 @@ func TestGetBlockHeaderByID(t *testing.T) { // Implicitly ensuring that 404 error code is returned // (see responseFromError implementation) - require.ErrorIs(t, err, handlertypes.ErrNotFound) + require.ErrorIs(t, err, middleware.ErrNotFound) }, }, { @@ -523,7 +523,7 @@ func TestGetBlockHeaderByID(t *testing.T) { }, check: func(t *testing.T, _ common.Root, _ any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrInvalidRequest) + require.ErrorIs(t, err, middleware.ErrInvalidRequest) }, }, { diff --git a/node-api/handlers/beacon/historical.go b/node-api/handlers/beacon/historical.go index 358ac23eed..37b360d5e9 100644 --- a/node-api/handlers/beacon/historical.go +++ b/node-api/handlers/beacon/historical.go @@ -23,6 +23,7 @@ package beacon import ( "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" ) @@ -33,7 +34,7 @@ func (h *Handler) GetStateRoot(c handlers.Context) (any, error) { if err != nil { return nil, err } - slot, err := utils.StateIDToHeight(req.StateID, h.backend) + slot, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } @@ -51,7 +52,7 @@ func (h *Handler) GetStateFork(c handlers.Context) (any, error) { if err != nil { return nil, err } - slot, err := utils.StateIDToHeight(req.StateID, h.backend) + slot, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } diff --git a/node-api/handlers/beacon/randao.go b/node-api/handlers/beacon/randao.go index 29e7853679..0f87bf1675 100644 --- a/node-api/handlers/beacon/randao.go +++ b/node-api/handlers/beacon/randao.go @@ -24,6 +24,7 @@ import ( "github.com/berachain/beacon-kit/errors" "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/primitives/math" ) @@ -38,7 +39,7 @@ func (h *Handler) GetRandao(c handlers.Context) (any, error) { } // Get slot and associated state - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } diff --git a/node-api/handlers/beacon/randao_test.go b/node-api/handlers/beacon/randao_test.go index 9fc99198f8..4bedc924fd 100644 --- a/node-api/handlers/beacon/randao_test.go +++ b/node-api/handlers/beacon/randao_test.go @@ -34,8 +34,8 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/common" "github.com/berachain/beacon-kit/primitives/math" @@ -66,7 +66,7 @@ func TestGetRandao(t *testing.T) { { name: "randao request - specified epoch", inputs: func() beacontypes.GetRandaoRequest { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead epoch := strconv.Itoa(int(testEpoch.Unwrap())) return beacontypes.GetRandaoRequest{ StateIDRequest: handlertypes.StateIDRequest{ @@ -98,7 +98,7 @@ func TestGetRandao(t *testing.T) { { name: "randao request - unspecified epoch", inputs: func() beacontypes.GetRandaoRequest { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead return beacontypes.GetRandaoRequest{ StateIDRequest: handlertypes.StateIDRequest{ StateID: stateID, diff --git a/node-api/handlers/beacon/validators.go b/node-api/handlers/beacon/validators.go index 1faec6c387..60f9d3fd2b 100644 --- a/node-api/handlers/beacon/validators.go +++ b/node-api/handlers/beacon/validators.go @@ -27,8 +27,9 @@ import ( "cosmossdk.io/collections" "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - types "github.com/berachain/beacon-kit/node-api/handlers/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/middleware" ) func (h *Handler) GetStateValidators(c handlers.Context) (any, error) { @@ -39,7 +40,7 @@ func (h *Handler) GetStateValidators(c handlers.Context) (any, error) { return nil, err } - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } @@ -58,7 +59,7 @@ func (h *Handler) PostStateValidators(c handlers.Context) (any, error) { return nil, err } - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } @@ -77,7 +78,7 @@ func (h *Handler) GetStateValidator(c handlers.Context) (any, error) { return nil, err } - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } @@ -101,7 +102,7 @@ func (h *Handler) getValidator(height int64, validatorID string) (*beacontypes.V if err != nil { if errors.Is(err, collections.ErrNotFound) { // this should happen when validatorID is an unknown pub key - return nil, fmt.Errorf("%s: %w", err.Error(), types.ErrNotFound) + return nil, fmt.Errorf("%s: %w", err.Error(), middleware.ErrNotFound) } return nil, fmt.Errorf("failed to get validator index by id %s: %w", validatorID, err) } @@ -110,7 +111,7 @@ func (h *Handler) getValidator(height int64, validatorID string) (*beacontypes.V if err != nil { // this should happen when validatorID is an unknown index if errors.Is(err, collections.ErrNotFound) { - return nil, fmt.Errorf("%s: %w", err.Error(), types.ErrNotFound) + return nil, fmt.Errorf("%s: %w", err.Error(), middleware.ErrNotFound) } return nil, fmt.Errorf("failed to get validator by index %s: %w", validatorID, err) } diff --git a/node-api/handlers/beacon/validators_balances.go b/node-api/handlers/beacon/validators_balances.go index ae4d15a48b..025a23464c 100644 --- a/node-api/handlers/beacon/validators_balances.go +++ b/node-api/handlers/beacon/validators_balances.go @@ -28,8 +28,10 @@ import ( "github.com/berachain/beacon-kit/node-api/backend" "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/types" "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/crypto" "github.com/berachain/beacon-kit/primitives/math" ) @@ -42,7 +44,7 @@ func (h *Handler) GetStateValidatorBalances(c handlers.Context) (any, error) { return nil, err } - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } @@ -56,7 +58,7 @@ func (h *Handler) GetStateValidatorBalances(c handlers.Context) (any, error) { func (h *Handler) PostStateValidatorBalances(c handlers.Context) (any, error) { var ids []string if err := c.Bind(&ids); err != nil { - return nil, fmt.Errorf("%s: %w", err.Error(), types.ErrInvalidRequest) + return nil, fmt.Errorf("%s: %w", err.Error(), middleware.ErrInvalidRequest) } // Get state_id from URL path parameter req := beacontypes.PostValidatorBalancesRequest{ @@ -65,10 +67,10 @@ func (h *Handler) PostStateValidatorBalances(c handlers.Context) (any, error) { } if err := c.Validate(&req); err != nil { - return nil, types.ErrInvalidRequest + return nil, middleware.ErrInvalidRequest } - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } diff --git a/node-api/handlers/beacon/validators_balances_test.go b/node-api/handlers/beacon/validators_balances_test.go index 6d85350047..624863f42e 100644 --- a/node-api/handlers/beacon/validators_balances_test.go +++ b/node-api/handlers/beacon/validators_balances_test.go @@ -34,8 +34,8 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/bytes" "github.com/berachain/beacon-kit/primitives/math" @@ -62,7 +62,7 @@ func TestGetStateValidatorBalances(t *testing.T) { { name: "all validators", inputs: func() (beacontypes.GetValidatorBalancesRequest, beacontypes.PostValidatorBalancesRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead IDs := []string(nil) return beacontypes.GetValidatorBalancesRequest{ StateIDRequest: handlertypes.StateIDRequest{ @@ -109,7 +109,7 @@ func TestGetStateValidatorBalances(t *testing.T) { { name: "single validators", inputs: func() (beacontypes.GetValidatorBalancesRequest, beacontypes.PostValidatorBalancesRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead IDs := []string{ stateValidators[0].Validator.PublicKey, stateValidators[1].Validator.PublicKey, @@ -159,7 +159,7 @@ func TestGetStateValidatorBalances(t *testing.T) { { name: "mixed know and unknow validators", inputs: func() (beacontypes.GetValidatorBalancesRequest, beacontypes.PostValidatorBalancesRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead unknownValPk := bytes.B48{0xff, 0xff} unknownValIdx := strconv.Itoa(2025) IDs := []string{ diff --git a/node-api/handlers/beacon/validators_filters.go b/node-api/handlers/beacon/validators_filters.go index b0faa0b2c3..a9063fbdb0 100644 --- a/node-api/handlers/beacon/validators_filters.go +++ b/node-api/handlers/beacon/validators_filters.go @@ -25,14 +25,11 @@ import ( "slices" consensustypes "github.com/berachain/beacon-kit/consensus-types/types" - cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service" "github.com/berachain/beacon-kit/errors" "github.com/berachain/beacon-kit/node-api/backend" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" "github.com/berachain/beacon-kit/primitives/crypto" "github.com/berachain/beacon-kit/primitives/math" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // errStatusFilterMismatch is an error for when a validator status does not @@ -45,14 +42,6 @@ var errStatusFilterMismatch = errors.New("validator status does not match status func (h *Handler) FilterValidators(height int64, ids []string, statuses []string) ([]*beacontypes.ValidatorData, error) { st, resolvedSlot, err := h.backend.StateAndSlotFromHeight(height) if err != nil { - if errors.Is(err, cometbft.ErrAppNotReady) { - // chain not ready, like when genesis time is set in the future - return nil, handlertypes.ErrNotFound - } - if errors.Is(err, sdkerrors.ErrInvalidHeight) { - // height requested too high - return nil, handlertypes.ErrNotFound - } return nil, fmt.Errorf("failed to get state from height %d: %w", height, err) } diff --git a/node-api/handlers/beacon/validators_filters_test.go b/node-api/handlers/beacon/validators_filters_test.go index ded3d52dff..85297c9ba4 100644 --- a/node-api/handlers/beacon/validators_filters_test.go +++ b/node-api/handlers/beacon/validators_filters_test.go @@ -32,14 +32,13 @@ import ( "github.com/berachain/beacon-kit/chain" "github.com/berachain/beacon-kit/config/spec" ctypes "github.com/berachain/beacon-kit/consensus-types/types" - cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service" "github.com/berachain/beacon-kit/log" "github.com/berachain/beacon-kit/log/noop" "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/node-core/components/metrics" "github.com/berachain/beacon-kit/primitives/constants" @@ -47,7 +46,6 @@ import ( statedb "github.com/berachain/beacon-kit/state-transition/core/state" statetransition "github.com/berachain/beacon-kit/testing/state-transition" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/labstack/echo/v4" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -72,7 +70,7 @@ func TestFilterValidators(t *testing.T) { { name: "all validators", inputs: func() (beacontypes.GetStateValidatorsRequest, beacontypes.PostStateValidatorsRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead return beacontypes.GetStateValidatorsRequest{ StateIDRequest: handlertypes.StateIDRequest{ StateID: stateID, @@ -109,7 +107,7 @@ func TestFilterValidators(t *testing.T) { { name: "some validators by indexes", inputs: func() (beacontypes.GetStateValidatorsRequest, beacontypes.PostStateValidatorsRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead IDs := []string{"1", "3"} return beacontypes.GetStateValidatorsRequest{ StateIDRequest: handlertypes.StateIDRequest{ @@ -119,7 +117,7 @@ func TestFilterValidators(t *testing.T) { Statuses: nil, }, beacontypes.PostStateValidatorsRequest{ StateIDRequest: handlertypes.StateIDRequest{ - StateID: utils.StateIDHead, + StateID: mapping.StateIDHead, }, IDs: IDs, Statuses: nil, @@ -155,7 +153,7 @@ func TestFilterValidators(t *testing.T) { { name: "some validators by pub keys", inputs: func() (beacontypes.GetStateValidatorsRequest, beacontypes.PostStateValidatorsRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead IDs := []string{ stateValidators[2].Validator.PublicKey, stateValidators[4].Validator.PublicKey, @@ -167,7 +165,7 @@ func TestFilterValidators(t *testing.T) { IDs: IDs, }, beacontypes.PostStateValidatorsRequest{ StateIDRequest: handlertypes.StateIDRequest{ - StateID: utils.StateIDHead, + StateID: mapping.StateIDHead, }, IDs: IDs, } @@ -202,7 +200,7 @@ func TestFilterValidators(t *testing.T) { { name: "some validators by status", inputs: func() (beacontypes.GetStateValidatorsRequest, beacontypes.PostStateValidatorsRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead statuses := []string{ constants.ValidatorStatusActiveOngoing, constants.ValidatorStatusActiveSlashed, @@ -252,7 +250,7 @@ func TestFilterValidators(t *testing.T) { { name: "chain not ready", inputs: func() (beacontypes.GetStateValidatorsRequest, beacontypes.PostStateValidatorsRequest) { - stateID := utils.StateIDHead + stateID := mapping.StateIDHead return beacontypes.GetStateValidatorsRequest{ StateIDRequest: handlertypes.StateIDRequest{ StateID: stateID, @@ -266,13 +264,13 @@ func TestFilterValidators(t *testing.T) { setMockExpectations: func(b *mocks.Backend) { // cometbft.ErrAppNotReady is the error flag returned when // genesis has not yet been processed and chain is not ready. - b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), cometbft.ErrAppNotReady) + b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), middleware.ErrNotFound) }, check: func(t *testing.T, res any, err error) { t.Helper() - // handlertypes.ErrNotFound is the error flag used to return 404 error code - require.ErrorIs(t, err, handlertypes.ErrNotFound) + // middleware.ErrNotFound is the error flag used to return 404 error code + require.ErrorIs(t, err, middleware.ErrNotFound) require.Nil(t, res) }, }, @@ -293,13 +291,13 @@ func TestFilterValidators(t *testing.T) { setMockExpectations: func(b *mocks.Backend) { // sdkerrors.ErrInvalidHeight is the error flag returned when // requested height is not in the state. - b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), sdkerrors.ErrInvalidHeight) + b.EXPECT().StateAndSlotFromHeight(mock.Anything).Return(nil, math.Slot(0), middleware.ErrNotFound) }, check: func(t *testing.T, res any, err error) { t.Helper() - // handlertypes.ErrNotFound is the error flag used to return 404 error code - require.ErrorIs(t, err, handlertypes.ErrNotFound) + // middleware.ErrNotFound is the error flag used to return 404 error code + require.ErrorIs(t, err, middleware.ErrNotFound) require.Nil(t, res) }, }, diff --git a/node-api/handlers/beacon/validators_test.go b/node-api/handlers/beacon/validators_test.go index e496c398d3..16ac8cd60a 100644 --- a/node-api/handlers/beacon/validators_test.go +++ b/node-api/handlers/beacon/validators_test.go @@ -34,8 +34,8 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/bytes" "github.com/berachain/beacon-kit/primitives/math" @@ -64,7 +64,7 @@ func TestGetValidator(t *testing.T) { inputs: func() beacontypes.GetStateValidatorRequest { return beacontypes.GetStateValidatorRequest{ StateIDRequest: handlertypes.StateIDRequest{ - StateID: utils.StateIDHead, + StateID: mapping.StateIDHead, }, ValidatorID: stateValidators[0].Validator.PublicKey, } @@ -95,7 +95,7 @@ func TestGetValidator(t *testing.T) { unknownValIdx := strconv.Itoa(2025) return beacontypes.GetStateValidatorRequest{ StateIDRequest: handlertypes.StateIDRequest{ - StateID: utils.StateIDHead, + StateID: mapping.StateIDHead, }, ValidatorID: unknownValIdx, } @@ -109,7 +109,7 @@ func TestGetValidator(t *testing.T) { }, check: func(t *testing.T, res any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrNotFound) + require.ErrorIs(t, err, middleware.ErrNotFound) require.Nil(t, res) }, }, @@ -119,7 +119,7 @@ func TestGetValidator(t *testing.T) { unknownValPk := bytes.B48{0xff, 0xff} return beacontypes.GetStateValidatorRequest{ StateIDRequest: handlertypes.StateIDRequest{ - StateID: utils.StateIDHead, + StateID: mapping.StateIDHead, }, ValidatorID: unknownValPk.String(), } @@ -133,7 +133,7 @@ func TestGetValidator(t *testing.T) { }, check: func(t *testing.T, res any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrNotFound) + require.ErrorIs(t, err, middleware.ErrNotFound) require.Nil(t, res) }, }, diff --git a/node-api/handlers/beacon/withdrawal.go b/node-api/handlers/beacon/withdrawal.go index 327536bc20..ff52e45990 100644 --- a/node-api/handlers/beacon/withdrawal.go +++ b/node-api/handlers/beacon/withdrawal.go @@ -25,8 +25,9 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/version" ) @@ -39,7 +40,7 @@ func (h *Handler) GetPendingPartialWithdrawals(c handlers.Context) (any, error) } // Load state for the requested state ID - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } @@ -54,7 +55,7 @@ func (h *Handler) GetPendingPartialWithdrawals(c handlers.Context) (any, error) return nil, err } if version.IsBefore(forkVersion.CurrentVersion, version.Electra()) { - return nil, fmt.Errorf("%w: Electra fork not active yet", handlertypes.ErrInvalidRequest) + return nil, fmt.Errorf("%w: Electra fork not active yet", middleware.ErrInvalidRequest) } // Retrieve and return withdrawals diff --git a/node-api/handlers/beacon/withdrawal_test.go b/node-api/handlers/beacon/withdrawal_test.go index 284e5be991..371c3cc891 100644 --- a/node-api/handlers/beacon/withdrawal_test.go +++ b/node-api/handlers/beacon/withdrawal_test.go @@ -34,8 +34,8 @@ import ( "github.com/berachain/beacon-kit/node-api/handlers/beacon" "github.com/berachain/beacon-kit/node-api/handlers/beacon/mocks" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" handlertypes "github.com/berachain/beacon-kit/node-api/handlers/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/node-api/middleware" "github.com/berachain/beacon-kit/primitives/math" "github.com/berachain/beacon-kit/primitives/version" @@ -150,7 +150,7 @@ func TestGetPendingPartialWithdrawals(t *testing.T) { check: func(t *testing.T, res any, err error) { t.Helper() - require.ErrorIs(t, err, handlertypes.ErrInvalidRequest) + require.ErrorIs(t, err, middleware.ErrInvalidRequest) require.Nil(t, res) }, }, @@ -173,7 +173,7 @@ func TestGetPendingPartialWithdrawals(t *testing.T) { // create input input := beacontypes.GetPendingPartialWithdrawalsRequest{ StateIDRequest: handlertypes.StateIDRequest{ - StateID: utils.StateIDGenesis, + StateID: mapping.StateIDGenesis, }, } inputBytes, err := json.Marshal(input) //nolint:musttag // TODO:fix diff --git a/node-api/handlers/debug/state.go b/node-api/handlers/debug/state.go index f116109a5b..e75e6a6541 100644 --- a/node-api/handlers/debug/state.go +++ b/node-api/handlers/debug/state.go @@ -23,6 +23,7 @@ package debug import ( "github.com/berachain/beacon-kit/node-api/handlers" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/node-api/handlers/utils" "github.com/berachain/beacon-kit/primitives/version" ) @@ -34,7 +35,7 @@ func (h *Handler) GetState(c handlers.Context) (any, error) { if err != nil { return nil, err } - height, err := utils.StateIDToHeight(req.StateID, h.backend) + height, err := mapping.StateIDToHeight(req.StateID, h.backend) if err != nil { return nil, err } diff --git a/node-api/handlers/utils/constants.go b/node-api/handlers/mapping/constants.go similarity index 98% rename from node-api/handlers/utils/constants.go rename to node-api/handlers/mapping/constants.go index 81915d38e0..440e6469ef 100644 --- a/node-api/handlers/utils/constants.go +++ b/node-api/handlers/mapping/constants.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package utils +package mapping const ( StateIDGenesis = "genesis" diff --git a/node-api/handlers/utils/mappings.go b/node-api/handlers/mapping/mappings.go similarity index 99% rename from node-api/handlers/utils/mappings.go rename to node-api/handlers/mapping/mappings.go index 2baefee05f..4da23260fa 100644 --- a/node-api/handlers/utils/mappings.go +++ b/node-api/handlers/mapping/mappings.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package utils +package mapping import ( "fmt" diff --git a/node-api/handlers/proof/handler.go b/node-api/handlers/proof/handler.go index e9df512e05..5311fe257f 100644 --- a/node-api/handlers/proof/handler.go +++ b/node-api/handlers/proof/handler.go @@ -25,7 +25,7 @@ import ( "github.com/berachain/beacon-kit/log" "github.com/berachain/beacon-kit/node-api/backend" "github.com/berachain/beacon-kit/node-api/handlers" - "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/primitives/math" ) @@ -56,7 +56,7 @@ func (h *Handler) resolveTimestampID(timestampID string) ( slot math.Slot ) - height, err := utils.TimestampIDToParentHeight(timestampID, h.backend) + height, err := mapping.TimestampIDToParentHeight(timestampID, h.backend) if err != nil { return 0, beaconState, blockHeader, err } diff --git a/node-api/handlers/utils/context.go b/node-api/handlers/utils/context.go index 06a29f80d8..0c3e9d2167 100644 --- a/node-api/handlers/utils/context.go +++ b/node-api/handlers/utils/context.go @@ -25,17 +25,17 @@ import ( "github.com/berachain/beacon-kit/log" "github.com/berachain/beacon-kit/node-api/handlers" - "github.com/berachain/beacon-kit/node-api/handlers/types" + "github.com/berachain/beacon-kit/node-api/middleware" ) // BindAndValidate binds the request to the context and validates it. func BindAndValidate[RequestT any](c handlers.Context, logger log.Logger) (RequestT, error) { var req RequestT if err := c.Bind(&req); err != nil { - return req, fmt.Errorf("%w: failed to bind request: %s", types.ErrInvalidRequest, err.Error()) + return req, fmt.Errorf("%w: failed to bind request: %s", middleware.ErrInvalidRequest, err.Error()) } if err := c.Validate(&req); err != nil { - return req, fmt.Errorf("%w: failed to validate request: %s", types.ErrInvalidRequest, err.Error()) + return req, fmt.Errorf("%w: failed to validate request: %s", middleware.ErrInvalidRequest, err.Error()) } logger.Info("Request validation successful", "params", req) return req, nil diff --git a/node-api/handlers/types/errors.go b/node-api/middleware/errors.go similarity index 98% rename from node-api/handlers/types/errors.go rename to node-api/middleware/errors.go index d24e450ba8..9e40dd26ef 100644 --- a/node-api/handlers/types/errors.go +++ b/node-api/middleware/errors.go @@ -18,7 +18,7 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -package types +package middleware import "errors" diff --git a/node-api/middleware/request.go b/node-api/middleware/request.go index 59cedc1a6a..c1ef58728f 100644 --- a/node-api/middleware/request.go +++ b/node-api/middleware/request.go @@ -25,7 +25,7 @@ import ( "fmt" "net/http" - "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/primitives/common" "github.com/berachain/beacon-kit/primitives/constants" "github.com/berachain/beacon-kit/primitives/crypto" @@ -103,14 +103,14 @@ func ValidateBlockID(fl validator.FieldLevel) bool { func ValidateTimestampID(fl validator.FieldLevel) bool { allowedValues := map[string]bool{ - utils.StateIDHead: true, - utils.StateIDGenesis: true, - utils.StateIDFinalized: true, - utils.StateIDJustified: true, + mapping.StateIDHead: true, + mapping.StateIDGenesis: true, + mapping.StateIDFinalized: true, + mapping.StateIDJustified: true, } value := fl.Field().String() - if utils.IsTimestampIDPrefix(value) { + if mapping.IsTimestampIDPrefix(value) { return ValidateUint64Dec(value[1:]) } diff --git a/node-api/middleware/response.go b/node-api/middleware/response.go index 8e6cd614bc..874a6097fd 100644 --- a/node-api/middleware/response.go +++ b/node-api/middleware/response.go @@ -25,7 +25,6 @@ import ( "github.com/berachain/beacon-kit/errors" "github.com/berachain/beacon-kit/node-api/handlers" - "github.com/berachain/beacon-kit/node-api/handlers/types" "github.com/labstack/echo/v4" ) @@ -51,17 +50,17 @@ func responseFromError(data any, err error) (int, any) { switch { case err == nil: return http.StatusOK, data - case errors.Is(err, types.ErrNotFound): + case errors.Is(err, ErrNotFound): return http.StatusNotFound, ErrorResponse{ Code: http.StatusNotFound, Message: err.Error(), } - case errors.Is(err, types.ErrInvalidRequest): + case errors.Is(err, ErrInvalidRequest): return http.StatusBadRequest, ErrorResponse{ Code: http.StatusBadRequest, Message: err.Error(), } - case errors.Is(err, types.ErrNotImplemented): + case errors.Is(err, ErrNotImplemented): return http.StatusNotImplemented, ErrorResponse{ Code: http.StatusNotImplemented, Message: err.Error(), diff --git a/testing/e2e/e2e_beacon_api_test.go b/testing/e2e/e2e_beacon_api_test.go index 5e1c344d03..9dd1616302 100644 --- a/testing/e2e/e2e_beacon_api_test.go +++ b/testing/e2e/e2e_beacon_api_test.go @@ -35,7 +35,7 @@ import ( "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/berachain/beacon-kit/config/spec" beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/primitives/version" "github.com/berachain/beacon-kit/testing/e2e/config" "github.com/berachain/beacon-kit/testing/e2e/suite/types" @@ -100,7 +100,7 @@ func (s *BeaconKitE2ESuite) TestBeaconStateRoot() { stateRootResp, err := client.BeaconStateRoot( s.Ctx(), &beaconapi.BeaconStateRootOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, }, ) s.Require().NoError(err) @@ -116,7 +116,7 @@ func (s *BeaconKitE2ESuite) TestBeaconValidatorsWithIndices() { validatorsResp, err := client.Validators( s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: indices, }, ) @@ -179,7 +179,7 @@ func (s *BeaconKitE2ESuite) TestValidatorsEmptyIndicesAndStatuses() { validatorsResp, err := client.Validators( s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: emptyIndices, ValidatorStates: emptyStatuses, }, @@ -214,7 +214,7 @@ func (s *BeaconKitE2ESuite) TestValidatorsWithMultipleIndices() { indices := []phase0.ValidatorIndex{0, 1, 2} validatorsResp, err := client.Validators(s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: indices, }) s.Require().NoError(err) @@ -229,7 +229,7 @@ func (s *BeaconKitE2ESuite) TestValidatorsWithInvalidIndex() { indices := []phase0.ValidatorIndex{999999} // Invalid index validatorsResp, err := client.Validators(s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: indices, }) s.Require().NoError(err) @@ -244,7 +244,7 @@ func (s *BeaconKitE2ESuite) TestValidatorsWithSpecificStatus() { client := s.initBeaconTest() validatorsResp, err := client.Validators(s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, ValidatorStates: []apiv1.ValidatorState{apiv1.ValidatorStateActiveOngoing}, }) s.Require().NoError(err) @@ -261,7 +261,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalances() { client := s.initBeaconTest() balancesResp, err := client.ValidatorBalances(s.Ctx(), &beaconapi.ValidatorBalancesOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, }) s.Require().NoError(err) s.Require().NotNil(balancesResp) @@ -285,7 +285,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalancesWithSpecificIndices() { indices := []phase0.ValidatorIndex{0} balancesResp, err := client.ValidatorBalances(s.Ctx(), &beaconapi.ValidatorBalancesOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: indices, }) s.Require().NoError(err) @@ -313,7 +313,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalancesMultipleIndices() { balancesResp, err := client.ValidatorBalances( s.Ctx(), &beaconapi.ValidatorBalancesOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: indices, }, ) @@ -343,7 +343,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalancesWithInvalidIndex() { indices := []phase0.ValidatorIndex{999999} // Invalid index balancesResp, err := client.ValidatorBalances(s.Ctx(), &beaconapi.ValidatorBalancesOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: indices, }) s.Require().NoError(err) @@ -358,7 +358,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalancesWithPubkey() { // First call validators to get the validator public key validatorsResp, err := client.Validators(s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, }) s.Require().NoError(err) s.Require().NotNil(validatorsResp) @@ -368,7 +368,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalancesWithPubkey() { pubkey := validator.Validator.PublicKey balancesResp, err := client.ValidatorBalances(s.Ctx(), &beaconapi.ValidatorBalancesOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: []phase0.ValidatorIndex{}, PubKeys: []phase0.BLSPubKey{pubkey}, }) @@ -393,7 +393,7 @@ func (s *BeaconKitE2ESuite) TestValidatorBalancesWithInvalidPubkey() { notFoundPubkey := "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a" balancesResp, err := client.ValidatorBalances(s.Ctx(), &beaconapi.ValidatorBalancesOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, Indices: []phase0.ValidatorIndex{}, PubKeys: []phase0.BLSPubKey{phase0.BLSPubKey(common.FromHex(notFoundPubkey))}, }) @@ -464,7 +464,7 @@ func (s *BeaconKitE2ESuite) getStateValidator(stateID, validatorID string) (*htt // TestGetStateValidatorByIndex tests getting the state validator by index. func (s *BeaconKitE2ESuite) TestGetStateValidatorByIndex() { - resp, err := s.getStateValidator(utils.StateIDHead, "0") + resp, err := s.getStateValidator(mapping.StateIDHead, "0") s.Require().NoError(err) s.Require().NotNil(resp, "response should not be nil") s.Require().Equal(http.StatusOK, resp.StatusCode) @@ -498,7 +498,7 @@ func (s *BeaconKitE2ESuite) TestGetStateValidatorBySlotAndIndex() { // TestGetStateValidatorByPubkey tests getting the state validator by pubkey. func (s *BeaconKitE2ESuite) TestGetStateValidatorByPubkey() { // First call validators to get the validator public key - resp, err := s.getStateValidator(utils.StateIDHead, "0") + resp, err := s.getStateValidator(mapping.StateIDHead, "0") s.Require().NoError(err) s.Require().NotNil(resp, "response should not be nil") s.Require().Equal(http.StatusOK, resp.StatusCode) @@ -512,7 +512,7 @@ func (s *BeaconKitE2ESuite) TestGetStateValidatorByPubkey() { pubkey := validatorResp.Validator.PublicKey // Actual test starts here. - resp, err = s.getStateValidator(utils.StateIDHead, pubkey) + resp, err = s.getStateValidator(mapping.StateIDHead, pubkey) s.Require().NoError(err) s.Require().NotNil(resp, "response should not be nil") s.Require().Equal(http.StatusOK, resp.StatusCode) @@ -527,7 +527,7 @@ func (s *BeaconKitE2ESuite) TestGetStateValidatorByPubkey() { // TestGetStateValidatorInvalidID tests getting the state validator with an invalid id. func (s *BeaconKitE2ESuite) TestGetStateValidatorInvalidID() { - resp, err := s.getStateValidator(utils.StateIDHead, "invalid_id") + resp, err := s.getStateValidator(mapping.StateIDHead, "invalid_id") s.Require().NoError(err) s.Require().NotNil(resp, "response should not be nil") s.Require().Equal(http.StatusBadRequest, resp.StatusCode) @@ -571,7 +571,7 @@ func (s *BeaconKitE2ESuite) decodeValidatorBalancesResponse(resp *http.Response) // TestGetValidatorBalances tests querying validator balances for state head. func (s *BeaconKitE2ESuite) TestGetValidatorBalances() { - resp, err := s.getValidatorBalances(utils.StateIDHead) + resp, err := s.getValidatorBalances(mapping.StateIDHead) s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() @@ -590,7 +590,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalances() { // TestGetValidatorBalancesWithSpecificID tests querying validator balances with specific ID. func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithSpecificID() { - resp, err := s.getValidatorBalances(utils.StateIDHead, "0") + resp, err := s.getValidatorBalances(mapping.StateIDHead, "0") s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() @@ -607,7 +607,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithSpecificID() { // TestGetValidatorBalancesWithMultipleIDs tests querying validator balances with multiple IDs. func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithMultipleIDs() { - resp, err := s.getValidatorBalances(utils.StateIDHead, "0", "1") + resp, err := s.getValidatorBalances(mapping.StateIDHead, "0", "1") s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() @@ -629,7 +629,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithMultipleIDs() { // TestGetValidatorBalancesWithInvalidID tests querying validator balances with invalid ID. func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithInvalidID() { - resp, err := s.getValidatorBalances(utils.StateIDHead, "invalid_id") + resp, err := s.getValidatorBalances(mapping.StateIDHead, "invalid_id") s.Require().NoError(err) s.Require().Equal(http.StatusBadRequest, resp.StatusCode) defer resp.Body.Close() @@ -642,7 +642,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithInvalidID() { // TestGetValidatorBalancesWithNonExistentIndex tests querying validator balances with non-existent index. func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithNonExistentIndex() { - resp, err := s.getValidatorBalances(utils.StateIDHead, "99999") + resp, err := s.getValidatorBalances(mapping.StateIDHead, "99999") s.Require().NoError(err) // If an index does not match any validator, no balance will be returned but this will not cause an error. // The response should be 200 OK with empty data. @@ -661,7 +661,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithPublicKey() { // First call validators to get the validator public key validatorsResp, err := client.Validators(s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, }) s.Require().NoError(err) s.Require().NotNil(validatorsResp) @@ -670,7 +670,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithPublicKey() { s.Require().NotNil(validator) pubkey := validator.Validator.PublicKey - resp, err := s.getValidatorBalances(utils.StateIDHead, pubkey.String()) + resp, err := s.getValidatorBalances(mapping.StateIDHead, pubkey.String()) s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() @@ -691,7 +691,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithInvalidPublicKey() { // Example validator pubkey (48 bytes with 0x prefix) notFoundPubkey := "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a" - resp, err := s.getValidatorBalances(utils.StateIDHead, notFoundPubkey) + resp, err := s.getValidatorBalances(mapping.StateIDHead, notFoundPubkey) s.Require().NoError(err) // If public key does not match any validator, no balance will be returned but this will not cause an error. // The response should be 200 OK with empty data. @@ -706,7 +706,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorBalancesWithInvalidPublicKey() { // TestGetValidatorBalancesForGenesis tests querying validator balances for state genesis. func (s *BeaconKitE2ESuite) TestGetValidatorBalancesForGenesis() { - resp, err := s.getValidatorBalances(utils.StateIDGenesis) + resp, err := s.getValidatorBalances(mapping.StateIDGenesis) s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() @@ -768,7 +768,7 @@ func (s *BeaconKitE2ESuite) decodeValidatorsResponse(resp *http.Response) (*[]be // TestGetValidatorsWithStateHead tests querying validators with state head. func (s *BeaconKitE2ESuite) TestGetValidatorsWithStateHead() { - resp, err := s.getValidator(utils.StateIDHead) + resp, err := s.getValidator(mapping.StateIDHead) s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() @@ -787,7 +787,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorsWithStateHead() { // TestGetValidatorsWithID tests querying validators with ID parameter. func (s *BeaconKitE2ESuite) TestGetValidatorsWithID() { - resp, err := s.getValidator(utils.StateIDHead, map[string]string{ + resp, err := s.getValidator(mapping.StateIDHead, map[string]string{ "id": "0", }) s.Require().NoError(err) @@ -807,7 +807,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorsWithID() { // TestGetValidatorsWithStatus tests querying validators with status parameter. func (s *BeaconKitE2ESuite) TestGetValidatorsWithStatus() { - resp, err := s.getValidator(utils.StateIDHead, map[string]string{ + resp, err := s.getValidator(mapping.StateIDHead, map[string]string{ "status": "active_ongoing", }) s.Require().NoError(err) @@ -829,7 +829,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorsWithStatus() { // TestGetValidatorsWithIDAndStatus tests querying validators with both ID and status parameters. func (s *BeaconKitE2ESuite) TestGetValidatorsWithIDAndStatus() { // Call with both ID and status - resp, err := s.getValidator(utils.StateIDHead, map[string]string{ + resp, err := s.getValidator(mapping.StateIDHead, map[string]string{ "id": "0", "status": "active_ongoing", }) @@ -853,7 +853,7 @@ func (s *BeaconKitE2ESuite) TestGetValidatorsWithIDAndStatus() { // TestGetValidatorsWithStateGenesis tests querying validators with state genesis. func (s *BeaconKitE2ESuite) TestGetValidatorsWithStateGenesis() { - resp, err := s.getValidator(utils.StateIDGenesis) + resp, err := s.getValidator(mapping.StateIDGenesis) s.Require().NoError(err) s.Require().Equal(http.StatusOK, resp.StatusCode) defer resp.Body.Close() diff --git a/testing/e2e/e2e_withdrawal_test.go b/testing/e2e/e2e_withdrawal_test.go index fdbacd57f6..33f569b229 100644 --- a/testing/e2e/e2e_withdrawal_test.go +++ b/testing/e2e/e2e_withdrawal_test.go @@ -32,7 +32,7 @@ import ( ctypes "github.com/berachain/beacon-kit/consensus-types/types" "github.com/berachain/beacon-kit/execution/requests/eip7002" "github.com/berachain/beacon-kit/node-api/handlers/beacon/types" - "github.com/berachain/beacon-kit/node-api/handlers/utils" + "github.com/berachain/beacon-kit/node-api/handlers/mapping" "github.com/berachain/beacon-kit/primitives/crypto" beaconmath "github.com/berachain/beacon-kit/primitives/math" "github.com/berachain/beacon-kit/primitives/version" @@ -120,7 +120,7 @@ func (s *BeaconKitE2ESuite) findValidatorWithExecutionCredentials(client *e2etyp validatorsResp, err := client.Validators( s.Ctx(), &beaconapi.ValidatorsOpts{ - State: utils.StateIDHead, + State: mapping.StateIDHead, }, ) if err != nil { @@ -182,7 +182,7 @@ func (s *BeaconKitE2ESuite) TestSubmitPartialWithdrawalTransaction() { s.T().Logf("Block number before withdrawal: %d", blkNum) // Check for pending partial withdrawals before submitting the transaction - pendingWithdrawalsBefore, err := s.getPendingPartialWithdrawals(utils.StateIDHead) + pendingWithdrawalsBefore, err := s.getPendingPartialWithdrawals(mapping.StateIDHead) s.Require().NoError(err) s.Require().Len(pendingWithdrawalsBefore, 0, "Expected no pending withdrawals initially") @@ -245,7 +245,7 @@ func (s *BeaconKitE2ESuite) TestSubmitPartialWithdrawalTransaction() { s.T().Logf("Withdrawal transaction included in block: %d", receipt.BlockNumber) // Check for pending partial withdrawals after submitting the transaction - pendingWithdrawalsAfter, err := s.getPendingPartialWithdrawals(utils.StateIDHead) + pendingWithdrawalsAfter, err := s.getPendingPartialWithdrawals(mapping.StateIDHead) s.Require().NoError(err) s.Require().Len(pendingWithdrawalsAfter, 1, "Expected one pending withdrawal after transaction")