Anoncoin  0.9.4
P2P Digital Currency
core.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Copyright (c) 2013-2014 The Anoncoin Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef ANONCOIN_CORE_H
8 #define ANONCOIN_CORE_H
9 
10 #include "script.h"
11 #include "scrypt.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 
15 #include <stdint.h>
16 
17 class CTransaction;
18 
20 static const int64_t MAX_MONEY = 84000000 * COIN;
21 inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
22 
24 class COutPoint
25 {
26 public:
28  unsigned int n;
29 
30  COutPoint() { SetNull(); }
31  COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
33  void SetNull() { hash = 0; n = (unsigned int) -1; }
34  bool IsNull() const { return (hash == 0 && n == (unsigned int) -1); }
35 
36  friend bool operator<(const COutPoint& a, const COutPoint& b)
37  {
38  return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
39  }
40 
41  friend bool operator==(const COutPoint& a, const COutPoint& b)
42  {
43  return (a.hash == b.hash && a.n == b.n);
44  }
45 
46  friend bool operator!=(const COutPoint& a, const COutPoint& b)
47  {
48  return !(a == b);
49  }
50 
51  std::string ToString() const;
52  void print() const;
53 };
54 
56 class CInPoint
57 {
58 public:
59  const CTransaction* ptx;
60  unsigned int n;
61 
62  CInPoint() { SetNull(); }
63  CInPoint(const CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
64  void SetNull() { ptx = NULL; n = (unsigned int) -1; }
65  bool IsNull() const { return (ptx == NULL && n == (unsigned int) -1); }
66 };
67 
72 class CTxIn
73 {
74 public:
77  unsigned int nSequence;
78 
80  {
81  nSequence = std::numeric_limits<unsigned int>::max();
82  }
83 
84  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
85  CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
86 
88  (
89  READWRITE(prevout);
90  READWRITE(scriptSig);
91  READWRITE(nSequence);
92  )
93 
94  bool IsFinal() const
95  {
96  return (nSequence == std::numeric_limits<unsigned int>::max());
97  }
98 
99  friend bool operator==(const CTxIn& a, const CTxIn& b)
100  {
101  return (a.prevout == b.prevout &&
102  a.scriptSig == b.scriptSig &&
103  a.nSequence == b.nSequence);
104  }
105 
106  friend bool operator!=(const CTxIn& a, const CTxIn& b)
107  {
108  return !(a == b);
109  }
110 
111  std::string ToString() const;
112  void print() const;
113 };
114 
115 
116 
117 
121 class CTxOut
122 {
123 public:
124  int64_t nValue;
126 
128  {
129  SetNull();
130  }
131 
132  CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
133 
135  (
136  READWRITE(nValue);
137  READWRITE(scriptPubKey);
138  )
139 
140  void SetNull()
141  {
142  nValue = -1;
143  scriptPubKey.clear();
144  }
145 
146  bool IsNull() const
147  {
148  return (nValue == -1);
149  }
150 
151  uint256 GetHash() const;
152 
153  bool IsDust(int64_t nMinRelayTxFee) const
154  {
155  // Litecoin: IsDust() detection disabled, allows any valid dust to be relayed.
156  // The fees imposed on each dust txo is considered sufficient spam deterrant.
157  return false;
158  }
159 
160  friend bool operator==(const CTxOut& a, const CTxOut& b)
161  {
162  return (a.nValue == b.nValue &&
163  a.scriptPubKey == b.scriptPubKey);
164  }
165 
166  friend bool operator!=(const CTxOut& a, const CTxOut& b)
167  {
168  return !(a == b);
169  }
170 
171  std::string ToString() const;
172  void print() const;
173 };
174 
175 
180 {
181 public:
182  static int64_t nMinTxFee;
183  static int64_t nMinRelayTxFee;
184  static const int CURRENT_VERSION=1;
185  int nVersion;
186  std::vector<CTxIn> vin;
187  std::vector<CTxOut> vout;
188  unsigned int nLockTime;
189 
191  {
192  SetNull();
193  }
194 
196  (
197  READWRITE(this->nVersion);
198  nVersion = this->nVersion;
199  READWRITE(vin);
200  READWRITE(vout);
201  READWRITE(nLockTime);
202  )
203 
204  void SetNull()
205  {
207  vin.clear();
208  vout.clear();
209  nLockTime = 0;
210  }
211 
212  bool IsNull() const
213  {
214  return (vin.empty() && vout.empty());
215  }
216 
217  uint256 GetHash() const;
218  bool IsNewerThan(const CTransaction& old) const;
219 
220  // Return sum of txouts.
221  int64_t GetValueOut() const;
222  // GetValueIn() is a method on CCoinsViewCache, because
223  // inputs must be known to compute value in.
224 
225  // Compute priority, given priority of inputs and (optionally) tx size
226  double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
227 
228  bool IsCoinBase() const
229  {
230  return (vin.size() == 1 && vin[0].prevout.IsNull());
231  }
232 
233  friend bool operator==(const CTransaction& a, const CTransaction& b)
234  {
235  return (a.nVersion == b.nVersion &&
236  a.vin == b.vin &&
237  a.vout == b.vout &&
238  a.nLockTime == b.nLockTime);
239  }
240 
241  friend bool operator!=(const CTransaction& a, const CTransaction& b)
242  {
243  return !(a == b);
244  }
245 
246 
247  std::string ToString() const;
248  void print() const;
249 };
250 
253 {
254 private:
256 
257 public:
258  static uint64_t CompressAmount(uint64_t nAmount);
259  static uint64_t DecompressAmount(uint64_t nAmount);
260 
261  CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
262 
264  if (!fRead) {
265  uint64_t nVal = CompressAmount(txout.nValue);
266  READWRITE(VARINT(nVal));
267  } else {
268  uint64_t nVal = 0;
269  READWRITE(VARINT(nVal));
270  txout.nValue = DecompressAmount(nVal);
271  }
272  CScriptCompressor cscript(REF(txout.scriptPubKey));
273  READWRITE(cscript);
274  });)
275 };
276 
284 {
285 public:
286  CTxOut txout; // the txout data before being spent
287  bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
288  unsigned int nHeight; // if the outpoint was the last unspent: its height
289  int nVersion; // if the outpoint was the last unspent: its version
290 
291  CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
292  CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
293 
294  unsigned int GetSerializeSize(int nType, int nVersion) const {
295  return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
296  (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
297  ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
298  }
299 
300  template<typename Stream>
301  void Serialize(Stream &s, int nType, int nVersion) const {
302  ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
303  if (nHeight > 0)
304  ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
305  ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
306  }
307 
308  template<typename Stream>
309  void Unserialize(Stream &s, int nType, int nVersion) {
310  unsigned int nCode = 0;
311  ::Unserialize(s, VARINT(nCode), nType, nVersion);
312  nHeight = nCode / 2;
313  fCoinBase = nCode & 1;
314  if (nHeight > 0)
315  ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
316  ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
317  }
318 };
319 
321 class CTxUndo
322 {
323 public:
324  // undo information for all txins
325  std::vector<CTxInUndo> vprevout;
326 
328  READWRITE(vprevout);
329  )
330 };
331 
332 
341 {
342 public:
343  // header
344  static const int CURRENT_VERSION=2;
345  int nVersion;
348  unsigned int nTime;
349  unsigned int nBits;
350  unsigned int nNonce;
351 
353  {
354  SetNull();
355  }
356 
358  (
359  READWRITE(this->nVersion);
360  nVersion = this->nVersion;
361  READWRITE(hashPrevBlock);
362  READWRITE(hashMerkleRoot);
363  READWRITE(nTime);
364  READWRITE(nBits);
365  READWRITE(nNonce);
366  )
367 
368  void SetNull()
369  {
371  hashPrevBlock = 0;
372  hashMerkleRoot = 0;
373  nTime = 0;
374  nBits = 0;
375  nNonce = 0;
376  }
377 
378  bool IsNull() const
379  {
380  return (nBits == 0);
381  }
382 
383  uint256 GetHash() const;
384 
385  uint256 GetPoWHash() const;
386 
387  int64_t GetBlockTime() const
388  {
389  return (int64_t)nTime;
390  }
391 };
392 
393 
394 class CBlock : public CBlockHeader
395 {
396 public:
397  // network and disk
398  std::vector<CTransaction> vtx;
399 
400  // memory only
401  mutable std::vector<uint256> vMerkleTree;
402 
404  {
405  SetNull();
406  }
407 
408  CBlock(const CBlockHeader &header)
409  {
410  SetNull();
411  *((CBlockHeader*)this) = header;
412  }
413 
415  (
416  READWRITE(*(CBlockHeader*)this);
417  READWRITE(vtx);
418  )
419 
420  void SetNull()
421  {
422  CBlockHeader::SetNull();
423  vtx.clear();
424  vMerkleTree.clear();
425  }
426 
428  {
429  CBlockHeader block;
430  block.nVersion = nVersion;
431  block.hashPrevBlock = hashPrevBlock;
432  block.hashMerkleRoot = hashMerkleRoot;
433  block.nTime = nTime;
434  block.nBits = nBits;
435  block.nNonce = nNonce;
436  return block;
437  }
438 
439  uint256 BuildMerkleTree() const;
440 
441  const uint256 &GetTxHash(unsigned int nIndex) const {
442  assert(vMerkleTree.size() > 0); // BuildMerkleTree must have been called first
443  assert(nIndex < vtx.size());
444  return vMerkleTree[nIndex];
445  }
446 
447  std::vector<uint256> GetMerkleBranch(int nIndex) const;
448  static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
449  void print() const;
450 };
451 
452 
458 {
459  std::vector<uint256> vHave;
460 
462 
463  CBlockLocator(const std::vector<uint256>& vHaveIn)
464  {
465  vHave = vHaveIn;
466  }
467 
469  (
470  if (!(nType & SER_GETHASH))
471  READWRITE(nVersion);
472  READWRITE(vHave);
473  )
474 
475  void SetNull()
476  {
477  vHave.clear();
478  }
479 
480  bool IsNull()
481  {
482  return vHave.empty();
483  }
484 };
485 
486 #endif
#define VARINT(obj)
Definition: serialize.h:319
bool IsDust(int64_t nMinRelayTxFee) const
Definition: core.h:153
CBlockHeader()
Definition: core.h:352
int64_t GetValueOut() const
Definition: core.cpp:110
unsigned int n
Definition: core.h:60
int nVersion
Definition: core.h:185
COutPoint()
Definition: core.h:30
CBlockLocator()
Definition: core.h:461
int nVersion
Definition: core.h:345
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: core.h:46
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: core.h:457
CScript scriptPubKey
Definition: core.h:125
#define READWRITE(obj)
Definition: serialize.h:101
void print() const
Definition: core.cpp:50
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
Definition: core.cpp:122
Definition: core.h:394
CBlock(const CBlockHeader &header)
Definition: core.h:408
bool IsNull() const
Definition: core.h:146
CTxInUndo()
Definition: core.h:291
wrapper for CTxOut that provides a more compact serialization
Definition: core.h:252
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: core.h:294
void Serialize(Stream &s, char a, int, int=0)
Definition: serialize.h:128
void print() const
Definition: core.cpp:71
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: core.h:160
void print() const
Definition: core.cpp:157
IMPLEMENT_SERIALIZE(READWRITE(prevout);READWRITE(scriptSig);READWRITE(nSequence);) bool IsFinal() const
Definition: core.h:88
uint256 GetHash() const
Definition: core.cpp:76
bool MoneyRange(int64_t nValue)
Definition: core.h:21
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:63
Compact serializer for scripts.
Definition: script.h:737
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: core.h:166
CTxOutCompressor(CTxOut &txoutIn)
Definition: core.h:261
unsigned int n
Definition: core.h:28
static int64_t nMinTxFee
Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) ...
Definition: core.h:182
CBlock()
Definition: core.h:403
#define FLATDATA(obj)
Definition: serialize.h:318
Undo information for a CTxIn.
Definition: core.h:283
std::string ToString() const
Definition: core.cpp:66
bool IsNewerThan(const CTransaction &old) const
Definition: core.cpp:81
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: core.h:41
IMPLEMENT_SERIALIZE(READWRITE(this->nVersion);nVersion=this->nVersion;READWRITE(vin);READWRITE(vout);READWRITE(nLockTime);) void SetNull()
Definition: core.h:196
void Serialize(Stream &s, int nType, int nVersion) const
Definition: core.h:301
unsigned int nLockTime
Definition: core.h:188
uint256 hashMerkleRoot
Definition: core.h:347
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:114
int64_t GetBlockTime() const
Definition: core.h:387
An input of a transaction.
Definition: core.h:72
void SetNull()
Definition: core.h:64
bool IsNull() const
Definition: core.h:65
bool fCoinBase
Definition: core.h:287
std::vector< CTxOut > vout
Definition: core.h:187
void Unserialize(Stream &s, char &a, int, int=0)
Definition: serialize.h:142
CBlockHeader GetBlockHeader() const
Definition: core.h:427
CInPoint(const CTransaction *ptxIn, unsigned int nIn)
Definition: core.h:63
std::vector< CTxIn > vin
Definition: core.h:186
uint256 hashPrevBlock
Definition: core.h:346
bool IsNull()
Definition: core.h:480
CTxOut txout
Definition: core.h:286
unsigned int nNonce
Definition: core.h:350
std::vector< uint256 > vMerkleTree
Definition: core.h:401
An output of a transaction.
Definition: core.h:121
std::vector< uint256 > vHave
Definition: core.h:459
static uint64_t CompressAmount(uint64_t nAmount)
Definition: core.cpp:171
unsigned int nHeight
Definition: core.h:288
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:24
std::vector< CTxInUndo > vprevout
Definition: core.h:325
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: core.h:463
CTxOut()
Definition: core.h:127
std::string ToString() const
Definition: core.cpp:141
CInPoint()
Definition: core.h:62
static const int CURRENT_VERSION
Definition: core.h:344
uint256 GetHash() const
Definition: core.cpp:61
std::string ToString() const
Definition: core.cpp:35
IMPLEMENT_SERIALIZE(READWRITE(FLATDATA(*this));) void SetNull()
Definition: core.h:32
const uint256 & GetTxHash(unsigned int nIndex) const
Definition: core.h:441
COutPoint(uint256 hashIn, unsigned int nIn)
Definition: core.h:31
CScript scriptSig
Definition: core.h:76
static uint64_t DecompressAmount(uint64_t nAmount)
Definition: core.cpp:190
unsigned int nSequence
Definition: core.h:77
std::string ToString() const
Definition: core.cpp:11
IMPLEMENT_SERIALIZE(READWRITE(nValue);READWRITE(scriptPubKey);) void SetNull()
Definition: core.h:135
256-bit unsigned integer
Definition: uint256.h:532
unsigned int nTime
Definition: core.h:348
void Unserialize(Stream &s, int nType, int nVersion)
Definition: core.h:309
CTxOut & txout
Definition: core.h:255
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
CTxIn()
Definition: core.h:79
Undo information for a CTransaction.
Definition: core.h:321
CTransaction()
Definition: core.h:190
An inpoint - a combination of a transaction and an index n into its vin.
Definition: core.h:56
static int64_t nMinRelayTxFee
Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) ...
Definition: core.h:183
bool IsCoinBase() const
Definition: core.h:228
bool IsNull() const
Definition: core.h:34
int nVersion
Definition: core.h:289
std::vector< CTransaction > vtx
Definition: core.h:398
bool IsNull() const
Definition: core.h:378
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: core.h:36
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:179
static const int CURRENT_VERSION
Definition: core.h:184
unsigned int nBits
Definition: core.h:349
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: core.h:99
const CTransaction * ptx
Definition: core.h:59
CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn=false, unsigned int nHeightIn=0, int nVersionIn=0)
Definition: core.h:292
COutPoint prevout
Definition: core.h:75
T & REF(const T &val)
Definition: serialize.h:41
void print() const
Definition: core.cpp:16
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: core.h:106
bool IsNull() const
Definition: core.h:212
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: core.h:340
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: core.h:233
int64_t nValue
Definition: core.h:124
uint256 hash
Definition: core.h:27
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: core.h:241