sessionwantsender.go 21.5 KB
Newer Older
dirkmc's avatar
dirkmc committed
1 2 3 4 5
package session

import (
	"context"

6
	bsbpm "github.com/ipfs/go-bitswap/internal/blockpresencemanager"
dirkmc's avatar
dirkmc committed
7 8 9 10 11

	cid "github.com/ipfs/go-cid"
	peer "github.com/libp2p/go-libp2p-core/peer"
)

12 13 14 15 16 17 18
const (
	// Maximum number of changes to accept before blocking
	changesBufferSize = 128
	// If the session receives this many DONT_HAVEs in a row from a peer,
	// it prunes the peer from the session
	peerDontHaveLimit = 16
)
dirkmc's avatar
dirkmc committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32

// BlockPresence indicates whether a peer has a block.
// Note that the order is important, we decide which peer to send a want to
// based on knowing whether peer has the block. eg we're more likely to send
// a want to a peer that has the block than a peer that doesnt have the block
// so BPHave > BPDontHave
type BlockPresence int

const (
	BPDontHave BlockPresence = iota
	BPUnknown
	BPHave
)

33 34 35 36 37 38
// SessionWantsCanceller provides a method to cancel wants
type SessionWantsCanceller interface {
	// Cancel wants for this session
	CancelSessionWants(sid uint64, wants []cid.Cid)
}

dirkmc's avatar
dirkmc committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// update encapsulates a message received by the session
type update struct {
	// Which peer sent the update
	from peer.ID
	// cids of blocks received
	ks []cid.Cid
	// HAVE message
	haves []cid.Cid
	// DONT_HAVE message
	dontHaves []cid.Cid
}

// peerAvailability indicates a peer's connection state
type peerAvailability struct {
	target    peer.ID
	available bool
}

57 58
// change can be new wants, a new message received by the session,
// or a change in the connect status of a peer
dirkmc's avatar
dirkmc committed
59 60 61
type change struct {
	// new wants requested
	add []cid.Cid
62 63
	// wants cancelled
	cancel []cid.Cid
dirkmc's avatar
dirkmc committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	// new message received by session (blocks / HAVEs / DONT_HAVEs)
	update update
	// peer has connected / disconnected
	availability peerAvailability
}

type onSendFn func(to peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid)
type onPeersExhaustedFn func([]cid.Cid)

//
// sessionWantSender is responsible for sending want-have and want-block to
// peers. For each want, it sends a single optimistic want-block request to
// one peer and want-have requests to all other peers in the session.
// To choose the best peer for the optimistic want-block it maintains a list
// of how peers have responded to each want (HAVE / DONT_HAVE / Unknown) and
// consults the peer response tracker (records which peers sent us blocks).
//
type sessionWantSender struct {
82
	// The context is used when sending wants
dirkmc's avatar
dirkmc committed
83
	ctx context.Context
84 85
	// Called to shutdown the sessionWantSender
	shutdown func()
86
	// The sessionWantSender uses the closed channel to signal when it's
87 88
	// finished shutting down
	closed chan struct{}
dirkmc's avatar
dirkmc committed
89 90 91 92 93 94
	// The session ID
	sessionID uint64
	// A channel that collects incoming changes (events)
	changes chan change
	// Information about each want indexed by CID
	wants map[cid.Cid]*wantInfo
95 96
	// Keeps track of how many consecutive DONT_HAVEs a peer has sent
	peerConsecutiveDontHaves map[peer.ID]int
dirkmc's avatar
dirkmc committed
97 98 99 100 101 102
	// Tracks which peers we have send want-block to
	swbt *sentWantBlocksTracker
	// Tracks the number of blocks each peer sent us
	peerRspTrkr *peerResponseTracker
	// Sends wants to peers
	pm PeerManager
103 104
	// Keeps track of peers in the session
	spm SessionPeerManager
105 106
	// Cancels wants
	canceller SessionWantsCanceller
dirkmc's avatar
dirkmc committed
107 108 109 110 111 112 113 114
	// Keeps track of which peer has / doesn't have a block
	bpm *bsbpm.BlockPresenceManager
	// Called when wants are sent
	onSend onSendFn
	// Called when all peers explicitly don't have a block
	onPeersExhausted onPeersExhaustedFn
}

115
func newSessionWantSender(sid uint64, pm PeerManager, spm SessionPeerManager, canceller SessionWantsCanceller,
116
	bpm *bsbpm.BlockPresenceManager, onSend onSendFn, onPeersExhausted onPeersExhaustedFn) sessionWantSender {
dirkmc's avatar
dirkmc committed
117

118
	ctx, cancel := context.WithCancel(context.Background())
119
	sws := sessionWantSender{
120
		ctx:                      ctx,
121
		shutdown:                 cancel,
122
		closed:                   make(chan struct{}),
123 124 125 126 127 128
		sessionID:                sid,
		changes:                  make(chan change, changesBufferSize),
		wants:                    make(map[cid.Cid]*wantInfo),
		peerConsecutiveDontHaves: make(map[peer.ID]int),
		swbt:                     newSentWantBlocksTracker(),
		peerRspTrkr:              newPeerResponseTracker(),
dirkmc's avatar
dirkmc committed
129 130

		pm:               pm,
131
		spm:              spm,
132
		canceller:        canceller,
dirkmc's avatar
dirkmc committed
133 134 135 136 137
		bpm:              bpm,
		onSend:           onSend,
		onPeersExhausted: onPeersExhausted,
	}

138
	return sws
dirkmc's avatar
dirkmc committed
139 140
}

141 142
func (sws *sessionWantSender) ID() uint64 {
	return sws.sessionID
dirkmc's avatar
dirkmc committed
143 144 145
}

// Add is called when new wants are added to the session
146
func (sws *sessionWantSender) Add(ks []cid.Cid) {
dirkmc's avatar
dirkmc committed
147 148 149
	if len(ks) == 0 {
		return
	}
150
	sws.addChange(change{add: ks})
dirkmc's avatar
dirkmc committed
151 152
}

153 154 155 156 157 158 159 160
// Cancel is called when a request is cancelled
func (sws *sessionWantSender) Cancel(ks []cid.Cid) {
	if len(ks) == 0 {
		return
	}
	sws.addChange(change{cancel: ks})
}

dirkmc's avatar
dirkmc committed
161 162
// Update is called when the session receives a message with incoming blocks
// or HAVE / DONT_HAVE
163
func (sws *sessionWantSender) Update(from peer.ID, ks []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) {
dirkmc's avatar
dirkmc committed
164
	hasUpdate := len(ks) > 0 || len(haves) > 0 || len(dontHaves) > 0
165
	if !hasUpdate {
dirkmc's avatar
dirkmc committed
166 167 168
		return
	}

169 170 171
	sws.addChange(change{
		update: update{from, ks, haves, dontHaves},
	})
dirkmc's avatar
dirkmc committed
172 173 174 175
}

// SignalAvailability is called by the PeerManager to signal that a peer has
// connected / disconnected
176
func (sws *sessionWantSender) SignalAvailability(p peer.ID, isAvailable bool) {
dirkmc's avatar
dirkmc committed
177
	availability := peerAvailability{p, isAvailable}
178 179 180
	// Add the change in a non-blocking manner to avoid the possibility of a
	// deadlock
	sws.addChangeNonBlocking(change{availability: availability})
dirkmc's avatar
dirkmc committed
181 182 183
}

// Run is the main loop for processing incoming changes
184
func (sws *sessionWantSender) Run() {
dirkmc's avatar
dirkmc committed
185 186
	for {
		select {
187 188
		case ch := <-sws.changes:
			sws.onChange([]change{ch})
189 190 191 192
		case <-sws.ctx.Done():
			// Unregister the session with the PeerManager
			sws.pm.UnregisterSession(sws.sessionID)

193 194 195
			// Close the 'closed' channel to signal to Shutdown() that the run
			// loop has exited
			close(sws.closed)
dirkmc's avatar
dirkmc committed
196 197 198 199 200
			return
		}
	}
}

201 202 203
// Shutdown the sessionWantSender
func (sws *sessionWantSender) Shutdown() {
	// Signal to the run loop to stop processing
204
	sws.shutdown()
205 206 207 208
	// Wait for run loop to complete
	<-sws.closed
}

dirkmc's avatar
dirkmc committed
209
// addChange adds a new change to the queue
210
func (sws *sessionWantSender) addChange(c change) {
dirkmc's avatar
dirkmc committed
211
	select {
212
	case sws.changes <- c:
213
	case <-sws.ctx.Done():
dirkmc's avatar
dirkmc committed
214 215 216
	}
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
// addChangeNonBlocking adds a new change to the queue, using a go-routine
// if the change blocks, so as to avoid potential deadlocks
func (sws *sessionWantSender) addChangeNonBlocking(c change) {
	select {
	case sws.changes <- c:
	default:
		// changes channel is full, so add change in a go routine instead
		go func() {
			select {
			case sws.changes <- c:
			case <-sws.ctx.Done():
			}
		}()
	}
}

dirkmc's avatar
dirkmc committed
233 234
// collectChanges collects all the changes that have occurred since the last
// invocation of onChange
235
func (sws *sessionWantSender) collectChanges(changes []change) []change {
dirkmc's avatar
dirkmc committed
236 237
	for len(changes) < changesBufferSize {
		select {
238
		case next := <-sws.changes:
dirkmc's avatar
dirkmc committed
239 240 241 242 243 244 245 246 247
			changes = append(changes, next)
		default:
			return changes
		}
	}
	return changes
}

// onChange processes the next set of changes
248
func (sws *sessionWantSender) onChange(changes []change) {
dirkmc's avatar
dirkmc committed
249 250
	// Several changes may have been recorded since the last time we checked,
	// so pop all outstanding changes from the channel
251
	changes = sws.collectChanges(changes)
dirkmc's avatar
dirkmc committed
252 253 254

	// Apply each change
	availability := make(map[peer.ID]bool, len(changes))
255
	cancels := make([]cid.Cid, 0)
dirkmc's avatar
dirkmc committed
256 257 258 259
	var updates []update
	for _, chng := range changes {
		// Initialize info for new wants
		for _, c := range chng.add {
260
			sws.trackWant(c)
dirkmc's avatar
dirkmc committed
261 262
		}

263 264 265 266 267 268
		// Remove cancelled wants
		for _, c := range chng.cancel {
			sws.untrackWant(c)
			cancels = append(cancels, c)
		}

dirkmc's avatar
dirkmc committed
269 270
		// Consolidate updates and changes to availability
		if chng.update.from != "" {
271 272 273
			// If the update includes blocks or haves, treat it as signaling that
			// the peer is available
			if len(chng.update.ks) > 0 || len(chng.update.haves) > 0 {
274 275 276 277 278
				p := chng.update.from
				availability[p] = true

				// Register with the PeerManager
				sws.pm.RegisterSession(p, sws)
279 280
			}

dirkmc's avatar
dirkmc committed
281 282 283 284 285 286 287 288
			updates = append(updates, chng.update)
		}
		if chng.availability.target != "" {
			availability[chng.availability.target] = chng.availability.available
		}
	}

	// Update peer availability
289
	newlyAvailable, newlyUnavailable := sws.processAvailability(availability)
dirkmc's avatar
dirkmc committed
290 291

	// Update wants
292
	dontHaves := sws.processUpdates(updates)
293 294 295

	// Check if there are any wants for which all peers have indicated they
	// don't have the want
296
	sws.checkForExhaustedWants(dontHaves, newlyUnavailable)
dirkmc's avatar
dirkmc committed
297

298 299 300 301 302
	// If there are any cancels, send them
	if len(cancels) > 0 {
		sws.canceller.CancelSessionWants(sws.sessionID, cancels)
	}

dirkmc's avatar
dirkmc committed
303
	// If there are some connected peers, send any pending wants
304 305
	if sws.spm.HasPeers() {
		sws.sendNextWants(newlyAvailable)
dirkmc's avatar
dirkmc committed
306 307 308 309 310
	}
}

// processAvailability updates the want queue with any changes in
// peer availability
311 312 313
// It returns the peers that have become
// - newly available
// - newly unavailable
314
func (sws *sessionWantSender) processAvailability(availability map[peer.ID]bool) (avail []peer.ID, unavail []peer.ID) {
dirkmc's avatar
dirkmc committed
315
	var newlyAvailable []peer.ID
316
	var newlyUnavailable []peer.ID
dirkmc's avatar
dirkmc committed
317
	for p, isNowAvailable := range availability {
318 319 320 321 322 323 324 325 326 327 328 329
		stateChange := false
		if isNowAvailable {
			isNewPeer := sws.spm.AddPeer(p)
			if isNewPeer {
				stateChange = true
				newlyAvailable = append(newlyAvailable, p)
			}
		} else {
			wasAvailable := sws.spm.RemovePeer(p)
			if wasAvailable {
				stateChange = true
				newlyUnavailable = append(newlyUnavailable, p)
dirkmc's avatar
dirkmc committed
330 331
			}
		}
332 333 334 335 336 337 338 339

		// If the state has changed
		if stateChange {
			sws.updateWantsPeerAvailability(p, isNowAvailable)
			// Reset the count of consecutive DONT_HAVEs received from the
			// peer
			delete(sws.peerConsecutiveDontHaves, p)
		}
dirkmc's avatar
dirkmc committed
340 341
	}

342
	return newlyAvailable, newlyUnavailable
dirkmc's avatar
dirkmc committed
343 344 345
}

// trackWant creates a new entry in the map of CID -> want info
346 347
func (sws *sessionWantSender) trackWant(c cid.Cid) {
	if _, ok := sws.wants[c]; ok {
dirkmc's avatar
dirkmc committed
348 349 350 351
		return
	}

	// Create the want info
352 353
	wi := newWantInfo(sws.peerRspTrkr)
	sws.wants[c] = wi
dirkmc's avatar
dirkmc committed
354 355 356

	// For each available peer, register any information we know about
	// whether the peer has the block
357 358
	for _, p := range sws.spm.Peers() {
		sws.updateWantBlockPresence(c, p)
dirkmc's avatar
dirkmc committed
359 360 361
	}
}

362 363 364 365 366
// untrackWant removes an entry from the map of CID -> want info
func (sws *sessionWantSender) untrackWant(c cid.Cid) {
	delete(sws.wants, c)
}

367 368
// processUpdates processes incoming blocks and HAVE / DONT_HAVEs.
// It returns all DONT_HAVEs.
369
func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid {
370 371
	// Process received blocks keys
	blkCids := cid.NewSet()
dirkmc's avatar
dirkmc committed
372
	for _, upd := range updates {
373 374
		for _, c := range upd.ks {
			blkCids.Add(c)
Dirk McCormick's avatar
Dirk McCormick committed
375

376 377 378 379 380 381 382 383 384 385
			// Remove the want
			removed := sws.removeWant(c)
			if removed != nil {
				// Inform the peer tracker that this peer was the first to send
				// us the block
				sws.peerRspTrkr.receivedBlockFrom(upd.from)
			}
			delete(sws.peerConsecutiveDontHaves, upd.from)
		}
	}
dirkmc's avatar
dirkmc committed
386

387 388 389 390
	// Process received DONT_HAVEs
	dontHaves := cid.NewSet()
	prunePeers := make(map[peer.ID]struct{})
	for _, upd := range updates {
dirkmc's avatar
dirkmc committed
391
		for _, c := range upd.dontHaves {
392 393 394 395 396 397 398 399 400
			// Track the number of consecutive DONT_HAVEs each peer receives
			if sws.peerConsecutiveDontHaves[upd.from] == peerDontHaveLimit {
				prunePeers[upd.from] = struct{}{}
			} else {
				sws.peerConsecutiveDontHaves[upd.from]++
			}

			// If we already received a block for the want, there's no need to
			// update block presence etc
401 402 403 404
			if blkCids.Has(c) {
				continue
			}

dirkmc's avatar
dirkmc committed
405 406 407
			dontHaves.Add(c)

			// Update the block presence for the peer
408
			sws.updateWantBlockPresence(c, upd.from)
dirkmc's avatar
dirkmc committed
409 410 411

			// Check if the DONT_HAVE is in response to a want-block
			// (could also be in response to want-have)
412
			if sws.swbt.haveSentWantBlockTo(upd.from, c) {
dirkmc's avatar
dirkmc committed
413 414
				// If we were waiting for a response from this peer, clear
				// sentTo so that we can send the want to another peer
415 416
				if sentTo, ok := sws.getWantSentTo(c); ok && sentTo == upd.from {
					sws.setWantSentTo(c, "")
dirkmc's avatar
dirkmc committed
417 418 419
				}
			}
		}
420
	}
dirkmc's avatar
dirkmc committed
421

422 423
	// Process received HAVEs
	for _, upd := range updates {
dirkmc's avatar
dirkmc committed
424
		for _, c := range upd.haves {
425 426 427 428
			// If we haven't already received a block for the want
			if !blkCids.Has(c) {
				// Update the block presence for the peer
				sws.updateWantBlockPresence(c, upd.from)
429 430 431
			}

			// Clear the consecutive DONT_HAVE count for the peer
432
			delete(sws.peerConsecutiveDontHaves, upd.from)
433
			delete(prunePeers, upd.from)
dirkmc's avatar
dirkmc committed
434 435 436
		}
	}

437 438
	// If any peers have sent us too many consecutive DONT_HAVEs, remove them
	// from the session
439 440 441 442 443 444 445
	for p := range prunePeers {
		// Before removing the peer from the session, check if the peer
		// sent us a HAVE for a block that we want
		for c := range sws.wants {
			if sws.bpm.PeerHasBlock(p, c) {
				delete(prunePeers, p)
				break
446 447
			}
		}
448 449
	}
	if len(prunePeers) > 0 {
450 451
		go func() {
			for p := range prunePeers {
452
				// Peer doesn't have anything we want, so remove it
Dirk McCormick's avatar
Dirk McCormick committed
453
				log.Infof("peer %s sent too many dont haves, removing from session %d", p, sws.ID())
454
				sws.SignalAvailability(p, false)
455 456
			}
		}()
457
	}
458 459 460 461 462 463

	return dontHaves.Keys()
}

// checkForExhaustedWants checks if there are any wants for which all peers
// have sent a DONT_HAVE. We call these "exhausted" wants.
464
func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyUnavailable []peer.ID) {
465 466 467 468 469 470 471 472 473 474 475 476 477
	// If there are no new DONT_HAVEs, and no peers became unavailable, then
	// we don't need to check for exhausted wants
	if len(dontHaves) == 0 && len(newlyUnavailable) == 0 {
		return
	}

	// We need to check each want for which we just received a DONT_HAVE
	wants := dontHaves

	// If a peer just became unavailable, then we need to check all wants
	// (because it may be the last peer who hadn't sent a DONT_HAVE for a CID)
	if len(newlyUnavailable) > 0 {
		// Collect all pending wants
478 479
		wants = make([]cid.Cid, len(sws.wants))
		for c := range sws.wants {
480 481 482 483 484
			wants = append(wants, c)
		}

		// If the last available peer in the session has become unavailable
		// then we need to broadcast all pending wants
485 486
		if !sws.spm.HasPeers() {
			sws.processExhaustedWants(wants)
487 488 489 490 491 492 493
			return
		}
	}

	// If all available peers for a cid sent a DONT_HAVE, signal to the session
	// that we've exhausted available peers
	if len(wants) > 0 {
494 495
		exhausted := sws.bpm.AllPeersDoNotHaveBlock(sws.spm.Peers(), wants)
		sws.processExhaustedWants(exhausted)
496 497 498 499 500
	}
}

// processExhaustedWants filters the list so that only those wants that haven't
// already been marked as exhausted are passed to onPeersExhausted()
501 502
func (sws *sessionWantSender) processExhaustedWants(exhausted []cid.Cid) {
	newlyExhausted := sws.newlyExhausted(exhausted)
503
	if len(newlyExhausted) > 0 {
504
		sws.onPeersExhausted(newlyExhausted)
505
	}
dirkmc's avatar
dirkmc committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
}

// convenience structs for passing around want-blocks and want-haves for a peer
type wantSets struct {
	wantBlocks *cid.Set
	wantHaves  *cid.Set
}

type allWants map[peer.ID]*wantSets

func (aw allWants) forPeer(p peer.ID) *wantSets {
	if _, ok := aw[p]; !ok {
		aw[p] = &wantSets{
			wantBlocks: cid.NewSet(),
			wantHaves:  cid.NewSet(),
		}
	}
	return aw[p]
}

// sendNextWants sends wants to peers according to the latest information
// about which peers have / dont have blocks
528
func (sws *sessionWantSender) sendNextWants(newlyAvailable []peer.ID) {
dirkmc's avatar
dirkmc committed
529 530
	toSend := make(allWants)

531
	for c, wi := range sws.wants {
dirkmc's avatar
dirkmc committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		// Ensure we send want-haves to any newly available peers
		for _, p := range newlyAvailable {
			toSend.forPeer(p).wantHaves.Add(c)
		}

		// We already sent a want-block to a peer and haven't yet received a
		// response yet
		if wi.sentTo != "" {
			continue
		}

		// All the peers have indicated that they don't have the block
		// corresponding to this want, so we must wait to discover more peers
		if wi.bestPeer == "" {
			// TODO: work this out in real time instead of using bestP?
			continue
		}

		// Record that we are sending a want-block for this want to the peer
551
		sws.setWantSentTo(c, wi.bestPeer)
dirkmc's avatar
dirkmc committed
552 553 554 555 556

		// Send a want-block to the chosen peer
		toSend.forPeer(wi.bestPeer).wantBlocks.Add(c)

		// Send a want-have to each other peer
557
		for _, op := range sws.spm.Peers() {
dirkmc's avatar
dirkmc committed
558 559 560 561 562 563 564
			if op != wi.bestPeer {
				toSend.forPeer(op).wantHaves.Add(c)
			}
		}
	}

	// Send any wants we've collected
565
	sws.sendWants(toSend)
dirkmc's avatar
dirkmc committed
566 567 568
}

// sendWants sends want-have and want-blocks to the appropriate peers
569
func (sws *sessionWantSender) sendWants(sends allWants) {
dirkmc's avatar
dirkmc committed
570 571 572
	// For each peer we're sending a request to
	for p, snd := range sends {
		// Piggyback some other want-haves onto the request to the peer
573
		for _, c := range sws.getPiggybackWantHaves(p, snd.wantBlocks) {
dirkmc's avatar
dirkmc committed
574 575 576 577 578 579 580 581 582
			snd.wantHaves.Add(c)
		}

		// Send the wants to the peer.
		// Note that the PeerManager ensures that we don't sent duplicate
		// want-haves / want-blocks to a peer, and that want-blocks take
		// precedence over want-haves.
		wblks := snd.wantBlocks.Keys()
		whaves := snd.wantHaves.Keys()
583
		sws.pm.SendWants(sws.ctx, p, wblks, whaves)
dirkmc's avatar
dirkmc committed
584 585

		// Inform the session that we've sent the wants
586
		sws.onSend(p, wblks, whaves)
dirkmc's avatar
dirkmc committed
587 588

		// Record which peers we send want-block to
589
		sws.swbt.addSentWantBlocksTo(p, wblks)
dirkmc's avatar
dirkmc committed
590 591 592 593 594
	}
}

// getPiggybackWantHaves gets the want-haves that should be piggybacked onto
// a request that we are making to send want-blocks to a peer
595
func (sws *sessionWantSender) getPiggybackWantHaves(p peer.ID, wantBlocks *cid.Set) []cid.Cid {
dirkmc's avatar
dirkmc committed
596
	var whs []cid.Cid
597
	for c := range sws.wants {
dirkmc's avatar
dirkmc committed
598 599
		// Don't send want-have if we're already sending a want-block
		// (or have previously)
600
		if !wantBlocks.Has(c) && !sws.swbt.haveSentWantBlockTo(p, c) {
dirkmc's avatar
dirkmc committed
601 602 603 604 605 606 607 608
			whs = append(whs, c)
		}
	}
	return whs
}

// newlyExhausted filters the list of keys for wants that have not already
// been marked as exhausted (all peers indicated they don't have the block)
609
func (sws *sessionWantSender) newlyExhausted(ks []cid.Cid) []cid.Cid {
dirkmc's avatar
dirkmc committed
610 611
	var res []cid.Cid
	for _, c := range ks {
612
		if wi, ok := sws.wants[c]; ok {
dirkmc's avatar
dirkmc committed
613 614 615 616 617 618 619 620 621 622
			if !wi.exhausted {
				res = append(res, c)
				wi.exhausted = true
			}
		}
	}
	return res
}

// removeWant is called when the corresponding block is received
623 624 625
func (sws *sessionWantSender) removeWant(c cid.Cid) *wantInfo {
	if wi, ok := sws.wants[c]; ok {
		delete(sws.wants, c)
dirkmc's avatar
dirkmc committed
626 627 628 629 630 631 632
		return wi
	}
	return nil
}

// updateWantsPeerAvailability is called when the availability changes for a
// peer. It updates all the wants accordingly.
633 634
func (sws *sessionWantSender) updateWantsPeerAvailability(p peer.ID, isNowAvailable bool) {
	for c, wi := range sws.wants {
dirkmc's avatar
dirkmc committed
635
		if isNowAvailable {
636
			sws.updateWantBlockPresence(c, p)
dirkmc's avatar
dirkmc committed
637 638 639 640 641 642 643 644
		} else {
			wi.removePeer(p)
		}
	}
}

// updateWantBlockPresence is called when a HAVE / DONT_HAVE is received for the given
// want / peer
645 646
func (sws *sessionWantSender) updateWantBlockPresence(c cid.Cid, p peer.ID) {
	wi, ok := sws.wants[c]
dirkmc's avatar
dirkmc committed
647 648 649 650 651 652
	if !ok {
		return
	}

	// If the peer sent us a HAVE or DONT_HAVE for the cid, adjust the
	// block presence for the peer / cid combination
653
	if sws.bpm.PeerHasBlock(p, c) {
dirkmc's avatar
dirkmc committed
654
		wi.setPeerBlockPresence(p, BPHave)
655
	} else if sws.bpm.PeerDoesNotHaveBlock(p, c) {
dirkmc's avatar
dirkmc committed
656 657 658 659 660 661 662
		wi.setPeerBlockPresence(p, BPDontHave)
	} else {
		wi.setPeerBlockPresence(p, BPUnknown)
	}
}

// Which peer was the want sent to
663 664
func (sws *sessionWantSender) getWantSentTo(c cid.Cid) (peer.ID, bool) {
	if wi, ok := sws.wants[c]; ok {
dirkmc's avatar
dirkmc committed
665 666 667 668 669 670
		return wi.sentTo, true
	}
	return "", false
}

// Record which peer the want was sent to
671 672
func (sws *sessionWantSender) setWantSentTo(c cid.Cid, p peer.ID) {
	if wi, ok := sws.wants[c]; ok {
dirkmc's avatar
dirkmc committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
		wi.sentTo = p
	}
}

// wantInfo keeps track of the information for a want
type wantInfo struct {
	// Tracks HAVE / DONT_HAVE sent to us for the want by each peer
	blockPresence map[peer.ID]BlockPresence
	// The peer that we've sent a want-block to (cleared when we get a response)
	sentTo peer.ID
	// The "best" peer to send the want to next
	bestPeer peer.ID
	// Keeps track of how many hits / misses each peer has sent us for wants
	// in the session
	peerRspTrkr *peerResponseTracker
	// true if all known peers have sent a DONT_HAVE for this want
	exhausted bool
}

// func newWantInfo(prt *peerResponseTracker, c cid.Cid, startIndex int) *wantInfo {
func newWantInfo(prt *peerResponseTracker) *wantInfo {
	return &wantInfo{
		blockPresence: make(map[peer.ID]BlockPresence),
		peerRspTrkr:   prt,
		exhausted:     false,
	}
}

// setPeerBlockPresence sets the block presence for the given peer
func (wi *wantInfo) setPeerBlockPresence(p peer.ID, bp BlockPresence) {
	wi.blockPresence[p] = bp
	wi.calculateBestPeer()

	// If a peer informed us that it has a block then make sure the want is no
	// longer flagged as exhausted (exhausted means no peers have the block)
	if bp == BPHave {
		wi.exhausted = false
	}
}

// removePeer deletes the given peer from the want info
func (wi *wantInfo) removePeer(p peer.ID) {
	// If we were waiting to hear back from the peer that is being removed,
	// clear the sentTo field so we no longer wait
	if p == wi.sentTo {
		wi.sentTo = ""
	}
	delete(wi.blockPresence, p)
	wi.calculateBestPeer()
}

// calculateBestPeer finds the best peer to send the want to next
func (wi *wantInfo) calculateBestPeer() {
	// Recalculate the best peer
	bestBP := BPDontHave
	bestPeer := peer.ID("")

	// Find the peer with the best block presence, recording how many peers
	// share the block presence
	countWithBest := 0
	for p, bp := range wi.blockPresence {
		if bp > bestBP {
			bestBP = bp
			bestPeer = p
			countWithBest = 1
		} else if bp == bestBP {
			countWithBest++
		}
	}
	wi.bestPeer = bestPeer

	// If no peer has a block presence better than DONT_HAVE, bail out
	if bestPeer == "" {
		return
	}

	// If there was only one peer with the best block presence, we're done
	if countWithBest <= 1 {
		return
	}

	// There were multiple peers with the best block presence, so choose one of
	// them to be the best
	var peersWithBest []peer.ID
	for p, bp := range wi.blockPresence {
		if bp == bestBP {
			peersWithBest = append(peersWithBest, p)
		}
	}
	wi.bestPeer = wi.peerRspTrkr.choose(peersWithBest)
}