wallet: Fix typo in assert that is compile-time true #18853

pull MarcoFalke wants to merge 1 commits into bitcoin:master from MarcoFalke:2004-walletAssertAlwaysTrue changing 2 files +11 −2
  1. MarcoFalke commented at 6:04 PM on May 2, 2020: member

    Commit 92bcd70808b9cac56b184903aa6d37baf9641b37 presumably added a check that a dest of type CNoDestination implies an empty scriptChange.

    However, it accidentally checked for boost::variant::empty, which always returns false: https://www.boost.org/doc/libs/1_72_0/doc/html/boost/variant.html#id-1_3_46_5_4_1_1_16_2-bb

  2. DrahtBot added the label Tests on May 2, 2020
  3. DrahtBot added the label Wallet on May 2, 2020
  4. fanquake requested review from Sjors on May 3, 2020
  5. fanquake commented at 12:11 AM on May 3, 2020: member

    Looks like this was pointed out at the time:

    It's not clear to me what this assert is checking for.

    If dest is not empty, we got a change destination and so scriptChange is not empty. If dest is empty, then scriptChange will be empty. So this just covers all cases and can't fail? .... I guess it's fine, but it just seems like useless code that might be confusing to future readers. I would suggest that it be removed in a followup that touches this code.

    Maybe just remove entirely as suggested by @achow101 ?

  6. MarcoFalke commented at 12:21 AM on May 3, 2020: member

    At least now the check will fail when the assumption doesn't hold, so it is no longer entirely useless and confusing. I have a slight preference to document this assumption. Either with a CHECK_NONFATAL, or a test, or a developer comment, not sure about removing it altogether.

  7. MarcoFalke removed the label Tests on May 3, 2020
  8. in src/wallet/wallet.cpp:2780 in fa1b24164f outdated
    2776 | @@ -2777,7 +2777,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
    2777 |                      strFailReason = _("Transaction needs a change address, but we can't generate it. Please call keypoolrefill first.").translated;
    2778 |                  }
    2779 |                  scriptChange = GetScriptForDestination(dest);
    2780 | -                assert(!dest.empty() || scriptChange.empty());
    2781 | +                CHECK_NONFATAL(!boost::get<CNoDestination>(&dest) == !scriptChange.empty());
    


    hebasto commented at 12:30 AM on May 3, 2020:

    Could the boost::get() return value be converted to bool explicitly?


    MarcoFalke commented at 12:20 PM on May 3, 2020:

    Could the boost::get() return value be converted to bool explicitly?

    Yes, for example with the !! "operator".

                    CHECK_NONFATAL(!!boost::get<CNoDestination>(&dest) == scriptChange.empty());
    
  9. hebasto commented at 12:31 AM on May 3, 2020: member

    Concept ACK.

  10. MarcoFalke force-pushed on May 3, 2020
  11. MarcoFalke force-pushed on May 3, 2020
  12. promag commented at 11:48 PM on May 3, 2020: member

    Concept ACK, how did you catch this?

    Could we also do this?

    CScript GetScriptForDestination(const CTxDestination& dest)
    {
        CScript script;
    
        bool result = boost::apply_visitor(CScriptVisitor(&script), dest);
        assert(result != script.empty());
        return script;
    }
    
  13. MarcoFalke commented at 11:54 PM on May 3, 2020: member

    Concept ACK, how did you catch this?

    Reading the source code and boost documentation

  14. in src/wallet/wallet.cpp:2780 in fa1529de34 outdated
    2776 | @@ -2777,7 +2777,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
    2777 |                      strFailReason = _("Transaction needs a change address, but we can't generate it. Please call keypoolrefill first.").translated;
    2778 |                  }
    2779 |                  scriptChange = GetScriptForDestination(dest);
    2780 | -                assert(!dest.empty() || scriptChange.empty());
    2781 | +                CHECK_NONFATAL(bool(boost::get<CNoDestination>(&dest)) == scriptChange.empty());
    


    Sjors commented at 9:50 AM on May 4, 2020:

    Maybe this is more readable:

    // scriptChange must be empty if dest is a dummy CNoDestination
    CHECK_NONFATAL(IsValidDestination(dest) || scriptChange.empty());
    

    MarcoFalke commented at 11:28 AM on May 4, 2020:

    Thanks, done

  15. Sjors commented at 10:03 AM on May 4, 2020: member

    Yikes, good catch.

    The reason I added that check was there used to be an early return if the keypool ran out. I want to make sure we don't send change into a void. We now return later (line 2999) if this error occurs. That check relies on scriptChange.empty(), hence my assert here to make really sure of that.

  16. MarcoFalke force-pushed on May 4, 2020
  17. MarcoFalke commented at 11:28 AM on May 4, 2020: member

    switched to IsValidDestination helper

  18. MarcoFalke force-pushed on May 4, 2020
  19. wallet: Fix typo in assert that is compile-time true fa47cf9d95
  20. MarcoFalke force-pushed on May 4, 2020
  21. Sjors commented at 3:03 PM on May 4, 2020: member

    utACK fa47cf9d95dc2c2822fc96df16f179176935bf96

  22. MarcoFalke commented at 7:14 PM on May 4, 2020: member

    Open-Close to re-run ci. See #15847 (comment)

  23. MarcoFalke closed this on May 4, 2020

  24. MarcoFalke reopened this on May 4, 2020

  25. laanwj merged this on May 6, 2020
  26. laanwj closed this on May 6, 2020

  27. MarcoFalke deleted the branch on May 6, 2020
  28. sidhujag referenced this in commit e0d7d5e99c on May 12, 2020
  29. Fabcien referenced this in commit 9059575a88 on Jan 28, 2021
  30. DrahtBot locked this on Feb 15, 2022

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-17 06:14 UTC

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