From 2cf5659da2e9aebdb59132ac37ec28e1cbbd7e0f Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 18:46:28 -0800 Subject: [PATCH 1/2] gx: update go-libp2p-peerstore --- package.json | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 3f49be09af..dbe36b564a 100644 --- a/package.json +++ b/package.json @@ -139,9 +139,9 @@ }, { "author": "whyrusleeping", - "hash": "QmYijbtjCxFEjSXaudaQAUz3LN5VKLssm8WCUsRoqzXmQR", + "hash": "QmQoXyRXtorcAtWwnud9HBod3vjPC8vTu1gJnsg9PLwuiT", "name": "go-libp2p-peerstore", - "version": "1.4.10" + "version": "1.4.11" }, { "author": "whyrusleeping", @@ -187,15 +187,15 @@ }, { "author": "whyrusleeping", - "hash": "QmU4vCDZTPLDqSDKguWbHCiUe46mZUtmM2g2suBZ9NE8ko", + "hash": "Qmegn1aFLQmbCg4xurYgLug9TJ3rTVFjJ3L5fD3m9m4qMf", "name": "go-libp2p-net", - "version": "2.0.3" + "version": "2.0.4" }, { "author": "whyrusleeping", - "hash": "QmaL2WYJGbWKqHoLujoi9GQ5jj4JVFrBqHUBWmEYzJPVWT", + "hash": "QmazrTQKkjFcajz55duY9eCePfjv1mVWH3xJbn5bu7qUXQ", "name": "go-libp2p-metrics", - "version": "2.0.2" + "version": "2.0.3" }, { "author": "whyrusleeping", @@ -205,15 +205,15 @@ }, { "author": "whyrusleeping", - "hash": "QmP46LGWhzVZTMmt5akNNLfoV8qL4h5wTwmzQxLyDafggd", + "hash": "QmbEk4hyXjohArLiff8inoGsoywzfPD71SqD4qHZBiWQyr", "name": "go-libp2p-host", - "version": "2.1.3" + "version": "2.1.4" }, { "author": "whyrusleeping", - "hash": "QmUhvp4VoQ9cKDVLqAxciEKdm8ymBx2Syx4C1Tv6SmSTPa", + "hash": "QmaNqYUXoM5x2gUVLKSdXBDBrw4maNydw1rpxbzPPuSQ8b", "name": "go-libp2p-swarm", - "version": "2.1.2" + "version": "2.1.3" }, { "author": "whyrusleeping", @@ -223,15 +223,15 @@ }, { "author": "whyrusleeping", - "hash": "QmZTcPxK6VqrwY94JpKZPvEqAZ6tEr1rLrpcqJbbRZbg2V", + "hash": "QmR84wjmVyNUXQuL2s9rKr5QcL7CLUXjhQTFgosUh22UEZ", "name": "go-libp2p-netutil", - "version": "0.3.6" + "version": "0.3.7" }, { "author": "whyrusleeping", - "hash": "QmYmhgAcvmDGXct1qBvc1kz9BxQSit1XBrTeiGZp2FvRyn", + "hash": "QmP4BoRH7qHbyVAjpXqZqXm3X2sMWokgACF9m5RXBWLDkq", "name": "go-libp2p-blankhost", - "version": "0.2.3" + "version": "0.2.4" }, { "author": "whyrusleeping", @@ -265,9 +265,9 @@ }, { "author": "vyzo", - "hash": "QmVqNgx6RByRcnLVsr9spbo5ScomJyDZrb9Gz4XMWkYB7Z", + "hash": "QmPLBz772MAef586ooAEVqGjYsUiGQPUSJHcvWtD9BKhaF", "name": "go-libp2p-circuit", - "version": "2.0.7" + "version": "2.0.8" }, { "author": "lgierth", @@ -277,9 +277,9 @@ }, { "author": "why", - "hash": "QmSAJm4QdTJ3EGF2cvgNcQyXTEbxqWSW1x4kCVV1aJQUQr", + "hash": "QmayGe6p6EJT8sbdmsh5eyVjp9u8KvFwkMacKwx2MDWzN8", "name": "go-libp2p-interface-connmgr", - "version": "0.0.4" + "version": "0.0.5" } ], "gxVersion": "0.4.0", From 544094183fccf4ffd04aacc48f6fb64c7541d9dd Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 19:06:17 -0800 Subject: [PATCH 2/2] fix peerstore apocalypse redux This commit prevents us from repeatedly extending the lifetimes of all observed addresses if a peer keeps on reconnecting. It also fixes two race conditions: 1. We may end up processing a disconnect after a re-connect and may accidentally give the addresses associated with that peer a RecentlyConnectedAddrTTL instead of a ConnectedAddrTTL. 2. We may end up processing a connect after the associated disconnect storing the associated peer addresses indefinitely. --- p2p/protocol/identify/id.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 83749778d2..cf506c8d49 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -50,6 +50,8 @@ type IDService struct { currid map[inet.Conn]chan struct{} currmu sync.RWMutex + addrMu sync.Mutex + // our own observed addresses. // TODO: instead of expiring, remove these when we disconnect observedAddrs ObservedAddrSet @@ -220,9 +222,17 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) { lmaddrs = append(lmaddrs, c.RemoteMultiaddr()) } - // update our peerstore with the addresses. here, we SET the addresses, clearing old ones. - // We are receiving from the peer itself. this is current address ground truth. - ids.Host.Peerstore().SetAddrs(p, lmaddrs, pstore.ConnectedAddrTTL) + // Extend the TTLs on the known (probably) good addresses. + // Taking the lock ensures that we don't concurrently process a disconnect. + ids.addrMu.Lock() + switch ids.Host.Network().Connectedness(p) { + case inet.Connected: + ids.Host.Peerstore().AddAddrs(p, lmaddrs, pstore.ConnectedAddrTTL) + default: + ids.Host.Peerstore().AddAddrs(p, lmaddrs, pstore.RecentlyConnectedAddrTTL) + } + ids.addrMu.Unlock() + log.Debugf("%s received listen addrs for %s: %s", c.LocalPeer(), c.RemotePeer(), lmaddrs) // get protocol versions @@ -449,9 +459,14 @@ func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) { func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) { // undo the setting of addresses to peer.ConnectedAddrTTL we did ids := nn.IDService() - ps := ids.Host.Peerstore() - addrs := ps.Addrs(v.RemotePeer()) - ps.SetAddrs(v.RemotePeer(), addrs, pstore.RecentlyConnectedAddrTTL) + ids.addrMu.Lock() + defer ids.addrMu.Unlock() + + if ids.Host.Network().Connectedness(v.RemotePeer()) != inet.Connected { + // Last disconnect. + ps := ids.Host.Peerstore() + ps.UpdateAddrs(v.RemotePeer(), pstore.ConnectedAddrTTL, pstore.RecentlyConnectedAddrTTL) + } } func (nn *netNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}