refactor: log `nEvicted` message in `LimitOrphans` then return void #25683

pull chinggg wants to merge 1 commits into bitcoin:master from chinggg:fix-txorphan-limit changing 4 files +5 −10
  1. chinggg commented at 12:57 PM on July 23, 2022: contributor

    Fix https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=49347

    LimitOrphans() can log expired tx and it should log evicted tx as well instead of returning the nEvicted number for caller to print the message. Since LimitOrphans() now returns void, the redundant assertion check in fuzz test is also removed.

  2. DrahtBot added the label Tests on Jul 23, 2022
  3. DrahtBot commented at 3:18 PM on July 23, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

  4. in src/txorphanage.cpp:129 in 50364e17cf outdated
     128 |          // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
     129 |          nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
     130 | -        if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
     131 | +        if (nExpired > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nExpired);
     132 |      }
     133 | +    unsigned int nEvicted = nExpired;
    


    MarcoFalke commented at 2:08 PM on July 24, 2022:

    You are now logging expired txs as evicted, see the caller:

                    // DoS prevention: do not allow m_orphanage to grow unbounded (see CVE-2012-3789)
                    unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, gArgs.GetIntArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
                    unsigned int nEvicted = m_orphanage.LimitOrphans(nMaxOrphanTx);
                    if (nEvicted > 0) {
                        LogPrint(BCLog::MEMPOOL, "orphanage overflow, removed %u tx\n", nEvicted);
                    }
    

    chinggg commented at 2:27 PM on July 24, 2022:

    I didn't notice LimitOrphans is called here. So it meant to return the number of evicted tx (not including expired), though the number is only used for logging.

  5. MarcoFalke commented at 2:09 PM on July 24, 2022: member

    Not sure if the prefix is accurate. You are modifying a log message, not a test.

    I think an alternative way to solve this would be to move both log messages into the function and not return anything?

  6. chinggg force-pushed on Jul 25, 2022
  7. chinggg force-pushed on Jul 25, 2022
  8. chinggg renamed this:
    test: Fix `nEvicted` returned by `LimitOrphans`
    log: move `nEvicted` message into `LimitOrphans`
    on Jul 25, 2022
  9. Onyeali approved
  10. in src/txorphanage.cpp:139 in 7b9b184dfa outdated
     134 | @@ -135,7 +135,8 @@ unsigned int TxOrphanage::LimitOrphans(unsigned int max_orphans)
     135 |          EraseTx(m_orphan_list[randompos]->first);
     136 |          ++nEvicted;
     137 |      }
     138 | -    return nEvicted;
     139 | +    if (nEvicted > 0) LogPrint(BCLog::MEMPOOL, "orphanage overflow, removed %u tx\n", nEvicted);
     140 | +    return nExpired + nEvicted;
    


    MarcoFalke commented at 5:55 AM on July 25, 2022:

    The return value isn't used outside of tests, so I think it can be removed. If someone is interested in the size, they can just call .size().


    chinggg commented at 8:15 AM on July 25, 2022:
    auto size_before = orphanage.Size();
    auto limit = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
    auto n_evicted = WITH_LOCK(g_cs_orphans, return orphanage.LimitOrphans(limit));
    Assert(size_before - n_evicted <= limit);
    Assert(orphanage.Size() <= limit);
    

    In most cases size_before - n_evicted == orphange.Size(), so we don't need the return value of LimitOrphans. But IMO it will not hurt to use the return value for an additional check. Actually I also think EraseForBlock can return nErased.


    MarcoFalke commented at 9:07 AM on July 25, 2022:

    In most cases size_before - n_evicted == orphange.Size(), so we don't need the return value of LimitOrphans

    Can you give an example where the equality wouldn't hold, assuming after the changes in this pull requests?


    chinggg commented at 9:33 AM on July 25, 2022:

    The equality wouldn't hold if n_evicted or n_expired is incremented but EraseTx fail to remove tx from m_orphanage. It seems this never happen since the txid passed to EraseTx is valid. So feel free to request change and I will make the function return void.


    MarcoFalke commented at 9:38 AM on July 25, 2022:

    Yeah, I am thinking, either:

    • the equality holds, in which case the return value is unused and redundant, also confuse the devs which is the "correct" way to get the size. Also, if the return value was used in the future, it can trivially be added back.
    • the equality doesn't hold, in which case the return value is confusing, since it is wrong.

    Regardless of which case holds, removing the return value is likely the correct "fix".

  11. MarcoFalke removed the label Tests on Jul 25, 2022
  12. DrahtBot added the label Utils/log/libs on Jul 25, 2022
  13. in src/txorphanage.cpp:127 in f813763eaf outdated
     127 |              }
     128 |          }
     129 |          // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
     130 |          nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
     131 | -        if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
     132 | +        if (nExpired > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nExpired);
    


    MarcoFalke commented at 11:55 AM on July 25, 2022:

    The current name isn't optimal. There are two options here:

    • You don't change the name of nErased to nExpired, because it is not needed.
    • You change the name to num_expired, because the style guide recommends that for new symbols.

    chinggg commented at 12:33 PM on July 25, 2022:

    I prefer the 1st option. Since other functions in txorphanage also have camelCase symbols, its better to change them all in a style: commit.

    Feel free to change the PR title since its not only about log now.

  14. chinggg force-pushed on Jul 25, 2022
  15. in src/txorphanage.cpp:113 in a61fb900bc outdated
     111 |      static int64_t nNextSweep;
     112 |      int64_t nNow = GetTime();
     113 |      if (nNextSweep <= nNow) {
     114 |          // Sweep out expired orphan pool entries:
     115 | -        int nErased = 0;
     116 | +        unsigned int nErased = 0;
    


    MarcoFalke commented at 12:34 PM on July 25, 2022:

    This doesn't matter, but it might be better to leave this as-is as well, as the format token is %d, not %u

  16. MarcoFalke commented at 12:35 PM on July 25, 2022: member
  17. chinggg force-pushed on Jul 25, 2022
  18. chinggg force-pushed on Jul 25, 2022
  19. chinggg renamed this:
    log: move `nEvicted` message into `LimitOrphans`
    refactor: log `nEvicted` message in `LimitOrphans` then return void
    on Jul 25, 2022
  20. MarcoFalke removed the label Utils/log/libs on Jul 25, 2022
  21. MarcoFalke commented at 12:51 PM on July 25, 2022: member

    review ACK 9fd3102319903694f27adabdaafdaf770df40e83

  22. DrahtBot added the label Refactoring on Jul 25, 2022
  23. fanquake requested review from dergoegge on Jul 25, 2022
  24. fanquake requested review from glozow on Jul 25, 2022
  25. dergoegge approved
  26. dergoegge commented at 2:58 PM on July 27, 2022: member

    ACK 9fd3102319903694f27adabdaafdaf770df40e83

    LimitOrphans() removes expired transactions from the orphanage and additionally evicts transactions at random should the limit still be exceeded after removing expired transactions. Before this PR, the number of evicted transactions (excluding the number of expired transactions) was returned, which let to hitting an asserting in the fuzz tests that assumed the return value to be the total number of removed transactions (evicted + expired).

    This PR removes the assertion from the fuzz test and changes LimitOrphans() to return nothing (the logging of the number of evicted transactions is therefore moved to the orphanage).

    nit: The PR description still says "Now the function returns the sum of expired and evicted tx, used for test assertion." which is not true for the current version of the PR.

  27. refactor: log `nEvicted` message in `LimitOrphans` then return void
    `LimitOrphans()` can log expired tx and it should log evicted tx as well
    instead of returning the number for caller to print the message.
    Since `LimitOrphans()` now return void, the redundant assertion check in
    fuzz test is also removed.
    b4b657ba57
  28. chinggg force-pushed on Jul 28, 2022
  29. chinggg commented at 6:43 AM on July 28, 2022: contributor

    Thanks for reviewing, I modify the PR description and a typo redundant in commit message.

  30. MarcoFalke approved
  31. MarcoFalke merged this on Jul 29, 2022
  32. MarcoFalke closed this on Jul 29, 2022

  33. sidhujag referenced this in commit b16c3ee410 on Jul 29, 2022
  34. bitcoin locked this on Jul 29, 2023


glozow


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: 2026-04-22 06:13 UTC

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