What changed, and why it matters
This commit only adds new automated unit tests for the CChain class in Bitcoin Core. It does not change any production code, fix bugs, or introduce security-relevant behavior. The tests verify basic chain operations like height, tip, indexing, contains, next, and genesis on empty and two-block chains.
No security action needed. Treat as routine test-coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a BOOST_AUTO_TEST_CASE(basic_tests) block to src/test/chain_tests.cpp. It constructs an empty CChain and a two-block CChain, then asserts expected return values for CChain::Height, Tip, operator[], Contains, Next, and Genesis. No source code outside the test suite is modified. Two commented-out lines note that calling Contains/Next with nullptr causes memory access violations, but these are observations about existing behavior, not changes.
Changed components
src/test/chain_tests.cppInspect captured patch +44 / −0
diff --git a/src/test/chain_tests.cpp b/src/test/chain_tests.cpp
index a782e880..d2d68289 100644
--- a/src/test/chain_tests.cpp
+++ b/src/test/chain_tests.cpp
@@ -41,6 +41,50 @@ const CBlockIndex* NaiveLastCommonAncestor(const CBlockIndex* a, const CBlockInd
} // namespace
+BOOST_AUTO_TEST_CASE(basic_tests)
+{
+ // An empty chain
+ const CChain chain_0;
+ // A chain with 2 blocks
+ CChain chain_2;
+
+ CBlockIndex genesis;
+ genesis.nHeight = 0;
+ chain_2.SetTip(genesis);
+
+ CBlockIndex bi1;
+ bi1.pprev = &genesis;
+ bi1.nHeight = 1;
+ chain_2.SetTip(bi1);
+
+ BOOST_CHECK_EQUAL(chain_0.Height(), -1);
+ BOOST_CHECK_EQUAL(chain_2.Height(), 1);
+
+ BOOST_CHECK_EQUAL(chain_0.Tip(), nullptr);
+ BOOST_CHECK_EQUAL(chain_2.Tip(), &bi1);
+
+ // Indexer accessor: call with valid and invalid (low & high) values
+ BOOST_CHECK_EQUAL(chain_2[-1], nullptr);
+ BOOST_CHECK_EQUAL(chain_2[0], &genesis);
+ BOOST_CHECK_EQUAL(chain_2[1], &bi1);
+ BOOST_CHECK_EQUAL(chain_2[2], nullptr);
+
+ // Contains: call with contained & non-contained blocks
+ BOOST_CHECK(chain_2.Contains(&genesis));
+ BOOST_CHECK(chain_2.Contains(&bi1));
+ BOOST_CHECK(!chain_0.Contains(&genesis));
+ // BOOST_CHECK(!chain_0.Contains(nullptr)); // fail with memory access violation
+
+ // Call with non-tip & tip blocks
+ BOOST_CHECK_EQUAL(chain_2.Next(&genesis), &bi1);
+ BOOST_CHECK_EQUAL(chain_2.Next(&bi1), nullptr);
+ BOOST_CHECK_EQUAL(chain_0.Next(&genesis), nullptr);
+ // BOOST_CHECK_EQUAL(chain_0.Next(nullptr), nullptr); // fail with memory access violation
+
+ BOOST_CHECK_EQUAL(chain_2.Genesis(), &genesis);
+ BOOST_CHECK_EQUAL(chain_0.Genesis(), nullptr);
+}
+
BOOST_AUTO_TEST_CASE(chain_test)
{
FastRandomContext ctx;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.