<p>Your first point (&quot;Excessive package comparisons&quot;) gets at the essence of the issue, though it's not exactly what you described.</p>
<p>There are two main operations where the ancestor and descendant limits come up in practice: mining and eviction. When constructing a block template, what we do is iterate over the mempool in descending &quot;ancestor feerate order&quot;, where the ancestor feerate of a transaction is defined as the minimum of it's own feerate and the total feerate including its unconfirmed ancestors (dependencies).  We select the highest such transaction for inclusion in the block candidate, including all unconfirmed ancestors, and then we'd like to update the sort order of the remaining transactions so that we no longer include as ancestors the transactions which have been selected.</p>
<p>So if you imagine that we might select a transaction with 24 ancestors as our first choice, then for each of the 25 transactions that we add to the block, we must potentially update the ancestor scores of 24 transactions that descend from those transactions.  So there's (up to) an n^2 blowup here in the amount of work we do for each transaction we add, where n is the number of ancestors or descendants that we permit.</p>
<p>Something similar happens in the eviction algorithm.  There, we look at the transaction that has the worst feerate-with-descendants score, and evict it along with all descendants.  After we do so, we have to update the descendant scores of all the ancestors of those evicted transactions, which again is an n^2 amount of work we might have to do.</p>
<p>The reason we have these limits is related to these O(n^2) computations  that come up.  25 seemed low enough that we can mostly manage; raising it to (say) 100 would mean these algorithms could take 16 times as long to run (in the worst case).</p>
<p>For some additional background, there was some discussion on the bitcoin-dev mailing list back when these limits were proposed:</p>
<ul>
<li><p>Initial (generous) limits were first proposed here: <a href="https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-August/010221.html" rel="noreferrer">https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-August/010221.html</a></p>
</li>
<li><p>Later, the proposal was significantly tightened up, after much thinking about various denial-of-service attacks:
<a href="https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-October/011401.html" rel="noreferrer">https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-October/011401.html</a></p>
</li>
</ul>

CC BY-SA 4.0
2023-05-05T21:50:06.110Z
Excellent answer, thanks!  It occurs to me, looking at my original question, that we do have separate limits to address the separate problems.  -limit(ancestor|descendant)count addresses the quadratic blowup in template creation; -limit(ancestor|descendant)size addresses the problem of a single mined replacement invalidating an excessive amount of mempool contents (in addition to addressing other problems mentioned in your emails).
David A. Harding
CC BY-SA 4.0
2023-05-06T11:01:32.340Z
Yes, that's right. The size limits (in vbytes) actually address a couple different issues.  The ancestor size limit is there to ensure that we don't take a transaction to our mempool that is unlikely to confirm anytime soon because it depends on too many transactions (imagine a transaction that required 1MB of ancestors to be confirmed first).  The descendant size limit is there to mitigate the problem of "free relay" when transactions are evicted from the mempool (when the mempool gets full, we evict some tx and its descendants, and we don't want that to be too large).
sdaftuar
CC BY-SA 4.0
2023-05-05T19:07:00.480Z
2023-05-05T19:07:00.480Z
sdaftuar
<p>Since 2015, Bitcoin Core has included a <code>-limitdescendantcount</code> configuration parameter (added in <a href="https://github.com/bitcoin/bitcoin/commit/5add7a74a672cb12b0a2a630d318d9bc64dd0f77" rel="noreferrer">this commit</a>) that forbids a transaction in a node's mempool from having more than x descendants accepted into that mempool (default x is 25).  I think this addresses two concerns:</p>
<ul>
<li><p><strong>Excessive package comparisons:</strong> for ancestor feerate mining, a node builds multiple different virtual packages containing related transactions and derives their package feerate.  If the package feerate is higher than the feerate of the individual transactions contained within it, the node will prefer to keep and mine that package over the individual transactions.  The more related transactions there are in a mempool, the greater the number of packages that need to be compared; given the choice of 25 as the value here, I'm guessing the number of comparisons that needs to be made isn't combinatorial but can be exponential in the worst case.</p>
</li>
<li><p><strong>Wasted bandwidth and censorship:</strong> if a node accepts one parent with as many descendents as is possible to fit in its mempool, then a miner producing a block with a transaction that conflicts with the parent can invalidate the entire mempool.  This can waste a huge amount of relay node bandwidth (~150 MB every 10 minutes times ~50,000 nodes is 540 TB per day, not including inv overhead).  It may also be able to use this mechanism to prevent other people's unrelated transactions from confirming at low cost to the denial of service attacker.</p>
</li>
</ul>
<p>Which of those concerns is the one that most motivates keeping this limit at 25?  I realize the answer there might be opinionated (and so a poor choice for this site), but imagine that we had to disentangle the concerns and have two different limits: one for preventing excessive comparisons and one for reducing the liklihood of wasted bandwidth.  In that case, what <code>limitdescendantcount</code> values would we use for each of those different cases?</p>

<bitcoin-core><mempool><relay>
What's the governing motivation for the descendent size limit?
David A. Harding
# What's the governing motivation for the descendent size limit?

(Asked by: username_0 on 2023-05-05)

Since 2015, Bitcoin Core has included a `-limitdescendantcount` configuration parameter (added in [this commit](https://github.com/bitcoin/bitcoin/commit/5add7a74a672cb12b0a2a630d318d9bc64dd0f77)) that forbids a transaction in a node's mempool from having more than x descendants accepted into that mempool (default x is 25). I think this addresses two concerns:

* **Excessive package comparisons:** for ancestor feerate mining, a node builds multiple different virtual packages containing related transactions and derives their package feerate. If the package feerate is higher than the feerate of the individual transactions contained within it, the node will prefer to keep and mine that package over the individual transactions. The more related transactions there are in a mempool, the greater the number of packages that need to be compared; given the choice of 25 as the value here, I'm guessing the number of comparisons that needs to be made isn't combinatorial but can be exponential in the worst case.
* **Wasted bandwidth and censorship:** if a node accepts one parent with as many descendents as is possible to fit in its mempool, then a miner producing a block with a transaction that conflicts with the parent can invalidate the entire mempool. This can waste a huge amount of relay node bandwidth (~150 MB every 10 minutes times ~50,000 nodes is 540 TB per day, not including inv overhead). It may also be able to use this mechanism to prevent other people's unrelated transactions from confirming at low cost to the denial of service attacker.

Which of those concerns is the one that most motivates keeping this limit at 25? I realize the answer there might be opinionated (and so a poor choice for this site), but imagine that we had to disentangle the concerns and have two different limits: one for preventing excessive comparisons and one for reducing the liklihood of wasted bandwidth. In that case, what `limitdescendantcount` values would we use for each of those different cases?

**Tags:** bitcoin-core, mempool, relay


---

## 1 answers

### Answer by username_1 on 2023-05-05. Score: 7 | Accepted answer

Your first point ("Excessive package comparisons") gets at the essence of the issue, though it's not exactly what you described.

There are two main operations where the ancestor and descendant limits come up in practice: mining and eviction. When constructing a block template, what we do is iterate over the mempool in descending "ancestor feerate order", where the ancestor feerate of a transaction is defined as the minimum of it's own feerate and the total feerate including its unconfirmed ancestors (dependencies). We select the highest such transaction for inclusion in the block candidate, including all unconfirmed ancestors, and then we'd like to update the sort order of the remaining transactions so that we no longer include as ancestors the transactions which have been selected.

So if you imagine that we might select a transaction with 24 ancestors as our first choice, then for each of the 25 transactions that we add to the block, we must potentially update the ancestor scores of 24 transactions that descend from those transactions. So there's (up to) an n^2 blowup here in the amount of work we do for each transaction we add, where n is the number of ancestors or descendants that we permit.

Something similar happens in the eviction algorithm. There, we look at the transaction that has the worst feerate-with-descendants score, and evict it along with all descendants. After we do so, we have to update the descendant scores of all the ancestors of those evicted transactions, which again is an n^2 amount of work we might have to do.

The reason we have these limits is related to these O(n^2) computations that come up. 25 seemed low enough that we can mostly manage; raising it to (say) 100 would mean these algorithms could take 16 times as long to run (in the worst case).

For some additional background, there was some discussion on the bitcoin-dev mailing list back when these limits were proposed:

* Initial (generous) limits were first proposed here: <https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-August/010221.html>
* Later, the proposal was significantly tightened up, after much thinking about various denial-of-service attacks:
  <https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-October/011401.html>

> **Comments:**
> **username_0**: Excellent answer, thanks!  It occurs to me, looking at my original question, that we do have separate limits to address the separate problems.  -limit(ancestor|descendant)count addresses the quadratic blowup in template creation; -limit(ancestor|descendant)size addresses the problem of a single mined replacement invalidating an excessive amount of mempool contents (in addition to addressing other problems mentioned in your emails).  
> **username_1**: Yes, that's right. The size limits (in vbytes) actually address a couple different issues.  The ancestor size limit is there to ensure that we don't take a transaction to our mempool that is unlikely to confirm anytime soon because it depends on too many transactions (imagine a transaction that required 1MB of ancestors to be confirmed first).  The descendant size limit is there to mitigate the problem of "free relay" when transactions are evicted from the mempool (when the mempool gets full, we evict some tx and its descendants, and we don't want that to be too large).  

---

