Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/route finder single route #568

Closed
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 6 additions & 3 deletions cmd/skywire-cli/commands/rtfind/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rtfind

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -34,10 +35,12 @@ var RootCmd = &cobra.Command{
internal.Catch(srcPK.Set(args[0]))
internal.Catch(dstPK.Set(args[1]))

forward, reverse, err := rfc.PairedRoutes(srcPK, dstPK, frMinHops, frMaxHops)
ctx := context.Background()
routes, err := rfc.FindRoutes(ctx, [][2]cipher.PubKey{{srcPK, dstPK}, {dstPK, srcPK}},
&client.RouteOptions{MinHops: frMinHops, MaxHops: frMaxHops})
internal.Catch(err)

fmt.Println("forward: ", forward)
fmt.Println("reverse: ", reverse)
fmt.Println("forward: ", routes[0][0])
fmt.Println("reverse: ", routes[1][0])
},
}
50 changes: 23 additions & 27 deletions pkg/route-finder/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ const defaultContextTimeout = 10 * time.Second

var log = logging.MustGetLogger("route-finder")

// GetRoutesRequest parses json body for /routes endpoint request
type GetRoutesRequest struct {
SrcPK cipher.PubKey `json:"src_pk,omitempty"`
DstPK cipher.PubKey `json:"dst_pk,omitempty"`
MinHops uint16 `json:"min_hops,omitempty"`
MaxHops uint16 `json:"max_hops,omitempty"`
// RouteOptions for FindRoutesRequest. If nil MinHops and MaxHops will take default values
type RouteOptions struct {
MinHops uint16
MaxHops uint16
}

// GetRoutesResponse encodes the json body of /routes response
type GetRoutesResponse struct {
Forward []routing.Route `json:"forward"`
Reverse []routing.Route `json:"response"`
// FindRoutesRequest parses json body for /routes endpoint request
type FindRoutesRequest struct {
Edges [][2]cipher.PubKey
Opts *RouteOptions
}

// HTTPResponse represents http response struct
Expand All @@ -49,7 +47,7 @@ type HTTPError struct {

// Client implements route finding operations.
type Client interface {
PairedRoutes(source, destiny cipher.PubKey, minHops, maxHops uint16) ([]routing.Route, []routing.Route, error)
FindRoutes(ctx context.Context, rts [][2]cipher.PubKey, opts *RouteOptions) ([][]routing.Route, error)
}

// APIClient implements Client interface
Expand All @@ -72,23 +70,21 @@ func NewHTTP(addr string, apiTimeout time.Duration) Client {
}
}

// PairedRoutes returns routes from source skywire visor to destiny, that has at least the given minHops and as much
// the given maxHops as well as the reverse routes from destiny to source.
func (c *apiClient) PairedRoutes(source, destiny cipher.PubKey, minHops, maxHops uint16) ([]routing.Route, []routing.Route, error) {
requestBody := &GetRoutesRequest{
SrcPK: source,
DstPK: destiny,
MinHops: minHops,
MaxHops: maxHops,
// FindRoutes returns routes from source skywire visor to destiny, that has at least the given minHops and as much
// the given maxHops.
func (c *apiClient) FindRoutes(ctx context.Context, rts [][2]cipher.PubKey, opts *RouteOptions) ([][]routing.Route, error) {

Choose a reason for hiding this comment

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

Have the following changes:

  • Create a new type type Path []routing.Hop
  • Use the following as the output: map[[2]cipher.PubKey][]routing.Path

requestBody := &FindRoutesRequest{
Edges: rts,
Opts: opts,
}
marshaledBody, err := json.Marshal(requestBody)
if err != nil {
return nil, nil, err
return nil, err
}

req, err := http.NewRequest(http.MethodGet, c.addr+"/routes", bytes.NewBuffer(marshaledBody))
if err != nil {
return nil, nil, err
return nil, err
}
req.Header.Set("Content-Type", "application/json")
ctx, cancel := context.WithTimeout(context.Background(), c.apiTimeout)
Expand All @@ -104,27 +100,27 @@ func (c *apiClient) PairedRoutes(source, destiny cipher.PubKey, minHops, maxHops
}()
}
if err != nil {
return nil, nil, err
return nil, err
}

if res.StatusCode != http.StatusOK {
var apiErr HTTPResponse

err = json.NewDecoder(res.Body).Decode(&apiErr)
if err != nil {
return nil, nil, err
return nil, err
}

return nil, nil, errors.New(apiErr.Error.Message)
return nil, errors.New(apiErr.Error.Message)
}

var routes GetRoutesResponse
var routes [][]routing.Route
err = json.NewDecoder(res.Body).Decode(&routes)
if err != nil {
return nil, nil, err
return nil, err
}

return routes.Forward, routes.Reverse, nil
return routes, nil
}

func sanitizedAddr(addr string) string {
Expand Down
31 changes: 15 additions & 16 deletions pkg/route-finder/client/mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"context"
"fmt"
"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/skywire/pkg/routing"
Expand All @@ -23,27 +25,24 @@ func (r *mockClient) SetError(err error) {
r.err = err
}

// PairedRoutes implements Client for MockClient
func (r *mockClient) PairedRoutes(src, dst cipher.PubKey, minHops, maxHops uint16) ([]routing.Route, []routing.Route, error) {
// FindRoutes implements Client for MockClient
func (r *mockClient) FindRoutes(ctx context.Context, rts [][2]cipher.PubKey, opts *RouteOptions) ([][]routing.Route, error) {
if r.err != nil {
return nil, nil, r.err
return nil, r.err
}

return []routing.Route{
{
&routing.Hop{
From: src,
To: dst,
Transport: transport.MakeTransportID(src, dst, ""),
},
},
}, []routing.Route{
if len(rts) == 0 {
return nil, fmt.Errorf("no edges provided to returns routes from")
}
return [][]routing.Route{
{
{
&routing.Hop{
From: src,
To: dst,
Transport: transport.MakeTransportID(src, dst, ""),
From: rts[0][0],
To: rts[0][1],
Transport: transport.MakeTransportID(rts[0][0], rts[0][1], ""),
},
},
}, nil
},
}, nil
}
8 changes: 5 additions & 3 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ func (r *Router) fetchBestRoutes(source, destination cipher.PubKey) (fwd routing
defer timer.Stop()

fetchRoutesAgain:
fwdRoutes, revRoutes, err := r.conf.RouteFinder.PairedRoutes(source, destination, minHops, maxHops)
ctx := context.Background()
routes, err := r.conf.RouteFinder.FindRoutes(ctx, [][2]cipher.PubKey{{source, destination}, {destination, source}},
&routeFinder.RouteOptions{MinHops: minHops, MaxHops: maxHops})
if err != nil {
select {
case <-timer.C:
Expand All @@ -426,8 +428,8 @@ fetchRoutesAgain:
}
}

r.Logger.Infof("Found routes Forward: %s. Reverse %s", fwdRoutes, revRoutes)
return fwdRoutes[0], revRoutes[0], nil
r.Logger.Infof("Found routes Forward: %s. Reverse %s", routes[0], routes[1])
return routes[0][0], routes[1][0], nil
}

// SetupIsTrusted checks if setup node is trusted.
Expand Down