You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core.h 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. #ifndef BITCOIN_CORE_H
  6. #define BITCOIN_CORE_H
  7. #include "script.h"
  8. #include "serialize.h"
  9. #include "uint256.h"
  10. #include <stdint.h>
  11. #include <boost/foreach.hpp>
  12. class CTransaction;
  13. /** An outpoint - a combination of a transaction hash and an index n into its vout */
  14. class COutPoint
  15. {
  16. public:
  17. uint256 hash;
  18. unsigned int n;
  19. COutPoint() { SetNull(); }
  20. COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
  21. IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
  22. void SetNull() { hash = 0; n = (unsigned int) -1; }
  23. bool IsNull() const { return (hash == 0 && n == (unsigned int) -1); }
  24. friend bool operator<(const COutPoint& a, const COutPoint& b)
  25. {
  26. return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
  27. }
  28. friend bool operator==(const COutPoint& a, const COutPoint& b)
  29. {
  30. return (a.hash == b.hash && a.n == b.n);
  31. }
  32. friend bool operator!=(const COutPoint& a, const COutPoint& b)
  33. {
  34. return !(a == b);
  35. }
  36. std::string ToString() const;
  37. void print() const;
  38. };
  39. /** An inpoint - a combination of a transaction and an index n into its vin */
  40. class CInPoint
  41. {
  42. public:
  43. CTransaction* ptx;
  44. unsigned int n;
  45. CInPoint() { SetNull(); }
  46. CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
  47. void SetNull() { ptx = NULL; n = (unsigned int) -1; }
  48. bool IsNull() const { return (ptx == NULL && n == (unsigned int) -1); }
  49. };
  50. /** An input of a transaction. It contains the location of the previous
  51. * transaction's output that it claims and a signature that matches the
  52. * output's public key.
  53. */
  54. class CTxIn
  55. {
  56. public:
  57. COutPoint prevout;
  58. CScript scriptSig;
  59. unsigned int nSequence;
  60. CTxIn()
  61. {
  62. nSequence = std::numeric_limits<unsigned int>::max();
  63. }
  64. explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
  65. CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
  66. IMPLEMENT_SERIALIZE
  67. (
  68. READWRITE(prevout);
  69. READWRITE(scriptSig);
  70. READWRITE(nSequence);
  71. )
  72. bool IsFinal() const
  73. {
  74. return (nSequence == std::numeric_limits<unsigned int>::max());
  75. }
  76. friend bool operator==(const CTxIn& a, const CTxIn& b)
  77. {
  78. return (a.prevout == b.prevout &&
  79. a.scriptSig == b.scriptSig &&
  80. a.nSequence == b.nSequence);
  81. }
  82. friend bool operator!=(const CTxIn& a, const CTxIn& b)
  83. {
  84. return !(a == b);
  85. }
  86. std::string ToString() const;
  87. void print() const;
  88. };
  89. /** An output of a transaction. It contains the public key that the next input
  90. * must be able to sign with to claim it.
  91. */
  92. class CTxOut
  93. {
  94. public:
  95. int64_t nValue;
  96. CScript scriptPubKey;
  97. CTxOut()
  98. {
  99. SetNull();
  100. }
  101. CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
  102. IMPLEMENT_SERIALIZE
  103. (
  104. READWRITE(nValue);
  105. READWRITE(scriptPubKey);
  106. )
  107. void SetNull()
  108. {
  109. nValue = -1;
  110. scriptPubKey.clear();
  111. }
  112. bool IsNull() const
  113. {
  114. return (nValue == -1);
  115. }
  116. uint256 GetHash() const;
  117. bool IsDust(int64_t nMinRelayTxFee) const
  118. {
  119. // "Dust" is defined in terms of CTransaction::nMinRelayTxFee,
  120. // which has units satoshis-per-kilobyte.
  121. // If you'd pay more than 1/3 in fees
  122. // to spend something, then we consider it dust.
  123. // A typical txout is 34 bytes big, and will
  124. // need a CTxIn of at least 148 bytes to spend,
  125. // so dust is a txout less than 54 uBTC
  126. // (5460 satoshis) with default nMinRelayTxFee
  127. return ((nValue*1000)/(3*((int)GetSerializeSize(SER_DISK,0)+148)) < nMinRelayTxFee);
  128. }
  129. friend bool operator==(const CTxOut& a, const CTxOut& b)
  130. {
  131. return (a.nValue == b.nValue &&
  132. a.scriptPubKey == b.scriptPubKey);
  133. }
  134. friend bool operator!=(const CTxOut& a, const CTxOut& b)
  135. {
  136. return !(a == b);
  137. }
  138. std::string ToString() const;
  139. void print() const;
  140. };
  141. /** The basic transaction that is broadcasted on the network and contained in
  142. * blocks. A transaction can contain multiple inputs and outputs.
  143. */
  144. class CTransaction
  145. {
  146. public:
  147. static int64_t nMinTxFee;
  148. static int64_t nMinRelayTxFee;
  149. static const int CURRENT_VERSION=1;
  150. int nVersion;
  151. std::vector<CTxIn> vin;
  152. std::vector<CTxOut> vout;
  153. unsigned int nLockTime;
  154. CTransaction()
  155. {
  156. SetNull();
  157. }
  158. IMPLEMENT_SERIALIZE
  159. (
  160. READWRITE(this->nVersion);
  161. nVersion = this->nVersion;
  162. READWRITE(vin);
  163. READWRITE(vout);
  164. READWRITE(nLockTime);
  165. )
  166. void SetNull()
  167. {
  168. nVersion = CTransaction::CURRENT_VERSION;
  169. vin.clear();
  170. vout.clear();
  171. nLockTime = 0;
  172. }
  173. bool IsNull() const
  174. {
  175. return (vin.empty() && vout.empty());
  176. }
  177. uint256 GetHash() const;
  178. bool IsNewerThan(const CTransaction& old) const;
  179. bool IsCoinBase() const
  180. {
  181. return (vin.size() == 1 && vin[0].prevout.IsNull());
  182. }
  183. friend bool operator==(const CTransaction& a, const CTransaction& b)
  184. {
  185. return (a.nVersion == b.nVersion &&
  186. a.vin == b.vin &&
  187. a.vout == b.vout &&
  188. a.nLockTime == b.nLockTime);
  189. }
  190. friend bool operator!=(const CTransaction& a, const CTransaction& b)
  191. {
  192. return !(a == b);
  193. }
  194. std::string ToString() const;
  195. void print() const;
  196. };
  197. /** wrapper for CTxOut that provides a more compact serialization */
  198. class CTxOutCompressor
  199. {
  200. private:
  201. CTxOut &txout;
  202. public:
  203. static uint64_t CompressAmount(uint64_t nAmount);
  204. static uint64_t DecompressAmount(uint64_t nAmount);
  205. CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
  206. IMPLEMENT_SERIALIZE(({
  207. if (!fRead) {
  208. uint64_t nVal = CompressAmount(txout.nValue);
  209. READWRITE(VARINT(nVal));
  210. } else {
  211. uint64_t nVal = 0;
  212. READWRITE(VARINT(nVal));
  213. txout.nValue = DecompressAmount(nVal);
  214. }
  215. CScriptCompressor cscript(REF(txout.scriptPubKey));
  216. READWRITE(cscript);
  217. });)
  218. };
  219. /** Undo information for a CTxIn
  220. *
  221. * Contains the prevout's CTxOut being spent, and if this was the
  222. * last output of the affected transaction, its metadata as well
  223. * (coinbase or not, height, transaction version)
  224. */
  225. class CTxInUndo
  226. {
  227. public:
  228. CTxOut txout; // the txout data before being spent
  229. bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
  230. unsigned int nHeight; // if the outpoint was the last unspent: its height
  231. int nVersion; // if the outpoint was the last unspent: its version
  232. CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
  233. CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
  234. unsigned int GetSerializeSize(int nType, int nVersion) const {
  235. return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
  236. (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
  237. ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
  238. }
  239. template<typename Stream>
  240. void Serialize(Stream &s, int nType, int nVersion) const {
  241. ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
  242. if (nHeight > 0)
  243. ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
  244. ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
  245. }
  246. template<typename Stream>
  247. void Unserialize(Stream &s, int nType, int nVersion) {
  248. unsigned int nCode = 0;
  249. ::Unserialize(s, VARINT(nCode), nType, nVersion);
  250. nHeight = nCode / 2;
  251. fCoinBase = nCode & 1;
  252. if (nHeight > 0)
  253. ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
  254. ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
  255. }
  256. };
  257. /** Undo information for a CTransaction */
  258. class CTxUndo
  259. {
  260. public:
  261. // undo information for all txins
  262. std::vector<CTxInUndo> vprevout;
  263. IMPLEMENT_SERIALIZE(
  264. READWRITE(vprevout);
  265. )
  266. };
  267. /** pruned version of CTransaction: only retains metadata and unspent transaction outputs
  268. *
  269. * Serialized format:
  270. * - VARINT(nVersion)
  271. * - VARINT(nCode)
  272. * - unspentness bitvector, for vout[2] and further; least significant byte first
  273. * - the non-spent CTxOuts (via CTxOutCompressor)
  274. * - VARINT(nHeight)
  275. *
  276. * The nCode value consists of:
  277. * - bit 1: IsCoinBase()
  278. * - bit 2: vout[0] is not spent
  279. * - bit 4: vout[1] is not spent
  280. * - The higher bits encode N, the number of non-zero bytes in the following bitvector.
  281. * - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at
  282. * least one non-spent output).
  283. *
  284. * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e
  285. * <><><--------------------------------------------><---->
  286. * | \ | /
  287. * version code vout[1] height
  288. *
  289. * - version = 1
  290. * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow)
  291. * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0
  292. * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35
  293. * * 8358: compact amount representation for 60000000000 (600 BTC)
  294. * * 00: special txout type pay-to-pubkey-hash
  295. * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160
  296. * - height = 203998
  297. *
  298. *
  299. * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b
  300. * <><><--><--------------------------------------------------><----------------------------------------------><---->
  301. * / \ \ | | /
  302. * version code unspentness vout[4] vout[16] height
  303. *
  304. * - version = 1
  305. * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent,
  306. * 2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow)
  307. * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent
  308. * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee
  309. * * 86ef97d579: compact amount representation for 234925952 (2.35 BTC)
  310. * * 00: special txout type pay-to-pubkey-hash
  311. * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160
  312. * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4
  313. * * bbd123: compact amount representation for 110397 (0.001 BTC)
  314. * * 00: special txout type pay-to-pubkey-hash
  315. * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160
  316. * - height = 120891
  317. */
  318. class CCoins
  319. {
  320. public:
  321. // whether transaction is a coinbase
  322. bool fCoinBase;
  323. // unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
  324. std::vector<CTxOut> vout;
  325. // at which height this transaction was included in the active block chain
  326. int nHeight;
  327. // version of the CTransaction; accesses to this value should probably check for nHeight as well,
  328. // as new tx version will probably only be introduced at certain heights
  329. int nVersion;
  330. // construct a CCoins from a CTransaction, at a given height
  331. CCoins(const CTransaction &tx, int nHeightIn) : fCoinBase(tx.IsCoinBase()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion) {
  332. ClearUnspendable();
  333. }
  334. // empty constructor
  335. CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
  336. // remove spent outputs at the end of vout
  337. void Cleanup() {
  338. while (vout.size() > 0 && vout.back().IsNull())
  339. vout.pop_back();
  340. if (vout.empty())
  341. std::vector<CTxOut>().swap(vout);
  342. }
  343. void ClearUnspendable() {
  344. BOOST_FOREACH(CTxOut &txout, vout) {
  345. if (txout.scriptPubKey.IsUnspendable())
  346. txout.SetNull();
  347. }
  348. Cleanup();
  349. }
  350. void swap(CCoins &to) {
  351. std::swap(to.fCoinBase, fCoinBase);
  352. to.vout.swap(vout);
  353. std::swap(to.nHeight, nHeight);
  354. std::swap(to.nVersion, nVersion);
  355. }
  356. // equality test
  357. friend bool operator==(const CCoins &a, const CCoins &b) {
  358. // Empty CCoins objects are always equal.
  359. if (a.IsPruned() && b.IsPruned())
  360. return true;
  361. return a.fCoinBase == b.fCoinBase &&
  362. a.nHeight == b.nHeight &&
  363. a.nVersion == b.nVersion &&
  364. a.vout == b.vout;
  365. }
  366. friend bool operator!=(const CCoins &a, const CCoins &b) {
  367. return !(a == b);
  368. }
  369. void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const;
  370. bool IsCoinBase() const {
  371. return fCoinBase;
  372. }
  373. unsigned int GetSerializeSize(int nType, int nVersion) const {
  374. unsigned int nSize = 0;
  375. unsigned int nMaskSize = 0, nMaskCode = 0;
  376. CalcMaskSize(nMaskSize, nMaskCode);
  377. bool fFirst = vout.size() > 0 && !vout[0].IsNull();
  378. bool fSecond = vout.size() > 1 && !vout[1].IsNull();
  379. assert(fFirst || fSecond || nMaskCode);
  380. unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
  381. // version
  382. nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
  383. // size of header code
  384. nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
  385. // spentness bitmask
  386. nSize += nMaskSize;
  387. // txouts themself
  388. for (unsigned int i = 0; i < vout.size(); i++)
  389. if (!vout[i].IsNull())
  390. nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
  391. // height
  392. nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
  393. return nSize;
  394. }
  395. template<typename Stream>
  396. void Serialize(Stream &s, int nType, int nVersion) const {
  397. unsigned int nMaskSize = 0, nMaskCode = 0;
  398. CalcMaskSize(nMaskSize, nMaskCode);
  399. bool fFirst = vout.size() > 0 && !vout[0].IsNull();
  400. bool fSecond = vout.size() > 1 && !vout[1].IsNull();
  401. assert(fFirst || fSecond || nMaskCode);
  402. unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
  403. // version
  404. ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
  405. // header code
  406. ::Serialize(s, VARINT(nCode), nType, nVersion);
  407. // spentness bitmask
  408. for (unsigned int b = 0; b<nMaskSize; b++) {
  409. unsigned char chAvail = 0;
  410. for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
  411. if (!vout[2+b*8+i].IsNull())
  412. chAvail |= (1 << i);
  413. ::Serialize(s, chAvail, nType, nVersion);
  414. }
  415. // txouts themself
  416. for (unsigned int i = 0; i < vout.size(); i++) {
  417. if (!vout[i].IsNull())
  418. ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
  419. }
  420. // coinbase height
  421. ::Serialize(s, VARINT(nHeight), nType, nVersion);
  422. }
  423. template<typename Stream>
  424. void Unserialize(Stream &s, int nType, int nVersion) {
  425. unsigned int nCode = 0;
  426. // version
  427. ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
  428. // header code
  429. ::Unserialize(s, VARINT(nCode), nType, nVersion);
  430. fCoinBase = nCode & 1;
  431. std::vector<bool> vAvail(2, false);
  432. vAvail[0] = nCode & 2;
  433. vAvail[1] = nCode & 4;
  434. unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
  435. // spentness bitmask
  436. while (nMaskCode > 0) {
  437. unsigned char chAvail = 0;
  438. ::Unserialize(s, chAvail, nType, nVersion);
  439. for (unsigned int p = 0; p < 8; p++) {
  440. bool f = (chAvail & (1 << p)) != 0;
  441. vAvail.push_back(f);
  442. }
  443. if (chAvail != 0)
  444. nMaskCode--;
  445. }
  446. // txouts themself
  447. vout.assign(vAvail.size(), CTxOut());
  448. for (unsigned int i = 0; i < vAvail.size(); i++) {
  449. if (vAvail[i])
  450. ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
  451. }
  452. // coinbase height
  453. ::Unserialize(s, VARINT(nHeight), nType, nVersion);
  454. Cleanup();
  455. }
  456. // mark an outpoint spent, and construct undo information
  457. bool Spend(const COutPoint &out, CTxInUndo &undo);
  458. // mark a vout spent
  459. bool Spend(int nPos);
  460. // check whether a particular output is still available
  461. bool IsAvailable(unsigned int nPos) const {
  462. return (nPos < vout.size() && !vout[nPos].IsNull());
  463. }
  464. // check whether the entire CCoins is spent
  465. // note that only !IsPruned() CCoins can be serialized
  466. bool IsPruned() const {
  467. BOOST_FOREACH(const CTxOut &out, vout)
  468. if (!out.IsNull())
  469. return false;
  470. return true;
  471. }
  472. };
  473. /** Nodes collect new transactions into a block, hash them into a hash tree,
  474. * and scan through nonce values to make the block's hash satisfy proof-of-work
  475. * requirements. When they solve the proof-of-work, they broadcast the block
  476. * to everyone and the block is added to the block chain. The first transaction
  477. * in the block is a special one that creates a new coin owned by the creator
  478. * of the block.
  479. */
  480. class CBlockHeader
  481. {
  482. public:
  483. // header
  484. static const int CURRENT_VERSION=2;
  485. int nVersion;
  486. uint256 hashPrevBlock;
  487. uint256 hashMerkleRoot;
  488. unsigned int nTime;
  489. unsigned int nBits;
  490. unsigned int nNonce;
  491. CBlockHeader()
  492. {
  493. SetNull();
  494. }
  495. IMPLEMENT_SERIALIZE
  496. (
  497. READWRITE(this->nVersion);
  498. nVersion = this->nVersion;
  499. READWRITE(hashPrevBlock);
  500. READWRITE(hashMerkleRoot);
  501. READWRITE(nTime);
  502. READWRITE(nBits);
  503. READWRITE(nNonce);
  504. )
  505. void SetNull()
  506. {
  507. nVersion = CBlockHeader::CURRENT_VERSION;
  508. hashPrevBlock = 0;
  509. hashMerkleRoot = 0;
  510. nTime = 0;
  511. nBits = 0;
  512. nNonce = 0;
  513. }
  514. bool IsNull() const
  515. {
  516. return (nBits == 0);
  517. }
  518. uint256 GetHash() const;
  519. int64_t GetBlockTime() const
  520. {
  521. return (int64_t)nTime;
  522. }
  523. };
  524. class CBlock : public CBlockHeader
  525. {
  526. public:
  527. // network and disk
  528. std::vector<CTransaction> vtx;
  529. // memory only
  530. mutable std::vector<uint256> vMerkleTree;
  531. CBlock()
  532. {
  533. SetNull();
  534. }
  535. CBlock(const CBlockHeader &header)
  536. {
  537. SetNull();
  538. *((CBlockHeader*)this) = header;
  539. }
  540. IMPLEMENT_SERIALIZE
  541. (
  542. READWRITE(*(CBlockHeader*)this);
  543. READWRITE(vtx);
  544. )
  545. void SetNull()
  546. {
  547. CBlockHeader::SetNull();
  548. vtx.clear();
  549. vMerkleTree.clear();
  550. }
  551. CBlockHeader GetBlockHeader() const
  552. {
  553. CBlockHeader block;
  554. block.nVersion = nVersion;
  555. block.hashPrevBlock = hashPrevBlock;
  556. block.hashMerkleRoot = hashMerkleRoot;
  557. block.nTime = nTime;
  558. block.nBits = nBits;
  559. block.nNonce = nNonce;
  560. return block;
  561. }
  562. uint256 BuildMerkleTree() const;
  563. const uint256 &GetTxHash(unsigned int nIndex) const {
  564. assert(vMerkleTree.size() > 0); // BuildMerkleTree must have been called first
  565. assert(nIndex < vtx.size());
  566. return vMerkleTree[nIndex];
  567. }
  568. std::vector<uint256> GetMerkleBranch(int nIndex) const;
  569. static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
  570. void print() const;
  571. };
  572. /** Describes a place in the block chain to another node such that if the
  573. * other node doesn't have the same branch, it can find a recent common trunk.
  574. * The further back it is, the further before the fork it may be.
  575. */
  576. struct CBlockLocator
  577. {
  578. std::vector<uint256> vHave;
  579. CBlockLocator() {}
  580. CBlockLocator(const std::vector<uint256>& vHaveIn)
  581. {
  582. vHave = vHaveIn;
  583. }
  584. IMPLEMENT_SERIALIZE
  585. (
  586. if (!(nType & SER_GETHASH))
  587. READWRITE(nVersion);
  588. READWRITE(vHave);
  589. )
  590. void SetNull()
  591. {
  592. vHave.clear();
  593. }
  594. bool IsNull()
  595. {
  596. return vHave.empty();
  597. }
  598. };
  599. #endif