Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api/gateway) return error from gateway if header is not synced yet #2108

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/gateway/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

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

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

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 header store head height: %v is lower then requested height: %v", headHeight, height)
walldiss marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 additions & 0 deletions api/gateway/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

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

Expand Down Expand Up @@ -69,6 +70,17 @@ func (h *Handler) performGetHeaderRequest(
writeError(w, http.StatusBadRequest, endpoint, err)
return nil, err
}
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 store head height: %v is lower then requested height: %v", 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 {
Expand Down
18 changes: 14 additions & 4 deletions api/gateway/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"

Expand Down Expand Up @@ -98,10 +99,19 @@ func (h *Handler) getShares(ctx context.Context, height uint64, nID namespace.ID
err error
header *header.ExtendedHeader
)
switch height {
case 0:
header, err = h.header.LocalHead(ctx)
default:

header, err = h.header.LocalHead(ctx)
if err != nil {
return nil, 0, err
}

if height > 0 {
if storeHeight := uint64(header.Height()); storeHeight < height {
return nil, 0, fmt.Errorf(
"current header store head height: %v is lower then requested height: %v",
storeHeight, height,
)
}
header, err = h.header.GetByHeight(ctx, height)
}
if err != nil {
Expand Down