Changeset View
Changeset View
Standalone View
Standalone View
src/test/blockcheck_tests.cpp
| Show All 16 Lines | |||||
| static void RunCheckOnBlockImpl(const GlobalConfig &config, const CBlock &block, | static void RunCheckOnBlockImpl(const GlobalConfig &config, const CBlock &block, | ||||
| CValidationState &state, bool expected) { | CValidationState &state, bool expected) { | ||||
| block.fChecked = false; | block.fChecked = false; | ||||
| const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); | const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); | ||||
| bool fValid = CheckBlock(config, block, state, params, false, false); | bool fValid = CheckBlock(config, block, state, params, false, false); | ||||
| BOOST_CHECK_EQUAL(fValid, expected); | BOOST_CHECK_EQUAL(fValid, expected); | ||||
| BOOST_CHECK_EQUAL(fValid, state.IsValid()); | BOOST_CHECK_EQUAL(fValid, state.IsValid()); | ||||
| BOOST_CHECK_EQUAL(!fValid, state.IsNotValid()); | |||||
deadalnixUnsubmitted Not Done Inline Actions
deadalnix: likestamp | |||||
| } | } | ||||
| static void RunCheckOnBlock(const GlobalConfig &config, const CBlock &block) { | static void RunCheckOnBlock(const GlobalConfig &config, const CBlock &block) { | ||||
| CValidationState state; | CValidationState state; | ||||
| RunCheckOnBlockImpl(config, block, state, true); | RunCheckOnBlockImpl(config, block, state, true); | ||||
| } | } | ||||
| static void RunCheckOnBlock(const GlobalConfig &config, const CBlock &block, | static void RunCheckOnBlock(const GlobalConfig &config, const CBlock &block, | ||||
| const std::string &reason) { | const std::string &reason) { | ||||
| CValidationState state; | CValidationState state; | ||||
| RunCheckOnBlockImpl(config, block, state, false); | RunCheckOnBlockImpl(config, block, state, false); | ||||
| BOOST_CHECK_EQUAL(state.GetRejectCode(), REJECT_INVALID); | BOOST_CHECK_EQUAL(state.GetRejectCode(), REJECT_INVALID); | ||||
| BOOST_CHECK_EQUAL(state.GetRejectReason(), reason); | BOOST_CHECK_EQUAL(state.GetRejectReason(), reason); | ||||
| } | } | ||||
| // Excessive method tests, calling the `IsNotValid` function it's not enough | |||||
| // because the blocks can be excessive or invalid | |||||
| static void RunExessiveCheckOnBlock(const GlobalConfig &config, | |||||
| const CBlock &block) { | |||||
| CValidationState state; | |||||
| RunCheckOnBlockImpl(config, block, state, true); | |||||
| BOOST_CHECK_EQUAL(state.IsExcessive(), false); | |||||
| } | |||||
| static void RunExessiveCheckOnBlock(const GlobalConfig &config, | |||||
| const CBlock &block, | |||||
| const std::string &reason) { | |||||
| CValidationState state; | |||||
| RunCheckOnBlockImpl(config, block, state, false); | |||||
| BOOST_CHECK_EQUAL(state.GetRejectCode(), REJECT_INVALID); | |||||
deadalnixUnsubmitted Not Done Inline ActionsShould there be a different code for excessive and invalid ? deadalnix: Should there be a different code for excessive and invalid ? | |||||
| BOOST_CHECK_EQUAL(state.GetRejectReason(), reason); | |||||
| BOOST_CHECK_EQUAL(state.IsExcessive(), true); | |||||
| } | |||||
| BOOST_AUTO_TEST_CASE(blockfail) { | BOOST_AUTO_TEST_CASE(blockfail) { | ||||
| SelectParams(CBaseChainParams::MAIN); | SelectParams(CBaseChainParams::MAIN); | ||||
| // Set max blocksize to default in case other tests left it dirty | // Set max blocksize to default in case other tests left it dirty | ||||
| GlobalConfig config; | GlobalConfig config; | ||||
| config.SetMaxBlockSize(DEFAULT_MAX_BLOCK_SIZE); | config.SetMaxBlockSize(DEFAULT_MAX_BLOCK_SIZE); | ||||
| CBlock block; | CBlock block; | ||||
| Show All 34 Lines | BOOST_AUTO_TEST_CASE(blockfail) { | ||||
| auto maxTxCount = ((DEFAULT_MAX_BLOCK_SIZE - 1) / txSize) - 1; | auto maxTxCount = ((DEFAULT_MAX_BLOCK_SIZE - 1) / txSize) - 1; | ||||
| for (size_t i = 1; i < maxTxCount; i++) { | for (size_t i = 1; i < maxTxCount; i++) { | ||||
| tx.vin[0].prevout.hash = GetRandHash(); | tx.vin[0].prevout.hash = GetRandHash(); | ||||
| block.vtx.push_back(MakeTransactionRef(tx)); | block.vtx.push_back(MakeTransactionRef(tx)); | ||||
| } | } | ||||
| // Check that at this point, we still accept the block. | // Check that at this point, we still accept the block. | ||||
| RunCheckOnBlock(config, block); | RunExessiveCheckOnBlock(config, block); | ||||
| // But reject it with one more transaction as it goes over the maximum | // But reject it with one more transaction as it goes over the maximum | ||||
| // allowed block size. | // allowed block size. | ||||
| tx.vin[0].prevout.hash = GetRandHash(); | tx.vin[0].prevout.hash = GetRandHash(); | ||||
| block.vtx.push_back(MakeTransactionRef(tx)); | block.vtx.push_back(MakeTransactionRef(tx)); | ||||
| RunCheckOnBlock(config, block, "bad-blk-length"); | RunExessiveCheckOnBlock(config, block, "bad-blk-length"); | ||||
| } | } | ||||
| BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() | ||||
