-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multi: Add getaddrv2 and addrv2. #2627
base: master
Are you sure you want to change the base?
Changes from 1 commit
650a935
77e2118
2d13ac7
a32aa05
ddf3d7d
27215a0
404ec69
c8b98b1
3632c29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -684,10 +684,11 @@ func (a *AddrManager) NeedMoreAddresses() bool { | |||||
return a.numAddresses() < needAddressThreshold | ||||||
} | ||||||
|
||||||
// AddressCache returns a randomized subset of all known addresses. | ||||||
// AddressCache returns a randomized subset of all addresses known to the | ||||||
// address manager with a network address type matching the provided type flags. | ||||||
// | ||||||
// This function is safe for concurrent access. | ||||||
func (a *AddrManager) AddressCache() []*NetAddress { | ||||||
func (a *AddrManager) AddressCache(supportedNetAddressType NetAddressTypeFilter) []*NetAddress { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also a breaking change to the public API which needs a major module version bump. I'll stop calling these out from now on since I mentioned it in my previous review and once the major is bumped, the API can be changed at will. |
||||||
a.mtx.Lock() | ||||||
defer a.mtx.Unlock() | ||||||
|
||||||
|
@@ -700,6 +701,10 @@ func (a *AddrManager) AddressCache() []*NetAddress { | |||||
allAddr := make([]*NetAddress, 0, addrLen) | ||||||
// Iteration order is undefined here, but we randomize it anyway. | ||||||
for _, v := range a.addrIndex { | ||||||
// Skip address types not requested by the caller. | ||||||
if !supportedNetAddressType(v.na.Type) { | ||||||
continue | ||||||
} | ||||||
// Skip low quality addresses. | ||||||
if v.isBad() { | ||||||
continue | ||||||
|
@@ -1220,14 +1225,18 @@ func getReachabilityFrom(localAddr, remoteAddr *NetAddress) NetAddressReach { | |||||
// for the given remote address. | ||||||
// | ||||||
// This function is safe for concurrent access. | ||||||
func (a *AddrManager) GetBestLocalAddress(remoteAddr *NetAddress) *NetAddress { | ||||||
func (a *AddrManager) GetBestLocalAddress(remoteAddr *NetAddress, supportedNetAddressType NetAddressTypeFilter) *NetAddress { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
a.lamtx.Lock() | ||||||
defer a.lamtx.Unlock() | ||||||
|
||||||
bestreach := Default | ||||||
var bestscore AddressPriority | ||||||
var bestAddress *NetAddress | ||||||
for _, la := range a.localAddresses { | ||||||
// Only return addresses matching the type flags provided by the caller. | ||||||
if !supportedNetAddressType(la.na.Type) { | ||||||
continue | ||||||
} | ||||||
reach := getReachabilityFrom(la.na, remoteAddr) | ||||||
if reach > bestreach || | ||||||
(reach == bestreach && la.score > bestscore) { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -444,9 +444,8 @@ func (ps *peerState) ResolveLocalAddress(netType addrmgr.NetAddressType, addrMgr | |
|
||
// Add a local address if the network address is a probable external | ||
// endpoint of the listener. | ||
lNa := wire.NewNetAddressIPPort(listenerIP, uint16(port), services) | ||
lNet := addrmgr.IPv4Address | ||
if lNa.IP.To4() == nil { | ||
if listenerIP.To4() == nil { | ||
lNet = addrmgr.IPv6Address | ||
} | ||
|
||
|
@@ -705,6 +704,25 @@ func hasServices(advertised, desired wire.ServiceFlag) bool { | |
return advertised&desired == desired | ||
} | ||
|
||
// isSupportedNetAddressTypeV1 returns whether the provided address manager | ||
// network address type is supported by the addr wire message. | ||
func isSupportedNetAddressTypeV1(netAddressType addrmgr.NetAddressType) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to comments on the previous commit, I would suggest a consistent param name |
||
switch netAddressType { | ||
case addrmgr.IPv4Address: | ||
case addrmgr.IPv6Address: | ||
case addrmgr.TORv2Address: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// getNetAddressTypeFilter returns a function that determines whether a | ||
// specific address manager network address type is supported by the | ||
// provided protocol version. | ||
func getNetAddressTypeFilter(pver uint32) addrmgr.NetAddressTypeFilter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per effective Go, funcs really shouldn't be named with As an aside, the comment doesn't match what it's really doing either. It claims that it determines whether a specific address type is supported, but what it's really doing is returning a filter for the address types supported by the provided protocol version. |
||
return isSupportedNetAddressTypeV1 | ||
} | ||
|
||
// OnVersion is invoked when a peer receives a version wire message and is used | ||
// to negotiate the protocol version details as well as kick start the | ||
// communications. | ||
|
@@ -720,6 +738,7 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) { | |
// it is updated regardless in the case a new minimum protocol version is | ||
// enforced and the remote node has not upgraded yet. | ||
isInbound := sp.Inbound() | ||
msgProtocolVersion := uint32(msg.ProtocolVersion) | ||
remoteAddr := wireToAddrmgrNetAddress(sp.NA()) | ||
addrManager := sp.server.addrManager | ||
if !cfg.SimNet && !cfg.RegNet && !isInbound { | ||
|
@@ -730,7 +749,7 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) { | |
} | ||
|
||
// Reject peers that have a protocol version that is too old. | ||
if msg.ProtocolVersion < int32(wire.SendHeadersVersion) { | ||
if msgProtocolVersion < wire.SendHeadersVersion { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is changing the semantics improperly and could lead to a bunch of undesirable things. Currently, a negative protocol version will be rejected due to this check, but with this change, negative protocol versions would become large positive versions and thus improperly pass this check. This must be reverted. The following code demonstrates: package main
import "fmt"
func main() {
var pver int32 = -1
var testPver uint32 = 3
if pver < int32(testPver) {
fmt.Println("original too low as expected")
}
if uint32(pver) < testPver {
fmt.Println("notice the modified version does not see it as too low")
} else {
fmt.Println("treated as ok even though it's not")
}
} Output:
|
||
srvrLog.Debugf("Rejecting peer %s with protocol version %d prior to "+ | ||
"the required version %d", sp.Peer, msg.ProtocolVersion, | ||
wire.SendHeadersVersion) | ||
|
@@ -760,7 +779,8 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) { | |
// known tip. | ||
if !cfg.DisableListen && sp.server.syncManager.IsCurrent() { | ||
// Get address that best matches. | ||
lna := addrManager.GetBestLocalAddress(remoteAddr) | ||
naTypeFilter := getNetAddressTypeFilter(msgProtocolVersion) | ||
lna := addrManager.GetBestLocalAddress(remoteAddr, naTypeFilter) | ||
if lna.IsRoutable() { | ||
// Filter addresses the peer already knows about. | ||
addresses := []*addrmgr.NetAddress{lna} | ||
|
@@ -1387,7 +1407,9 @@ func (sp *serverPeer) OnGetAddr(p *peer.Peer, msg *wire.MsgGetAddr) { | |
sp.addrsSent = true | ||
|
||
// Get the current known addresses from the address manager. | ||
addrCache := sp.server.addrManager.AddressCache() | ||
pver := sp.ProtocolVersion() | ||
naTypeFilter := getNetAddressTypeFilter(pver) | ||
addrCache := sp.server.addrManager.AddressCache(naTypeFilter) | ||
|
||
// Push the addresses. | ||
sp.pushAddrMsg(addrCache) | ||
|
@@ -3781,6 +3803,9 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB, | |
if !cfg.SimNet && !cfg.RegNet && len(cfg.ConnectPeers) == 0 { | ||
newAddressFunc = func() (net.Addr, error) { | ||
for tries := 0; tries < 100; tries++ { | ||
// Note that this does not filter by address type. Unsupported | ||
// network address types should be pruned from the address | ||
// manager's internal storage prior to calling this function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
addr := s.addrManager.GetAddress() | ||
if addr == nil { | ||
break | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.