@@ -86,35 +86,44 @@ func (pwm *peerWantManager) prepareSendWants(p peer.ID, wantBlocks []cid.Cid, wa
86
86
resWantHvs := make ([]cid.Cid , 0 )
87
87
88
88
// Get the existing want-blocks and want-haves for the peer
89
- if pws , ok := pwm .peerWants [p ]; ok {
90
- // Iterate over the requested want-blocks
91
- for _ , c := range wantBlocks {
92
- // If the want-block hasn't been sent to the peer
93
- if ! pws .wantBlocks .Has (c ) {
94
- // Record that the CID was sent as a want-block
95
- pws .wantBlocks .Add (c )
89
+ pws , ok := pwm .peerWants [p ]
90
+
91
+ if ! ok {
92
+ // In practice this should never happen:
93
+ // - PeerManager calls addPeer() as soon as the peer connects
94
+ // - PeerManager calls removePeer() as soon as the peer disconnects
95
+ // - All calls to PeerWantManager are locked
96
+ log .Errorf ("prepareSendWants() called with peer %s but peer not found in peerWantManager" , string (p ))
97
+ return resWantBlks , resWantHvs
98
+ }
96
99
97
- // Add the CID to the results
98
- resWantBlks = append (resWantBlks , c )
100
+ // Iterate over the requested want-blocks
101
+ for _ , c := range wantBlocks {
102
+ // If the want-block hasn't been sent to the peer
103
+ if ! pws .wantBlocks .Has (c ) {
104
+ // Record that the CID was sent as a want-block
105
+ pws .wantBlocks .Add (c )
99
106
100
- // Make sure the CID is no longer recorded as a want-have
101
- pws . wantHaves . Remove ( c )
107
+ // Add the CID to the results
108
+ resWantBlks = append ( resWantBlks , c )
102
109
103
- // Increment the count of want-blocks
104
- pwm .wantBlockGauge .Inc ()
105
- }
110
+ // Make sure the CID is no longer recorded as a want-have
111
+ pws .wantHaves .Remove (c )
112
+
113
+ // Increment the count of want-blocks
114
+ pwm .wantBlockGauge .Inc ()
106
115
}
116
+ }
107
117
108
- // Iterate over the requested want-haves
109
- for _ , c := range wantHaves {
110
- // If the CID has not been sent as a want-block or want-have
111
- if ! pws .wantBlocks .Has (c ) && ! pws .wantHaves .Has (c ) {
112
- // Record that the CID was sent as a want-have
113
- pws .wantHaves .Add (c )
118
+ // Iterate over the requested want-haves
119
+ for _ , c := range wantHaves {
120
+ // If the CID has not been sent as a want-block or want-have
121
+ if ! pws .wantBlocks .Has (c ) && ! pws .wantHaves .Has (c ) {
122
+ // Record that the CID was sent as a want-have
123
+ pws .wantHaves .Add (c )
114
124
115
- // Add the CID to the results
116
- resWantHvs = append (resWantHvs , c )
117
- }
125
+ // Add the CID to the results
126
+ resWantHvs = append (resWantHvs , c )
118
127
}
119
128
}
120
129
0 commit comments