Changeset View
Changeset View
Standalone View
Standalone View
src/test/blockcheck_tests.cpp
Show All 9 Lines | |||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
BOOST_FIXTURE_TEST_SUITE(blockcheck_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(blockcheck_tests, BasicTestingSetup) | ||||
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, | ||||
bool fCheckExcessive = true) { | |||||
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, fCheckExcessive); | |||||
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()); | BOOST_CHECK_EQUAL(!fValid, state.IsNotValid()); | ||||
} | } | ||||
static void RunCheckOnBlock(const GlobalConfig &config, const CBlock &block) { | static void RunCheckOnBlock(const GlobalConfig &config, const CBlock &block) { | ||||
CValidationState state; | CValidationState state; | ||||
Show All 24 Lines | static void RunExessiveCheckOnBlock(const GlobalConfig &config, | ||||
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); | ||||
BOOST_CHECK_EQUAL(state.IsExcessive(), true); | BOOST_CHECK_EQUAL(state.IsExcessive(), true); | ||||
} | } | ||||
static void RunNonExessiveCheckOnBlock(const GlobalConfig &config, | |||||
const CBlock &block) { | |||||
CValidationState state; | |||||
RunCheckOnBlockImpl(config, block, state, true, false); | |||||
} | |||||
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 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(blockfail) { | ||||
// Check that at this point, we still accept the block. | // Check that at this point, we still accept the block. | ||||
RunExessiveCheckOnBlock(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)); | ||||
RunExessiveCheckOnBlock(config, block, "bad-blk-length"); | RunExessiveCheckOnBlock(config, block, "bad-blk-length"); | ||||
// Check everything but the block size, there should be no error | |||||
RunNonExessiveCheckOnBlock(config, block); | |||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |