Skip to content

Commit

Permalink
PRT-438 Add dapp id to all interfaces (#292)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ranlavanet authored Feb 6, 2023
1 parent 04793ec commit 9940478
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 67 deletions.
35 changes: 14 additions & 21 deletions protocol/chainlib/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions protocol/chainlib/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 3 additions & 7 deletions protocol/chainlib/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 10 additions & 18 deletions relayer/chainproxy/chainproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/lavanet/lava/relayer/metrics"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions relayer/chainproxy/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions relayer/chainproxy/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 3 additions & 7 deletions relayer/chainproxy/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9940478

Please sign in to comment.