fuzz: wallet, add target for `fees` #27647

pull brunoerg wants to merge 1 commits into bitcoin:master from brunoerg:2023-05-fuzz-wallet-fees changing 2 files +69 −0
  1. brunoerg commented at 9:57 PM on May 12, 2023: contributor

    This PR adds fuzz coverage for wallet/fees. Some functions may use or not (non default) values from wallet, CCoinControl or FeeCalculation. So the logic is to make the test sometimes fill up some attributes and others no.

    Obs: As soon as this PR gets some reviews, I can open the proper PR to qa-assets as well.

  2. DrahtBot commented at 9:57 PM on May 12, 2023: 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 Xekyo, MarcoFalke, dergoegge

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

  3. DrahtBot added the label Tests on May 12, 2023
  4. brunoerg force-pushed on May 12, 2023
  5. DrahtBot added the label CI failed on May 12, 2023
  6. Aminkavoos approved
  7. brunoerg force-pushed on May 12, 2023
  8. brunoerg force-pushed on May 12, 2023
  9. brunoerg force-pushed on May 12, 2023
  10. DrahtBot removed the label CI failed on May 13, 2023
  11. in src/wallet/test/fuzz/fees.cpp:30 in 583ccc75e7 outdated
      25 | +            FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      26 | +            CMutableTransaction random_mutable_transaction;
      27 | +            const auto& node = g_setup->m_node;
      28 | +            auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
      29 | +
      30 | +            CWallet wallet(node.chain.get(), "", CreateDummyWalletDatabase());
    


    maflcko commented at 6:24 AM on May 13, 2023:

    Any reason to create a fresh wallet for each iteration? Should give more iter/second if this was static.

    Also, could run clang-format to avoid the leading whitespace bloat?


    brunoerg commented at 11:59 AM on May 13, 2023:

    In fact, there is no need to create it for each iteration, gonna change it. Thanks.

  12. brunoerg force-pushed on May 13, 2023
  13. brunoerg commented at 12:00 PM on May 13, 2023: contributor

    Force-pushed addressing @MarcoFalke's review.

    • Changed wallet to be static
    • Changed format according to clang-format
  14. in src/wallet/test/fuzz/fees.cpp:31 in 604f869f1f outdated
      25 | +{
      26 | +    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      27 | +    CMutableTransaction random_mutable_transaction;
      28 | +    const auto& node = g_setup->m_node;
      29 | +    auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
      30 | +    static CWallet wallet(node.chain.get(), "", CreateDummyWalletDatabase());
    


    maflcko commented at 8:27 AM on May 14, 2023:

    This is fine, but I think for documentation purposes, the static globals are initialized in initialize_setup in the fuzz tests


    brunoerg commented at 1:29 PM on May 15, 2023:

    Nice, make sense having it in initialize_setup, if I have to retouch the code again I can do it.


    maflcko commented at 1:43 PM on May 15, 2023:

    Looks like you forgot to address this?


    brunoerg commented at 1:44 PM on May 15, 2023:

    Yea, sorry!


    brunoerg commented at 2:17 PM on May 15, 2023:

    Seems the way to do it would be:

    diff --git a/src/wallet/test/fuzz/fees.cpp b/src/wallet/test/fuzz/fees.cpp
    index c4e1b7d61..fc9b6a5e4 100644
    --- a/src/wallet/test/fuzz/fees.cpp
    +++ b/src/wallet/test/fuzz/fees.cpp
    @@ -15,23 +15,27 @@
     namespace wallet {
     namespace {
     const TestingSetup* g_setup;
    +static std::unique_ptr<CWallet> wallet_ptr;
    +static Chainstate* chainstate = nullptr;
     
     void initialize_setup()
     {
         static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
         g_setup = testing_setup.get();
    +    const auto& node{g_setup->m_node};
    +    chainstate = &node.chainman->ActiveChainstate();
    +    wallet_ptr = std::make_unique<CWallet>(node.chain.get(), "", CreateDummyWalletDatabase());
     }
     
     FUZZ_TARGET_INIT(wallet_fees, initialize_setup)
     {
         FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
         CMutableTransaction random_mutable_transaction;
    -    const auto& node{g_setup->m_node};
    -    const auto& chainstate{node.chainman->ActiveChainstate()};
    -    static CWallet wallet(node.chain.get(), "", CreateDummyWalletDatabase());
    +
    +    CWallet& wallet = *wallet_ptr;
         {
             LOCK(wallet.cs_wallet);
    -        wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
    +        wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
         }
     
         if (fuzzed_data_provider.ConsumeBool()) {
    

    Seems reasonable? Any alternative?


    maflcko commented at 2:19 PM on May 15, 2023:

    Yeah, lgtm. Maybe use g_ prefix for the static globals?


    brunoerg commented at 2:48 PM on May 15, 2023:

    Yea, gonna do it!

  15. in src/wallet/test/fuzz/fees.cpp:29 in 604f869f1f outdated
      24 | +FUZZ_TARGET_INIT(wallet_fees, initialize_setup)
      25 | +{
      26 | +    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      27 | +    CMutableTransaction random_mutable_transaction;
      28 | +    const auto& node = g_setup->m_node;
      29 | +    auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
    


    maflcko commented at 8:27 AM on May 14, 2023:

    Any reason for the cast?


    brunoerg commented at 12:32 PM on May 15, 2023:

    Since I'm not using anything related to mempool, I don't think so, will remove it.

  16. in src/wallet/test/fuzz/fees.cpp:66 in 604f869f1f outdated
      61 | +        (void)GetMinimumFeeRate(wallet, coin_control, &fee_calculation);
      62 | +        (void)GetMinimumFee(wallet, tx_bytes, coin_control, &fee_calculation);
      63 | +    } else {
      64 | +        (void)GetMinimumFeeRate(wallet, coin_control, nullptr);
      65 | +        (void)GetMinimumFee(wallet, tx_bytes, coin_control, nullptr);
      66 | +    }
    


    maflcko commented at 8:30 AM on May 14, 2023:

    nit for less code (feel free to ignore):

            FeeCalculation fee_calculation;
                    FeeCalculation* maybe_fee_calculation{    fuzzed_data_provider.ConsumeBool()?nullptr:&fee_calculation};
            (void)GetMinimumFeeRate(wallet, coin_control, maybe_fee_calculation);
            (void)GetMinimumFee(wallet, tx_bytes, coin_control, maybe_fee_calculation);
    

    brunoerg commented at 1:28 PM on May 15, 2023:

    Seems good, thanks

  17. maflcko approved
  18. fanquake requested review from murchandamus on May 15, 2023
  19. fanquake requested review from furszy on May 15, 2023
  20. DrahtBot added the label CI failed on May 15, 2023
  21. brunoerg force-pushed on May 15, 2023
  22. brunoerg commented at 1:31 PM on May 15, 2023: contributor

    Force-pushed addressing @MarcoFalke's review.

  23. brunoerg force-pushed on May 15, 2023
  24. brunoerg force-pushed on May 15, 2023
  25. brunoerg commented at 2:57 PM on May 15, 2023: contributor

    Force-pushed

    Thanks @MarcoFalke for the review

  26. brunoerg commented at 4:44 PM on May 15, 2023: contributor

    CI failure seems unrelated

  27. brunoerg force-pushed on May 15, 2023
  28. brunoerg commented at 10:37 PM on May 15, 2023: contributor

    Force-pushed to replace CreateMockWalletDatabasefor CreateMockableWalletDatabase.

  29. DrahtBot removed the label CI failed on May 15, 2023
  30. in src/wallet/test/fuzz/fees.cpp:33 in 4018eb268f outdated
      28 | +}
      29 | +
      30 | +FUZZ_TARGET_INIT(wallet_fees, initialize_setup)
      31 | +{
      32 | +    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      33 | +    CMutableTransaction random_mutable_transaction;
    


    Ayush170-Future commented at 6:55 AM on May 31, 2023:

    random_mutable_transaction appears to be of no use.


    brunoerg commented at 4:43 PM on May 31, 2023:

    Removed it, thanks!

  31. brunoerg force-pushed on May 31, 2023
  32. brunoerg commented at 4:43 PM on May 31, 2023: contributor

    Force-pushed removing the unused variable random_mutable_transaction

  33. brunoerg force-pushed on May 31, 2023
  34. DrahtBot added the label CI failed on May 31, 2023
  35. DrahtBot removed the label CI failed on Jun 1, 2023
  36. Ayush170-Future approved
  37. brunoerg requested review from maflcko on Jun 7, 2023
  38. in src/wallet/test/fuzz/fees.cpp:26 in 4da8d6bf17 outdated
      21 | +void initialize_setup()
      22 | +{
      23 | +    static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
      24 | +    g_setup = testing_setup.get();
      25 | +    const auto& node{g_setup->m_node};
      26 | +    g_chainstate = &node.chainman->ActiveChainstate();
    


    maflcko commented at 4:39 AM on June 8, 2023:

    Any reason to make this a global when it can be just a reference local in the body of FUZZ_TARGET_INIT?


    brunoerg commented at 5:12 PM on June 13, 2023:

    No, going to change it.

  39. in src/wallet/test/fuzz/fees.cpp:8 in 4da8d6bf17 outdated
       0 | @@ -0,0 +1,68 @@
       1 | +// Copyright (c) 2022 The Bitcoin Core developers
       2 | +// Distributed under the MIT software license, see the accompanying
       3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4 | +
       5 | +#include <test/fuzz/FuzzedDataProvider.h>
       6 | +#include <test/fuzz/fuzz.h>
       7 | +#include <test/fuzz/util.h>
       8 | +#include <test/fuzz/util/mempool.h>
    


    maflcko commented at 4:39 AM on June 8, 2023:

    not needed?


    Ayush170-Future commented at 2:44 PM on June 11, 2023:

    Yes, I agree that fuzz/util/mempool.h is not required here. I think we need to include validation.h for the ActiveChainstate method.

    The reason this does not result in a compilation error is that fuzz/util/mempool.h includes the validation.h file.

  40. maflcko commented at 4:40 AM on June 8, 2023: member

    lgtm ACK 4da8d6bf17c0875c1a8f60ad2bb1bbd418d3a7cd

    left some nits, didn't test

  41. brunoerg force-pushed on Jun 13, 2023
  42. brunoerg commented at 6:59 PM on June 13, 2023: contributor

    Force-pushed addressing #27647 (review) and #27647 (review)

    CI error is unrelated.

  43. DrahtBot added the label CI failed on Jun 13, 2023
  44. fuzz: wallet, add target for `fees` 162602b208
  45. brunoerg force-pushed on Jun 14, 2023
  46. murchandamus commented at 2:54 PM on June 14, 2023: contributor

    ACK 162602b208493e7da1984044e661402c3e52932b

  47. DrahtBot removed review request from murchandamus on Jun 14, 2023
  48. DrahtBot requested review from maflcko on Jun 14, 2023
  49. DrahtBot removed the label CI failed on Jun 14, 2023
  50. maflcko commented at 7:07 AM on June 15, 2023: member

    lgtm ACK 162602b208493e7da1984044e661402c3e52932b

  51. DrahtBot removed review request from maflcko on Jun 15, 2023
  52. fanquake requested review from dergoegge on Jun 15, 2023
  53. dergoegge approved
  54. dergoegge commented at 10:39 AM on June 15, 2023: member

    Code review ACK 162602b208493e7da1984044e661402c3e52932b

  55. fanquake merged this on Jun 15, 2023
  56. fanquake closed this on Jun 15, 2023

  57. sidhujag referenced this in commit a8c5a3d659 on Jun 15, 2023
  58. maflcko commented at 12:57 PM on June 19, 2023: member

    When adding a fuzz target it makes sense to run it with the sanitizers enabled, so that bugs are caught and fixed either before or with adding the target.

    echo 'OiAAAPr//wAAAAAAAAA=' | base64  --decode > /tmp/a
    UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1" FUZZ=wallet_fees ./src/test/fuzz/fuzz  /tmp/a
    
    wallet/fees.cpp:58:58: runtime error: implicit conversion from type 'unsigned int' of value 4294574080 (32-bit, unsigned) to type 'int' changed the value to -393216 (32-bit, signed)
        [#0](/bitcoin-bitcoin/0/) 0x5625ef46a094 in wallet::GetMinimumFeeRate(wallet::CWallet const&, wallet::CCoinControl const&, FeeCalculation*) src/wallet/fees.cpp:58:58
        [#1](/bitcoin-bitcoin/1/) 0x5625eedd467f in wallet::(anonymous namespace)::wallet_fees_fuzz_target(Span<unsigned char const>) src/wallet/test/fuzz/fees.cpp:64:11
    ...
    
    SUMMARY: UndefinedBehaviorSanitizer: implicit-integer-sign-change wallet/fees.cpp:58:58 in 
    
  59. maflcko commented at 10:11 AM on June 20, 2023: member
  60. bitcoin locked this on Jun 19, 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-04-18 06:13 UTC

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