Changeset View
Changeset View
Standalone View
Standalone View
src/test/mempool_tests.cpp
Show First 20 Lines • Show All 928 Lines • ▼ Show 20 Lines | make_tx(std::vector<Amount> &&output_values, | ||||
} | } | ||||
for (size_t i = 0; i < output_values.size(); ++i) { | for (size_t i = 0; i < output_values.size(); ++i) { | ||||
tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL; | tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL; | ||||
tx.vout[i].nValue = output_values[i]; | tx.vout[i].nValue = output_values[i]; | ||||
} | } | ||||
return MakeTransactionRef(tx); | return MakeTransactionRef(tx); | ||||
} | } | ||||
#define MK_OUTPUTS(amounts...) \ | |||||
std::vector<Amount> { amounts } | |||||
#define MK_INPUTS(txs...) \ | |||||
std::vector<CTransactionRef> { txs } | |||||
#define MK_INPUT_IDX(idxes...) \ | |||||
std::vector<uint32_t> { idxes } | |||||
BOOST_AUTO_TEST_CASE(MempoolAncestryTests) { | BOOST_AUTO_TEST_CASE(MempoolAncestryTests) { | ||||
size_t ancestors, descendants; | size_t ancestors, descendants; | ||||
CTxMemPool pool; | CTxMemPool pool; | ||||
LOCK2(cs_main, pool.cs); | LOCK2(cs_main, pool.cs); | ||||
TestMemPoolEntryHelper entry; | TestMemPoolEntryHelper entry; | ||||
/* Base transaction */ | /* Base transaction */ | ||||
// | // | ||||
// [tx1] | // [tx1] | ||||
// | // | ||||
CTransactionRef tx1 = make_tx(MK_OUTPUTS(10 * COIN)); | CTransactionRef tx1 = make_tx(/* output_values */ {10 * COIN}); | ||||
pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx1)); | pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx1)); | ||||
// Ancestors / descendants should be 1 / 1 (itself / itself) | // Ancestors / descendants should be 1 / 1 (itself / itself) | ||||
pool.GetTransactionAncestry(tx1->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tx1->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, 1ULL); | BOOST_CHECK_EQUAL(ancestors, 1ULL); | ||||
BOOST_CHECK_EQUAL(descendants, 1ULL); | BOOST_CHECK_EQUAL(descendants, 1ULL); | ||||
/* Child transaction */ | /* Child transaction */ | ||||
// | // | ||||
// [tx1].0 <- [tx2] | // [tx1].0 <- [tx2] | ||||
// | // | ||||
CTransactionRef tx2 = | CTransactionRef tx2 = | ||||
make_tx(MK_OUTPUTS(495 * CENT, 5 * COIN), MK_INPUTS(tx1)); | make_tx(/* output_values */ {495 * CENT, 5 * COIN}, /* inputs */ {tx1}); | ||||
pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx2)); | pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx2)); | ||||
// Ancestors / descendants should be: | // Ancestors / descendants should be: | ||||
// transaction ancestors descendants | // transaction ancestors descendants | ||||
// ============ =========== =========== | // ============ =========== =========== | ||||
// tx1 1 (tx1) 2 (tx1,2) | // tx1 1 (tx1) 2 (tx1,2) | ||||
// tx2 2 (tx1,2) 2 (tx1,2) | // tx2 2 (tx1,2) 2 (tx1,2) | ||||
pool.GetTransactionAncestry(tx1->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tx1->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, 1ULL); | BOOST_CHECK_EQUAL(ancestors, 1ULL); | ||||
BOOST_CHECK_EQUAL(descendants, 2ULL); | BOOST_CHECK_EQUAL(descendants, 2ULL); | ||||
pool.GetTransactionAncestry(tx2->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tx2->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, 2ULL); | BOOST_CHECK_EQUAL(ancestors, 2ULL); | ||||
BOOST_CHECK_EQUAL(descendants, 2ULL); | BOOST_CHECK_EQUAL(descendants, 2ULL); | ||||
/* Grand-child 1 */ | /* Grand-child 1 */ | ||||
// | // | ||||
// [tx1].0 <- [tx2].0 <- [tx3] | // [tx1].0 <- [tx2].0 <- [tx3] | ||||
// | // | ||||
CTransactionRef tx3 = | CTransactionRef tx3 = make_tx(/* output_values */ {290 * CENT, 200 * CENT}, | ||||
make_tx(MK_OUTPUTS(290 * CENT, 200 * CENT), MK_INPUTS(tx2)); | /* inputs */ {tx2}); | ||||
pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx3)); | pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx3)); | ||||
// Ancestors / descendants should be: | // Ancestors / descendants should be: | ||||
// transaction ancestors descendants | // transaction ancestors descendants | ||||
// ============ =========== =========== | // ============ =========== =========== | ||||
// tx1 1 (tx1) 3 (tx1,2,3) | // tx1 1 (tx1) 3 (tx1,2,3) | ||||
// tx2 2 (tx1,2) 3 (tx1,2,3) | // tx2 2 (tx1,2) 3 (tx1,2,3) | ||||
// tx3 3 (tx1,2,3) 3 (tx1,2,3) | // tx3 3 (tx1,2,3) 3 (tx1,2,3) | ||||
pool.GetTransactionAncestry(tx1->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tx1->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, 1ULL); | BOOST_CHECK_EQUAL(ancestors, 1ULL); | ||||
BOOST_CHECK_EQUAL(descendants, 3ULL); | BOOST_CHECK_EQUAL(descendants, 3ULL); | ||||
pool.GetTransactionAncestry(tx2->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tx2->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, 2ULL); | BOOST_CHECK_EQUAL(ancestors, 2ULL); | ||||
BOOST_CHECK_EQUAL(descendants, 3ULL); | BOOST_CHECK_EQUAL(descendants, 3ULL); | ||||
pool.GetTransactionAncestry(tx3->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tx3->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, 3ULL); | BOOST_CHECK_EQUAL(ancestors, 3ULL); | ||||
BOOST_CHECK_EQUAL(descendants, 3ULL); | BOOST_CHECK_EQUAL(descendants, 3ULL); | ||||
/* Grand-child 2 */ | /* Grand-child 2 */ | ||||
// | // | ||||
// [tx1].0 <- [tx2].0 <- [tx3] | // [tx1].0 <- [tx2].0 <- [tx3] | ||||
// | | // | | ||||
// \---1 <- [tx4] | // \---1 <- [tx4] | ||||
// | // | ||||
CTransactionRef tx4 = make_tx(MK_OUTPUTS(290 * CENT, 250 * CENT), | CTransactionRef tx4 = make_tx(/* output_values */ {290 * CENT, 250 * CENT}, | ||||
MK_INPUTS(tx2), MK_INPUT_IDX(1)); | /* inputs */ {tx2}, /* input_indices */ {1}); | ||||
pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx4)); | pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx4)); | ||||
// Ancestors / descendants should be: | // Ancestors / descendants should be: | ||||
// transaction ancestors descendants | // transaction ancestors descendants | ||||
// ============ =========== =========== | // ============ =========== =========== | ||||
// tx1 1 (tx1) 4 (tx1,2,3,4) | // tx1 1 (tx1) 4 (tx1,2,3,4) | ||||
// tx2 2 (tx1,2) 4 (tx1,2,3,4) | // tx2 2 (tx1,2) 4 (tx1,2,3,4) | ||||
// tx3 3 (tx1,2,3) 4 (tx1,2,3,4) | // tx3 3 (tx1,2,3) 4 (tx1,2,3,4) | ||||
Show All 19 Lines | BOOST_AUTO_TEST_CASE(MempoolAncestryTests) { | ||||
// | | // | | ||||
// \---1 <- [tx4] | // \---1 <- [tx4] | ||||
// | // | ||||
CTransactionRef ty1, ty2, ty3, ty4, ty5; | CTransactionRef ty1, ty2, ty3, ty4, ty5; | ||||
CTransactionRef *ty[5] = {&ty1, &ty2, &ty3, &ty4, &ty5}; | CTransactionRef *ty[5] = {&ty1, &ty2, &ty3, &ty4, &ty5}; | ||||
Amount v = 5 * COIN; | Amount v = 5 * COIN; | ||||
for (uint64_t i = 0; i < 5; i++) { | for (uint64_t i = 0; i < 5; i++) { | ||||
CTransactionRef &tyi = *ty[i]; | CTransactionRef &tyi = *ty[i]; | ||||
tyi = make_tx(MK_OUTPUTS(v), i > 0 ? MK_INPUTS(*ty[i - 1]) | tyi = make_tx(/* output_values */ {v}, | ||||
: std::vector<CTransactionRef>()); | /* inputs */ i > 0 | ||||
? std::vector<CTransactionRef>{*ty[i - 1]} | |||||
: std::vector<CTransactionRef>{}); | |||||
v -= 50 * CENT; | v -= 50 * CENT; | ||||
pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tyi)); | pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tyi)); | ||||
pool.GetTransactionAncestry(tyi->GetId(), ancestors, descendants); | pool.GetTransactionAncestry(tyi->GetId(), ancestors, descendants); | ||||
BOOST_CHECK_EQUAL(ancestors, i + 1); | BOOST_CHECK_EQUAL(ancestors, i + 1); | ||||
BOOST_CHECK_EQUAL(descendants, i + 1); | BOOST_CHECK_EQUAL(descendants, i + 1); | ||||
} | } | ||||
CTransactionRef ty6 = make_tx(MK_OUTPUTS(5 * COIN), MK_INPUTS(tx3, ty5)); | CTransactionRef ty6 = | ||||
make_tx(/* output_values */ {5 * COIN}, /* inputs */ {tx3, ty5}); | |||||
pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(ty6)); | pool.addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(ty6)); | ||||
// Ancestors / descendants should be: | // Ancestors / descendants should be: | ||||
// transaction ancestors descendants | // transaction ancestors descendants | ||||
// ============ =================== =========== | // ============ =================== =========== | ||||
// tx1 1 (tx1) 5 (tx1,2,3,4, ty6) | // tx1 1 (tx1) 5 (tx1,2,3,4, ty6) | ||||
// tx2 2 (tx1,2) 5 (tx1,2,3,4, ty6) | // tx2 2 (tx1,2) 5 (tx1,2,3,4, ty6) | ||||
// tx3 3 (tx1,2,3) 5 (tx1,2,3,4, ty6) | // tx3 3 (tx1,2,3) 5 (tx1,2,3,4, ty6) | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |