PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core.h

https://gitlab.com/freedmpure/bitcoin
C Header | 476 lines | 345 code | 84 blank | 47 comment | 36 complexity | e1b49bc587139efb1d464cda64a3257f MD5 | raw 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. #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. class CTransaction;
  12. /** An outpoint - a combination of a transaction hash and an index n into its vout */
  13. class COutPoint
  14. {
  15. public:
  16. uint256 hash;
  17. unsigned int n;
  18. COutPoint() { SetNull(); }
  19. COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
  20. IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
  21. void SetNull() { hash = 0; n = (unsigned int) -1; }
  22. bool IsNull() const { return (hash == 0 && n == (unsigned int) -1); }
  23. friend bool operator<(const COutPoint& a, const COutPoint& b)
  24. {
  25. return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
  26. }
  27. friend bool operator==(const COutPoint& a, const COutPoint& b)
  28. {
  29. return (a.hash == b.hash && a.n == b.n);
  30. }
  31. friend bool operator!=(const COutPoint& a, const COutPoint& b)
  32. {
  33. return !(a == b);
  34. }
  35. std::string ToString() const;
  36. void print() const;
  37. };
  38. /** An inpoint - a combination of a transaction and an index n into its vin */
  39. class CInPoint
  40. {
  41. public:
  42. CTransaction* ptx;
  43. unsigned int n;
  44. CInPoint() { SetNull(); }
  45. CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
  46. void SetNull() { ptx = NULL; n = (unsigned int) -1; }
  47. bool IsNull() const { return (ptx == NULL && n == (unsigned int) -1); }
  48. };
  49. /** An input of a transaction. It contains the location of the previous
  50. * transaction's output that it claims and a signature that matches the
  51. * output's public key.
  52. */
  53. class CTxIn
  54. {
  55. public:
  56. COutPoint prevout;
  57. CScript scriptSig;
  58. unsigned int nSequence;
  59. CTxIn()
  60. {
  61. nSequence = std::numeric_limits<unsigned int>::max();
  62. }
  63. explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
  64. CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
  65. IMPLEMENT_SERIALIZE
  66. (
  67. READWRITE(prevout);
  68. READWRITE(scriptSig);
  69. READWRITE(nSequence);
  70. )
  71. bool IsFinal() const
  72. {
  73. return (nSequence == std::numeric_limits<unsigned int>::max());
  74. }
  75. friend bool operator==(const CTxIn& a, const CTxIn& b)
  76. {
  77. return (a.prevout == b.prevout &&
  78. a.scriptSig == b.scriptSig &&
  79. a.nSequence == b.nSequence);
  80. }
  81. friend bool operator!=(const CTxIn& a, const CTxIn& b)
  82. {
  83. return !(a == b);
  84. }
  85. std::string ToString() const;
  86. void print() const;
  87. };
  88. /** An output of a transaction. It contains the public key that the next input
  89. * must be able to sign with to claim it.
  90. */
  91. class CTxOut
  92. {
  93. public:
  94. int64_t nValue;
  95. CScript scriptPubKey;
  96. CTxOut()
  97. {
  98. SetNull();
  99. }
  100. CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
  101. IMPLEMENT_SERIALIZE
  102. (
  103. READWRITE(nValue);
  104. READWRITE(scriptPubKey);
  105. )
  106. void SetNull()
  107. {
  108. nValue = -1;
  109. scriptPubKey.clear();
  110. }
  111. bool IsNull() const
  112. {
  113. return (nValue == -1);
  114. }
  115. uint256 GetHash() const;
  116. bool IsDust(int64_t nMinRelayTxFee) const
  117. {
  118. // "Dust" is defined in terms of CTransaction::nMinRelayTxFee,
  119. // which has units satoshis-per-kilobyte.
  120. // If you'd pay more than 1/3 in fees
  121. // to spend something, then we consider it dust.
  122. // A typical txout is 34 bytes big, and will
  123. // need a CTxIn of at least 148 bytes to spend,
  124. // so dust is a txout less than 54 uBTC
  125. // (5460 satoshis) with default nMinRelayTxFee
  126. return ((nValue*1000)/(3*((int)GetSerializeSize(SER_DISK,0)+148)) < nMinRelayTxFee);
  127. }
  128. friend bool operator==(const CTxOut& a, const CTxOut& b)
  129. {
  130. return (a.nValue == b.nValue &&
  131. a.scriptPubKey == b.scriptPubKey);
  132. }
  133. friend bool operator!=(const CTxOut& a, const CTxOut& b)
  134. {
  135. return !(a == b);
  136. }
  137. std::string ToString() const;
  138. void print() const;
  139. };
  140. /** The basic transaction that is broadcasted on the network and contained in
  141. * blocks. A transaction can contain multiple inputs and outputs.
  142. */
  143. class CTransaction
  144. {
  145. public:
  146. static int64_t nMinTxFee;
  147. static int64_t nMinRelayTxFee;
  148. static const int CURRENT_VERSION=1;
  149. int nVersion;
  150. std::vector<CTxIn> vin;
  151. std::vector<CTxOut> vout;
  152. unsigned int nLockTime;
  153. CTransaction()
  154. {
  155. SetNull();
  156. }
  157. IMPLEMENT_SERIALIZE
  158. (
  159. READWRITE(this->nVersion);
  160. nVersion = this->nVersion;
  161. READWRITE(vin);
  162. READWRITE(vout);
  163. READWRITE(nLockTime);
  164. )
  165. void SetNull()
  166. {
  167. nVersion = CTransaction::CURRENT_VERSION;
  168. vin.clear();
  169. vout.clear();
  170. nLockTime = 0;
  171. }
  172. bool IsNull() const
  173. {
  174. return (vin.empty() && vout.empty());
  175. }
  176. uint256 GetHash() const;
  177. bool IsNewerThan(const CTransaction& old) const;
  178. bool IsCoinBase() const
  179. {
  180. return (vin.size() == 1 && vin[0].prevout.IsNull());
  181. }
  182. friend bool operator==(const CTransaction& a, const CTransaction& b)
  183. {
  184. return (a.nVersion == b.nVersion &&
  185. a.vin == b.vin &&
  186. a.vout == b.vout &&
  187. a.nLockTime == b.nLockTime);
  188. }
  189. friend bool operator!=(const CTransaction& a, const CTransaction& b)
  190. {
  191. return !(a == b);
  192. }
  193. std::string ToString() const;
  194. void print() const;
  195. };
  196. /** wrapper for CTxOut that provides a more compact serialization */
  197. class CTxOutCompressor
  198. {
  199. private:
  200. CTxOut &txout;
  201. public:
  202. static uint64_t CompressAmount(uint64_t nAmount);
  203. static uint64_t DecompressAmount(uint64_t nAmount);
  204. CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
  205. IMPLEMENT_SERIALIZE(({
  206. if (!fRead) {
  207. uint64_t nVal = CompressAmount(txout.nValue);
  208. READWRITE(VARINT(nVal));
  209. } else {
  210. uint64_t nVal = 0;
  211. READWRITE(VARINT(nVal));
  212. txout.nValue = DecompressAmount(nVal);
  213. }
  214. CScriptCompressor cscript(REF(txout.scriptPubKey));
  215. READWRITE(cscript);
  216. });)
  217. };
  218. /** Undo information for a CTxIn
  219. *
  220. * Contains the prevout's CTxOut being spent, and if this was the
  221. * last output of the affected transaction, its metadata as well
  222. * (coinbase or not, height, transaction version)
  223. */
  224. class CTxInUndo
  225. {
  226. public:
  227. CTxOut txout; // the txout data before being spent
  228. bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
  229. unsigned int nHeight; // if the outpoint was the last unspent: its height
  230. int nVersion; // if the outpoint was the last unspent: its version
  231. CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
  232. CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
  233. unsigned int GetSerializeSize(int nType, int nVersion) const {
  234. return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
  235. (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
  236. ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
  237. }
  238. template<typename Stream>
  239. void Serialize(Stream &s, int nType, int nVersion) const {
  240. ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
  241. if (nHeight > 0)
  242. ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
  243. ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
  244. }
  245. template<typename Stream>
  246. void Unserialize(Stream &s, int nType, int nVersion) {
  247. unsigned int nCode = 0;
  248. ::Unserialize(s, VARINT(nCode), nType, nVersion);
  249. nHeight = nCode / 2;
  250. fCoinBase = nCode & 1;
  251. if (nHeight > 0)
  252. ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
  253. ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
  254. }
  255. };
  256. /** Undo information for a CTransaction */
  257. class CTxUndo
  258. {
  259. public:
  260. // undo information for all txins
  261. std::vector<CTxInUndo> vprevout;
  262. IMPLEMENT_SERIALIZE(
  263. READWRITE(vprevout);
  264. )
  265. };
  266. /** Nodes collect new transactions into a block, hash them into a hash tree,
  267. * and scan through nonce values to make the block's hash satisfy proof-of-work
  268. * requirements. When they solve the proof-of-work, they broadcast the block
  269. * to everyone and the block is added to the block chain. The first transaction
  270. * in the block is a special one that creates a new coin owned by the creator
  271. * of the block.
  272. */
  273. class CBlockHeader
  274. {
  275. public:
  276. // header
  277. static const int CURRENT_VERSION=2;
  278. int nVersion;
  279. uint256 hashPrevBlock;
  280. uint256 hashMerkleRoot;
  281. unsigned int nTime;
  282. unsigned int nBits;
  283. unsigned int nNonce;
  284. CBlockHeader()
  285. {
  286. SetNull();
  287. }
  288. IMPLEMENT_SERIALIZE
  289. (
  290. READWRITE(this->nVersion);
  291. nVersion = this->nVersion;
  292. READWRITE(hashPrevBlock);
  293. READWRITE(hashMerkleRoot);
  294. READWRITE(nTime);
  295. READWRITE(nBits);
  296. READWRITE(nNonce);
  297. )
  298. void SetNull()
  299. {
  300. nVersion = CBlockHeader::CURRENT_VERSION;
  301. hashPrevBlock = 0;
  302. hashMerkleRoot = 0;
  303. nTime = 0;
  304. nBits = 0;
  305. nNonce = 0;
  306. }
  307. bool IsNull() const
  308. {
  309. return (nBits == 0);
  310. }
  311. uint256 GetHash() const;
  312. int64_t GetBlockTime() const
  313. {
  314. return (int64_t)nTime;
  315. }
  316. };
  317. class CBlock : public CBlockHeader
  318. {
  319. public:
  320. // network and disk
  321. std::vector<CTransaction> vtx;
  322. // memory only
  323. mutable std::vector<uint256> vMerkleTree;
  324. CBlock()
  325. {
  326. SetNull();
  327. }
  328. CBlock(const CBlockHeader &header)
  329. {
  330. SetNull();
  331. *((CBlockHeader*)this) = header;
  332. }
  333. IMPLEMENT_SERIALIZE
  334. (
  335. READWRITE(*(CBlockHeader*)this);
  336. READWRITE(vtx);
  337. )
  338. void SetNull()
  339. {
  340. CBlockHeader::SetNull();
  341. vtx.clear();
  342. vMerkleTree.clear();
  343. }
  344. CBlockHeader GetBlockHeader() const
  345. {
  346. CBlockHeader block;
  347. block.nVersion = nVersion;
  348. block.hashPrevBlock = hashPrevBlock;
  349. block.hashMerkleRoot = hashMerkleRoot;
  350. block.nTime = nTime;
  351. block.nBits = nBits;
  352. block.nNonce = nNonce;
  353. return block;
  354. }
  355. uint256 BuildMerkleTree() const;
  356. const uint256 &GetTxHash(unsigned int nIndex) const {
  357. assert(vMerkleTree.size() > 0); // BuildMerkleTree must have been called first
  358. assert(nIndex < vtx.size());
  359. return vMerkleTree[nIndex];
  360. }
  361. std::vector<uint256> GetMerkleBranch(int nIndex) const;
  362. static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
  363. void print() const;
  364. };
  365. /** Describes a place in the block chain to another node such that if the
  366. * other node doesn't have the same branch, it can find a recent common trunk.
  367. * The further back it is, the further before the fork it may be.
  368. */
  369. struct CBlockLocator
  370. {
  371. std::vector<uint256> vHave;
  372. CBlockLocator() {}
  373. CBlockLocator(const std::vector<uint256>& vHaveIn)
  374. {
  375. vHave = vHaveIn;
  376. }
  377. IMPLEMENT_SERIALIZE
  378. (
  379. if (!(nType & SER_GETHASH))
  380. READWRITE(nVersion);
  381. READWRITE(vHave);
  382. )
  383. void SetNull()
  384. {
  385. vHave.clear();
  386. }
  387. bool IsNull()
  388. {
  389. return vHave.empty();
  390. }
  391. };
  392. #endif