diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 5379bed4b..1838cbc2f 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -5,8 +5,6 @@ import ( "io" "time" - identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" - logging "github.com/ipfs/go-log" goprocess "github.com/jbenet/goprocess" goprocessctx "github.com/jbenet/goprocess/context" @@ -15,6 +13,7 @@ import ( peer "github.com/libp2p/go-libp2p-peer" pstore "github.com/libp2p/go-libp2p-peerstore" protocol "github.com/libp2p/go-libp2p-protocol" + identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" ma "github.com/multiformats/go-multiaddr" madns "github.com/multiformats/go-multiaddr-dns" msmux "github.com/multiformats/go-multistream" diff --git a/p2p/protocol/identify/obsaddr.go b/p2p/protocol/identify/obsaddr.go index 61ccc0bed..40c6f78df 100644 --- a/p2p/protocol/identify/obsaddr.go +++ b/p2p/protocol/identify/obsaddr.go @@ -62,7 +62,6 @@ func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) { } now := time.Now() - filteredAddrMap := make(map[string][]*ObservedAddr) for local, observedAddrs := range oas.addrs { filteredAddrs := make([]*ObservedAddr, 0, len(observedAddrs)) for _, a := range observedAddrs { @@ -74,9 +73,8 @@ func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) { } } } - filteredAddrMap[local] = filteredAddrs + oas.addrs[local] = filteredAddrs } - oas.addrs = filteredAddrMap return addrs } @@ -95,36 +93,28 @@ func (oas *ObservedAddrSet) Add(observed, local, observer ma.Multiaddr, now := time.Now() observerString := observerGroup(observer) localString := local.String() - observedAddr := &ObservedAddr{ - Addr: observed, - SeenBy: map[string]observation{ - observerString: { - seenTime: now, - connDirection: direction, - }, - }, - LastSeen: now, + ob := observation{ + seenTime: now, + connDirection: direction, } - observedAddrs, found := oas.addrs[localString] - // map key not exist yet, init with new values - if !found { - oas.addrs[localString] = []*ObservedAddr{observedAddr} - return - } + observedAddrs := oas.addrs[localString] // check if observed address seen yet, if so, update it for i, previousObserved := range observedAddrs { if previousObserved.Addr.Equal(observed) { - observedAddrs[i].SeenBy[observerString] = observation{ - seenTime: now, - connDirection: direction, - } + observedAddrs[i].SeenBy[observerString] = ob observedAddrs[i].LastSeen = now return } } // observed address not seen yet, append it - oas.addrs[localString] = append(oas.addrs[localString], observedAddr) + oas.addrs[localString] = append(oas.addrs[localString], &ObservedAddr{ + Addr: observed, + SeenBy: map[string]observation{ + observerString: ob, + }, + LastSeen: now, + }) } // observerGroup is a function that determines what part of