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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 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 "chainparams.h"
  11. #include "serialize.h"
  12. #include "netbase.h"
  13. #include <string>
  14. #include "uint256.h"
  15. /** Message header.
  16. * (4) message start.
  17. * (12) command.
  18. * (4) size.
  19. * (4) checksum.
  20. */
  21. class CMessageHeader
  22. {
  23. public:
  24. CMessageHeader();
  25. CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
  26. std::string GetCommand() const;
  27. bool IsValid() const;
  28. IMPLEMENT_SERIALIZE
  29. (
  30. READWRITE(FLATDATA(pchMessageStart));
  31. READWRITE(FLATDATA(pchCommand));
  32. READWRITE(nMessageSize);
  33. READWRITE(nChecksum);
  34. )
  35. // TODO: make private (improves encapsulation)
  36. public:
  37. enum {
  38. COMMAND_SIZE=12,
  39. MESSAGE_SIZE_SIZE=sizeof(int),
  40. CHECKSUM_SIZE=sizeof(int),
  41. MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
  42. CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE,
  43. HEADER_SIZE=MESSAGE_START_SIZE+COMMAND_SIZE+MESSAGE_SIZE_SIZE+CHECKSUM_SIZE
  44. };
  45. char pchMessageStart[MESSAGE_START_SIZE];
  46. char pchCommand[COMMAND_SIZE];
  47. unsigned int nMessageSize;
  48. unsigned int nChecksum;
  49. };
  50. /** nServices flags */
  51. enum
  52. {
  53. NODE_NETWORK = (1 << 0),
  54. };
  55. /** A CService with information about it as peer */
  56. class CAddress : public CService
  57. {
  58. public:
  59. CAddress();
  60. explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
  61. void Init();
  62. IMPLEMENT_SERIALIZE
  63. (
  64. CAddress* pthis = const_cast<CAddress*>(this);
  65. CService* pip = (CService*)pthis;
  66. if (fRead)
  67. pthis->Init();
  68. if (nType & SER_DISK)
  69. READWRITE(nVersion);
  70. if ((nType & SER_DISK) ||
  71. (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
  72. READWRITE(nTime);
  73. READWRITE(nServices);
  74. READWRITE(*pip);
  75. )
  76. void print() const;
  77. // TODO: make private (improves encapsulation)
  78. public:
  79. uint64 nServices;
  80. // disk and network only
  81. unsigned int nTime;
  82. // memory only
  83. int64 nLastTry;
  84. };
  85. /** inv message data */
  86. class CInv
  87. {
  88. public:
  89. CInv();
  90. CInv(int typeIn, const uint256& hashIn);
  91. CInv(const std::string& strType, const uint256& hashIn);
  92. IMPLEMENT_SERIALIZE
  93. (
  94. READWRITE(type);
  95. READWRITE(hash);
  96. )
  97. friend bool operator<(const CInv& a, const CInv& b);
  98. bool IsKnownType() const;
  99. const char* GetCommand() const;
  100. std::string ToString() const;
  101. void print() const;
  102. // TODO: make private (improves encapsulation)
  103. public:
  104. int type;
  105. uint256 hash;
  106. };
  107. enum
  108. {
  109. MSG_TX = 1,
  110. MSG_BLOCK,
  111. // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
  112. // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
  113. MSG_FILTERED_BLOCK,
  114. };
  115. #endif // __INCLUDED_PROTOCOL_H__