Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. All tests passed
2. Code formatted and linted
  • Loading branch information
ayuryshev committed Apr 3, 2019
1 parent 6eb91fe commit 389668e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 35 deletions.
1 change: 1 addition & 0 deletions pkg/transport/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type SignedEntry struct {
Registered int64 `json:"registered,omitempty"`
}

// Index returns position of a given pk in edges
func (se *SignedEntry) Index(pk cipher.PubKey) byte {
if pk == se.Entry.Edges()[1] {
return 1
Expand Down
22 changes: 10 additions & 12 deletions pkg/transport/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"time"

"github.com/google/uuid"

"github.com/skycoin/skywire/pkg/cipher"
)

Expand All @@ -30,23 +28,22 @@ func (handshake settlementHandshake) Do(tm *Manager, tr Transport, timeout time.
}
}

func settlementInitiatorHandshake(id uuid.UUID, public bool) settlementHandshake {
func settlementInitiatorHandshake(public bool) settlementHandshake {
return func(tm *Manager, tr Transport) (*Entry, error) {
entry := &Entry{
ID: id,
ID: GetTransportUUID(tr.Edges()[0], tr.Edges()[1], tr.Type()),
EdgesKeys: tr.Edges(),
Type: tr.Type(),
Public: public,
}

newEntry := id == uuid.UUID{}
if newEntry {
entry.ID = GetTransportUUID(entry.Edges()[0], entry.Edges()[1], entry.Type)
}

// sEntry := &SignedEntry{Entry: entry, Signatures: [2]cipher.Sig{entry.Signature(tm.config.SecKey)}}

sEntry := NewSignedEntry(entry, tm.config.PubKey, tm.config.SecKey)
if err := validateSignedEntry(sEntry, tr, tm.config.PubKey); err != nil {
return nil, fmt.Errorf("NewSignedEntry: %s", err)
}

if err := json.NewEncoder(tr).Encode(sEntry); err != nil {
return nil, fmt.Errorf("write: %s", err)
}
Expand All @@ -60,6 +57,7 @@ func settlementInitiatorHandshake(id uuid.UUID, public bool) settlementHandshake
return nil, err
}

newEntry := tm.walkEntries(func(e *Entry) bool { return *e == *sEntry.Entry }) == nil
if newEntry {
tm.addEntry(entry)
}
Expand All @@ -75,7 +73,7 @@ func settlementResponderHandshake(tm *Manager, tr Transport) (*Entry, error) {
}

// it must be tm.Local() ?
if err := validateEntry(sEntry, tr, tm.Remote(tr.Edges())); err != nil {
if err := validateSignedEntry(sEntry, tr, tm.Remote(tr.Edges())); err != nil {
return nil, err
}

Expand Down Expand Up @@ -109,7 +107,7 @@ func settlementResponderHandshake(tm *Manager, tr Transport) (*Entry, error) {
return sEntry.Entry, nil
}

func validateEntry(sEntry *SignedEntry, tr Transport, rpk cipher.PubKey) error {
func validateSignedEntry(sEntry *SignedEntry, tr Transport, pk cipher.PubKey) error {
entry := sEntry.Entry
if entry.Type != tr.Type() {
return errors.New("invalid entry type")
Expand All @@ -124,7 +122,7 @@ func validateEntry(sEntry *SignedEntry, tr Transport, rpk cipher.PubKey) error {
return errors.New("invalid entry signature")
}

return verifySig(sEntry, rpk)
return verifySig(sEntry, pk)
}

func verifySig(sEntry *SignedEntry, pk cipher.PubKey) error {
Expand Down
85 changes: 71 additions & 14 deletions pkg/transport/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -37,8 +36,8 @@ func newHsMockEnv() *hsMockEnv {
tr1 := NewMockTransport(in, pk1, pk2)
tr2 := NewMockTransport(out, pk2, pk1)

m1, err1 := NewManager(&ManagerConfig{SecKey: sk1, DiscoveryClient: client})
m2, err2 := NewManager(&ManagerConfig{SecKey: sk2, DiscoveryClient: client})
m1, err1 := NewManager(&ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client})
m2, err2 := NewManager(&ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client})

return &hsMockEnv{
client: client,
Expand All @@ -61,7 +60,7 @@ func TestHsMock(t *testing.T) {
require.NoError(t, mockEnv.err2)
}

func ExampleNewHsMock() {
func Example_newHsMock() {
mockEnv := newHsMockEnv()

fmt.Printf("client is set: %v\n", mockEnv.client != nil)
Expand Down Expand Up @@ -99,14 +98,14 @@ func Example_validateEntry() {
Entry: &Entry{Type: "mock",
EdgesKeys: SortPubKeys(pk2, pk3),
}}
if err := validateEntry(entryInvalidEdges, tr, pk1); err != nil {
if err := validateSignedEntry(entryInvalidEdges, tr, pk1); err != nil {
fmt.Println(err.Error())
}

entry := NewEntry(pk1, pk2, "mock", true)
sEntry := NewSignedEntry(entry, pk1, sk1)
if Ok := validateEntry(sEntry, tr, pk1); Ok != nil {
fmt.Printf(Ok.Error())
if Ok := validateSignedEntry(sEntry, tr, pk1); Ok != nil {
fmt.Println(Ok.Error())
}

// Output: invalid entry edges
Expand Down Expand Up @@ -152,7 +151,7 @@ func TestValidateEntry(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.err, func(t *testing.T) {
err := validateEntry(tc.sEntry, tr, pk2)
err := validateSignedEntry(tc.sEntry, tr, pk2)
require.Error(t, err)
assert.Equal(t, tc.err, err.Error())
})
Expand All @@ -162,7 +161,7 @@ func TestValidateEntry(t *testing.T) {
sEntry.SetSignature(pk1, sk1)
sEntry.SetSignature(pk2, sk2)

require.NoError(t, validateEntry(sEntry, tr, pk1))
require.NoError(t, validateSignedEntry(sEntry, tr, pk1))
}

func TestSettlementHandshake(t *testing.T) {
Expand All @@ -181,7 +180,7 @@ func TestSettlementHandshake(t *testing.T) {
errCh <- err
}()

entry, err := settlementInitiatorHandshake(uuid.UUID{}, true)(mockEnv.m1, mockEnv.tr1)
entry, err := settlementInitiatorHandshake(true)(mockEnv.m1, mockEnv.tr1)
require.NoError(t, <-errCh)
require.NoError(t, err)

Expand All @@ -194,15 +193,17 @@ func TestSettlementHandshake(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, entry, dEntry.Entry)

}

/*
func TestSettlementHandshakeInvalidSig(t *testing.T) {
mockEnv := newHsMockEnv()
require.NoError(t, mockEnv.err1)
require.NoError(t, mockEnv.err2)
go settlementInitiatorHandshake(uuid.UUID{}, true)(mockEnv.m2, mockEnv.tr1) // nolint: errcheck
go settlementInitiatorHandshake(true)(mockEnv.m2, mockEnv.tr1) // nolint: errcheck
_, err := settlementResponderHandshake(mockEnv.m2, mockEnv.tr2)
require.Error(t, err)
assert.Equal(t, "Recovered pubkey does not match pubkey", err.Error())
Expand All @@ -212,10 +213,12 @@ func TestSettlementHandshakeInvalidSig(t *testing.T) {
tr2 := NewMockTransport(out, mockEnv.pk2, mockEnv.pk1)
go settlementResponderHandshake(mockEnv.m1, tr2) // nolint: errcheck
_, err = settlementInitiatorHandshake(uuid.UUID{}, true)(mockEnv.m1, tr1)
_, err = settlementInitiatorHandshake(true)(mockEnv.m1, tr1)
require.Error(t, err)
assert.Equal(t, "Recovered pubkey does not match pubkey", err.Error())
}
*/

func TestSettlementHandshakePrivate(t *testing.T) {
mockEnv := newHsMockEnv()
Expand All @@ -231,7 +234,7 @@ func TestSettlementHandshakePrivate(t *testing.T) {
errCh <- err
}()

entry, err := settlementInitiatorHandshake(uuid.UUID{}, false)(mockEnv.m1, mockEnv.tr1)
entry, err := settlementInitiatorHandshake(false)(mockEnv.m1, mockEnv.tr1)
require.NoError(t, <-errCh)
require.NoError(t, err)

Expand All @@ -241,6 +244,7 @@ func TestSettlementHandshakePrivate(t *testing.T) {
assert.Equal(t, entry.ID, resEntry.ID)
_, err = mockEnv.client.GetTransportByID(context.TODO(), entry.ID)
require.Error(t, err)

}

func TestSettlementHandshakeExistingTransport(t *testing.T) {
Expand Down Expand Up @@ -277,7 +281,7 @@ func TestSettlementHandshakeExistingTransport(t *testing.T) {
errCh <- err
}()

entry, err := settlementInitiatorHandshake(entry.ID, true)(mockEnv.m1, mockEnv.tr1)
entry, err := settlementInitiatorHandshake(true)(mockEnv.m1, mockEnv.tr1)
require.NoError(t, <-errCh)
require.NoError(t, err)

Expand All @@ -289,4 +293,57 @@ func TestSettlementHandshakeExistingTransport(t *testing.T) {
require.NoError(t, err)

assert.True(t, dEntry.IsUp)

}

func Example_verifySig() {
mockEnv := newHsMockEnv()

tm, tr := mockEnv.m1, mockEnv.tr1
entry := NewEntry(mockEnv.pk1, mockEnv.pk2, "mock", true)
sEntry := NewSignedEntry(entry, tm.config.PubKey, tm.config.SecKey)
if err := validateSignedEntry(sEntry, tr, tm.config.PubKey); err != nil {
fmt.Printf("NewSignedEntry: %v", err.Error())
}

fmt.Printf("System is working")
// Output: System is working
}

func Example_settlementInitiatorHandshake() {
mockEnv := newHsMockEnv()

// uid := GetTransportUUID(mockEnv.pk1, mockEnv.pk2, "mock")

initHandshake := settlementInitiatorHandshake(true)
respondHandshake := settlementResponderHandshake

// resultCh := make(chan hsResult)
errCh := make(chan error)
go func() {
entry, err := initHandshake(mockEnv.m1, mockEnv.tr1)
if err != nil {
fmt.Printf("initHandshake error: %v\n entry:\n%v\n", err.Error(), entry)
errCh <- err
}
errCh <- nil
// resultCh <- hsResult{entry, err}
}()

go func() {
if _, err := respondHandshake(mockEnv.m2, mockEnv.tr2); err != nil {
fmt.Printf("respondHandshake error: %v\n", err.Error())
errCh <- err
}
errCh <- nil
}()

<-errCh
<-errCh

_ = mockEnv
_ = initHandshake
_ = respondHandshake
fmt.Println("System is working")
// Output: System is working
}
14 changes: 7 additions & 7 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (tm *Manager) ReconnectTransports(ctx context.Context) {
continue
}

_, err := tm.createTransport(ctx, entry.Edges()[1], entry.Type, entry.ID, entry.Public)
_, err := tm.createTransport(ctx, entry.Edges()[1], entry.Type, entry.Public)
if err != nil {
tm.Logger.Warnf("Failed to re-establish transport: %s", err)
continue
Expand Down Expand Up @@ -242,7 +242,7 @@ func SortEdges(edges [2]cipher.PubKey) [2]cipher.PubKey {

// CreateTransport begins to attempt to establish transports to the given 'remote' node.
func (tm *Manager) CreateTransport(ctx context.Context, remote cipher.PubKey, tpType string, public bool) (*ManagedTransport, error) {
return tm.createTransport(ctx, remote, tpType, GetTransportUUID(tm.config.PubKey, remote, tpType), public)
return tm.createTransport(ctx, remote, tpType, public)
}

// DeleteTransport disconnects and removes the Transport of Transport ID.
Expand Down Expand Up @@ -291,13 +291,13 @@ func (tm *Manager) Close() error {
return nil
}

func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tpType string, id uuid.UUID, public bool) (*ManagedTransport, error) {
func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tpType string, public bool) (*ManagedTransport, error) {
factory := tm.factories[tpType]
if factory == nil {
return nil, errors.New("unknown transport type")
}

tr, entry, err := tm.dialTransport(ctx, factory, remote, id, public)
tr, entry, err := tm.dialTransport(ctx, factory, remote, public)
if err != nil {
return nil, err
}
Expand All @@ -323,7 +323,7 @@ func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tp
return
case err := <-managedTr.errChan:
tm.Logger.Infof("Transport %s failed with error: %s. Re-dialing...", managedTr.ID, err)
tr, _, err := tm.dialTransport(ctx, factory, remote, managedTr.ID, public)
tr, _, err := tm.dialTransport(ctx, factory, remote, public)
if err != nil {
tm.Logger.Infof("Failed to re-dial Transport %s: %s", managedTr.ID, err)
if err := tm.DeleteTransport(managedTr.ID); err != nil {
Expand All @@ -340,13 +340,13 @@ func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tp
return managedTr, nil
}

func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote cipher.PubKey, id uuid.UUID, public bool) (Transport, *Entry, error) {
func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote cipher.PubKey, public bool) (Transport, *Entry, error) {
tr, err := factory.Dial(ctx, remote)
if err != nil {
return nil, nil, err
}

entry, err := settlementInitiatorHandshake(id, public).Do(tm, tr, time.Minute)
entry, err := settlementInitiatorHandshake(public).Do(tm, tr, time.Minute)
if err != nil {
tr.Close()
return nil, nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestTransportManager(t *testing.T) {

dEntry, err := client.GetTransportByID(context.TODO(), tr2.ID)
require.NoError(t, err)
assert.Equal(t, [2]cipher.PubKey{pk2, pk1}, dEntry.Entry.Edges)
assert.Equal(t, SortPubKeys(pk2, pk1), dEntry.Entry.Edges())
assert.True(t, dEntry.IsUp)

require.NoError(t, m1.DeleteTransport(tr1.ID))
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) {

dEntry, err := client.GetTransportByID(context.TODO(), tr2.ID)
require.NoError(t, err)
assert.Equal(t, [2]cipher.PubKey{pk2, pk1}, dEntry.Entry.Edges)
assert.Equal(t, SortPubKeys(pk2, pk1), dEntry.Entry.Edges())
assert.True(t, dEntry.IsUp)

require.NoError(t, m2.Close())
Expand Down

0 comments on commit 389668e

Please sign in to comment.