rpc: raw pointers should be replaced with shared_ptr #18590

issue brakmic openend this issue on April 11, 2020
  1. brakmic commented at 0:43 am on April 11, 2020: contributor

    While working on this PR #18570 I ran into a bit confusing situation where in one function a CWallet reference was expected while two others dealt with CWallet pointers. My initial reaction was: let's harmonize them by making them references. However, not being part of the PR itself I did’t change anything and completed the PR first.

    Now, after having analyzed the rpcwallet.cpp code I came to the conclusion that:

    • converting them all into references is not the solution as they’re originally shared_ptrs
    • but also: there is no (visible) reason why every RPC function should begin like this:
    0std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
    1const CWallet* const pwallet = wallet.get();
    

    This clearly defeats the purpose of shared pointers for no obvious reason. Maybe I am missing something?

    In my (unpublished) code changes all raw pointers got replaced with shared_ptrs but kept their original names (that is, pwallet). In the end, they’re pointers too, so the most of the original code remained untouched.

    However, some of the helper functions couldn’t be changed because external code is accessing them and I didn’t want to make this task “virulent”. So I introduced new (“Ref”) variants that expect shared_ptr parameters, namely:

    0void EnsureWalletRefIsUnlocked(const std::shared_ptr<CWallet>);
    1bool EnsureWalletRefIsAvailable(const std::shared_ptr<CWallet>, bool avoidException);
    

    I have tested the code, with python and boost-tests, and could not find any errors. But before opening a PR I would like to open a discussion about this.

    Is there any reason to keep on working with raw pointers when every function call actually begins with shared_ptrs?

    Regards,

  2. brakmic added the label Bug on Apr 11, 2020
  3. fanquake added the label RPC/REST/ZMQ on Apr 11, 2020
  4. promag commented at 0:59 am on April 11, 2020: member

    See this thread #13063 (review). So feel free to submit that change.

    In your commit you have:

    0WalletRescanReserver reserver(pwallet.get());
    

    IMO cases like this should be changed too - in this case to *pwallet - but it can be in another PR.

  5. brakmic commented at 8:48 am on April 11, 2020: contributor

    See this thread #13063 (comment). So feel free to submit that change.

    Thanks.

    Ok, will prepare it now.

    In your commit you have:

    0WalletRescanReserver reserver(pwallet.get());
    

    Yes, and I tried to change it first, but then later realized that it affects various other code that’s outside of rcpwallet.cpp.

    IMO cases like this should be changed too - in this case to *pwallet - but it can be in another PR.

    Yes, maybe a PR dedicated to WalletRescanReserver only.

  6. promag commented at 10:35 am on April 11, 2020: member
    This is not a bug.
  7. brakmic commented at 10:36 am on April 11, 2020: contributor

    This is not a bug.

    True, but I found no better option for the issue. I’ll gladly change it to something else.

  8. MarcoFalke removed the label Bug on Apr 11, 2020
  9. MarcoFalke added the label Brainstorming on Apr 11, 2020
  10. promag commented at 1:09 am on May 6, 2020: member

    IMO cases like this should be changed too - in this case to *pwallet - but it can be in another PR.

    Done in #18601.

  11. MarcoFalke closed this on Mar 10, 2021

  12. DrahtBot locked this on Aug 18, 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: 2024-12-18 12:12 UTC

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