forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
311 lines (264 loc) · 8.8 KB
/
rest.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package chainproxy
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/btcsuite/btcd/btcec"
"github.com/gofiber/fiber/v2"
"github.com/lavanet/lava/relayer/chainproxy/rpcclient"
"github.com/lavanet/lava/relayer/lavasession"
"github.com/lavanet/lava/relayer/parser"
"github.com/lavanet/lava/relayer/sentry"
"github.com/lavanet/lava/utils"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
)
type RestMessage struct {
cp *RestChainProxy
serviceApi *spectypes.ServiceApi
path string
msg []byte
requestedBlock int64
Result json.RawMessage
connectionType string
}
type RestChainProxy struct {
nodeUrl string
sentry *sentry.Sentry
csm *lavasession.ConsumerSessionManager
portalLogs *PortalLogs
}
func (r *RestMessage) GetMsg() interface{} {
return r.msg
}
func NewRestChainProxy(nodeUrl string, sentry *sentry.Sentry, csm *lavasession.ConsumerSessionManager, pLogs *PortalLogs) ChainProxy {
nodeUrl = strings.TrimSuffix(nodeUrl, "/")
return &RestChainProxy{
nodeUrl: nodeUrl,
sentry: sentry,
csm: csm,
portalLogs: pLogs,
}
}
func (cp *RestChainProxy) GetConsumerSessionManager() *lavasession.ConsumerSessionManager {
return cp.csm
}
func (cp *RestChainProxy) NewMessage(path string, data []byte) (*RestMessage, error) {
//
// Check api is supported an save it in nodeMsg
serviceApi, err := cp.getSupportedApi(path)
if err != nil {
return nil, err
}
nodeMsg := &RestMessage{
cp: cp,
serviceApi: serviceApi,
path: path,
msg: data,
}
return nodeMsg, nil
}
func (m RestMessage) GetParams() interface{} {
retArr := make([]interface{}, 0)
retArr = append(retArr, m.msg)
return retArr
}
func (m RestMessage) GetResult() json.RawMessage {
return m.Result
}
func (m RestMessage) ParseBlock(inp string) (int64, error) {
return parser.ParseDefaultBlockParameter(inp)
}
func (cp *RestChainProxy) FetchBlockHashByNum(ctx context.Context, blockNum int64) (string, error) {
serviceApi, ok := cp.GetSentry().GetSpecApiByTag(spectypes.GET_BLOCK_BY_NUM)
if !ok {
return "", errors.New(spectypes.GET_BLOCKNUM + " tag function not found")
}
var nodeMsg NodeMessage
var err error
if serviceApi.GetParsing().FunctionTemplate != "" {
nodeMsg, err = cp.ParseMsg(fmt.Sprintf(serviceApi.GetParsing().FunctionTemplate, blockNum), nil, http.MethodGet)
} else {
nodeMsg, err = cp.NewMessage(serviceApi.Name, nil)
}
if err != nil {
return "", err
}
_, _, _, err = nodeMsg.Send(ctx, nil)
if err != nil {
return "", utils.LavaFormatError("Error On Send FetchBlockHashByNum", err, &map[string]string{"nodeUrl": cp.nodeUrl})
}
blockData, err := parser.ParseMessageResponse((nodeMsg.(*RestMessage)), serviceApi.Parsing.ResultParsing)
if err != nil {
return "", err
}
// blockData is an interface array with the parsed result in index 0.
// we know to expect a string result for a hash.
return blockData[spectypes.DEFAULT_PARSED_RESULT_INDEX].(string), nil
}
func (cp *RestChainProxy) FetchLatestBlockNum(ctx context.Context) (int64, error) {
serviceApi, ok := cp.GetSentry().GetSpecApiByTag(spectypes.GET_BLOCKNUM)
if !ok {
return spectypes.NOT_APPLICABLE, errors.New(spectypes.GET_BLOCKNUM + " tag function not found")
}
params := []byte{}
nodeMsg, err := cp.NewMessage(serviceApi.GetName(), params)
if err != nil {
return spectypes.NOT_APPLICABLE, err
}
_, _, _, err = nodeMsg.Send(ctx, nil)
if err != nil {
return spectypes.NOT_APPLICABLE, utils.LavaFormatError("Error On Send FetchLatestBlockNum", err, &map[string]string{"nodeUrl": cp.nodeUrl})
}
blocknum, err := parser.ParseBlockFromReply(nodeMsg, serviceApi.Parsing.ResultParsing)
if err != nil {
return spectypes.NOT_APPLICABLE, utils.LavaFormatError("Failed To Parse FetchLatestBlockNum", err, &map[string]string{
"nodeUrl": cp.nodeUrl,
"Method": nodeMsg.path,
"Response": string(nodeMsg.Result),
})
}
return blocknum, nil
}
func (cp *RestChainProxy) GetSentry() *sentry.Sentry {
return cp.sentry
}
func (cp *RestChainProxy) Start(context.Context) error {
return nil
}
func (cp *RestChainProxy) getSupportedApi(path string) (*spectypes.ServiceApi, error) {
path = strings.SplitN(path, "?", 2)[0]
if api, ok := cp.sentry.MatchSpecApiByName(path); ok {
if !api.Enabled {
return nil, fmt.Errorf("REST Api is disabled %s ", path)
}
return &api, nil
}
return nil, fmt.Errorf("REST Api not supported %s ", path)
}
func (cp *RestChainProxy) ParseMsg(path string, data []byte, connectionType string) (NodeMessage, error) {
//
// Check api is supported an save it in nodeMsg
serviceApi, err := cp.getSupportedApi(path)
if err != nil {
return nil, err
}
// data contains the query string
nodeMsg := &RestMessage{
cp: cp,
serviceApi: serviceApi,
path: path,
msg: data,
connectionType: connectionType, // POST,GET etc..
}
return nodeMsg, nil
}
func (cp *RestChainProxy) PortalStart(ctx context.Context, privKey *btcec.PrivateKey, listenAddr string) {
//
// Setup HTTP Server
app := fiber.New(fiber.Config{})
//
// Catch Post
app.Post("/:dappId/*", func(c *fiber.Ctx) error {
cp.portalLogs.LogStartTransaction("rest-http")
path := "/" + c.Params("*")
// TODO: handle contentType, in case its not application/json currently we set it to application/json in the Send() method
// contentType := string(c.Context().Request.Header.ContentType())
log.Println("in <<< ", path)
requestBody := string(c.Body())
reply, _, err := SendRelay(ctx, cp, privKey, path, requestBody, http.MethodPost)
if err != nil {
msgSeed := cp.portalLogs.GetUniqueGuidResponseForError(err)
cp.portalLogs.LogRequestAndResponse("http in/out", true, http.MethodPost, path, requestBody, "", msgSeed, err)
return c.SendString(fmt.Sprintf(`{"error": "unsupported api","more_information" %s}`, msgSeed))
}
responseBody := string(reply.Data)
cp.portalLogs.LogRequestAndResponse("http in/out", false, http.MethodPost, path, requestBody, responseBody, "", nil)
return c.SendString(responseBody)
})
//
// Catch the others
app.Use("/:dappId/*", func(c *fiber.Ctx) error {
cp.portalLogs.LogStartTransaction("rest-http")
URI := c.Request().URI()
if strings.Contains(URI.String(), "favicon.ico") {
return nil
}
query := "?" + string(URI.QueryString())
path := "/" + c.Params("*")
log.Println("in <<< ", path)
reply, _, err := SendRelay(ctx, cp, privKey, path, query, http.MethodGet)
if err != nil {
msgSeed := cp.portalLogs.GetUniqueGuidResponseForError(err)
cp.portalLogs.LogRequestAndResponse("http in/out", true, http.MethodGet, path, "", "", msgSeed, err)
return c.SendString(fmt.Sprintf(`{"error": "unsupported api","more_information" %s}`, msgSeed))
}
responseBody := string(reply.Data)
cp.portalLogs.LogRequestAndResponse("http in/out", false, http.MethodGet, path, "", responseBody, "", nil)
return c.SendString(responseBody)
})
//
// Go
err := app.Listen(listenAddr)
if err != nil {
log.Println(err)
}
}
func (nm *RestMessage) RequestedBlock() int64 {
return nm.requestedBlock
}
func (nm *RestMessage) GetServiceApi() *spectypes.ServiceApi {
return nm.serviceApi
}
func (nm *RestMessage) Send(ctx context.Context, ch chan interface{}) (relayReply *pairingtypes.RelayReply, subscriptionID string, relayReplyServer *rpcclient.ClientSubscription, err error) {
if ch != nil {
return nil, "", nil, utils.LavaFormatError("Subscribe is not allowed on rest", nil, nil)
}
httpClient := http.Client{
Timeout: DefaultTimeout, // Timeout after 5 seconds
}
var connectionTypeSlected string = http.MethodGet
// if ConnectionType is default value or empty we will choose http.MethodGet otherwise choosing the header type provided
if nm.connectionType != "" {
connectionTypeSlected = nm.connectionType
}
msgBuffer := bytes.NewBuffer(nm.msg)
url := nm.cp.nodeUrl + nm.path
// Only get calls uses query params the rest uses the body
if connectionTypeSlected == http.MethodGet {
url += string(nm.msg)
}
req, err := http.NewRequest(connectionTypeSlected, url, msgBuffer)
if err != nil {
nm.Result = []byte(fmt.Sprintf("%s", err))
return nil, "", nil, err
}
// setting the content-type to be application/json instead of Go's defult http.DefaultClient
if connectionTypeSlected == "POST" || connectionTypeSlected == "PUT" {
req.Header.Set("Content-Type", "application/json")
}
res, err := httpClient.Do(req)
if err != nil {
nm.Result = []byte(fmt.Sprintf("%s", err))
return nil, "", nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
nm.Result = []byte(fmt.Sprintf("%s", err))
return nil, "", nil, err
}
reply := &pairingtypes.RelayReply{
Data: body,
}
nm.Result = body
return reply, "", nil, nil
}