Skip to content

Commit

Permalink
Refactor BlockMap to use an unordered_set instead of an unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRubin committed Aug 7, 2020
1 parent 82127d2 commit e3d661e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/bench/rpc_blockchain.cpp
Expand Up @@ -22,7 +22,7 @@ static void BlockToJsonVerbose(benchmark::Bench& bench)

CBlockIndex blockindex;
const uint256 blockHash = block.GetHash();
blockindex.phashBlock = &blockHash;
blockindex.m_hash_block = blockHash;
blockindex.nBits = 403014710;

bench.run([&] {
Expand Down
4 changes: 2 additions & 2 deletions src/chain.h
Expand Up @@ -138,7 +138,7 @@ class CBlockIndex
{
public:
//! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
const uint256* phashBlock{nullptr};
uint256 m_hash_block;

//! pointer to the index of the predecessor of this block
CBlockIndex* pprev{nullptr};
Expand Down Expand Up @@ -232,7 +232,7 @@ class CBlockIndex

uint256 GetBlockHash() const
{
return *phashBlock;
return m_hash_block;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/rpc/blockchain.cpp
Expand Up @@ -1347,10 +1347,10 @@ static UniValue getchaintips(const JSONRPCRequest& request)
std::set<const CBlockIndex*> setOrphans;
std::set<const CBlockIndex*> setPrevs;

for (const std::pair<const uint256, CBlockIndex*>& item : chainman.BlockIndex()) {
if (!chainman.ActiveChain().Contains(item.second)) {
setOrphans.insert(item.second);
setPrevs.insert(item.second->pprev);
for (CBlockIndex* pindex : chainman.BlockIndex()) {
if (!chainman.ActiveChain().Contains(pindex)) {
setOrphans.insert(pindex);
setPrevs.insert(pindex->pprev);
}
}

Expand All @@ -1368,7 +1368,7 @@ static UniValue getchaintips(const JSONRPCRequest& request)
for (const CBlockIndex* block : setTips) {
UniValue obj(UniValue::VOBJ);
obj.pushKV("height", block->nHeight);
obj.pushKV("hash", block->phashBlock->GetHex());
obj.pushKV("hash", block->m_hash_block.GetHex());

const int branchLen = block->nHeight - chainman.ActiveChain().FindFork(block)->nHeight;
obj.pushKV("branchlen", branchLen);
Expand Down
5 changes: 2 additions & 3 deletions src/test/miner_tests.cpp
Expand Up @@ -377,7 +377,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
while (::ChainActive().Tip()->nHeight < 209999) {
CBlockIndex* prev = ::ChainActive().Tip();
CBlockIndex* next = new CBlockIndex();
next->phashBlock = new uint256(InsecureRand256());
next->m_hash_block = uint256(InsecureRand256());
::ChainstateActive().CoinsTip().SetBestBlock(next->GetBlockHash());
next->pprev = prev;
next->nHeight = prev->nHeight + 1;
Expand All @@ -389,7 +389,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
while (::ChainActive().Tip()->nHeight < 210000) {
CBlockIndex* prev = ::ChainActive().Tip();
CBlockIndex* next = new CBlockIndex();
next->phashBlock = new uint256(InsecureRand256());
next->m_hash_block = uint256(InsecureRand256());
::ChainstateActive().CoinsTip().SetBestBlock(next->GetBlockHash());
next->pprev = prev;
next->nHeight = prev->nHeight + 1;
Expand Down Expand Up @@ -421,7 +421,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
CBlockIndex* del = ::ChainActive().Tip();
::ChainActive().SetTip(del->pprev);
::ChainstateActive().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
delete del->phashBlock;
delete del;
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/skiplist_tests.cpp
Expand Up @@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances.
vBlocksMain[i].nHeight = i;
vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : nullptr;
vBlocksMain[i].phashBlock = &vHashMain[i];
vBlocksMain[i].m_hash_block = vHashMain[i];
vBlocksMain[i].BuildSkip();
BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight);
BOOST_CHECK(vBlocksMain[i].pprev == nullptr || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
Expand All @@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
vHashSide[i] = ArithToUint256(i + 50000 + (arith_uint256(1) << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
vBlocksSide[i].nHeight = i + 50000;
vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : (vBlocksMain.data()+49999);
vBlocksSide[i].phashBlock = &vHashSide[i];
vBlocksSide[i].m_hash_block = vHashSide[i];
vBlocksSide[i].BuildSkip();
BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight);
BOOST_CHECK(vBlocksSide[i].pprev == nullptr || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
Expand Down Expand Up @@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height
vBlocksMain[i].nHeight = i;
vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : nullptr;
vBlocksMain[i].phashBlock = &vHashMain[i];
vBlocksMain[i].m_hash_block = vHashMain[i];
vBlocksMain[i].BuildSkip();
if (i < 10) {
vBlocksMain[i].nTime = i;
Expand Down

0 comments on commit e3d661e

Please sign in to comment.