Peer: only add addresses once.
Juan Batiz-Benet committed
Sep 16, 2014 at 06:13 UTC
71e411e5387ac0b485130f4298138de7848e0f2a
2 files changed
+12
-1
peer/peer.go
+5
@@ -48,6 +48,11 @@ func (p *Peer) Key() u.Key {
48
49
// AddAddress adds the given Multiaddr address to Peer's addresses.
50
func (p *Peer) AddAddress(a *ma.Multiaddr) {
51
+ for _, addr := range p.Addresses {
52
+ if addr.Equal(a) {
53
+ return
54
+ }
55
+ }
56
p.Addresses = append(p.Addresses, a)
57
}
58
peer/peer_test.go
+7
-1
@@ -1,9 +1,10 @@
1
package peer
2
3
import (
4
+ "testing"
5
+
6
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
7
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
6
- "testing"
8
)
9
10
func TestNetAddress(t *testing.T) {
@@ -29,6 +30,11 @@ func TestNetAddress(t *testing.T) {
30
p := Peer{ID: ID(mh)}
31
p.AddAddress(tcp)
32
p.AddAddress(udp)
33
+ p.AddAddress(tcp)
34
+
35
+ if len(p.Addresses) == 3 {
36
+ t.Error("added same address twice")
37
+ }
38
39
tcp2 := p.NetAddress("tcp")
40
if tcp2 != tcp {