Browse Source

NetAddress.Same checks ID or DialString

pull/1105/head
Ethan Buchman 7 years ago
parent
commit
68237911ba
2 changed files with 17 additions and 6 deletions
  1. +14
    -1
      p2p/netaddress.go
  2. +3
    -5
      p2p/switch.go

+ 14
- 1
p2p/netaddress.go View File

@ -127,12 +127,25 @@ func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
return na return na
} }
// Equals reports whether na and other are the same addresses.
// Equals reports whether na and other are the same addresses,
// including their ID, IP, and Port.
func (na *NetAddress) Equals(other interface{}) bool { func (na *NetAddress) Equals(other interface{}) bool {
if o, ok := other.(*NetAddress); ok { if o, ok := other.(*NetAddress); ok {
return na.String() == o.String() return na.String() == o.String()
} }
return false
}
// Same returns true is na has the same non-empty ID or DialString as other.
func (na *NetAddress) Same(other interface{}) bool {
if o, ok := other.(*NetAddress); ok {
if na.DialString() == o.DialString() {
return true
}
if na.ID != "" && na.ID == o.ID {
return true
}
}
return false return false
} }


+ 3
- 5
p2p/switch.go View File

@ -318,18 +318,16 @@ func (sw *Switch) IsDialing(id ID) bool {
// DialPeersAsync dials a list of peers asynchronously in random order (optionally, making them persistent). // DialPeersAsync dials a list of peers asynchronously in random order (optionally, making them persistent).
func (sw *Switch) DialPeersAsync(addrBook *AddrBook, peers []string, persistent bool) error { func (sw *Switch) DialPeersAsync(addrBook *AddrBook, peers []string, persistent bool) error {
netAddrs, errs := NewNetAddressStrings(peers) netAddrs, errs := NewNetAddressStrings(peers)
// TODO: IDs
for _, err := range errs { for _, err := range errs {
sw.Logger.Error("Error in peer's address", "err", err) sw.Logger.Error("Error in peer's address", "err", err)
} }
if addrBook != nil { if addrBook != nil {
// add peers to `addrBook` // add peers to `addrBook`
ourAddrS := sw.nodeInfo.ListenAddr
ourAddr, _ := NewNetAddressString(ourAddrS)
ourAddr := sw.nodeInfo.NetAddress()
for _, netAddr := range netAddrs { for _, netAddr := range netAddrs {
// do not add ourselves
if netAddr.Equals(ourAddr) {
// do not add our address or ID
if netAddr.Same(ourAddr) {
continue continue
} }
addrBook.AddAddress(netAddr, ourAddr) addrBook.AddAddress(netAddr, ourAddr)


Loading…
Cancel
Save