From c3b0445c12bfcc8d781d6308d5655cd679620cae Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 8 Apr 2019 11:51:30 +0300 Subject: [PATCH] Changes: 1. Cleared all unnecessary Infof/Printf statements written in previous commits 2. Manager.ReconnectTransports - there was bug after changing Edges logic 3. Formatted, linted Needed: Tests with setup-node with changes in pkg/transport from this commit --- pkg/app/packet_test.go | 2 +- pkg/manager/user.go | 2 +- pkg/node/node_test.go | 2 +- pkg/setup/setup_protocol_test.go | 4 +++- pkg/transport/handshake.go | 26 +++++++------------------- pkg/transport/handshake_test.go | 2 +- pkg/transport/manager.go | 21 ++++++--------------- pkg/transport/manager_test.go | 20 +++++++++++++------- 8 files changed, 33 insertions(+), 46 deletions(-) diff --git a/pkg/app/packet_test.go b/pkg/app/packet_test.go index d88c71697d..7e7b6f1f8e 100644 --- a/pkg/app/packet_test.go +++ b/pkg/app/packet_test.go @@ -6,7 +6,7 @@ import ( "github.com/skycoin/skywire/pkg/cipher" ) -func Example() { +func ExamplePacket() { pk := cipher.PubKey{} addr := Addr{pk, 0} loopAddr := LoopAddr{0, addr} diff --git a/pkg/manager/user.go b/pkg/manager/user.go index 0ac492c5fc..087fbea7ae 100644 --- a/pkg/manager/user.go +++ b/pkg/manager/user.go @@ -26,7 +26,7 @@ func init() { // User represents a user of the manager. type User struct { Name string - PwSalt []byte/* */ + PwSalt []byte /* */ PwHash cipher.SHA256 } diff --git a/pkg/node/node_test.go b/pkg/node/node_test.go index 5edef3b8e5..40085ce754 100644 --- a/pkg/node/node_test.go +++ b/pkg/node/node_test.go @@ -136,7 +136,7 @@ func TestNodeSpawnAppValidations(t *testing.T) { err string }{ {&AppConfig{App: "chat", Version: "1.0", Port: 2}, "can't bind to reserved port 2"}, - {&AppConfig{App: "chat", Version: "1.0", Port: 10}, "App chat is already started"}, + {&AppConfig{App: "chat", Version: "1.0", Port: 10}, "app chat is already started"}, {&AppConfig{App: "foo", Version: "1.0", Port: 11}, "failed to run app executable: foo"}, } diff --git a/pkg/setup/setup_protocol_test.go b/pkg/setup/setup_protocol_test.go index 474e3a2e90..ba54d3e80e 100644 --- a/pkg/setup/setup_protocol_test.go +++ b/pkg/setup/setup_protocol_test.go @@ -95,7 +95,9 @@ func ExampleProtocol_CreateLoop() { fmt.Printf("packet: %v, payload: %v\n", packet, string(payload)) - inProto.Respond(nil) + if err := inProto.Respond(nil); err != nil { + fmt.Println(err.Error()) + } }(inProto) // TODO: create non-empty loop diff --git a/pkg/transport/handshake.go b/pkg/transport/handshake.go index d52674fab5..9426f4b4b0 100644 --- a/pkg/transport/handshake.go +++ b/pkg/transport/handshake.go @@ -78,33 +78,21 @@ func settlementResponderHandshake(tm *Manager, tr Transport) (*Entry, error) { return nil, fmt.Errorf("read: %s", err) } - remote, errRemote := tm.Remote(tr.Edges()) - if errRemote != nil { - return nil, errRemote + remote, err := tm.Remote(tr.Edges()) + if err != nil { + return nil, err + } + + if err := validateSignedEntry(sEntry, tr, remote); err != nil { + return nil, err } - chkRemote := validateSignedEntry(sEntry, tr, remote) - chkLocal := validateSignedEntry(sEntry, tr, tm.Local()) - - tm.Logger.Infof(`validateSignedEntry - chkLocal: %v - chkRemote: %v - sEntry: %v - tr: %v - remote: %v - Edges: %v - tm.config.PubKey: %v - `, chkLocal, chkRemote, sEntry, tr, remote, tr.Edges(), tm.config.PubKey) - - // Write second signature - // sEntry.Signatures[1] = sEntry.Entry.Signature(tm.config.SecKey) if err := sEntry.Sign(tm.Local(), tm.config.SecKey); err != nil { return nil, err } newEntry := tm.walkEntries(func(e *Entry) bool { return *e == *sEntry.Entry }) == nil - var err error if sEntry.Entry.Public { if !newEntry { _, err = tm.config.DiscoveryClient.UpdateStatuses(context.Background(), &Status{ID: sEntry.Entry.ID, IsUp: true}) diff --git a/pkg/transport/handshake_test.go b/pkg/transport/handshake_test.go index f9a9e1b76a..b2c604e4dd 100644 --- a/pkg/transport/handshake_test.go +++ b/pkg/transport/handshake_test.go @@ -296,7 +296,7 @@ func TestSettlementHandshakeExistingTransport(t *testing.T) { } -func Example_verifySig() { +func Example_validateSignedEntry() { mockEnv := newHsMockEnv() tm, tr := mockEnv.m1, mockEnv.tr1 diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 378321b0d0..39bf77e204 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -3,7 +3,6 @@ package transport import ( "context" "errors" - "fmt" "math/big" "strings" "sync" @@ -122,15 +121,17 @@ func (tm *Manager) ReconnectTransports(ctx context.Context) { entries := tm.entries tm.mu.RUnlock() for _, entry := range entries { - if entry.Edges()[0] != tm.config.PubKey { - continue - } if tm.Transport(entry.ID) != nil { continue } - _, err := tm.createTransport(ctx, entry.Edges()[1], entry.Type, entry.Public) + remote, err := tm.Remote(entry.Edges()) + if err != nil { + tm.Logger.Warnf("Failed to re-establish transport: %s", err) + continue + } + _, err = tm.createTransport(ctx, remote, entry.Type, entry.Public) if err != nil { tm.Logger.Warnf("Failed to re-establish transport: %s", err) continue @@ -349,12 +350,6 @@ func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tp func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote cipher.PubKey, public bool) (Transport, *Entry, error) { - fmt.Printf("Manager.dialTransport: %v %v\n", tm.Local(), remote) - - if tm.Local() == remote { - fmt.Println("local and remote are equal. No need to dial") - } - tr, err := factory.Dial(ctx, remote) if err != nil { return nil, nil, err @@ -375,10 +370,6 @@ func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (*Manag return nil, err } - tm.Logger.Infof("Trying to handshake with: %T %v\n", tr, tr) - tm.Logger.Infof("Handshake from factory: %T %v\n", factory, factory) - tm.Logger.Infof("Handshake: tr.Edges(): %v\n tr.Type(): %v\n", tr.Edges(), tr.Type()) - var handshake settlementHandshake = settlementResponderHandshake entry, err := handshake.Do(tm, tr, 30*time.Second) if err != nil { diff --git a/pkg/transport/manager_test.go b/pkg/transport/manager_test.go index aec3edaa3c..709af73281 100644 --- a/pkg/transport/manager_test.go +++ b/pkg/transport/manager_test.go @@ -119,7 +119,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) { assert.Equal(t, []string{"mock"}, m1.Factories()) - errCh := make(chan error) + errCh := make(chan error, 2) go func() { errCh <- m1.Serve(context.TODO()) }() @@ -140,22 +140,28 @@ func TestTransportManagerReEstablishTransports(t *testing.T) { require.NoError(t, m2.Close()) - dEntry, err = client.GetTransportByID(context.TODO(), tr2.ID) + dEntry2, err := client.GetTransportByID(context.TODO(), tr2.ID) require.NoError(t, err) - assert.False(t, dEntry.IsUp) + assert.False(t, dEntry2.IsUp) m2, err = NewManager(c2, f2) require.NoError(t, err) - go m2.Serve(context.TODO()) // nolint - time.Sleep(time.Second) + // errCh2 := make(chan error) + go func() { + errCh <- m2.Serve(context.TODO()) // nolint + }() - dEntry, err = client.GetTransportByID(context.TODO(), tr2.ID) + time.Sleep(time.Second) // TODO: this time.Sleep looks fishy - figure out later + dEntry3, err := client.GetTransportByID(context.TODO(), tr2.ID) require.NoError(t, err) - assert.True(t, dEntry.IsUp) + + assert.True(t, dEntry3.IsUp) require.NoError(t, m2.Close()) require.NoError(t, m1.Close()) + + require.NoError(t, <-errCh) require.NoError(t, <-errCh) }