From 9940478c51aebbc0b86332dba681a1ce9e2c2506 Mon Sep 17 00:00:00 2001 From: Ran Mishael <106548467+ranlavanet@users.noreply.github.com> Date: Mon, 6 Feb 2023 19:36:17 +0100 Subject: [PATCH] PRT-438 Add dapp id to all interfaces (#292) * PRT-438 adding dapp id to all interfaces. * PRT-438 adding dapp id prints to ws as well. * PRT-438 missing dappID extraction from tendermint URI * PRT-438 missing dappID extraction from rest. --- protocol/chainlib/common.go | 35 ++++++++++++----------------- protocol/chainlib/jsonRPC.go | 4 ++-- protocol/chainlib/rest.go | 6 +---- protocol/chainlib/tendermintRPC.go | 10 +++------ relayer/chainproxy/chainproxy.go | 28 +++++++++-------------- relayer/chainproxy/jsonRPC.go | 4 ++-- relayer/chainproxy/rest.go | 6 +---- relayer/chainproxy/tendermintRPC.go | 10 +++------ 8 files changed, 36 insertions(+), 67 deletions(-) diff --git a/protocol/chainlib/common.go b/protocol/chainlib/common.go index 53bad0af8a..1d9084fcd3 100644 --- a/protocol/chainlib/common.go +++ b/protocol/chainlib/common.go @@ -45,27 +45,31 @@ func (pm parsedMessage) GetRPCMessage() parser.RPCInput { } func extractDappIDFromFiberContext(c *fiber.Ctx) (dappID string) { - if len(c.Route().Params) > 1 { - dappID = c.Route().Params[1] - dappID = strings.ReplaceAll(dappID, "*", "") - return + dappID = c.Params("dappId") + if dappID == "" { + dappID = "NoDappID" } - return "NoDappID" + return dappID } func constructFiberCallbackWithDappIDExtraction(callbackToBeCalled fiber.Handler) fiber.Handler { webSocketCallback := callbackToBeCalled handler := func(c *fiber.Ctx) error { - // dappID := "" - // if len(c.Route().Params) > 1 { - // dappID = c.Route().Params[1] - // dappID = strings.ReplaceAll(dappID, "*", "") - // } + dappId := extractDappIDFromFiberContext(c) + c.Locals("dappId", dappId) return webSocketCallback(c) // uses external dappID } return handler } +func extractDappIDFromWebsocketConnection(c *websocket.Conn) string { + dappId, ok := c.Locals("dappId").(string) + if !ok { + dappId = "NoDappID" + } + return dappId +} + func convertToJsonError(errorMsg string) string { jsonResponse, err := json.Marshal(fiber.Map{ "error": errorMsg, @@ -77,17 +81,6 @@ func convertToJsonError(errorMsg string) string { return string(jsonResponse) } -func extractDappIDFromWebsocketConnection(c *websocket.Conn) string { - dappIDLocal := c.Locals(ContextUserValueKeyDappID) - if dappID, ok := dappIDLocal.(string); ok { - // zeroallocation policy for fiber.Ctx - buffer := make([]byte, len(dappID)) - copy(buffer, dappID) - return string(buffer) - } - return "NoDappID" -} - func addAttributeToError(key string, value string, errorMessage string) string { return errorMessage + fmt.Sprintf(", %v: %v", key, value) } diff --git a/protocol/chainlib/jsonRPC.go b/protocol/chainlib/jsonRPC.go index 1e9842d524..b3335a5632 100644 --- a/protocol/chainlib/jsonRPC.go +++ b/protocol/chainlib/jsonRPC.go @@ -219,11 +219,11 @@ func (apil *JsonRPCChainListener) Serve(ctx context.Context) { apil.logger.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC) break } - utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg)}) + dappID := extractDappIDFromWebsocketConnection(c) + utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg), "dappID": dappID}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() // incase there's a problem make sure to cancel the connection - dappID := extractDappIDFromWebsocketConnection(c) metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) reply, replyServer, err := apil.relaySender.SendRelay(ctx, "", string(msg), http.MethodGet, dappID, metricsData) go apil.logger.AddMetric(metricsData, err != nil) diff --git a/protocol/chainlib/rest.go b/protocol/chainlib/rest.go index 71577aa9f8..ecb0c2f304 100644 --- a/protocol/chainlib/rest.go +++ b/protocol/chainlib/rest.go @@ -235,11 +235,7 @@ func (apil *RestChainListener) Serve(ctx context.Context) { query := "?" + string(c.Request().URI().QueryString()) path := "/" + c.Params("*") - dappID := "" - if len(c.Route().Params) > 1 { - dappID = c.Route().Params[1] - dappID = strings.ReplaceAll(dappID, "*", "") - } + dappID := extractDappIDFromFiberContext(c) utils.LavaFormatInfo("in <<<", &map[string]string{"path": path, "dappID": dappID, "msgSeed": msgSeed}) analytics := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) diff --git a/protocol/chainlib/tendermintRPC.go b/protocol/chainlib/tendermintRPC.go index 7b4a24d549..19a5665a39 100644 --- a/protocol/chainlib/tendermintRPC.go +++ b/protocol/chainlib/tendermintRPC.go @@ -264,11 +264,11 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context) { apil.logger.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "tendermint") break } - utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg)}) + dappID := extractDappIDFromWebsocketConnection(c) + utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg), "dappID": dappID}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() // incase there's a problem make sure to cancel the connection - dappID := extractDappIDFromWebsocketConnection(c) metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) reply, replyServer, err := apil.relaySender.SendRelay(ctx, "", string(msg), http.MethodGet, dappID, metricsData) go apil.logger.AddMetric(metricsData, err != nil) @@ -354,11 +354,7 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context) { query := "?" + string(c.Request().URI().QueryString()) path := c.Params("*") - dappID := "" - if len(c.Route().Params) > 1 { - dappID = c.Route().Params[1] - dappID = strings.ReplaceAll(dappID, "*", "") - } + dappID := extractDappIDFromFiberContext(c) msgSeed := apil.logger.GetMessageSeed() utils.LavaFormatInfo("urirpc in <<<", &map[string]string{"seed": msgSeed, "msg": path, "dappID": dappID}) metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) diff --git a/relayer/chainproxy/chainproxy.go b/relayer/chainproxy/chainproxy.go index 36919f5412..bd49fdca69 100644 --- a/relayer/chainproxy/chainproxy.go +++ b/relayer/chainproxy/chainproxy.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "strings" "time" "github.com/lavanet/lava/relayer/metrics" @@ -318,34 +317,27 @@ func SendRelay( func ConstructFiberCallbackWithDappIDExtraction(callbackToBeCalled fiber.Handler) fiber.Handler { webSocketCallback := callbackToBeCalled handler := func(c *fiber.Ctx) error { - // dappID := "" - // if len(c.Route().Params) > 1 { - // dappID = c.Route().Params[1] - // dappID = strings.ReplaceAll(dappID, "*", "") - // } + dappId := ExtractDappIDFromFiberContext(c) + c.Locals("dappId", dappId) return webSocketCallback(c) // uses external dappID } return handler } func ExtractDappIDFromWebsocketConnection(c *websocket.Conn) string { - dappIDLocal := c.Locals(ContextUserValueKeyDappID) - if dappID, ok := dappIDLocal.(string); ok { - // zeroallocation policy for fiber.Ctx - buffer := make([]byte, len(dappID)) - copy(buffer, dappID) - return string(buffer) + dappId, ok := c.Locals("dappId").(string) + if !ok { + dappId = "NoDappID" } - return "NoDappID" + return dappId } func ExtractDappIDFromFiberContext(c *fiber.Ctx) (dappID string) { - if len(c.Route().Params) > 1 { - dappID = c.Route().Params[1] - dappID = strings.ReplaceAll(dappID, "*", "") - return + dappID = c.Params("dappId") + if dappID == "" { + dappID = "NoDappID" } - return "NoDappID" + return dappID } func getTimePerCu(cu uint64) time.Duration { diff --git a/relayer/chainproxy/jsonRPC.go b/relayer/chainproxy/jsonRPC.go index e468de9dca..0ee9050ebe 100644 --- a/relayer/chainproxy/jsonRPC.go +++ b/relayer/chainproxy/jsonRPC.go @@ -306,11 +306,11 @@ func (cp *JrpcChainProxy) PortalStart(ctx context.Context, privKey *btcec.Privat cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC) break } - utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg)}) + dappID := ExtractDappIDFromWebsocketConnection(c) + utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg), "dappID": dappID}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() // incase there's a problem make sure to cancel the connection - dappID := ExtractDappIDFromWebsocketConnection(c) metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) reply, replyServer, err := SendRelay(ctx, cp, privKey, "", string(msg), http.MethodGet, dappID, metricsData) go cp.portalLogs.AddMetric(metricsData, err != nil) diff --git a/relayer/chainproxy/rest.go b/relayer/chainproxy/rest.go index ddd85de92c..284b135bcd 100644 --- a/relayer/chainproxy/rest.go +++ b/relayer/chainproxy/rest.go @@ -284,11 +284,7 @@ func (cp *RestChainProxy) PortalStart(ctx context.Context, privKey *btcec.Privat query := "?" + string(c.Request().URI().QueryString()) path := "/" + c.Params("*") - dappID := "" - if len(c.Route().Params) > 1 { - dappID = c.Route().Params[1] - dappID = strings.ReplaceAll(dappID, "*", "") - } + dappID := ExtractDappIDFromFiberContext(c) utils.LavaFormatInfo("in <<<", &map[string]string{"path": path, "dappID": dappID, "msgSeed": msgSeed}) analytics := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) diff --git a/relayer/chainproxy/tendermintRPC.go b/relayer/chainproxy/tendermintRPC.go index 7d0bedd8c8..3b4a3b9bde 100644 --- a/relayer/chainproxy/tendermintRPC.go +++ b/relayer/chainproxy/tendermintRPC.go @@ -289,11 +289,11 @@ func (cp *tendermintRpcChainProxy) PortalStart(ctx context.Context, privKey *btc cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "tendermint") break } - utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg)}) + dappID := ExtractDappIDFromWebsocketConnection(c) + utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg), "dappID": dappID}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() // incase there's a problem make sure to cancel the connection - dappID := ExtractDappIDFromWebsocketConnection(c) metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface) reply, replyServer, err := SendRelay(ctx, cp, privKey, "", string(msg), http.MethodGet, dappID, metricsData) go cp.portalLogs.AddMetric(metricsData, err != nil) @@ -379,11 +379,7 @@ func (cp *tendermintRpcChainProxy) PortalStart(ctx context.Context, privKey *btc query := "?" + string(c.Request().URI().QueryString()) path := c.Params("*") - dappID := "" - if len(c.Route().Params) > 1 { - dappID = c.Route().Params[1] - dappID = strings.ReplaceAll(dappID, "*", "") - } + dappID := ExtractDappIDFromFiberContext(c) msgSeed := cp.portalLogs.GetMessageSeed() utils.LavaFormatInfo("urirpc in <<<", &map[string]string{"seed": msgSeed, "msg": path, "dappID": dappID}) metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface)