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.

chain.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "chain.h"
  6. /**
  7. * CChain implementation
  8. */
  9. void CChain::SetTip(CBlockIndex *pindex) {
  10. if (pindex == nullptr) {
  11. vChain.clear();
  12. return;
  13. }
  14. vChain.resize(pindex->nHeight + 1);
  15. while (pindex && vChain[pindex->nHeight] != pindex) {
  16. vChain[pindex->nHeight] = pindex;
  17. pindex = pindex->pprev;
  18. }
  19. }
  20. CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
  21. int nStep = 1;
  22. std::vector<uint256> vHave;
  23. vHave.reserve(32);
  24. if (!pindex)
  25. pindex = Tip();
  26. while (pindex) {
  27. vHave.push_back(pindex->GetBlockHash());
  28. // Stop when we have added the genesis block.
  29. if (pindex->nHeight == 0)
  30. break;
  31. // Exponentially larger steps back, plus the genesis block.
  32. int nHeight = std::max(pindex->nHeight - nStep, 0);
  33. if (Contains(pindex)) {
  34. // Use O(1) CChain index if possible.
  35. pindex = (*this)[nHeight];
  36. } else {
  37. // Otherwise, use O(log n) skiplist.
  38. pindex = pindex->GetAncestor(nHeight);
  39. }
  40. if (vHave.size() > 10)
  41. nStep *= 2;
  42. }
  43. return CBlockLocator(vHave);
  44. }
  45. const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
  46. if (pindex == nullptr) {
  47. return nullptr;
  48. }
  49. if (pindex->nHeight > Height())
  50. pindex = pindex->GetAncestor(Height());
  51. while (pindex && !Contains(pindex))
  52. pindex = pindex->pprev;
  53. return pindex;
  54. }
  55. CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime) const
  56. {
  57. std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime,
  58. [](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; });
  59. return (lower == vChain.end() ? nullptr : *lower);
  60. }
  61. /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
  62. int static inline InvertLowestOne(int n) { return n & (n - 1); }
  63. /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
  64. int static inline GetSkipHeight(int height) {
  65. if (height < 2)
  66. return 0;
  67. // Determine which height to jump back to. Any number strictly lower than height is acceptable,
  68. // but the following expression seems to perform well in simulations (max 110 steps to go back
  69. // up to 2**18 blocks).
  70. return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
  71. }
  72. CBlockIndex* CBlockIndex::GetAncestor(int height)
  73. {
  74. if (height > nHeight || height < 0)
  75. return nullptr;
  76. CBlockIndex* pindexWalk = this;
  77. int heightWalk = nHeight;
  78. while (heightWalk > height) {
  79. int heightSkip = GetSkipHeight(heightWalk);
  80. int heightSkipPrev = GetSkipHeight(heightWalk - 1);
  81. if (pindexWalk->pskip != nullptr &&
  82. (heightSkip == height ||
  83. (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
  84. heightSkipPrev >= height)))) {
  85. // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
  86. pindexWalk = pindexWalk->pskip;
  87. heightWalk = heightSkip;
  88. } else {
  89. assert(pindexWalk->pprev);
  90. pindexWalk = pindexWalk->pprev;
  91. heightWalk--;
  92. }
  93. }
  94. return pindexWalk;
  95. }
  96. const CBlockIndex* CBlockIndex::GetAncestor(int height) const
  97. {
  98. return const_cast<CBlockIndex*>(this)->GetAncestor(height);
  99. }
  100. void CBlockIndex::BuildSkip()
  101. {
  102. if (pprev)
  103. pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
  104. }
  105. arith_uint256 GetBlockProof(const CBlockIndex& block)
  106. {
  107. arith_uint256 bnTarget;
  108. bool fNegative;
  109. bool fOverflow;
  110. bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
  111. if (fNegative || fOverflow || bnTarget == 0)
  112. return 0;
  113. // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
  114. // as it's too large for an arith_uint256. However, as 2**256 is at least as large
  115. // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
  116. // or ~bnTarget / (nTarget+1) + 1.
  117. return (~bnTarget / (bnTarget + 1)) + 1;
  118. }
  119. int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
  120. {
  121. arith_uint256 r;
  122. int sign = 1;
  123. if (to.nChainWork > from.nChainWork) {
  124. r = to.nChainWork - from.nChainWork;
  125. } else {
  126. r = from.nChainWork - to.nChainWork;
  127. sign = -1;
  128. }
  129. r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
  130. if (r.bits() > 63) {
  131. return sign * std::numeric_limits<int64_t>::max();
  132. }
  133. return sign * r.GetLow64();
  134. }
  135. /** Find the last common ancestor two blocks have.
  136. * Both pa and pb must be non-nullptr. */
  137. const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
  138. if (pa->nHeight > pb->nHeight) {
  139. pa = pa->GetAncestor(pb->nHeight);
  140. } else if (pb->nHeight > pa->nHeight) {
  141. pb = pb->GetAncestor(pa->nHeight);
  142. }
  143. while (pa != pb && pa && pb) {
  144. pa = pa->pprev;
  145. pb = pb->pprev;
  146. }
  147. // Eventually all chain branches meet at the genesis block.
  148. assert(pa == pb);
  149. return pa;
  150. }