aboutsummaryrefslogtreecommitdiff
path: root/src/node
diff options
context:
space:
mode:
authorCarl Dong <contact@carldong.me>2022-01-19 13:55:40 -0500
committerCarl Dong <contact@carldong.me>2022-02-22 11:56:49 -0500
commit6c23c415613d8b847e6f6a2f872be893da9f4384 (patch)
tree70dcee382f2f102213e80b0cee4569061bb77fad /src/node
parentc05cf7aa1e1c15089753897a10c14762027d4b99 (diff)
downloadbitcoin-6c23c415613d8b847e6f6a2f872be893da9f4384.tar.xz
refactor: Rewrite AddToBlockIndex with try_emplace
Diffstat (limited to 'src/node')
-rw-r--r--src/node/blockstorage.cpp15
1 files changed, 5 insertions, 10 deletions
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index 476f0d8148..7392830261 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -50,22 +50,17 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block)
{
AssertLockHeld(cs_main);
- // Check for duplicate
- uint256 hash = block.GetHash();
- BlockMap::iterator it = m_block_index.find(hash);
- if (it != m_block_index.end()) {
- return &it->second;
+ auto [mi, inserted] = m_block_index.try_emplace(block.GetHash(), block);
+ if (!inserted) {
+ return &mi->second;
}
+ CBlockIndex* pindexNew = &(*mi).second;
- // Construct new block index object
- CBlockIndex new_index{block};
// We assign the sequence id to blocks only when the full data is available,
// to avoid miners withholding blocks but broadcasting headers, to get a
// competitive advantage.
- new_index.nSequenceId = 0;
- BlockMap::iterator mi = m_block_index.insert(std::make_pair(hash, std::move(new_index))).first;
+ pindexNew->nSequenceId = 0;
- CBlockIndex* pindexNew = &(*mi).second;
pindexNew->phashBlock = &((*mi).first);
BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock);
if (miPrev != m_block_index.end()) {