From 9409dcbabb24cee5048a9ac5d07b665b309b9924 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Tue, 2 Jul 2019 20:40:35 +0300 Subject: [PATCH] Move setup transport check from router to transport --- cmd/skywire-cli/commands/node/gen-config.go | 6 +++--- pkg/node/config.go | 10 +++++----- pkg/node/node.go | 3 +-- pkg/node/rpc.go | 8 ++++---- pkg/router/router.go | 20 ++++---------------- pkg/router/router_test.go | 4 ---- pkg/transport/manager.go | 18 ++++++++++++++++++ pkg/transport/manager_test.go | 12 ++++++------ 8 files changed, 41 insertions(+), 40 deletions(-) diff --git a/cmd/skywire-cli/commands/node/gen-config.go b/cmd/skywire-cli/commands/node/gen-config.go index 0f16fe65a4..c388ade0aa 100644 --- a/cmd/skywire-cli/commands/node/gen-config.go +++ b/cmd/skywire-cli/commands/node/gen-config.go @@ -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) diff --git a/pkg/node/config.go b/pkg/node/config.go index 8184ec1e3a..c1714bfef0 100644 --- a/pkg/node/config.go +++ b/pkg/node/config.go @@ -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"` diff --git a/pkg/node/node.go b/pkg/node/node.go index 0c9bb1707c..4658bd842e 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -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 @@ -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 { @@ -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 diff --git a/pkg/node/rpc.go b/pkg/node/rpc.go index b7e8ace227..77ed75c11d 100644 --- a/pkg/node/rpc.go +++ b/pkg/node/rpc.go @@ -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{ @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/router/router.go b/pkg/router/router.go index a920d79f91..eef4c6010c 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -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 @@ -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) @@ -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) } @@ -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 -} diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 508950ea1f..3d522e500e 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 87f200732c..4666e3a43c 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -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. @@ -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 +} diff --git a/pkg/transport/manager_test.go b/pkg/transport/manager_test.go index 0994271e16..17c9ddffb8 100644 --- a/pkg/transport/manager_test.go +++ b/pkg/transport/manager_test.go @@ -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) @@ -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) @@ -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)