Skip to content
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

server: Update peer attempt timestamp before dialing. #3014

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,6 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) {
remoteAddr := wireToAddrmgrNetAddress(sp.NA())
addrManager := sp.server.addrManager
if !cfg.SimNet && !cfg.RegNet && !isInbound {
// Be sure the address exists in the address manager.
addrManager.AddAddresses([]*addrmgr.NetAddress{remoteAddr},
remoteAddr)

err := addrManager.SetServices(remoteAddr, msg.Services)
if err != nil {
srvrLog.Errorf("Setting services for address failed: %v", err)
Expand Down Expand Up @@ -1512,6 +1508,35 @@ func randomUint16Number(max uint16) uint16 {
}
}

// attemptDcrdDial is a wrapper function around dcrdDial which adds and marks
// the remote peer as attempted in the address manager.
func (s *server) attemptDcrdDial(ctx context.Context, network, addr string) (net.Conn, error) {
davecgh marked this conversation as resolved.
Show resolved Hide resolved
if !cfg.SimNet && !cfg.RegNet {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return nil, err
}
remoteAddr, err := s.addrManager.HostToNetAddress(host, uint16(port), 0)
if err != nil {
return nil, err
}
// Be sure the address exists in the address manager.
s.addrManager.AddAddresses([]*addrmgr.NetAddress{remoteAddr},
remoteAddr)

err = s.addrManager.Attempt(remoteAddr)
if err != nil {
srvrLog.Errorf("Marking address as attempted failed: %v", err)
}
}

return dcrdDial(ctx, network, addr)
}

// AddRebroadcastInventory adds 'iv' to the list of inventories to be
// rebroadcasted at random intervals until they show up in a block.
func (s *server) AddRebroadcastInventory(iv *wire.InvVect, data interface{}) {
Expand Down Expand Up @@ -1837,7 +1862,7 @@ func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) {
remoteAddr := wireToAddrmgrNetAddress(sp.NA())
err := s.addrManager.Connected(remoteAddr)
if err != nil {
srvrLog.Debugf("Marking address as connected failed: %v", err)
srvrLog.Errorf("Marking address as connected failed: %v", err)
}
}

Expand Down Expand Up @@ -2217,8 +2242,7 @@ func (s *server) inboundPeerConnected(conn net.Conn) {
// outboundPeerConnected is invoked by the connection manager when a new
// outbound connection is established. It initializes a new outbound server
// peer instance, associates it with the relevant state such as the connection
// request instance and the connection itself, and finally notifies the address
// manager of the attempt.
// request instance and the connection itself.
func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) {
sp := newServerPeer(s, c.Permanent)
p, err := peer.NewOutboundPeer(newPeerConfig(sp), c.Addr.String())
Expand All @@ -2232,12 +2256,6 @@ func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) {
sp.isWhitelisted = isWhitelisted(conn.RemoteAddr())
sp.AssociateConnection(conn)
go s.peerDoneHandler(sp)

remoteAddr := wireToAddrmgrNetAddress(sp.NA())
err = s.addrManager.Attempt(remoteAddr)
if err != nil {
srvrLog.Debugf("Marking address as attempted failed: %v", err)
}
}

// peerDoneHandler handles peer disconnects by notifying the server that it's
Expand Down Expand Up @@ -3702,7 +3720,7 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB,
OnAccept: s.inboundPeerConnected,
RetryDuration: connectionRetryInterval,
TargetOutbound: uint32(targetOutbound),
Dial: dcrdDial,
Dial: s.attemptDcrdDial,
Timeout: cfg.DialTimeout,
OnConnection: s.outboundPeerConnected,
GetNewAddress: newAddressFunc,
Expand Down