From f9bd92d235a187a9bfea4c956bf74e2e08cebb46 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 4 Oct 2016 11:11:21 +0000 Subject: [PATCH 1/3] version.h: s/shord/short/ in comment --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 68ccd6d378..87bd655066 100644 --- a/src/version.h +++ b/src/version.h @@ -39,7 +39,7 @@ static const int SENDHEADERS_VERSION = 70012; //! "feefilter" tells peers to filter invs to you by fee starts with this version static const int FEEFILTER_VERSION = 70013; -//! shord-id-based block download starts with this version +//! short-id-based block download starts with this version static const int SHORT_IDS_BLOCKS_VERSION = 70014; #endif // BITCOIN_VERSION_H From 2c09a5209ab00573a2422e1e65c437a6e2f59624 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 4 Oct 2016 11:12:23 +0000 Subject: [PATCH 2/3] protocol.h: Move MESSAGE_START_SIZE into CMessageHeader Also move the enum to the top, and remove a deceptive TODO comment. --- src/main.cpp | 6 +++--- src/protocol.h | 23 ++++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 437e972382..9b66aad429 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4358,11 +4358,11 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskB unsigned int nSize = 0; try { // locate a header - unsigned char buf[MESSAGE_START_SIZE]; + unsigned char buf[CMessageHeader::MESSAGE_START_SIZE]; blkdat.FindByte(chainparams.MessageStart()[0]); nRewind = blkdat.GetPos()+1; blkdat >> FLATDATA(buf); - if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE)) + if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) continue; // read size blkdat >> nSize; @@ -6232,7 +6232,7 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman) it++; // Scan for message start - if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), MESSAGE_START_SIZE) != 0) { + if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) { LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id); fOk = false; break; diff --git a/src/protocol.h b/src/protocol.h index 1bc1c25b37..cdb76b9038 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -18,8 +18,6 @@ #include #include -#define MESSAGE_START_SIZE 4 - /** Message header. * (4) message start. * (12) command. @@ -29,6 +27,16 @@ class CMessageHeader { public: + enum { + MESSAGE_START_SIZE = 4, + COMMAND_SIZE = 12, + MESSAGE_SIZE_SIZE = 4, + CHECKSUM_SIZE = 4, + + MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE, + CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE, + HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE + }; typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; CMessageHeader(const MessageStartChars& pchMessageStartIn); @@ -48,17 +56,6 @@ public: READWRITE(FLATDATA(pchChecksum)); } - // TODO: make private (improves encapsulation) -public: - enum { - COMMAND_SIZE = 12, - MESSAGE_SIZE_SIZE = 4, - CHECKSUM_SIZE = 4, - - MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE, - CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE, - HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE - }; char pchMessageStart[MESSAGE_START_SIZE]; char pchCommand[COMMAND_SIZE]; uint32_t nMessageSize; From 1df311118d79c04df1d41e044b19444cfda015da Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 4 Oct 2016 17:55:12 +0200 Subject: [PATCH 3/3] protocol.h: Make enums in GetDataMsg concrete values This concretizes the numbers and adds a comment to make it clear that these numbers are fixed by the protocol, and may avoid people forgetting to claim numbers in the future (e.g. issue #8500). Also gets rid of a weird unused `MSG_TYPE_MAX` in the middle of the enumeration (thanks @paveljanik for noticing). --- src/protocol.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/protocol.h b/src/protocol.h index cdb76b9038..d19e0d3a5e 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -312,20 +312,24 @@ public: unsigned int nTime; }; -/** getdata message types */ +/** getdata message type flags */ const uint32_t MSG_WITNESS_FLAG = 1 << 30; const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2; + +/** getdata / inv message types. + * These numbers are defined by the protocol. When adding a new value, be sure + * to mention it in the respective BIP. + */ enum GetDataMsg { UNDEFINED = 0, - MSG_TX, - MSG_BLOCK, - MSG_TYPE_MAX = MSG_BLOCK, + MSG_TX = 1, + MSG_BLOCK = 2, // The following can only occur in getdata. Invs always use TX or BLOCK. - MSG_FILTERED_BLOCK, - MSG_CMPCT_BLOCK, - MSG_WITNESS_BLOCK = MSG_BLOCK | MSG_WITNESS_FLAG, - MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG, + MSG_FILTERED_BLOCK = 3, //!< Defined in BIP37 + MSG_CMPCT_BLOCK = 4, //!< Defined in BIP152 + MSG_WITNESS_BLOCK = MSG_BLOCK | MSG_WITNESS_FLAG, //!< Defined in BIP144 + MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG, //!< Defined in BIP144 MSG_FILTERED_WITNESS_BLOCK = MSG_FILTERED_BLOCK | MSG_WITNESS_FLAG, };