Skip to content

Commit

Permalink
created const and replaced all occurances (lavanet#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet authored Dec 26, 2022
1 parent 8467965 commit b5b165a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
8 changes: 4 additions & 4 deletions relayer/chainproxy/chainproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ type ChainProxy interface {
func GetChainProxy(nodeUrl string, nConns uint, sentry *sentry.Sentry, pLogs *PortalLogs) (ChainProxy, error) {
consumerSessionManagerInstance := &lavasession.ConsumerSessionManager{}
switch sentry.ApiInterface {
case "jsonrpc":
case spectypes.APIInterfaceJsonRPC:
return NewJrpcChainProxy(nodeUrl, nConns, sentry, consumerSessionManagerInstance, pLogs), nil
case "tendermintrpc":
case spectypes.APIInterfaceTendermintRPC:
return NewtendermintRpcChainProxy(nodeUrl, nConns, sentry, consumerSessionManagerInstance, pLogs), nil
case "rest":
case spectypes.APIInterfaceRest:
return NewRestChainProxy(nodeUrl, sentry, consumerSessionManagerInstance, pLogs), nil
case "grpc":
case spectypes.APIInterfaceGrpc:
return NewGrpcChainProxy(nodeUrl, nConns, sentry, consumerSessionManagerInstance, pLogs), nil
}
return nil, fmt.Errorf("chain proxy for apiInterface (%s) not found", sentry.ApiInterface)
Expand Down
14 changes: 7 additions & 7 deletions relayer/chainproxy/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (cp *JrpcChainProxy) PortalStart(ctx context.Context, privKey *btcec.Privat
msgSeed := cp.portalLogs.GetMessageSeed()
for {
if mt, msg, err = c.ReadMessage(); err != nil {
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
break
}
utils.LavaFormatInfo("ws in <<<", &map[string]string{"seed": msgSeed, "msg": string(msg)})
Expand All @@ -307,7 +307,7 @@ func (cp *JrpcChainProxy) PortalStart(ctx context.Context, privKey *btcec.Privat
dappID := ExtractDappIDFromWebsocketConnection(c)
reply, replyServer, err := SendRelay(ctx, cp, privKey, "", string(msg), http.MethodGet, dappID)
if err != nil {
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
continue
}

Expand All @@ -316,34 +316,34 @@ func (cp *JrpcChainProxy) PortalStart(ctx context.Context, privKey *btcec.Privat
var reply pairingtypes.RelayReply
err = (*replyServer).RecvMsg(&reply) // this reply contains the RPC ID
if err != nil {
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
continue
}

if err = c.WriteMessage(mt, reply.Data); err != nil {
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
continue
}
cp.portalLogs.LogRequestAndResponse("jsonrpc ws msg", false, "ws", c.LocalAddr().String(), string(msg), string(reply.Data), msgSeed, nil)
for {
err = (*replyServer).RecvMsg(&reply)
if err != nil {
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
break
}

// If portal cant write to the client
if err = c.WriteMessage(mt, reply.Data); err != nil {
cancel()
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
// break
}

cp.portalLogs.LogRequestAndResponse("jsonrpc ws msg", false, "ws", c.LocalAddr().String(), string(msg), string(reply.Data), msgSeed, nil)
}
} else {
if err = c.WriteMessage(mt, reply.Data); err != nil {
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, "jsonrpc")
cp.portalLogs.AnalyzeWebSocketErrorAndWriteMessage(c, mt, err, msgSeed, msg, spectypes.APIInterfaceJsonRPC)
continue
}
cp.portalLogs.LogRequestAndResponse("jsonrpc ws msg", false, "ws", c.LocalAddr().String(), string(msg), string(reply.Data), msgSeed, nil)
Expand Down
2 changes: 1 addition & 1 deletion relayer/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (s *Sentry) getServiceApis(spec *spectypes.QueryGetSpecResponse) (retServer
// spec will contain many api interfaces, we only need those that belong to the apiInterface of this sentry
continue
}
if apiInterface.Interface == "rest" {
if apiInterface.Interface == spectypes.APIInterfaceRest {
re := regexp.MustCompile(`{[^}]+}`)
processedName := string(re.ReplaceAll([]byte(api.Name), []byte("replace-me-with-regex")))
processedName = regexp.QuoteMeta(processedName)
Expand Down
5 changes: 3 additions & 2 deletions relayer/testclients/aptos_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/lavanet/lava/relayer/chainproxy"
"github.com/lavanet/lava/relayer/sentry"
spectypes "github.com/lavanet/lava/x/spec/types"
)

const (
restString string = "rest"
tendermintString string = "tendermintrpc"
restString string = spectypes.APIInterfaceRest
tendermintString string = spectypes.APIInterfaceTendermintRPC
)

// AptosTests
Expand Down
16 changes: 10 additions & 6 deletions x/spec/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
)

const (
NOT_APPLICABLE int64 = -1
LATEST_BLOCK int64 = -2
EARLIEST_BLOCK int64 = -3
PENDING_BLOCK int64 = -4
SAFE_BLOCK int64 = -5
FINALIZED_BLOCK int64 = -6
NOT_APPLICABLE int64 = -1
LATEST_BLOCK int64 = -2
EARLIEST_BLOCK int64 = -3
PENDING_BLOCK int64 = -4
SAFE_BLOCK int64 = -5
FINALIZED_BLOCK int64 = -6
APIInterfaceJsonRPC = "jsonrpc"
APIInterfaceTendermintRPC = "tendermintrpc"
APIInterfaceRest = "rest"
APIInterfaceGrpc = "grpc"
)

const (
Expand Down

0 comments on commit b5b165a

Please sign in to comment.