-
Notifications
You must be signed in to change notification settings - Fork 206
/
common.go
440 lines (386 loc) · 13.5 KB
/
common.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package chainlib
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/websocket/v2"
common "github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/protocol/metrics"
"github.com/lavanet/lava/utils"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
"google.golang.org/grpc/metadata"
)
const (
ContextUserValueKeyDappID = "dappID"
RetryListeningInterval = 10 // seconds
debug = false
refererMatchString = "refererMatch"
relayMsgLogMaxChars = 200
)
var InvalidResponses = []string{"null", "", "nil", "undefined"}
type RelayReplyWrapper struct {
StatusCode int
RelayReply *pairingtypes.RelayReply
}
type VerificationKey struct {
Extension string
Addon string
}
type VerificationContainer struct {
ConnectionType string
Name string
ParseDirective spectypes.ParseDirective
Value string
LatestDistance uint64
Severity spectypes.ParseValue_VerificationSeverity
VerificationKey
}
func (vc *VerificationContainer) IsActive() bool {
if vc.Value == "" && vc.LatestDistance == 0 {
return false
}
return true
}
type TaggedContainer struct {
Parsing *spectypes.ParseDirective
ApiCollection *spectypes.ApiCollection
}
type ApiContainer struct {
api *spectypes.Api
collectionKey CollectionKey
}
type ApiKey struct {
Name string
ConnectionType string
InternalPath string
}
type CollectionKey struct {
ConnectionType string
InternalPath string
Addon string
}
type BaseChainProxy struct {
ErrorHandler
averageBlockTime time.Duration
NodeUrl common.NodeUrl
ChainID string
}
// returns the node url and chain id for that proxy.
func (bcp *BaseChainProxy) GetChainProxyInformation() (common.NodeUrl, string) {
return bcp.NodeUrl, bcp.ChainID
}
func (bcp *BaseChainProxy) CapTimeoutForSend(ctx context.Context, chainMessage ChainMessageForSend) (context.Context, context.CancelFunc) {
relayTimeout := GetRelayTimeout(chainMessage, bcp.averageBlockTime)
processingTimeout := common.GetTimeoutForProcessing(relayTimeout, GetTimeoutInfo(chainMessage))
connectCtx, cancel := bcp.NodeUrl.LowerContextTimeout(ctx, processingTimeout)
return connectCtx, cancel
}
func extractDappIDFromFiberContext(c *fiber.Ctx) (dappID string) {
// Read the dappID from the headers
dappID = c.Get("dapp-id")
if dappID == "" {
dappID = generateNewDappID()
}
return dappID
}
// extractDappIDFromGrpcHeader extracts dappID from GRPC header
func extractDappIDFromGrpcHeader(metadataValues metadata.MD) string {
dappId := generateNewDappID()
if values, ok := metadataValues["dapp-id"]; ok && len(values) > 0 {
dappId = values[0]
}
return dappId
}
// generateNewDappID generates default dappID
// In future we can also implement unique dappID generation
func generateNewDappID() string {
return "DefaultDappID"
}
func constructFiberCallbackWithHeaderAndParameterExtraction(callbackToBeCalled fiber.Handler, isMetricEnabled bool) fiber.Handler {
webSocketCallback := callbackToBeCalled
handler := func(c *fiber.Ctx) error {
// Extract dappID from headers
dappID := extractDappIDFromFiberContext(c)
// Store dappID in the local context
c.Locals("dapp-id", dappID)
if isMetricEnabled {
c.Locals(metrics.RefererHeaderKey, c.Get(metrics.RefererHeaderKey, ""))
c.Locals(metrics.UserAgentHeaderKey, c.Get(metrics.UserAgentHeaderKey, ""))
c.Locals(metrics.OriginHeaderKey, c.Get(metrics.OriginHeaderKey, ""))
}
return webSocketCallback(c) // uses external dappID
}
return handler
}
func constructFiberCallbackWithHeaderAndParameterExtractionAndReferer(callbackToBeCalled fiber.Handler, isMetricEnabled bool) fiber.Handler {
webSocketCallback := callbackToBeCalled
handler := func(c *fiber.Ctx) error {
// Extract dappID from headers
dappID := extractDappIDFromFiberContext(c)
// Store dappID in the local context
c.Locals("dapp-id", dappID)
if isMetricEnabled {
c.Locals(metrics.RefererHeaderKey, c.Get(metrics.RefererHeaderKey, ""))
c.Locals(metrics.UserAgentHeaderKey, c.Get(metrics.UserAgentHeaderKey, ""))
c.Locals(metrics.OriginHeaderKey, c.Get(metrics.OriginHeaderKey, ""))
}
c.Locals(refererMatchString, c.Params(refererMatchString, ""))
return webSocketCallback(c) // uses external dappID
}
return handler
}
func convertToJsonError(errorMsg string) string {
jsonResponse, err := json.Marshal(fiber.Map{
"error": errorMsg,
})
if err != nil {
return `{"error": "Failed to marshal error response to json"}`
}
return string(jsonResponse)
}
func addAttributeToError(key, value, errorMessage string) string {
return errorMessage + fmt.Sprintf(`, "%v": "%v"`, key, value)
}
// rpc default endpoint should be websocket. otherwise return an error
func verifyRPCEndpoint(endpoint string) {
u, err := url.Parse(endpoint)
if err != nil {
utils.LavaFormatFatal("unparsable url", err, utils.Attribute{Key: "url", Value: endpoint})
}
switch u.Scheme {
case "ws", "wss":
return
default:
utils.LavaFormatWarning("URL scheme should be websocket (ws/wss), got: "+u.Scheme+", By not setting ws/wss your provider wont be able to accept ws subscriptions, therefore might receive less rewards and lower QOS score. if subscriptions are not applicable for this chain you can ignore this warning", nil)
}
}
// rpc default endpoint should be websocket. otherwise return an error
func verifyTendermintEndpoint(endpoints []common.NodeUrl) (websocketEndpoint, httpEndpoint common.NodeUrl) {
for _, endpoint := range endpoints {
u, err := url.Parse(endpoint.Url)
if err != nil {
utils.LavaFormatFatal("unparsable url", err, utils.Attribute{Key: "url", Value: endpoint.UrlStr()})
}
switch u.Scheme {
case "http", "https":
httpEndpoint = endpoint
case "ws", "wss":
websocketEndpoint = endpoint
default:
utils.LavaFormatFatal("URL scheme should be websocket (ws/wss) or (http/https), got: "+u.Scheme, nil)
}
}
if websocketEndpoint.String() == "" || httpEndpoint.String() == "" {
utils.LavaFormatError("Tendermint Provider was not provided with both http and websocket urls. please provide both", nil,
utils.Attribute{Key: "websocket", Value: websocketEndpoint.String()}, utils.Attribute{Key: "http", Value: httpEndpoint.String()})
if httpEndpoint.String() != "" {
return httpEndpoint, httpEndpoint
} else {
utils.LavaFormatFatal("Tendermint Provider was not provided with http url. please provide a url that starts with http/https", nil)
}
}
return websocketEndpoint, httpEndpoint
}
func ListenWithRetry(app *fiber.App, address string) {
for {
err := app.Listen(address)
if err != nil {
utils.LavaFormatError("app.Listen(listenAddr)", err)
}
time.Sleep(RetryListeningInterval * time.Second)
}
}
func GetListenerWithRetryGrpc(protocol, addr string) net.Listener {
for {
lis, err := net.Listen(protocol, addr)
if err == nil {
return lis
}
utils.LavaFormatError("failure setting up listener, net.Listen(protocol, addr)", err, utils.Attribute{Key: "listenAddr", Value: addr})
time.Sleep(RetryListeningInterval * time.Second)
utils.LavaFormatWarning("Attempting connection retry", nil)
}
}
// rest request headers are formatted like map[string]string
func convertToMetadataMap(md map[string][]string) []pairingtypes.Metadata {
metadata := make([]pairingtypes.Metadata, len(md))
indexer := 0
for k, v := range md {
metadata[indexer] = pairingtypes.Metadata{Name: k, Value: strings.Join(v, ", ")}
indexer += 1
}
return metadata
}
// rest response headers / grpc headers are formatted like map[string][]string
func convertToMetadataMapOfSlices(md map[string][]string) []pairingtypes.Metadata {
metadata := make([]pairingtypes.Metadata, len(md))
indexer := 0
for k, v := range md {
metadata[indexer] = pairingtypes.Metadata{Name: k, Value: v[0]}
indexer += 1
}
return metadata
}
func convertRelayMetaDataToMDMetaData(md []pairingtypes.Metadata) metadata.MD {
responseMetaData := make(metadata.MD)
for _, v := range md {
responseMetaData[v.Name] = append(responseMetaData[v.Name], v.Value)
}
return responseMetaData
}
// split two requested blocks to the most advanced and most behind
// the hierarchy is as follows:
// NOT_APPLICABLE
// LATEST_BLOCK
// PENDING_BLOCK
// SAFE
// FINALIZED
// numeric value (descending)
// EARLIEST
func CompareRequestedBlockInBatch(firstRequestedBlock int64, second int64) (latestCombinedBlock int64, earliestCombinedBlock int64) {
if firstRequestedBlock == spectypes.EARLIEST_BLOCK {
return second, firstRequestedBlock
}
if second == spectypes.EARLIEST_BLOCK {
return firstRequestedBlock, second
}
returnBigger := func(in_first int64, in_second int64) (int64, int64) {
if in_first > in_second {
return in_first, in_second
}
return in_second, in_first
}
if firstRequestedBlock < 0 {
if second < 0 {
// both are negative
return returnBigger(firstRequestedBlock, second)
}
// first is negative non earliest second is positive
return firstRequestedBlock, second
}
if second < 0 {
// second is negative non earliest first is positive
return second, firstRequestedBlock
}
// both are positive
return returnBigger(firstRequestedBlock, second)
}
func GetRelayTimeout(chainMessage ChainMessageForSend, averageBlockTime time.Duration) time.Duration {
if chainMessage.TimeoutOverride() != 0 {
return chainMessage.TimeoutOverride()
}
// Calculate extra RelayTimeout
extraRelayTimeout := time.Duration(0)
if IsHangingApi(chainMessage) {
extraRelayTimeout = averageBlockTime * 2
}
relayTimeAddition := common.GetTimePerCu(GetComputeUnits(chainMessage))
if chainMessage.GetApi().TimeoutMs > 0 {
relayTimeAddition = time.Millisecond * time.Duration(chainMessage.GetApi().TimeoutMs)
}
// Set relay timout, increase it every time we fail a relay on timeout
return extraRelayTimeout + relayTimeAddition + common.AverageWorldLatency
}
// setup a common preflight and cors configuration allowing wild cards and preflight caching.
func createAndSetupBaseAppListener(cmdFlags common.ConsumerCmdFlags, healthCheckPath string, healthReporter HealthReporter) *fiber.App {
app := fiber.New(fiber.Config{})
app.Use(favicon.New())
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
app.Use(func(c *fiber.Ctx) error {
// we set up wild card by default.
c.Set("Access-Control-Allow-Origin", cmdFlags.OriginFlag)
// Handle preflight requests directly
if c.Method() == "OPTIONS" {
// set up all allowed methods.
c.Set("Access-Control-Allow-Methods", cmdFlags.MethodsFlag)
// allow headers
c.Set("Access-Control-Allow-Headers", cmdFlags.HeadersFlag)
// allow credentials
c.Set("Access-Control-Allow-Credentials", cmdFlags.CredentialsFlag)
// Cache preflight request for 24 hours (in seconds)
c.Set("Access-Control-Max-Age", cmdFlags.CDNCacheDuration)
return c.SendStatus(fiber.StatusNoContent)
}
if c.Method() == "DELETE" {
return c.SendStatus(fiber.StatusNoContent)
}
return c.Next()
})
app.Get(healthCheckPath, func(fiberCtx *fiber.Ctx) error {
if healthReporter.IsHealthy() {
fiberCtx.Status(http.StatusOK)
return fiberCtx.SendString("Health status OK")
} else {
fiberCtx.Status(http.StatusServiceUnavailable)
return fiberCtx.SendString("Health status Failure")
}
})
return app
}
func truncateAndPadString(s string, maxLength int) string {
// Truncate to a maximum length
if len(s) > maxLength {
s = s[:maxLength]
}
// Pad with empty strings if the length is less than the specified maximum length
s = fmt.Sprintf("%-*s", maxLength, s)
return s
}
// return if response is valid or not - true
func ValidateNilResponse(responseString string) error {
return nil // this feature was disabled in version 0.35.8 due to some nodes request this response.
// after the timeout features we can add support for this filtering as it would be parsed and
// returned to the user if multiple providers returned the same type of response
// Removed on 0.35.8
// if slices.Contains(InvalidResponses, responseString) {
// return fmt.Errorf("response returned an empty value: %s", responseString)
// }
// return nil
}
type RefererData struct {
Address string
Marker string
ReferrerClient *metrics.ConsumerReferrerClient
}
func (rd *RefererData) SendReferer(refererMatchString string, chainId string, msg string, headers map[string][]string, c *websocket.Conn) error {
if rd == nil || rd.Address == "" {
return nil
}
if rd.ReferrerClient == nil {
return nil
}
if c == nil && headers == nil {
return nil
}
referer := ""
origin := ""
userAgent := ""
if headers != nil {
referer = strings.Join(headers[metrics.RefererHeaderKey], ", ")
origin = strings.Join(headers[metrics.OriginHeaderKey], ", ")
userAgent = strings.Join(headers[metrics.UserAgentHeaderKey], ", ")
} else if c != nil {
referer, _ = c.Locals(metrics.RefererHeaderKey).(string)
origin, _ = c.Locals(metrics.OriginHeaderKey).(string)
userAgent, _ = c.Locals(metrics.UserAgentHeaderKey).(string)
}
utils.LavaFormatDebug("referer detected", utils.LogAttr("referer", refererMatchString))
rd.ReferrerClient.AppendReferrer(metrics.NewReferrerRequest(refererMatchString, chainId, msg, referer, origin, userAgent))
return nil
}
func GetTimeoutInfo(chainMessage ChainMessageForSend) common.TimeoutInfo {
return common.TimeoutInfo{
CU: chainMessage.GetApi().ComputeUnits,
Hanging: IsHangingApi(chainMessage),
Stateful: GetStateful(chainMessage),
}
}