Skip to content

Commit

Permalink
Merge pull request #4796
Browse files Browse the repository at this point in the history
e8b5f0d Move CBlockIndex, CChain and related code out of main (jtimon)
6db83db Decouple CChain from mapBlockIndex (jtimon)
  • Loading branch information
sipa committed Sep 29, 2014
2 parents a38eaea + e8b5f0d commit bf3a5dd
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 442 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ BITCOIN_CORE_H = \
allocators.h \
base58.h \
bloom.h \
chain.h \
chainparams.h \
chainparamsbase.h \
chainparamsseeds.h \
Expand Down Expand Up @@ -145,6 +146,7 @@ libbitcoin_server_a_SOURCES = \
addrman.cpp \
alert.cpp \
bloom.cpp \
chain.cpp \
checkpoints.cpp \
init.cpp \
leveldbwrapper.cpp \
Expand Down
59 changes: 59 additions & 0 deletions src/chain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "chain.h"

using namespace std;

// CChain implementation

CBlockIndex *CChain::SetTip(CBlockIndex *pindex) {
if (pindex == NULL) {
vChain.clear();
return NULL;
}
vChain.resize(pindex->nHeight + 1);
while (pindex && vChain[pindex->nHeight] != pindex) {
vChain[pindex->nHeight] = pindex;
pindex = pindex->pprev;
}
return pindex;
}

CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
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.
int nHeight = std::max(pindex->nHeight - nStep, 0);
if (Contains(pindex)) {
// Use O(1) CChain index if possible.
pindex = (*this)[nHeight];
} 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 {
if (pindex->nHeight > Height())
pindex = pindex->GetAncestor(Height());
while (pindex && !Contains(pindex))
pindex = pindex->pprev;
return pindex;
}
Loading

0 comments on commit bf3a5dd

Please sign in to comment.