PIVX Core  5.6.99
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2021 The Dash Core developers
4 // Copyright (c) 2017-2022 The PIVX Core developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #include "protocol.h"
9 
10 #include "util/system.h"
11 #include "utilstrencodings.h"
12 
13 #ifndef WIN32
14 #include <arpa/inet.h>
15 #endif
16 
17 namespace NetMsgType
18 {
19 const char* VERSION = "version";
20 const char* VERACK = "verack";
21 const char* ADDR = "addr";
22 const char* ADDRV2="addrv2";
23 const char* SENDADDRV2="sendaddrv2";
24 const char* INV = "inv";
25 const char* GETDATA = "getdata";
26 const char* MERKLEBLOCK = "merkleblock";
27 const char* GETBLOCKS = "getblocks";
28 const char* GETHEADERS = "getheaders";
29 const char* TX = "tx";
30 const char* HEADERS = "headers";
31 const char* BLOCK = "block";
32 const char* GETADDR = "getaddr";
33 const char* MEMPOOL = "mempool";
34 const char* PING = "ping";
35 const char* PONG = "pong";
36 const char* ALERT = "alert";
37 const char* NOTFOUND = "notfound";
38 const char* FILTERLOAD = "filterload";
39 const char* FILTERADD = "filteradd";
40 const char* FILTERCLEAR = "filterclear";
41 const char* SENDHEADERS = "sendheaders";
42 const char* SPORK = "spork";
43 const char* GETSPORKS = "getsporks";
44 const char* MNBROADCAST = "mnb";
45 const char* MNBROADCAST2 = "mnb2"; // BIP155 support
46 const char* MNPING = "mnp";
47 const char* MNWINNER = "mnw";
48 const char* GETMNWINNERS = "mnget";
49 const char* BUDGETPROPOSAL = "mprop";
50 const char* BUDGETVOTE = "mvote";
51 const char* BUDGETVOTESYNC = "mnvs";
52 const char* FINALBUDGET = "fbs";
53 const char* FINALBUDGETVOTE = "fbvote";
54 const char* SYNCSTATUSCOUNT = "ssc";
55 const char* GETMNLIST = "dseg";
56 const char* QFCOMMITMENT = "qfcommit";
57 const char* QSENDRECSIGS = "qsendrecsigs";
58 const char* MNAUTH = "mnauth";
59 const char* QCONTRIB = "qcontrib";
60 const char* QCOMPLAINT = "qcomplaint";
61 const char* QJUSTIFICATION = "qjustify";
62 const char* QPCOMMITMENT = "qpcommit";
63 const char* QSIGSESANN = "qsigsesann";
64 const char* QSIGSHARESINV = "qsigsinv";
65 const char* QGETSIGSHARES = "qgetsigs";
66 const char* QBSIGSHARES = "qbsigs";
67 const char* QSIGREC = "qsigrec";
68 const char* QSIGSHARE = "qsigshare";
69 const char* CLSIG = "clsig";
70 }; // namespace NetMsgType
71 
72 
76 const static std::string allNetMessageTypes[] = {
100  "filtered block", // Should never occur
101  "ix", // deprecated
102  "txlvote", // deprecated
103  NetMsgType::SPORK, // --- tiertwoNetMessageTypes start here ---
105  "mnodescanerr",
110  "mnq",
113  "dstx", // deprecated
134 };
135 const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes + ARRAYLEN(allNetMessageTypes));
136 const static std::vector<std::string> tiertwoNetMessageTypesVec(std::find(allNetMessageTypesVec.begin(), allNetMessageTypesVec.end(), NetMsgType::SPORK), allNetMessageTypesVec.end());
137 
138 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
139 {
140  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
141  memset(pchCommand, 0, sizeof(pchCommand));
142  nMessageSize = -1;
143  memset(pchChecksum, 0, CHECKSUM_SIZE);
144 }
145 
146 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
147 {
148  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
149 
150  // Copy the command name, zero-padding to COMMAND_SIZE bytes
151  size_t i = 0;
152  for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
153  assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
154  for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;
155 
156  nMessageSize = nMessageSizeIn;
157  memset(pchChecksum, 0, CHECKSUM_SIZE);
158 }
159 
160 std::string CMessageHeader::GetCommand() const
161 {
162  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
163 }
164 
165 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
166 {
167  // Check start string
168  if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
169  return false;
170 
171  // Check the command string for errors
172  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++) {
173  if (*p1 == 0) {
174  // Must be all zeros after the first zero
175  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
176  if (*p1 != 0)
177  return false;
178  } else if (*p1 < ' ' || *p1 > 0x7E)
179  return false;
180  }
181 
182  // Message size
183  if (nMessageSize > MAX_SIZE) {
184  LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
185  return false;
186  }
187 
188  return true;
189 }
190 
192 {
193  type = 0;
194  hash.SetNull();
195 }
196 
197 CInv::CInv(int typeIn, const uint256& hashIn)
198 {
199  type = typeIn;
200  hash = hashIn;
201 }
202 
203 bool operator<(const CInv& a, const CInv& b)
204 {
205  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
206 }
207 
209  return type > 2;
210 }
211 
212 std::string CInv::GetCommand() const
213 {
214  std::string cmd;
215  switch (type) {
216  case MSG_TX: return cmd.append(NetMsgType::TX);
217  case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
218  case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
219  case MSG_TXLOCK_REQUEST: return cmd.append("ix"); // Deprecated
220  case MSG_TXLOCK_VOTE: return cmd.append("txlvote"); // Deprecated
221  case MSG_SPORK: return cmd.append(NetMsgType::SPORK);
222  case MSG_MASTERNODE_WINNER: return cmd.append(NetMsgType::MNWINNER);
223  case MSG_MASTERNODE_SCANNING_ERROR: return cmd.append("mnodescanerr"); // Deprecated
224  case MSG_BUDGET_VOTE: return cmd.append(NetMsgType::BUDGETVOTE);
225  case MSG_BUDGET_PROPOSAL: return cmd.append(NetMsgType::BUDGETPROPOSAL);
226  case MSG_BUDGET_FINALIZED: return cmd.append(NetMsgType::FINALBUDGET);
228  case MSG_MASTERNODE_QUORUM: return cmd.append("mnq"); // Unused
229  case MSG_MASTERNODE_ANNOUNCE: return cmd.append(NetMsgType::MNBROADCAST); // or MNBROADCAST2
230  case MSG_MASTERNODE_PING: return cmd.append(NetMsgType::MNPING);
231  case MSG_DSTX: return cmd.append("dstx"); // Deprecated
232  case MSG_QUORUM_FINAL_COMMITMENT: return cmd.append(NetMsgType::QFCOMMITMENT);
233  case MSG_QUORUM_CONTRIB: return cmd.append(NetMsgType::QCONTRIB);
234  case MSG_QUORUM_COMPLAINT: return cmd.append(NetMsgType::QCOMPLAINT);
235  case MSG_QUORUM_JUSTIFICATION: return cmd.append(NetMsgType::QJUSTIFICATION);
237  case MSG_QUORUM_RECOVERED_SIG: return cmd.append(NetMsgType::QSIGREC);
238  case MSG_CLSIG: return cmd.append(NetMsgType::CLSIG);
239  default:
240  throw std::out_of_range(strprintf("%s: type=%d unknown type", __func__, type));
241  }
242 }
243 
244 
245 std::string CInv::ToString() const
246 {
247  return strprintf("%s %s", GetCommand(), hash.ToString());
248 }
249 
250 const std::vector<std::string>& getAllNetMessageTypes()
251 {
252  return allNetMessageTypesVec;
253 }
254 
255 const std::vector<std::string>& getTierTwoNetMessageTypes()
256 {
257  return tiertwoNetMessageTypesVec;
258 }
inv message data
Definition: protocol.h:466
int type
Definition: protocol.h:479
std::string ToString() const
Definition: protocol.cpp:245
std::string GetCommand() const
Definition: protocol.cpp:212
CInv()
Definition: protocol.cpp:191
bool IsMasterNodeType() const
Definition: protocol.cpp:208
uint256 hash
Definition: protocol.h:480
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:54
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:36
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:165
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:55
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:57
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:33
std::string GetCommand() const
Definition: protocol.cpp:160
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:34
uint32_t nMessageSize
Definition: protocol.h:56
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:138
std::string ToString() const
Definition: uint256.cpp:65
void SetNull()
Definition: uint256.h:44
256-bit opaque blob.
Definition: uint256.h:138
void * memcpy(void *a, const void *b, size_t c)
Bitcoin protocol message types.
Definition: protocol.cpp:18
const char * QFCOMMITMENT
The qfcommit message is used to propagate LLMQ final commitments.
Definition: protocol.cpp:56
const char * MNBROADCAST2
The mnbroadcast2 message is used to broadcast masternode startup data to connected peers Supporting B...
Definition: protocol.cpp:45
const char * BUDGETVOTESYNC
The budgetvotesync message is used to request budget vote data from connected peers.
Definition: protocol.cpp:51
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:38
const char * MNWINNER
The mnwinner message is used to relay and distribute consensus for masternode payout ordering.
Definition: protocol.cpp:47
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:31
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.cpp:40
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:30
const char * GETMNLIST
The dseg message is used to request the Masternode list or an specific entry.
Definition: protocol.cpp:55
const char * QSIGSESANN
Definition: protocol.cpp:63
const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message,...
Definition: protocol.cpp:22
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:41
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:35
const char * GETMNWINNERS
The getmnwinners message is used to request winning masternode data from connected peers.
Definition: protocol.cpp:48
const char * QSENDRECSIGS
The qsendrecsigs message is used to propagate LLMQ intra-quorum partial recovered signatures.
Definition: protocol.cpp:57
const char * QGETSIGSHARES
Definition: protocol.cpp:65
const char * QBSIGSHARES
Definition: protocol.cpp:66
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:32
const char * FINALBUDGETVOTE
The finalbudgetvote message is used to broadcast or relay finalized budget votes to connected peers.
Definition: protocol.cpp:53
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:37
const char * QSIGREC
Definition: protocol.cpp:67
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:33
const char * QCONTRIB
Definition: protocol.cpp:59
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:29
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:39
const char * QPCOMMITMENT
Definition: protocol.cpp:62
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:21
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:19
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:27
const char * MNBROADCAST
The mnbroadcast message is used to broadcast masternode startup data to connected peers.
Definition: protocol.cpp:44
const char * QCOMPLAINT
Definition: protocol.cpp:60
const char * QSIGSHARE
Definition: protocol.cpp:68
const char * FINALBUDGET
The finalbudget message is used to broadcast or relay finalized budget metadata to connected peers.
Definition: protocol.cpp:52
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:28
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:25
const char * MNPING
The mnping message is used to ensure a masternode is still active.
Definition: protocol.cpp:46
const char * QJUSTIFICATION
Definition: protocol.cpp:61
const char * BUDGETVOTE
The budgetvote message is used to broadcast or relay budget proposal votes to connected peers.
Definition: protocol.cpp:50
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:20
const char * SYNCSTATUSCOUNT
The syncstatuscount message is used to track the layer 2 syncing process.
Definition: protocol.cpp:54
const char * BUDGETPROPOSAL
The budgetproposal message is used to broadcast or relay budget proposal metadata to connected peers.
Definition: protocol.cpp:49
const char * CLSIG
Definition: protocol.cpp:69
const char * ALERT
The alert message warns nodes of problems that may affect them or the rest of the network.
Definition: protocol.cpp:36
const char * SENDADDRV2
The sendaddrv2 message signals support for receiving ADDRV2 messages (BIP155).
Definition: protocol.cpp:23
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.cpp:34
const char * SPORK
The spork message is used to send spork values to connected peers.
Definition: protocol.cpp:42
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:26
const char * GETSPORKS
The getsporks message is used to request spork data from connected peers.
Definition: protocol.cpp:43
const char * QSIGSHARESINV
Definition: protocol.cpp:64
const char * MNAUTH
The mnauth message is used authenticate MN connections.
Definition: protocol.cpp:58
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:24
bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:203
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:250
const std::vector< std::string > & getTierTwoNetMessageTypes()
Definition: protocol.cpp:255
@ MSG_TX
Definition: protocol.h:436
@ MSG_BUDGET_VOTE
Definition: protocol.h:446
@ MSG_FILTERED_BLOCK
Definition: protocol.h:440
@ MSG_QUORUM_JUSTIFICATION
Definition: protocol.h:457
@ MSG_BUDGET_PROPOSAL
Definition: protocol.h:447
@ MSG_QUORUM_COMPLAINT
Definition: protocol.h:456
@ MSG_MASTERNODE_QUORUM
Definition: protocol.h:450
@ MSG_MASTERNODE_ANNOUNCE
Definition: protocol.h:451
@ MSG_MASTERNODE_WINNER
Definition: protocol.h:444
@ MSG_BLOCK
Definition: protocol.h:437
@ MSG_MASTERNODE_PING
Definition: protocol.h:452
@ MSG_TXLOCK_REQUEST
Definition: protocol.h:441
@ MSG_QUORUM_CONTRIB
Definition: protocol.h:455
@ MSG_MASTERNODE_SCANNING_ERROR
Definition: protocol.h:445
@ MSG_BUDGET_FINALIZED_VOTE
Definition: protocol.h:449
@ MSG_TXLOCK_VOTE
Definition: protocol.h:442
@ MSG_QUORUM_PREMATURE_COMMITMENT
Definition: protocol.h:458
@ MSG_QUORUM_RECOVERED_SIG
Definition: protocol.h:459
@ MSG_DSTX
Definition: protocol.h:453
@ MSG_SPORK
Definition: protocol.h:443
@ MSG_CLSIG
Definition: protocol.h:460
@ MSG_BUDGET_FINALIZED
Definition: protocol.h:448
@ MSG_QUORUM_FINAL_COMMITMENT
Definition: protocol.h:454
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
#define strprintf
Definition: tinyformat.h:1056
#define ARRAYLEN(array)