Changeset View
Changeset View
Standalone View
Standalone View
src/test/blockcheck_tests.cpp
Show All 33 Lines | 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); | ||||
} | } | ||||
static COutPoint InsecureRandOutPoint() { | |||||
return COutPoint(TxId(InsecureRand256()), 0); | |||||
} | |||||
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; | ||||
RunCheckOnBlock(config, block, "bad-cb-missing"); | RunCheckOnBlock(config, block, "bad-cb-missing"); | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
// Coinbase only. | // Coinbase only. | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vin[0].scriptSig.resize(10); | tx.vin[0].scriptSig.resize(10); | ||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
tx.vout[0].nValue = 42 * SATOSHI; | tx.vout[0].nValue = 42 * SATOSHI; | ||||
auto coinbaseTx = CTransaction(tx); | auto coinbaseTx = CTransaction(tx); | ||||
block.vtx.resize(1); | block.vtx.resize(1); | ||||
block.vtx[0] = MakeTransactionRef(tx); | block.vtx[0] = MakeTransactionRef(tx); | ||||
RunCheckOnBlock(config, block); | RunCheckOnBlock(config, block); | ||||
// No coinbase | // No coinbase | ||||
tx.vin[0].prevout = COutPoint(InsecureRand256(), 0); | tx.vin[0].prevout = InsecureRandOutPoint(); | ||||
block.vtx[0] = MakeTransactionRef(tx); | block.vtx[0] = MakeTransactionRef(tx); | ||||
RunCheckOnBlock(config, block, "bad-cb-missing"); | RunCheckOnBlock(config, block, "bad-cb-missing"); | ||||
// Invalid coinbase | // Invalid coinbase | ||||
tx = CMutableTransaction(coinbaseTx); | tx = CMutableTransaction(coinbaseTx); | ||||
tx.vin[0].scriptSig.resize(0); | tx.vin[0].scriptSig.resize(0); | ||||
block.vtx[0] = MakeTransactionRef(tx); | block.vtx[0] = MakeTransactionRef(tx); | ||||
RunCheckOnBlock(config, block, "bad-cb-length"); | RunCheckOnBlock(config, block, "bad-cb-length"); | ||||
// Oversize block. | // Oversize block. | ||||
tx = CMutableTransaction(coinbaseTx); | tx = CMutableTransaction(coinbaseTx); | ||||
block.vtx[0] = MakeTransactionRef(tx); | block.vtx[0] = MakeTransactionRef(tx); | ||||
auto txSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | auto txSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | ||||
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 = COutPoint(InsecureRand256(), 0); | tx.vin[0].prevout = InsecureRandOutPoint(); | ||||
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); | RunCheckOnBlock(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 = COutPoint(InsecureRand256(), 0); | tx.vin[0].prevout = InsecureRandOutPoint(); | ||||
block.vtx.push_back(MakeTransactionRef(tx)); | block.vtx.push_back(MakeTransactionRef(tx)); | ||||
RunCheckOnBlock(config, block, "bad-blk-length"); | RunCheckOnBlock(config, block, "bad-blk-length"); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |