2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (c) 2009-2014 The Bitcoin Core developers
|
2021-06-20 22:44:44 +02:00
|
|
|
* Copyright (C) 2019 Tom Zander <tom@flowee.org>
|
2017-11-09 19:34:51 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2014-09-03 02:20:09 +02:00
|
|
|
|
|
|
|
|
#include "chain.h"
|
|
|
|
|
|
2014-10-25 16:46:54 +08:00
|
|
|
/**
|
|
|
|
|
* CChain implementation
|
|
|
|
|
*/
|
2019-04-02 19:56:36 +02:00
|
|
|
CChain::CChain()
|
|
|
|
|
: m_tip(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CChain::CChain(const CChain &o)
|
2019-06-11 22:17:09 +02:00
|
|
|
: m_chain(o.m_chain),
|
2019-04-02 19:56:36 +02:00
|
|
|
m_tip(o.m_tip.load())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-19 21:41:37 -04:00
|
|
|
void CChain::SetTip(CBlockIndex *pindex) {
|
2019-12-01 14:48:03 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2019-04-02 19:56:36 +02:00
|
|
|
if (pindex == nullptr) {
|
2019-06-11 22:17:09 +02:00
|
|
|
m_chain.clear();
|
2019-04-02 19:56:36 +02:00
|
|
|
m_tip = nullptr;
|
2014-10-19 21:41:37 -04:00
|
|
|
return;
|
2014-09-03 02:20:09 +02:00
|
|
|
}
|
2019-06-11 22:17:09 +02:00
|
|
|
m_chain.resize(pindex->nHeight + 1);
|
2019-04-02 19:56:36 +02:00
|
|
|
m_tip = pindex;
|
2019-06-11 22:17:09 +02:00
|
|
|
while (pindex && m_chain[pindex->nHeight] != pindex) {
|
|
|
|
|
m_chain[pindex->nHeight] = pindex;
|
2014-09-03 02:20:09 +02:00
|
|
|
pindex = pindex->pprev;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
|
2019-12-01 14:48:03 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2014-09-03 02:20:09 +02:00
|
|
|
int nStep = 1;
|
|
|
|
|
std::vector<uint256> vHave;
|
|
|
|
|
vHave.reserve(32);
|
|
|
|
|
|
|
|
|
|
if (!pindex)
|
|
|
|
|
pindex = Tip();
|
|
|
|
|
while (pindex) {
|
|
|
|
|
vHave.push_back(pindex->GetBlockHash());
|
|
|
|
|
// Stop when we have added the genesis block.
|
|
|
|
|
if (pindex->nHeight == 0)
|
|
|
|
|
break;
|
|
|
|
|
// Exponentially larger steps back, plus the genesis block.
|
2019-06-13 00:39:36 +02:00
|
|
|
const int nHeight = std::max(pindex->nHeight - nStep, 0);
|
2019-06-13 21:47:23 +02:00
|
|
|
if (nHeight < (int) m_chain.size() && m_chain[nHeight] == pindex) {
|
2014-09-03 02:20:09 +02:00
|
|
|
// Use O(1) CChain index if possible.
|
2019-06-13 00:39:36 +02:00
|
|
|
pindex = m_chain[nHeight];
|
2014-09-03 02:20:09 +02:00
|
|
|
} else {
|
|
|
|
|
// Otherwise, use O(log n) skiplist.
|
|
|
|
|
pindex = pindex->GetAncestor(nHeight);
|
|
|
|
|
}
|
|
|
|
|
if (vHave.size() > 10)
|
|
|
|
|
nStep *= 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CBlockLocator(vHave);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
|
2019-12-01 14:48:03 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2019-06-11 22:17:09 +02:00
|
|
|
if (pindex == nullptr) {
|
|
|
|
|
return nullptr;
|
2014-11-18 22:16:32 +01:00
|
|
|
}
|
2014-09-03 02:20:09 +02:00
|
|
|
if (pindex->nHeight > Height())
|
|
|
|
|
pindex = pindex->GetAncestor(Height());
|
|
|
|
|
while (pindex && !Contains(pindex))
|
|
|
|
|
pindex = pindex->pprev;
|
|
|
|
|
return pindex;
|
|
|
|
|
}
|
2014-11-23 14:07:38 +01:00
|
|
|
|
|
|
|
|
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
|
|
|
|
|
int static inline InvertLowestOne(int n) { return n & (n - 1); }
|
|
|
|
|
|
|
|
|
|
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
|
|
|
|
|
int static inline GetSkipHeight(int height) {
|
|
|
|
|
if (height < 2)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// Determine which height to jump back to. Any number strictly lower than height is acceptable,
|
|
|
|
|
// but the following expression seems to perform well in simulations (max 110 steps to go back
|
|
|
|
|
// up to 2**18 blocks).
|
|
|
|
|
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CBlockIndex* CBlockIndex::GetAncestor(int height)
|
|
|
|
|
{
|
|
|
|
|
if (height > nHeight || height < 0)
|
2019-06-11 22:17:09 +02:00
|
|
|
return nullptr;
|
2014-11-23 14:07:38 +01:00
|
|
|
|
|
|
|
|
CBlockIndex* pindexWalk = this;
|
|
|
|
|
int heightWalk = nHeight;
|
|
|
|
|
while (heightWalk > height) {
|
|
|
|
|
int heightSkip = GetSkipHeight(heightWalk);
|
|
|
|
|
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
|
2019-06-11 22:17:09 +02:00
|
|
|
if (pindexWalk->pskip != nullptr &&
|
2015-03-19 05:34:06 -07:00
|
|
|
(heightSkip == height ||
|
|
|
|
|
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
|
|
|
|
|
heightSkipPrev >= height)))) {
|
2014-11-23 14:07:38 +01:00
|
|
|
// Only follow pskip if pprev->pskip isn't better than pskip->pprev.
|
|
|
|
|
pindexWalk = pindexWalk->pskip;
|
|
|
|
|
heightWalk = heightSkip;
|
|
|
|
|
} else {
|
|
|
|
|
pindexWalk = pindexWalk->pprev;
|
|
|
|
|
heightWalk--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pindexWalk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
|
|
|
|
|
{
|
|
|
|
|
return const_cast<CBlockIndex*>(this)->GetAncestor(height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CBlockIndex::BuildSkip()
|
|
|
|
|
{
|
|
|
|
|
if (pprev)
|
|
|
|
|
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
|
|
|
|
|
}
|