@@ -75,11 +75,11 @@ type Network interface {
7575
7676 // Should only be called once, will run until either a fatal error occurs,
7777 // or the network is closed.
78- Dispatch () error
78+ Dispatch (ctx context. Context ) error
7979
8080 // Attempt to connect to this IP. The network will never stop attempting to
8181 // connect to this ID.
82- ManuallyTrack (nodeID ids.NodeID , ip netip.AddrPort )
82+ ManuallyTrack (ctx context. Context , nodeID ids.NodeID , ip netip.AddrPort )
8383
8484 // PeerInfo returns information about peers. If [nodeIDs] is empty, returns
8585 // info about all peers that have finished the handshake. Otherwise, returns
@@ -506,10 +506,13 @@ func (n *network) AllowConnection(nodeID ids.NodeID) bool {
506506 return areWeAPrimaryNetworkAValidator || n .ipTracker .WantsConnection (nodeID )
507507}
508508
509- func (n * network ) Track (claimedIPPorts []* ips.ClaimedIPPort ) error {
509+ func (n * network ) Track (
510+ ctx context.Context ,
511+ claimedIPPorts []* ips.ClaimedIPPort ,
512+ ) error {
510513 _ , areWeAPrimaryNetworkAValidator := n .config .Validators .GetValidator (constants .PrimaryNetworkID , n .config .MyNodeID )
511514 for _ , ip := range claimedIPPorts {
512- if err := n .track (ip , areWeAPrimaryNetworkAValidator ); err != nil {
515+ if err := n .track (ctx , ip , areWeAPrimaryNetworkAValidator ); err != nil {
513516 return err
514517 }
515518 }
@@ -521,17 +524,17 @@ func (n *network) Track(claimedIPPorts []*ips.ClaimedIPPort) error {
521524// It is guaranteed that [Connected] will not be called with [nodeID] after this
522525// call. Note that this is from the perspective of a single peer object, because
523526// a peer with the same ID can reconnect to this network instance.
524- func (n * network ) Disconnected (nodeID ids.NodeID ) {
527+ func (n * network ) Disconnected (ctx context. Context , nodeID ids.NodeID ) {
525528 n .peersLock .RLock ()
526529 _ , connecting := n .connectingPeers .GetByID (nodeID )
527530 peer , connected := n .connectedPeers .GetByID (nodeID )
528531 n .peersLock .RUnlock ()
529532
530533 if connecting {
531- n .disconnectedFromConnecting (nodeID )
534+ n .disconnectedFromConnecting (ctx , nodeID )
532535 }
533536 if connected {
534- n .disconnectedFromConnected (peer , nodeID )
537+ n .disconnectedFromConnected (ctx , peer , nodeID )
535538 }
536539}
537540
@@ -599,7 +602,7 @@ func (n *network) Peers(
599602
600603// Dispatch starts accepting connections from other nodes attempting to connect
601604// to this node.
602- func (n * network ) Dispatch () error {
605+ func (n * network ) Dispatch (ctx context. Context ) error {
603606 go n .runTimers () // Periodically perform operations
604607 go n .inboundConnUpgradeThrottler .Dispatch ()
605608 for n .onCloseCtx .Err () == nil { // Continuously accept new connections
@@ -647,7 +650,7 @@ func (n *network) Dispatch() error {
647650 zap .Stringer ("peerIP" , ip ),
648651 )
649652
650- if err := n .upgrade (conn , n .serverUpgrader , true ); err != nil {
653+ if err := n .upgrade (ctx , conn , n .serverUpgrader , true ); err != nil {
651654 n .peerConfig .Log .Verbo ("failed to upgrade connection" ,
652655 zap .String ("direction" , "inbound" ),
653656 zap .Error (err ),
@@ -670,7 +673,11 @@ func (n *network) Dispatch() error {
670673 return errs .Err
671674}
672675
673- func (n * network ) ManuallyTrack (nodeID ids.NodeID , ip netip.AddrPort ) {
676+ func (n * network ) ManuallyTrack (
677+ ctx context.Context ,
678+ nodeID ids.NodeID ,
679+ ip netip.AddrPort ,
680+ ) {
674681 n .ipTracker .ManuallyTrack (nodeID )
675682
676683 n .peersLock .Lock ()
@@ -688,11 +695,15 @@ func (n *network) ManuallyTrack(nodeID ids.NodeID, ip netip.AddrPort) {
688695 if ! isTracked {
689696 tracked := newTrackedIP (ip )
690697 n .trackedIPs [nodeID ] = tracked
691- n .dial (nodeID , tracked )
698+ n .dial (ctx , nodeID , tracked )
692699 }
693700}
694701
695- func (n * network ) track (ip * ips.ClaimedIPPort , trackAllSubnets bool ) error {
702+ func (n * network ) track (
703+ ctx context.Context ,
704+ ip * ips.ClaimedIPPort ,
705+ trackAllSubnets bool ,
706+ ) error {
696707 // To avoid signature verification when the IP isn't needed, we
697708 // optimistically filter out IPs. This can result in us not tracking an IP
698709 // that we otherwise would have. This case can only happen if the node
@@ -741,7 +752,7 @@ func (n *network) track(ip *ips.ClaimedIPPort, trackAllSubnets bool) error {
741752 tracked = newTrackedIP (ip .AddrPort )
742753 }
743754 n .trackedIPs [ip .NodeID ] = tracked
744- n .dial (ip .NodeID , tracked )
755+ n .dial (ctx , ip .NodeID , tracked )
745756 return nil
746757}
747758
@@ -834,7 +845,10 @@ func (n *network) samplePeers(
834845 )
835846}
836847
837- func (n * network ) disconnectedFromConnecting (nodeID ids.NodeID ) {
848+ func (n * network ) disconnectedFromConnecting (
849+ ctx context.Context ,
850+ nodeID ids.NodeID ,
851+ ) {
838852 n .peersLock .Lock ()
839853 defer n .peersLock .Unlock ()
840854
@@ -846,7 +860,7 @@ func (n *network) disconnectedFromConnecting(nodeID ids.NodeID) {
846860 if n .ipTracker .WantsConnection (nodeID ) {
847861 tracked := tracked .trackNewIP (tracked .ip )
848862 n .trackedIPs [nodeID ] = tracked
849- n .dial (nodeID , tracked )
863+ n .dial (ctx , nodeID , tracked )
850864 } else {
851865 tracked .stopTracking ()
852866 delete (n .trackedIPs , nodeID )
@@ -856,7 +870,11 @@ func (n *network) disconnectedFromConnecting(nodeID ids.NodeID) {
856870 n .metrics .disconnected .Inc ()
857871}
858872
859- func (n * network ) disconnectedFromConnected (peer peer.Peer , nodeID ids.NodeID ) {
873+ func (n * network ) disconnectedFromConnected (
874+ ctx context.Context ,
875+ peer peer.Peer ,
876+ nodeID ids.NodeID ,
877+ ) {
860878 n .ipTracker .Disconnected (nodeID )
861879 n .router .Disconnected (nodeID )
862880
@@ -869,7 +887,7 @@ func (n *network) disconnectedFromConnected(peer peer.Peer, nodeID ids.NodeID) {
869887 if ip , wantsConnection := n .ipTracker .GetIP (nodeID ); wantsConnection {
870888 tracked := newTrackedIP (ip .AddrPort )
871889 n .trackedIPs [nodeID ] = tracked
872- n .dial (nodeID , tracked )
890+ n .dial (ctx , nodeID , tracked )
873891 }
874892
875893 n .metrics .markDisconnected (peer )
@@ -894,7 +912,7 @@ func (n *network) disconnectedFromConnected(peer peer.Peer, nodeID ids.NodeID) {
894912// If initiating a connection to [ip] fails, then dial will reattempt. However,
895913// there is a randomized exponential backoff to avoid spamming connection
896914// attempts.
897- func (n * network ) dial (nodeID ids.NodeID , ip * trackedIP ) {
915+ func (n * network ) dial (ctx context. Context , nodeID ids.NodeID , ip * trackedIP ) {
898916 n .peerConfig .Log .Verbo ("attempting to dial node" ,
899917 zap .Stringer ("nodeID" , nodeID ),
900918 zap .Stringer ("ip" , ip .ip ),
@@ -994,7 +1012,7 @@ func (n *network) dial(nodeID ids.NodeID, ip *trackedIP) {
9941012 zap .Stringer ("peerIP" , ip .ip ),
9951013 )
9961014
997- err = n .upgrade (conn , n .clientUpgrader , false )
1015+ err = n .upgrade (ctx , conn , n .clientUpgrader , false )
9981016 if err != nil {
9991017 n .peerConfig .Log .Verbo (
10001018 "failed to upgrade, attempting again" ,
@@ -1017,7 +1035,12 @@ func (n *network) dial(nodeID ids.NodeID, ip *trackedIP) {
10171035// If the connection is desired by the node, then the resulting upgraded
10181036// connection will be used to create a new peer. Otherwise the connection will
10191037// be immediately closed.
1020- func (n * network ) upgrade (conn net.Conn , upgrader peer.Upgrader , isIngress bool ) error {
1038+ func (n * network ) upgrade (
1039+ ctx context.Context ,
1040+ conn net.Conn ,
1041+ upgrader peer.Upgrader ,
1042+ isIngress bool ,
1043+ ) error {
10211044 upgradeTimeout := n .peerConfig .Clock .Time ().Add (n .config .ReadHandshakeTimeout )
10221045 if err := conn .SetReadDeadline (upgradeTimeout ); err != nil {
10231046 _ = conn .Close ()
@@ -1027,7 +1050,7 @@ func (n *network) upgrade(conn net.Conn, upgrader peer.Upgrader, isIngress bool)
10271050 return err
10281051 }
10291052
1030- nodeID , tlsConn , cert , err := upgrader .Upgrade (conn )
1053+ nodeID , tlsConn , cert , err := upgrader .Upgrade (ctx , conn )
10311054 if err != nil {
10321055 _ = conn .Close ()
10331056 n .peerConfig .Log .Verbo ("failed to upgrade connection" ,
@@ -1107,6 +1130,7 @@ func (n *network) upgrade(conn net.Conn, upgrader peer.Upgrader, isIngress bool)
11071130 // same [peerConfig.InboundMsgThrottler]. This is guaranteed by the above
11081131 // de-duplications for [connectingPeers] and [connectedPeers].
11091132 peer := peer .Start (
1133+ ctx ,
11101134 n .peerConfig ,
11111135 tlsConn ,
11121136 cert ,
0 commit comments