From 76da7b0d22392b08fc11de54a4b57addcff54ee7 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Fri, 5 Jun 2020 21:07:30 +0300 Subject: [PATCH 1/4] Initial work on adding trusted visors --- Makefile | 2 +- pkg/transport/manager.go | 25 +++++++++++++++++++++++++ pkg/visor/visor.go | 7 +++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a9ded5caf8..42b77fcd16 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ BUILD_OPTS?=$(BUILDINFO) check: lint test ## Run linters and tests -build: dep host-apps bin ## Install dependencies, build apps and binaries. `go build` with ${OPTS} +build: dep host-apps bin ## Install dependencies, build apps and binaries. `go build` with ${OPTS} run: stop build config ## Run skywire-visor on host ./skywire-visor skywire.json diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 4c1df36428..375a758b54 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -17,6 +17,12 @@ import ( "github.com/SkycoinProject/skywire-mainnet/pkg/skyenv" "github.com/SkycoinProject/skywire-mainnet/pkg/snet" "github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest" + "github.com/SkycoinProject/skywire-mainnet/pkg/snet/stcpr" +) + +const ( + trustedVisorsTransportType = stcpr.Type + TrustedVisorsDelay = 10 * time.Second ) // ManagerConfig configures a Manager. @@ -136,6 +142,25 @@ func (tm *Manager) serve(ctx context.Context) { } } +func (tm *Manager) AddTrustedVisors(ctx context.Context) { + for _, pk := range tm.Conf.DefaultVisors { + tm.Logger.WithField("pk", pk).Infof("Adding trusted visor") + + if _, err := tm.SaveTransport(ctx, pk, trustedVisorsTransportType); err != nil { + tm.Logger. + WithError(err). + WithField("pk", pk). + WithField("type", trustedVisorsTransportType). + Warnf("Failed to add transport to trusted visor via") + } else { + tm.Logger. + WithField("pk", pk). + WithField("type", trustedVisorsTransportType). + Infof("Added transport to trusted visor") + } + } +} + func (tm *Manager) initTransports(ctx context.Context) { tm.mx.Lock() defer tm.mx.Unlock() diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index b375a81f03..175f798fd7 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -2,6 +2,7 @@ package visor import ( + "context" "errors" "fmt" "os/exec" @@ -161,6 +162,12 @@ func NewVisor(conf *visorconfig.V1, restartCtx *restart.Context) (v *Visor, ok b return v, ok } + ctx := context.Background() + go func(tpM *transport.Manager) { + time.Sleep(transport.TrustedVisorsDelay) + tpM.AddTrustedVisors(ctx) + }(v.tpM) + log.Info("Startup complete!") return v, ok } From ccb1d4730263ee2e35bf5a7a762b528f417fed75 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Fri, 5 Jun 2020 21:26:18 +0300 Subject: [PATCH 2/4] Decrease delay before establishing connection to trusted visors --- pkg/transport/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 375a758b54..6dadb0c0e4 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -22,7 +22,7 @@ import ( const ( trustedVisorsTransportType = stcpr.Type - TrustedVisorsDelay = 10 * time.Second + TrustedVisorsDelay = 5 * time.Second ) // ManagerConfig configures a Manager. From b3e5dc60fd375e70474c4a3028d2a1d3f05ef9d1 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Fri, 5 Jun 2020 21:32:14 +0300 Subject: [PATCH 3/4] Fix linter --- pkg/transport/manager.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 6dadb0c0e4..3eefa0ca7f 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -22,7 +22,8 @@ import ( const ( trustedVisorsTransportType = stcpr.Type - TrustedVisorsDelay = 5 * time.Second + // TrustedVisorsDelay defines a delay before adding transports to trusted visors. + TrustedVisorsDelay = 5 * time.Second ) // ManagerConfig configures a Manager. @@ -142,6 +143,7 @@ func (tm *Manager) serve(ctx context.Context) { } } +// AddTrustedVisors adds transports to trusted visors from config. func (tm *Manager) AddTrustedVisors(ctx context.Context) { for _, pk := range tm.Conf.DefaultVisors { tm.Logger.WithField("pk", pk).Infof("Adding trusted visor") From 495d7b7f6aa10b2110eaea9da6f55233a554688d Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Mon, 6 Jul 2020 17:17:28 +0300 Subject: [PATCH 4/4] Changes suggested by @evanlinjin --- pkg/transport/manager.go | 24 ------------------------ pkg/visor/init.go | 28 ++++++++++++++++++++++++++++ pkg/visor/visor.go | 10 ---------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 6ce22931b4..0934851a8e 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -17,11 +17,9 @@ import ( "github.com/SkycoinProject/skywire-mainnet/pkg/skyenv" "github.com/SkycoinProject/skywire-mainnet/pkg/snet" "github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest" - "github.com/SkycoinProject/skywire-mainnet/pkg/snet/stcpr" ) const ( - trustedVisorsTransportType = stcpr.Type // TrustedVisorsDelay defines a delay before adding transports to trusted visors. TrustedVisorsDelay = 5 * time.Second ) @@ -165,26 +163,6 @@ func (tm *Manager) serve(ctx context.Context) { tm.listenersMu.Unlock() } -// AddTrustedVisors adds transports to trusted visors from config. -func (tm *Manager) AddTrustedVisors(ctx context.Context) { - for _, pk := range tm.Conf.DefaultVisors { - tm.Logger.WithField("pk", pk).Infof("Adding trusted visor") - - if _, err := tm.SaveTransport(ctx, pk, trustedVisorsTransportType); err != nil { - tm.Logger. - WithError(err). - WithField("pk", pk). - WithField("type", trustedVisorsTransportType). - Warnf("Failed to add transport to trusted visor via") - } else { - tm.Logger. - WithField("pk", pk). - WithField("type", trustedVisorsTransportType). - Infof("Added transport to trusted visor") - } - } -} - func (tm *Manager) initTransports(ctx context.Context) { tm.mx.Lock() defer tm.mx.Unlock() @@ -309,8 +287,6 @@ func isSTCPTableError(remotePK cipher.PubKey, err error) bool { return err.Error() == fmt.Sprintf("pk table: entry of %s does not exist", remotePK.String()) } -var () - func (tm *Manager) saveTransport(remote cipher.PubKey, netName string) (*ManagedTransport, error) { if !snet.IsKnownNetwork(netName) { return nil, snet.ErrUnknownNetwork diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 676e649006..f91b766f6d 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -26,6 +26,7 @@ import ( "github.com/SkycoinProject/skywire-mainnet/pkg/skyenv" "github.com/SkycoinProject/skywire-mainnet/pkg/snet" "github.com/SkycoinProject/skywire-mainnet/pkg/snet/arclient" + "github.com/SkycoinProject/skywire-mainnet/pkg/snet/directtp/tptypes" "github.com/SkycoinProject/skywire-mainnet/pkg/transport" "github.com/SkycoinProject/skywire-mainnet/pkg/transport/tpdclient" "github.com/SkycoinProject/skywire-mainnet/pkg/util/updater" @@ -47,6 +48,7 @@ func initStack() []initFunc { initCLI, initHypervisors, initUptimeTracker, + initTrustedVisors, } } @@ -454,6 +456,32 @@ func initUptimeTracker(v *Visor) bool { return true } +func initTrustedVisors(v *Visor) bool { + const trustedVisorsTransportType = tptypes.STCPR + + go func() { + time.Sleep(transport.TrustedVisorsDelay) + for _, pk := range v.tpM.Conf.DefaultVisors { + v.log.WithField("pk", pk).Infof("Adding trusted visor") + + if _, err := v.tpM.SaveTransport(context.Background(), pk, trustedVisorsTransportType); err != nil { + v.log. + WithError(err). + WithField("pk", pk). + WithField("type", trustedVisorsTransportType). + Warnf("Failed to add transport to trusted visor via") + } else { + v.log. + WithField("pk", pk). + WithField("type", trustedVisorsTransportType). + Infof("Added transport to trusted visor") + } + } + }() + + return true +} + func connectToTpDisc(v *Visor) (transport.DiscoveryClient, error) { const ( initBO = 1 * time.Second diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 8f3f680997..d459e7fdb1 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -2,7 +2,6 @@ package visor import ( - "context" "errors" "fmt" "os/exec" @@ -41,9 +40,6 @@ const ( supportedProtocolVersion = "0.1.0" ownerRWX = 0700 shortHashLen = 6 -) - -const ( // moduleShutdownTimeout is the timeout given to a module to shutdown cleanly. // Otherwise the shutdown logic will continue and report a timeout error. moduleShutdownTimeout = time.Second * 2 @@ -168,12 +164,6 @@ func NewVisor(conf *visorconfig.V1, restartCtx *restart.Context) (v *Visor, ok b return v, ok } - ctx := context.Background() - go func(tpM *transport.Manager) { - time.Sleep(transport.TrustedVisorsDelay) - tpM.AddTrustedVisors(ctx) - }(v.tpM) - log.Info("Startup complete!") return v, ok }