From 377470e816f6da9d9b719d213b6fdf0de7e3d7e6 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Thu, 4 Apr 2019 19:31:09 +0300 Subject: [PATCH] Changes: 1. MakeTransportID - different IDs for public and private transports --- pkg/messaging/channel.go | 1 - pkg/messaging/channel_test.go | 2 +- pkg/node/rpc_client.go | 4 +-- pkg/route-finder/client/mock.go | 4 +-- pkg/transport-discovery/client/client_test.go | 2 +- pkg/transport/discovery_test.go | 4 +-- pkg/transport/entry.go | 8 +++--- pkg/transport/handshake.go | 4 +-- pkg/transport/handshake_test.go | 8 ++---- pkg/transport/manager.go | 11 +++++--- pkg/transport/manager_test.go | 27 ++++++++++--------- pkg/transport/mock.go | 4 +-- 12 files changed, 40 insertions(+), 39 deletions(-) diff --git a/pkg/messaging/channel.go b/pkg/messaging/channel.go index 3d4c28353..4a640b8ef 100644 --- a/pkg/messaging/channel.go +++ b/pkg/messaging/channel.go @@ -128,7 +128,6 @@ func (c *channel) Close() error { return nil } - func (c *channel) SetDeadline(t time.Time) error { c.deadline = t return nil diff --git a/pkg/messaging/channel_test.go b/pkg/messaging/channel_test.go index c519f1816..aa117408a 100644 --- a/pkg/messaging/channel_test.go +++ b/pkg/messaging/channel_test.go @@ -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, } diff --git a/pkg/node/rpc_client.go b/pkg/node/rpc_client.go index 996c4dd5f..385ce5c9e 100644 --- a/pkg/node/rpc_client.go +++ b/pkg/node/rpc_client.go @@ -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)], @@ -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, diff --git a/pkg/route-finder/client/mock.go b/pkg/route-finder/client/mock.go index 83d09c0bf..57914181e 100644 --- a/pkg/route-finder/client/mock.go +++ b/pkg/route-finder/client/mock.go @@ -33,7 +33,7 @@ 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{ @@ -41,7 +41,7 @@ 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), }, }, }, nil diff --git a/pkg/transport-discovery/client/client_test.go b/pkg/transport-discovery/client/client_test.go index f1f67b9d8..a2529961f 100644 --- a/pkg/transport-discovery/client/client_test.go +++ b/pkg/transport-discovery/client/client_test.go @@ -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, } diff --git a/pkg/transport/discovery_test.go b/pkg/transport/discovery_test.go index dc16337d0..db0126143 100644 --- a/pkg/transport/discovery_test.go +++ b/pkg/transport/discovery_test.go @@ -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 { @@ -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 diff --git a/pkg/transport/entry.go b/pkg/transport/entry.go index 24af3de75..bfc61799b 100644 --- a/pkg/transport/entry.go +++ b/pkg/transport/entry.go @@ -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, @@ -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]) } @@ -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 } diff --git a/pkg/transport/handshake.go b/pkg/transport/handshake.go index 6d238484f..35b8b9231 100644 --- a/pkg/transport/handshake.go +++ b/pkg/transport/handshake.go @@ -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, @@ -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 { diff --git a/pkg/transport/handshake_test.go b/pkg/transport/handshake_test.go index 95c75f105..f9a9e1b76 100644 --- a/pkg/transport/handshake_test.go +++ b/pkg/transport/handshake_test.go @@ -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 @@ -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, @@ -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) @@ -327,7 +324,6 @@ func Example_settlementInitiatorHandshake() { errCh <- err } errCh <- nil - // resultCh <- hsResult{entry, err} }() go func() { diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index e3ae736b2..3d532db5c 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -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 diff --git a/pkg/transport/manager_test.go b/pkg/transport/manager_test.go index 66974afac..4a4abd7ae 100644 --- a/pkg/transport/manager_test.go +++ b/pkg/transport/manager_test.go @@ -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() { diff --git a/pkg/transport/mock.go b/pkg/transport/mock.go index af2d5f052..6098223aa 100644 --- a/pkg/transport/mock.go +++ b/pkg/transport/mock.go @@ -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 }