don't track relay addrs, use the peerstore

This commit is contained in:
vyzo 2019-04-20 00:04:26 +03:00
parent bd22c49b0d
commit f4f924e1d4
3 changed files with 23 additions and 31 deletions

View File

@ -4,7 +4,6 @@ import (
"encoding/binary"
circuit "github.com/libp2p/go-libp2p-circuit"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
dns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr-net"
@ -12,10 +11,10 @@ import (
// This function cleans up a relay's address set to remove private addresses and curtail
// addrsplosion.
func cleanupAddressSet(pi pstore.PeerInfo) pstore.PeerInfo {
func cleanupAddressSet(addrs []ma.Multiaddr) []ma.Multiaddr {
var public, private []ma.Multiaddr
for _, a := range pi.Addrs {
for _, a := range addrs {
if isRelayAddr(a) {
continue
}
@ -32,11 +31,10 @@ func cleanupAddressSet(pi pstore.PeerInfo) pstore.PeerInfo {
}
if !hasAddrsplosion(public) {
return pstore.PeerInfo{ID: pi.ID, Addrs: public}
return public
}
addrs := sanitizeAddrsplodedSet(public, private)
return pstore.PeerInfo{ID: pi.ID, Addrs: addrs}
return sanitizeAddrsplodedSet(public, private)
}
func isRelayAddr(a ma.Multiaddr) bool {

View File

@ -1,10 +1,8 @@
package relay
import (
"fmt"
"testing"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
_ "github.com/multiformats/go-multiaddr-dns"
)
@ -24,9 +22,8 @@ func TestCleanupAddrs(t *testing.T) {
"/dnsaddr/somedomain.com/tcp/4002/ws",
)
pi := cleanupAddressSet(pstore.PeerInfo{Addrs: addrs})
if !sameAddrs(clean, pi.Addrs) {
fmt.Println(pi.Addrs)
r := cleanupAddressSet(addrs)
if !sameAddrs(clean, r) {
t.Fatal("cleaned up set doesn't match expected")
}
@ -43,8 +40,8 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/tcp/4001",
"/ip4/1.2.3.4/udp/4002/quic",
)
pi = cleanupAddressSet(pstore.PeerInfo{Addrs: addrs})
if !sameAddrs(clean, pi.Addrs) {
r = cleanupAddressSet(addrs)
if !sameAddrs(clean, r) {
t.Fatal("cleaned up set doesn't match expected")
}
@ -60,8 +57,8 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/tcp/4001",
"/ip4/1.2.3.4/udp/4002/quic",
)
pi = cleanupAddressSet(pstore.PeerInfo{Addrs: addrs})
if !sameAddrs(clean, pi.Addrs) {
r = cleanupAddressSet(addrs)
if !sameAddrs(clean, r) {
t.Fatal("cleaned up set doesn't match expected")
}
@ -76,8 +73,8 @@ func TestCleanupAddrs(t *testing.T) {
clean = makeAddrList(
"/ip4/1.2.3.4/tcp/12345",
)
pi = cleanupAddressSet(pstore.PeerInfo{Addrs: addrs})
if !sameAddrs(clean, pi.Addrs) {
r = cleanupAddressSet(addrs)
if !sameAddrs(clean, r) {
t.Fatal("cleaned up set doesn't match expected")
}
@ -87,8 +84,8 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/udp/4001/quic",
)
clean = addrs
pi = cleanupAddressSet(pstore.PeerInfo{Addrs: addrs})
if !sameAddrs(clean, pi.Addrs) {
r = cleanupAddressSet(addrs)
if !sameAddrs(clean, r) {
t.Fatal("cleaned up set doesn't match expected")
}
}

View File

@ -41,7 +41,7 @@ type AutoRelay struct {
disconnect chan struct{}
mx sync.Mutex
relays map[peer.ID]pstore.PeerInfo
relays map[peer.ID]struct{}
addrs []ma.Multiaddr
}
@ -51,7 +51,7 @@ func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discover
discover: discover,
router: router,
addrsF: bhost.AddrsFactory,
relays: make(map[peer.ID]pstore.PeerInfo),
relays: make(map[peer.ID]struct{}),
disconnect: make(chan struct{}, 1),
}
ar.autonat = autonat.NewAutoNAT(ctx, bhost, ar.baseAddrs)
@ -179,7 +179,7 @@ again:
log.Debugf("connected to relay %s", pi.ID)
ar.mx.Lock()
ar.relays[pi.ID] = pi
ar.relays[pi.ID] = struct{}{}
haveRelays++
ar.mx.Unlock()
@ -261,12 +261,7 @@ func (ar *AutoRelay) selectRelays(ctx context.Context, pis []pstore.PeerInfo, co
case qr := <-resultCh:
rcount++
if qr.err == nil {
pi := cleanupAddressSet(qr.pi)
if len(pi.Addrs) > 0 {
result = append(result, pi)
} else {
log.Debugf("ignoring relay peer %s: cleaned up address set is empty", pi.ID)
}
result = append(result, qr.pi)
}
case <-qctx.Done():
@ -308,13 +303,15 @@ func (ar *AutoRelay) doUpdateAddrs() {
}
// add relay specific addrs to the list
for _, pi := range ar.relays {
circuit, err := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", pi.ID.Pretty()))
for p := range ar.relays {
addrs := cleanupAddressSet(ar.host.Peerstore().Addrs(p))
circuit, err := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", p.Pretty()))
if err != nil {
panic(err)
}
for _, addr := range pi.Addrs {
for _, addr := range addrs {
pub := addr.Encapsulate(circuit)
raddrs = append(raddrs, pub)
}