refactor: split CWallet::Create #20773

pull S3RK wants to merge 4 commits into bitcoin:master from S3RK:refactor_wallet_create changing 11 files +93 −68
  1. S3RK commented at 4:05 pm on December 26, 2020: member

    This is a followup for #20365 (review) First part of a refactoring with overall goal to simplify CWallet and de-duplicate code with wallettool

    Rationale: split CWallet::Create and create CWallet::AttachChain.

    CWallet::AttachChain takes chain as first parameter on purpose. In future I suggest we can remove chain from CWallet constructor.

    The second commit is based on be164f9cf89b123f03b926aa980996919924ee64 from #15719 (thanks ryanofsky)

    cc ryanofsky achow101

  2. DrahtBot added the label GUI on Dec 26, 2020
  3. DrahtBot added the label Refactoring on Dec 26, 2020
  4. DrahtBot added the label Wallet on Dec 26, 2020
  5. DrahtBot commented at 6:04 pm on December 26, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #20243 (rpc, wallet: Expose wallet id in getwalletinfo RPC output by hebasto)
    • #20205 (wallet: Properly support a wallet id by achow101)
    • #19101 (refactor: remove ::vpwallets and related global variables 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.

  6. S3RK force-pushed on Dec 29, 2020
  7. S3RK force-pushed on Jan 1, 2021
  8. S3RK force-pushed on Jan 8, 2021
  9. in src/wallet/wallet.h:724 in b528485dc7 outdated
    719@@ -720,6 +720,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
    720     // ScriptPubKeyMan::GetID. In many cases it will be the hash of an internal structure
    721     std::map<uint256, std::unique_ptr<ScriptPubKeyMan>> m_spk_managers;
    722 
    723+    /** True if this wallet is loaded for the first time. */
    724+    bool m_first_run;
    


    ryanofsky commented at 11:54 pm on January 11, 2021:

    In commit “refactor: Track first run state in CWallet” (b528485dc7701dcd8fafcf2da996160c52546f07)

    Could this be initialized with bool m_first_run = true or bool m_first_run = false here? It would be nice to avoid possibility of non-determinism since it is technically possible to create a CWallet object without loading it


    S3RK commented at 9:10 am on January 14, 2021:
    Thanks. I initialised it to false so it wouldn’t call chainStateFlushed by default. I think this is a safer option.
  10. in src/wallet/wallet.cpp:4053 in eabcd952d1 outdated
    3954@@ -3955,11 +3955,6 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
    3955                                _("This is the transaction fee you will pay if you send a transaction."));
    3956         }
    3957         walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
    3958-        if (walletInstance->m_pay_tx_fee < chain.relayMinFee()) {
    3959-            error = strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
    


    ryanofsky commented at 0:52 am on January 12, 2021:

    In commit “refactor: Move chain checks into CWallet::AttachChain” (eabcd952d1c664cd964eb3376bc2865a49b33f16)

    I think this commit is not an improvement and that it would be better to drop. Safer and clearer to check these values for errors early as they are initialized, than to initialize one place and do checking later.


    S3RK commented at 9:45 am on January 14, 2021:
    Addressed in a general comment below

    ryanofsky commented at 2:19 pm on May 18, 2021:

    In commit “refactor: Move chain checks into CWallet::AttachChain” (88f19822bd14d23e2e0f4ebc7e7221f029d0abad)

    I still think this commit is not an improvement and should be dropped because it makes the already complicated handling of these fee options and errors more complicated and less transparent. Instead of handling the settings in one place in Create, it splits up handling between two functions Create and AttachChain, when the code doesn’t actually have anything to do with attaching to the chain. It is true that this code currently depends on some chain methods. But this should be fixed in the future by making the chain object less monolithic and by improving options handling in general.

    I understand the motivation for this commit which is stated at #20773 (comment) and is basically to help implement the subsequent commit “wallet: make chain optional for CWallet::Create” (37b55f38d31c05f1ae6e22537eea8707463168a6).

    But I don’t think a good way to make the chain optional is to move these checks to an unrelated place where they don’t belong. The simplest and clearest way to make the chain optional for now is simply to skip these checks if the chain pointer is null. And in the future, these checks can be updated to not use the chain interface at all.


    S3RK commented at 6:54 pm on May 18, 2021:

    Hm… thanks for mentioning that the dependency on the chain is accidental. I didn’t look deep into it before.

    I’m much more happy to drop that commit now, but should we explore other options as well?

    It looks like we’re just comparing one arg against other args. paytxfee and maxtxfee against minRelayFee, plus minRelayFee against some constant. There should be a better place for those checks rather than wallet creation code. I’m not familiar with the codebase enough to say where it belongs. @ryanofsky and @fjahr could you help me locate a better place for those checks?


    ryanofsky commented at 9:59 pm on May 18, 2021:

    @ryanofsky and @fjahr could you help me locate a better place for those checks?

    I think the checks are in a good place. Good to validate settings in the same place where they are parsed. There are just other ways this code could access ::minRelayTxFee value other than by calling a chain method. It could passed as a create option or be part of a different struct or interface. Simply changing the check to if (chain && walletInstance->m_pay_tx_fee < chain->relayMinFee()) seems good for now though.


    fjahr commented at 10:03 pm on May 18, 2021:

    And in the future, these checks can be updated to not use the chain interface at all.

    For the chain topic this seems a good solution going forward.

    Otherwise, I am not sure why you are interested in moving this code. These are checks that can make the wallet creation process fail or at least print a warning. So this seems to be a good place for it. Fee options are part of the wallet “world”. Maybe the confusion is because these are config options and not args of createwallet? Or you don’t see the direct connection between fee options and the wallet? These options are only relevant to the wallet and thus are only checked once the user wants to create a wallet because only then weird numbers there can be dangerous for the user.

    On a smaller, organizational scale some of these checks could probably be helper functions and overall the fee part of wallet create can probably be encapsulated similar to AttachChain. If, however, you want to challenge that fee checks are done at all during wallet creation, that would require a lot more conceptual discussion. After that conceptual discussion, there could be a decision on where to put it. But the chances of a significant change in this regard are slim because this would be a huge behavior change I think, potentially breaking compatibility with any program that uses the wallet because these options will need to be set and checked in some kind of way.

    Just some first thoughts by someone also not very familiar with the codebase :) If you have ideas or want to challenge this in general as I teased above I would recommend you to bring that as a topic to one of the next wallet IRC meetings, this Friday for example.


    S3RK commented at 6:54 am on May 19, 2021:

    Maybe the confusion is because these are config options and not args of createwallet?

    yes, and

    and thus are only checked once the user wants to create a wallet.

    Shouldn’t we check them earlier? They are from the wallet “world” but have nothing to do with the wallet creation. Especially chain.relayMinFee().GetFeePerK() > HIGH_TX_FEE_PER_KB. It seems just a coincidence that the option parsing is done during wallet creation process.

    Nevermind, I’ve dropped the commit. Thanks for your time and patience.


    ryanofsky commented at 11:42 am on May 19, 2021:

    Shouldn’t we check them earlier? They are from the wallet “world” but have nothing to do with the wallet creation.

    Yes, these settings could be WalletContext members and shared across multiple CWallet instances instead of being CWallet members. (Or maybe if we want keep these as global settings but allow individual wallet overrides they could be members of both CWallet and WalletContext, with CWallet copying the WalletContext values on construction.) There’s a general issue about wallet setting in #13044 for background & discussion about these things. And #19101 is another PR moving more variables to WalletContext.

    My only reason for wanting to drop the commit is wanting to keep options parsing and option validation code together. I wouldn’t want to move the checks without also moving the IsArgSet/GetArg code they are a part of. But all of this code could run before wallet creation if settings were stored outside CWallet in a place like WalletContext.

    Especially chain.relayMinFee().GetFeePerK() > HIGH_TX_FEE_PER_KB. It seems just a coincidence that the option parsing is done during wallet creation process.

    I’m not sure. This is also a warning passed back to the loadwallet and createwallet RPCs. It seems like a reasonable thing to warn user about when they are loading a wallet, but maybe it could happen at another time. It’s more of a UX question than a code organization question, I think.

  11. in src/wallet/test/wallet_test_fixture.cpp:15 in 847588a4b8 outdated
    11 {
    12     m_wallet.LoadWallet();
    13-    CWallet::AttachChain({ &m_wallet, [](CWallet*) {} });
    14+    bilingual_str error;
    15+    std::vector<bilingual_str> warnings;
    16+    CWalle::AttachChain({&m_wallet, [](CWallet*) {}}, *m_node.chain.get(), error, warnings);
    


    ryanofsky commented at 0:54 am on January 12, 2021:

    In commit “refactor: Update CWallet::AttachChain signature” (847588a4b8e0dcc9975a675c46eca095c7cce4eb)

    Compile error this line (should say CWallet)


    S3RK commented at 9:12 am on January 14, 2021:
    🤦
  12. in src/wallet/wallet.h:795 in 847588a4b8 outdated
    790@@ -791,15 +791,12 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
    791     /** Registered interfaces::Chain::Notifications handler. */
    792     std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
    793 
    794-    /** Result of scanning a chain for new transactions */
    795-    enum class ScanStatus { SUCCESS, FAILED, MISSING_BLOCKS, SKIPPED };
    


    ryanofsky commented at 1:00 am on January 12, 2021:

    In commit “refactor: Update CWallet::AttachChain signature” (847588a4b8e0dcc9975a675c46eca095c7cce4eb)

    This change seems good but it would be nice to squash this commit into the first commit 4398236619aa1f70886a0e9ae7093b413ec172c6 to make the PR simpler and avoid defining an enum in an an earlier change that gets deleted later


    S3RK commented at 9:11 am on January 14, 2021:
    Squashed.
  13. in src/wallet/wallet.cpp:3983 in 9b575783f0 outdated
    3989@@ -3980,10 +3990,6 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
    3990 
    3991     LOCK(walletInstance->cs_wallet);
    3992 
    3993-    if (!walletInstance->AttachChain(chain, error, warnings)) {
    


    ryanofsky commented at 1:07 am on January 12, 2021:

    In commit “CWallet::Create move CWallet::AttachChain up into calling code” (9b575783f0f075d66350def60a842aee9ee05267)

    I don’t think it’s safe to reorder loading sequence and call AttachChain after g_load_wallet_fns callbacks. It also adds extra code to give callers responsibility to call AttachChain. If the point is to allow wallets to be created without syncing, would suggest just changing Create methods’s Chain& argument to Chain* and if (!walletInstance->AttachChain(chain)) to if (chain && !walletInstance->AttachChain(*chain))


    S3RK commented at 9:52 am on January 14, 2021:

    Thanks for bringing it up. I looked at g_load_wallet_fns and there are two use-cases for it both in UI:

    • register handler for show progress messages
    • create and connect wallet model for UI controller

    I have almost zero understanding of GUI code and QT, but it looks like a safe change to me. Can we ask someone with better understanding of GUI to comment on that?

    Also see my general comment below regarding the responsibility to call AttachChain.

  14. in src/wallet/wallet.cpp:4071 in b20c1e516d outdated
    4033@@ -4036,6 +4034,10 @@ CWallet::ScanStatus CWallet::AttachChain(std::shared_ptr<CWallet> wallet, bool s
    4034     auto& walletInstance = wallet;
    4035     LOCK(walletInstance->cs_wallet);
    4036 
    4037+    if (walletInstance->m_first_run) {
    


    ryanofsky commented at 2:50 pm on January 12, 2021:

    In commit “refactor: Handle first run in CWallet::AttachChain” (b20c1e516d5bfc87e8d75a34799a91d71475ee8b)

    I think it’s good that this PR adds m_first_run, to be able to freely move code around without changing behavior. In the future, though I think it would be good to eliminate m_first_run. This chainStateFlushed call seems to be the main place it is used, and it would seem better if this code updated the best block in a smarter way less awkwardly tied to the presence of keys and descriptors.


    S3RK commented at 9:14 am on January 14, 2021:
    Totally! I started to look at how the best block record is used and maintained but it sent me down the rabbit hole as I’m not familiar with the codebase yet. I’ll look at it as a follow up.
  15. ryanofsky approved
  16. ryanofsky commented at 3:03 pm on January 12, 2021: member

    Nice PR! This helps more clearly separate CWallet creation from syncing so duplicate wallet tool code can be removed without changing behavior. Would suggest a few changes, though:

    • It would be good to squash 5th commit into 1st commit. No need to add a method and enum in one commit and then change the signature and drop the enum right after.

    • I think 4th and 7th commits should probably be dropped, but see specific comments below.

    Reviewed:

    • 4398236619aa1f70886a0e9ae7093b413ec172c6 refactor: Add CWallet:::AttachChain method (1/7)
    • b528485dc7701dcd8fafcf2da996160c52546f07 refactor: Track first run state in CWallet (2/7)
    • b20c1e516d5bfc87e8d75a34799a91d71475ee8b refactor: Handle first run in CWallet::AttachChain (3/7)
    • eabcd952d1c664cd964eb3376bc2865a49b33f16 refactor: Move chain checks into CWallet::AttachChain (4/7)
    • 847588a4b8e0dcc9975a675c46eca095c7cce4eb refactor: Update CWallet::AttachChain signature (5/7)
    • 190317a57a9b97228fccb5e649c782a26f74f991 CWallet::Create move chain init message up into calling code (6/7)
    • 9b575783f0f075d66350def60a842aee9ee05267 CWallet::Create move CWallet::AttachChain up into calling code (7/7)
  17. S3RK force-pushed on Jan 14, 2021
  18. S3RK commented at 9:52 am on January 14, 2021: member

    @ryanofsky greatly appreciate your review.

    • It would be good to squash 5th commit into 1st commit. No need to add a method and enum in one commit and then change the signature and drop the enum right after.

    I squashed handling first run and changing signature commits into adding AttachChain. There are less changing back and forth and overall it looks a bit simpler now.

    • I think 4th and 7th commits should probably be dropped, but see specific comments below.

    I believe it’s good to discuss a bit the design and future of CWallet before moving forward. I made those changes based on the following assumptions: A) a wallet instance is not dependent on a chain object and could exist without a chain B) a wallet instance could be later attached to a chain if required Those assumption are based on my observation of the wallet tool which operates completely “offline”. In my opinion this provides nice and clear wallet lifecycle and delineation of concerns. Thus we would remove chain argument from CWallet ctr and Create methods in the future. It would require changing how tx are loaded, but I think it’s doable and would simplify the code.

    I’d like to know what do you think.

  19. ryanofsky approved
  20. ryanofsky commented at 0:52 am on January 15, 2021: member

    Code review ACK a481665d5d90aa43f34434396eea174de5277df3. Overall is this is a very good change, but I’d still encourage dropping the 3th and 5th commits (or moving them to another PR where they can be better motivated). I think they complicate things unnecessarily and are based on an incorrect assumption.

    • 6f8b907b6590e596576d271662a827dfeff538e3 refactor: Track first run state in CWallet (1/5)
    • 2a9fbc4be04577e11640f380240f3cf99e3c9881 refactor: Add CWallet:::AttachChain method (2/5)
    • a888db2f5ef6b9ea7150d2908001cd9c36d467d8 refactor: Move chain checks into CWallet::AttachChain (3/5)
    • 2b77279058e11e7a5ad714255d2a9d3e15f42d55 CWallet::Create move chain init message up into calling code (4/5)
    • a481665d5d90aa43f34434396eea174de5277df3 CWallet::Create move CWallet::AttachChain up into calling code (5/5)

    I believe it’s good to discuss a bit the design and future of CWallet before moving forward. I made those changes based on the following assumptions: A) a wallet instance is not dependent on a chain object and could exist without a chain B) a wallet instance could be later attached to a chain if required Those assumption are based on my observation of the wallet tool which operates completely “offline”. In my opinion this provides nice and clear wallet lifecycle and delineation of concerns. Thus we would remove chain argument from CWallet ctr and Create methods in the future. It would require changing how tx are loaded, but I think it’s doable and would simplify the code.

    I’d like to know what do you think.

    This is well put. Basically I just think the second assumption is wrong and complicates things unnecessarily. The only change we need to make to remove duplicate code in wallet tool is to tweak the wallet create function to take a null chain and stop requiring a non-null chain. There’s no need to allow a different chain to be swapped in after the wallet is created.

    Trying to separate creation from loading seems like all cost and no benefit. Callers have to do more work, settings parsing gets separated from settings validation, and the benefit is __? Maybe there could be a benefit in a future PR, but the 3rd and 5th commits seem a bit messy and out of place here to me. I don’t think they cause much harm though, so if you really think they improve things then please do keep them! Thanks for working on this!

    EDIT: Fixed references to 3rd and 5th commits instead of 4th and 5th commits

  21. achow101 commented at 8:31 pm on January 15, 2021: member

    ISTM there are 5 main components of CWallet::Create: Loading/creating the wallet file, initializing new wallets, processing all of the wallet CLI args, setting up the chain, and setting up wallet client interfaces.

    What I had envisioned for splitting up CWallet::Create was to have separate functions for each of those components but still having CWallet::Create call all of them. This would allow for the typical use case to have a convenience function that does everything. But for the wallet tool where we don’t need to do some of those parts, we could just call the underlying functions directly. For example, for bitcoin-wallet create, we would just call the load/create wallet file function, and then the setup new wallet function.

    I agree with @ryanofsky that the assumption that changing the attached chain later is incorrect. I don’t think it makes sense that the wallet would want to be able to change to a new chain after being initialized. However it does make sense that we would want to create a wallet with no chain for the offline mode stuff.

  22. S3RK force-pushed on Jan 18, 2021
  23. S3RK commented at 8:51 am on January 18, 2021: member

    Thanks ryanofsky and achow101, I have a better understanding now and adjusted my PR accordingly:

    1. I removed the last commit and restored AttachChain call within CWallet::Create
    2. I made chain arg of CWallet::Create optional to accommodate wallet tool use-case

    I kept the 3rd commit which moves chain related checks to AttachChain. This way we can have all chain related code in AttachChain method in alignment with what achow101 said. As a benefit we need to check if chain is passed into CWallet::Create only once.

    Thanks for your reviews, I’m more satisfied with how this PR looks right now.

  24. ryanofsky approved
  25. ryanofsky commented at 6:27 pm on January 20, 2021: member

    Code review ACK f71618c2a18afea6dfe52dfd5282f82f05b93686. Looks very good and thanks again! I still think commit 3 “refactor: Move chain checks into CWallet::AttachChain” (fcf56ee612ae2ff5422d9a2bd801e6af22f4ab2c) is a little worse for code organization and error handling and would be better to drop, but it seems fine to keep if you just prefer it or think having fewer null chain checks justifies it.

    Changes since last review: tweaking AttachChain call in second commit to fix interim compile error, replacing last commit with new commit making CWallet::Create chain argument optional & adding test case for this invocation.

    Would encourage other reviewers to review this. No behavior is changing here, just a big clumsy Create function being broken up and made more sane.

  26. ryanofsky referenced this in commit bdcb2a2325 on Feb 17, 2021
  27. ryanofsky referenced this in commit 2a855bf092 on Feb 17, 2021
  28. ryanofsky referenced this in commit 11dd250fb1 on Mar 4, 2021
  29. ryanofsky referenced this in commit 01f4bb2693 on Mar 4, 2021
  30. DrahtBot added the label Needs rebase on Mar 4, 2021
  31. S3RK force-pushed on Mar 8, 2021
  32. DrahtBot removed the label Needs rebase on Mar 8, 2021
  33. ryanofsky referenced this in commit 74d45298e5 on Mar 8, 2021
  34. ryanofsky referenced this in commit 065e94fd97 on Mar 8, 2021
  35. ryanofsky referenced this in commit 68e865e6b7 on Mar 10, 2021
  36. ryanofsky referenced this in commit 8da243cc98 on Mar 10, 2021
  37. DrahtBot added the label Needs rebase on Mar 12, 2021
  38. ryanofsky referenced this in commit e7eebeb6d1 on Mar 21, 2021
  39. ryanofsky referenced this in commit 7c188f3508 on Mar 21, 2021
  40. S3RK force-pushed on Mar 22, 2021
  41. DrahtBot removed the label Needs rebase on Mar 22, 2021
  42. ryanofsky approved
  43. ryanofsky commented at 5:21 pm on March 31, 2021: member
    Code review ACK c112b1ba9f2360626e73e36b03370456f1e65484. Only change since last review was simple rebase working around removal of Optional and MakeUnique
  44. fanquake commented at 9:18 am on April 1, 2021: member
    @meshcollider did you want to have a look here?
  45. MarcoFalke requested review from achow101 on Apr 1, 2021
  46. MarcoFalke removed the label GUI on Apr 1, 2021
  47. meshcollider commented at 4:54 am on April 6, 2021: contributor

    Yep I will review this sometime in the next couple of days 👍

    Concept ACK

  48. ryanofsky referenced this in commit a71a7db9a9 on Apr 13, 2021
  49. ryanofsky referenced this in commit 9b6bdfb08a on Apr 13, 2021
  50. DrahtBot added the label Needs rebase on Apr 13, 2021
  51. S3RK force-pushed on Apr 14, 2021
  52. S3RK commented at 7:53 am on April 14, 2021: member
    Rebased to fix conflict in src/wallet/test/wallet_tests.cpp
  53. DrahtBot removed the label Needs rebase on Apr 14, 2021
  54. ryanofsky approved
  55. ryanofsky commented at 6:52 pm on April 23, 2021: member
    Code review ACK 84980ae4deb828f0675478fad2f7e0b6dec69520. No changes since last review except rebase around test conflicts
  56. meshcollider commented at 5:18 am on April 30, 2021: contributor

    Code review ACK 84980ae4deb828f0675478fad2f7e0b6dec69520

    Overall very nice change set, thanks S3RK

  57. ryanofsky referenced this in commit c9e8a508e5 on Apr 30, 2021
  58. ryanofsky referenced this in commit 4984c4548c on Apr 30, 2021
  59. laanwj added this to the "Blockers" column in a project

  60. in src/wallet/wallet.cpp:3880 in 84980ae4de outdated
    3876@@ -3879,7 +3877,7 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
    3877         }
    3878     }
    3879 
    3880-    if (fFirstRun)
    3881+    if (walletInstance->m_first_run)
    


    kiminuo commented at 1:55 pm on May 9, 2021:

    Question: How does one distinguish whether accessing a wallet field (in this case walletInstance->m_first_run) requires cs_wallet lock or not? I’m asking because cs_wallet is described like this:

    0/*
    1 * Main wallet lock.
    2 * This lock protects all the fields added by CWallet.  */
    3mutable RecursiveMutex cs_wallet;
    

    Can anybody shed light on this for my information?


    ryanofsky commented at 12:54 pm on May 10, 2021:

    Question: How does one distinguish whether accessing a wallet field (in this case walletInstance->m_first_run) requires cs_wallet lock or not?

    People may have different ideas about what “requires” means, but I would say a variable is required to be locked if not locking it would cause bugs. I.e. if it can be read and written at the same time from multiple threads. So a lock is not required for this because parts of wallet loading code which use this are single threaded.

    But I do think it would be a small improvement to write bool m_first_run GUARDED_BY(cs_wallet) = false; and lock during init to avoid questions and doubts about races during loading.


    S3RK commented at 6:48 pm on May 10, 2021:
    Thanks for the review. Since I need to rebase the branch anyway I’m going to incorporate the suggestion

    kiminuo commented at 8:24 pm on May 10, 2021:

    People may have different ideas about what “requires” means, but I would say a variable is required to be locked if not locking it would cause bugs. I.e. if it can be read and written at the same time from multiple threads. So a lock is not required for this because parts of wallet loading code which use this are single threaded.

    Thank you for the explanation.

    But I do think it would be a small improvement to write bool m_first_run GUARDED_BY(cs_wallet) = false; and lock during init to avoid questions and doubts about races during loading.

    This makes a lot of sense to me because it’s much easier to follow comments specifying a class design rather than read all code that uses given class (possibly many many places).


    S3RK commented at 1:02 pm on May 15, 2021:
    I’ve dropped m_first_run altogether
  61. DrahtBot added the label Needs rebase on May 10, 2021
  62. achow101 commented at 8:09 pm on May 10, 2021: member

    While I think this is a good improvement, there are a few changes I would like to see before ACK’ing this.

    1. We don’t need m_first_run. The only reason it needs to be in AttachChain is to allow for new wallets to be initialized with a best block that is the current tip to avoid unnecessary rescanning. But because we shouldn’t be using AttachChain outside of Create, I think it would be better to have first_run parameter on AttachChain rather than CWallet::m_first_run.

      Even so, I’m not sure if it really makes sense to have AttachChain be setting that for newly created wallets. Instead, because Create has a chain optionally, we can just check if the chain exists and do the walletInstance->chainStateFlushed if it does.

    2. AttachChain should be a private function since I don’t think we want to have callers to be able to change the chain after the wallet has been created.

    Also, needs rebase.

  63. ryanofsky commented at 8:36 pm on May 10, 2021: member

    Agree with Achow’s suggestions. But if the suggestions turn out to be hard to implement, I hope we consider them optional. The big improvement I see in this PR is that that it unscrambles wallet loading code from wallet syncing code. This is important so syncing code can be moved to a higher-level, so wallet code can be less monolithic and so multiple wallets can be synced in a single scan during loading.

    If having a first run member or a having public attach method helps keep this PR a plain refactor instead of introducing new logic, or helps keep this PR small, while still separating loading from syncing, this doesn’t seem like a bad tradeoff. But it’s pretty likely no tradeoff is necessary, so this does seem worth looking into.

  64. S3RK force-pushed on May 15, 2021
  65. S3RK commented at 1:00 pm on May 15, 2021: member
    1. Reworked the first commit. It doesn’t introduce m_frist_run, but rather just moves the first run detection closer to the point of use
    2. Updated AttachChain signature. Made it private and changed the first param to const reference
    3. Added a check that chain can’t be changed.
  66. DrahtBot removed the label Needs rebase on May 15, 2021
  67. in src/wallet/wallet.cpp:3924 in efd5396b89 outdated
    3919@@ -3924,6 +3920,10 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
    3920         }
    3921     }
    3922 
    3923+    // This wallet is in its first run if there are no ScriptPubKeyMans and it isn't blank or no privkeys
    3924+    bool fFirstRun = walletInstance->m_spk_managers.empty() &&
    


    fjahr commented at 6:06 pm on May 16, 2021:
    nit: could be const

    S3RK commented at 6:50 am on May 19, 2021:
    done
  68. fjahr commented at 7:50 pm on May 16, 2021: member

    Code review ACK 37b55f38d31c05f1ae6e22537eea8707463168a6

    I agree with @ryanofsky that the 3rd commit should be dropped but it’s not a blocker.

  69. achow101 commented at 7:10 pm on May 17, 2021: member
    Code Review ACK 37b55f38d31c05f1ae6e22537eea8707463168a6
  70. in src/wallet/wallet.cpp:4120 in 491feef0e9 outdated
    4113+
    4114+bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interfaces::Chain& chain, bilingual_str& error, std::vector<bilingual_str>& warnings)
    4115+{
    4116+    LOCK(walletInstance->cs_wallet);
    4117+    // allow setting the chain if it hasn't been set already but prevent changing it
    4118+    assert(!walletInstance->m_chain || walletInstance->m_chain == &chain);
    


    ryanofsky commented at 1:55 pm on May 18, 2021:

    In commit “refactor: Add CWallet:::AttachChain method” (491feef0e97adf4ec5ca1b17a0c097a4f7324a9e)

    Not very important but I wonder if you could drop the || walletInstance->m_chain == &chain condition. It seems like it would be a bug to call attachchain more than once, even with the same chain pointer.


    S3RK commented at 6:00 pm on May 18, 2021:
    m_chain is initialized in the CWallet ctr. I’ve tried to drop it originally, but there were some issues, so I decided to leave it for the follow up.
  71. ryanofsky approved
  72. ryanofsky commented at 2:31 pm on May 18, 2021: member

    Code review ACK 37b55f38d31c05f1ae6e22537eea8707463168a6. I still would really like to drop the third commit “refactor: Move chain checks into CWallet::AttachChain” 88f19822bd14d23e2e0f4ebc7e7221f029d0abad (see reasoning and suggested alternative below), but I think this PR is great overall and all the other parts are a lot more significant than the one questionable commit.

    Changes since last review: rebase due to conflicts, cleverly dropping m_first_run while still dropping first_run output arguments, adding assert to make sure AttachChain is called correctly, tweaking AttachChain signature

  73. refactor: move first run detection to client code e2a47ce085
  74. refactor: Add CWallet:::AttachChain method
    This commit does not change behavior, it just moves code from
    CWallet::CreateWalletFromFile to CWallet:::AttachChain so it can be updated in
    the next commit.
    
    This commit is most easily reviewed with
    "git diff -w --color-moved=dimmed_zebra" or by diffing CWallet:::AttachChain
    against the previous code with an external diff tool.
    44c430ffac
  75. CWallet::Create move chain init message up into calling code d73ae93964
  76. wallet: make chain optional for CWallet::Create 489ebb7b34
  77. S3RK force-pushed on May 19, 2021
  78. S3RK commented at 6:54 am on May 19, 2021: member
    I’ve dropped “refactor: Move chain checks into CWallet::AttachChain” (88f1982)
  79. ryanofsky approved
  80. ryanofsky commented at 11:47 am on May 19, 2021: member
    Code review ACK 489ebb7b34c403a3ce78ff6fb271f8e6ecb47304. Only changes since last review were adding a const variable declaration, and implementing suggestion not to move feerate option checks to AttachChain. Thanks for updates and fast responses!
  81. laanwj merged this on May 19, 2021
  82. laanwj closed this on May 19, 2021

  83. laanwj removed this from the "Blockers" column in a project

  84. sidhujag referenced this in commit e3bbf04df9 on May 19, 2021
  85. ryanofsky referenced this in commit 264b945fa7 on May 20, 2021
  86. ryanofsky referenced this in commit 0415a53c9c on May 20, 2021
  87. ryanofsky referenced this in commit ed565f93e2 on May 25, 2021
  88. ryanofsky referenced this in commit ae93b95a68 on May 25, 2021
  89. ryanofsky referenced this in commit c7bd5842e4 on May 26, 2021
  90. ryanofsky referenced this in commit 9ece3479c9 on May 26, 2021
  91. meshcollider referenced this in commit 55a156fca0 on May 30, 2021
  92. rebroad referenced this in commit 7c229d7a74 on Jun 23, 2021
  93. gwillen referenced this in commit e552043a9a on Jun 1, 2022
  94. 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-22 06:12 UTC

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