Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions rmb-sdk-go/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"net/url"
"slices"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -145,34 +144,55 @@ func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, erro
priv := secp256k1.PrivKeyFromBytes(keyPair.Seed())
return priv, nil
}

// getRelayConnections tries to connect to all relays and returns only the successful ones
// getRelayConnections returns InnerConnections for all valid relay URLs
func getRelayConnections(relayURLs []string, identity substrate.Identity, session string, twinID uint32) ([]string, []InnerConnection, error) {
var validRelayURLs []string
var connections []InnerConnection
func validateRelayURLs(relayURLs []string) ([]*url.URL, error) {
var validRelayURLs []*url.URL

for _, relayURL := range relayURLs {
parsedURL, err := url.Parse(relayURL)
if err != nil {
log.Warn().Err(err).Str("url", relayURL).Msg("failed to parse relay URL, skipping")
continue
}
validRelayURLs = append(validRelayURLs, parsedURL.Host)
conn := NewConnection(identity, relayURL, session, twinID)
connections = append(connections, conn)
// make sure it is ws or wss
if parsedURL.Scheme != "ws" && parsedURL.Scheme != "wss" {
log.Warn().Str("url", relayURL).Msg("relay URL must be ws or wss, skipping")
continue
}
validRelayURLs = append(validRelayURLs, parsedURL)
}

if len(connections) == 0 {
return nil, nil, ErrNoValidRelayURLs
if len(validRelayURLs) == 0 {
return nil, ErrNoValidRelayURLs
}

sort.Slice(validRelayURLs, func(i, j int) bool {
return strings.ToLower(validRelayURLs[i]) < strings.ToLower(validRelayURLs[j])
slices.SortFunc(validRelayURLs, func(a, b *url.URL) int {
return strings.Compare(a.Host, b.Host)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also we depend on hosts only ignoring ports

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

})
validRelayURLs = slices.CompactFunc(validRelayURLs, func(a, b *url.URL) bool {
return a.Host == b.Host
})
validRelayURLs = slices.Compact(validRelayURLs)

return validRelayURLs, connections, nil
return validRelayURLs, nil
}

// getRelayConnections tries to connect to all relays and returns only the successful ones
// getRelayConnections returns InnerConnections for all valid relay URLs
func getRelayConnections(relayURLs []string, identity substrate.Identity, session string, twinID uint32) ([]string, []InnerConnection, error) {
validRelayURLs, err := validateRelayURLs(relayURLs)
if err != nil {
return nil, nil, err
}
connections := make([]InnerConnection, 0, len(validRelayURLs))
hosts := make([]string, 0, len(validRelayURLs))

for _, relayURL := range validRelayURLs {
conn := NewConnection(identity, relayURL.String(), session, twinID)
connections = append(connections, conn)
host := strings.ToLower(relayURL.Host)
hosts = append(hosts, host)
}

return hosts, connections, nil
}

func getIdentity(keytype string, mnemonics string) (substrate.Identity, error) {
Expand Down Expand Up @@ -266,7 +286,7 @@ func NewPeer(
publicKey = privKey.PubKey().SerializeCompressed()
}

relayURLs, conns, err := getRelayConnections(cfg.relayURLs, identity, cfg.session, twin.ID)
hosts, conns, err := getRelayConnections(cfg.relayURLs, identity, cfg.session, twin.ID)
if err != nil {
return nil, err
}
Expand All @@ -281,7 +301,7 @@ func NewPeer(
}
}

joinURLs := strings.Join(relayURLs, "_")
joinURLs := strings.Join(hosts, "_")
if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || joinURLs != *twin.Relay {
log.Info().Str("Relay url/s", joinURLs).Msg("twin relay/public key didn't match, updating on chain ...")
if _, err = subConn.UpdateTwin(identity, joinURLs, publicKey); err != nil {
Expand Down Expand Up @@ -320,7 +340,7 @@ func NewPeer(
cons: cons,
handler: handler,
encoder: cfg.encoder,
relays: relayURLs,
relays: hosts,
}

go cl.process(ctx)
Expand Down
Loading