Refactor spend conflict checks in the mempool storage to increase performance #2784
Closed
Labels
C-enhancement
Category: This is an improvement
S-needs-investigation
Status: Needs further investigation
Milestone
Motivation
ZIP-401 prevents memory DoS by setting a maximum mempool size. This mempool size is much larger than Zebra's current mempool size.
PR #2765 implemented a simple algorithm to check if a transaction has spend conflicts with another transaction already in the mempool.
That algorithm works well for a mempool with few transactions. But as the size of the mempool increases, performance will be impacted. The algorithm is
O(n*m)
, wheren
is the number of spent UTXOs and revealed nullifiers in the transaction andm
is the number of spent UTXOs and revealed nullifiers in all transactions already in the mempool.So as part of implementing ZIP-401, we need to make the spend conflict checks more efficient. Otherwise, we replace a memory DoS with a CPU DoS.
Specifications
https://zips.z.cash/zip-0401#motivation
https://zips.z.cash/zip-0401#rationale
Designs
The original idea was to to cache the spent UTXOs and revealed nullifiers from all transactions in
the mempool using
HashSet
s. This would mean that the conflict check would only beO(n)
, but it adds complexity to the code to add a transaction to the mempool and to remove a transaction from the mempool, because the caches must be updated.If this idea is implemented, the
has_spend_conflicts
method could be updated to something like:The text was updated successfully, but these errors were encountered: