Skip to content

Commit

Permalink
Merge branch 'mempool/rm_pre_tachon_in_txmempool' into 'master'
Browse files Browse the repository at this point in the history
Remove pre-tachyon logic and code from txmempool and related tests

See merge request bitcoin-cash-node/bitcoin-cash-node!1184
  • Loading branch information
ftrader committed May 20, 2021
2 parents f494201 + 431374d commit 55ac736
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 616 deletions.
10 changes: 0 additions & 10 deletions src/bench/chained_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ static void benchATMP(const Config& config,
benchmark::State& state,
const std::vector<CTransactionRef> chainedTxs)
{
const bool tachyonBefore = g_mempool.tachyonLatched.exchange(true); // test with tachyon latched (faster)

const Amount absurdFee(Amount::zero());

gArgs.ForceSetArg("-limitdescendantcount", std::to_string(chainedTxs.size()));
Expand All @@ -158,8 +156,6 @@ static void benchATMP(const Config& config,
}
g_mempool.clear();
}

g_mempool.tachyonLatched = tachyonBefore; // restore state
}


Expand All @@ -174,7 +170,6 @@ static void benchReorg(const Config& config,
size_t chainSizePerBlock,
bool includeMempoolTxRemoval)
{
const bool tachyonBefore = g_mempool.tachyonLatched.exchange(true); // test with tachyon latched (faster)
auto utxos = createUTXOs(config, reorgDepth);
std::vector<std::vector<CTransactionRef>> chains;
for (const auto &utxo : utxos) {
Expand Down Expand Up @@ -259,8 +254,6 @@ static void benchReorg(const Config& config,
assert(::ChainActive().Tip() == mostWorkTip);
assert(g_mempool.size() == 0);
}

g_mempool.tachyonLatched = tachyonBefore; // restore state
}

static void benchGenerateNewBlock(const Config& config,
Expand All @@ -271,7 +264,6 @@ static void benchGenerateNewBlock(const Config& config,
entry.nFee = 1337 * SATOSHI;

CTxMemPool mempool;
const bool tachyonBefore = g_mempool.tachyonLatched.exchange(true); // test with tachyon latched (faster)

// Fill mempool
size_t txCount = 0;
Expand All @@ -296,7 +288,6 @@ static void benchGenerateNewBlock(const Config& config,
assert(blocktemplate->block.vtx.size() == txCount + 1);
}

g_mempool.tachyonLatched = tachyonBefore; // restore state
}

static void benchEviction(const Config&,
Expand All @@ -311,7 +302,6 @@ static void benchEviction(const Config&,
for (uint64_t i = 0; i < state.m_num_iters * state.m_num_evals + 1; ++i) {
pools.emplace_back();
CTxMemPool &pool = pools.back();
pool.tachyonLatched = true; // test with tachyon latched (faster)
TestMemPoolEntryHelper entry;
// Fill mempool
size_t txCount = 0;
Expand Down
5 changes: 1 addition & 4 deletions src/bench/removeforblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ static void benchRemoveForBlock(const Config& config, benchmark::State& state,
assert(nTx > 0 && blockMB > 0);

// save initial state
const bool origTachyonLatched = g_mempool.tachyonLatched.exchange(true);
const auto origBlockMinTxFee = gArgs.GetArg("-blockmintxfee", "");
gArgs.ForceSetArg("-blockmintxfee", "0");

// undo above on scope end
const Defer d([&origTachyonLatched, &origBlockMinTxFee] {
g_mempool.tachyonLatched = origTachyonLatched;
const Defer d([&origBlockMinTxFee] {
gArgs.ForceSetArg("-blockmintxfee", origBlockMinTxFee);
g_mempool.clear();
});
Expand Down Expand Up @@ -145,7 +143,6 @@ static void benchRemoveForBlock(const Config& config, benchmark::State& state,
const auto &index = g_mempool.mapTx.get<entry_id>(); // iterate by entry id
pools.emplace_back();
auto &pool = pools.back();
pool.tachyonLatched = g_mempool.tachyonLatched.load();
for (auto it = index.begin(); it != index.end(); ++it) {
LOCK(pool.cs);
pool.addUnchecked(*it);
Expand Down
5 changes: 1 addition & 4 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,7 @@ static UniValue getmempoolancestors(const Config &config,
}

CTxMemPool::setEntries setAncestors;
uint64_t noLimit = std::numeric_limits<uint64_t>::max();
std::string dummy;
g_mempool.CalculateMemPoolAncestors(*it, setAncestors, noLimit, noLimit,
noLimit, noLimit, dummy, false);
g_mempool.CalculateMemPoolAncestors(*it, setAncestors, false);

if (!fVerbose) {
UniValue::Array ret;
Expand Down
150 changes: 44 additions & 106 deletions src/test/mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,111 +20,6 @@

BOOST_FIXTURE_TEST_SUITE(mempool_tests, TestingSetup)


// this test should be removed after tachyon is checkpointed
BOOST_AUTO_TEST_CASE(TestPreAndPostTachyonAccounting) {
for (const bool tachyon : {false, true}) {
CTxMemPool testPool;
testPool.tachyonLatched = tachyon; // test pre-tachyon versus post-tachyon behavior
LOCK2(cs_main, testPool.cs);
TestMemPoolEntryHelper entry;
CMutableTransaction parentOfAll;

// Vector to track unspent outputs that are used to construct txs
std::vector<CTxIn> outpoints;
const size_t maxOutputs = 3;

// Construct a parent for the rest of the chain
parentOfAll.vin.resize(1);
parentOfAll.vin[0].scriptSig = CScript();
// Give us a couple outpoints so we can spend them
for (size_t i = 0; i < maxOutputs; i++) {
parentOfAll.vout.emplace_back(10 * SATOSHI, CScript() << OP_TRUE);
}
TxId parentOfAllId = parentOfAll.GetId();
testPool.addUnchecked(entry.SigOpCount(0).FromTx(parentOfAll));

// Add some outpoints to the tracking vector
for (size_t i = 0; i < maxOutputs; i++) {
outpoints.emplace_back(COutPoint(parentOfAllId, i));
}

Amount totalFee = Amount::zero();
size_t totalSize = CTransaction(parentOfAll).GetTotalSize();
size_t totalVirtualSize = totalSize;
int64_t totalSigOpCount = 0;

// Generate 100 transactions
for (size_t totalTransactions = 0; totalTransactions < 100;
totalTransactions++) {
CMutableTransaction tx;

// Consume random inputs, but make sure we don't consume more than
// available
for (size_t input = std::min(InsecureRandRange(maxOutputs) + 1,
uint64_t(outpoints.size()));
input > 0; input--) {
std::swap(outpoints[InsecureRandRange(outpoints.size())],
outpoints.back());
tx.vin.emplace_back(outpoints.back());
outpoints.pop_back();
}

// Produce random number of outputs
for (size_t output = InsecureRandRange(maxOutputs) + 1; output > 0;
output--) {
tx.vout.emplace_back(10 * SATOSHI, CScript() << OP_TRUE);
}

TxId curId = tx.GetId();

// Record the outputs
for (size_t output = tx.vout.size(); output > 0; output--) {
outpoints.emplace_back(COutPoint(curId, output));
}

const Amount randFee = int64_t(InsecureRandRange(300)) * SATOSHI;
const int randSigOpCount = InsecureRandRange(5);

testPool.addUnchecked(
entry.Fee(randFee).SigOpCount(randSigOpCount).FromTx(tx));

// Calculate overall values
totalFee += randFee;
const auto txSize = CTransaction(tx).GetTotalSize();
totalSize += txSize;
const auto txVSize = GetVirtualTransactionSize(CTransaction(tx).GetTotalSize(), randSigOpCount);
totalVirtualSize += txVSize;
totalSigOpCount += randSigOpCount;
const CTxMemPoolEntry &parentEntry = *testPool.mapTx.find(parentOfAllId);

uint64_t totalVirtualSize_strict =
GetVirtualTransactionSize(totalSize, totalSigOpCount);

if (!tachyon) {
BOOST_CHECK_EQUAL(parentEntry.GetCountWithDescendants(),
testPool.mapTx.size());
BOOST_CHECK_EQUAL(parentEntry.GetSizeWithDescendants(), totalSize);
BOOST_CHECK_EQUAL(parentEntry.GetVirtualSizeWithDescendants(),
totalVirtualSize_strict);
BOOST_CHECK_EQUAL(parentEntry.GetModFeesWithDescendants(), totalFee);
BOOST_CHECK_EQUAL(parentEntry.GetSigOpCountWithDescendants(),
totalSigOpCount);
} else {
// with tachyon latched, we stop tracking these -- they stay at their defaults
BOOST_CHECK_EQUAL(parentEntry.GetCountWithDescendants(), 1);
BOOST_CHECK_EQUAL(parentEntry.GetSizeWithDescendants(), parentEntry.GetTxSize());
BOOST_CHECK_EQUAL(parentEntry.GetVirtualSizeWithDescendants(), parentEntry.GetTxVirtualSize());
BOOST_CHECK_EQUAL(parentEntry.GetModFeesWithDescendants(), parentEntry.GetModifiedFee());
BOOST_CHECK_EQUAL(parentEntry.GetSigOpCount(), 0);
BOOST_CHECK_EQUAL(parentEntry.GetSigOpCountWithDescendants(), 0);
}
// Verify that Tachyon activation status didn't accidentally change during the test.
BOOST_CHECK_EQUAL(testPool.tachyonLatched, tachyon);
}
}
}

BOOST_AUTO_TEST_CASE(MempoolRemoveTest) {
// Test CTxMemPool::remove functionality

Expand Down Expand Up @@ -610,10 +505,53 @@ make_tx(std::vector<Amount> &&output_values,
return MakeTransactionRef(tx);
}

struct TestMemPoolWithAncestryChecker : CTxMemPool
{
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
// find parent with highest descendant count
std::vector<txiter> candidates;
setEntries counted;
candidates.push_back(entry);
uint64_t maximum = 0;
while (candidates.size()) {
txiter candidate = candidates.back();
candidates.pop_back();
if (!counted.insert(candidate).second) {
continue;
}
const setEntries &parents = GetMemPoolParents(candidate);
if (parents.size() == 0) {
setEntries descendants;
CalculateDescendants(candidate, descendants);
maximum = std::max(maximum, uint64_t{descendants.size()});
} else {
for (txiter i : parents) {
candidates.push_back(i);
}
}
}
return maximum;
}

void GetTransactionAncestry_deprecated_slow(const TxId &txId, size_t &ancestors, size_t &descendants) const
EXCLUSIVE_LOCKS_REQUIRED(cs) {
ancestors = descendants = 0;
auto it = mapTx.find(txId);
if (it == mapTx.end())
return;
const auto &entry = *it;
CTxMemPool::setEntries setAncestors;
std::string errString;
CalculateMemPoolAncestors(entry, setAncestors, false);
ancestors = setAncestors.size() + 1 /* add this tx */;
descendants = CalculateDescendantMaximum(it);
}
};

BOOST_AUTO_TEST_CASE(MempoolAncestryTests) {
size_t ancestors, descendants;

CTxMemPool pool;
TestMemPoolWithAncestryChecker pool;
LOCK2(cs_main, pool.cs);
TestMemPoolEntryHelper entry;

Expand Down

0 comments on commit 55ac736

Please sign in to comment.