Changeset View
Changeset View
Standalone View
Standalone View
src/test/miner_tests.cpp
Show First 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | void TestPackageSelection(Config &config, CScript scriptPubKey, | ||||
// these 3 tests assume blockprioritypercentage is 0. | // these 3 tests assume blockprioritypercentage is 0. | ||||
config.SetBlockPriorityPercentage(0); | config.SetBlockPriorityPercentage(0); | ||||
// Test that a medium fee transaction will be selected after a higher fee | // Test that a medium fee transaction will be selected after a higher fee | ||||
// rate package with a low fee rate parent. | // rate package with a low fee rate parent. | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vin[0].scriptSig = CScript() << OP_1; | tx.vin[0].scriptSig = CScript() << OP_1; | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vin[0].prevout.n = 0; | |||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
tx.vout[0].nValue = Amount(5000000000LL - 1000); | tx.vout[0].nValue = Amount(5000000000LL - 1000); | ||||
// This tx has a low fee: 1000 satoshis. | // This tx has a low fee: 1000 satoshis. | ||||
// Save this txid for later use. | // Save this txid for later use. | ||||
TxId parentTxId = tx.GetId(); | TxId parentTxId = tx.GetId(); | ||||
mempool.addUnchecked(parentTxId, | mempool.addUnchecked(parentTxId, | ||||
entry.Fee(Amount(1000)) | entry.Fee(Amount(1000)) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(true) | .SpendsCoinbase(true) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
// This tx has a medium fee: 10000 satoshis. | // This tx has a medium fee: 10000 satoshis. | ||||
tx.vin[0].prevout.hash = txFirst[1]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[1]->GetId(), 0); | ||||
tx.vout[0].nValue = Amount(5000000000LL - 10000); | tx.vout[0].nValue = Amount(5000000000LL - 10000); | ||||
TxId mediumFeeTxId = tx.GetId(); | TxId mediumFeeTxId = tx.GetId(); | ||||
mempool.addUnchecked(mediumFeeTxId, | mempool.addUnchecked(mediumFeeTxId, | ||||
entry.Fee(Amount(10000)) | entry.Fee(Amount(10000)) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(true) | .SpendsCoinbase(true) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
// This tx has a high fee, but depends on the first transaction. | // This tx has a high fee, but depends on the first transaction. | ||||
tx.vin[0].prevout.hash = parentTxId; | tx.vin[0].prevout = COutPoint(parentTxId, 0); | ||||
// 50k satoshi fee. | // 50k satoshi fee. | ||||
tx.vout[0].nValue = Amount(5000000000LL - 1000 - 50000); | tx.vout[0].nValue = Amount(5000000000LL - 1000 - 50000); | ||||
TxId highFeeTxId = tx.GetId(); | TxId highFeeTxId = tx.GetId(); | ||||
mempool.addUnchecked(highFeeTxId, | mempool.addUnchecked(highFeeTxId, | ||||
entry.Fee(Amount(50000)) | entry.Fee(Amount(50000)) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(false) | .SpendsCoinbase(false) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
std::unique_ptr<CBlockTemplate> pblocktemplate = | std::unique_ptr<CBlockTemplate> pblocktemplate = | ||||
BlockAssembler(config).CreateNewBlock(scriptPubKey); | BlockAssembler(config).CreateNewBlock(scriptPubKey); | ||||
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetId() == parentTxId); | BOOST_CHECK(pblocktemplate->block.vtx[1]->GetId() == parentTxId); | ||||
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetId() == highFeeTxId); | BOOST_CHECK(pblocktemplate->block.vtx[2]->GetId() == highFeeTxId); | ||||
BOOST_CHECK(pblocktemplate->block.vtx[3]->GetId() == mediumFeeTxId); | BOOST_CHECK(pblocktemplate->block.vtx[3]->GetId() == mediumFeeTxId); | ||||
// Test that a package below the block min tx fee doesn't get included | // Test that a package below the block min tx fee doesn't get included | ||||
tx.vin[0].prevout.hash = highFeeTxId; | tx.vin[0].prevout = COutPoint(highFeeTxId, 0); | ||||
// 0 fee. | // 0 fee. | ||||
tx.vout[0].nValue = Amount(5000000000LL - 1000 - 50000); | tx.vout[0].nValue = Amount(5000000000LL - 1000 - 50000); | ||||
TxId freeTxId = tx.GetId(); | TxId freeTxId = tx.GetId(); | ||||
mempool.addUnchecked(freeTxId, entry.Fee(Amount(0)).FromTx(tx)); | mempool.addUnchecked(freeTxId, entry.Fee(Amount(0)).FromTx(tx)); | ||||
size_t freeTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | size_t freeTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | ||||
// Calculate a fee on child transaction that will put the package just | // Calculate a fee on child transaction that will put the package just | ||||
// below the block min tx fee (assuming 1 child tx of the same size). | // below the block min tx fee (assuming 1 child tx of the same size). | ||||
Amount feeToUse = blockMinFeeRate.GetFee(2 * freeTxSize) - Amount(1); | Amount feeToUse = blockMinFeeRate.GetFee(2 * freeTxSize) - Amount(1); | ||||
tx.vin[0].prevout.hash = freeTxId; | tx.vin[0].prevout = COutPoint(freeTxId, 0); | ||||
tx.vout[0].nValue = Amount(5000000000LL - 1000 - 50000) - feeToUse; | tx.vout[0].nValue = Amount(5000000000LL - 1000 - 50000) - feeToUse; | ||||
TxId lowFeeTxId = tx.GetId(); | TxId lowFeeTxId = tx.GetId(); | ||||
mempool.addUnchecked(lowFeeTxId, entry.Fee(feeToUse).FromTx(tx)); | mempool.addUnchecked(lowFeeTxId, entry.Fee(feeToUse).FromTx(tx)); | ||||
pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | ||||
// Verify that the free tx and the low fee tx didn't get selected. | // Verify that the free tx and the low fee tx didn't get selected. | ||||
for (const auto &txn : pblocktemplate->block.vtx) { | for (const auto &txn : pblocktemplate->block.vtx) { | ||||
BOOST_CHECK(txn->GetId() != freeTxId); | BOOST_CHECK(txn->GetId() != freeTxId); | ||||
BOOST_CHECK(txn->GetId() != lowFeeTxId); | BOOST_CHECK(txn->GetId() != lowFeeTxId); | ||||
Show All 10 Lines | mempool.addUnchecked(lowFeeTxId, | ||||
entry.Fee(feeToUse + Amount(2)).FromTx(tx)); | entry.Fee(feeToUse + Amount(2)).FromTx(tx)); | ||||
pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | ||||
BOOST_CHECK(pblocktemplate->block.vtx[4]->GetId() == freeTxId); | BOOST_CHECK(pblocktemplate->block.vtx[4]->GetId() == freeTxId); | ||||
BOOST_CHECK(pblocktemplate->block.vtx[5]->GetId() == lowFeeTxId); | BOOST_CHECK(pblocktemplate->block.vtx[5]->GetId() == lowFeeTxId); | ||||
// Test that transaction selection properly updates ancestor fee | // Test that transaction selection properly updates ancestor fee | ||||
// calculations as ancestor transactions get included in a block. Add a | // calculations as ancestor transactions get included in a block. Add a | ||||
// 0-fee transaction that has 2 outputs. | // 0-fee transaction that has 2 outputs. | ||||
tx.vin[0].prevout.hash = txFirst[2]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[2]->GetId(), 0); | ||||
tx.vout.resize(2); | tx.vout.resize(2); | ||||
tx.vout[0].nValue = Amount(5000000000LL - 100000000); | tx.vout[0].nValue = Amount(5000000000LL - 100000000); | ||||
// 1BCC output. | // 1BCC output. | ||||
tx.vout[1].nValue = Amount(100000000); | tx.vout[1].nValue = Amount(100000000); | ||||
TxId freeTxId2 = tx.GetId(); | TxId freeTxId2 = tx.GetId(); | ||||
mempool.addUnchecked(freeTxId2, | mempool.addUnchecked(freeTxId2, | ||||
entry.Fee(Amount(0)).SpendsCoinbase(true).FromTx(tx)); | entry.Fee(Amount(0)).SpendsCoinbase(true).FromTx(tx)); | ||||
// This tx can't be mined by itself. | // This tx can't be mined by itself. | ||||
tx.vin[0].prevout.hash = freeTxId2; | tx.vin[0].prevout = COutPoint(freeTxId2, 0); | ||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
feeToUse = blockMinFeeRate.GetFee(freeTxSize); | feeToUse = blockMinFeeRate.GetFee(freeTxSize); | ||||
tx.vout[0].nValue = Amount(5000000000LL) - Amount(100000000) - feeToUse; | tx.vout[0].nValue = Amount(5000000000LL) - Amount(100000000) - feeToUse; | ||||
TxId lowFeeTxId2 = tx.GetId(); | TxId lowFeeTxId2 = tx.GetId(); | ||||
mempool.addUnchecked(lowFeeTxId2, | mempool.addUnchecked(lowFeeTxId2, | ||||
entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx)); | entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx)); | ||||
pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | ||||
// Verify that this tx isn't selected. | // Verify that this tx isn't selected. | ||||
for (const auto &txn : pblocktemplate->block.vtx) { | for (const auto &txn : pblocktemplate->block.vtx) { | ||||
BOOST_CHECK(txn->GetId() != freeTxId2); | BOOST_CHECK(txn->GetId() != freeTxId2); | ||||
BOOST_CHECK(txn->GetId() != lowFeeTxId2); | BOOST_CHECK(txn->GetId() != lowFeeTxId2); | ||||
} | } | ||||
// This tx will be mineable, and should cause lowFeeTxId2 to be selected | // This tx will be mineable, and should cause lowFeeTxId2 to be selected as | ||||
// as well. | // well. | ||||
tx.vin[0].prevout.n = 1; | tx.vin[0].prevout = COutPoint(freeTxId2, 1); | ||||
// 10k satoshi fee. | // 10k satoshi fee. | ||||
tx.vout[0].nValue = Amount(100000000 - 10000); | tx.vout[0].nValue = Amount(100000000 - 10000); | ||||
mempool.addUnchecked(tx.GetId(), entry.Fee(Amount(10000)).FromTx(tx)); | mempool.addUnchecked(tx.GetId(), entry.Fee(Amount(10000)).FromTx(tx)); | ||||
pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | pblocktemplate = BlockAssembler(config).CreateNewBlock(scriptPubKey); | ||||
BOOST_CHECK(pblocktemplate->block.vtx[8]->GetId() == lowFeeTxId2); | BOOST_CHECK(pblocktemplate->block.vtx[8]->GetId() == lowFeeTxId2); | ||||
} | } | ||||
void TestCoinbaseMessageEB(uint64_t eb, std::string cbmsg) { | void TestCoinbaseMessageEB(uint64_t eb, std::string cbmsg) { | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { | ||||
const Amount HIGHFEE = COIN; | const Amount HIGHFEE = COIN; | ||||
const Amount HIGHERFEE = 4 * COIN; | const Amount HIGHERFEE = 4 * COIN; | ||||
// block sigops > limit: 1000 CHECKMULTISIG + 1 | // block sigops > limit: 1000 CHECKMULTISIG + 1 | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
// NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG | // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG | ||||
tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP | tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP | ||||
<< OP_CHECKMULTISIG << OP_1; | << OP_CHECKMULTISIG << OP_1; | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vin[0].prevout.n = 0; | |||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
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; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
// Only first tx spends coinbase. | // Only first tx spends coinbase. | ||||
bool spendsCoinbase = (i == 0) ? true : false; | bool spendsCoinbase = (i == 0) ? true : false; | ||||
// 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. | ||||
mempool.addUnchecked(hash, | mempool.addUnchecked(hash, | ||||
entry.Fee(LOWFEE) | entry.Fee(LOWFEE) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(spendsCoinbase) | .SpendsCoinbase(spendsCoinbase) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
tx.vin[0].prevout.hash = hash; | tx.vin[0].prevout = COutPoint(hash, 0); | ||||
} | } | ||||
BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), | BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error); | ||||
mempool.clear(); | mempool.clear(); | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | 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; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
// Only first tx spends coinbase. | // Only first tx spends coinbase. | ||||
bool spendsCoinbase = (i == 0) ? true : false; | bool spendsCoinbase = (i == 0) ? true : false; | ||||
// If we do set the # of sig ops in the CTxMemPoolEntry, template | // If we do set the # of sig ops in the CTxMemPoolEntry, template | ||||
// creation passes. | // creation passes. | ||||
mempool.addUnchecked(hash, | mempool.addUnchecked(hash, | ||||
entry.Fee(LOWFEE) | entry.Fee(LOWFEE) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(spendsCoinbase) | .SpendsCoinbase(spendsCoinbase) | ||||
.SigOpsCost(80) | .SigOpsCost(80) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
tx.vin[0].prevout.hash = hash; | tx.vin[0].prevout = COutPoint(hash, 0); | ||||
} | } | ||||
BOOST_CHECK(pblocktemplate = | BOOST_CHECK(pblocktemplate = | ||||
BlockAssembler(config).CreateNewBlock(scriptPubKey)); | BlockAssembler(config).CreateNewBlock(scriptPubKey)); | ||||
mempool.clear(); | mempool.clear(); | ||||
// block size > limit | // block size > limit | ||||
tx.vin[0].scriptSig = CScript(); | tx.vin[0].scriptSig = CScript(); | ||||
// 18 * (520char + DROP) + OP_1 = 9433 bytes | // 18 * (520char + DROP) + OP_1 = 9433 bytes | ||||
std::vector<uint8_t> vchData(520); | std::vector<uint8_t> vchData(520); | ||||
for (unsigned int i = 0; i < 18; ++i) { | for (unsigned int i = 0; i < 18; ++i) { | ||||
tx.vin[0].scriptSig << vchData << OP_DROP; | tx.vin[0].scriptSig << vchData << OP_DROP; | ||||
} | } | ||||
tx.vin[0].scriptSig << OP_1; | tx.vin[0].scriptSig << OP_1; | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | 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 < 128; ++i) { | for (unsigned int i = 0; i < 128; ++i) { | ||||
tx.vout[0].nValue -= LOWFEE; | tx.vout[0].nValue -= LOWFEE; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
// Only first tx spends coinbase. | // Only first tx spends coinbase. | ||||
bool spendsCoinbase = (i == 0) ? true : false; | bool spendsCoinbase = (i == 0) ? true : false; | ||||
mempool.addUnchecked(hash, | mempool.addUnchecked(hash, | ||||
entry.Fee(LOWFEE) | entry.Fee(LOWFEE) | ||||
.Time(GetTime()) | .Time(GetTime()) | ||||
.SpendsCoinbase(spendsCoinbase) | .SpendsCoinbase(spendsCoinbase) | ||||
.FromTx(tx)); | .FromTx(tx)); | ||||
tx.vin[0].prevout.hash = hash; | tx.vin[0].prevout = COutPoint(hash, 0); | ||||
} | } | ||||
BOOST_CHECK(pblocktemplate = | BOOST_CHECK(pblocktemplate = | ||||
BlockAssembler(config).CreateNewBlock(scriptPubKey)); | BlockAssembler(config).CreateNewBlock(scriptPubKey)); | ||||
mempool.clear(); | mempool.clear(); | ||||
// Orphan in mempool, template creation fails. | // Orphan in mempool, template creation fails. | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked(hash, entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx)); | mempool.addUnchecked(hash, entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx)); | ||||
BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), | BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error); | ||||
mempool.clear(); | 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.hash = txFirst[1]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[1]->GetId(), 0); | ||||
tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE; | tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked( | mempool.addUnchecked( | ||||
hash, | hash, | ||||
entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | ||||
tx.vin[0].prevout.hash = hash; | tx.vin[0].prevout = COutPoint(hash, 0); | ||||
tx.vin.resize(2); | tx.vin.resize(2); | ||||
tx.vin[1].scriptSig = CScript() << OP_1; | tx.vin[1].scriptSig = CScript() << OP_1; | ||||
tx.vin[1].prevout.hash = txFirst[0]->GetId(); | tx.vin[1].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vin[1].prevout.n = 0; | |||||
// First txn output + fresh coinbase - new txn fee. | // First txn output + fresh coinbase - new txn fee. | ||||
tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; | tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked( | mempool.addUnchecked( | ||||
hash, | hash, | ||||
entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | ||||
BOOST_CHECK(pblocktemplate = | BOOST_CHECK(pblocktemplate = | ||||
BlockAssembler(config).CreateNewBlock(scriptPubKey)); | BlockAssembler(config).CreateNewBlock(scriptPubKey)); | ||||
Show All 19 Lines | for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { | ||||
// Trick the MedianTimePast. | // Trick the MedianTimePast. | ||||
times[i] = chainActive.Tip() | times[i] = chainActive.Tip() | ||||
->GetAncestor(chainActive.Tip()->nHeight - i) | ->GetAncestor(chainActive.Tip()->nHeight - i) | ||||
->nTime; | ->nTime; | ||||
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime = | chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime = | ||||
P2SH_ACTIVATION_TIME; | P2SH_ACTIVATION_TIME; | ||||
} | } | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vin[0].prevout.n = 0; | |||||
tx.vin[0].scriptSig = CScript() << OP_1; | tx.vin[0].scriptSig = CScript() << OP_1; | ||||
tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE; | tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE; | ||||
script = CScript() << OP_0; | script = CScript() << OP_0; | ||||
tx.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(script)); | tx.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(script)); | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked( | mempool.addUnchecked( | ||||
hash, | hash, | ||||
entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx)); | ||||
tx.vin[0].prevout.hash = hash; | tx.vin[0].prevout = COutPoint(hash, 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; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked( | mempool.addUnchecked( | ||||
hash, | hash, | ||||
entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx)); | entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx)); | ||||
BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), | BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), | ||||
std::runtime_error); | std::runtime_error); | ||||
mempool.clear(); | mempool.clear(); | ||||
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { | for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { | ||||
// Restore the MedianTimePast. | // Restore the MedianTimePast. | ||||
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime = | chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime = | ||||
times[i]; | times[i]; | ||||
} | } | ||||
// Double spend txn pair in mempool, template creation fails. | // Double spend txn pair in mempool, template creation fails. | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | 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; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked( | mempool.addUnchecked( | ||||
hash, | hash, | ||||
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; | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { | ||||
// height map | // height map | ||||
std::vector<int> prevheights; | std::vector<int> prevheights; | ||||
// Relative height locked. | // Relative height locked. | ||||
tx.nVersion = 2; | tx.nVersion = 2; | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
prevheights.resize(1); | prevheights.resize(1); | ||||
// Only 1 transaction. | // Only 1 transaction. | ||||
tx.vin[0].prevout.hash = txFirst[0]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0); | ||||
tx.vin[0].prevout.n = 0; | |||||
tx.vin[0].scriptSig = CScript() << OP_1; | tx.vin[0].scriptSig = CScript() << OP_1; | ||||
// txFirst[0] is the 2nd block | // txFirst[0] is the 2nd block | ||||
tx.vin[0].nSequence = chainActive.Tip()->nHeight + 1; | tx.vin[0].nSequence = chainActive.Tip()->nHeight + 1; | ||||
prevheights[0] = baseheight + 1; | prevheights[0] = baseheight + 1; | ||||
tx.vout.resize(1); | tx.vout.resize(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; | ||||
tx.nLockTime = 0; | tx.nLockTime = 0; | ||||
Show All 13 Lines | BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { | ||||
// Sequence locks fail. | // Sequence locks fail. | ||||
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); | BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); | ||||
// Sequence locks pass on 2nd block. | // Sequence locks pass on 2nd block. | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
SequenceLocks(CTransaction(tx), flags, &prevheights, | SequenceLocks(CTransaction(tx), flags, &prevheights, | ||||
CreateBlockIndex(chainActive.Tip()->nHeight + 2))); | CreateBlockIndex(chainActive.Tip()->nHeight + 2))); | ||||
// Relative time locked. | // Relative time locked. | ||||
tx.vin[0].prevout.hash = txFirst[1]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[1]->GetId(), 0); | ||||
// txFirst[1] is the 3rd block. | // txFirst[1] is the 3rd block. | ||||
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | | tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | | ||||
(((chainActive.Tip()->GetMedianTimePast() + 1 - | (((chainActive.Tip()->GetMedianTimePast() + 1 - | ||||
chainActive[1]->GetMedianTimePast()) >> | chainActive[1]->GetMedianTimePast()) >> | ||||
CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) + | CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) + | ||||
1); | 1); | ||||
prevheights[0] = baseheight + 2; | prevheights[0] = baseheight + 2; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
Show All 21 Lines | BOOST_CHECK( | ||||
CreateBlockIndex(chainActive.Tip()->nHeight + 1))); | CreateBlockIndex(chainActive.Tip()->nHeight + 1))); | ||||
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { | for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { | ||||
// Undo tricked MTP. | // Undo tricked MTP. | ||||
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime -= | chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime -= | ||||
512; | 512; | ||||
} | } | ||||
// Absolute height locked. | // Absolute height locked. | ||||
tx.vin[0].prevout.hash = txFirst[2]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[2]->GetId(), 0); | ||||
tx.vin[0].nSequence = CTxIn::SEQUENCE_FINAL - 1; | tx.vin[0].nSequence = CTxIn::SEQUENCE_FINAL - 1; | ||||
prevheights[0] = baseheight + 3; | prevheights[0] = baseheight + 3; | ||||
tx.nLockTime = chainActive.Tip()->nHeight + 1; | tx.nLockTime = chainActive.Tip()->nHeight + 1; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked(hash, entry.Time(GetTime()).FromTx(tx)); | mempool.addUnchecked(hash, entry.Time(GetTime()).FromTx(tx)); | ||||
{ | { | ||||
// Locktime fails. | // Locktime fails. | ||||
Show All 12 Lines | BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); | ||||
GlobalConfig config; | GlobalConfig config; | ||||
CValidationState state; | CValidationState state; | ||||
BOOST_CHECK(ContextualCheckTransaction( | BOOST_CHECK(ContextualCheckTransaction( | ||||
config, CTransaction(tx), state, chainActive.Tip()->nHeight + 2, | config, CTransaction(tx), state, chainActive.Tip()->nHeight + 2, | ||||
chainActive.Tip()->GetMedianTimePast())); | chainActive.Tip()->GetMedianTimePast())); | ||||
} | } | ||||
// Absolute time locked. | // Absolute time locked. | ||||
tx.vin[0].prevout.hash = txFirst[3]->GetId(); | tx.vin[0].prevout = COutPoint(txFirst[3]->GetId(), 0); | ||||
tx.nLockTime = chainActive.Tip()->GetMedianTimePast(); | tx.nLockTime = chainActive.Tip()->GetMedianTimePast(); | ||||
prevheights.resize(1); | prevheights.resize(1); | ||||
prevheights[0] = baseheight + 4; | prevheights[0] = baseheight + 4; | ||||
hash = tx.GetId(); | hash = tx.GetId(); | ||||
mempool.addUnchecked(hash, entry.Time(GetTime()).FromTx(tx)); | mempool.addUnchecked(hash, entry.Time(GetTime()).FromTx(tx)); | ||||
{ | { | ||||
// Locktime fails. | // Locktime fails. | ||||
Show All 12 Lines | BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); | ||||
GlobalConfig config; | GlobalConfig config; | ||||
CValidationState state; | CValidationState state; | ||||
BOOST_CHECK(ContextualCheckTransaction( | BOOST_CHECK(ContextualCheckTransaction( | ||||
config, CTransaction(tx), state, chainActive.Tip()->nHeight + 1, | config, CTransaction(tx), state, chainActive.Tip()->nHeight + 1, | ||||
chainActive.Tip()->GetMedianTimePast() + 1)); | chainActive.Tip()->GetMedianTimePast() + 1)); | ||||
} | } | ||||
// mempool-dependent transactions (not added) | // mempool-dependent transactions (not added) | ||||
tx.vin[0].prevout.hash = hash; | tx.vin[0].prevout = COutPoint(hash, 0); | ||||
prevheights[0] = chainActive.Tip()->nHeight + 1; | prevheights[0] = chainActive.Tip()->nHeight + 1; | ||||
tx.nLockTime = 0; | tx.nLockTime = 0; | ||||
tx.vin[0].nSequence = 0; | tx.vin[0].nSequence = 0; | ||||
{ | { | ||||
// Locktime passes. | // Locktime passes. | ||||
GlobalConfig config; | GlobalConfig config; | ||||
CValidationState state; | CValidationState state; | ||||
▲ Show 20 Lines • Show All 127 Lines • Show Last 20 Lines |