Skip to content

Commit

Permalink
replace chain with registrar in rmb client
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Feb 27, 2025
1 parent 8188c09 commit 0fda763
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 117 deletions.
29 changes: 1 addition & 28 deletions rmb-sdk-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,8 @@ This connection could be established using a `direct-client`, or an `rmb-peer`.
A process could connect to an `rmb-relay` using a direct client.\
To create a new direct client instance, a process needs to have:

- A valid mnemonics, with an activated account on the TFChain.
- A valid private key, with an activated account on the Registrar.
- The key type of these mnemonics.
- A relay URL that the direct client will connect to.
- A session id. This could be anything, but a twin must only have a unique session id per connection.
- A substrate connection.

#### **Example**

Creating a new direct client instance:

```Go
subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws")
sub, err := subManager.Substrate()
if err != nil {
return fmt.Errorf("failed to connect to substrate: %w", err)
}

defer sub.Close()
client, err := direct.NewRpcClient(direct.KeyTypeSr25519, mnemonics, "wss://relay.dev.grid.tf", "test-client", sub, false)
if err != nil {
return fmt.Errorf("failed to create direct client: %w", err)
}
```

Assuming there is a remote calculator process that could add two integers, an rmb call using the direct client would look like this:

```Go
x := 1
y := 2
var sum int
err := client.Call(ctx, destinationTwinID, "calculator.add", []int{x, y}, &sum)
```
2 changes: 1 addition & 1 deletion rmb-sdk-go/examples/rpc_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [rpc client](https://github.com/threefoldte

To use the example, you needs to:

- Set the mnemonics variable to a valid mnemonics, with an activated account on the TFChain.
- Set the private key variable to a valid private key, with an activated account on the Registrar.
- A node id to send the call to

## Usage
Expand Down
19 changes: 13 additions & 6 deletions rmb-sdk-go/examples/rpc_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"context"
"encoding/hex"
"fmt"
"log"
"time"

substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
)

Expand All @@ -16,13 +16,20 @@ type version struct {
}

func app() error {
mnemonics := "<mnemonics goes here>"
subNodeURL := "wss://tfchain.dev.grid.tf/ws"
relayURL := "wss://relay.dev.grid.tf"
privateKey := "<private key goes here>"

subManager := substrate.NewManager(subNodeURL)
privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return fmt.Errorf("failed to decode private key: %w", err)
}

client, err := peer.NewRpcClient(context.Background(), mnemonics, subManager, peer.WithRelay(relayURL), peer.WithSession("test-client"))
client, err := peer.NewRpcClient(
context.Background(),
privateKeyBytes,
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
peer.WithRelay("wss://relay.dev.grid.tf"),
peer.WithSession("test-client"),
)
if err != nil {
return fmt.Errorf("failed to create direct client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion rmb-sdk-go/peer/examples/peer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [direct client](https://github.com/threefol

To use the example, you needs to:

- Set the mnemonics variable to a valid mnemonics, with an activated account on the TFChain.
- Set the mnemonics variable to a valid mnemonics, with an activated account on the Registrar.
- Set dst to the twinId of a remote calculator process that could add two integers

## Usage
Expand Down
15 changes: 10 additions & 5 deletions rmb-sdk-go/peer/examples/peer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@ package main

import (
"context"
"encoding/hex"
"fmt"
"math/rand"

"github.com/google/uuid"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types"
)

var resultsChan = make(chan bool)

func app() error {
mnemonics := "<mnemonics goes here>"
subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws")
privateKey := "<private key here>"
ctx := context.Background()

privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return fmt.Errorf("failed to decode private key: %w", err)
}

peer, err := peer.NewPeer(
ctx,
mnemonics,
subManager,
privateKeyBytes,
relayCallback,
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
peer.WithRelay("wss://relay.dev.grid.tf"),
peer.WithSession("test-client"),
peer.WithInMemoryExpiration(6*60*60), // six hours
peer.WithKeyType(peer.KeyTypeEd25519),
)

if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions rmb-sdk-go/peer/examples/peer_pingmany/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -10,17 +11,14 @@ import (
"time"

"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"

"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer/types"
// "rmbClient/peer"
)

const (
chainUrl = "wss://tfchain.grid.tf/"
relayUrl = "ws://localhost:"
mnemonic = "<mnemonic>"
relayUrl = "ws://localhost:"
privateKey = "<private key>"
)

type Node struct {
Expand All @@ -32,8 +30,6 @@ var static = []uint32{7, 9, 10, 13, 14, 16, 22, 23, 24, 27, 29, 35, 46, 47, 69,
const use_static = true

func main() {
subMan := substrate.NewManager(chainUrl)

count := 500
var wg sync.WaitGroup
wg.Add(count)
Expand All @@ -59,10 +55,16 @@ func main() {
log.Info().Uint32("twin", env.Source.Twin).Str("version", version).Msg("received response")
}

privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
log.Error().Err(err).Msg("failed to decode private key")
return
}

bus, err := peer.NewPeer(context.Background(),
mnemonic,
subMan,
privateKeyBytes,
handler,
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
peer.WithKeyType(peer.KeyTypeSr25519),
peer.WithSession("rmb-playground999"),
peer.WithInMemoryExpiration(10*60*60), // in seconds that's 10 hours
Expand Down
2 changes: 1 addition & 1 deletion rmb-sdk-go/peer/examples/router_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [peer router using direct client](https://g

To use the example, you needs to:

- Set the mnemonics variable to a valid mnemonics for client peer and server, with an activated account on the TFChain.
- Set the mnemonics variable to a valid mnemonics for client peer and server, with an activated account on the Registrar.
- set the client peer destination twin and session with the ones of the created peer router.
- make sure you are running the server before the client peer.

Expand Down
17 changes: 11 additions & 6 deletions rmb-sdk-go/peer/examples/router_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"

substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
)
Expand Down Expand Up @@ -59,20 +59,25 @@ func app() error {
})

// adding a peer for the router
mnemonics := "<mnemonics goes here>"
subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws")
privateKey := "<private key here>"
ctx := context.Background()

privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return fmt.Errorf("failed to decode private key: %w", err)
}

// this peer will be a 'calculator' session.
// means other peers on the network need to know that
// session id to use when they are making calls
_, err := peer.NewPeer(
_, err = peer.NewPeer(
ctx,
mnemonics,
subManager,
privateKeyBytes,
router.Serve,
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
peer.WithRelay("wss://relay.dev.grid.tf"),
peer.WithSession("calculator"),
peer.WithKeyType(peer.KeyTypeEd25519),
)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rmb-sdk-go/peer/examples/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a `Go` example for the `RMB` [rpc client](https://github.com/threefoldte

To use the example, you needs to:

- Set the mnemonics variable to a valid mnemonics, with an activated account on the TFChain.
- Set the mnemonics variable to a valid mnemonics, with an activated account on the Registrar.
- A twinId of a remote calculator process that could add two integers

## Usage
Expand Down
14 changes: 9 additions & 5 deletions rmb-sdk-go/peer/examples/rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ package main

import (
"context"
"encoding/hex"
"fmt"
"time"

"log"

substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
)

func app() error {
mnemonics := "<mnemonics goes here>"
subManager := substrate.NewManager("wss://tfchain.dev.grid.tf/ws")
privateKey := "<private key goes here>"

privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return fmt.Errorf("failed to decode private key: %w", err)
}

client, err := peer.NewRpcClient(
context.Background(),
mnemonics,
subManager,
privateKeyBytes,
peer.WithRegistrarUrl("https://registrar.dev4.grid.tf"),
peer.WithKeyType(peer.KeyTypeSr25519),
peer.WithRelay("wss://relay.dev.grid.tf"),
peer.WithSession("test-client"),
Expand Down
Loading

0 comments on commit 0fda763

Please sign in to comment.