Raven Core  3.0.0
P2P Digital Currency
protocol.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef __cplusplus
8 #error This header can only be compiled as C++.
9 #endif
10 
11 #ifndef RAVEN_PROTOCOL_H
12 #define RAVEN_PROTOCOL_H
13 
14 #include "netaddress.h"
15 #include "serialize.h"
16 #include "uint256.h"
17 #include "version.h"
18 
19 #include <stdint.h>
20 #include <string>
21 
29 {
30 public:
31  enum {
36 
39  HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE
40  };
41  typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
42 
43  explicit CMessageHeader(const MessageStartChars& pchMessageStartIn);
44  CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
45 
46  std::string GetCommand() const;
47  bool IsValid(const MessageStartChars& messageStart) const;
48 
50 
51  template <typename Stream, typename Operation>
52  inline void SerializationOp(Stream& s, Operation ser_action)
53  {
58  }
59 
62  uint32_t nMessageSize;
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;
132 extern const char *HEADERS;
137 extern const char *BLOCK;
143 extern const char *GETADDR;
150 extern const char *MEMPOOL;
156 extern const char *PING;
163 extern const char *PONG;
170 extern const char *NOTFOUND;
179 extern const char *FILTERLOAD;
188 extern const char *FILTERADD;
197 extern const char *FILTERCLEAR;
204 extern const char *REJECT;
211 extern const char *SENDHEADERS;
217 extern const char *FEEFILTER;
225 extern const char *SENDCMPCT;
231 extern const char *CMPCTBLOCK;
237 extern const char *GETBLOCKTXN;
243 extern const char *BLOCKTXN;
244 
250 extern const char *GETASSETDATA;
251 
257 extern const char *ASSETDATA;
258 
264  extern const char *ASSETNOTFOUND;
265 };
266 
267 /* Get a vector of all valid message types (see above) */
268 const std::vector<std::string> &getAllNetMessageTypes();
269 
271 enum ServiceFlags : uint64_t {
272  // Nothing
274  // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
275  // set by all Raven Core nodes, and is unset by SPV clients or other peers that just want
276  // network services but don't provide them.
277  NODE_NETWORK = (1 << 0),
278  // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
279  // Raven Core does not support this but a patch set called Raven XT does.
280  // See BIP 64 for details on how this is implemented.
281  NODE_GETUTXO = (1 << 1),
282  // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
283  // Raven Core nodes used to support this by default, without advertising this bit,
284  // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
285  NODE_BLOOM = (1 << 2),
286  // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
287  // witness data.
288  NODE_WITNESS = (1 << 3),
289  // NODE_XTHIN means the node supports Xtreme Thinblocks
290  // If this is turned off then the node will not service nor make xthin requests
291  NODE_XTHIN = (1 << 4),
292 
293  // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
294  // isn't getting used, or one not being used much, and notify the
295  // raven-development mailing list. Remember that service bits are just
296  // unauthenticated advertisements, so your code must be robust against
297  // collisions and other cases where nodes may be advertising a service they
298  // do not actually support. Other service bits should be allocated via the
299  // BIP process.
300 };
301 
318 static ServiceFlags GetDesirableServiceFlags(ServiceFlags services) {
319  return ServiceFlags(NODE_NONE);
320 }
321 
327 static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
328  return !(GetDesirableServiceFlags(services) & (~services));
329 }
330 
335 static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
336  return services & NODE_NETWORK;
337 }
338 
340 class CAddress : public CService
341 {
342 public:
343  CAddress();
344  explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
345 
346  void Init();
347 
349 
350  template <typename Stream, typename Operation>
351  inline void SerializationOp(Stream& s, Operation ser_action)
352  {
353  if (ser_action.ForRead())
354  Init();
355  int nVersion = s.GetVersion();
356  if (s.GetType() & SER_DISK)
357  READWRITE(nVersion);
358  if ((s.GetType() & SER_DISK) ||
359  (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
360  READWRITE(nTime);
361  uint64_t nServicesInt = nServices;
362  READWRITE(nServicesInt);
363  nServices = (ServiceFlags)nServicesInt;
364  READWRITE(*(CService*)this);
365  }
366 
367  // TODO: make private (improves encapsulation)
368 public:
370 
371  // disk and network only
372  unsigned int nTime;
373 };
374 
376 const uint32_t MSG_WITNESS_FLAG = 1 << 30;
377 const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
378 
384 {
386  MSG_TX = 1,
388  // The following can only occur in getdata. Invs always use TX or BLOCK.
394 };
395 
397 class CInv
398 {
399 public:
400  CInv();
401  CInv(int typeIn, const uint256& hashIn);
402 
404 
405  template <typename Stream, typename Operation>
406  inline void SerializationOp(Stream& s, Operation ser_action)
407  {
408  READWRITE(type);
409  READWRITE(hash);
410  }
411 
412  friend bool operator<(const CInv& a, const CInv& b);
413 
414  std::string GetCommand() const;
415  std::string ToString() const;
416 
417  // TODO: make private (improves encapsulation)
418 public:
419  int type;
421 };
422 
425 {
426 public:
427  CInvAsset();
428  CInvAsset(std::string name);
429 
431 
432  template <typename Stream, typename Operation>
433  inline void SerializationOp(Stream& s, Operation ser_action)
434  {
435  READWRITE(name);
436  }
437 
438  friend bool operator<(const CInvAsset& a, const CInvAsset& b);
439 
440  std::string ToString() const;
441 
442 public:
443  std::string name; // block height that asset data should come from
444 };
445 
446 #endif // RAVEN_PROTOCOL_H
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:30
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:33
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
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:63
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
ServiceFlags
nServices flags
Definition: protocol.h:271
const char * ASSETNOTFOUND
The asstnotfound message is a reply to a getassetdata message which requested an object the receiving...
Definition: protocol.cpp:45
#define READWRITE(obj)
Definition: serialize.h:163
Defined in BIP144.
Definition: protocol.h:392
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:194
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:28
Defined in BIP152.
Definition: protocol.h:390
const char * GETASSETDATA
Contains a AssetDataRequest.
Definition: protocol.cpp:43
inv message data
Definition: protocol.h:397
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:61
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:351
Defined in BIP144.
Definition: protocol.h:391
ADD_SERIALIZE_METHODS
Definition: protocol.h:348
ADD_SERIALIZE_METHODS
Definition: protocol.h:430
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:84
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:52
GetDataMsg
getdata / inv message types.
Definition: protocol.h:383
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:376
uint32_t nMessageSize
Definition: protocol.h:62
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:377
std::string GetCommand() const
Definition: protocol.cpp:101
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:31
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:106
#define FLATDATA(obj)
Definition: serialize.h:366
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:26
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:20
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:24
Raven protocol message types.
Definition: protocol.cpp:16
int type
Definition: protocol.h:419
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:141
Removed for reorganization.
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:37
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:29
A CService with information about it as peer.
Definition: protocol.h:340
uint256 hash
Definition: protocol.h:420
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:19
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:35
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:32
const char * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
Definition: protocol.cpp:38
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:41
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected...
Definition: protocol.cpp:36
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:23
ADD_SERIALIZE_METHODS
Definition: protocol.h:403
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:303
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:18
256-bit opaque blob.
Definition: uint256.h:123
unsigned int nTime
Definition: protocol.h:372
std::string name
Definition: protocol.h:443
ServiceFlags nServices
Definition: protocol.h:369
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:433
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:40
inv message data
Definition: protocol.h:424
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:21
const char * ASSETDATA
Contains a AssetData Sent in response to a "getassetdata" message.
Definition: protocol.cpp:44
void SerializationOp(Stream &s, Operation ser_action)
Definition: protocol.h:406
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:25
#define VERSION
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:60
Defined in BIP37.
Definition: protocol.h:389
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:34
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:41
Message header.
Definition: protocol.h:28