Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <protocol.h>
7 
8 #include <util.h>
9 #include <utilstrencodings.h>
10 
11 #ifndef WIN32
12 # include <arpa/inet.h>
13 #endif
14 
15 static std::atomic<bool> g_initial_block_download_completed(false);
16 
17 namespace NetMsgType {
18 const char *VERSION="version";
19 const char *VERACK="verack";
20 const char *ADDR="addr";
21 const char *INV="inv";
22 const char *GETDATA="getdata";
23 const char *MERKLEBLOCK="merkleblock";
24 const char *GETBLOCKS="getblocks";
25 const char *GETHEADERS="getheaders";
26 const char *TX="tx";
27 const char *HEADERS="headers";
28 const char *BLOCK="block";
29 const char *GETADDR="getaddr";
30 const char *MEMPOOL="mempool";
31 const char *PING="ping";
32 const char *PONG="pong";
33 const char *NOTFOUND="notfound";
34 const char *FILTERLOAD="filterload";
35 const char *FILTERADD="filteradd";
36 const char *FILTERCLEAR="filterclear";
37 const char *REJECT="reject";
38 const char *SENDHEADERS="sendheaders";
39 const char *SENDCMPCT="sendcmpct";
40 const char *CMPCTBLOCK="cmpctblock";
41 const char *GETBLOCKTXN="getblocktxn";
42 const char *BLOCKTXN="blocktxn";
43 // Dash message types
44 const char *LEGACYTXLOCKREQUEST="ix";
45 const char *SPORK="spork";
46 const char *GETSPORKS="getsporks";
47 const char *DSACCEPT="dsa";
48 const char *DSVIN="dsi";
49 const char *DSFINALTX="dsf";
50 const char *DSSIGNFINALTX="dss";
51 const char *DSCOMPLETE="dsc";
52 const char *DSSTATUSUPDATE="dssu";
53 const char *DSTX="dstx";
54 const char *DSQUEUE="dsq";
55 const char *SENDDSQUEUE="senddsq";
56 const char *SYNCSTATUSCOUNT="ssc";
57 const char *MNGOVERNANCESYNC="govsync";
58 const char *MNGOVERNANCEOBJECT="govobj";
59 const char *MNGOVERNANCEOBJECTVOTE="govobjvote";
60 const char *GETMNLISTDIFF="getmnlistd";
61 const char *MNLISTDIFF="mnlistdiff";
62 const char *QSENDRECSIGS="qsendrecsigs";
63 const char *QFCOMMITMENT="qfcommit";
64 const char *QCONTRIB="qcontrib";
65 const char *QCOMPLAINT="qcomplaint";
66 const char *QJUSTIFICATION="qjustify";
67 const char *QPCOMMITMENT="qpcommit";
68 const char *QWATCH="qwatch";
69 const char *QSIGSESANN="qsigsesann";
70 const char *QSIGSHARESINV="qsigsinv";
71 const char *QGETSIGSHARES="qgetsigs";
72 const char *QBSIGSHARES="qbsigs";
73 const char *QSIGREC="qsigrec";
74 const char *QSIGSHARE="qsigshare";
75 const char *CLSIG="clsig";
76 const char *ISLOCK="islock";
77 const char *MNAUTH="mnauth";
78 }; // namespace NetMsgType
79 
83 const static std::string allNetMessageTypes[] = {
109  // Dash message types
110  // NOTE: do NOT include non-implmented here, we want them to be "Unknown command" in ProcessMessage()
145 };
147 
148 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
149 {
150  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
151  memset(pchCommand, 0, sizeof(pchCommand));
152  nMessageSize = -1;
153  memset(pchChecksum, 0, CHECKSUM_SIZE);
154 }
155 
156 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
157 {
158  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
159  memset(pchCommand, 0, sizeof(pchCommand));
160  strncpy(pchCommand, pszCommand, COMMAND_SIZE);
161  nMessageSize = nMessageSizeIn;
162  memset(pchChecksum, 0, CHECKSUM_SIZE);
163 }
164 
165 std::string CMessageHeader::GetCommand() const
166 {
167  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
168 }
169 
170 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
171 {
172  // Check start string
173  if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
174  return false;
175 
176  // Check the command string for errors
177  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
178  {
179  if (*p1 == 0)
180  {
181  // Must be all zeros after the first zero
182  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
183  if (*p1 != 0)
184  return false;
185  }
186  else if (*p1 < ' ' || *p1 > 0x7E)
187  return false;
188  }
189 
190  // Message size
191  if (nMessageSize > MAX_SIZE)
192  {
193  LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
194  return false;
195  }
196 
197  return true;
198 }
199 
200 
204  }
205  return ServiceFlags(NODE_NETWORK);
206 }
207 
208 void SetServiceFlagsIBDCache(bool state) {
210 }
211 
212 
214 {
215  Init();
216 }
217 
219 {
220  Init();
221  nServices = nServicesIn;
222 }
223 
225 {
227  nTime = 100000000;
228 }
229 
231 {
232  type = 0;
233  hash.SetNull();
234 }
235 
236 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
237 
238 bool operator<(const CInv& a, const CInv& b)
239 {
240  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
241 }
242 
243 bool CInv::IsKnownType() const
244 {
245  return GetCommandInternal() != nullptr;
246 }
247 
248 const char* CInv::GetCommandInternal() const
249 {
250  switch (type)
251  {
252  case MSG_TX: return NetMsgType::TX;
253  case MSG_BLOCK: return NetMsgType::BLOCK;
257  case MSG_SPORK: return NetMsgType::SPORK;
258  case MSG_DSTX: return NetMsgType::DSTX;
267  case MSG_CLSIG: return NetMsgType::CLSIG;
268  case MSG_ISLOCK: return NetMsgType::ISLOCK;
269  default:
270  return nullptr;
271  }
272 }
273 
274 std::string CInv::GetCommand() const
275 {
276  auto cmd = GetCommandInternal();
277  if (cmd == nullptr) {
278  throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
279  }
280  return cmd;
281 }
282 
283 std::string CInv::ToString() const
284 {
285  auto cmd = GetCommandInternal();
286  if (!cmd) {
287  return strprintf("0x%08x %s", type, hash.ToString());
288  } else {
289  return strprintf("%s %s", cmd, hash.ToString());
290  }
291 }
292 
293 const std::vector<std::string> &getAllNetMessageTypes()
294 {
295  return allNetMessageTypesVec;
296 }
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:31
const char * QBSIGSHARES
Definition: protocol.cpp:72
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:34
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:23
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:60
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
static const std::string allNetMessageTypes[]
All known message types.
Definition: protocol.cpp:83
ServiceFlags
nServices flags
Definition: protocol.h:280
void SetNull()
Definition: uint256.h:41
const char * DSACCEPT
Definition: protocol.cpp:47
const char * QPCOMMITMENT
Definition: protocol.cpp:67
std::string GetCommand() const
Definition: protocol.cpp:274
std::string ToString() const
Definition: protocol.cpp:283
const char * QFCOMMITMENT
Definition: protocol.cpp:63
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:29
void Init()
Definition: protocol.cpp:224
Defined in BIP152.
Definition: protocol.h:415
#define strprintf
Definition: tinyformat.h:1066
inv message data
Definition: protocol.h:429
const char * QSIGSESANN
Definition: protocol.cpp:69
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:39
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:58
const char * DSVIN
Definition: protocol.cpp:48
static const std::vector< std::string > allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes))
const char * DSSTATUSUPDATE
Definition: protocol.cpp:52
void SetServiceFlagsIBDCache(bool state)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:208
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:148
const char * GetCommandInternal() const
Definition: protocol.cpp:248
uint32_t nMessageSize
Definition: protocol.h:59
const char * QSIGSHARE
Definition: protocol.cpp:74
std::string GetCommand() const
Definition: protocol.cpp:165
const char * PONG
The pong message replies to a ping message, proving to the pinging node that the ponging node is stil...
Definition: protocol.cpp:32
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:170
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:27
const char * QJUSTIFICATION
Definition: protocol.cpp:66
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:21
const char * DSFINALTX
Definition: protocol.cpp:49
static std::atomic< bool > g_initial_block_download_completed(false)
const char * ISLOCK
Definition: protocol.cpp:76
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:25
#define LogPrintf(...)
Definition: util.h:203
const char * MNLISTDIFF
Definition: protocol.cpp:61
const char * DSCOMPLETE
Definition: protocol.cpp:51
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:32
Bitcoin protocol message types.
Definition: protocol.cpp:17
int type
Definition: protocol.h:455
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
const char * LEGACYTXLOCKREQUEST
Definition: protocol.cpp:44
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:38
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:30
const char * GETSPORKS
Definition: protocol.cpp:46
bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:238
const char * QSIGREC
Definition: protocol.cpp:73
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:293
uint256 hash
Definition: protocol.h:456
const char * GETMNLISTDIFF
Definition: protocol.cpp:60
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:20
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:36
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:34
std::string ToString() const
Definition: uint256.cpp:62
const char * MNGOVERNANCESYNC
Definition: protocol.cpp:57
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:33
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:28
const char * DSSIGNFINALTX
Definition: protocol.cpp:50
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected...
Definition: protocol.cpp:37
const char * QSIGSHARESINV
Definition: protocol.cpp:70
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:31
const char * SYNCSTATUSCOUNT
Definition: protocol.cpp:56
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:24
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
const char * QSENDRECSIGS
Definition: protocol.cpp:62
256-bit opaque blob.
Definition: uint256.h:123
unsigned int nTime
Definition: protocol.h:390
ServiceFlags nServices
Definition: protocol.h:387
const char * QGETSIGSHARES
Definition: protocol.cpp:71
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:40
#define ARRAYLEN(array)
const char * QCOMPLAINT
Definition: protocol.cpp:65
static const unsigned int MAX_SIZE
Definition: serialize.h:29
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
const char * DSQUEUE
Definition: protocol.cpp:54
void * memcpy(void *a, const void *b, size_t c)
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:201
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:22
bool IsKnownType() const
Definition: protocol.cpp:243
const char * DSTX
Definition: protocol.cpp:53
const char * MNAUTH
Definition: protocol.cpp:77
const char * QCONTRIB
Definition: protocol.cpp:64
CInv()
Definition: protocol.cpp:230
const char * CLSIG
Definition: protocol.cpp:75
const char * MNGOVERNANCEOBJECT
Definition: protocol.cpp:58
const char * MNGOVERNANCEOBJECTVOTE
Definition: protocol.cpp:59
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
const char * QWATCH
Definition: protocol.cpp:68
const char * SPORK
Definition: protocol.cpp:45
const char * SENDDSQUEUE
Definition: protocol.cpp:55
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:57
Defined in BIP37.
Definition: protocol.h:402
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:35
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:41
Released under the MIT license