Skip to content

Conversation

sdaftuar
Copy link
Member

Following up on #12127 and #12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of queryHashes() and a small information leak when prioritizing transactions.

Left undone is nuking queryHashes altogether; that would require changing the behavior of the getrawmempool rpc call, which I think I might be in favor of doing, but wanted to save for its own PR.

@meshcollider
Copy link
Contributor

utACK 16a8389

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is usually bigger?

  1. CTxMemPool::mapTx
  2. CBlockPolicyEstimator::mapMemPoolTxs

My suggestion is to loop the shorter map.

@@ -984,10 +984,9 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein)
void CBlockPolicyEstimator::FlushUnconfirmed(CTxMemPool& pool) {
int64_t startclear = GetTimeMicros();
std::vector<uint256> txids;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

for (auto& txid : txids) {
removeTx(txid, false);
LOCK2(pool.cs, cs_feeEstimator);
for (auto mi=pool.mapTx.begin(); mi != pool.mapTx.end(); ++mi) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, can early exit loop if mapMemPoolTxs is empty.

removeTx(txid, false);
LOCK2(pool.cs, cs_feeEstimator);
for (auto mi=pool.mapTx.begin(); mi != pool.mapTx.end(); ++mi) {
removeTx(mi->GetTx().GetHash(), false);
}
int64_t endclear = GetTimeMicros();
LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n",txids.size(), (endclear - startclear)*0.000001);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

txids is empty, update logging.

@sdaftuar
Copy link
Member Author

@promag After further investigation, it looks like FlushUnconfirmed() doesn't need the mempool at all, so I re-wrote it a bit.

// Remove every entry in mapMemPoolTxs
while (!mapMemPoolTxs.empty()) {
auto mi = mapMemPoolTxs.begin();
removeTx(mi->first, false); // this calls erase() on mapMemPoolTxs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO decreasing one by one is avoidable when the end result is to be empty. Prefer something like?

LOCK(cs_feeEstimator);
size_t num_entries = mapMemPoolTxs.size();
for (const auto& it = mapMemPoolTxs.begin(); it != mapMemPoolTxs.end(); ++it) {
    const auto& stat = it->second;
    feeStats->removeTx(stat.blockHeight, nBestSeenHeight, stat.bucketIndex, false);
    shortStats->removeTx(stat.blockHeight, nBestSeenHeight, stat.bucketIndex, false);
    longStats->removeTx(stat.blockHeight, nBestSeenHeight, stat.bucketIndex, false);
}
mapMemPoolTxs.clear();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to duplicate the code in removeTx() or figure out how to refactor it to be more efficient for this purpose -- since FlushUnconfirmed() is only called in shutdown, I don't think the performance savings here from more code changes is worth the review/maintenance burden.

If you'd rather see a bigger refactor here to clean things up, I think I'd prefer to just remove this commit and someone can take it on in a new PR...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as it is. I'll take note to measure an alternative implementation.

src/txmempool.h Outdated
@@ -242,14 +242,17 @@ class CompareTxMemPoolEntryByDescendantScore
/** \class CompareTxMemPoolEntryByScore
*
* Sort by score of entry ((fee+delta)/size) in descending order
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove +delta?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@sdaftuar sdaftuar force-pushed the 2018-01-mempool-cleanups branch from 466267d to 669c943 Compare January 25, 2018 23:23
@sdaftuar
Copy link
Member Author

Squashed 12225.1 -> 669c943

@promag
Copy link
Contributor

promag commented Feb 3, 2018

utACK 669c943.

@laanwj
Copy link
Member

laanwj commented Feb 8, 2018

utACK 669c943

@laanwj laanwj merged commit 669c943 into bitcoin:master Feb 8, 2018
laanwj added a commit that referenced this pull request Feb 8, 2018
669c943 Avoid leaking prioritization information when relaying transactions (Suhas Daftuar)
e868b22 fee estimator: avoid sorting mempool on shutdown (Suhas Daftuar)
0975406 Correct mempool mapTx comment (Suhas Daftuar)

Pull request description:

  Following up on #12127 and #12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of `queryHashes()` and a small information leak when prioritizing transactions.

  Left undone is nuking queryHashes altogether; that would require changing the behavior of the `getrawmempool` rpc call, which I think I might be in favor of doing, but wanted to save for its own PR.

Tree-SHA512: c97d10b96dcd6520459287a4a2eda92774173757695100fcfe61e526aef86f394507c331d17f9e0c14b496c33ec46198a0f165a847762ca50f7c6780b993f162
Mengerian pushed a commit to Mengerian/bitcoin-abc that referenced this pull request Aug 6, 2019
Summary:
669c943 Avoid leaking prioritization information when relaying transactions (Suhas Daftuar)
0975406 Correct mempool mapTx comment (Suhas Daftuar)

Pull request description:

  Following up on #12127 and #12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of `queryHashes()` and a small information leak when prioritizing transactions.

  Left undone is nuking queryHashes altogether; that would require changing the behavior of the `getrawmempool` rpc call, which I think I might be in favor of doing, but wanted to save for its own PR.

Tree-SHA512: c97d10b96dcd6520459287a4a2eda92774173757695100fcfe61e526aef86f394507c331d17f9e0c14b496c33ec46198a0f165a847762ca50f7c6780b993f162

Backport of Core PR12225
bitcoin/bitcoin#12225

Depends on D3711

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: deadalnix, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3789
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 17, 2020
669c943 Avoid leaking prioritization information when relaying transactions (Suhas Daftuar)
e868b22 fee estimator: avoid sorting mempool on shutdown (Suhas Daftuar)
0975406 Correct mempool mapTx comment (Suhas Daftuar)

Pull request description:

  Following up on bitcoin#12127 and bitcoin#12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of `queryHashes()` and a small information leak when prioritizing transactions.

  Left undone is nuking queryHashes altogether; that would require changing the behavior of the `getrawmempool` rpc call, which I think I might be in favor of doing, but wanted to save for its own PR.

Tree-SHA512: c97d10b96dcd6520459287a4a2eda92774173757695100fcfe61e526aef86f394507c331d17f9e0c14b496c33ec46198a0f165a847762ca50f7c6780b993f162
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 17, 2020
669c943 Avoid leaking prioritization information when relaying transactions (Suhas Daftuar)
e868b22 fee estimator: avoid sorting mempool on shutdown (Suhas Daftuar)
0975406 Correct mempool mapTx comment (Suhas Daftuar)

Pull request description:

  Following up on bitcoin#12127 and bitcoin#12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of `queryHashes()` and a small information leak when prioritizing transactions.

  Left undone is nuking queryHashes altogether; that would require changing the behavior of the `getrawmempool` rpc call, which I think I might be in favor of doing, but wanted to save for its own PR.

Tree-SHA512: c97d10b96dcd6520459287a4a2eda92774173757695100fcfe61e526aef86f394507c331d17f9e0c14b496c33ec46198a0f165a847762ca50f7c6780b993f162
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 17, 2020
669c943 Avoid leaking prioritization information when relaying transactions (Suhas Daftuar)
e868b22 fee estimator: avoid sorting mempool on shutdown (Suhas Daftuar)
0975406 Correct mempool mapTx comment (Suhas Daftuar)

Pull request description:

  Following up on bitcoin#12127 and bitcoin#12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of `queryHashes()` and a small information leak when prioritizing transactions.

  Left undone is nuking queryHashes altogether; that would require changing the behavior of the `getrawmempool` rpc call, which I think I might be in favor of doing, but wanted to save for its own PR.

Tree-SHA512: c97d10b96dcd6520459287a4a2eda92774173757695100fcfe61e526aef86f394507c331d17f9e0c14b496c33ec46198a0f165a847762ca50f7c6780b993f162
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants