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.
7 #error This header can only be compiled as C++.
10 #ifndef BITCOIN_PROTOCOL_H
11 #define BITCOIN_PROTOCOL_H
14 #include "serialize.h"
21 #define MESSAGE_START_SIZE 4
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
);
51 // TODO: make private (improves encapsulation)
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 */
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
88 /** A CService with information about it as peer */
89 class CAddress
: public CService
93 explicit CAddress(CService ipIn
, uint64_t nServicesIn
= NODE_NETWORK
);
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())
104 if (nType
& SER_DISK
)
106 if ((nType
& SER_DISK
) ||
107 (nVersion
>= CADDR_TIME_VERSION
&& !(nType
& SER_GETHASH
)))
109 READWRITE(nServices
);
110 READWRITE(*(CService
*)this);
113 // TODO: make private (improves encapsulation)
117 // disk and network only
121 /** inv message data */
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
)
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)
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.
158 #endif // BITCOIN_PROTOCOL_H