From: YenForYang <YenForYang@users.noreply.github.com>
Date: Sat, 4 Sep 2021 06:30:14 +0000 (-0500)
Subject: Drop xerrors and reflection dependency
X-Git-Tag: v1.32.0~101
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=99ba75f45818b82384b8980dd772e7c074ad4798;p=btrtrc.git

Drop xerrors and reflection dependency

Nothing wrong with missinggo.CopyExact -- but fewer dependencies is better IMO.  Also changed String() to use a consistent receiver name -- not a big deal.
---

diff --git a/peer_protocol/handshake.go b/peer_protocol/handshake.go
index 5e6f123d..ba81a0cf 100644
--- a/peer_protocol/handshake.go
+++ b/peer_protocol/handshake.go
@@ -2,12 +2,11 @@ package peer_protocol
 
 import (
 	"encoding/hex"
+	"errors"
 	"fmt"
 	"io"
+	"strconv"
 
-	"golang.org/x/xerrors"
-
-	"github.com/anacrolix/missinggo/v2"
 	"github.com/anacrolix/torrent/metainfo"
 )
 
@@ -34,8 +33,8 @@ type (
 	PeerExtensionBits [8]byte
 )
 
-func (me PeerExtensionBits) String() string {
-	return hex.EncodeToString(me[:])
+func (pex PeerExtensionBits) String() string {
+	return hex.EncodeToString(pex[:])
 }
 
 func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) {
@@ -98,7 +97,7 @@ func Handshake(
 		// Wait until writes complete before returning from handshake.
 		err = <-writeDone
 		if err != nil {
-			err = fmt.Errorf("error writing: %s", err)
+			err = fmt.Errorf("error writing: %w", err)
 		}
 	}()
 
@@ -119,16 +118,21 @@ func Handshake(
 	var b [68]byte
 	_, err = io.ReadFull(sock, b[:68])
 	if err != nil {
-		err = xerrors.Errorf("while reading: %w", err)
-		return
+		return res, fmt.Errorf("while reading: %w", err)
 	}
 	if string(b[:20]) != Protocol {
-		err = xerrors.Errorf("unexpected protocol string")
-		return
+		return res, errors.New("unexpected protocol string")
+	}
+
+	copyExact := func(dst []byte, src []byte) {
+		if dstLen, srcLen := uint64(len(dst)), uint64(len(src)); dstLen != srcLen {
+			panic("dst len " + strconv.FormatUint(dstLen,10) + " != src len " + strconv.FormatUint(srcLen,10))
+		}
+		copy(dst, src)
 	}
-	missinggo.CopyExact(&res.PeerExtensionBits, b[20:28])
-	missinggo.CopyExact(&res.Hash, b[28:48])
-	missinggo.CopyExact(&res.PeerID, b[48:68])
+	copyExact(res.PeerExtensionBits[:], b[20:28])
+	copyExact(res.Hash[:], b[28:48])
+	copyExact(res.PeerID[:], b[48:68])
 	// peerExtensions.Add(res.PeerExtensionBits.String(), 1)
 
 	// TODO: Maybe we can just drop peers here if we're not interested. This