forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
142 lines (129 loc) · 3.64 KB
/
share.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package rpc
import (
"context"
"encoding/hex"
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/tendermint/tendermint/types"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/service/share"
"github.com/celestiaorg/nmt/namespace"
)
const (
namespacedSharesEndpoint = "/namespaced_shares"
namespacedDataEndpoint = "/namespaced_data"
)
var nIDKey = "nid"
// NamespacedSharesResponse represents the response to a
// SharesByNamespace request.
type NamespacedSharesResponse struct {
Shares []share.Share `json:"shares"`
Height uint64 `json:"height"`
}
// NamespacedDataResponse represents the response to a
// DataByNamespace request.
type NamespacedDataResponse struct {
Data [][]byte `json:"data"`
Height uint64 `json:"height"`
}
func (h *Handler) handleSharesByNamespaceRequest(w http.ResponseWriter, r *http.Request) {
height, nID, err := parseGetByNamespaceArgs(r)
if err != nil {
writeError(w, http.StatusBadRequest, namespacedSharesEndpoint, err)
return
}
shares, headerHeight, 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),
})
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedSharesEndpoint, err)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorw("serving request", "endpoint", namespacedSharesEndpoint, "err", err)
}
}
func (h *Handler) handleDataByNamespaceRequest(w http.ResponseWriter, r *http.Request) {
height, nID, err := parseGetByNamespaceArgs(r)
if err != nil {
writeError(w, http.StatusBadRequest, namespacedDataEndpoint, err)
return
}
shares, headerHeight, err := h.getShares(r.Context(), height, nID)
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedDataEndpoint, err)
return
}
data, err := dataFromShares(shares)
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedDataEndpoint, err)
return
}
resp, err := json.Marshal(&NamespacedDataResponse{
Data: data,
Height: uint64(headerHeight),
})
if err != nil {
writeError(w, http.StatusInternalServerError, namespacedDataEndpoint, err)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorw("serving request", "endpoint", namespacedDataEndpoint, "err", err)
}
}
func (h *Handler) getShares(ctx context.Context, height uint64, nID namespace.ID) ([]share.Share, int64, error) {
// get header
var (
err error
header *header.ExtendedHeader
)
switch height {
case 0:
header, err = h.header.Head(ctx)
default:
header, err = h.header.GetByHeight(ctx, height)
}
if err != nil {
return nil, 0, err
}
// perform request
shares, err := h.share.GetSharesByNamespace(ctx, header.DAH, nID)
return shares, header.Height, err
}
func dataFromShares(shares []share.Share) ([][]byte, error) {
messages, err := types.ParseMsgs(shares)
if err != nil {
return nil, err
}
data := make([][]byte, len(messages.MessagesList))
for i := range messages.MessagesList {
data[i] = messages.MessagesList[i].Data
}
return data, nil
}
func parseGetByNamespaceArgs(r *http.Request) (height uint64, nID namespace.ID, err error) {
vars := mux.Vars(r)
// if a height was given, parse it, otherwise get namespaced shares/data from the latest header
if strHeight, ok := vars[heightKey]; ok {
height, err = strconv.ParseUint(strHeight, 10, 64)
if err != nil {
return 0, nil, err
}
}
hexNID := vars[nIDKey]
nID, err = hex.DecodeString(hexNID)
if err != nil {
return 0, nil, err
}
return height, nID, nil
}