refactor: Replace `std::optional<bilingual_str>` with `util::Result` #25977

pull ryanofsky wants to merge 2 commits into bitcoin:master from ryanofsky:pr/bresult-opt changing 16 files +70 −66
  1. ryanofsky commented at 7:47 PM on September 1, 2022: contributor

    Replace uses of std::optional<bilingual_str> with util::Result as suggested #25648 (review), #27632 (review), #27632 (review), #24313 (review)

  2. DrahtBot added the label Refactoring on Sep 1, 2022
  3. DrahtBot commented at 9:26 PM on September 1, 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 MarcoFalke, TheCharlatan, hebasto

    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:

    • #27300 (build: debug enable addrman consistency checks by willcl-ark)
    • #26762 (refactor: Make CCheckQueue RAII-styled by hebasto)
    • #25665 (refactor: Add util::Result failure values, multiple error and warning messages by ryanofsky)

    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.

  4. DrahtBot cross-referenced this on Sep 1, 2022 from issue refactor: Use util::Result class for wallet loading by ryanofsky
  5. DrahtBot cross-referenced this on Sep 1, 2022 from issue refactor: Add util::Result failure values, multiple error and warning messages by ryanofsky
  6. DrahtBot cross-referenced this on Sep 2, 2022 from issue Use steady clock for all millis bench logging by maflcko
  7. DrahtBot cross-referenced this on Sep 6, 2022 from issue Add util::ResultPtr class by ryanofsky
  8. DrahtBot cross-referenced this on Sep 7, 2022 from issue assumeutxo: background validation completion by jamesob
  9. DrahtBot cross-referenced this on Sep 7, 2022 from issue assumeutxo: snapshot initialization by jamesob
  10. DrahtBot cross-referenced this on Sep 8, 2022 from issue CChainState -> Chainstate by jamesob
  11. ryanofsky force-pushed on Sep 12, 2022
  12. ryanofsky commented at 6:53 PM on September 12, 2022: contributor

    Updated 9d3918d47daf7b118507ea6db0dcef69cedf7005 -> ebc493e94bbc43b6611b7211b77c12c453171ce9 (pr/bresult-opt.1 -> pr/bresult-opt.2, compare) to fix CI compile error https://cirrus-ci.com/task/6195420723412992?logs=ci#L2419 Rebased ebc493e94bbc43b6611b7211b77c12c453171ce9 -> f1a9c713540b3f00cea3c29ace4958b77390a8ef (pr/bresult-opt.2 -> pr/bresult-opt.3, compare) on top of pr/bresult2.24 to fix conflicts in base PR #25665 Rebased f1a9c713540b3f00cea3c29ace4958b77390a8ef -> fb94363ba45acd0b5ca702a60f41533a7b3fbec4 (pr/bresult-opt.3 -> pr/bresult-opt.4, compare) due to conflict with #25667 Rebased fb94363ba45acd0b5ca702a60f41533a7b3fbec4 -> 080268dc424301198ae93c3d002c13dfbfe6b9c9 (pr/bresult-opt.4 -> pr/bresult-opt.5, compare) due to conflict with #26251 and #26286 Rebased 080268dc424301198ae93c3d002c13dfbfe6b9c9 -> 2804dd4033f9ca636ff7df74453b9ddc048417de (pr/bresult-opt.5 -> pr/bresult-opt.6, compare) Rebased 2804dd4033f9ca636ff7df74453b9ddc048417de -> 4883d30d6a764d9fa551ceecdc807fa3283c891a (pr/bresult-opt.6 -> pr/bresult-opt.7, compare) on top of conflicted pr/bresult2.30, Rebased 4883d30d6a764d9fa551ceecdc807fa3283c891a -> 59c96ae3342d1e396a1df63d0af8457ecf60964a (pr/bresult-opt.7 -> pr/bresult-opt.8, compare) due to conflict with #27254 Rebased 59c96ae3342d1e396a1df63d0af8457ecf60964a -> 74655e5870888e0165c62fec75000fe04f062147 (pr/bresult-opt.8 -> pr/bresult-opt.9, compare) on top of pr/bresult2.33 #27254 Rebased 74655e5870888e0165c62fec75000fe04f062147 -> b2bcba068e2c5542ab157e62c82bb868f678beeb (pr/bresult-opt.9 -> pr/bresult-opt.10, compare) dropping dependency on #27254

  13. DrahtBot added the label Needs rebase on Sep 13, 2022
  14. DrahtBot cross-referenced this on Sep 14, 2022 from issue Multiprocess bitcoin by ryanofsky
  15. ryanofsky force-pushed on Sep 20, 2022
  16. DrahtBot removed the label Needs rebase on Sep 20, 2022
  17. DrahtBot cross-referenced this on Sep 21, 2022 from issue test: clean and extend availablecoins_tests coverage by furszy
  18. DrahtBot cross-referenced this on Oct 6, 2022 from issue multiprocess: Add bitcoin-gui -ipcconnect option by ryanofsky
  19. DrahtBot cross-referenced this on Oct 6, 2022 from issue multiprocess: Add bitcoin-wallet -ipcconnect option by ryanofsky
  20. DrahtBot cross-referenced this on Oct 7, 2022 from issue validation: Improve error handling when VerifyDB dosn't finish successfully by mzumsande
  21. DrahtBot cross-referenced this on Oct 10, 2022 from issue test: Remove unused txmempool include from tests by maflcko
  22. DrahtBot added the label Needs rebase on Oct 13, 2022
  23. ryanofsky force-pushed on Oct 14, 2022
  24. DrahtBot removed the label Needs rebase on Oct 14, 2022
  25. DrahtBot added the label Needs rebase on Oct 19, 2022
  26. ryanofsky force-pushed on Jan 20, 2023
  27. DrahtBot removed the label Needs rebase on Jan 21, 2023
  28. DrahtBot cross-referenced this on Jan 21, 2023 from issue refactor: Add BlockManager getters by maflcko
  29. DrahtBot cross-referenced this on Jan 21, 2023 from issue Add reindex=auto flag to automatically reindex corrupt data by AaronDewes
  30. DrahtBot cross-referenced this on Jan 21, 2023 from issue wallet: Migrate legacy wallets to descriptor wallets without requiring BDB by achow101
  31. DrahtBot cross-referenced this on Jan 21, 2023 from issue wallet: be able to specify a wallet name and passphrase to migratewallet by achow101
  32. DrahtBot cross-referenced this on Jan 21, 2023 from issue Remove almost all blockstorage globals by maflcko
  33. DrahtBot added the label Needs rebase on Jan 27, 2023
  34. ryanofsky force-pushed on Feb 11, 2023
  35. DrahtBot removed the label Needs rebase on Feb 11, 2023
  36. DrahtBot cross-referenced this on Feb 13, 2023 from issue refactor, wallet: Avoid variable shadowing by hebasto
  37. DrahtBot added the label Needs rebase on Feb 22, 2023
  38. ryanofsky force-pushed on Mar 1, 2023
  39. DrahtBot removed the label Needs rebase on Mar 2, 2023
  40. DrahtBot cross-referenced this on Mar 2, 2023 from issue assumeutxo by jamesob
  41. DrahtBot added the label Needs rebase on Mar 8, 2023
  42. ryanofsky force-pushed on Apr 7, 2023
  43. DrahtBot removed the label Needs rebase on Apr 7, 2023
  44. DrahtBot cross-referenced this on Apr 19, 2023 from issue refactor: Move chain constants to the util library by TheCharlatan
  45. DrahtBot added the label CI failed on Apr 23, 2023
  46. ryanofsky force-pushed on May 3, 2023
  47. DrahtBot removed the label CI failed on May 3, 2023
  48. DrahtBot cross-referenced this on May 5, 2023 from issue Allow accepting non-standard transactions on mainnet by benthecarman
  49. DrahtBot cross-referenced this on May 5, 2023 from issue kernel: Remove args, settings, chainparams, chainparamsbase from kernel library by TheCharlatan
  50. DrahtBot cross-referenced this on May 8, 2023 from issue assumeutxo (2) by jamesob
  51. DrahtBot added the label Needs rebase on May 9, 2023
  52. ryanofsky cross-referenced this on May 12, 2023 from issue Raise on invalid -debug and -loglevel config options by jonatack
  53. ryanofsky cross-referenced this on May 24, 2023 from issue Improve display address handling for external signer by Sjors
  54. util: Add void support to util::Result
    A minimal (but hacky) way to add support for void to Result
    originally posted https://github.com/bitcoin/bitcoin/pull/27632#discussion_r1195604095
    5f49cb1bc8
  55. refactor: Replace std::optional<bilingual_str> with util::Result 8aa8f73adc
  56. ryanofsky force-pushed on May 24, 2023
  57. ryanofsky marked this as ready for review on May 24, 2023
  58. maflcko commented at 2:29 PM on May 24, 2023: member

    Looks like there is a clang bug. Could be fixed by either bumping it again (to clang-13), see #27682, but I am not sure how to do that for macOS. Or by replacing return addrman; with return {std::move(addrman)};, or something like that (temporarily).

    godbolt for clang-12 (broken): https://godbolt.org/z/1hraPjxrf

  59. DrahtBot added the label CI failed on May 24, 2023
  60. DrahtBot removed the label Needs rebase on May 24, 2023
  61. ryanofsky force-pushed on May 24, 2023
  62. ryanofsky commented at 7:35 PM on May 24, 2023: contributor

    Thanks, added std::move as suggested.

    Updated b2bcba068e2c5542ab157e62c82bb868f678beeb -> 8aa8f73adce72e1ae855b43413c1f65504423cb7 (pr/bresult-opt.10 -> pr/bresult-opt.11, compare) with workaround for CI errors https://cirrus-ci.com/task/6456067106799616?logs=ci#L1528 https://cirrus-ci.com/task/5048692223246336?logs=ci#L1240 https://cirrus-ci.com/task/6174592130088960?logs=ci#L1328

    addrdb.cpp:213:12: error: call to implicitly-deleted copy constructor of 'util::Result<std::__1::unique_ptr<AddrMan, std::__1::default_delete<AddrMan> > >::T' (aka 'std::__1::unique_ptr<AddrMan, std::__1::default_delete<AddrMan> >')
        return addrman;
               ^~~~~~~
    /usr/lib/llvm-10/bin/../include/c++/v1/memory:2513:3: note: copy constructor is implicitly deleted because 'unique_ptr<AddrMan, std::__1::default_delete<AddrMan> >' has a user-declared move constructor
      unique_ptr(unique_ptr&& __u) _NOEXCEPT
      ^
    ./util/result.h:47:14: note: passing argument to parameter 'obj' here
        Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
                 ^
    1 error generated.
    
  63. DrahtBot cross-referenced this on May 25, 2023 from issue build: debug enable addrman consistency checks by willcl-ark
  64. DrahtBot cross-referenced this on May 25, 2023 from issue bugfix: Make `CCheckQueue` RAII-styled (attempt 2) by hebasto
  65. DrahtBot removed the label CI failed on May 25, 2023
  66. maflcko commented at 7:17 AM on May 25, 2023: member

    very nice ACK 8aa8f73adce72e1ae855b43413c1f65504423cb ๐Ÿ

    <details><summary>Show signature</summary>

    Signature:

    untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
    RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    trusted comment: very nice ACK 8aa8f73adce72e1ae855b43413c1f65504423cb ๐Ÿ
    FlnWlOVGKZVAPgirGB1Me+n1IHxeaKXXl2LHKPIpsnt+N2jG0ILNCy/guKB9V+K/JzCbDLy9h/UJBgDsXbspAw==
    

    </details>

  67. maflcko requested review from TheCharlatan on May 25, 2023
  68. in src/txdb.h:102 in 8aa8f73adc
      98 | @@ -98,6 +99,6 @@ class CBlockTreeDB : public CDBWrapper
      99 |          EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     100 |  };
     101 |  
     102 | -std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db);
     103 | +util::Result<void> CheckLegacyTxindex(CBlockTreeDB& block_tree_db);
    


    maflcko commented at 7:57 AM on May 25, 2023:

    nit (if you retouch): Could add [[nodiscard]] (either as fixup or new commit), while touching all those header files?

  69. TheCharlatan approved
  70. TheCharlatan commented at 8:02 AM on May 25, 2023: contributor

    ACK 8aa8f73adce72e1ae855b43413c1f65504423cb7

    I think [[nodiscard] qualifiers for all the util::Result return types would be nice.

  71. maflcko assigned ryanofsky on May 25, 2023
  72. in src/util/result.h:46 in 8aa8f73adc
      42 |  
      43 |      template <typename FT>
      44 |      friend bilingual_str ErrorString(const Result<FT>& result);
      45 |  
      46 |  public:
      47 | +    Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {}  // constructor for void
    


    hebasto commented at 10:01 AM on May 26, 2023:

    5f49cb1bc8e6ba0671c21bf6292d2d3de43fd001

    nit: Add #include <utility>?

  73. hebasto approved
  74. hebasto commented at 10:01 AM on May 26, 2023: member

    ACK 8aa8f73adce72e1ae855b43413c1f65504423cb7, I have reviewed the code and it looks OK.

  75. fanquake merged this on May 26, 2023
  76. fanquake closed this on May 26, 2023

  77. hebasto commented at 12:54 PM on May 26, 2023: member

    Looks like there is a clang bug. Could be fixed by either bumping it again, see #27682, but I am not sure how to do that for macOS. Or by replacing return addrman; with return {std::move(addrman)};, or something like that (temporarily).

    Although GCC complains:

    $ ./configure CONFIG_SITE=$PWD/depends/arm-linux-gnueabihf/share/config.site --enable-suppress-external-warnings CXXFLAGS="-Wextra -Wno-psabi"
    $ make > /dev/null 
    addrdb.cpp: In function โ€˜util::Result<std::unique_ptr<AddrMan> > LoadAddrman(const NetGroupManager&, const ArgsManager&)โ€™:
    addrdb.cpp:213:21: warning: redundant move in return statement [-Wredundant-move]
      213 |     return std::move(addrman); // std::move should be unneccessary but is temporarily needed to work around clang bug (https://github.com/bitcoin/bitcoin/pull/25977#issuecomment-1561270092)
          |            ~~~~~~~~~^~~~~~~~~
    addrdb.cpp:213:21: note: remove โ€˜std::moveโ€™ call
    
  78. sidhujag referenced this in commit 20272f34e0 on May 26, 2023
  79. maflcko commented at 8:00 AM on May 29, 2023: member

    Yeah, that's why I suggested {std::move()}, not std::move(), see #25977 (comment)

  80. maflcko cross-referenced this on May 29, 2023 from issue refactor: Add [[nodiscard]] where ignoring a Result return type is an error by maflcko
  81. fanquake referenced this in commit 214f8f18b3 on May 30, 2023
  82. sidhujag referenced this in commit 4b69edba9b on May 30, 2023
  83. Fabcien referenced this in commit e0df40da76 on Dec 15, 2023
  84. Fabcien referenced this in commit f3139ddbac on Dec 15, 2023
  85. bitcoin locked this on May 28, 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: 2026-05-20 06:53 UTC