f7331c19233d7a22742f83af9c2fe003d509e741
[novacoin.git] / src / protocol.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt 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 __INCLUDED_PROTOCOL_H__
11 #define __INCLUDED_PROTOCOL_H__
12
13 #include "serialize.h"
14 #include "netbase.h"
15 #include <string>
16 #include "uint256.h"
17
18 extern bool fTestNet;
19 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
20 {
21     return testnet ? 18333 : 8333;
22 }
23
24
25 extern unsigned char pchMessageStart[4];
26
27 /** Message header.
28  * (4) message start.
29  * (12) command.
30  * (4) size.
31  * (4) checksum.
32  */
33 class CMessageHeader
34 {
35     public:
36         CMessageHeader();
37         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
38
39         std::string GetCommand() const;
40         bool IsValid() const;
41
42         IMPLEMENT_SERIALIZE
43             (
44              READWRITE(FLATDATA(pchMessageStart));
45              READWRITE(FLATDATA(pchCommand));
46              READWRITE(nMessageSize);
47              READWRITE(nChecksum);
48             )
49
50     // TODO: make private (improves encapsulation)
51     public:
52         enum { COMMAND_SIZE=12 };
53         char pchMessageStart[sizeof(::pchMessageStart)];
54         char pchCommand[COMMAND_SIZE];
55         unsigned int nMessageSize;
56         unsigned int nChecksum;
57 };
58
59 /** nServices flags */
60 enum
61 {
62     NODE_NETWORK = (1 << 0),
63 };
64
65 /** A CService with information about it as peer */
66 class CAddress : public CService
67 {
68     public:
69         CAddress();
70         explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
71
72         void Init();
73
74         IMPLEMENT_SERIALIZE
75             (
76              CAddress* pthis = const_cast<CAddress*>(this);
77              CService* pip = (CService*)pthis;
78              if (fRead)
79                  pthis->Init();
80              if (nType & SER_DISK)
81                  READWRITE(nVersion);
82              if ((nType & SER_DISK) ||
83                  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
84                  READWRITE(nTime);
85              READWRITE(nServices);
86              READWRITE(*pip);
87             )
88
89         void print() const;
90
91     // TODO: make private (improves encapsulation)
92     public:
93         uint64 nServices;
94
95         // disk and network only
96         unsigned int nTime;
97
98         // memory only
99         int64 nLastTry;
100 };
101
102 /** inv message data */
103 class CInv
104 {
105     public:
106         CInv();
107         CInv(int typeIn, const uint256& hashIn);
108         CInv(const std::string& strType, const uint256& hashIn);
109
110         IMPLEMENT_SERIALIZE
111         (
112             READWRITE(type);
113             READWRITE(hash);
114         )
115
116         friend bool operator<(const CInv& a, const CInv& b);
117
118         bool IsKnownType() const;
119         const char* GetCommand() const;
120         std::string ToString() const;
121         void print() const;
122
123     // TODO: make private (improves encapsulation)
124     public:
125         int type;
126         uint256 hash;
127 };
128
129 #endif // __INCLUDED_PROTOCOL_H__