Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ BITCOIN_CORE_H = \
flat-database.h \
flatfile.h \
fs.h \
headerssync.h \
httprpc.h \
httpserver.h \
i2p.h \
Expand Down Expand Up @@ -391,6 +392,7 @@ BITCOIN_CORE_H = \
undo.h \
unordered_lru_cache.h \
util/bip32.h \
util/bitdeque.h \
util/bytevectorhash.h \
util/check.h \
util/edge.h \
Expand Down Expand Up @@ -531,6 +533,7 @@ libbitcoin_node_a_SOURCES = \
governance/vote.cpp \
governance/votedb.cpp \
gsl/assert.cpp \
headerssync.cpp \
httprpc.cpp \
httpserver.cpp \
i2p.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ BITCOIN_TESTS =\
test/coinjoin_dstxmanager_tests.cpp \
test/coinjoin_queue_tests.cpp \
test/hash_tests.cpp \
test/headers_sync_chainwork_tests.cpp \
test/httpserver_tests.cpp \
test/i2p_tests.cpp \
test/interfaces_tests.cpp \
Expand Down Expand Up @@ -283,6 +284,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/base_encode_decode.cpp \
test/fuzz/bech32.cpp \
test/fuzz/bip324.cpp \
test/fuzz/bitdeque.cpp \
test/fuzz/block.cpp \
test/fuzz/block_header.cpp \
test/fuzz/blockfilter.cpp \
Expand Down
47 changes: 24 additions & 23 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,33 @@ void CChain::SetTip(CBlockIndex& block)
}
}

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;
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
{
int step = 1;
std::vector<uint256> have;
if (index == nullptr) return have;

have.reserve(32);
while (index) {
have.emplace_back(index->GetBlockHash());
if (index->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;
int height = std::max(index->nHeight - step, 0);
// Use skiplist.
index = index->GetAncestor(height);
if (have.size() > 10) step *= 2;
}
return have;
}

return CBlockLocator(vHave);
CBlockLocator GetLocator(const CBlockIndex* index)
{
return CBlockLocator{LocatorEntries(index)};
}

CBlockLocator CChain::GetLocator() const
{
return ::GetLocator(Tip());
}

const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
Expand Down
10 changes: 8 additions & 2 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ class CChain
/** Set/initialize a chain with a given tip. */
void SetTip(CBlockIndex& block);

/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
CBlockLocator GetLocator(const CBlockIndex* pindex = nullptr) const;
/** Return a CBlockLocator that refers to the tip in of this chain. */
CBlockLocator GetLocator() const;

/** Find the last common block between this chain and a block index entry. */
const CBlockIndex* FindFork(const CBlockIndex* pindex) const;
Expand All @@ -482,4 +482,10 @@ class CChain
CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const;
};

/** Get a locator for a block index entry. */
CBlockLocator GetLocator(const CBlockIndex* index);

/** Construct a list of hash entries to put in a locator. */
std::vector<uint256> LocatorEntries(const CBlockIndex* index);

#endif // BITCOIN_CHAIN_H
1 change: 1 addition & 0 deletions src/consensus/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum class BlockValidationResult {
BLOCK_INVALID_PREV, //!< A block this one builds on is invalid
BLOCK_TIME_FUTURE, //!< block timestamp was > 2 hours in the future (or our clock is bad)
BLOCK_CHECKPOINT, //!< the block failed to meet one of our checkpoints
BLOCK_HEADER_LOW_WORK, //!< the block header may be on a too-little-work chain
BLOCK_CHAINLOCK, //!< the block conflicts with the ChainLock
};

Expand Down
Loading
Loading