diff --git a/src/test/checkpoints_tests.cpp b/src/test/checkpoints_tests.cpp --- a/src/test/checkpoints_tests.cpp +++ b/src/test/checkpoints_tests.cpp @@ -10,12 +10,16 @@ #include "checkpoints.h" #include "chainparams.h" +#include "config.h" +#include "consensus/validation.h" +#include "miner.h" #include "test/test_bitcoin.h" #include "uint256.h" +#include "validation.h" #include -BOOST_FIXTURE_TEST_SUITE(checkpoints_tests, BasicTestingSetup) +BOOST_FIXTURE_TEST_SUITE(checkpoints_tests, TestChain100Setup) BOOST_AUTO_TEST_CASE(sanity) { const auto params = CreateChainParams(CBaseChainParams::MAIN); @@ -36,4 +40,40 @@ BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 134444 + 1, p11111)); } +BOOST_AUTO_TEST_CASE(ban_fork_prior_to_checkpoint) { + DummyConfig config; + + // Sanity check that a checkpoint exists at the genesis block + auto checkpoints = config.GetChainParams().Checkpoints().mapCheckpoints; + assert(checkpoints.find(0) != checkpoints.end()); + + // Another precomputed genesis block (height 0) should conflict with the + // regnet genesis block checkpoint, but not be accepted or stored in memory + CBlockHeader header; + CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); + stream << ParseHex( + "0100000000000000000000000000000000000000000000000000000000000000000000" + "003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adbe5" + "494dffff7f200200000001010000000100000000000000000000000000000000000000" + "00000000000000000000000000ffffffff4d04ffff001d0104455468652054696d6573" + "2030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66" + "207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01" + "000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f" + "61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + "ac00000000"); + stream >> header; + + // Header should not be accepted + CValidationState state; + CBlockHeader invalid; + const CBlockIndex *pindex = nullptr; + BOOST_CHECK( + !ProcessNewBlockHeaders(config, {header}, state, &pindex, &invalid)); + BOOST_CHECK(pindex == nullptr); + BOOST_CHECK(invalid.GetHash() == header.GetHash()); + + // Sanity check to ensure header was not saved in memory + BOOST_CHECK(mapBlockIndex.find(header.GetHash()) == mapBlockIndex.end()); +} + BOOST_AUTO_TEST_SUITE_END()