]> Git Repo - VerusCoin.git/blob - src/protocol.h
Bools
[VerusCoin.git] / src / protocol.h
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.
5
6 #ifndef __cplusplus
7 #error This header can only be compiled as C++.
8 #endif
9
10 #ifndef BITCOIN_PROTOCOL_H
11 #define BITCOIN_PROTOCOL_H
12
13 #include "netbase.h"
14 #include "serialize.h"
15 #include "uint256.h"
16 #include "version.h"
17
18 #include <stdint.h>
19 #include <string>
20
21 #define MESSAGE_START_SIZE 4
22
23 /** Message header.
24  * (4) message start.
25  * (12) command.
26  * (4) size.
27  * (4) checksum.
28  */
29 class CMessageHeader
30 {
31 public:
32     typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
33
34     CMessageHeader(const MessageStartChars& pchMessageStartIn);
35     CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
36
37     std::string GetCommand() const;
38     bool IsValid(const MessageStartChars& messageStart) const;
39
40     ADD_SERIALIZE_METHODS;
41
42     template <typename Stream, typename Operation>
43     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
44     {
45         READWRITE(FLATDATA(pchMessageStart));
46         READWRITE(FLATDATA(pchCommand));
47         READWRITE(nMessageSize);
48         READWRITE(nChecksum);
49     }
50
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),
57
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
61     };
62     char pchMessageStart[MESSAGE_START_SIZE];
63     char pchCommand[COMMAND_SIZE];
64     unsigned int nMessageSize;
65     unsigned int nChecksum;
66 };
67
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
75     // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
76     // isn't getting used, or one not being used much, and notify the
77     // bitcoin-development mailing list. Remember that service bits are just
78     // unauthenticated advertisements, so your code must be robust against
79     // collisions and other cases where nodes may be advertising a service they
80     // do not actually support. Other service bits should be allocated via the
81     // BIP process.
82 };
83
84 /** A CService with information about it as peer */
85 class CAddress : public CService
86 {
87 public:
88     CAddress();
89     explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK);
90
91     void Init();
92
93     ADD_SERIALIZE_METHODS;
94
95     template <typename Stream, typename Operation>
96     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
97     {
98         if (ser_action.ForRead())
99             Init();
100         if (nType & SER_DISK)
101             READWRITE(nVersion);
102         if ((nType & SER_DISK) ||
103             (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
104             READWRITE(nTime);
105         READWRITE(nServices);
106         READWRITE(*(CService*)this);
107     }
108
109     // TODO: make private (improves encapsulation)
110 public:
111     uint64_t nServices;
112
113     // disk and network only
114     unsigned int nTime;
115 };
116
117 /** inv message data */
118 class CInv
119 {
120 public:
121     CInv();
122     CInv(int typeIn, const uint256& hashIn);
123     CInv(const std::string& strType, const uint256& hashIn);
124
125     ADD_SERIALIZE_METHODS;
126
127     template <typename Stream, typename Operation>
128     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
129     {
130         READWRITE(type);
131         READWRITE(hash);
132     }
133
134     friend bool operator<(const CInv& a, const CInv& b);
135
136     bool IsKnownType() const;
137     const char* GetCommand() const;
138     std::string ToString() const;
139
140     // TODO: make private (improves encapsulation)
141 public:
142     int type;
143     uint256 hash;
144 };
145
146 enum {
147     MSG_TX = 1,
148     MSG_BLOCK,
149     // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
150     // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
151     MSG_FILTERED_BLOCK,
152 };
153
154 #endif // BITCOIN_PROTOCOL_H
This page took 0.034134 seconds and 4 git commands to generate.