Skip to content

Commit

Permalink
test: Verify findCommonAncestor always initializes outputs
Browse files Browse the repository at this point in the history
Also add code comment to clarify surprising code noted by practicalswift
bitcoin#18657 (comment)
  • Loading branch information
ryanofsky committed Apr 15, 2020
1 parent 4bd6bc5 commit 9986608
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ class ChainImpl : public Chain
const CBlockIndex* block1 = LookupBlockIndex(block_hash1);
const CBlockIndex* block2 = LookupBlockIndex(block_hash2);
const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
// Using & instead of && below to avoid short circuiting and leaving
// output uninitialized.
return FillBlock(ancestor, ancestor_out, lock) & FillBlock(block1, block1_out, lock) & FillBlock(block2, block2_out, lock);
}
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
Expand Down
6 changes: 6 additions & 0 deletions src/test/interfaces_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ BOOST_AUTO_TEST_CASE(findCommonAncestor)
BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight);
BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10);
BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash());

uint256 active_hash, orig_hash;
BOOST_CHECK(!chain->findCommonAncestor(active.Tip()->GetBlockHash(), {}, {}, FoundBlock().hash(active_hash), {}));
BOOST_CHECK(!chain->findCommonAncestor({}, orig_tip->GetBlockHash(), {}, {}, FoundBlock().hash(orig_hash)));
BOOST_CHECK_EQUAL(active_hash, active.Tip()->GetBlockHash());
BOOST_CHECK_EQUAL(orig_hash, orig_tip->GetBlockHash());
}

BOOST_AUTO_TEST_CASE(hasBlocks)
Expand Down

0 comments on commit 9986608

Please sign in to comment.