You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protocol.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef __cplusplus
  6. #error This header can only be compiled as C++.
  7. #endif
  8. #ifndef BITCOIN_PROTOCOL_H
  9. #define BITCOIN_PROTOCOL_H
  10. #include "netbase.h"
  11. #include "serialize.h"
  12. #include "uint256.h"
  13. #include "version.h"
  14. #include <stdint.h>
  15. #include <string>
  16. #define MESSAGE_START_SIZE 4
  17. /** Message header.
  18. * (4) message start.
  19. * (12) command.
  20. * (4) size.
  21. * (4) checksum.
  22. */
  23. class CMessageHeader
  24. {
  25. public:
  26. typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
  27. CMessageHeader(const MessageStartChars& pchMessageStartIn);
  28. CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
  29. std::string GetCommand() const;
  30. bool IsValid(const MessageStartChars& messageStart) const;
  31. ADD_SERIALIZE_METHODS;
  32. template <typename Stream, typename Operation>
  33. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
  34. {
  35. READWRITE(FLATDATA(pchMessageStart));
  36. READWRITE(FLATDATA(pchCommand));
  37. READWRITE(nMessageSize);
  38. READWRITE(nChecksum);
  39. }
  40. // TODO: make private (improves encapsulation)
  41. public:
  42. enum {
  43. COMMAND_SIZE = 12,
  44. MESSAGE_SIZE_SIZE = sizeof(int),
  45. CHECKSUM_SIZE = sizeof(int),
  46. MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE,
  47. CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE,
  48. HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE
  49. };
  50. char pchMessageStart[MESSAGE_START_SIZE];
  51. char pchCommand[COMMAND_SIZE];
  52. unsigned int nMessageSize;
  53. unsigned int nChecksum;
  54. };
  55. /** nServices flags */
  56. enum {
  57. // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
  58. // set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
  59. // network services but don't provide them.
  60. NODE_NETWORK = (1 << 0),
  61. // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
  62. // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
  63. // See BIP 64 for details on how this is implemented.
  64. NODE_GETUTXO = (1 << 1),
  65. // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
  66. // Bitcoin Core nodes used to support this by default, without advertising this bit,
  67. // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
  68. NODE_BLOOM = (1 << 2),
  69. // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
  70. // isn't getting used, or one not being used much, and notify the
  71. // bitcoin-development mailing list. Remember that service bits are just
  72. // unauthenticated advertisements, so your code must be robust against
  73. // collisions and other cases where nodes may be advertising a service they
  74. // do not actually support. Other service bits should be allocated via the
  75. // BIP process.
  76. };
  77. /** A CService with information about it as peer */
  78. class CAddress : public CService
  79. {
  80. public:
  81. CAddress();
  82. explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK);
  83. void Init();
  84. ADD_SERIALIZE_METHODS;
  85. template <typename Stream, typename Operation>
  86. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
  87. {
  88. if (ser_action.ForRead())
  89. Init();
  90. if (nType & SER_DISK)
  91. READWRITE(nVersion);
  92. if ((nType & SER_DISK) ||
  93. (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
  94. READWRITE(nTime);
  95. READWRITE(nServices);
  96. READWRITE(*(CService*)this);
  97. }
  98. // TODO: make private (improves encapsulation)
  99. public:
  100. uint64_t nServices;
  101. // disk and network only
  102. unsigned int nTime;
  103. };
  104. /** inv message data */
  105. class CInv
  106. {
  107. public:
  108. CInv();
  109. CInv(int typeIn, const uint256& hashIn);
  110. CInv(const std::string& strType, const uint256& hashIn);
  111. ADD_SERIALIZE_METHODS;
  112. template <typename Stream, typename Operation>
  113. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
  114. {
  115. READWRITE(type);
  116. READWRITE(hash);
  117. }
  118. friend bool operator<(const CInv& a, const CInv& b);
  119. bool IsKnownType() const;
  120. const char* GetCommand() const;
  121. std::string ToString() const;
  122. // TODO: make private (improves encapsulation)
  123. public:
  124. int type;
  125. uint256 hash;
  126. };
  127. enum {
  128. MSG_TX = 1,
  129. MSG_BLOCK,
  130. // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
  131. // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
  132. MSG_FILTERED_BLOCK,
  133. };
  134. #endif // BITCOIN_PROTOCOL_H