Changeset View
Changeset View
Standalone View
Standalone View
src/test/checkpoints_tests.cpp
Show First 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | |||||
* \--> B \-> AB | * \--> B \-> AB | ||||
* After the node has accepted only A and AA, these rejects should occur: | * After the node has accepted only A and AA, these rejects should occur: | ||||
* * B should be rejected for forking prior to an accepted checkpoint | * * B should be rejected for forking prior to an accepted checkpoint | ||||
* * AB should be rejected for forking at an accepted checkpoint | * * AB should be rejected for forking at an accepted checkpoint | ||||
*/ | */ | ||||
BOOST_AUTO_TEST_CASE(ban_fork_prior_to_and_at_checkpoints) { | BOOST_AUTO_TEST_CASE(ban_fork_prior_to_and_at_checkpoints) { | ||||
MainnetConfigWithTestCheckpoints config; | MainnetConfigWithTestCheckpoints config; | ||||
CValidationState state; | |||||
CBlockHeader invalid; | CBlockHeader invalid; | ||||
const CBlockIndex *pindex = nullptr; | const CBlockIndex *pindex = nullptr; | ||||
// Start with mainnet genesis block | // Start with mainnet genesis block | ||||
CBlockHeader headerG = config.GetChainParams().GenesisBlock(); | CBlockHeader headerG = config.GetChainParams().GenesisBlock(); | ||||
BOOST_CHECK(headerG.GetHash() == | BOOST_CHECK(headerG.GetHash() == | ||||
uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f" | uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f" | ||||
"1b60a8ce26f")); | "1b60a8ce26f")); | ||||
BOOST_CHECK( | |||||
ProcessNewBlockHeaders(config, {headerG}, state, &pindex, &invalid)); | { | ||||
CValidationState state; | |||||
BOOST_CHECK(ProcessNewBlockHeaders(config, {headerG}, state, &pindex, | |||||
&invalid)); | |||||
pindex = nullptr; | pindex = nullptr; | ||||
} | |||||
CBlockHeader headerA, headerB, headerAA, headerAB; | CBlockHeader headerA, headerB, headerAA, headerAB; | ||||
CDataStream stream = CDataStream( | CDataStream stream = CDataStream( | ||||
ParseHex( | ParseHex( | ||||
"010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000" | "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000" | ||||
"000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e85723" | "000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e85723" | ||||
"3e0e61bc6649ffff001d01e3629901010000000100000000000000000000000000" | "3e0e61bc6649ffff001d01e3629901010000000100000000000000000000000000" | ||||
"00000000000000000000000000000000000000ffffffff0704ffff001d0104ffff" | "00000000000000000000000000000000000000ffffffff0704ffff001d0104ffff" | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | stream = CDataStream( | ||||
"00000000000000000000000000000000000000ffffffff0d52024902082f454233" | "00000000000000000000000000000000000000ffffffff0d52024902082f454233" | ||||
"322e302fffffffff0100f2052a010000002321020a56690eb0e2454c1f362d3599" | "322e302fffffffff0100f2052a010000002321020a56690eb0e2454c1f362d3599" | ||||
"89198a0b23505578be4164a65521ee7751eb1dac00000000"), | "89198a0b23505578be4164a65521ee7751eb1dac00000000"), | ||||
SER_NETWORK, PROTOCOL_VERSION); | SER_NETWORK, PROTOCOL_VERSION); | ||||
stream >> headerAB; | stream >> headerAB; | ||||
BOOST_CHECK(headerAB.hashPrevBlock == headerA.GetHash()); | BOOST_CHECK(headerAB.hashPrevBlock == headerA.GetHash()); | ||||
// Headers A and AA should be accepted | // Headers A and AA should be accepted | ||||
BOOST_CHECK( | { | ||||
ProcessNewBlockHeaders(config, {headerA}, state, &pindex, &invalid)); | CValidationState state; | ||||
BOOST_CHECK(ProcessNewBlockHeaders(config, {headerA}, state, &pindex, | |||||
&invalid)); | |||||
BOOST_CHECK(state.IsValid()); | BOOST_CHECK(state.IsValid()); | ||||
BOOST_CHECK(pindex != nullptr); | BOOST_CHECK(pindex != nullptr); | ||||
pindex = nullptr; | pindex = nullptr; | ||||
BOOST_CHECK(invalid.IsNull()); | BOOST_CHECK(invalid.IsNull()); | ||||
} | |||||
BOOST_CHECK( | { | ||||
ProcessNewBlockHeaders(config, {headerAA}, state, &pindex, &invalid)); | CValidationState state; | ||||
BOOST_CHECK(ProcessNewBlockHeaders(config, {headerAA}, state, &pindex, | |||||
&invalid)); | |||||
BOOST_CHECK(state.IsValid()); | BOOST_CHECK(state.IsValid()); | ||||
BOOST_CHECK(pindex != nullptr); | BOOST_CHECK(pindex != nullptr); | ||||
pindex = nullptr; | pindex = nullptr; | ||||
BOOST_CHECK(invalid.IsNull()); | BOOST_CHECK(invalid.IsNull()); | ||||
} | |||||
// Header B should be rejected | // Header B should be rejected | ||||
BOOST_CHECK( | { | ||||
!ProcessNewBlockHeaders(config, {headerB}, state, &pindex, &invalid)); | CValidationState state; | ||||
BOOST_CHECK(!ProcessNewBlockHeaders(config, {headerB}, state, &pindex, | |||||
&invalid)); | |||||
BOOST_CHECK(state.IsInvalid()); | BOOST_CHECK(state.IsInvalid()); | ||||
BOOST_CHECK(state.GetRejectCode() == REJECT_CHECKPOINT); | BOOST_CHECK(state.GetRejectCode() == REJECT_CHECKPOINT); | ||||
BOOST_CHECK(state.GetRejectReason() == "bad-fork-prior-to-checkpoint"); | BOOST_CHECK(state.GetRejectReason() == "bad-fork-prior-to-checkpoint"); | ||||
BOOST_CHECK(pindex == nullptr); | BOOST_CHECK(pindex == nullptr); | ||||
BOOST_CHECK(invalid.GetHash() == headerB.GetHash()); | BOOST_CHECK(invalid.GetHash() == headerB.GetHash()); | ||||
} | |||||
// Sanity check to ensure header was not saved in memory | // Sanity check to ensure header was not saved in memory | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
BOOST_CHECK(LookupBlockIndex(headerB.GetHash()) == nullptr); | BOOST_CHECK(LookupBlockIndex(headerB.GetHash()) == nullptr); | ||||
} | } | ||||
// Header AB should be rejected | // Header AB should be rejected | ||||
BOOST_CHECK( | { | ||||
!ProcessNewBlockHeaders(config, {headerAB}, state, &pindex, &invalid)); | CValidationState state; | ||||
BOOST_CHECK(!ProcessNewBlockHeaders(config, {headerAB}, state, &pindex, | |||||
&invalid)); | |||||
BOOST_CHECK(state.IsInvalid()); | BOOST_CHECK(state.IsInvalid()); | ||||
BOOST_CHECK(state.GetRejectCode() == REJECT_CHECKPOINT); | BOOST_CHECK(state.GetRejectCode() == REJECT_CHECKPOINT); | ||||
BOOST_CHECK(state.GetRejectReason() == "checkpoint mismatch"); | BOOST_CHECK(state.GetRejectReason() == "checkpoint mismatch"); | ||||
BOOST_CHECK(pindex == nullptr); | BOOST_CHECK(pindex == nullptr); | ||||
BOOST_CHECK(invalid.GetHash() == headerAB.GetHash()); | BOOST_CHECK(invalid.GetHash() == headerAB.GetHash()); | ||||
} | |||||
// Sanity check to ensure header was not saved in memory | // Sanity check to ensure header was not saved in memory | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
BOOST_CHECK(LookupBlockIndex(headerAB.GetHash()) == nullptr); | BOOST_CHECK(LookupBlockIndex(headerAB.GetHash()) == nullptr); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |