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.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include "core.h"
  6. #include "util.h"
  7. #include <stdint.h>
  8. std::string COutPoint::ToString() const
  9. {
  10. return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10).c_str(), n);
  11. }
  12. void COutPoint::print() const
  13. {
  14. LogPrintf("%s\n", ToString().c_str());
  15. }
  16. CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, unsigned int nSequenceIn)
  17. {
  18. prevout = prevoutIn;
  19. scriptSig = scriptSigIn;
  20. nSequence = nSequenceIn;
  21. }
  22. CTxIn::CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn, unsigned int nSequenceIn)
  23. {
  24. prevout = COutPoint(hashPrevTx, nOut);
  25. scriptSig = scriptSigIn;
  26. nSequence = nSequenceIn;
  27. }
  28. std::string CTxIn::ToString() const
  29. {
  30. std::string str;
  31. str += "CTxIn(";
  32. str += prevout.ToString();
  33. if (prevout.IsNull())
  34. str += strprintf(", coinbase %s", HexStr(scriptSig).c_str());
  35. else
  36. str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str());
  37. if (nSequence != std::numeric_limits<unsigned int>::max())
  38. str += strprintf(", nSequence=%u", nSequence);
  39. str += ")";
  40. return str;
  41. }
  42. void CTxIn::print() const
  43. {
  44. LogPrintf("%s\n", ToString().c_str());
  45. }
  46. CTxOut::CTxOut(int64_t nValueIn, CScript scriptPubKeyIn)
  47. {
  48. nValue = nValueIn;
  49. scriptPubKey = scriptPubKeyIn;
  50. }
  51. uint256 CTxOut::GetHash() const
  52. {
  53. return SerializeHash(*this);
  54. }
  55. std::string CTxOut::ToString() const
  56. {
  57. return strprintf("CTxOut(nValue=%"PRId64".%08"PRId64", scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30).c_str());
  58. }
  59. void CTxOut::print() const
  60. {
  61. LogPrintf("%s\n", ToString().c_str());
  62. }
  63. uint256 CTransaction::GetHash() const
  64. {
  65. return SerializeHash(*this);
  66. }
  67. bool CTransaction::IsNewerThan(const CTransaction& old) const
  68. {
  69. if (vin.size() != old.vin.size())
  70. return false;
  71. for (unsigned int i = 0; i < vin.size(); i++)
  72. if (vin[i].prevout != old.vin[i].prevout)
  73. return false;
  74. bool fNewer = false;
  75. unsigned int nLowest = std::numeric_limits<unsigned int>::max();
  76. for (unsigned int i = 0; i < vin.size(); i++)
  77. {
  78. if (vin[i].nSequence != old.vin[i].nSequence)
  79. {
  80. if (vin[i].nSequence <= nLowest)
  81. {
  82. fNewer = false;
  83. nLowest = vin[i].nSequence;
  84. }
  85. if (old.vin[i].nSequence < nLowest)
  86. {
  87. fNewer = true;
  88. nLowest = old.vin[i].nSequence;
  89. }
  90. }
  91. }
  92. return fNewer;
  93. }
  94. std::string CTransaction::ToString() const
  95. {
  96. std::string str;
  97. str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%"PRIszu", vout.size=%"PRIszu", nLockTime=%u)\n",
  98. GetHash().ToString().substr(0,10).c_str(),
  99. nVersion,
  100. vin.size(),
  101. vout.size(),
  102. nLockTime);
  103. for (unsigned int i = 0; i < vin.size(); i++)
  104. str += " " + vin[i].ToString() + "\n";
  105. for (unsigned int i = 0; i < vout.size(); i++)
  106. str += " " + vout[i].ToString() + "\n";
  107. return str;
  108. }
  109. void CTransaction::print() const
  110. {
  111. LogPrintf("%s", ToString().c_str());
  112. }
  113. // Amount compression:
  114. // * If the amount is 0, output 0
  115. // * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
  116. // * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
  117. // * call the result n
  118. // * output 1 + 10*(9*n + d - 1) + e
  119. // * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
  120. // (this is decodable, as d is in [1-9] and e is in [0-9])
  121. uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
  122. {
  123. if (n == 0)
  124. return 0;
  125. int e = 0;
  126. while (((n % 10) == 0) && e < 9) {
  127. n /= 10;
  128. e++;
  129. }
  130. if (e < 9) {
  131. int d = (n % 10);
  132. assert(d >= 1 && d <= 9);
  133. n /= 10;
  134. return 1 + (n*9 + d - 1)*10 + e;
  135. } else {
  136. return 1 + (n - 1)*10 + 9;
  137. }
  138. }
  139. uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
  140. {
  141. // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
  142. if (x == 0)
  143. return 0;
  144. x--;
  145. // x = 10*(9*n + d - 1) + e
  146. int e = x % 10;
  147. x /= 10;
  148. uint64_t n = 0;
  149. if (e < 9) {
  150. // x = 9*n + d - 1
  151. int d = (x % 9) + 1;
  152. x /= 9;
  153. // x = n
  154. n = x*10 + d;
  155. } else {
  156. n = x+1;
  157. }
  158. while (e) {
  159. n *= 10;
  160. e--;
  161. }
  162. return n;
  163. }
  164. // calculate number of bytes for the bitmask, and its number of non-zero bytes
  165. // each bit in the bitmask represents the availability of one output, but the
  166. // availabilities of the first two outputs are encoded separately
  167. void CCoins::CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const {
  168. unsigned int nLastUsedByte = 0;
  169. for (unsigned int b = 0; 2+b*8 < vout.size(); b++) {
  170. bool fZero = true;
  171. for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) {
  172. if (!vout[2+b*8+i].IsNull()) {
  173. fZero = false;
  174. continue;
  175. }
  176. }
  177. if (!fZero) {
  178. nLastUsedByte = b + 1;
  179. nNonzeroBytes++;
  180. }
  181. }
  182. nBytes += nLastUsedByte;
  183. }
  184. bool CCoins::Spend(const COutPoint &out, CTxInUndo &undo) {
  185. if (out.n >= vout.size())
  186. return false;
  187. if (vout[out.n].IsNull())
  188. return false;
  189. undo = CTxInUndo(vout[out.n]);
  190. vout[out.n].SetNull();
  191. Cleanup();
  192. if (vout.size() == 0) {
  193. undo.nHeight = nHeight;
  194. undo.fCoinBase = fCoinBase;
  195. undo.nVersion = this->nVersion;
  196. }
  197. return true;
  198. }
  199. bool CCoins::Spend(int nPos) {
  200. CTxInUndo undo;
  201. COutPoint out(0, nPos);
  202. return Spend(out, undo);
  203. }
  204. uint256 CBlockHeader::GetHash() const
  205. {
  206. return Hash(BEGIN(nVersion), END(nNonce));
  207. }
  208. uint256 CBlock::BuildMerkleTree() const
  209. {
  210. vMerkleTree.clear();
  211. BOOST_FOREACH(const CTransaction& tx, vtx)
  212. vMerkleTree.push_back(tx.GetHash());
  213. int j = 0;
  214. for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
  215. {
  216. for (int i = 0; i < nSize; i += 2)
  217. {
  218. int i2 = std::min(i+1, nSize-1);
  219. vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
  220. BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
  221. }
  222. j += nSize;
  223. }
  224. return (vMerkleTree.empty() ? 0 : vMerkleTree.back());
  225. }
  226. std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
  227. {
  228. if (vMerkleTree.empty())
  229. BuildMerkleTree();
  230. std::vector<uint256> vMerkleBranch;
  231. int j = 0;
  232. for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
  233. {
  234. int i = std::min(nIndex^1, nSize-1);
  235. vMerkleBranch.push_back(vMerkleTree[j+i]);
  236. nIndex >>= 1;
  237. j += nSize;
  238. }
  239. return vMerkleBranch;
  240. }
  241. uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
  242. {
  243. if (nIndex == -1)
  244. return 0;
  245. BOOST_FOREACH(const uint256& otherside, vMerkleBranch)
  246. {
  247. if (nIndex & 1)
  248. hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash));
  249. else
  250. hash = Hash(BEGIN(hash), END(hash), BEGIN(otherside), END(otherside));
  251. nIndex >>= 1;
  252. }
  253. return hash;
  254. }
  255. void CBlock::print() const
  256. {
  257. LogPrintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%"PRIszu")\n",
  258. GetHash().ToString().c_str(),
  259. nVersion,
  260. hashPrevBlock.ToString().c_str(),
  261. hashMerkleRoot.ToString().c_str(),
  262. nTime, nBits, nNonce,
  263. vtx.size());
  264. for (unsigned int i = 0; i < vtx.size(); i++)
  265. {
  266. LogPrintf(" ");
  267. vtx[i].print();
  268. }
  269. LogPrintf(" vMerkleTree: ");
  270. for (unsigned int i = 0; i < vMerkleTree.size(); i++)
  271. LogPrintf("%s ", vMerkleTree[i].ToString().c_str());
  272. LogPrintf("\n");
  273. }