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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 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. #ifndef __cplusplus
  6. # error This header can only be compiled as C++.
  7. #endif
  8. #ifndef __INCLUDED_PROTOCOL_H__
  9. #define __INCLUDED_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. CMessageHeader();
  27. CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
  28. std::string GetCommand() const;
  29. bool IsValid() const;
  30. ADD_SERIALIZE_METHODS;
  31. template <typename Stream, typename Operation>
  32. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  33. READWRITE(FLATDATA(pchMessageStart));
  34. READWRITE(FLATDATA(pchCommand));
  35. READWRITE(nMessageSize);
  36. READWRITE(nChecksum);
  37. }
  38. // TODO: make private (improves encapsulation)
  39. public:
  40. enum {
  41. COMMAND_SIZE=12,
  42. MESSAGE_SIZE_SIZE=sizeof(int),
  43. CHECKSUM_SIZE=sizeof(int),
  44. MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
  45. CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE,
  46. HEADER_SIZE=MESSAGE_START_SIZE+COMMAND_SIZE+MESSAGE_SIZE_SIZE+CHECKSUM_SIZE
  47. };
  48. char pchMessageStart[MESSAGE_START_SIZE];
  49. char pchCommand[COMMAND_SIZE];
  50. unsigned int nMessageSize;
  51. unsigned int nChecksum;
  52. };
  53. /** nServices flags */
  54. enum
  55. {
  56. NODE_NETWORK = (1 << 0),
  57. // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
  58. // isn't getting used, or one not being used much, and notify the
  59. // bitcoin-development mailing list. Remember that service bits are just
  60. // unauthenticated advertisements, so your code must be robust against
  61. // collisions and other cases where nodes may be advertising a service they
  62. // do not actually support. Other service bits should be allocated via the
  63. // BIP process.
  64. };
  65. /** A CService with information about it as peer */
  66. class CAddress : public CService
  67. {
  68. public:
  69. CAddress();
  70. explicit CAddress(CService ipIn, uint64_t nServicesIn=NODE_NETWORK);
  71. void Init();
  72. ADD_SERIALIZE_METHODS;
  73. template <typename Stream, typename Operation>
  74. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  75. if (ser_action.ForRead())
  76. Init();
  77. if (nType & SER_DISK)
  78. READWRITE(nVersion);
  79. if ((nType & SER_DISK) ||
  80. (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
  81. READWRITE(nTime);
  82. READWRITE(nServices);
  83. READWRITE(*(CService*)this);
  84. }
  85. // TODO: make private (improves encapsulation)
  86. public:
  87. uint64_t nServices;
  88. // disk and network only
  89. unsigned int nTime;
  90. // memory only
  91. int64_t nLastTry;
  92. };
  93. /** inv message data */
  94. class CInv
  95. {
  96. public:
  97. CInv();
  98. CInv(int typeIn, const uint256& hashIn);
  99. CInv(const std::string& strType, const uint256& hashIn);
  100. ADD_SERIALIZE_METHODS;
  101. template <typename Stream, typename Operation>
  102. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  103. READWRITE(type);
  104. READWRITE(hash);
  105. }
  106. friend bool operator<(const CInv& a, const CInv& b);
  107. bool IsKnownType() const;
  108. const char* GetCommand() const;
  109. std::string ToString() const;
  110. // TODO: make private (improves encapsulation)
  111. public:
  112. int type;
  113. uint256 hash;
  114. };
  115. enum
  116. {
  117. MSG_TX = 1,
  118. MSG_BLOCK,
  119. // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
  120. // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
  121. MSG_FILTERED_BLOCK,
  122. };
  123. #endif // __INCLUDED_PROTOCOL_H__