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