]> Sergey Matveev's repositories - btrtrc.git/blobdiff - peerconn.go
Check that incoming peer request chunk lengths don't exceed the upload rate limiter...
[btrtrc.git] / peerconn.go
index e683be5c46f1d876b8ad365bf8a917f408a07e49..bd6b376b508d7b9606e6b4ad5e61e354a472e7c9 100644 (file)
@@ -5,26 +5,28 @@ import (
        "bytes"
        "errors"
        "fmt"
+       "golang.org/x/time/rate"
        "io"
        "math/rand"
        "net"
-       "sort"
        "strconv"
        "strings"
+       "sync/atomic"
        "time"
 
        "github.com/RoaringBitmap/roaring"
+       "github.com/anacrolix/chansync"
+       . "github.com/anacrolix/generics"
        "github.com/anacrolix/log"
        "github.com/anacrolix/missinggo/iter"
        "github.com/anacrolix/missinggo/v2/bitmap"
        "github.com/anacrolix/multiless"
-
-       "github.com/anacrolix/chansync"
        "github.com/anacrolix/torrent/bencode"
        "github.com/anacrolix/torrent/metainfo"
        "github.com/anacrolix/torrent/mse"
        pp "github.com/anacrolix/torrent/peer_protocol"
        request_strategy "github.com/anacrolix/torrent/request-strategy"
+       "github.com/anacrolix/torrent/typed-roaring"
 )
 
 type PeerSource string
@@ -47,11 +49,10 @@ type PeerRemoteAddr interface {
        String() string
 }
 
-// Since we have to store all the requests in memory, we can't reasonably exceed what would be
-// indexable with the memory space available.
 type (
-       maxRequests  = int
-       requestState = request_strategy.PeerNextRequestState
+       // Since we have to store all the requests in memory, we can't reasonably exceed what could be
+       // indexed with the memory space available.
+       maxRequests = int
 )
 
 type Peer struct {
@@ -63,9 +64,10 @@ type Peer struct {
        peerImpl
        callbacks *Callbacks
 
-       outgoing   bool
-       Network    string
-       RemoteAddr PeerRemoteAddr
+       outgoing     bool
+       Network      string
+       RemoteAddr   PeerRemoteAddr
+       bannableAddr Option[bannableAddr]
        // True if the connection is operating over MSE obfuscation.
        headerEncrypted bool
        cryptoMethod    mse.CryptoMethod
@@ -83,7 +85,10 @@ type Peer struct {
 
        // Stuff controlled by the local peer.
        needRequestUpdate    string
-       actualRequestState   requestState
+       requestState         request_strategy.PeerRequestState
+       updateRequestsTimer  *time.Timer
+       lastRequestUpdate    time.Time
+       peakRequests         maxRequests
        lastBecameInterested time.Time
        priorInterest        time.Duration
 
@@ -94,9 +99,9 @@ type Peer struct {
        choking                                bool
        piecesReceivedSinceLastRequestUpdate   maxRequests
        maxPiecesReceivedBetweenRequestUpdates maxRequests
-       // Chunks that we might reasonably expect to receive from the peer. Due to
-       // latency, buffering, and implementation differences, we may receive
-       // chunks that are no longer in the set of requests actually want.
+       // Chunks that we might reasonably expect to receive from the peer. Due to latency, buffering,
+       // and implementation differences, we may receive chunks that are no longer in the set of
+       // requests actually want. This could use a roaring.BSI if the memory use becomes noticeable.
        validReceiveChunks map[RequestIndex]int
        // Indexed by metadata piece, set to true if posted and pending a
        // response.
@@ -109,26 +114,27 @@ type Peer struct {
        peerRequests          map[Request]*peerRequestState
        PeerPrefersEncryption bool // as indicated by 'e' field in extension handshake
        PeerListenPort        int
-       // The pieces the peer has claimed to have.
-       _peerPieces roaring.Bitmap
-       // The peer has everything. This can occur due to a special message, when
-       // we may not even know the number of pieces in the torrent yet.
-       peerSentHaveAll bool
        // The highest possible number of pieces the torrent could have based on
        // communication with the peer. Generally only useful until we have the
        // torrent info.
        peerMinPieces pieceIndex
        // Pieces we've accepted chunks for from the peer.
        peerTouchedPieces map[pieceIndex]struct{}
-       peerAllowedFast   roaring.Bitmap
+       peerAllowedFast   typedRoaring.Bitmap[pieceIndex]
 
        PeerMaxRequests  maxRequests // Maximum pending requests the peer allows.
        PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber
-       PeerClientName   string
+       PeerClientName   atomic.Value
 
        logger log.Logger
 }
 
+type peerRequests = orderedBitmap[RequestIndex]
+
+func (p *Peer) initRequestState() {
+       p.requestState.Requests = &peerRequests{}
+}
+
 // Maintains the state of a BitTorrent-protocol based connection with a peer.
 type PeerConn struct {
        Peer
@@ -141,7 +147,8 @@ type PeerConn struct {
        PeerID             PeerID
        PeerExtensionBytes pp.PeerExtensionBits
 
-       // The actual Conn, used for closing, and setting socket options.
+       // The actual Conn, used for closing, and setting socket options. Do not use methods on this
+       // while holding any mutexes.
        conn net.Conn
        // The Reader and Writer for this Conn, with hooks installed for stats,
        // limiting, deadlines etc.
@@ -152,6 +159,12 @@ type PeerConn struct {
 
        uploadTimer *time.Timer
        pex         pexConnState
+
+       // The pieces the peer has claimed to have.
+       _peerPieces roaring.Bitmap
+       // The peer has everything. This can occur due to a special message, when
+       // we may not even know the number of pieces in the torrent yet.
+       peerSentHaveAll bool
 }
 
 func (cn *PeerConn) connStatusString() string {
@@ -172,21 +185,21 @@ func (cn *Peer) updateExpectingChunks() {
 }
 
 func (cn *Peer) expectingChunks() bool {
-       if cn.actualRequestState.Requests.IsEmpty() {
+       if cn.requestState.Requests.IsEmpty() {
                return false
        }
-       if !cn.actualRequestState.Interested {
+       if !cn.requestState.Interested {
                return false
        }
        if !cn.peerChoking {
                return true
        }
        haveAllowedFastRequests := false
-       cn.peerAllowedFast.Iterate(func(i uint32) bool {
-               haveAllowedFastRequests = roaringBitmapRangeCardinality(
-                       &cn.actualRequestState.Requests,
-                       cn.t.pieceRequestIndexOffset(pieceIndex(i)),
-                       cn.t.pieceRequestIndexOffset(pieceIndex(i+1)),
+       cn.peerAllowedFast.Iterate(func(i pieceIndex) bool {
+               haveAllowedFastRequests = roaringBitmapRangeCardinality[RequestIndex](
+                       cn.requestState.Requests,
+                       cn.t.pieceRequestIndexOffset(i),
+                       cn.t.pieceRequestIndexOffset(i+1),
                ) == 0
                return !haveAllowedFastRequests
        })
@@ -194,7 +207,7 @@ func (cn *Peer) expectingChunks() bool {
 }
 
 func (cn *Peer) remoteChokingPiece(piece pieceIndex) bool {
-       return cn.peerChoking && !cn.peerAllowedFast.Contains(bitmap.BitIndex(piece))
+       return cn.peerChoking && !cn.peerAllowedFast.Contains(piece)
 }
 
 // Returns true if the connection is over IPv6.
@@ -215,33 +228,33 @@ func (cn *PeerConn) isPreferredDirection() bool {
 // Returns whether the left connection should be preferred over the right one,
 // considering only their networking properties. If ok is false, we can't
 // decide.
-func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) (left, ok bool) {
-       var ml multiLess
-       ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection())
-       ml.NextBool(!l.utp(), !r.utp())
-       ml.NextBool(l.ipv6(), r.ipv6())
-       return ml.FinalOk()
+func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
+       var ml multiless.Computation
+       ml = ml.Bool(r.isPreferredDirection(), l.isPreferredDirection())
+       ml = ml.Bool(l.utp(), r.utp())
+       ml = ml.Bool(r.ipv6(), l.ipv6())
+       return ml.Less()
 }
 
 func (cn *Peer) cumInterest() time.Duration {
        ret := cn.priorInterest
-       if cn.actualRequestState.Interested {
+       if cn.requestState.Interested {
                ret += time.Since(cn.lastBecameInterested)
        }
        return ret
 }
 
-func (cn *Peer) peerHasAllPieces() (all bool, known bool) {
+func (cn *PeerConn) peerHasAllPieces() (all, known bool) {
        if cn.peerSentHaveAll {
                return true, true
        }
        if !cn.t.haveInfo() {
                return false, false
        }
-       return roaring.Flip(&cn._peerPieces, 0, bitmap.BitRange(cn.t.numPieces())).IsEmpty(), true
+       return cn._peerPieces.GetCardinality() == uint64(cn.t.numPieces()), true
 }
 
-func (cn *PeerConn) locker() *lockWithDeferreds {
+func (cn *Peer) locker() *lockWithDeferreds {
        return cn.t.cl.locker()
 }
 
@@ -259,8 +272,8 @@ func (cn *Peer) bestPeerNumPieces() pieceIndex {
 }
 
 func (cn *Peer) completedString() string {
-       have := pieceIndex(cn._peerPieces.GetCardinality())
-       if cn.peerSentHaveAll {
+       have := pieceIndex(cn.peerPieces().GetCardinality())
+       if all, _ := cn.peerHasAllPieces(); all {
                have = cn.bestPeerNumPieces()
        }
        return fmt.Sprintf("%d/%d", have, cn.bestPeerNumPieces())
@@ -277,6 +290,10 @@ func (cn *PeerConn) setNumPieces(num pieceIndex) {
        cn.peerPiecesChanged()
 }
 
+func (cn *PeerConn) peerPieces() *roaring.Bitmap {
+       return &cn._peerPieces
+}
+
 func eventAgeString(t time.Time) string {
        if t.IsZero() {
                return "never"
@@ -309,7 +326,7 @@ func (cn *Peer) statusFlags() (ret string) {
        c := func(b byte) {
                ret += string([]byte{b})
        }
-       if cn.actualRequestState.Interested {
+       if cn.requestState.Interested {
                c('i')
        }
        if cn.choking {
@@ -335,13 +352,32 @@ func (cn *Peer) downloadRate() float64 {
        return float64(num) / cn.totalExpectingTime().Seconds()
 }
 
-func (cn *Peer) numRequestsByPiece() (ret map[pieceIndex]int) {
-       ret = make(map[pieceIndex]int)
-       cn.actualRequestState.Requests.Iterate(func(x uint32) bool {
-               ret[pieceIndex(x/cn.t.chunksPerRegularPiece())]++
+func (cn *Peer) DownloadRate() float64 {
+       cn.locker().Lock()
+       defer cn.locker().Unlock()
+
+       return cn.downloadRate()
+}
+
+func (cn *Peer) iterContiguousPieceRequests(f func(piece pieceIndex, count int)) {
+       var last Option[pieceIndex]
+       var count int
+       next := func(item Option[pieceIndex]) {
+               if item == last {
+                       count++
+               } else {
+                       if count != 0 {
+                               f(last.Value, count)
+                       }
+                       last = item
+                       count = 1
+               }
+       }
+       cn.requestState.Requests.Iterate(func(requestIndex request_strategy.RequestIndex) bool {
+               next(Some(cn.t.pieceIndexOfRequestIndex(requestIndex)))
                return true
        })
-       return
+       next(None[pieceIndex]())
 }
 
 func (cn *Peer) writeStatus(w io.Writer, t *Torrent) {
@@ -364,13 +400,14 @@ func (cn *Peer) writeStatus(w io.Writer, t *Torrent) {
                cn.totalExpectingTime(),
        )
        fmt.Fprintf(w,
-               "    %s completed, %d pieces touched, good chunks: %v/%v-%v reqq: %d/(%d/%d)-%d/%d, flags: %s, dr: %.1f KiB/s\n",
+               "    %s completed, %d pieces touched, good chunks: %v/%v:%v reqq: %d+%v/(%d/%d):%d/%d, flags: %s, dr: %.1f KiB/s\n",
                cn.completedString(),
                len(cn.peerTouchedPieces),
                &cn._stats.ChunksReadUseful,
                &cn._stats.ChunksRead,
                &cn._stats.ChunksWritten,
-               cn.actualRequestState.Requests.GetCardinality(),
+               cn.requestState.Requests.GetCardinality(),
+               cn.requestState.Cancelled.GetCardinality(),
                cn.nominalMaxRequests(),
                cn.PeerMaxRequests,
                len(cn.peerRequests),
@@ -379,20 +416,9 @@ func (cn *Peer) writeStatus(w io.Writer, t *Torrent) {
                cn.downloadRate()/(1<<10),
        )
        fmt.Fprintf(w, "    requested pieces:")
-       type pieceNumRequestsType struct {
-               piece       pieceIndex
-               numRequests int
-       }
-       var pieceNumRequests []pieceNumRequestsType
-       for piece, count := range cn.numRequestsByPiece() {
-               pieceNumRequests = append(pieceNumRequests, pieceNumRequestsType{piece, count})
-       }
-       sort.Slice(pieceNumRequests, func(i, j int) bool {
-               return pieceNumRequests[i].piece < pieceNumRequests[j].piece
+       cn.iterContiguousPieceRequests(func(piece pieceIndex, count int) {
+               fmt.Fprintf(w, " %v(%v)", piece, count)
        })
-       for _, elem := range pieceNumRequests {
-               fmt.Fprintf(w, " %v(%v)", elem.piece, elem.numRequests)
-       }
        fmt.Fprintf(w, "\n")
 }
 
@@ -400,6 +426,9 @@ func (p *Peer) close() {
        if !p.closed.Set() {
                return
        }
+       if p.updateRequestsTimer != nil {
+               p.updateRequestsTimer.Stop()
+       }
        p.peerImpl.onClose()
        if p.t != nil {
                p.t.decPeerPieceAvailability(p)
@@ -415,20 +444,28 @@ func (cn *PeerConn) onClose() {
        }
        cn.tickleWriter()
        if cn.conn != nil {
-               cn.conn.Close()
+               go cn.conn.Close()
        }
        if cb := cn.callbacks.PeerConnClosed; cb != nil {
                cb(cn)
        }
 }
 
+// Peer definitely has a piece, for purposes of requesting. So it's not sufficient that we think
+// they do (known=true).
 func (cn *Peer) peerHasPiece(piece pieceIndex) bool {
-       return cn.peerSentHaveAll || cn._peerPieces.Contains(bitmap.BitIndex(piece))
+       if all, known := cn.peerHasAllPieces(); all && known {
+               return true
+       }
+       return cn.peerPieces().ContainsInt(piece)
 }
 
 // 64KiB, but temporarily less to work around an issue with WebRTC. TODO: Update when
 // https://github.com/pion/datachannel/issues/59 is fixed.
-const writeBufferHighWaterLen = 1 << 15
+const (
+       writeBufferHighWaterLen = 1 << 15
+       writeBufferLowWaterLen  = writeBufferHighWaterLen / 2
+)
 
 // Writes a message into the write buffer. Returns whether it's okay to keep writing. Writing is
 // done asynchronously, so it may be that we're not able to honour backpressure from this method.
@@ -464,9 +501,17 @@ func (cn *PeerConn) requestedMetadataPiece(index int) bool {
        return index < len(cn.metadataRequests) && cn.metadataRequests[index]
 }
 
+var (
+       interestedMsgLen = len(pp.Message{Type: pp.Interested}.MustMarshalBinary())
+       requestMsgLen    = len(pp.Message{Type: pp.Request}.MustMarshalBinary())
+       // This is the maximum request count that could fit in the write buffer if it's at or below the
+       // low water mark when we run maybeUpdateActualRequestState.
+       maxLocalToRemoteRequests = (writeBufferHighWaterLen - writeBufferLowWaterLen - interestedMsgLen) / requestMsgLen
+)
+
 // The actual value to use as the maximum outbound requests.
-func (cn *Peer) nominalMaxRequests() (ret maxRequests) {
-       return maxRequests(clamp(1, int64(cn.PeerMaxRequests), 2048))
+func (cn *Peer) nominalMaxRequests() maxRequests {
+       return maxInt(1, minInt(cn.PeerMaxRequests, cn.peakRequests*2, maxLocalToRemoteRequests))
 }
 
 func (cn *Peer) totalExpectingTime() (ret time.Duration) {
@@ -475,7 +520,6 @@ func (cn *Peer) totalExpectingTime() (ret time.Duration) {
                ret += time.Since(cn.lastStartedExpectingToReceiveChunks)
        }
        return
-
 }
 
 func (cn *PeerConn) onPeerSentCancel(r Request) {
@@ -498,12 +542,7 @@ func (cn *PeerConn) choke(msg messageWriter) (more bool) {
        more = msg(pp.Message{
                Type: pp.Choke,
        })
-       if cn.fastEnabled() {
-               for r := range cn.peerRequests {
-                       // TODO: Don't reject pieces in allowed fast set.
-                       cn.reject(r)
-               }
-       } else {
+       if !cn.fastEnabled() {
                cn.peerRequests = nil
        }
        return
@@ -520,10 +559,10 @@ func (cn *PeerConn) unchoke(msg func(pp.Message) bool) bool {
 }
 
 func (cn *Peer) setInterested(interested bool) bool {
-       if cn.actualRequestState.Interested == interested {
+       if cn.requestState.Interested == interested {
                return true
        }
-       cn.actualRequestState.Interested = interested
+       cn.requestState.Interested = interested
        if interested {
                cn.lastBecameInterested = time.Now()
        } else if !cn.lastBecameInterested.IsZero() {
@@ -550,8 +589,13 @@ func (pc *PeerConn) writeInterested(interested bool) bool {
 // are okay.
 type messageWriter func(pp.Message) bool
 
+// This function seems to only used by Peer.request. It's all logic checks, so maybe we can no-op it
+// when we want to go fast.
 func (cn *Peer) shouldRequest(r RequestIndex) error {
-       pi := pieceIndex(r / cn.t.chunksPerRegularPiece())
+       pi := cn.t.pieceIndexOfRequestIndex(r)
+       if cn.requestState.Cancelled.Contains(r) {
+               return errors.New("request is cancelled and waiting acknowledgement")
+       }
        if !cn.peerHasPiece(pi) {
                return errors.New("requesting piece peer doesn't have")
        }
@@ -567,28 +611,43 @@ func (cn *Peer) shouldRequest(r RequestIndex) error {
        if cn.t.pieceQueuedForHash(pi) {
                panic("piece is queued for hash")
        }
-       if cn.peerChoking && !cn.peerAllowedFast.Contains(bitmap.BitIndex(pi)) {
-               panic("peer choking and piece not allowed fast")
+       if cn.peerChoking && !cn.peerAllowedFast.Contains(pi) {
+               // This could occur if we made a request with the fast extension, and then got choked and
+               // haven't had the request rejected yet.
+               if !cn.requestState.Requests.Contains(r) {
+                       panic("peer choking and piece not allowed fast")
+               }
        }
        return nil
 }
 
+func (cn *Peer) mustRequest(r RequestIndex) bool {
+       more, err := cn.request(r)
+       if err != nil {
+               panic(err)
+       }
+       return more
+}
+
 func (cn *Peer) request(r RequestIndex) (more bool, err error) {
        if err := cn.shouldRequest(r); err != nil {
                panic(err)
        }
-       if cn.actualRequestState.Requests.Contains(r) {
+       if cn.requestState.Requests.Contains(r) {
                return true, nil
        }
-       if maxRequests(cn.actualRequestState.Requests.GetCardinality()) >= cn.nominalMaxRequests() {
+       if maxRequests(cn.requestState.Requests.GetCardinality()) >= cn.nominalMaxRequests() {
                return true, errors.New("too many outstanding requests")
        }
-       cn.actualRequestState.Requests.Add(r)
+       cn.requestState.Requests.Add(r)
        if cn.validReceiveChunks == nil {
                cn.validReceiveChunks = make(map[RequestIndex]int)
        }
        cn.validReceiveChunks[r]++
-       cn.t.pendingRequests.Inc(r)
+       cn.t.requestState[r] = requestState{
+               peer: cn,
+               when: time.Now(),
+       }
        cn.updateExpectingChunks()
        ppReq := cn.t.requestIndexToRequest(r)
        for _, f := range cn.callbacks.SentRequest {
@@ -606,24 +665,38 @@ func (me *PeerConn) _request(r Request) bool {
        })
 }
 
-func (me *Peer) cancel(r RequestIndex) bool {
-       if me.deleteRequest(r) {
-               if me.actualRequestState.Requests.GetCardinality() == 0 {
-                       me.updateRequests("Peer.cancel")
+func (me *Peer) cancel(r RequestIndex) {
+       if !me.deleteRequest(r) {
+               panic("request not existing should have been guarded")
+       }
+       if me._cancel(r) {
+               if !me.requestState.Cancelled.CheckedAdd(r) {
+                       panic("request already cancelled")
                }
-               return me.peerImpl._cancel(me.t.requestIndexToRequest(r))
        }
-       return true
+       me.decPeakRequests()
+       if me.isLowOnRequests() {
+               me.updateRequests("Peer.cancel")
+       }
 }
 
-func (me *PeerConn) _cancel(r Request) bool {
-       return me.write(makeCancelMessage(r))
+func (me *PeerConn) _cancel(r RequestIndex) bool {
+       me.write(makeCancelMessage(me.t.requestIndexToRequest(r)))
+       // Transmission does not send rejects for received cancels. See
+       // https://github.com/transmission/transmission/pull/2275.
+       return me.fastEnabled() && !me.remoteIsTransmission()
 }
 
 func (cn *PeerConn) fillWriteBuffer() {
-       if !cn.applyNextRequestState() {
-               return
-       }
+       if cn.messageWriter.writeBuffer.Len() > writeBufferLowWaterLen {
+               // Fully committing to our max requests requires sufficient space (see
+               // maxLocalToRemoteRequests). Flush what we have instead. We also prefer always to make
+               // requests than to do PEX or upload, so we short-circuit before handling those. Any update
+               // request reason will not be cleared, so we'll come right back here when there's space. We
+               // can't do this in maybeUpdateActualRequestState because it's a method on Peer and has no
+               // knowledge of write buffers.
+       }
+       cn.maybeUpdateActualRequestState()
        if cn.pex.IsEnabled() {
                if flow := cn.pex.Share(cn.write); !flow {
                        return
@@ -657,11 +730,20 @@ func (cn *PeerConn) postBitfield() {
        cn.sentHaves = bitmap.Bitmap{cn.t._completedPieces.Clone()}
 }
 
-func (cn *PeerConn) updateRequests(reason string) {
+// Sets a reason to update requests, and if there wasn't already one, handle it.
+func (cn *Peer) updateRequests(reason string) {
        if cn.needRequestUpdate != "" {
                return
        }
+       if reason != peerUpdateRequestsTimerReason && !cn.isLowOnRequests() {
+               return
+       }
        cn.needRequestUpdate = reason
+       cn.handleUpdateRequests()
+}
+
+func (cn *PeerConn) handleUpdateRequests() {
+       // The writer determines the request state as needed when it can write.
        cn.tickleWriter()
 }
 
@@ -727,46 +809,66 @@ func (cn *PeerConn) peerSentBitfield(bf []bool) error {
                // Ignore known excess pieces.
                bf = bf[:cn.t.numPieces()]
        }
-       pp := cn.newPeerPieces()
+       bm := boolSliceToBitmap(bf)
+       if cn.t.haveInfo() && pieceIndex(bm.GetCardinality()) == cn.t.numPieces() {
+               cn.onPeerHasAllPieces()
+               return nil
+       }
+       if !bm.IsEmpty() {
+               cn.raisePeerMinPieces(pieceIndex(bm.Maximum()) + 1)
+       }
+       shouldUpdateRequests := false
+       if cn.peerSentHaveAll {
+               if !cn.t.deleteConnWithAllPieces(&cn.Peer) {
+                       panic(cn)
+               }
+               cn.peerSentHaveAll = false
+               if !cn._peerPieces.IsEmpty() {
+                       panic("if peer has all, we expect no individual peer pieces to be set")
+               }
+       } else {
+               bm.Xor(&cn._peerPieces)
+       }
        cn.peerSentHaveAll = false
-       for i, have := range bf {
-               if have {
-                       cn.raisePeerMinPieces(pieceIndex(i) + 1)
-                       if !pp.Contains(bitmap.BitIndex(i)) {
-                               cn.t.incPieceAvailability(i)
-                       }
+       // bm is now 'on' for pieces that are changing
+       bm.Iterate(func(x uint32) bool {
+               pi := pieceIndex(x)
+               if cn._peerPieces.Contains(x) {
+                       // Then we must be losing this piece
+                       cn.t.decPieceAvailability(pi)
                } else {
-                       if pp.Contains(bitmap.BitIndex(i)) {
-                               cn.t.decPieceAvailability(i)
-                       }
-               }
-               if have {
-                       cn._peerPieces.Add(uint32(i))
-                       if cn.t.wantPieceIndex(i) {
-                               cn.updateRequests("bitfield")
+                       if !shouldUpdateRequests && cn.t.wantPieceIndex(pieceIndex(x)) {
+                               shouldUpdateRequests = true
                        }
-               } else {
-                       cn._peerPieces.Remove(uint32(i))
+                       // We must be gaining this piece
+                       cn.t.incPieceAvailability(pieceIndex(x))
                }
+               return true
+       })
+       // Apply the changes. If we had everything previously, this should be empty, so xor is the same
+       // as or.
+       cn._peerPieces.Xor(&bm)
+       if shouldUpdateRequests {
+               cn.updateRequests("bitfield")
        }
+       // We didn't guard this before, I see no reason to do it now.
        cn.peerPiecesChanged()
        return nil
 }
 
-func (cn *Peer) onPeerHasAllPieces() {
+func (cn *PeerConn) onPeerHasAllPieces() {
        t := cn.t
        if t.haveInfo() {
-               npp, pc := cn.newPeerPieces(), t.numPieces()
-               for i := 0; i < pc; i += 1 {
-                       if !npp.Contains(bitmap.BitIndex(i)) {
-                               t.incPieceAvailability(i)
-                       }
-               }
+               cn._peerPieces.Iterate(func(x uint32) bool {
+                       t.decPieceAvailability(pieceIndex(x))
+                       return true
+               })
        }
+       t.addConnWithAllPieces(&cn.Peer)
        cn.peerSentHaveAll = true
        cn._peerPieces.Clear()
-       if cn.t._pendingPieces.Len() != 0 {
-               cn.updateRequests("have all")
+       if !cn.t._pendingPieces.IsEmpty() {
+               cn.updateRequests("Peer.onPeerHasAllPieces")
        }
        cn.peerPiecesChanged()
 }
@@ -777,7 +879,9 @@ func (cn *PeerConn) onPeerSentHaveAll() error {
 }
 
 func (cn *PeerConn) peerSentHaveNone() error {
-       cn.t.decPeerPieceAvailability(&cn.Peer)
+       if cn.peerSentHaveAll {
+               cn.t.decPeerPieceAvailability(&cn.Peer)
+       }
        cn._peerPieces.Clear()
        cn.peerSentHaveAll = false
        cn.peerPiecesChanged()
@@ -840,7 +944,7 @@ func (cn *PeerConn) wroteBytes(n int64) {
        cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesWritten }))
 }
 
-func (cn *PeerConn) readBytes(n int64) {
+func (cn *Peer) readBytes(n int64) {
        cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesRead }))
 }
 
@@ -883,10 +987,22 @@ func (c *PeerConn) reject(r Request) {
        delete(c.peerRequests, r)
 }
 
-func (c *PeerConn) onReadRequest(r Request) error {
+func (c *PeerConn) maximumPeerRequestChunkLength() (_ Option[int]) {
+       uploadRateLimiter := c.t.cl.config.UploadRateLimiter
+       if uploadRateLimiter.Limit() == rate.Inf {
+               return
+       }
+       return Some(uploadRateLimiter.Burst())
+}
+
+// startFetch is for testing purposes currently.
+func (c *PeerConn) onReadRequest(r Request, startFetch bool) error {
        requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
        if _, ok := c.peerRequests[r]; ok {
                torrent.Add("duplicate requests received", 1)
+               if c.fastEnabled() {
+                       return errors.New("received duplicate request with fast enabled")
+               }
                return nil
        }
        if c.choking {
@@ -906,10 +1022,18 @@ func (c *PeerConn) onReadRequest(r Request) error {
                // BEP 6 says we may close here if we choose.
                return nil
        }
+       if opt := c.maximumPeerRequestChunkLength(); opt.Ok && int(r.Length) > opt.Value {
+               err := fmt.Errorf("peer requested chunk too long (%v)", r.Length)
+               c.logger.Levelf(log.Warning, err.Error())
+               if c.fastEnabled() {
+                       c.reject(r)
+                       return nil
+               } else {
+                       return err
+               }
+       }
        if !c.t.havePiece(pieceIndex(r.Index)) {
-               // This isn't necessarily them screwing up. We can drop pieces
-               // from our storage, and can't communicate this to peers
-               // except by reconnecting.
+               // TODO: Tell the peer we don't have the piece, and reject this request.
                requestsReceivedForMissingPieces.Add(1)
                return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int())
        }
@@ -923,8 +1047,10 @@ func (c *PeerConn) onReadRequest(r Request) error {
        }
        value := &peerRequestState{}
        c.peerRequests[r] = value
-       go c.peerRequestDataReader(r, value)
-       //c.tickleWriter()
+       if startFetch {
+               // TODO: Limit peer request data read concurrency.
+               go c.peerRequestDataReader(r, value)
+       }
        return nil
 }
 
@@ -938,7 +1064,9 @@ func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
                if b == nil {
                        panic("data must be non-nil to trigger send")
                }
+               torrent.Add("peer request data read successes", 1)
                prs.data = b
+               // This might be required for the error case too (#752 and #753).
                c.tickleWriter()
        }
 }
@@ -946,7 +1074,17 @@ func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
 // If this is maintained correctly, we might be able to support optional synchronous reading for
 // chunk sending, the way it used to work.
 func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
-       c.logger.WithDefaultLevel(log.Warning).Printf("error reading chunk for peer Request %v: %v", r, err)
+       torrent.Add("peer request data read failures", 1)
+       logLevel := log.Warning
+       if c.t.hasStorageCap() {
+               // It's expected that pieces might drop. See
+               // https://github.com/anacrolix/torrent/issues/702#issuecomment-1000953313.
+               logLevel = log.Debug
+       }
+       c.logger.WithDefaultLevel(logLevel).Printf("error reading chunk for peer Request %v: %v", r, err)
+       if c.t.closed.IsSet() {
+               return
+       }
        i := pieceIndex(r.Index)
        if c.t.pieceComplete(i) {
                // There used to be more code here that just duplicated the following break. Piece
@@ -955,14 +1093,23 @@ func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
                // here.
                c.t.updatePieceCompletion(i)
        }
-       // If we failed to send a chunk, choke the peer to ensure they flush all their requests. We've
-       // probably dropped a piece from storage, but there's no way to communicate this to the peer. If
-       // they ask for it again, we'll kick them to allow us to send them an updated bitfield on the
-       // next connect. TODO: Support rejecting here too.
-       if c.choking {
-               c.logger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
+       // We've probably dropped a piece from storage, but there's no way to communicate this to the
+       // peer. If they ask for it again, we kick them allowing us to send them updated piece states if
+       // we reconnect. TODO: Instead, we could just try to update them with Bitfield or HaveNone and
+       // if they kick us for breaking protocol, on reconnect we will be compliant again (at least
+       // initially).
+       if c.fastEnabled() {
+               c.reject(r)
+       } else {
+               if c.choking {
+                       // If fast isn't enabled, I think we would have wiped all peer requests when we last
+                       // choked, and requests while we're choking would be ignored. It could be possible that
+                       // a peer request data read completed concurrently to it being deleted elsewhere.
+                       c.logger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
+               }
+               // Choking a non-fast peer should cause them to flush all their requests.
+               c.choke(c.write)
        }
-       c.choke(c.write)
 }
 
 func readPeerRequestData(r Request, c *PeerConn) ([]byte, error) {
@@ -989,6 +1136,12 @@ func runSafeExtraneous(f func()) {
        }
 }
 
+func (c *PeerConn) logProtocolBehaviour(level log.Level, format string, arg ...interface{}) {
+       c.logger.WithContextText(fmt.Sprintf(
+               "peer id %q, ext v %q", c.PeerID, c.PeerClientName.Load(),
+       )).SkipCallers(1).Levelf(level, format, arg...)
+}
+
 // Processes incoming BitTorrent wire-protocol messages. The client lock is held upon entry and
 // exit. Returning will end the connection.
 func (c *PeerConn) mainReadLoop() (err error) {
@@ -1004,7 +1157,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
 
        decoder := pp.Decoder{
                R:         bufio.NewReaderSize(c.r, 1<<17),
-               MaxLength: 256 * 1024,
+               MaxLength: 4 * pp.Integer(max(int64(t.chunkSize), defaultChunkSize)),
                Pool:      &t.chunkPool,
        }
        for {
@@ -1039,31 +1192,45 @@ func (c *PeerConn) mainReadLoop() (err error) {
                                break
                        }
                        if !c.fastEnabled() {
-                               c.deleteAllRequests()
+                               c.deleteAllRequests("choked by non-fast PeerConn")
                        } else {
-                               c.actualRequestState.Requests.Iterate(func(x uint32) bool {
-                                       if !c.peerAllowedFast.Contains(x / c.t.chunksPerRegularPiece()) {
-                                               c.t.pendingRequests.Dec(x)
-                                       }
-                                       return true
-                               })
+                               // We don't decrement pending requests here, let's wait for the peer to either
+                               // reject or satisfy the outstanding requests. Additionally, some peers may unchoke
+                               // us and resume where they left off, we don't want to have piled on to those chunks
+                               // in the meanwhile. I think a peer's ability to abuse this should be limited: they
+                               // could let us request a lot of stuff, then choke us and never reject, but they're
+                               // only a single peer, our chunk balancing should smooth over this abuse.
                        }
                        c.peerChoking = true
-                       // We can then reset our interest.
-                       c.updateRequests("choked")
                        c.updateExpectingChunks()
                case pp.Unchoke:
                        if !c.peerChoking {
-                               return errors.New("got unchoke but not choked")
+                               // Some clients do this for some reason. Transmission doesn't error on this, so we
+                               // won't for consistency.
+                               c.logProtocolBehaviour(log.Debug, "received unchoke when already unchoked")
+                               break
                        }
                        c.peerChoking = false
-                       c.actualRequestState.Requests.Iterate(func(x uint32) bool {
-                               if !c.peerAllowedFast.Contains(x / c.t.chunksPerRegularPiece()) {
-                                       c.t.pendingRequests.Inc(x)
+                       preservedCount := 0
+                       c.requestState.Requests.Iterate(func(x RequestIndex) bool {
+                               if !c.peerAllowedFast.Contains(c.t.pieceIndexOfRequestIndex(x)) {
+                                       preservedCount++
                                }
                                return true
                        })
-                       c.updateRequests("unchoked")
+                       if preservedCount != 0 {
+                               // TODO: Yes this is a debug log but I'm not happy with the state of the logging lib
+                               // right now.
+                               c.logger.Levelf(log.Debug,
+                                       "%v requests were preserved while being choked (fast=%v)",
+                                       preservedCount,
+                                       c.fastEnabled())
+
+                               torrent.Add("requestsPreservedThroughChoking", int64(preservedCount))
+                       }
+                       if !c.t._pendingPieces.IsEmpty() {
+                               c.updateRequests("unchoked")
+                       }
                        c.updateExpectingChunks()
                case pp.Interested:
                        c.peerInterested = true
@@ -1079,7 +1246,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
                        err = c.peerSentBitfield(msg.Bitfield)
                case pp.Request:
                        r := newRequestFromMessage(&msg)
-                       err = c.onReadRequest(r)
+                       err = c.onReadRequest(r, true)
                case pp.Piece:
                        c.doChunkReadStats(int64(len(msg.Piece)))
                        err = c.receiveChunk(&msg)
@@ -1109,35 +1276,21 @@ func (c *PeerConn) mainReadLoop() (err error) {
                        })
                case pp.Suggest:
                        torrent.Add("suggests received", 1)
-                       log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).SetLevel(log.Debug).Log(c.t.logger)
+                       log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).LogLevel(log.Debug, c.t.logger)
                        c.updateRequests("suggested")
                case pp.HaveAll:
                        err = c.onPeerSentHaveAll()
                case pp.HaveNone:
                        err = c.peerSentHaveNone()
                case pp.Reject:
-                       c.remoteRejectedRequest(c.t.requestIndexFromRequest(newRequestFromMessage(&msg)))
+                       req := newRequestFromMessage(&msg)
+                       if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req)) {
+                               c.logger.Printf("received invalid reject [request=%v, peer=%v]", req, c)
+                               err = fmt.Errorf("received invalid reject [request=%v]", req)
+                       }
                case pp.AllowedFast:
                        torrent.Add("allowed fasts received", 1)
-                       log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).SetLevel(log.Debug).Log(c.t.logger)
-                       pieceIndex := msg.Index.Int()
-                       // If we have outstanding requests that aren't currently counted toward the combined
-                       // outstanding request count, increment them.
-                       if c.peerAllowedFast.CheckedAdd(msg.Index.Uint32()) && c.peerChoking &&
-                               // The check here could be against having the info, but really what we need to know
-                               // is if there are any existing requests.
-                               !c.actualRequestState.Requests.IsEmpty() {
-
-                               i := c.actualRequestState.Requests.Iterator()
-                               i.AdvanceIfNeeded(t.pieceRequestIndexOffset(pieceIndex))
-                               for i.HasNext() {
-                                       r := i.Next()
-                                       if r >= t.pieceRequestIndexOffset(pieceIndex+1) {
-                                               break
-                                       }
-                                       c.t.pendingRequests.Inc(r)
-                               }
-                       }
+                       log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).LogLevel(log.Debug, c.t.logger)
                        c.updateRequests("PeerConn.mainReadLoop allowed fast")
                case pp.Extended:
                        err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
@@ -1150,13 +1303,18 @@ func (c *PeerConn) mainReadLoop() (err error) {
        }
 }
 
-func (c *Peer) remoteRejectedRequest(r RequestIndex) {
+// Returns true if it was valid to reject the request.
+func (c *Peer) remoteRejectedRequest(r RequestIndex) bool {
        if c.deleteRequest(r) {
-               if c.actualRequestState.Requests.GetCardinality() == 0 {
-                       c.updateRequests("Peer.remoteRejectedRequest")
-               }
-               c.decExpectedChunkReceive(r)
+               c.decPeakRequests()
+       } else if !c.requestState.Cancelled.CheckedRemove(r) {
+               return false
+       }
+       if c.isLowOnRequests() {
+               c.updateRequests("Peer.remoteRejectedRequest")
        }
+       c.decExpectedChunkReceive(r)
+       return true
 }
 
 func (c *Peer) decExpectedChunkReceive(r RequestIndex) {
@@ -1193,11 +1351,11 @@ func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err
                if cb := c.callbacks.ReadExtendedHandshake; cb != nil {
                        cb(c, &d)
                }
-               //c.logger.WithDefaultLevel(log.Debug).Printf("received extended handshake message:\n%s", spew.Sdump(d))
+               // c.logger.WithDefaultLevel(log.Debug).Printf("received extended handshake message:\n%s", spew.Sdump(d))
                if d.Reqq != 0 {
                        c.PeerMaxRequests = d.Reqq
                }
-               c.PeerClientName = d.V
+               c.PeerClientName.Store(d.V)
                if c.PeerExtensionIDs == nil {
                        c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber, len(d.M))
                }
@@ -1205,7 +1363,11 @@ func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err
                c.PeerPrefersEncryption = d.Encryption
                for name, id := range d.M {
                        if _, ok := c.PeerExtensionIDs[name]; !ok {
-                               peersSupportingExtension.Add(string(name), 1)
+                               peersSupportingExtension.Add(
+                                       // expvar.Var.String must produce valid JSON. "ut_payme\xeet_address" was being
+                                       // entered here which caused problems later when unmarshalling.
+                                       strconv.Quote(string(name)),
+                                       1)
                        }
                        c.PeerExtensionIDs[name] = id
                }
@@ -1260,6 +1422,11 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
 
        ppReq := newRequestFromMessage(msg)
        req := c.t.requestIndexFromRequest(ppReq)
+       t := c.t
+
+       if c.bannableAddr.Ok {
+               t.smartBanCache.RecordBlock(c.bannableAddr.Value, req, msg.Piece)
+       }
 
        if c.peerChoking {
                chunksReceived.Add("while choked", 1)
@@ -1271,37 +1438,40 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
        }
        c.decExpectedChunkReceive(req)
 
-       if c.peerChoking && c.peerAllowedFast.Contains(bitmap.BitIndex(ppReq.Index)) {
+       if c.peerChoking && c.peerAllowedFast.Contains(pieceIndex(ppReq.Index)) {
                chunksReceived.Add("due to allowed fast", 1)
        }
 
        // The request needs to be deleted immediately to prevent cancels occurring asynchronously when
        // have actually already received the piece, while we have the Client unlocked to write the data
        // out.
-       deletedRequest := false
+       intended := false
        {
-               if c.actualRequestState.Requests.Contains(req) {
+               if c.requestState.Requests.Contains(req) {
                        for _, f := range c.callbacks.ReceivedRequested {
                                f(PeerMessageEvent{c, msg})
                        }
                }
                // Request has been satisfied.
-               if c.deleteRequest(req) {
-                       deletedRequest = true
+               if c.deleteRequest(req) || c.requestState.Cancelled.CheckedRemove(req) {
+                       intended = true
                        if !c.peerChoking {
                                c._chunksReceivedWhileExpecting++
                        }
+                       if c.isLowOnRequests() {
+                               c.updateRequests("Peer.receiveChunk deleted request")
+                       }
                } else {
-                       chunksReceived.Add("unwanted", 1)
+                       chunksReceived.Add("unintended", 1)
                }
        }
 
-       t := c.t
        cl := t.cl
 
        // Do we actually want this chunk?
        if t.haveChunk(ppReq) {
-               chunksReceived.Add("wasted", 1)
+               // panic(fmt.Sprintf("%+v", ppReq))
+               chunksReceived.Add("redundant", 1)
                c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadWasted }))
                return nil
        }
@@ -1310,11 +1480,8 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
 
        c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadUseful }))
        c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulData }))
-       if deletedRequest {
+       if intended {
                c.piecesReceivedSinceLastRequestUpdate++
-               if c.actualRequestState.Requests.GetCardinality() == 0 {
-                       c.updateRequests("Peer.receiveChunk deleted request")
-               }
                c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulIntendedData }))
        }
        for _, f := range c.t.cl.config.Callbacks.ReceivedUsefulData {
@@ -1330,12 +1497,12 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
        piece.unpendChunkIndex(chunkIndexFromChunkSpec(ppReq.ChunkSpec, t.chunkSize))
 
        // Cancel pending requests for this chunk from *other* peers.
-       t.iterPeers(func(p *Peer) {
+       if p := t.requestingPeer(req); p != nil {
                if p == c {
-                       return
+                       panic("should not be pending request from conn that just received it")
                }
                p.cancel(req)
-       })
+       }
 
        err := func() error {
                cl.unlock()
@@ -1462,47 +1629,78 @@ func (cn *PeerConn) drop() {
        cn.t.dropConnection(cn)
 }
 
+func (cn *PeerConn) ban() {
+       cn.t.cl.banPeerIP(cn.remoteIp())
+}
+
 func (cn *Peer) netGoodPiecesDirtied() int64 {
        return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64()
 }
 
 func (c *Peer) peerHasWantedPieces() bool {
-       if c.peerSentHaveAll {
-               return !c.t.haveAllPieces()
+       if all, _ := c.peerHasAllPieces(); all {
+               return !c.t.haveAllPieces() && !c.t._pendingPieces.IsEmpty()
        }
        if !c.t.haveInfo() {
-               return c._peerPieces.GetCardinality() != 0
+               return !c.peerPieces().IsEmpty()
        }
-       return c._peerPieces.Intersects(
-               roaring.FlipInt(&c.t._completedPieces, 0, c.t.numPieces()),
-       )
+       return c.peerPieces().Intersects(&c.t._pendingPieces)
 }
 
+// Returns true if an outstanding request is removed. Cancelled requests should be handled
+// separately.
 func (c *Peer) deleteRequest(r RequestIndex) bool {
-       if !c.actualRequestState.Requests.CheckedRemove(r) {
+       if !c.requestState.Requests.CheckedRemove(r) {
                return false
        }
        for _, f := range c.callbacks.DeletedRequest {
                f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)})
        }
        c.updateExpectingChunks()
-       if !c.peerChoking || c.peerAllowedFast.Contains(r/c.t.chunksPerRegularPiece()) {
-               c.t.pendingRequests.Dec(r)
-       }
+       if c.t.requestingPeer(r) != c {
+               panic("only one peer should have a given request at a time")
+       }
+       delete(c.t.requestState, r)
+       // c.t.iterPeers(func(p *Peer) {
+       //      if p.isLowOnRequests() {
+       //              p.updateRequests("Peer.deleteRequest")
+       //      }
+       // })
        return true
 }
 
-func (c *Peer) deleteAllRequests() {
-       c.actualRequestState.Requests.Clone().Iterate(func(x uint32) bool {
-               c.deleteRequest(x)
+func (c *Peer) deleteAllRequests(reason string) {
+       if c.requestState.Requests.IsEmpty() {
+               return
+       }
+       c.requestState.Requests.IterateSnapshot(func(x RequestIndex) bool {
+               if !c.deleteRequest(x) {
+                       panic("request should exist")
+               }
                return true
        })
-       if !c.actualRequestState.Requests.IsEmpty() {
-               panic(c.actualRequestState.Requests.GetCardinality())
+       c.assertNoRequests()
+       c.t.iterPeers(func(p *Peer) {
+               if p.isLowOnRequests() {
+                       p.updateRequests(reason)
+               }
+       })
+       return
+}
+
+func (c *Peer) assertNoRequests() {
+       if !c.requestState.Requests.IsEmpty() {
+               panic(c.requestState.Requests.GetCardinality())
        }
-       // for c := range c.t.conns {
-       //      c.tickleWriter()
-       // }
+}
+
+func (c *Peer) cancelAllRequests() {
+       c.requestState.Requests.IterateSnapshot(func(x RequestIndex) bool {
+               c.cancel(x)
+               return true
+       })
+       c.assertNoRequests()
+       return
 }
 
 // This is called when something has changed that should wake the writer, such as putting stuff into
@@ -1579,11 +1777,11 @@ func (c *PeerConn) dialAddr() PeerRemoteAddr {
 func (c *PeerConn) pexEvent(t pexEventType) pexEvent {
        f := c.pexPeerFlags()
        addr := c.dialAddr()
-       return pexEvent{t, addr, f}
+       return pexEvent{t, addr, f, nil}
 }
 
 func (c *PeerConn) String() string {
-       return fmt.Sprintf("connection %p", c)
+       return fmt.Sprintf("%T %p [id=%q, exts=%v, v=%q]", c, c, c.PeerID, c.PeerExtensionBytes, c.PeerClientName.Load())
 }
 
 func (c *Peer) trust() connectionTrust {
@@ -1610,8 +1808,8 @@ func (cn *PeerConn) PeerPieces() *roaring.Bitmap {
 // Returns a new Bitmap that includes bits for all pieces the peer could have based on their claims.
 func (cn *Peer) newPeerPieces() *roaring.Bitmap {
        // TODO: Can we use copy on write?
-       ret := cn._peerPieces.Clone()
-       if cn.peerSentHaveAll {
+       ret := cn.peerPieces().Clone()
+       if all, _ := cn.peerHasAllPieces(); all {
                if cn.t.haveInfo() {
                        ret.AddRange(0, bitmap.BitRange(cn.t.numPieces()))
                } else {
@@ -1630,6 +1828,10 @@ func (p *Peer) TryAsPeerConn() (*PeerConn, bool) {
        return pc, ok
 }
 
-func (p *PeerConn) onNextRequestStateChanged() {
-       p.tickleWriter()
+func (p *Peer) uncancelledRequests() uint64 {
+       return p.requestState.Requests.GetCardinality()
+}
+
+func (pc *PeerConn) remoteIsTransmission() bool {
+       return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
 }