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