Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // 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 BITCOIN_CORE_H
7 #define BITCOIN_CORE_H
8 
9 #include "script.h"
10 #include "serialize.h"
11 #include "uint256.h"
12 
13 #include <stdint.h>
14 
15 class CTransaction;
16 
18 static const int64_t MAX_MONEY = 21000000 * COIN;
19 inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
20 
22 class COutPoint
23 {
24 public:
26  unsigned int n;
27 
28  COutPoint() { SetNull(); }
29  COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
31  void SetNull() { hash = 0; n = (unsigned int) -1; }
32  bool IsNull() const { return (hash == 0 && n == (unsigned int) -1); }
33 
34  friend bool operator<(const COutPoint& a, const COutPoint& b)
35  {
36  return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
37  }
38 
39  friend bool operator==(const COutPoint& a, const COutPoint& b)
40  {
41  return (a.hash == b.hash && a.n == b.n);
42  }
43 
44  friend bool operator!=(const COutPoint& a, const COutPoint& b)
45  {
46  return !(a == b);
47  }
48 
49  std::string ToString() const;
50  void print() const;
51 };
52 
54 class CInPoint
55 {
56 public:
57  const CTransaction* ptx;
58  unsigned int n;
59 
60  CInPoint() { SetNull(); }
61  CInPoint(const CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
62  void SetNull() { ptx = NULL; n = (unsigned int) -1; }
63  bool IsNull() const { return (ptx == NULL && n == (unsigned int) -1); }
64 };
65 
70 class CTxIn
71 {
72 public:
75  unsigned int nSequence;
76 
78  {
79  nSequence = std::numeric_limits<unsigned int>::max();
80  }
81 
82  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
83  CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
84 
86  (
87  READWRITE(prevout);
88  READWRITE(scriptSig);
89  READWRITE(nSequence);
90  )
91 
92  bool IsFinal() const
93  {
94  return (nSequence == std::numeric_limits<unsigned int>::max());
95  }
96 
97  friend bool operator==(const CTxIn& a, const CTxIn& b)
98  {
99  return (a.prevout == b.prevout &&
100  a.scriptSig == b.scriptSig &&
101  a.nSequence == b.nSequence);
102  }
103 
104  friend bool operator!=(const CTxIn& a, const CTxIn& b)
105  {
106  return !(a == b);
107  }
108 
109  std::string ToString() const;
110  void print() const;
111 };
112 
113 
114 
115 
119 class CTxOut
120 {
121 public:
122  int64_t nValue;
124 
126  {
127  SetNull();
128  }
129 
130  CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
131 
133  (
134  READWRITE(nValue);
135  READWRITE(scriptPubKey);
136  )
137 
138  void SetNull()
139  {
140  nValue = -1;
141  scriptPubKey.clear();
142  }
143 
144  bool IsNull() const
145  {
146  return (nValue == -1);
147  }
148 
149  uint256 GetHash() const;
150 
151  bool IsDust(int64_t nMinRelayTxFee) const
152  {
153  // "Dust" is defined in terms of CTransaction::nMinRelayTxFee,
154  // which has units satoshis-per-kilobyte.
155  // If you'd pay more than 1/3 in fees
156  // to spend something, then we consider it dust.
157  // A typical txout is 34 bytes big, and will
158  // need a CTxIn of at least 148 bytes to spend,
159  // so dust is a txout less than 546 satoshis
160  // with default nMinRelayTxFee.
161  return ((nValue*1000)/(3*((int)GetSerializeSize(SER_DISK,0)+148)) < nMinRelayTxFee);
162  }
163 
164  friend bool operator==(const CTxOut& a, const CTxOut& b)
165  {
166  return (a.nValue == b.nValue &&
167  a.scriptPubKey == b.scriptPubKey);
168  }
169 
170  friend bool operator!=(const CTxOut& a, const CTxOut& b)
171  {
172  return !(a == b);
173  }
174 
175  std::string ToString() const;
176  void print() const;
177 };
178 
179 
184 {
185 public:
186  static int64_t nMinTxFee;
187  static int64_t nMinRelayTxFee;
188  static const int CURRENT_VERSION=1;
189  int nVersion;
190  std::vector<CTxIn> vin;
191  std::vector<CTxOut> vout;
192  unsigned int nLockTime;
193 
195  {
196  SetNull();
197  }
198 
200  (
201  READWRITE(this->nVersion);
202  nVersion = this->nVersion;
203  READWRITE(vin);
204  READWRITE(vout);
205  READWRITE(nLockTime);
206  )
207 
208  void SetNull()
209  {
211  vin.clear();
212  vout.clear();
213  nLockTime = 0;
214  }
215 
216  bool IsNull() const
217  {
218  return (vin.empty() && vout.empty());
219  }
220 
221  uint256 GetHash() const;
222  bool IsNewerThan(const CTransaction& old) const;
223 
224  // Return sum of txouts.
225  int64_t GetValueOut() const;
226  // GetValueIn() is a method on CCoinsViewCache, because
227  // inputs must be known to compute value in.
228 
229  // Compute priority, given priority of inputs and (optionally) tx size
230  double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
231 
232  bool IsCoinBase() const
233  {
234  return (vin.size() == 1 && vin[0].prevout.IsNull());
235  }
236 
237  friend bool operator==(const CTransaction& a, const CTransaction& b)
238  {
239  return (a.nVersion == b.nVersion &&
240  a.vin == b.vin &&
241  a.vout == b.vout &&
242  a.nLockTime == b.nLockTime);
243  }
244 
245  friend bool operator!=(const CTransaction& a, const CTransaction& b)
246  {
247  return !(a == b);
248  }
249 
250 
251  std::string ToString() const;
252  void print() const;
253 };
254 
257 {
258 private:
260 
261 public:
262  static uint64_t CompressAmount(uint64_t nAmount);
263  static uint64_t DecompressAmount(uint64_t nAmount);
264 
265  CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
266 
268  if (!fRead) {
269  uint64_t nVal = CompressAmount(txout.nValue);
270  READWRITE(VARINT(nVal));
271  } else {
272  uint64_t nVal = 0;
273  READWRITE(VARINT(nVal));
274  txout.nValue = DecompressAmount(nVal);
275  }
276  CScriptCompressor cscript(REF(txout.scriptPubKey));
277  READWRITE(cscript);
278  });)
279 };
280 
288 {
289 public:
290  CTxOut txout; // the txout data before being spent
291  bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
292  unsigned int nHeight; // if the outpoint was the last unspent: its height
293  int nVersion; // if the outpoint was the last unspent: its version
294 
295  CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
296  CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
297 
298  unsigned int GetSerializeSize(int nType, int nVersion) const {
299  return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
300  (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
301  ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
302  }
303 
304  template<typename Stream>
305  void Serialize(Stream &s, int nType, int nVersion) const {
306  ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
307  if (nHeight > 0)
308  ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
309  ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
310  }
311 
312  template<typename Stream>
313  void Unserialize(Stream &s, int nType, int nVersion) {
314  unsigned int nCode = 0;
315  ::Unserialize(s, VARINT(nCode), nType, nVersion);
316  nHeight = nCode / 2;
317  fCoinBase = nCode & 1;
318  if (nHeight > 0)
319  ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
320  ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
321  }
322 };
323 
325 class CTxUndo
326 {
327 public:
328  // undo information for all txins
329  std::vector<CTxInUndo> vprevout;
330 
332  READWRITE(vprevout);
333  )
334 };
335 
336 
345 {
346 public:
347  // header
348  static const int CURRENT_VERSION=2;
349  int nVersion;
352  unsigned int nTime;
353  unsigned int nBits;
354  unsigned int nNonce;
355 
357  {
358  SetNull();
359  }
360 
362  (
363  READWRITE(this->nVersion);
364  nVersion = this->nVersion;
365  READWRITE(hashPrevBlock);
366  READWRITE(hashMerkleRoot);
367  READWRITE(nTime);
368  READWRITE(nBits);
369  READWRITE(nNonce);
370  )
371 
372  void SetNull()
373  {
375  hashPrevBlock = 0;
376  hashMerkleRoot = 0;
377  nTime = 0;
378  nBits = 0;
379  nNonce = 0;
380  }
381 
382  bool IsNull() const
383  {
384  return (nBits == 0);
385  }
386 
387  uint256 GetHash() const;
388 
389  int64_t GetBlockTime() const
390  {
391  return (int64_t)nTime;
392  }
393 };
394 
395 
396 class CBlock : public CBlockHeader
397 {
398 public:
399  // network and disk
400  std::vector<CTransaction> vtx;
401 
402  // memory only
403  mutable std::vector<uint256> vMerkleTree;
404 
406  {
407  SetNull();
408  }
409 
410  CBlock(const CBlockHeader &header)
411  {
412  SetNull();
413  *((CBlockHeader*)this) = header;
414  }
415 
417  (
418  READWRITE(*(CBlockHeader*)this);
419  READWRITE(vtx);
420  )
421 
422  void SetNull()
423  {
424  CBlockHeader::SetNull();
425  vtx.clear();
426  vMerkleTree.clear();
427  }
428 
430  {
431  CBlockHeader block;
432  block.nVersion = nVersion;
433  block.hashPrevBlock = hashPrevBlock;
434  block.hashMerkleRoot = hashMerkleRoot;
435  block.nTime = nTime;
436  block.nBits = nBits;
437  block.nNonce = nNonce;
438  return block;
439  }
440 
441  uint256 BuildMerkleTree() const;
442 
443  const uint256 &GetTxHash(unsigned int nIndex) const {
444  assert(vMerkleTree.size() > 0); // BuildMerkleTree must have been called first
445  assert(nIndex < vtx.size());
446  return vMerkleTree[nIndex];
447  }
448 
449  std::vector<uint256> GetMerkleBranch(int nIndex) const;
450  static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
451  void print() const;
452 };
453 
454 
460 {
461  std::vector<uint256> vHave;
462 
464 
465  CBlockLocator(const std::vector<uint256>& vHaveIn)
466  {
467  vHave = vHaveIn;
468  }
469 
471  (
472  if (!(nType & SER_GETHASH))
473  READWRITE(nVersion);
474  READWRITE(vHave);
475  )
476 
477  void SetNull()
478  {
479  vHave.clear();
480  }
481 
482  bool IsNull()
483  {
484  return vHave.empty();
485  }
486 };
487 
488 #endif
#define VARINT(obj)
Definition: serialize.h:310
bool IsDust(int64_t nMinRelayTxFee) const
Definition: core.h:151
CBlockHeader()
Definition: core.h:356
int64_t GetValueOut() const
Definition: core.cpp:109
unsigned int n
Definition: core.h:58
int nVersion
Definition: core.h:189
COutPoint()
Definition: core.h:28
CBlockLocator()
Definition: core.h:463
int nVersion
Definition: core.h:349
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: core.h:44
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: core.h:459
CScript scriptPubKey
Definition: core.h:123
#define READWRITE(obj)
Definition: serialize.h:92
void print() const
Definition: core.cpp:49
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
Definition: core.cpp:121
Definition: core.h:396
CBlock(const CBlockHeader &header)
Definition: core.h:410
bool IsNull() const
Definition: core.h:144
CTxInUndo()
Definition: core.h:295
wrapper for CTxOut that provides a more compact serialization
Definition: core.h:256
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: core.h:298
void Serialize(Stream &s, char a, int, int=0)
Definition: serialize.h:119
void print() const
Definition: core.cpp:70
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: core.h:164
void print() const
Definition: core.cpp:156
IMPLEMENT_SERIALIZE(READWRITE(prevout);READWRITE(scriptSig);READWRITE(nSequence);) bool IsFinal() const
Definition: core.h:86
uint256 GetHash() const
Definition: core.cpp:75
bool MoneyRange(int64_t nValue)
Definition: core.h:19
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:54
Compact serializer for scripts.
Definition: script.h:728
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: core.h:170
CTxOutCompressor(CTxOut &txoutIn)
Definition: core.h:265
unsigned int n
Definition: core.h:26
static int64_t nMinTxFee
Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) ...
Definition: core.h:186
CBlock()
Definition: core.h:405
#define FLATDATA(obj)
Definition: serialize.h:309
Undo information for a CTxIn.
Definition: core.h:287
std::string ToString() const
Definition: core.cpp:65
bool IsNewerThan(const CTransaction &old) const
Definition: core.cpp:80
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: core.h:39
IMPLEMENT_SERIALIZE(READWRITE(this->nVersion);nVersion=this->nVersion;READWRITE(vin);READWRITE(vout);READWRITE(nLockTime);) void SetNull()
Definition: core.h:200
void Serialize(Stream &s, int nType, int nVersion) const
Definition: core.h:305
unsigned int nLockTime
Definition: core.h:192
uint256 hashMerkleRoot
Definition: core.h:351
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:105
int64_t GetBlockTime() const
Definition: core.h:389
An input of a transaction.
Definition: core.h:70
void SetNull()
Definition: core.h:62
bool IsNull() const
Definition: core.h:63
bool fCoinBase
Definition: core.h:291
std::vector< CTxOut > vout
Definition: core.h:191
void Unserialize(Stream &s, char &a, int, int=0)
Definition: serialize.h:133
CBlockHeader GetBlockHeader() const
Definition: core.h:429
CInPoint(const CTransaction *ptxIn, unsigned int nIn)
Definition: core.h:61
std::vector< CTxIn > vin
Definition: core.h:190
uint256 hashPrevBlock
Definition: core.h:350
bool IsNull()
Definition: core.h:482
CTxOut txout
Definition: core.h:290
unsigned int nNonce
Definition: core.h:354
std::vector< uint256 > vMerkleTree
Definition: core.h:403
An output of a transaction.
Definition: core.h:119
std::vector< uint256 > vHave
Definition: core.h:461
static uint64_t CompressAmount(uint64_t nAmount)
Definition: core.cpp:170
unsigned int nHeight
Definition: core.h:292
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:22
std::vector< CTxInUndo > vprevout
Definition: core.h:329
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: core.h:465
CTxOut()
Definition: core.h:125
std::string ToString() const
Definition: core.cpp:140
CInPoint()
Definition: core.h:60
static const int CURRENT_VERSION
Definition: core.h:348
uint256 GetHash() const
Definition: core.cpp:60
std::string ToString() const
Definition: core.cpp:34
IMPLEMENT_SERIALIZE(READWRITE(FLATDATA(*this));) void SetNull()
Definition: core.h:30
const uint256 & GetTxHash(unsigned int nIndex) const
Definition: core.h:443
COutPoint(uint256 hashIn, unsigned int nIn)
Definition: core.h:29
CScript scriptSig
Definition: core.h:74
static uint64_t DecompressAmount(uint64_t nAmount)
Definition: core.cpp:189
unsigned int nSequence
Definition: core.h:75
std::string ToString() const
Definition: core.cpp:10
IMPLEMENT_SERIALIZE(READWRITE(nValue);READWRITE(scriptPubKey);) void SetNull()
Definition: core.h:133
256-bit unsigned integer
Definition: uint256.h:531
static const int64_t COIN
Definition: util.h:38
unsigned int nTime
Definition: core.h:352
void Unserialize(Stream &s, int nType, int nVersion)
Definition: core.h:313
CTxOut & txout
Definition: core.h:259
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:401
CTxIn()
Definition: core.h:77
Undo information for a CTransaction.
Definition: core.h:325
CTransaction()
Definition: core.h:194
An inpoint - a combination of a transaction and an index n into its vin.
Definition: core.h:54
static int64_t nMinRelayTxFee
Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) ...
Definition: core.h:187
bool IsCoinBase() const
Definition: core.h:232
bool IsNull() const
Definition: core.h:32
int nVersion
Definition: core.h:293
std::vector< CTransaction > vtx
Definition: core.h:400
bool IsNull() const
Definition: core.h:382
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: core.h:34
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
static const int CURRENT_VERSION
Definition: core.h:188
unsigned int nBits
Definition: core.h:353
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: core.h:97
const CTransaction * ptx
Definition: core.h:57
CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn=false, unsigned int nHeightIn=0, int nVersionIn=0)
Definition: core.h:296
COutPoint prevout
Definition: core.h:73
T & REF(const T &val)
Definition: serialize.h:35
void print() const
Definition: core.cpp:15
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: core.h:104
bool IsNull() const
Definition: core.h:216
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: core.h:344
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: core.h:237
int64_t nValue
Definition: core.h:122
uint256 hash
Definition: core.h:25
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: core.h:245
static const int64_t MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: core.h:18