Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. `Manager.Remote` - now returns just `cipher.PubKey` (no error)
2. Linted
  • Loading branch information
ayuryshev committed Apr 2, 2019
1 parent f8882a4 commit 85a2a68
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 66 deletions.
4 changes: 2 additions & 2 deletions pkg/messaging/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type channel struct {
noise *noise.Noise
}

func (ch *channel) Edges() [2]cipher.PubKey {
return [2]cipher.PubKey{ch.link.Local(), ch.remotePK}
func (c *channel) Edges() [2]cipher.PubKey {
return [2]cipher.PubKey{c.link.Local(), c.remotePK}
}

func newChannel(initiator bool, secKey cipher.SecKey, remote cipher.PubKey, link *Link) (*channel, error) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ type TransportSummary struct {
}

func newTransportSummary(tm *transport.Manager, tp *transport.ManagedTransport, includeLogs bool) *TransportSummary {
remote, _ := tm.Remote(tp.Edges())
summary := TransportSummary{
ID: tp.ID,
Local: tm.Local(),
Remote: remote,
Remote: tm.Remote(tp.Edges()),
Type: tp.Type(),
}
if includeLogs {
Expand Down Expand Up @@ -180,8 +179,7 @@ func (r *RPC) Transports(in *TransportsIn, out *[]*TransportSummary) error {
return true
}
r.node.tm.WalkTransports(func(tp *transport.ManagedTransport) bool {
remote, _ := r.node.tm.Remote(tp.Edges())
if typeIncluded(tp.Type()) && pkIncluded(r.node.tm.Local(), remote) {
if typeIncluded(tp.Type()) && pkIncluded(r.node.tm.Local(), r.node.tm.Remote(tp.Edges())) {
*out = append(*out, newTransportSummary(r.node.tm, tp, in.ShowLogs))
}
return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/node/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestRPC(t *testing.T) {
executer := new(MockExecuter)
defer os.RemoveAll("chat")

pk1, _, tm1, tm2, errCh, err := transport.MockTransportManagers()
pk1, _, tm1, tm2, errCh, err := transport.MockTransportManagersPair()
require.NoError(t, err)
defer func() {
require.NoError(t, tm1.Close())
Expand Down
3 changes: 1 addition & 2 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ func (r *Router) advanceNoiseHandshake(addr *app.LoopAddr, noiseMsg []byte) (ni

func (r *Router) isSetupTransport(tr transport.Transport) bool {
for _, pk := range r.config.SetupNodes {
remote, _ := r.tm.Remote(tr.Edges())
if remote == pk {
if r.tm.Remote(tr.Edges()) == pk {
return true
}
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestRouterForwarding(t *testing.T) {
c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}
c3 := &transport.ManagerConfig{PubKey: pk3, SecKey: sk3, DiscoveryClient: client, LogStore: logStore}

f1, f2 := transport.NewMockFactory(pk1, pk2)
f3, f4 := transport.NewMockFactory(pk2, pk3)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f3, f4 := transport.NewMockFactoryPair(pk2, pk3)
f3.SetType("mock2")
f4.SetType("mock2")

Expand Down Expand Up @@ -144,7 +144,7 @@ func TestRouterApp(t *testing.T) {
c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}
c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}

f1, f2 := transport.NewMockFactory(pk1, pk2)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
m1, err := transport.NewManager(c1, f1)
require.NoError(t, err)

Expand Down Expand Up @@ -278,7 +278,7 @@ func TestRouterSetup(t *testing.T) {
c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}
c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}

f1, f2 := transport.NewMockFactory(pk1, pk2)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
m1, err := transport.NewManager(c1, f1)
require.NoError(t, err)

Expand Down Expand Up @@ -464,7 +464,7 @@ func TestRouterSetupLoop(t *testing.T) {
pk1, sk1 := cipher.GenerateKeyPair()
pk2, sk2 := cipher.GenerateKeyPair()

f1, f2 := transport.NewMockFactory(pk1, pk2)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f1.SetType("messaging")
f2.SetType("messaging")

Expand Down Expand Up @@ -567,7 +567,7 @@ func TestRouterCloseLoop(t *testing.T) {
pk2, sk2 := cipher.GenerateKeyPair()
pk3, _ := cipher.GenerateKeyPair()

f1, f2 := transport.NewMockFactory(pk1, pk2)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f1.SetType("messaging")

m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1)
Expand Down Expand Up @@ -655,7 +655,7 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) {
pk2, sk2 := cipher.GenerateKeyPair()
pk3, _ := cipher.GenerateKeyPair()

f1, f2 := transport.NewMockFactory(pk1, pk2)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f1.SetType("messaging")

m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1)
Expand Down Expand Up @@ -741,7 +741,7 @@ func TestRouterCloseLoopOnRouterClose(t *testing.T) {
pk2, sk2 := cipher.GenerateKeyPair()
pk3, _ := cipher.GenerateKeyPair()

f1, f2 := transport.NewMockFactory(pk1, pk2)
f1, f2 := transport.NewMockFactoryPair(pk1, pk2)
f1.SetType("messaging")

m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1)
Expand Down
11 changes: 6 additions & 5 deletions pkg/transport-discovery/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ var testPubKey, testSecKey = cipher.GenerateKeyPair()

func newTestEntry() *transport.Entry {
pk1, _ := cipher.GenerateKeyPair()
tpType := "messaging"
return &transport.Entry{
ID: transport.GetTransportUUID(pk1, testPubKey, tpType),
Edges: [2]cipher.PubKey{pk1, testPubKey},
Type: tpType,
entry := &transport.Entry{
ID: transport.GetTransportUUID(pk1, testPubKey, "messaging"),
Type: "messaging",
Public: true,
}
entry.SetEdges([2]cipher.PubKey{pk1, testPubKey})

return entry
}

func TestClientAuth(t *testing.T) {
Expand Down
10 changes: 8 additions & 2 deletions pkg/transport/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Entry struct {
ID uuid.UUID `json:"t_id"`

// Edges contains the public keys of the Transport's edge nodes (should only have 2 edges and the least-significant edge should come first).
edges [2]cipher.PubKey `json:"edges"`
EdgesKeys [2]cipher.PubKey `json:"edges"`

// Type represents the transport type.
Type string `json:"type"`
Expand All @@ -26,8 +26,14 @@ type Entry struct {
Public bool `json:"public"`
}

// Edges returns edges of Entry
func (e *Entry) Edges() [2]cipher.PubKey {
return e.edges
return e.EdgesKeys
}

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

// String implements stringer
Expand Down
24 changes: 8 additions & 16 deletions pkg/transport/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func (handshake settlementHandshake) Do(tm *Manager, tr Transport, timeout time.
func settlementInitiatorHandshake(id uuid.UUID, public bool) settlementHandshake {
return func(tm *Manager, tr Transport) (*Entry, error) {
entry := &Entry{
ID: id,
edges: tr.Edges(),
Type: tr.Type(),
Public: public,
ID: id,
EdgesKeys: tr.Edges(),
Type: tr.Type(),
Public: public,
}

newEntry := id == uuid.UUID{}
Expand All @@ -53,12 +53,8 @@ func settlementInitiatorHandshake(id uuid.UUID, public bool) settlementHandshake
return nil, fmt.Errorf("read: %s", err)
}

if remote, Ok := tm.Remote(tr.Edges()); Ok == nil {
if err := verifySig(sEntry, 1, remote); err != nil {
return nil, err
}
} else {
return nil, Ok
if err := verifySig(sEntry, 1, tm.Remote(tr.Edges())); err != nil {
return nil, err
}

if newEntry {
Expand All @@ -75,12 +71,8 @@ func settlementResponderHandshake(tm *Manager, tr Transport) (*Entry, error) {
return nil, fmt.Errorf("read: %s", err)
}

if remote, Ok := tm.Remote(tr.Edges()); Ok == nil {
if err := validateEntry(sEntry, tr, remote); err != nil {
return nil, err
}
} else {
return nil, Ok
if err := validateEntry(sEntry, tr, tm.Remote(tr.Edges())); err != nil {
return nil, err
}

sEntry.Signatures[1] = sEntry.Entry.Signature(tm.config.SecKey)
Expand Down
12 changes: 6 additions & 6 deletions pkg/transport/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestSettlementHandshakeExistingTransport(t *testing.T) {

entry := &Entry{
ID: GetTransportUUID(pk1, pk2, ""),
Edges: [2]cipher.PubKey{pk1, pk2},
edges: SortPubKeys(pk1, pk2),
Type: "mock",
Public: true,
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestValidateEntry(t *testing.T) {
pk2, sk2 := cipher.GenerateKeyPair()
tr := NewMockTransport(nil, pk1, pk2)

entry := &Entry{Type: "mock", Edges: [2]cipher.PubKey{pk2, pk1}}
entry := &Entry{Type: "mock", edges: SortPubKeys(pk2, pk1)}
tcs := []struct {
sEntry *SignedEntry
err string
Expand All @@ -179,11 +179,11 @@ func TestValidateEntry(t *testing.T) {
"invalid entry type",
},
{
&SignedEntry{Entry: &Entry{Type: "mock", Edges: [2]cipher.PubKey{pk1, pk2}}},
&SignedEntry{Entry: &Entry{Type: "mock", edges: SortPubKeys(pk2, pk1)}},
"invalid entry edges",
},
{
&SignedEntry{Entry: &Entry{Type: "mock", Edges: [2]cipher.PubKey{pk2, pk1}}},
&SignedEntry{Entry: &Entry{Type: "mock", edges: SortPubKeys(pk2, pk1)}},
"invalid entry signature",
},
{
Expand All @@ -198,12 +198,12 @@ func TestValidateEntry(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.err, func(t *testing.T) {
err := validateEntry(tc.sEntry, tr)
err := validateEntry(tc.sEntry, tr, pk2)
require.Error(t, err)
assert.Equal(t, tc.err, err.Error())
})
}

sEntry := &SignedEntry{Entry: entry, Signatures: [2]cipher.Sig{entry.Signature(sk2)}}
require.NoError(t, validateEntry(sEntry, tr))
require.NoError(t, validateEntry(sEntry, tr, pk2))
}
28 changes: 12 additions & 16 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,31 @@ func (tm *Manager) ReconnectTransports(ctx context.Context) {
}
}

// Local returns Manager.config.PubKey
func (tm *Manager) Local() cipher.PubKey {
return tm.config.PubKey
}

func (tm *Manager) Remote(edges [2]cipher.PubKey) (cipher.PubKey, error) {
// Remote returns the key from the edges that is not equal to Manager.config.PubKey
// in case when both edges are different - returns empty cipher.PubKey{}
func (tm *Manager) Remote(edges [2]cipher.PubKey) cipher.PubKey {
if tm.config.PubKey == edges[0] {
return edges[1], nil
return edges[1]
}
if tm.config.PubKey == edges[1] {
return edges[0], nil
return edges[0]
}
return cipher.PubKey{}, errors.New("Edges does not belongs to this Transport")
return cipher.PubKey{}
}

// CreateDefaultTransports created transports to DefaultNodes if they don't exist.
func (tm *Manager) CreateDefaultTransports(ctx context.Context) {
for _, pk := range tm.config.DefaultNodes {
exist := false
tm.WalkTransports(func(tr *ManagedTransport) bool {
if remote, Ok := tm.Remote(tr.Edges()); Ok == nil {
if remote == pk {
exist = true
return false
}
if tm.Remote(tr.Edges()) == pk {
exist = true
return false
}
return true
})
Expand Down Expand Up @@ -227,9 +228,8 @@ func SortPubKeys(keyA, keyB cipher.PubKey) [2]cipher.PubKey {
if keyA[i] != keyB[i] {
if keyA[i] < keyB[i] {
return [2]cipher.PubKey{keyA, keyB}
} else {
return [2]cipher.PubKey{keyB, keyA}
}
return [2]cipher.PubKey{keyB, keyA}
}
}
return [2]cipher.PubKey{keyA, keyB}
Expand Down Expand Up @@ -363,11 +363,7 @@ func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (*Manag
return nil, err
}

remote, err := tm.Remote(tr.Edges())
if err != nil {
return nil, err
}
tm.Logger.Infof("Accepted new transport with type %s from %s. ID: %s", factory.Type(), remote, entry.ID)
tm.Logger.Infof("Accepted new transport with type %s from %s. ID: %s", factory.Type(), tm.Remote(tr.Edges()), entry.ID)
managedTr := newManagedTransport(entry.ID, tr, entry.Public)
tm.mu.Lock()

Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func ExampleGetTransportUUID() {
// uuid is different for different types
}

func ExampleManagerCreateTransport() {
func ExampleManager_CreateTransport() {
// Repetition is required here to guarantee that correctness does not depends on order of edges
for i := 0; i < 256; i++ {
pkB, mgrA, err := MockTransportManager()
Expand Down
1 change: 1 addition & 0 deletions pkg/transport/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (m *MockTransport) Close() error {
return m.rw.Close()
}

// Edges returns edges of MockTransport
func (m *MockTransport) Edges() [2]cipher.PubKey {
return m.edges
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/tcp_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type TCPTransport struct {
// rpk cipher.PubKey
}

// Local returns the TCPTransport edges.
// Edges returns the TCPTransport edges.
func (tr *TCPTransport) Edges() [2]cipher.PubKey {
return tr.edges
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/tcp_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestTCPFactory(t *testing.T) {
tr, err := f2.Dial(context.TODO(), pk1)
require.NoError(t, err)
assert.Equal(t, "tcp", tr.Type())
assert.Equal(t, pk2, tr.Local())
assert.Equal(t, pk1, tr.Remote())
// assert.Equal(t, pk2, tr.Local())
// assert.Equal(t, pk1, tr.Remote())

buf := make([]byte, 3)
_, err = tr.Read(buf)
Expand Down

0 comments on commit 85a2a68

Please sign in to comment.