Skip to content

Commit

Permalink
fix(gateway): remove the GetByHeight hussle and rely on Module's log…
Browse files Browse the repository at this point in the history
…ic (#2302)
  • Loading branch information
Wondertan committed Jun 2, 2023
1 parent c2d204f commit a01d0b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 58 deletions.
15 changes: 0 additions & 15 deletions api/gateway/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gateway

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

Expand All @@ -28,20 +27,6 @@ func (h *Handler) handleHeightAvailabilityRequest(w http.ResponseWriter, r *http
return
}

//TODO: change this to NetworkHead once the adjacency in the store is fixed.
head, err := h.header.LocalHead(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
return
}
if headHeight := int(head.Height()); headHeight < height {
err = fmt.Errorf(
"current head local chain head: %d is lower than requested height: %d"+
" give header sync some time and retry later", headHeight, height)
writeError(w, http.StatusServiceUnavailable, heightAvailabilityEndpoint, err)
return
}

header, err := h.header.GetByHeight(r.Context(), uint64(height))
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
Expand Down
17 changes: 2 additions & 15 deletions api/gateway/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gateway

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

Expand Down Expand Up @@ -70,24 +69,12 @@ func (h *Handler) performGetHeaderRequest(
writeError(w, http.StatusBadRequest, endpoint, err)
return nil, err
}
//TODO: change this to NetworkHead once the adjacency in the store is fixed.
head, err := h.header.LocalHead(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
return nil, err
}
if headHeight := int(head.Height()); headHeight < height {
err = fmt.Errorf(
"current head local chain head: %d is lower than requested height: %d"+
" give header sync some time and retry later", headHeight, height)
writeError(w, http.StatusServiceUnavailable, endpoint, err)
return nil, err
}
// perform request

header, err := h.header.GetByHeight(r.Context(), uint64(height))
if err != nil {
writeError(w, http.StatusInternalServerError, endpoint, err)
return nil, err
}

return header, nil
}
39 changes: 11 additions & 28 deletions api/gateway/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"

Expand All @@ -13,7 +12,6 @@ import (
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/nmt/namespace"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/share"
)

Expand Down Expand Up @@ -44,14 +42,14 @@ func (h *Handler) handleSharesByNamespaceRequest(w http.ResponseWriter, r *http.
writeError(w, http.StatusBadRequest, namespacedSharesEndpoint, err)
return
}
shares, headerHeight, err := h.getShares(r.Context(), height, nID)
shares, err := h.getShares(r.Context(), height, nID)
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedSharesEndpoint, err)
return
}
resp, err := json.Marshal(&NamespacedSharesResponse{
Shares: shares,
Height: uint64(headerHeight),
Height: height,
})
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedSharesEndpoint, err)
Expand All @@ -69,7 +67,7 @@ func (h *Handler) handleDataByNamespaceRequest(w http.ResponseWriter, r *http.Re
writeError(w, http.StatusBadRequest, namespacedDataEndpoint, err)
return
}
shares, headerHeight, err := h.getShares(r.Context(), height, nID)
shares, err := h.getShares(r.Context(), height, nID)
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedDataEndpoint, err)
return
Expand All @@ -81,7 +79,7 @@ func (h *Handler) handleDataByNamespaceRequest(w http.ResponseWriter, r *http.Re
}
resp, err := json.Marshal(&NamespacedDataResponse{
Data: data,
Height: uint64(headerHeight),
Height: height,
})
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedDataEndpoint, err)
Expand All @@ -93,33 +91,18 @@ func (h *Handler) handleDataByNamespaceRequest(w http.ResponseWriter, r *http.Re
}
}

func (h *Handler) getShares(ctx context.Context, height uint64, nID namespace.ID) ([]share.Share, int64, error) {
// get header
var (
err error
header *header.ExtendedHeader
)

//TODO: change this to NetworkHead once the adjacency in the store is fixed.
header, err = h.header.LocalHead(ctx)
func (h *Handler) getShares(ctx context.Context, height uint64, nID namespace.ID) ([]share.Share, error) {
header, err := h.header.GetByHeight(ctx, height)
if err != nil {
return nil, 0, err
return nil, err
}

if height > 0 {
if storeHeight := uint64(header.Height()); storeHeight < height {
return nil, 0, fmt.Errorf(
"current head local chain head: %d is lower than requested height: %d"+
" give header sync some time and retry later", storeHeight, height)
}
header, err = h.header.GetByHeight(ctx, height)
}
shares, err := h.share.GetSharesByNamespace(ctx, header.DAH, nID)
if err != nil {
return nil, 0, err
return nil, err
}
// perform request
shares, err := h.share.GetSharesByNamespace(ctx, header.DAH, nID)
return shares.Flatten(), header.Height(), err

return shares.Flatten(), nil
}

func dataFromShares(input []share.Share) (data [][]byte, err error) {
Expand Down

0 comments on commit a01d0b5

Please sign in to comment.