From: Matt Joiner Date: Sat, 6 Jan 2018 04:50:45 +0000 (+1100) Subject: Make peerID a public type X-Git-Tag: v1.0.0~304 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=e13b0eccbfaeecbbc68d9131b47922a0c671718f;p=btrtrc.git Make peerID a public type Wanted it applied to Client Status output --- diff --git a/client.go b/client.go index d6da9ed1..7fb7d501 100644 --- a/client.go +++ b/client.go @@ -43,7 +43,7 @@ type Client struct { config Config halfOpenLimit int - peerID peerID + peerID PeerID defaultStorage *storage.Client onClose []func() tcpListener net.Listener @@ -90,7 +90,7 @@ func (cl *Client) SetIPBlockList(list iplist.Ranger) { } } -func (cl *Client) PeerID() [20]byte { +func (cl *Client) PeerID() PeerID { return cl.peerID } @@ -794,7 +794,7 @@ func (cl *Client) connBTHandshake(c *connection, ih *metainfo.Hash) (ret metainf } ret = res.Hash c.PeerExtensionBytes = res.peerExtensionBytes - c.PeerID = res.peerID + c.PeerID = res.PeerID c.completedHandshake = time.Now() return } diff --git a/connection.go b/connection.go index 1c2a24fe..60cadb20 100644 --- a/connection.go +++ b/connection.go @@ -73,7 +73,7 @@ type connection struct { sentHaves []bool // Stuff controlled by the remote peer. - PeerID peerID + PeerID PeerID PeerInterested bool PeerChoked bool PeerRequests map[request]struct{} diff --git a/handshake.go b/handshake.go index 76581517..60a54620 100644 --- a/handshake.go +++ b/handshake.go @@ -60,7 +60,7 @@ func (pex peerExtensionBytes) GetBit(bit ExtensionBit) bool { type handshakeResult struct { peerExtensionBytes - peerID + PeerID metainfo.Hash } @@ -116,7 +116,7 @@ func handshake(sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extension } missinggo.CopyExact(&res.peerExtensionBytes, b[20:28]) missinggo.CopyExact(&res.Hash, b[28:48]) - missinggo.CopyExact(&res.peerID, b[48:68]) + missinggo.CopyExact(&res.PeerID, b[48:68]) peerExtensions.Add(hex.EncodeToString(res.peerExtensionBytes[:]), 1) // TODO: Maybe we can just drop peers here if we're not interested. This diff --git a/peerid.go b/peerid.go index 9d8bf1cc..c9e3f4cd 100644 --- a/peerid.go +++ b/peerid.go @@ -4,9 +4,9 @@ import ( "encoding/hex" ) -type peerID [20]byte +type PeerID [20]byte -func (me peerID) String() string { +func (me PeerID) String() string { if me[0] == '-' && me[7] == '-' { return string(me[:8]) + hex.EncodeToString(me[8:]) } diff --git a/peerid_test.go b/peerid_test.go index 3d89ef4b..965a28ab 100644 --- a/peerid_test.go +++ b/peerid_test.go @@ -1,6 +1,7 @@ package torrent import ( + "fmt" "testing" "github.com/anacrolix/missinggo" @@ -15,8 +16,9 @@ func TestPeerIdString(t *testing.T) { {"\x1cNJ}\x9c\xc7\xc4o\x94<\x9b\x8c\xc2!I\x1c\a\xec\x98n", "1c4e4a7d9cc7c46f943c9b8cc221491c07ec986e"}, {"-FD51W\xe4-LaZMk0N8ZLA7", "-FD51W\xe4-4c615a4d6b304e385a4c4137"}, } { - var pi peerID + var pi PeerID missinggo.CopyExact(&pi, _case.id) assert.EqualValues(t, _case.s, pi.String()) + assert.EqualValues(t, fmt.Sprintf("%q", _case.s), fmt.Sprintf("%q", pi)) } } diff --git a/torrent_test.go b/torrent_test.go index 00f59498..39575dcc 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -183,7 +183,7 @@ func TestTorrentMetainfoIncompleteMetadata(t *testing.T) { require.NoError(t, err) assert.True(t, ok) assert.True(t, hr.peerExtensionBytes.GetBit(ExtensionBitExtended)) - assert.EqualValues(t, cl.PeerID(), hr.peerID) + assert.EqualValues(t, cl.PeerID(), hr.PeerID) assert.Equal(t, ih, hr.Hash) assert.EqualValues(t, 0, tt.metadataSize())