wallet: Change coin selection fee assert to error #26611

pull achow101 wants to merge 2 commits into bitcoin:master from achow101:coin-sel-dont-assert changing 3 files +12 −3
  1. achow101 commented at 5:54 PM on November 30, 2022: member

    Returning an error instead of asserting for the low fee check will be better as it does not crash the node and instructs users to report the bug.

  2. DrahtBot commented at 5:54 PM on November 30, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK aureleoules, furszy, S3RK
    Concept ACK MarcoFalke
    Stale ACK 1440000bytes

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #26643 (wallet: Move fee underpayment check to after all fee has been set by achow101)

    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. DrahtBot added the label Wallet on Nov 30, 2022
  4. ghost commented at 9:58 PM on November 30, 2022: none
  5. in src/wallet/spend.cpp:944 in a2b9c886c6 outdated
     939 | @@ -940,7 +940,9 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
     940 |  
     941 |      // The only time that fee_needed should be less than the amount available for fees is when
     942 |      // we are subtracting the fee from the outputs. If this occurs at any other time, it is a bug.
     943 | -    assert(coin_selection_params.m_subtract_fee_outputs || fee_needed <= nFeeRet);
     944 | +    if (!coin_selection_params.m_subtract_fee_outputs && fee_needed > nFeeRet) {
     945 | +        return util::Error{strprintf(_("Internal bug detected: Fee needed > fee required. Please report this issue to %s"), PACKAGE_BUGREPORT)};
    


    maflcko commented at 9:11 AM on December 1, 2022:

    nit: Is there any value in translating an internal bug report, which ideally should never happen?

    Also, it could make sense to make it usable in other places as well by defining a macro for it. This could allow to auto-attach more info to the bug report, such as the version and source code location.

    Example:

    diff --git a/src/util/check.cpp b/src/util/check.cpp
    index e50d6087d0..b17d48bfb1 100644
    --- a/src/util/check.cpp
    +++ b/src/util/check.cpp
    @@ -14,10 +14,13 @@
     #include <cstdlib>
     #include <string>
     
    +std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func)
    +{
    +    return strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", msg, file, line, func, PACKAGE_BUGREPORT);
    +}
     
     NonFatalCheckError::NonFatalCheckError(const char* msg, const char* file, int line, const char* func)
    -    : std::runtime_error{
    -          strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", msg, file, line, func, PACKAGE_BUGREPORT)}
    +    : std::runtime_error{StrFromatInternalBug(msg, file, line, func)}
     {
     }
     
    diff --git a/src/util/check.h b/src/util/check.h
    index b6c03bed2a..b791944502 100644
    --- a/src/util/check.h
    +++ b/src/util/check.h
    @@ -10,12 +10,16 @@
     #include <stdexcept>
     #include <utility>
     
    +std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func);
    +
     class NonFatalCheckError : public std::runtime_error
     {
     public:
         NonFatalCheckError(const char* msg, const char* file, int line, const char* func);
     };
     
    +#define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
    +
     /** Helper for CHECK_NONFATAL() */
     template <typename T>
     T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
    

    achow101 commented at 5:24 PM on December 1, 2022:

    Added this diff as a new commit, and changed to use it STR_INTERNAL_BUG.

  6. maflcko approved
  7. maflcko commented at 9:11 AM on December 1, 2022: member

    ACK

  8. util: Add StrFormatInternalBug and STR_INTERNAL_BUG c6e7f224c1
  9. achow101 force-pushed on Dec 1, 2022
  10. in src/wallet/spend.cpp:944 in 007f6092d7 outdated
     939 | @@ -940,7 +940,9 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
     940 |  
     941 |      // The only time that fee_needed should be less than the amount available for fees is when
     942 |      // we are subtracting the fee from the outputs. If this occurs at any other time, it is a bug.
     943 | -    assert(coin_selection_params.m_subtract_fee_outputs || fee_needed <= nFeeRet);
     944 | +    if (!coin_selection_params.m_subtract_fee_outputs && fee_needed > nFeeRet) {
     945 | +        return util::Error{STR_INTERNAL_BUG("Fee needed > fee required")};
    


    maflcko commented at 5:33 PM on December 1, 2022:
            return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee required"))};
    

    might have to mark untranslated?


    achow101 commented at 6:26 PM on December 1, 2022:

    Fixed

  11. maflcko approved
  12. achow101 force-pushed on Dec 1, 2022
  13. in src/wallet/spend.cpp:944 in a5612ef3af outdated
     939 | @@ -940,7 +940,9 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
     940 |  
     941 |      // The only time that fee_needed should be less than the amount available for fees is when
     942 |      // we are subtracting the fee from the outputs. If this occurs at any other time, it is a bug.
     943 | -    assert(coin_selection_params.m_subtract_fee_outputs || fee_needed <= nFeeRet);
     944 | +    if (!coin_selection_params.m_subtract_fee_outputs && fee_needed > nFeeRet) {
     945 | +        return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee required"))};
    


    S3RK commented at 8:24 AM on December 5, 2022:

    I think you have a typo here:

    -return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee required"))};
    +return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee paid"))};
    

    achow101 commented at 6:00 PM on December 5, 2022:

    Fixed.

  14. aureleoules commented at 11:23 AM on December 5, 2022: member

    Concept ACK

  15. wallet: Change coin selection fee assert to error
    Returning an error instead of asserting for the low fee check will be
    better as it does not crash the node and instructs users to report the
    bug.
    3eb041f014
  16. achow101 force-pushed on Dec 5, 2022
  17. aureleoules approved
  18. aureleoules commented at 6:30 PM on December 5, 2022: member

    ACK 3eb041f014870954db564369a4be4bd0dea48fbe

  19. furszy approved
  20. furszy commented at 10:37 PM on December 5, 2022: member

    ACK 3eb041f0

    With a nit: The error message is a bit ugly on the GUI but it works.

    <img width="536" alt="wallet_internal_bug" src="https://user-images.githubusercontent.com/5377650/205757557-a17e9e80-e056-4a5f-b821-73b8050d6ae0.png">

  21. S3RK commented at 7:31 AM on December 6, 2022: contributor

    ACK 3eb041f014870954db564369a4be4bd0dea48fbe

  22. maflcko merged this on Dec 6, 2022
  23. maflcko closed this on Dec 6, 2022

  24. sidhujag referenced this in commit 8b20caaf2a on Dec 6, 2022
  25. bitcoin locked this on Dec 6, 2023

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-19 00:13 UTC

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