Skip to content

Commit

Permalink
Move setup transport check from router to transport
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Jul 2, 2019
1 parent 91283de commit 9409dcb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 40 deletions.
6 changes: 3 additions & 3 deletions cmd/skywire-cli/commands/node/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func defaultConfig() *node.Config {
conf.Transport.Discovery = "https://transport.discovery.skywire.skycoin.net"
conf.Transport.LogStore.Type = "file"
conf.Transport.LogStore.Location = "./skywire/transport_logs"

conf.Routing.RouteFinder = "https://routefinder.skywire.skycoin.net/"
sPK := cipher.PubKey{}
sPK.UnmarshalText([]byte("0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557")) // nolint: errcheck
conf.Routing.SetupNodes = []cipher.PubKey{sPK}
conf.Transport.SetupNodes = []cipher.PubKey{sPK}

conf.Routing.RouteFinder = "https://routefinder.skywire.skycoin.net/"
conf.Routing.Table.Type = "boltdb"
conf.Routing.Table.Location = "./skywire/routing.db"
conf.Routing.RouteFinderTimeout = node.Duration(10 * time.Second)
Expand Down
10 changes: 5 additions & 5 deletions pkg/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ type Config struct {
} `json:"messaging"`

Transport struct {
Discovery string `json:"discovery"`
LogStore struct {
SetupNodes []cipher.PubKey `json:"setup_nodes"`
Discovery string `json:"discovery"`
LogStore struct {
Type string `json:"type"`
Location string `json:"location"`
} `json:"log_store"`
} `json:"transport"`

Routing struct {
SetupNodes []cipher.PubKey `json:"setup_nodes"`
RouteFinder string `json:"route_finder"`
RouteFinderTimeout Duration `json:"route_finder_timeout"`
RouteFinder string `json:"route_finder"`
RouteFinderTimeout Duration `json:"route_finder_timeout"`
Table struct {
Type string `json:"type"`
Location string `json:"location"`
Expand Down
3 changes: 1 addition & 2 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ type PacketRouter interface {
io.Closer
Serve(ctx context.Context) error
ServeApp(conn net.Conn, port uint16, appConf *app.Config) error
IsSetupTransport(tr *transport.ManagedTransport) bool
}

// Node provides messaging runtime for Apps by setting up all
Expand Down Expand Up @@ -138,6 +137,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error)
DiscoveryClient: trDiscovery,
LogStore: logStore,
DefaultNodes: config.TrustedNodes,
SetupNodes: config.Transport.SetupNodes,
}
node.tm, err = transport.NewManager(tmConfig, node.messenger)
if err != nil {
Expand All @@ -156,7 +156,6 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error)
TransportManager: node.tm,
RoutingTable: node.rt,
RouteFinder: routeFinder.NewHTTP(config.Routing.RouteFinder, time.Duration(config.Routing.RouteFinderTimeout)),
SetupNodes: config.Routing.SetupNodes,
}
r := router.New(rConfig)
node.router = r
Expand Down
8 changes: 4 additions & 4 deletions pkg/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *RPC) Summary(_ *struct{}, out *Summary) error {
var summaries []*TransportSummary
r.node.tm.WalkTransports(func(tp *transport.ManagedTransport) bool {
summaries = append(summaries,
newTransportSummary(r.node.tm, tp, false, r.node.router.IsSetupTransport(tp)))
newTransportSummary(r.node.tm, tp, false, r.node.tm.IsSetupTransport(tp)))
return true
})
*out = Summary{
Expand Down Expand Up @@ -171,7 +171,7 @@ func (r *RPC) Transports(in *TransportsIn, out *[]*TransportSummary) error {
r.node.tm.WalkTransports(func(tp *transport.ManagedTransport) bool {
if remote, ok := r.node.tm.Remote(tp.Edges()); ok {
if typeIncluded(tp.Type()) && pkIncluded(r.node.tm.Local(), remote) {
*out = append(*out, newTransportSummary(r.node.tm, tp, in.ShowLogs, r.node.router.IsSetupTransport(tp)))
*out = append(*out, newTransportSummary(r.node.tm, tp, in.ShowLogs, r.node.tm.IsSetupTransport(tp)))
}
return true
}
Expand All @@ -186,7 +186,7 @@ func (r *RPC) Transport(in *uuid.UUID, out *TransportSummary) error {
if tp == nil {
return ErrNotFound
}
*out = *newTransportSummary(r.node.tm, tp, true, r.node.router.IsSetupTransport(tp))
*out = *newTransportSummary(r.node.tm, tp, true, r.node.tm.IsSetupTransport(tp))
return nil
}

Expand All @@ -211,7 +211,7 @@ func (r *RPC) AddTransport(in *AddTransportIn, out *TransportSummary) error {
if err != nil {
return err
}
*out = *newTransportSummary(r.node.tm, tp, false, r.node.router.IsSetupTransport(tp))
*out = *newTransportSummary(r.node.tm, tp, false, r.node.tm.IsSetupTransport(tp))
return nil
}

Expand Down
20 changes: 4 additions & 16 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Config struct {
TransportManager *transport.Manager
RoutingTable routing.Table
RouteFinder routeFinder.Client
SetupNodes []cipher.PubKey
}

// Router implements node.PacketRouter. It manages routing table by
Expand Down Expand Up @@ -82,7 +81,7 @@ func (r *Router) Serve(ctx context.Context) error {
go func() {
for tp := range r.tm.TrChan {
r.mu.Lock()
isAccepted, isSetup := tp.Accepted, r.IsSetupTransport(tp)
isAccepted, isSetup := tp.Accepted, r.tm.IsSetupTransport(tp)
r.mu.Unlock()

r.Logger.Infof("New transport: isAccepted: %v, isSetup: %v", isAccepted, isSetup)
Expand Down Expand Up @@ -416,12 +415,13 @@ func (r *Router) destroyLoop(addr *app.LoopAddr) error {
}

func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Transport, error) {
if len(r.config.SetupNodes) == 0 {
setupNodes := r.tm.SetupNodes()
if len(setupNodes) == 0 {
return nil, nil, errors.New("route setup: no nodes")
}

// TODO(evanlinjin): need string constant for tp type.
tr, err := r.tm.CreateTransport(ctx, r.config.SetupNodes[0], dmsg.Type, false)
tr, err := r.tm.CreateTransport(ctx, setupNodes[0], dmsg.Type, false)
if err != nil {
return nil, nil, fmt.Errorf("transport: %s", err)
}
Expand Down Expand Up @@ -480,15 +480,3 @@ func (r *Router) advanceNoiseHandshake(addr *app.LoopAddr, noiseMsg []byte) (ni
noiseRes, err = ni.HandshakeMessage()
return
}

// IsSetupTransport checks whether `tr` is running in the `setup` mode.
func (r *Router) IsSetupTransport(tr *transport.ManagedTransport) bool {
for _, pk := range r.config.SetupNodes {
remote, ok := r.tm.Remote(tr.Edges())
if ok && (remote == pk) {
return true
}
}

return false
}
4 changes: 0 additions & 4 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ func TestRouterSetup(t *testing.T) {
SecKey: sk1,
TransportManager: m1,
RoutingTable: rt,
SetupNodes: []cipher.PubKey{pk2},
}
r := New(conf)
errCh := make(chan error)
Expand Down Expand Up @@ -492,7 +491,6 @@ func TestRouterSetupLoop(t *testing.T) {
TransportManager: m1,
RoutingTable: routing.InMemoryRoutingTable(),
RouteFinder: routeFinder.NewMock(),
SetupNodes: []cipher.PubKey{pk2},
}
r := New(conf)
errCh := make(chan error)
Expand Down Expand Up @@ -602,7 +600,6 @@ func TestRouterCloseLoop(t *testing.T) {
SecKey: sk1,
TransportManager: m1,
RoutingTable: rt,
SetupNodes: []cipher.PubKey{pk2},
}
r := New(conf)
errCh := make(chan error)
Expand Down Expand Up @@ -696,7 +693,6 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) {
SecKey: sk1,
TransportManager: m1,
RoutingTable: rt,
SetupNodes: []cipher.PubKey{pk2},
}
r := New(conf)
errCh := make(chan error)
Expand Down
18 changes: 18 additions & 0 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ManagerConfig struct {
DiscoveryClient DiscoveryClient
LogStore LogStore
DefaultNodes []cipher.PubKey // Nodes to automatically connect to
SetupNodes []cipher.PubKey
}

// Manager manages Transports.
Expand Down Expand Up @@ -414,3 +415,20 @@ func (tm *Manager) manageTransport(ctx context.Context, mTr *ManagedTransport, f
}
}
}

// IsSetupTransport checks whether `tr` is running in the `setup` mode.
func (tm *Manager) IsSetupTransport(tr *ManagedTransport) bool {
for _, pk := range tm.config.SetupNodes {
remote, ok := tm.Remote(tr.Edges())
if ok && (remote == pk) {
return true
}
}

return false
}

// SetupNodes returns a list of Setup Nodes from Manager config.
func (tm *Manager) SetupNodes() []cipher.PubKey {
return tm.config.SetupNodes
}
12 changes: 6 additions & 6 deletions pkg/transport/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestTransportManager(t *testing.T) {
pk1, sk1 := cipher.GenerateKeyPair()
pk2, sk2 := cipher.GenerateKeyPair()

c1 := &ManagerConfig{pk1, sk1, client, logStore, nil}
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil}
c1 := &ManagerConfig{pk1, sk1, client, logStore, nil, []cipher.PubKey{pk2}}
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil, []cipher.PubKey{pk2}}

f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, f1)
Expand Down Expand Up @@ -134,8 +134,8 @@ func TestTransportManagerReEstablishTransports(t *testing.T) {
pk1, sk1 := cipher.GenerateKeyPair()
pk2, sk2 := cipher.GenerateKeyPair()

c1 := &ManagerConfig{pk1, sk1, client, logStore, nil}
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil}
c1 := &ManagerConfig{pk1, sk1, client, logStore, nil, []cipher.PubKey{pk2}}
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil, []cipher.PubKey{pk2}}

f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, f1)
Expand Down Expand Up @@ -196,8 +196,8 @@ func TestTransportManagerLogs(t *testing.T) {
pk1, sk1 := cipher.GenerateKeyPair()
pk2, sk2 := cipher.GenerateKeyPair()

c1 := &ManagerConfig{pk1, sk1, client, logStore1, nil}
c2 := &ManagerConfig{pk2, sk2, client, logStore2, nil}
c1 := &ManagerConfig{pk1, sk1, client, logStore1, nil, []cipher.PubKey{pk2}}
c2 := &ManagerConfig{pk2, sk2, client, logStore2, nil, []cipher.PubKey{pk2}}

f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, f1)
Expand Down

0 comments on commit 9409dcb

Please sign in to comment.