From 54f43419e2fda72c5263824683defd067e49ff54 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 5 Feb 2018 18:00:47 +1100 Subject: [PATCH] Fix fast extension message type constants iota isn't worth the trouble --- peer_protocol/messagetype_string.go | 11 ++++++--- peer_protocol/protocol.go | 36 ++++++++++++++++------------- peer_protocol/protocol_test.go | 8 +++---- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/peer_protocol/messagetype_string.go b/peer_protocol/messagetype_string.go index a098b5c3..b543cd52 100644 --- a/peer_protocol/messagetype_string.go +++ b/peer_protocol/messagetype_string.go @@ -6,19 +6,24 @@ import "strconv" const ( _MessageType_name_0 = "ChokeUnchokeInterestedNotInterestedHaveBitfieldRequestPieceCancelPort" - _MessageType_name_1 = "Suggest" + _MessageType_name_1 = "SuggestHaveAllHaveNoneRejectAllowedFast" + _MessageType_name_2 = "Extended" ) var ( _MessageType_index_0 = [...]uint8{0, 5, 12, 22, 35, 39, 47, 54, 59, 65, 69} + _MessageType_index_1 = [...]uint8{0, 7, 14, 22, 28, 39} ) func (i MessageType) String() string { switch { case 0 <= i && i <= 9: return _MessageType_name_0[_MessageType_index_0[i]:_MessageType_index_0[i+1]] - case i == 23: - return _MessageType_name_1 + case 13 <= i && i <= 17: + i -= 13 + return _MessageType_name_1[_MessageType_index_1[i]:_MessageType_index_1[i+1]] + case i == 20: + return _MessageType_name_2 default: return "MessageType(" + strconv.FormatInt(int64(i), 10) + ")" } diff --git a/peer_protocol/protocol.go b/peer_protocol/protocol.go index 67aa5a61..cf3e6e3d 100644 --- a/peer_protocol/protocol.go +++ b/peer_protocol/protocol.go @@ -13,26 +13,30 @@ func (mt MessageType) FastExtension() bool { } const ( - Choke MessageType = iota - Unchoke // 1 - Interested // 2 - NotInterested // 3 - Have // 4 - Bitfield // 5 - Request // 6 - Piece // 7 - Cancel // 8 - Port // 9 + // BEP 3 + Choke MessageType = 0 + Unchoke MessageType = 1 + Interested MessageType = 2 + NotInterested MessageType = 3 + Have MessageType = 4 + Bitfield MessageType = 5 + Request MessageType = 6 + Piece MessageType = 7 + Cancel MessageType = 8 + Port MessageType = 9 // BEP 6 - Suggest MessageType = iota + 0xd // 13 - HaveAll = 0xe // 14 - HaveNone = 0xf // 15 - Reject = 0x10 // 16 - AllowedFast = 0x11 // 17 + Suggest MessageType = 0x0d // 13 + HaveAll MessageType = 0x0e // 14 + HaveNone MessageType = 0x0f // 15 + Reject MessageType = 0x10 // 16 + AllowedFast MessageType = 0x11 // 17 - Extended = 20 + // BEP 10 + Extended MessageType = 20 +) +const ( HandshakeExtendedID = 0 RequestMetadataExtensionMsgType = 0 diff --git a/peer_protocol/protocol_test.go b/peer_protocol/protocol_test.go index 580d26b2..eabbd545 100644 --- a/peer_protocol/protocol_test.go +++ b/peer_protocol/protocol_test.go @@ -5,6 +5,8 @@ import ( "bytes" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestBinaryReadSliceOfPointers(t *testing.T) { @@ -25,10 +27,8 @@ func TestBinaryReadSliceOfPointers(t *testing.T) { } func TestConstants(t *testing.T) { - // check that iota works as expected in the const block - if NotInterested != 3 { - t.FailNow() - } + assert.EqualValues(t, 3, NotInterested) + assert.EqualValues(t, 14, HaveAll) } func TestBitfieldEncode(t *testing.T) {