Changeset View
Changeset View
Standalone View
Standalone View
src/test/miner_tests.cpp
Show All 24 Lines | |||||
#include <test/test_bitcoin.h> | #include <test/test_bitcoin.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <memory> | #include <memory> | ||||
BOOST_FIXTURE_TEST_SUITE(miner_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(miner_tests, TestingSetup) | ||||
// BOOST_CHECK_EXCEPTION predicates to check the specific validation error | |||||
class HasReason { | |||||
public: | |||||
HasReason(const std::string &reason) : m_reason(reason) {} | |||||
bool operator()(const std::runtime_error &e) const { | |||||
return std::string(e.what()).find(m_reason) != std::string::npos; | |||||
}; | |||||
private: | |||||
const std::string m_reason; | |||||
}; | |||||
static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB); | static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB); | ||||
static BlockAssembler AssemblerForTest(const Config &config, | static BlockAssembler AssemblerForTest(const Config &config, | ||||
const CTxMemPool &mempool) { | const CTxMemPool &mempool) { | ||||
BlockAssembler::Options options; | BlockAssembler::Options options; | ||||
options.nBlockMaxSize = DEFAULT_MAX_BLOCK_SIZE; | options.nBlockMaxSize = DEFAULT_MAX_BLOCK_SIZE; | ||||
options.blockMinFeeRate = blockMinFeeRate; | options.blockMinFeeRate = blockMinFeeRate; | ||||
▲ Show 20 Lines • Show All 299 Lines • ▼ Show 20 Lines | for (unsigned int i = 0; i < 1001; ++i) { | ||||
// If we don't set the # of sig ops in the CTxMemPoolEntry, template | // If we don't set the # of sig ops in the CTxMemPoolEntry, template | ||||
// creation fails. | // creation fails. | ||||
g_mempool.addUnchecked(txid, entry.Fee(LOWFEE) | g_mempool.addUnchecked(txid, entry.Fee(LOWFEE) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(spendsCoinbase) | .SpendsCoinbase(spendsCoinbase) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
tx.vin[0].prevout = COutPoint(txid, 0); | tx.vin[0].prevout = COutPoint(txid, 0); | ||||
} | } | ||||
BOOST_CHECK_THROW( | BOOST_CHECK_EXCEPTION( | ||||
AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error, HasReason("bad-blk-sigops")); | ||||
g_mempool.clear(); | g_mempool.clear(); | ||||
tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vout[0].nValue = BLOCKSUBSIDY; | tx.vout[0].nValue = BLOCKSUBSIDY; | ||||
for (unsigned int i = 0; i < 1001; ++i) { | for (unsigned int i = 0; i < 1001; ++i) { | ||||
tx.vout[0].nValue -= LOWFEE; | tx.vout[0].nValue -= LOWFEE; | ||||
const TxId txid = tx.GetId(); | const TxId txid = tx.GetId(); | ||||
// Only first tx spends coinbase. | // Only first tx spends coinbase. | ||||
Show All 37 Lines | BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pblocktemplate = | pblocktemplate = | ||||
AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey)); | AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey)); | ||||
g_mempool.clear(); | g_mempool.clear(); | ||||
// Orphan in mempool, template creation fails. | // Orphan in mempool, template creation fails. | ||||
TxId txid = tx.GetId(); | TxId txid = tx.GetId(); | ||||
g_mempool.addUnchecked(txid, entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx)); | g_mempool.addUnchecked(txid, entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx)); | ||||
BOOST_CHECK_THROW( | BOOST_CHECK_EXCEPTION( | ||||
AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error, HasReason("bad-txns-inputs-missingorspent")); | ||||
g_mempool.clear(); | g_mempool.clear(); | ||||
// Child with higher priority than parent. | // Child with higher priority than parent. | ||||
tx.vin[0].scriptSig = CScript() << OP_1; | tx.vin[0].scriptSig = CScript() << OP_1; | ||||
tx.vin[0].prevout = COutPoint(txFirst[1]->GetId(), 0); | tx.vin[0].prevout = COutPoint(txFirst[1]->GetId(), 0); | ||||
tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE; | tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE; | ||||
txid = tx.GetId(); | txid = tx.GetId(); | ||||
g_mempool.addUnchecked( | g_mempool.addUnchecked( | ||||
Show All 19 Lines | BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { | ||||
tx.vin[0].prevout = COutPoint(); | tx.vin[0].prevout = COutPoint(); | ||||
tx.vin[0].scriptSig = CScript() << OP_0 << OP_1; | tx.vin[0].scriptSig = CScript() << OP_0 << OP_1; | ||||
tx.vout[0].nValue = Amount::zero(); | tx.vout[0].nValue = Amount::zero(); | ||||
txid = tx.GetId(); | txid = tx.GetId(); | ||||
// Give it a fee so it'll get mined. | // Give it a fee so it'll get mined. | ||||
g_mempool.addUnchecked( | g_mempool.addUnchecked( | ||||
txid, | txid, | ||||
entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx)); | entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx)); | ||||
BOOST_CHECK_THROW( | // Should throw bad-tx-coinbase | ||||
BOOST_CHECK_EXCEPTION( | |||||
AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error, HasReason("bad-tx-coinbase")); | ||||
nakihito: This differs from Core's exception because of D1739. | |||||
g_mempool.clear(); | g_mempool.clear(); | ||||
// Double spend txn pair in mempool, template creation fails. | // Double spend txn pair in mempool, template creation fails. | ||||
tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vin[0].scriptSig = CScript() << OP_1; | tx.vin[0].scriptSig = CScript() << OP_1; | ||||
tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE; | tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE; | ||||
tx.vout[0].scriptPubKey = CScript() << OP_1; | tx.vout[0].scriptPubKey = CScript() << OP_1; | ||||
txid = tx.GetId(); | txid = tx.GetId(); | ||||
g_mempool.addUnchecked( | g_mempool.addUnchecked( | ||||
txid, | txid, | ||||
entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | ||||
tx.vout[0].scriptPubKey = CScript() << OP_2; | tx.vout[0].scriptPubKey = CScript() << OP_2; | ||||
txid = tx.GetId(); | txid = tx.GetId(); | ||||
g_mempool.addUnchecked( | g_mempool.addUnchecked( | ||||
txid, | txid, | ||||
entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | ||||
BOOST_CHECK_THROW( | BOOST_CHECK_EXCEPTION( | ||||
AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error, HasReason("bad-txns-inputs-missingorspent")); | ||||
g_mempool.clear(); | g_mempool.clear(); | ||||
// Subsidy changing. | // Subsidy changing. | ||||
int nHeight = chainActive.Height(); | int nHeight = chainActive.Height(); | ||||
// Create an actual 209999-long block chain (without valid blocks). | // Create an actual 209999-long block chain (without valid blocks). | ||||
while (chainActive.Tip()->nHeight < 209999) { | while (chainActive.Tip()->nHeight < 209999) { | ||||
CBlockIndex *prev = chainActive.Tip(); | CBlockIndex *prev = chainActive.Tip(); | ||||
CBlockIndex *next = new CBlockIndex(); | CBlockIndex *next = new CBlockIndex(); | ||||
Show All 35 Lines | BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { | ||||
tx.vin[0].prevout = COutPoint(txid, 0); | tx.vin[0].prevout = COutPoint(txid, 0); | ||||
tx.vin[0].scriptSig = CScript() | tx.vin[0].scriptSig = CScript() | ||||
<< std::vector<uint8_t>(script.begin(), script.end()); | << std::vector<uint8_t>(script.begin(), script.end()); | ||||
tx.vout[0].nValue -= LOWFEE; | tx.vout[0].nValue -= LOWFEE; | ||||
txid = tx.GetId(); | txid = tx.GetId(); | ||||
g_mempool.addUnchecked( | g_mempool.addUnchecked( | ||||
txid, | txid, | ||||
entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx)); | entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx)); | ||||
BOOST_CHECK_THROW( | // Should throw block-validation-failed | ||||
jasonbcoxUnsubmitted Not Done Inline ActionsFix comment jasonbcox: Fix comment | |||||
BOOST_CHECK_EXCEPTION( | |||||
AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | AssemblerForTest(config, g_mempool).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error, HasReason("blk-bad-inputs")); | ||||
nakihitoAuthorUnsubmitted Done Inline ActionsThis differs from Core's error message because of D202 nakihito: This differs from Core's error message because of D202 | |||||
g_mempool.clear(); | g_mempool.clear(); | ||||
// Delete the dummy blocks again. | // Delete the dummy blocks again. | ||||
while (chainActive.Tip()->nHeight > nHeight) { | while (chainActive.Tip()->nHeight > nHeight) { | ||||
CBlockIndex *del = chainActive.Tip(); | CBlockIndex *del = chainActive.Tip(); | ||||
chainActive.SetTip(del->pprev); | chainActive.SetTip(del->pprev); | ||||
pcoinsTip->SetBestBlock(del->pprev->GetBlockHash()); | pcoinsTip->SetBestBlock(del->pprev->GetBlockHash()); | ||||
delete del->phashBlock; | delete del->phashBlock; | ||||
▲ Show 20 Lines • Show All 261 Lines • Show Last 20 Lines |
This differs from Core's exception because of D1739.