Merge pull request #6599
[bitcoinplatinum.git] / src / protocol.h
blobb5e65032a21fd111dbd01a119ac09685b51378ad
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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 "netbase.h"
14 #include "serialize.h"
15 #include "uint256.h"
16 #include "version.h"
18 #include <stdint.h>
19 #include <string>
21 #define MESSAGE_START_SIZE 4
23 /** Message header.
24 * (4) message start.
25 * (12) command.
26 * (4) size.
27 * (4) checksum.
29 class CMessageHeader
31 public:
32 typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
34 CMessageHeader(const MessageStartChars& pchMessageStartIn);
35 CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
37 std::string GetCommand() const;
38 bool IsValid(const MessageStartChars& messageStart) const;
40 ADD_SERIALIZE_METHODS;
42 template <typename Stream, typename Operation>
43 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
45 READWRITE(FLATDATA(pchMessageStart));
46 READWRITE(FLATDATA(pchCommand));
47 READWRITE(nMessageSize);
48 READWRITE(nChecksum);
51 // TODO: make private (improves encapsulation)
52 public:
53 enum {
54 COMMAND_SIZE = 12,
55 MESSAGE_SIZE_SIZE = sizeof(int),
56 CHECKSUM_SIZE = sizeof(int),
58 MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE,
59 CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE,
60 HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE
62 char pchMessageStart[MESSAGE_START_SIZE];
63 char pchCommand[COMMAND_SIZE];
64 unsigned int nMessageSize;
65 unsigned int nChecksum;
68 /** nServices flags */
69 enum {
70 // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
71 // set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
72 // network services but don't provide them.
73 NODE_NETWORK = (1 << 0),
74 // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request.
75 // Bitcoin Core does not support this but a patch set called Bitcoin XT does.
76 // See BIP 64 for details on how this is implemented.
77 NODE_GETUTXO = (1 << 1),
79 // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
80 // isn't getting used, or one not being used much, and notify the
81 // bitcoin-development mailing list. Remember that service bits are just
82 // unauthenticated advertisements, so your code must be robust against
83 // collisions and other cases where nodes may be advertising a service they
84 // do not actually support. Other service bits should be allocated via the
85 // BIP process.
88 /** A CService with information about it as peer */
89 class CAddress : public CService
91 public:
92 CAddress();
93 explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK);
95 void Init();
97 ADD_SERIALIZE_METHODS;
99 template <typename Stream, typename Operation>
100 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
102 if (ser_action.ForRead())
103 Init();
104 if (nType & SER_DISK)
105 READWRITE(nVersion);
106 if ((nType & SER_DISK) ||
107 (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
108 READWRITE(nTime);
109 READWRITE(nServices);
110 READWRITE(*(CService*)this);
113 // TODO: make private (improves encapsulation)
114 public:
115 uint64_t nServices;
117 // disk and network only
118 unsigned int nTime;
121 /** inv message data */
122 class CInv
124 public:
125 CInv();
126 CInv(int typeIn, const uint256& hashIn);
127 CInv(const std::string& strType, const uint256& hashIn);
129 ADD_SERIALIZE_METHODS;
131 template <typename Stream, typename Operation>
132 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
134 READWRITE(type);
135 READWRITE(hash);
138 friend bool operator<(const CInv& a, const CInv& b);
140 bool IsKnownType() const;
141 const char* GetCommand() const;
142 std::string ToString() const;
144 // TODO: make private (improves encapsulation)
145 public:
146 int type;
147 uint256 hash;
150 enum {
151 MSG_TX = 1,
152 MSG_BLOCK,
153 // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
154 // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
155 MSG_FILTERED_BLOCK,
158 #endif // BITCOIN_PROTOCOL_H