[refactor] Cleanup BlockAssembler mempool usage #28843

pull TheCharlatan wants to merge 1 commits into bitcoin:master from TheCharlatan:blockAssemblerRemoveMempool changing 2 files +9 −6
  1. TheCharlatan commented at 11:06 am on November 10, 2023: contributor

    The addPackageTxs method of the BlockAssembler currently has access to two mempool variables, as an argument and as a member. Clean this up and clarify that they both are the same mempool instance by removing the argument and instead only using the member variable in the method.

    This was noticed in this PR review: #25223 (review).

  2. DrahtBot commented at 11:06 am on November 10, 2023: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/28843.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK danielabrozzoni, stickies-v, achow101
    Stale ACK dergoegge

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #28676 ([WIP] Cluster mempool implementation by sdaftuar)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. maflcko approved
  4. maflcko commented at 11:10 am on November 10, 2023: member
    lgtm
  5. TheCharlatan marked this as ready for review on Nov 10, 2023
  6. dergoegge approved
  7. dergoegge commented at 1:27 pm on November 10, 2023: member
    Code review ACK ed71a7b9e1e67d6155fe1def5acf51beb12c9baf
  8. in src/node/miner.cpp:146 in ed71a7b9e1 outdated
    136@@ -138,9 +137,9 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
    137 
    138     int nPackagesSelected = 0;
    139     int nDescendantsUpdated = 0;
    140-    if (m_mempool) {
    141-        LOCK(m_mempool->cs);
    142-        addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated);
    


    ajtowns commented at 0:23 am on November 11, 2023:
    Why aren’t you just changing addPackageTxs to use m_mempool directly? Seems simpler, and doesn’t change the public api. Something like https://github.com/ajtowns/bitcoin/commit/944d8bc87690389de9bd51a6ea8a3223d10e4002

    TheCharlatan commented at 8:27 am on November 11, 2023:
    Looking at the review discussion in #25223 introducing a check in the function and using the arrow operator feels like a step back in code quality. I also feel like having raw pointers as class members is something we should try to avoid, since its ownership semantics are ambiguous. Relying on the member variable precludes this later comment on the one I linked in the PR description by making a refactor into fully static functions harder.

    ajtowns commented at 1:18 am on November 12, 2023:

    I could see that being an argument for doing away with BlockAssembler entirely (at least as a public interface), and replacing BlockAssembler(chainstate, mempool, options).CreateNewBlock(scriptPubKeyIn) it with a function: CreateNewBlock(chainstate, mempool, scriptPubKeyIn, options) (effectively reverting #7598).

    But if you’re keeping BlockAssembler around, the entire point of that class is to assemble a block with txs from the mempool – that’s why most of its private functions require CTxMemPool::setEntries references, eg – so it seems pretty strange to make it clumsier at doing that by pulling data it needs to do its job out of the class itself.

    (Unrelated, but also seems a bit bogus to call the m_last_block_* things “members” when they’re really globals…)

  9. DrahtBot added the label Needs rebase on Dec 14, 2023
  10. TheCharlatan force-pushed on Mar 13, 2024
  11. TheCharlatan commented at 11:15 am on March 13, 2024: contributor
    Rebased ed71a7b9e1e67d6155fe1def5acf51beb12c9baf -> a323388036d4f8b9fe8ff0915252f20abb91633e (blockAssemblerRemoveMempool_0 -> blockAssemblerRemoveMempool_1, compare)
  12. DrahtBot removed the label Needs rebase on Mar 13, 2024
  13. stickies-v commented at 3:07 pm on March 15, 2024: contributor
    Concept ACK
  14. DrahtBot added the label Needs rebase on Jun 25, 2024
  15. TheCharlatan renamed this:
    [refactor] Remove BlockAssembler m_mempool member
    [refactor] Cleanup BlockAssembler mempool usage
    on Sep 2, 2024
  16. TheCharlatan force-pushed on Sep 2, 2024
  17. TheCharlatan commented at 12:22 pm on September 2, 2024: contributor

    Reworked a323388036d4f8b9fe8ff0915252f20abb91633e -> 247ae848e669cc36c790362b766ff813cdfa3e66 (blockAssemblerRemoveMempool_1 -> blockAssemblerRemoveMempool_2, compare)

    • Took @ajtowns’s suggestion, removing the mempool argument instead of the m_mempool member.
  18. DrahtBot removed the label Needs rebase on Sep 2, 2024
  19. DrahtBot added the label CI failed on Sep 12, 2024
  20. DrahtBot removed the label CI failed on Sep 13, 2024
  21. Khalilheyrani6367 approved
  22. Khalilheyrani6367 commented at 1:44 pm on September 29, 2024: none
    60a0f2ac88c57fdf4482dade2ce409ef2da65998
  23. danielabrozzoni commented at 3:12 pm on October 3, 2024: contributor
    ACK 60a0f2ac88c57fdf4482dade2ce409ef2da65998
  24. DrahtBot requested review from stickies-v on Oct 3, 2024
  25. DrahtBot requested review from dergoegge on Oct 3, 2024
  26. in src/node/miner.h:191 in 60a0f2ac88 outdated
    186@@ -187,8 +187,9 @@ class BlockAssembler
    187     // Methods for how to add transactions to a block.
    188     /** Add transactions based on feerate including unconfirmed ancestors
    189       * Increments nPackagesSelected / nDescendantsUpdated with corresponding
    190-      * statistics from the package selection (for logging statistics). */
    191-    void addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
    192+      * statistics from the package selection (for logging statistics).
    193+      * No-op if the BlockAssembler has been configured without a mempool.*/
    


    stickies-v commented at 10:46 am on October 18, 2024:

    I feel a bit uneasy about adding this no-op, it makes understanding the program flow more difficult and I feel like it is a potential footgun. What do you think about asserting the mempool is configured instead?

     0diff --git a/src/node/miner.cpp b/src/node/miner.cpp
     1index 9e6954d257..afa410da10 100644
     2--- a/src/node/miner.cpp
     3+++ b/src/node/miner.cpp
     4@@ -141,7 +141,9 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
     5 
     6     int nPackagesSelected = 0;
     7     int nDescendantsUpdated = 0;
     8-    addPackageTxs(nPackagesSelected, nDescendantsUpdated);
     9+    if (m_mempool) {
    10+        addPackageTxs(nPackagesSelected, nDescendantsUpdated);
    11+    }
    12 
    13     const auto time_1{SteadyClock::now()};
    14 
    15@@ -291,9 +293,7 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::ve
    16 // transaction package to work on next.
    17 void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated)
    18 {
    19-    if (m_mempool == nullptr) return;
    20-
    21-    const auto& mempool{*m_mempool};
    22+    const auto& mempool{*Assert(m_mempool)};
    23     LOCK(mempool.cs);
    24 
    25     // mapModifiedTx will store sorted packages after they are modified
    26diff --git a/src/node/miner.h b/src/node/miner.h
    27index a262fb7dc5..25ce110b34 100644
    28--- a/src/node/miner.h
    29+++ b/src/node/miner.h
    30@@ -188,7 +188,9 @@ private:
    31     /** Add transactions based on feerate including unconfirmed ancestors
    32       * Increments nPackagesSelected / nDescendantsUpdated with corresponding
    33       * statistics from the package selection (for logging statistics).
    34-      * No-op if the BlockAssembler has been configured without a mempool.*/
    35+      *
    36+      * [@pre](/bitcoin-bitcoin/contributor/pre/) BlockAssembler::m_mempool must not be nullptr
    37+    */
    38     void addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(!m_mempool->cs);
    39 
    40     // helper functions for addPackageTxs()
    
  27. stickies-v commented at 10:59 am on October 18, 2024: contributor
    Approach ACK, code LGTM 60a0f2ac88c57fdf4482dade2ce409ef2da65998 modulo my comment about no-op. I don’t feel particularly strong about the current vs the previous approach, but this has the benefit of being a much smaller change, and it works well.
  28. [refactor] Cleanup BlockAssembler mempool usage
    The `addPackageTxs` method of the `BlockAssembler` currently has access
    to two mempool variables, as an argument and as a member. Clean this up
    and clarify that they both are the same mempool instance by removing the
    argument and instead only using the member variable in the method.
    
    Co-Authored-By: Anthony Towns <aj@erisian.com.au>
    Co-authored-by: stickies-v <stickies-v@protonmail.com>
    192dac1d33
  29. TheCharlatan force-pushed on Oct 21, 2024
  30. TheCharlatan commented at 1:59 pm on October 21, 2024: contributor

    Updated 60a0f2ac88c57fdf4482dade2ce409ef2da65998 -> 192dac1d3370edd579db235d69c034726f37c8da (blockAssemblerRemoveMempool_2 -> blockAssemblerRemoveMempool_3, compare)

    • Added @stickies-v’s patch, making BlockAssembler assert instead of immediately returning if no mempool is present.
  31. danielabrozzoni commented at 2:47 pm on October 21, 2024: contributor
    re-ACK 192dac1
  32. DrahtBot requested review from stickies-v on Oct 21, 2024
  33. stickies-v approved
  34. stickies-v commented at 2:50 pm on October 21, 2024: contributor
    ACK 192dac1d3370edd579db235d69c034726f37c8da
  35. lozanopo approved
  36. achow101 commented at 9:19 pm on November 14, 2024: member
    ACK 192dac1d3370edd579db235d69c034726f37c8da
  37. achow101 merged this on Nov 14, 2024
  38. achow101 closed this on Nov 14, 2024


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-07-05 21:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me