Skip to content

Commit

Permalink
refactor(gateway): remove deprecated endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Sep 26, 2023
1 parent ac67b42 commit 8fe4fa7
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 319 deletions.
28 changes: 0 additions & 28 deletions api/gateway/das.go

This file was deleted.

23 changes: 1 addition & 22 deletions api/gateway/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,7 @@ import (
"net/http"
)

func (h *Handler) RegisterEndpoints(rpc *Server, deprecatedEndpointsEnabled bool) {
if deprecatedEndpointsEnabled {
log.Warn("Deprecated endpoints will be removed from the gateway in the next release. Use the RPC instead.")
// state endpoints
rpc.RegisterHandlerFunc(balanceEndpoint, h.handleBalanceRequest, http.MethodGet)
rpc.RegisterHandlerFunc(submitPFBEndpoint, h.handleSubmitPFB, http.MethodPost)

// staking queries
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", queryDelegationEndpoint, addrKey), h.handleQueryDelegation,
http.MethodGet)
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", queryUnbondingEndpoint, addrKey), h.handleQueryUnbonding,
http.MethodGet)
rpc.RegisterHandlerFunc(queryRedelegationsEndpoint, h.handleQueryRedelegations,
http.MethodPost)

// DASer endpoints
// only register if DASer service is available
if h.das != nil {
rpc.RegisterHandlerFunc(dasStateEndpoint, h.handleDASStateRequest, http.MethodGet)
}
}

func (h *Handler) RegisterEndpoints(rpc *Server) {
// state endpoints
rpc.RegisterHandlerFunc(fmt.Sprintf("%s/{%s}", balanceEndpoint, addrKey), h.handleBalanceRequest,
http.MethodGet)
Expand Down
209 changes: 18 additions & 191 deletions api/gateway/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (
"github.com/cosmos/cosmos-sdk/types"
"github.com/gorilla/mux"

"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/state"
)

const (
balanceEndpoint = "/balance"
submitTxEndpoint = "/submit_tx"
submitPFBEndpoint = "/submit_pfb"
queryDelegationEndpoint = "/query_delegation"
queryUnbondingEndpoint = "/query_unbonding"
queryRedelegationsEndpoint = "/query_redelegations"
balanceEndpoint = "/balance"
submitTxEndpoint = "/submit_tx"
)

const addrKey = "address"
Expand All @@ -34,21 +29,6 @@ type submitTxRequest struct {
Tx string `json:"tx"`
}

// submitPFBRequest represents a request to submit a PayForBlob
// transaction.
type submitPFBRequest struct {
NamespaceID string `json:"namespace_id"`
Data string `json:"data"`
Fee int64 `json:"fee"`
GasLimit uint64 `json:"gas_limit"`
}

// queryRedelegationsRequest represents a request to query redelegations
type queryRedelegationsRequest struct {
From string `json:"from"`
To string `json:"to"`
}

func (h *Handler) handleBalanceRequest(w http.ResponseWriter, r *http.Request) {
var (
bal *state.Balance
Expand All @@ -57,24 +37,25 @@ func (h *Handler) handleBalanceRequest(w http.ResponseWriter, r *http.Request) {
// read and parse request
vars := mux.Vars(r)
addrStr, exists := vars[addrKey]
if exists {
// convert address to Address type
var addr state.AccAddress
addr, err = types.AccAddressFromBech32(addrStr)
if !exists {
writeError(w, http.StatusBadRequest, balanceEndpoint, errors.New("invalid endpoint"))
return
}

// convert address to Address type
var addr state.AccAddress
addr, err = types.AccAddressFromBech32(addrStr)
if err != nil {
// first check if it is a validator address and can be converted
valAddr, err := types.ValAddressFromBech32(addrStr)
if err != nil {
// first check if it is a validator address and can be converted
valAddr, err := types.ValAddressFromBech32(addrStr)
if err != nil {
writeError(w, http.StatusBadRequest, balanceEndpoint, ErrInvalidAddressFormat)
return
}
addr = valAddr.Bytes()
writeError(w, http.StatusBadRequest, balanceEndpoint, ErrInvalidAddressFormat)
return
}
bal, err = h.state.BalanceForAddress(r.Context(), state.Address{Address: addr})
} else {
logDeprecation(balanceEndpoint, "state.Balance")
bal, err = h.state.Balance(r.Context())
addr = valAddr.Bytes()
}

bal, err = h.state.BalanceForAddress(r.Context(), state.Address{Address: addr})
if err != nil {
writeError(w, http.StatusInternalServerError, balanceEndpoint, err)
return
Expand Down Expand Up @@ -119,157 +100,3 @@ func (h *Handler) handleSubmitTx(w http.ResponseWriter, r *http.Request) {
log.Errorw("writing response", "endpoint", submitTxEndpoint, "err", err)
}
}

func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
logDeprecation(submitPFBEndpoint, "blob.Submit or state.SubmitPayForBlob")
// decode request
var req submitPFBRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}
namespace, err := hex.DecodeString(req.NamespaceID)
if err != nil {
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}
data, err := hex.DecodeString(req.Data)
if err != nil {
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}
fee := types.NewInt(req.Fee)

constructedBlob, err := blob.NewBlobV0(namespace, data)
if err != nil {
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}

// perform request
txResp, err := h.state.SubmitPayForBlob(r.Context(), fee, req.GasLimit, []*blob.Blob{constructedBlob})
if err != nil {
if txResp == nil {
// no tx data to return
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}
// if error returned, change status from 200 to 206
w.WriteHeader(http.StatusPartialContent)
}

bs, err := json.Marshal(&txResp)
if err != nil {
writeError(w, http.StatusInternalServerError, submitPFBEndpoint, err)
return
}

_, err = w.Write(bs)
if err != nil {
log.Errorw("writing response", "endpoint", submitPFBEndpoint, "err", err)
}
}

func (h *Handler) handleQueryDelegation(w http.ResponseWriter, r *http.Request) {
logDeprecation(queryDelegationEndpoint, "state.QueryDelegation")
// read and parse request
vars := mux.Vars(r)
addrStr, exists := vars[addrKey]
if !exists {
writeError(w, http.StatusBadRequest, queryDelegationEndpoint, ErrMissingAddress)
return
}

// convert address to Address type
addr, err := types.ValAddressFromBech32(addrStr)
if err != nil {
writeError(w, http.StatusBadRequest, queryDelegationEndpoint, err)
return
}
delegation, err := h.state.QueryDelegation(r.Context(), addr)
if err != nil {
writeError(w, http.StatusInternalServerError, queryDelegationEndpoint, err)
return
}
resp, err := json.Marshal(delegation)
if err != nil {
writeError(w, http.StatusInternalServerError, queryDelegationEndpoint, err)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorw("writing response", "endpoint", queryDelegationEndpoint, "err", err)
}
}

func (h *Handler) handleQueryUnbonding(w http.ResponseWriter, r *http.Request) {
logDeprecation(queryUnbondingEndpoint, "state.QueryUnbonding")
// read and parse request
vars := mux.Vars(r)
addrStr, exists := vars[addrKey]
if !exists {
writeError(w, http.StatusBadRequest, queryUnbondingEndpoint, ErrMissingAddress)
return
}

// convert address to Address type
addr, err := types.ValAddressFromBech32(addrStr)
if err != nil {
writeError(w, http.StatusBadRequest, queryUnbondingEndpoint, err)
return
}
unbonding, err := h.state.QueryUnbonding(r.Context(), addr)
if err != nil {
writeError(w, http.StatusInternalServerError, queryUnbondingEndpoint, err)
return
}
resp, err := json.Marshal(unbonding)
if err != nil {
writeError(w, http.StatusInternalServerError, queryUnbondingEndpoint, err)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorw("writing response", "endpoint", queryUnbondingEndpoint, "err", err)
}
}

func (h *Handler) handleQueryRedelegations(w http.ResponseWriter, r *http.Request) {
logDeprecation(queryRedelegationsEndpoint, "state.QueryRedelegations")
var req queryRedelegationsRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
writeError(w, http.StatusBadRequest, queryRedelegationsEndpoint, err)
return
}
srcValAddr, err := types.ValAddressFromBech32(req.From)
if err != nil {
writeError(w, http.StatusBadRequest, queryRedelegationsEndpoint, err)
return
}
dstValAddr, err := types.ValAddressFromBech32(req.To)
if err != nil {
writeError(w, http.StatusBadRequest, queryRedelegationsEndpoint, err)
return
}
unbonding, err := h.state.QueryRedelegations(r.Context(), srcValAddr, dstValAddr)
if err != nil {
writeError(w, http.StatusInternalServerError, queryRedelegationsEndpoint, err)
return
}
resp, err := json.Marshal(unbonding)
if err != nil {
writeError(w, http.StatusInternalServerError, queryRedelegationsEndpoint, err)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorw("writing response", "endpoint", queryRedelegationsEndpoint, "err", err)
}
}

func logDeprecation(endpoint string, alternative string) {
log.Warn("The " + endpoint + " endpoint is deprecated and will be removed in the next release. Please " +
"use " + alternative + " from the RPC instead.")
}
57 changes: 0 additions & 57 deletions api/gateway/state_test.go

This file was deleted.

7 changes: 3 additions & 4 deletions nodebuilder/gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
)

type Config struct {
Address string
Port string
Enabled bool
deprecatedEndpoints bool
Address string
Port string
Enabled bool
}

func DefaultConfig() Config {
Expand Down
3 changes: 1 addition & 2 deletions nodebuilder/gateway/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (

// Handler constructs a new RPC Handler from the given services.
func Handler(
cfg *Config,
state state.Module,
share share.Module,
header header.Module,
daser *das.DASer,
serv *gateway.Server,
) {
handler := gateway.NewHandler(state, share, header, daser)
handler.RegisterEndpoints(serv, cfg.deprecatedEndpoints)
handler.RegisterEndpoints(serv)
handler.RegisterMiddleware(serv)
}

Expand Down
Loading

0 comments on commit 8fe4fa7

Please sign in to comment.