Skip to content

Commit

Permalink
Merge pull request lavanet#5 from lavanet/realyer-pairing
Browse files Browse the repository at this point in the history
feat(relayer): pairing, session handover
  • Loading branch information
gilxgil authored Mar 23, 2022
2 parents 0f24d76 + fd70e73 commit f4aa15d
Show file tree
Hide file tree
Showing 17 changed files with 630 additions and 283 deletions.
28 changes: 9 additions & 19 deletions relayer/cmd/relayer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/lavanet/lava/app"
"github.com/lavanet/lava/relayer"
"github.com/lavanet/lava/x/spec/types"
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/cosmoscmd"
)
Expand Down Expand Up @@ -40,7 +39,6 @@ func main() {
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

port, err := strconv.Atoi(args[1])
if err != nil {
Expand All @@ -54,67 +52,59 @@ func main() {

listenAddr := fmt.Sprintf("%s:%d", args[0], port)
ctx := context.Background()
relayer.Server(ctx, clientCtx, queryClient, listenAddr, args[2], specId)
relayer.Server(ctx, clientCtx, listenAddr, args[2], uint64(specId))

return nil
},
}

var cmdPortalServer = &cobra.Command{
Use: "portal_server [listen-ip] [listen-port] [relayer-url] [relayer-spec-id]",
Use: "portal_server [listen-ip] [listen-port] [relayer-spec-id]",
Short: "portal server",
Long: `portal server`,
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

port, err := strconv.Atoi(args[1])
if err != nil {
return err
}

specId, err := strconv.Atoi(args[3])
specId, err := strconv.Atoi(args[2])
if err != nil {
return err
}

listenAddr := fmt.Sprintf("%s:%d", args[0], port)
ctx := context.Background()
relayer.PortalServer(ctx, clientCtx, queryClient, listenAddr, args[2], specId)
relayer.PortalServer(ctx, clientCtx, listenAddr, uint64(specId))

return nil
},
}

var cmdTestClient = &cobra.Command{
Use: "test_client [listen-ip] [listen-port] [spec-id]",
Use: "test_client [spec-id]",
Short: "test client",
Long: `test client`,
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

port, err := strconv.Atoi(args[1])
if err != nil {
return err
}

specId, err := strconv.Atoi(args[2])
specId, err := strconv.Atoi(args[0])
if err != nil {
return err
}

listenAddr := fmt.Sprintf("%s:%d", args[0], port)
ctx := context.Background()
relayer.TestClient(ctx, clientCtx, queryClient, listenAddr, specId)
relayer.TestClient(ctx, clientCtx, uint64(specId))

return nil
},
Expand Down
40 changes: 9 additions & 31 deletions relayer/portal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,39 @@ package relayer
import (
context "context"
"log"
"math/rand"
"sync"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"github.com/lavanet/lava/x/spec/types"
grpc "google.golang.org/grpc"
)

var g_request_lock sync.Mutex

func PortalServer(
ctx context.Context,
clientCtx client.Context,
queryClient types.QueryClient,
listenAddr string,
relayerUrl string,
specId int,
specId uint64,
) {
//
// Get specs
_, clientApis, err := getSpec(ctx, queryClient, specId)
if err != nil {
log.Fatalln("error: getSpec", err)
}
log.Println(clientApis, specId)
g_clientApis = clientApis

//
// Start sentry
sentry := NewSentry(clientCtx.Client)
err = sentry.Init(ctx)
sentry := NewSentry(clientCtx, specId, true)
err := sentry.Init(ctx)
if err != nil {
log.Fatalln("error sentry.Init", err)
}
go sentry.Start()
go sentry.Start(ctx)
for sentry.GetBlockHeight() == 0 {
time.Sleep(1 * time.Second)
}
g_sentry = sentry
g_serverSpecId = specId

//
// Set up a connection to the server.
log.Println("PortalServer connecting to", relayerUrl)
log.Println("PortalServer")
keyName, err := getKeyName(clientCtx)
if err != nil {
log.Fatalln("error: getKeyName", err)
Expand All @@ -59,16 +47,6 @@ func PortalServer(
clientKey, _ := clientCtx.Keyring.Key(keyName)
log.Println("Client pubkey", clientKey.GetPubKey().Address())

connectCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
conn, err := grpc.DialContext(connectCtx, relayerUrl, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
relayerClient := NewRelayerClient(conn)
sessionId := rand.Int63()

//
// Setup HTTP Server
app := fiber.New(fiber.Config{})
Expand Down Expand Up @@ -97,7 +75,7 @@ func PortalServer(
log.Println("in <<< ", string(msg))

g_request_lock.Lock()
reply, _, err := sendRelay(ctx, clientCtx, relayerClient, privKey, specId, sessionId, string(msg), sentry.GetBlockHeight())
reply, err := sendRelay(ctx, sentry, privKey, g_serverSpecId, string(msg), sentry.GetBlockHeight())
g_request_lock.Unlock()
if err != nil {
log.Println(err)
Expand All @@ -117,7 +95,7 @@ func PortalServer(
defer g_request_lock.Unlock()

log.Println("in <<< ", string(c.Body()))
reply, _, err := sendRelay(ctx, clientCtx, relayerClient, privKey, specId, sessionId, string(c.Body()), sentry.GetBlockHeight())
reply, err := sendRelay(ctx, sentry, privKey, g_serverSpecId, string(c.Body()), sentry.GetBlockHeight())
if err != nil {
log.Println(err)
return nil
Expand Down
4 changes: 2 additions & 2 deletions relayer/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ go run relayer/cmd/relayer/main.go server 127.0.0.1 2222 wss://mainnet.infura.io

```bash
# in lava folder
go run relayer/cmd/relayer/main.go test_client 127.0.0.1 2222 0 --from alice
go run relayer/cmd/relayer/main.go test_client 0 --from alice
```

## Run portal server

```bash
# in lava folder
go run relayer/cmd/relayer/main.go portal_server 127.0.0.1 3333 127.0.0.1:2222 0 --from bob
go run relayer/cmd/relayer/main.go portal_server 127.0.0.1 3333 0 --from bob
```
Loading

0 comments on commit f4aa15d

Please sign in to comment.