PRCYCoin  2.0.0.7rc1
P2P Digital Currency
protocol.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef __cplusplus
7 #error This header can only be compiled as C++.
8 #endif
9 
10 #ifndef BITCOIN_PROTOCOL_H
11 #define BITCOIN_PROTOCOL_H
12 
13 #include "netaddress.h"
14 #include "serialize.h"
15 #include "uint256.h"
16 #include "version.h"
17 
18 #include <stdint.h>
19 #include <string>
20 
21 #define MESSAGE_START_SIZE 4
22 
30 {
31 public:
33  CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
34 
35  std::string GetCommand() const;
36  bool IsValid() const;
37 
39 
40  template <typename Stream, typename Operation>
41  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
42  {
47  }
48 
49  // TODO: make private (improves encapsulation)
50 public:
51  enum {
53  MESSAGE_SIZE_SIZE = sizeof(int),
54  CHECKSUM_SIZE = sizeof(int),
55 
59  };
62  unsigned int nMessageSize;
63  unsigned int nChecksum;
64 };
65 
70 namespace NetMsgType {
71 
77 extern const char *VERSION;
83 extern const char *VERACK;
89 extern const char *ADDR;
95 extern const char *INV;
100 extern const char *GETDATA;
107 extern const char *MERKLEBLOCK;
113 extern const char *GETBLOCKS;
120 extern const char *GETHEADERS;
125 extern const char *TX;
129 extern const char *DSC;
130 extern const char *DSF;
131 extern const char *DSQ;
132 extern const char *DSR;
133 extern const char *DSTX;
134 extern const char *DSEE;
135 extern const char *DSEG;
136 extern const char *DSEEP;
137 extern const char *DSSU;
144 extern const char *HEADERS;
149 extern const char *BLOCK;
155 extern const char *GETADDR;
162 extern const char *MEMPOOL;
168 extern const char *PING;
175 extern const char *PONG;
182 extern const char *ALERT;
189 extern const char *NOTFOUND;
198 extern const char *FILTERLOAD;
207 extern const char *FILTERADD;
216 extern const char *FILTERCLEAR;
223 extern const char *REJECT;
230 extern const char *SENDHEADERS;
234 extern const char *IX;
239 extern const char *IXLOCKVOTE;
244 extern const char *SPORK;
248 extern const char *GETSPORKS;
252 extern const char *MNBROADCAST;
256 extern const char *MNPING;
261 extern const char *MNWINNER;
265 extern const char *GETMNWINNERS;
269 extern const char *BUDGETPROPOSAL;
273 extern const char *BUDGETVOTE;
277 extern const char *BUDGETVOTESYNC;
281 extern const char *FINALBUDGET;
285 extern const char *FINALBUDGETVOTE;
289 extern const char *SYNCSTATUSCOUNT;
290 };
291 
292 /* Get a vector of all valid message types (see above) */
293 const std::vector<std::string> &getAllNetMessageTypes();
294 
296 enum ServiceFlags : uint64_t {
297  // Nothing
299  // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
300  // set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
301  // network services but don't provide them.
302  NODE_NETWORK = (1 << 0),
303 
304  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
305  // Bitcoin Core nodes used to support this by default, without advertising this bit,
306  // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
307  NODE_BLOOM = (1 << 2),
308 
309  // NODE_BLOOM_WITHOUT_MN means the node has the same features as NODE_BLOOM with the only difference
310  // that the node doesn't want to receive master nodes messages. (the 1<<3 was not picked as constant because on bitcoin 0.14 is witness and we want that update here )
312 
313  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
314  // isn't getting used, or one not being used much, and notify the
315  // bitcoin-development mailing list. Remember that service bits are just
316  // unauthenticated advertisements, so your code must be robust against
317  // collisions and other cases where nodes may be advertising a service they
318  // do not actually support. Other service bits should be allocated via the
319  // BIP process.
320 };
321 
323 class CAddress : public CService
324 {
325 public:
326  CAddress();
327  explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
328 
329  void Init();
330 
332 
333  template <typename Stream, typename Operation>
334  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
335  {
336  if (ser_action.ForRead())
337  Init();
338  if (nType & SER_DISK)
339  READWRITE(nVersion);
340  if ((nType & SER_DISK) ||
341  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
342  READWRITE(nTime);
343  uint64_t nServicesInt = nServices;
344  READWRITE(nServicesInt);
345  nServices = (ServiceFlags)nServicesInt;
346  READWRITE(*(CService*)this);
347  }
348 
349  // TODO: make private (improves encapsulation)
350 public:
352 
353  // disk and network only
354  unsigned int nTime;
355 };
356 
358 class CInv
359 {
360 public:
361  CInv();
362  CInv(int typeIn, const uint256& hashIn);
363  CInv(const std::string& strType, const uint256& hashIn);
364 
366 
367  template <typename Stream, typename Operation>
368  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
369  {
370  READWRITE(type);
371  READWRITE(hash);
372  }
373 
374  friend bool operator<(const CInv& a, const CInv& b);
375 
376  bool IsKnownType() const;
377  bool IsMasterNodeType() const;
378  const char* GetCommand() const;
379  std::string ToString() const;
380 
381  // TODO: make private (improves encapsulation)
382 public:
383  int type;
385 };
386 
387 enum {
388  MSG_TX = 1,
390  // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
391  // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
405 };
406 
407 #endif // BITCOIN_PROTOCOL_H
CMessageHeader::pchCommand
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:61
CMessageHeader::CHECKSUM_SIZE
@ CHECKSUM_SIZE
Definition: protocol.h:54
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:133
NetMsgType
Bitcoin protocol message types.
Definition: protocol.cpp:16
NetMsgType::DSR
const char * DSR
Definition: protocol.cpp:29
NetMsgType::DSEE
const char * DSEE
Definition: protocol.cpp:31
CInv::type
int type
Definition: protocol.h:383
MSG_TXLOCK_VOTE
@ MSG_TXLOCK_VOTE
Definition: protocol.h:394
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:161
FLATDATA
#define FLATDATA(obj)
Definition: serialize.h:365
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
NODE_BLOOM_WITHOUT_MN
@ NODE_BLOOM_WITHOUT_MN
Definition: protocol.h:311
MSG_BUDGET_PROPOSAL
@ MSG_BUDGET_PROPOSAL
Definition: protocol.h:398
CInv::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: protocol.h:365
uint256.h
NetMsgType::FINALBUDGET
const char * FINALBUDGET
The finalbudget message is used to broadcast or relay finalized budget metadata to connected peers.
Definition: protocol.cpp:57
CAddress::CAddress
CAddress()
Definition: protocol.cpp:187
NetMsgType::PING
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.cpp:39
NetMsgType::SPORK
const char * SPORK
The spork message is used to send spork values to connected peers.
NetMsgType::FILTERLOAD
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:43
NetMsgType::IX
const char * IX
The ix message transmits a single SwiftX transaction.
Definition: protocol.cpp:48
MSG_BLOCK
@ MSG_BLOCK
Definition: protocol.h:389
CAddress::nTime
unsigned int nTime
Definition: protocol.h:354
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:296
CMessageHeader::GetCommand
std::string GetCommand() const
Definition: protocol.cpp:155
CAddress::nServices
ServiceFlags nServices
Definition: protocol.h:351
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:302
CMessageHeader::HEADER_SIZE
@ HEADER_SIZE
Definition: protocol.h:58
CInv
inv message data
Definition: protocol.h:358
NetMsgType::PONG
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:40
MSG_MASTERNODE_ANNOUNCE
@ MSG_MASTERNODE_ANNOUNCE
Definition: protocol.h:402
CMessageHeader::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: protocol.h:38
MSG_TX
@ MSG_TX
Definition: protocol.h:388
version.h
NetMsgType::MNWINNER
const char * MNWINNER
The mnwinner message is used to relay and distribute consensus for masternode payout ordering.
Definition: protocol.cpp:52
CMessageHeader::MESSAGE_SIZE_SIZE
@ MESSAGE_SIZE_SIZE
Definition: protocol.h:53
NetMsgType::BUDGETVOTESYNC
const char * BUDGETVOTESYNC
The budgetvotesync message is used to request budget vote data from connected peers.
Definition: protocol.cpp:56
CMessageHeader::MESSAGE_SIZE_OFFSET
@ MESSAGE_SIZE_OFFSET
Definition: protocol.h:56
CAddress::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: protocol.h:334
CInv::IsMasterNodeType
bool IsMasterNodeType() const
Definition: protocol.cpp:240
CInv::GetCommand
const char * GetCommand() const
Definition: protocol.cpp:244
NetMsgType::INV
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:20
NetMsgType::GETHEADERS
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:24
NetMsgType::DSEG
const char * DSEG
Definition: protocol.cpp:32
CMessageHeader::CHECKSUM_OFFSET
@ CHECKSUM_OFFSET
Definition: protocol.h:57
CMessageHeader::nChecksum
unsigned int nChecksum
Definition: protocol.h:63
NetMsgType::GETSPORKS
const char * GETSPORKS
The getsporks message is used to request spork data from connected peers.
CMessageHeader::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: protocol.h:41
NetMsgType::IXLOCKVOTE
const char * IXLOCKVOTE
The ixlockvote message is used to reach consensus for SwiftX transaction locks.
Definition: protocol.cpp:49
NetMsgType::HEADERS
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:35
netaddress.h
CInv::operator<
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:230
CAddress::Init
void Init()
Definition: protocol.cpp:198
CInv::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: protocol.h:368
NODE_BLOOM
@ NODE_BLOOM
Definition: protocol.h:307
NetMsgType::GETMNWINNERS
const char * GETMNWINNERS
The getmnwinners message is used to request winning masternode data from connected peers.
Definition: protocol.cpp:53
MSG_TXLOCK_REQUEST
@ MSG_TXLOCK_REQUEST
Definition: protocol.h:393
NetMsgType::FINALBUDGETVOTE
const char * FINALBUDGETVOTE
The finalbudgetvote message is used to broadcast or relay finalized budget votes to connected peers.
Definition: protocol.cpp:58
SER_DISK
@ SER_DISK
Definition: serialize.h:160
NetMsgType::FILTERCLEAR
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.cpp:45
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
MSG_MASTERNODE_SCANNING_ERROR
@ MSG_MASTERNODE_SCANNING_ERROR
Definition: protocol.h:396
NetMsgType::SENDHEADERS
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:47
NetMsgType::REJECT
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected.
Definition: protocol.cpp:46
NetMsgType::DSQ
const char * DSQ
Definition: protocol.cpp:28
CInv::IsKnownType
bool IsKnownType() const
Definition: protocol.cpp:235
MESSAGE_START_SIZE
#define MESSAGE_START_SIZE
Definition: protocol.h:21
NetMsgType::GETBLOCKS
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:23
NetMsgType::DSSU
const char * DSSU
Definition: protocol.cpp:34
MSG_MASTERNODE_PING
@ MSG_MASTERNODE_PING
Definition: protocol.h:403
CMessageHeader::CMessageHeader
CMessageHeader()
Definition: protocol.cpp:138
NetMsgType::GETDATA
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:21
MSG_DSTX
@ MSG_DSTX
Definition: protocol.h:404
NetMsgType::NOTFOUND
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:42
NODE_NONE
@ NODE_NONE
Definition: protocol.h:298
NetMsgType::ADDR
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:19
NetMsgType::DSF
const char * DSF
Definition: protocol.cpp:27
NetMsgType::BUDGETPROPOSAL
const char * BUDGETPROPOSAL
The budgetproposal message is used to broadcast or relay budget proposal metadata to connected peers.
Definition: protocol.cpp:54
NetMsgType::DSC
const char * DSC
We are keeping these for now.
Definition: protocol.cpp:26
NetMsgType::BLOCK
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:36
NetMsgType::MEMPOOL
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:38
MSG_BUDGET_FINALIZED
@ MSG_BUDGET_FINALIZED
Definition: protocol.h:399
NetMsgType::ALERT
const char * ALERT
The alert message warns nodes of problems that may affect them or the rest of the network.
Definition: protocol.cpp:41
CInv::CInv
CInv()
Definition: protocol.cpp:204
CAddress
A CService with information about it as peer.
Definition: protocol.h:323
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
CMessageHeader
Message header.
Definition: protocol.h:29
NetMsgType::TX
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:25
CInv::hash
uint256 hash
Definition: protocol.h:384
getAllNetMessageTypes
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:259
NetMsgType::BUDGETVOTE
const char * BUDGETVOTE
The budgetvote message is used to broadcast or relay budget proposal votes to connected peers.
Definition: protocol.cpp:55
MSG_BUDGET_FINALIZED_VOTE
@ MSG_BUDGET_FINALIZED_VOTE
Definition: protocol.h:400
NetMsgType::DSTX
const char * DSTX
Definition: protocol.cpp:30
CMessageHeader::pchMessageStart
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:60
NetMsgType::SYNCSTATUSCOUNT
const char * SYNCSTATUSCOUNT
The syncstatuscount message is used to track the layer 2 syncing process.
Definition: protocol.cpp:59
MSG_MASTERNODE_QUORUM
@ MSG_MASTERNODE_QUORUM
Definition: protocol.h:401
serialize.h
NetMsgType::VERACK
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:18
CMessageHeader::nMessageSize
unsigned int nMessageSize
Definition: protocol.h:62
NetMsgType::FILTERADD
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:44
CAddress::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: protocol.h:331
MSG_FILTERED_BLOCK
@ MSG_FILTERED_BLOCK
Definition: protocol.h:392
CMessageHeader::IsValid
bool IsValid() const
Definition: protocol.cpp:160
NetMsgType::VERSION
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:17
MSG_BUDGET_VOTE
@ MSG_BUDGET_VOTE
Definition: protocol.h:397
NetMsgType::MNPING
const char * MNPING
The mnping message is used to ensure a masternode is still active.
Definition: protocol.cpp:51
NetMsgType::DSEEP
const char * DSEEP
Definition: protocol.cpp:33
NetMsgType::MNBROADCAST
const char * MNBROADCAST
The mnbroadcast message is used to broadcast masternode startup data to connected peers.
Definition: protocol.cpp:50
MSG_MASTERNODE_WINNER
@ MSG_MASTERNODE_WINNER
Definition: protocol.h:395
CInv::ToString
std::string ToString() const
Definition: protocol.cpp:254
NetMsgType::GETADDR
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:37
NetMsgType::MERKLEBLOCK
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:22
CMessageHeader::COMMAND_SIZE
@ COMMAND_SIZE
Definition: protocol.h:52