Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. MakeTransportID - different IDs for public and private transports
  • Loading branch information
ayuryshev committed Apr 4, 2019
1 parent 1ca7c11 commit 377470e
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 39 deletions.
1 change: 0 additions & 1 deletion pkg/messaging/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func (c *channel) Close() error {
return nil
}


func (c *channel) SetDeadline(t time.Time) error {
c.deadline = t
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/messaging/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func handshakeChannel(t *testing.T, c *channel, pk cipher.PubKey, sk cipher.SecK
noiseConf := noise.Config{
LocalSK: sk,
LocalPK: pk,
RemotePK: c.Local(),
RemotePK: c.link.Local(),
Initiator: false,
}

Expand Down
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: transport.MakeTransportID(localPK, remotePK, types[r.Int()%len(types)]),
ID: transport.MakeTransportID(localPK, remotePK, types[r.Int()%len(types)], true),
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: transport.MakeTransportID(mc.s.PubKey, remote, tpType),
ID: transport.MakeTransportID(mc.s.PubKey, remote, tpType, public),
Local: mc.s.PubKey,
Remote: remote,
Type: tpType,
Expand Down
4 changes: 2 additions & 2 deletions pkg/route-finder/client/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func (r *mockClient) PairedRoutes(src, dst cipher.PubKey, minHops, maxHops uint1
&routing.Hop{
From: src,
To: dst,
Transport: transport.MakeTransportID(src, dst, ""),
Transport: transport.MakeTransportID(src, dst, "", true),
},
},
}, []routing.Route{
{
&routing.Hop{
From: src,
To: dst,
Transport: transport.MakeTransportID(src, dst, ""),
Transport: transport.MakeTransportID(src, dst, "", true),
},
},
}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport-discovery/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var testPubKey, testSecKey = cipher.GenerateKeyPair()
func newTestEntry() *transport.Entry {
pk1, _ := cipher.GenerateKeyPair()
entry := &transport.Entry{
ID: transport.MakeTransportID(pk1, testPubKey, "messaging"),
ID: transport.MakeTransportID(pk1, testPubKey, "messaging", true),
Type: "messaging",
Public: true,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ExampleNewDiscoveryMock() {
if err := dc.RegisterTransports(context.TODO(), sEntry); err == nil {
fmt.Println("RegisterTransport success")
} else {
fmt.Println(Ok.Error())
fmt.Println(err.Error())
}

if entryWS, err := dc.GetTransportByID(context.TODO(), sEntry.Entry.ID); err == nil {
Expand All @@ -36,7 +36,7 @@ func ExampleNewDiscoveryMock() {
if _, err := dc.UpdateStatuses(context.TODO(), &Status{}); err == nil {
fmt.Println("UpdateStatuses success")
} else {
fmt.Println(Ok.Error())
fmt.Println(err.Error())
}

// Output: RegisterTransport success
Expand Down
8 changes: 4 additions & 4 deletions pkg/transport/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Entry struct {
// NewEntry constructs *Entry
func NewEntry(edgeA, edgeB cipher.PubKey, tpType string, public bool) *Entry {
return &Entry{
ID: MakeTransportID(edgeA, edgeB, tpType),
ID: MakeTransportID(edgeA, edgeB, tpType, public),
EdgeKeys: SortPubKeys(edgeA, edgeB),
Type: tpType,
Public: public,
Expand All @@ -48,7 +48,7 @@ func (e *Entry) Edges() [2]cipher.PubKey {

// SetEdges sets edges of Entry
func (e *Entry) SetEdges(edges [2]cipher.PubKey) {
e.ID = MakeTransportID(edges[0], edges[1], e.Type)
e.ID = MakeTransportID(edges[0], edges[1], e.Type, e.Public)
e.EdgeKeys = SortPubKeys(edges[0], edges[1])
}

Expand Down Expand Up @@ -113,14 +113,14 @@ func (se *SignedEntry) Sign(pk cipher.PubKey, secKey cipher.SecKey) error {
if err == nil {
se.Signatures[idx] = se.Entry.Signature(secKey)
}
return Ok
return err
}

// Signature gets Signature for a given PubKey from correct position
func (se *SignedEntry) Signature(pk cipher.PubKey) (cipher.Sig, error) {
idx, err := se.Index(pk)
if err != nil {
return cipher.Sig{}, Ok
return cipher.Sig{}, err
}
return se.Signatures[idx], nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (handshake settlementHandshake) Do(tm *Manager, tr Transport, timeout time.
func settlementInitiatorHandshake(public bool) settlementHandshake {
return func(tm *Manager, tr Transport) (*Entry, error) {
entry := &Entry{
ID: MakeTransportID(tr.Edges()[0], tr.Edges()[1], tr.Type()),
ID: MakeTransportID(tr.Edges()[0], tr.Edges()[1], tr.Type(), public),
EdgeKeys: tr.Edges(),
Type: tr.Type(),
Public: public,
Expand All @@ -43,7 +43,7 @@ func settlementInitiatorHandshake(public bool) settlementHandshake {
sEntry := NewSignedEntry(entry, tm.config.PubKey, tm.config.SecKey)
if err := validateSignedEntry(sEntry, tr, tm.config.PubKey); err != nil {

return nil, fmt.Errorf("settlementInitiatorHandshake NewSignedEntry: %s\n sEntry: %v\n", err, sEntry)
return nil, fmt.Errorf("settlementInitiatorHandshake NewSignedEntry: %s\n sEntry: %v", err, sEntry)
}

if err := json.NewEncoder(tr).Encode(sEntry); err != nil {
Expand Down
8 changes: 2 additions & 6 deletions pkg/transport/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Example_validateEntry() {
entry := NewEntry(pk1, pk2, "mock", true)
sEntry := NewSignedEntry(entry, pk1, sk1)
if err := validateSignedEntry(sEntry, tr, pk1); err != nil {
fmt.Println(Ok.Error())
fmt.Println(err.Error())
}

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

tpType := "mock"
entry := &Entry{
ID: MakeTransportID(mockEnv.pk1, mockEnv.pk2, tpType),
ID: MakeTransportID(mockEnv.pk1, mockEnv.pk2, tpType, true),
EdgeKeys: SortPubKeys(mockEnv.pk1, mockEnv.pk2),
Type: tpType,
Public: true,
Expand Down Expand Up @@ -313,12 +313,9 @@ func Example_verifySig() {
func Example_settlementInitiatorHandshake() {
mockEnv := newHsMockEnv()

// uid := MakeTransportID(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)
Expand All @@ -327,7 +324,6 @@ func Example_settlementInitiatorHandshake() {
errCh <- err
}
errCh <- nil
// resultCh <- hsResult{entry, err}
}()

go func() {
Expand Down
11 changes: 8 additions & 3 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,18 @@ func (tm *Manager) Serve(ctx context.Context) error {
return nil
}

// MakeTransportID generates uuid.UUID from pair of keys.
// MakeTransportID generates uuid.UUID from pair of keys + type + public
// Generated uuid is:
// - always the same for a given pair
// - GenTransportUUID(keyA,keyB) == GenTransportUUID(keyB, keyA)
func MakeTransportID(keyA, keyB cipher.PubKey, tpType string) uuid.UUID {
func MakeTransportID(keyA, keyB cipher.PubKey, tpType string, public bool) uuid.UUID {
keys := SortPubKeys(keyA, keyB)
return uuid.NewSHA1(uuid.UUID{}, append(append(keys[0][:], keys[1][:]...), []byte(tpType)...))
if public {
return uuid.NewSHA1(uuid.UUID{},
append(append(append(keys[0][:], keys[1][:]...), []byte(tpType)...), 1))
}
return uuid.NewSHA1(uuid.UUID{},
append(append(append(keys[0][:], keys[1][:]...), []byte(tpType)...), 0))
}

// SortPubKeys sorts keys so that least-significant comes first
Expand Down
27 changes: 14 additions & 13 deletions pkg/transport/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,42 +231,43 @@ func ExampleSortPubKeys() {
// SortPubKeys(keyA, keyB) == SortPubKeys(keyB, keyA)
}

// MakeTransportID(keyA,keyB, "type") == MakeTransportID(keyB, keyA, "type")
// GetTrasportUUID(keyA,keyB) is always the same for a given pair
// MakeTransportID(keyA, keyA) works for equal keys
// MakeTransportID(keyA,keyB, "type") != MakeTransportID(keyB, keyA, "type")
func ExampleMakeTransportID() {
keyA, _ := cipher.GenerateKeyPair()
keyB, _ := cipher.GenerateKeyPair()

uuidAB := MakeTransportID(keyA, keyB, "type")
uuidAB := MakeTransportID(keyA, keyB, "type", true)

for i := 0; i < 256; i++ {
if MakeTransportID(keyA, keyB, "type") != uuidAB {
fmt.Printf("uuid is unstable")
if MakeTransportID(keyA, keyB, "type", true) != uuidAB {
fmt.Println("uuid is unstable")
break
}
}
fmt.Printf("uuid is stable\n")

uuidBA := MakeTransportID(keyB, keyA, "type")
uuidBA := MakeTransportID(keyB, keyA, "type", true)
if uuidAB == uuidBA {
fmt.Printf("uuid is bidirectional\n")
fmt.Println("uuid is bidirectional")
} else {
fmt.Printf("keyA = %v\n keyB=%v\n uuidAB=%v\n uuidBA=%v\n", keyA, keyB, uuidAB, uuidBA)
}

_ = MakeTransportID(keyA, keyA, "type") // works for equal keys
fmt.Printf("works for equal keys\n")
_ = MakeTransportID(keyA, keyA, "type", true) // works for equal keys
fmt.Println("works for equal keys")

if MakeTransportID(keyA, keyB, "type") != MakeTransportID(keyA, keyB, "another_type") {
fmt.Printf("uuid is different for different types")
if MakeTransportID(keyA, keyB, "type", true) != MakeTransportID(keyA, keyB, "another_type", true) {
fmt.Println("uuid is different for different types")
}

if MakeTransportID(keyA, keyB, "type", true) != MakeTransportID(keyA, keyB, "type", false) {
fmt.Println("uuid is different for public and private transports")
}

// Output: uuid is stable
// uuid is bidirectional
// works for equal keys
// uuid is different for different types
// uuid is different for public and private transports
}

func ExampleManager_CreateTransport() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (f *MockFactory) Type() string {
// MockTransport is a transport that accepts custom writers and readers to use them in Read and Write
// operations
type MockTransport struct {
rw io.ReadWriteCloser
edges [2]cipher.PubKey
rw io.ReadWriteCloser
edges [2]cipher.PubKey
context context.Context
}

Expand Down

0 comments on commit 377470e

Please sign in to comment.