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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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 STARWELS_PROTOCOL_H
  9. #define STARWELS_PROTOCOL_H
  10. #include "netaddress.h"
  11. #include "serialize.h"
  12. #include "uint256.h"
  13. #include "version.h"
  14. #include <stdint.h>
  15. #include <string>
  16. /** Message header.
  17. * (4) message start.
  18. * (12) command.
  19. * (4) size.
  20. * (4) checksum.
  21. */
  22. class CMessageHeader
  23. {
  24. public:
  25. enum {
  26. MESSAGE_START_SIZE = 4,
  27. COMMAND_SIZE = 12,
  28. MESSAGE_SIZE_SIZE = 4,
  29. CHECKSUM_SIZE = 4,
  30. MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE,
  31. CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE,
  32. HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE
  33. };
  34. typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
  35. CMessageHeader(const MessageStartChars& pchMessageStartIn);
  36. CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
  37. std::string GetCommand() const;
  38. bool IsValid(const MessageStartChars& messageStart) const;
  39. ADD_SERIALIZE_METHODS;
  40. template <typename Stream, typename Operation>
  41. inline void SerializationOp(Stream& s, Operation ser_action)
  42. {
  43. READWRITE(FLATDATA(pchMessageStart));
  44. READWRITE(FLATDATA(pchCommand));
  45. READWRITE(nMessageSize);
  46. READWRITE(FLATDATA(pchChecksum));
  47. }
  48. char pchMessageStart[MESSAGE_START_SIZE];
  49. char pchCommand[COMMAND_SIZE];
  50. uint32_t nMessageSize;
  51. uint8_t pchChecksum[CHECKSUM_SIZE];
  52. };
  53. /**
  54. * Starwels protocol message types. When adding new message types, don't forget
  55. * to update allNetMessageTypes in protocol.cpp.
  56. */
  57. namespace NetMsgType {
  58. /**
  59. * The version message provides information about the transmitting node to the
  60. * receiving node at the beginning of a connection.
  61. * @see https://github.com/starwels/en/developer-reference#version
  62. */
  63. extern const char *VERSION;
  64. /**
  65. * The verack message acknowledges a previously-received version message,
  66. * informing the connecting node that it can begin to send other messages.
  67. * @see https://github.com/starwels/en/developer-reference#verack
  68. */
  69. extern const char *VERACK;
  70. /**
  71. * The addr (IP address) message relays connection information for peers on the
  72. * network.
  73. * @see https://github.com/starwels/en/developer-reference#addr
  74. */
  75. extern const char *ADDR;
  76. /**
  77. * The inv message (inventory message) transmits one or more inventories of
  78. * objects known to the transmitting peer.
  79. * @see https://github.com/starwels/en/developer-reference#inv
  80. */
  81. extern const char *INV;
  82. /**
  83. * The getdata message requests one or more data objects from another node.
  84. * @see https://github.com/starwels/en/developer-reference#getdata
  85. */
  86. extern const char *GETDATA;
  87. /**
  88. * The merkleblock message is a reply to a getdata message which requested a
  89. * block using the inventory type MSG_MERKLEBLOCK.
  90. * @since protocol version 70001 as described by BIP37.
  91. * @see https://github.com/starwels/en/developer-reference#merkleblock
  92. */
  93. extern const char *MERKLEBLOCK;
  94. /**
  95. * The getblocks message requests an inv message that provides block header
  96. * hashes starting from a particular point in the block chain.
  97. * @see https://github.com/starwels/en/developer-reference#getblocks
  98. */
  99. extern const char *GETBLOCKS;
  100. /**
  101. * The getheaders message requests a headers message that provides block
  102. * headers starting from a particular point in the block chain.
  103. * @since protocol version 31800.
  104. * @see https://github.com/starwels/en/developer-reference#getheaders
  105. */
  106. extern const char *GETHEADERS;
  107. /**
  108. * The tx message transmits a single transaction.
  109. * @see https://github.com/starwels/en/developer-reference#tx
  110. */
  111. extern const char *TX;
  112. /**
  113. * The headers message sends one or more block headers to a node which
  114. * previously requested certain headers with a getheaders message.
  115. * @since protocol version 31800.
  116. * @see https://github.com/starwels/en/developer-reference#headers
  117. */
  118. extern const char *HEADERS;
  119. /**
  120. * The block message transmits a single serialized block.
  121. * @see https://github.com/starwels/en/developer-reference#block
  122. */
  123. extern const char *BLOCK;
  124. /**
  125. * The getaddr message requests an addr message from the receiving node,
  126. * preferably one with lots of IP addresses of other receiving nodes.
  127. * @see https://github.com/starwels/en/developer-reference#getaddr
  128. */
  129. extern const char *GETADDR;
  130. /**
  131. * The mempool message requests the TXIDs of transactions that the receiving
  132. * node has verified as valid but which have not yet appeared in a block.
  133. * @since protocol version 60002.
  134. * @see https://github.com/starwels/en/developer-reference#mempool
  135. */
  136. extern const char *MEMPOOL;
  137. /**
  138. * The ping message is sent periodically to help confirm that the receiving
  139. * peer is still connected.
  140. * @see https://github.com/starwels/en/developer-reference#ping
  141. */
  142. extern const char *PING;
  143. /**
  144. * The pong message replies to a ping message, proving to the pinging node that
  145. * the ponging node is still alive.
  146. * @since protocol version 60001 as described by BIP31.
  147. * @see https://github.com/starwels/en/developer-reference#pong
  148. */
  149. extern const char *PONG;
  150. /**
  151. * The notfound message is a reply to a getdata message which requested an
  152. * object the receiving node does not have available for relay.
  153. * @since protocol version 70001.
  154. * @see https://github.com/starwels/en/developer-reference#notfound
  155. */
  156. extern const char *NOTFOUND;
  157. /**
  158. * The filterload message tells the receiving peer to filter all relayed
  159. * transactions and requested merkle blocks through the provided filter.
  160. * @since protocol version 70001 as described by BIP37.
  161. * Only available with service bit NODE_BLOOM since protocol version
  162. * 70011 as described by BIP111.
  163. * @see https://github.com/starwels/en/developer-reference#filterload
  164. */
  165. extern const char *FILTERLOAD;
  166. /**
  167. * The filteradd message tells the receiving peer to add a single element to a
  168. * previously-set bloom filter, such as a new public key.
  169. * @since protocol version 70001 as described by BIP37.
  170. * Only available with service bit NODE_BLOOM since protocol version
  171. * 70011 as described by BIP111.
  172. * @see https://github.com/starwels/en/developer-reference#filteradd
  173. */
  174. extern const char *FILTERADD;
  175. /**
  176. * The filterclear message tells the receiving peer to remove a previously-set
  177. * bloom filter.
  178. * @since protocol version 70001 as described by BIP37.
  179. * Only available with service bit NODE_BLOOM since protocol version
  180. * 70011 as described by BIP111.
  181. * @see https://github.com/starwels/en/developer-reference#filterclear
  182. */
  183. extern const char *FILTERCLEAR;
  184. /**
  185. * The reject message informs the receiving node that one of its previous
  186. * messages has been rejected.
  187. * @since protocol version 70002 as described by BIP61.
  188. * @see https://github.com/starwels/en/developer-reference#reject
  189. */
  190. extern const char *REJECT;
  191. /**
  192. * Indicates that a node prefers to receive new block announcements via a
  193. * "headers" message rather than an "inv".
  194. * @since protocol version 70012 as described by BIP130.
  195. * @see https://github.com/starwels/en/developer-reference#sendheaders
  196. */
  197. extern const char *SENDHEADERS;
  198. /**
  199. * The feefilter message tells the receiving peer not to inv us any txs
  200. * which do not meet the specified min fee rate.
  201. * @since protocol version 70013 as described by BIP133
  202. */
  203. extern const char *FEEFILTER;
  204. /**
  205. * Contains a 1-byte bool and 8-byte LE version number.
  206. * Indicates that a node is willing to provide blocks via "cmpctblock" messages.
  207. * May indicate that a node prefers to receive new block announcements via a
  208. * "cmpctblock" message rather than an "inv", depending on message contents.
  209. * @since protocol version 70014 as described by BIP 152
  210. */
  211. extern const char *SENDCMPCT;
  212. /**
  213. * Contains a CBlockHeaderAndShortTxIDs object - providing a header and
  214. * list of "short txids".
  215. * @since protocol version 70014 as described by BIP 152
  216. */
  217. extern const char *CMPCTBLOCK;
  218. /**
  219. * Contains a BlockTransactionsRequest
  220. * Peer should respond with "blocktxn" message.
  221. * @since protocol version 70014 as described by BIP 152
  222. */
  223. extern const char *GETBLOCKTXN;
  224. /**
  225. * Contains a BlockTransactions.
  226. * Sent in response to a "getblocktxn" message.
  227. * @since protocol version 70014 as described by BIP 152
  228. */
  229. extern const char *BLOCKTXN;
  230. };
  231. /* Get a vector of all valid message types (see above) */
  232. const std::vector<std::string> &getAllNetMessageTypes();
  233. /** nServices flags */
  234. enum ServiceFlags : uint64_t {
  235. // Nothing
  236. NODE_NONE = 0,
  237. // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
  238. // set by all Starwels nodes, and is unset by SPV clients or other peers that just want
  239. // network services but don't provide them.
  240. NODE_NETWORK = (1 << 0),
  241. // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
  242. // Starwels does not support this but a patch set called Starwels XT does.
  243. // See BIP 64 for details on how this is implemented.
  244. NODE_GETUTXO = (1 << 1),
  245. // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
  246. // Starwels nodes used to support this by default, without advertising this bit,
  247. // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
  248. NODE_BLOOM = (1 << 2),
  249. // NODE_WITNESS indicates that a node can be asked for blocks and transactions including
  250. // witness data.
  251. NODE_WITNESS = (1 << 3),
  252. // NODE_XTHIN means the node supports Xtreme Thinblocks
  253. // If this is turned off then the node will not service nor make xthin requests
  254. NODE_XTHIN = (1 << 4),
  255. // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
  256. // isn't getting used, or one not being used much, and notify the
  257. // starwels-development mailing list. Remember that service bits are just
  258. // unauthenticated advertisements, so your code must be robust against
  259. // collisions and other cases where nodes may be advertising a service they
  260. // do not actually support. Other service bits should be allocated via the
  261. // BIP process.
  262. };
  263. /** A CService with information about it as peer */
  264. class CAddress : public CService
  265. {
  266. public:
  267. CAddress();
  268. explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
  269. void Init();
  270. ADD_SERIALIZE_METHODS;
  271. template <typename Stream, typename Operation>
  272. inline void SerializationOp(Stream& s, Operation ser_action)
  273. {
  274. if (ser_action.ForRead())
  275. Init();
  276. int nVersion = s.GetVersion();
  277. if (s.GetType() & SER_DISK)
  278. READWRITE(nVersion);
  279. if ((s.GetType() & SER_DISK) ||
  280. (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
  281. READWRITE(nTime);
  282. uint64_t nServicesInt = nServices;
  283. READWRITE(nServicesInt);
  284. nServices = (ServiceFlags)nServicesInt;
  285. READWRITE(*(CService*)this);
  286. }
  287. // TODO: make private (improves encapsulation)
  288. public:
  289. ServiceFlags nServices;
  290. // disk and network only
  291. unsigned int nTime;
  292. };
  293. /** getdata message type flags */
  294. const uint32_t MSG_WITNESS_FLAG = 1 << 30;
  295. const uint32_t MSG_TYPE_MASK = 0xffffffff >> 2;
  296. /** getdata / inv message types.
  297. * These numbers are defined by the protocol. When adding a new value, be sure
  298. * to mention it in the respective BIP.
  299. */
  300. enum GetDataMsg
  301. {
  302. UNDEFINED = 0,
  303. MSG_TX = 1,
  304. MSG_BLOCK = 2,
  305. // The following can only occur in getdata. Invs always use TX or BLOCK.
  306. MSG_FILTERED_BLOCK = 3, //!< Defined in BIP37
  307. MSG_CMPCT_BLOCK = 4, //!< Defined in BIP152
  308. MSG_WITNESS_BLOCK = MSG_BLOCK | MSG_WITNESS_FLAG, //!< Defined in BIP144
  309. MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG, //!< Defined in BIP144
  310. MSG_FILTERED_WITNESS_BLOCK = MSG_FILTERED_BLOCK | MSG_WITNESS_FLAG,
  311. };
  312. /** inv message data */
  313. class CInv
  314. {
  315. public:
  316. CInv();
  317. CInv(int typeIn, const uint256& hashIn);
  318. ADD_SERIALIZE_METHODS;
  319. template <typename Stream, typename Operation>
  320. inline void SerializationOp(Stream& s, Operation ser_action)
  321. {
  322. READWRITE(type);
  323. READWRITE(hash);
  324. }
  325. friend bool operator<(const CInv& a, const CInv& b);
  326. std::string GetCommand() const;
  327. std::string ToString() const;
  328. // TODO: make private (improves encapsulation)
  329. public:
  330. int type;
  331. uint256 hash;
  332. };
  333. #endif // STARWELS_PROTOCOL_H