Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. Implemented transport.GetTransportUUID function that creates
namesplace-based uuid.UUID from pair of cipher.Pubkey
2. This function is used instead of uuid.New() for creation IDs of
Transports

Result:

- It passes all tests
- But with manual check it breaks on https://github.com/skycoin/skywire/blob/c1dc372655d380d05e3eb0558ab55650757b7439/pkg/transport/manager.go#L166
  • Loading branch information
ayuryshev committed Mar 29, 2019
1 parent c1dc372 commit 2e8a2da
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pkg/node/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
for i := range tps {
remotePK, _ := cipher.GenerateKeyPair()
tps[i] = &TransportSummary{
ID: uuid.New(),
ID: transport.GetTransportUUID(localPK, remotePK),
Local: localPK,
Remote: remotePK,
Type: types[r.Int()%len(types)],
Expand Down Expand Up @@ -314,7 +314,7 @@ func (mc *mockRPCClient) Transport(tid uuid.UUID) (*TransportSummary, error) {
// AddTransport implements RPCClient.
func (mc *mockRPCClient) AddTransport(remote cipher.PubKey, tpType string, public bool, _ time.Duration) (*TransportSummary, error) {
summary := &TransportSummary{
ID: uuid.New(),
ID: transport.GetTransportUUID(mc.s.PubKey, remote),
Local: mc.s.PubKey,
Remote: remote,
Type: tpType,
Expand Down
9 changes: 4 additions & 5 deletions pkg/route-finder/client/mock.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package client

import (
"github.com/google/uuid"

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

// MockClient implements mock route finder client.
Expand All @@ -23,7 +22,7 @@ func (r *mockClient) SetError(err error) {
r.err = err
}

// PairedRoutes implements Clien for MockClient
// PairedRoutes implements Client for MockClient
func (r *mockClient) PairedRoutes(src, dst cipher.PubKey, minHops, maxHops uint16) ([]routing.Route, []routing.Route, error) {
if r.err != nil {
return nil, nil, r.err
Expand All @@ -34,15 +33,15 @@ func (r *mockClient) PairedRoutes(src, dst cipher.PubKey, minHops, maxHops uint1
&routing.Hop{
From: src,
To: dst,
Transport: uuid.New(),
Transport: transport.GetTransportUUID(src, dst),
},
},
}, []routing.Route{
{
&routing.Hop{
From: src,
To: dst,
Transport: uuid.New(),
Transport: transport.GetTransportUUID(src, dst),
},
},
}, nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/transport-discovery/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"testing"

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

Expand All @@ -25,7 +24,7 @@ var testPubKey, testSecKey = cipher.GenerateKeyPair()
func newTestEntry() *transport.Entry {
pk1, _ := cipher.GenerateKeyPair()
return &transport.Entry{
ID: uuid.New(),
ID: transport.GetTransportUUID(pk1, testPubKey),
Edges: [2]cipher.PubKey{pk1, testPubKey},
Type: "messaging",
Public: true,
Expand Down
3 changes: 2 additions & 1 deletion pkg/transport/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func settlementInitiatorHandshake(id uuid.UUID, public bool) settlementHandshake

newEntry := id == uuid.UUID{}
if newEntry {
entry.ID = uuid.New()
entry.ID = GetTransportUUID(tr.Local(), tr.Remote())
}

sEntry := &SignedEntry{Entry: entry, Signatures: [2]cipher.Sig{entry.Signature(tm.config.SecKey)}}
Expand Down Expand Up @@ -76,6 +76,7 @@ func settlementResponderHandshake(tm *Manager, tr Transport) (*Entry, error) {
}

sEntry.Signatures[1] = sEntry.Entry.Signature(tm.config.SecKey)

newEntry := tm.walkEntries(func(e *Entry) bool { return *e == *sEntry.Entry }) == nil

var err error
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestSettlementHandshakeExistingTransport(t *testing.T) {
require.NoError(t, err)

entry := &Entry{
ID: uuid.New(),
ID: GetTransportUUID(pk1, pk2),
Edges: [2]cipher.PubKey{pk1, pk2},
Type: "mock",
Public: true,
Expand Down
30 changes: 18 additions & 12 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,22 @@ func (tm *Manager) Serve(ctx context.Context) error {
return nil
}

// GenTransportUUID generates uuid.UUID from pair of keys
func GetTransportUUID(keyA, keyB cipher.PubKey) uuid.UUID {
var mixedKeys = make([]byte, 66)
for i := 0; i < 33; i++ {
if keyA[i] < keyB[i] {
mixedKeys[i*2], mixedKeys[i*2+1] = keyA[i], keyB[i]
} else {
mixedKeys[i*2], mixedKeys[i*2+1] = keyB[i], keyA[i]
}
}
return uuid.NewSHA1(uuid.UUID{}, mixedKeys)
}

// 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) {
uid := uuid.NewSHA1(uuid.UUID{}, append(tm.config.PubKey[:], remote[:]...))
return tm.createTransport(ctx, remote, tpType, uid, public)
return tm.createTransport(ctx, remote, tpType, GetTransportUUID(tm.config.PubKey, remote), public)
}

// DeleteTransport disconnects and removes the Transport of Transport ID.
Expand Down Expand Up @@ -326,24 +338,18 @@ func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (*Manag
}

tm.Logger.Infof("Accepted new transport with type %s from %s. ID: %s", factory.Type(), tr.Remote(), entry.ID)

managedTr := newManagedTransport(entry.ID, tr, entry.Public)
tm.mu.Lock()
rpk, lpk := tr.Remote(), tr.Local()
uid := uuid.NewSHA1(uuid.UUID{}, append(lpk[:], rpk[:]...))
managedTr := newManagedTransport(uid, tr, entry.Public)

if existingTr, ok := tm.transports[uid]; ok {
return existingTr, nil
}

tm.transports[uid] = managedTr
tm.transports[entry.ID] = managedTr
select {
case <-tm.doneChan:
case tm.acceptedTrChan <- managedTr:
default:
}
tm.mu.Unlock()

// go func(managedTr *ManagedTransport, tm *Manager) {
go func() {
select {
case <-managedTr.doneChan:
Expand All @@ -354,7 +360,7 @@ func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (*Manag
return
case err := <-managedTr.errChan:
tm.Logger.Infof("Transport %s failed with error: %s. Re-dialing...", managedTr.ID, err)
if err := tm.DeleteTransport(uid); err != nil {
if err := tm.DeleteTransport(managedTr.ID); err != nil {
tm.Logger.Warnf("Failed to delete transport: %s", err)
}
}
Expand Down

0 comments on commit 2e8a2da

Please sign in to comment.