Bitcoin ABC 0.31.8
P2P Digital Currency
blockindex.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2020 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <blockindex.h>
6#include <logging.h>
7#include <tinyformat.h>
8#include <util/check.h>
9
13static inline int InvertLowestOne(int n) {
14 return n & (n - 1);
15}
16
18static inline int GetSkipHeight(int height) {
19 if (height < 2) {
20 return 0;
21 }
22
23 // Determine which height to jump back to. Any number strictly lower than
24 // height is acceptable, but the following expression seems to perform well
25 // in simulations (max 110 steps to go back up to 2**18 blocks).
26 return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1
27 : InvertLowestOne(height);
28}
29
30std::string CBlockIndex::ToString() const {
31 return strprintf(
32 "CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)", pprev,
34}
35
36const CBlockIndex *CBlockIndex::GetAncestor(int height) const {
37 if (height > nHeight || height < 0) {
38 return nullptr;
39 }
40
41 const CBlockIndex *pindexWalk = this;
42 int heightWalk = nHeight;
43 while (heightWalk > height) {
44 int heightSkip = GetSkipHeight(heightWalk);
45 int heightSkipPrev = GetSkipHeight(heightWalk - 1);
46 if (pindexWalk->pskip != nullptr &&
47 (heightSkip == height ||
48 (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
49 heightSkipPrev >= height)))) {
50 // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
51 pindexWalk = pindexWalk->pskip;
52 heightWalk = heightSkip;
53 } else {
54 assert(pindexWalk->pprev);
55 pindexWalk = pindexWalk->pprev;
56 heightWalk--;
57 }
58 }
59 return pindexWalk;
60}
61
63 return const_cast<CBlockIndex *>(
64 const_cast<const CBlockIndex *>(this)->GetAncestor(height));
65}
66
68 if (pprev) {
70 }
71}
static int GetSkipHeight(int height)
Compute what height to jump back to with the CBlockIndex::pskip pointer.
Definition: blockindex.cpp:18
static int InvertLowestOne(int n)
Turn the lowest '1' bit in the binary representation of a number into a '0'.
Definition: blockindex.cpp:13
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
uint256 hashMerkleRoot
Definition: blockindex.h:75
std::string ToString() const
Definition: blockindex.cpp:30
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: blockindex.cpp:67
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:35
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:62
BlockHash GetBlockHash() const
Definition: blockindex.h:130
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
std::string ToString() const
Definition: uint256.h:80
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())