[Wallet] get rid of pwalletMain, add simple CWallets infrastructure #8764

pull jonasschnelli wants to merge 1 commits into bitcoin:master from jonasschnelli:2016/09/wallet_pointer changing 10 files +307 −279
  1. jonasschnelli commented at 12:57 PM on September 20, 2016: contributor

    This is a very simple pull requests that removes the global pwalletMain pointer and replaces the calls with CWallets::defaultWallet().

    This PR leverages the internal global single wallet container to a vector. We have similar functionality on the Qt layer.

    This will allow a step-by-step transition towards multiwallet support like proposed by @luke-jr in #8694.

  2. jonasschnelli added the label Refactoring on Sep 20, 2016
  3. jonasschnelli added the label Wallet on Sep 20, 2016
  4. in src/init.cpp:None in e112026247 outdated
     229 | @@ -231,8 +230,8 @@ void Shutdown()
     230 |          pblocktree = NULL;
     231 |      }
     232 |  #ifdef ENABLE_WALLET
     233 | -    if (pwalletMain)
     234 | -        pwalletMain->Flush(true);
     235 | +    if (CWallets::defaultWallet())
    


    laanwj commented at 1:41 PM on September 20, 2016:

    CWallets::FlushAllWallets() ?


    jonasschnelli commented at 2:24 PM on September 20, 2016:

    Yes. Changed it to flushAllWallets(bool shutdown);

  5. in src/wallet/wallet.h:None in e112026247 outdated
     998 | +    }
     999 | +    static void addWallet(CWallet *newWallet)
    1000 | +    {
    1001 | +        LOCK(cs_wallets);
    1002 | +        wallets.push_back(newWallet);
    1003 | +        int i = 0;
    


    paveljanik commented at 1:50 PM on September 20, 2016:

    i looks unused here 8)


    jonasschnelli commented at 1:54 PM on September 20, 2016:

    That's black magic! :-)

    Will remove it, had it there for debug purposes.

  6. jonasschnelli force-pushed on Sep 20, 2016
  7. in src/init.cpp:None in aa6dce676f outdated
    1530 | @@ -1535,9 +1531,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
    1531 |      uiInterface.InitMessage(_("Done loading"));
    1532 |  
    1533 |  #ifdef ENABLE_WALLET
    1534 | -    if (pwalletMain) {
    1535 | +    if (CWallets::defaultWallet()) {
    1536 |          // Run a thread to flush wallet periodically
    1537 | -        threadGroup.create_thread(boost::bind(&ThreadFlushWalletDB, boost::ref(pwalletMain->strWalletFile)));
    1538 | +        threadGroup.create_thread(boost::bind(&ThreadFlushWalletDB, boost::ref(CWallets::defaultWallet()->strWalletFile)));
    


    jonasschnelli commented at 2:28 PM on September 20, 2016:

    We keep the ThreadFlushWalletDB with "capacity" for a single wallet file for now.


    MarcoFalke commented at 5:23 PM on September 20, 2016:

    Why?


    jonasschnelli commented at 6:46 PM on September 20, 2016:
    1. Scope creeping
    2. Moving target (ThreadFlushWalletDB is currently getting touched in different PRs).

    MarcoFalke commented at 6:50 PM on September 20, 2016:

    Fine, can be done later. Though, I think in case it is just a simple for loop, it should be part of this pull.


    jonasschnelli commented at 6:54 PM on September 20, 2016:

    I think we don't want a flush wallet thread per wallet. IMO it should be a single flush thread that loops though the wallet, check last-flush-time and optionally flushes.


    MarcoFalke commented at 6:58 PM on September 20, 2016:

    Jup, @luke-jr did this. (Create a loop in ThreadFlushWalletDB over all wallets)

  8. in src/wallet/wallet.h:None in aa6dce676f outdated
     983 | @@ -986,4 +984,36 @@ class CAccount
     984 |      }
     985 |  };
     986 |  
     987 | +static CCriticalSection cs_wallets;
     988 | +class CWallets
     989 | +{
     990 | +public:
     991 | +    static std::vector<CWallet *> wallets;
    


    MarcoFalke commented at 3:11 PM on September 20, 2016:

    Why public?

     class CWallets
     {
    -public:
    +private:
         static std::vector<CWallet *> wallets;
    +public:
         static CWallet* defaultWallet()
         {
    

    compiles for me


    jonasschnelli commented at 6:45 PM on September 20, 2016:

    Yes. I had a Namespace at the beginning and though that there is no big need for a Class or even private static class variables. But yes, why not private. Will update.

  9. in src/qt/signverifymessagedialog.cpp:None in aa6dce676f outdated
     141 | @@ -142,7 +142,7 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
     142 |      }
     143 |  
     144 |      CKey key;
     145 | -    if (!pwalletMain->GetKey(keyID, key))
     146 | +    if (!CWallets::defaultWallet()->GetKey(keyID, key))
    


    paveljanik commented at 5:13 PM on September 20, 2016:

    There is no if (pwalletMain) test here in the original code?


    MarcoFalke commented at 5:15 PM on September 20, 2016:

    @paveljanik The dialog does not exist when there is no wallet.

  10. in src/wallet/rpcdump.cpp:None in aa6dce676f outdated
     269 | @@ -270,7 +270,7 @@ UniValue importprunedfunds(const UniValue& params, bool fHelp)
     270 |      if (!DecodeHexTx(tx, params[0].get_str()))
     271 |          throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
     272 |      uint256 hashTx = tx.GetHash();
     273 | -    CWalletTx wtx(pwalletMain,tx);
     274 | +    CWalletTx wtx(CWallets::defaultWallet(),tx);
    


    paveljanik commented at 5:24 PM on September 20, 2016:

    SPC after , :-)


    MarcoFalke commented at 5:41 PM on September 20, 2016:

    Or just git diff HEAD~ -U0|cfd.

    alias cfd='~/workspace/bitcoin/contrib/devtools/clang-format-diff.py -p1 -i -v'
    
  11. paveljanik commented at 5:36 PM on September 20, 2016: contributor

    I'm all for supporting multiple wallets, so concept ACK.

    But isn't this only the part of @luke-jr 's work (64bcde7c5535cda12fc20ded718558ca74b28191)? Can you please coordinate the work to save your time? ;-)

  12. jonasschnelli commented at 6:43 PM on September 20, 2016: contributor

    I really like @luke-jr #8694, though, my experience with lager pull requests tells me, that #8694 will have a hard time getting in as a whole. I try to slice out independent chunks, improve them and PR them separately. This is how I get you all reviewing parts of #8694.

  13. jonasschnelli force-pushed on Sep 20, 2016
  14. jonasschnelli commented at 6:48 PM on September 20, 2016: contributor

    Fixed nits.

  15. in src/wallet/wallet.h:None in 9cc96b59f4 outdated
    1009 | +            pWallet->Flush(shutdown);
    1010 | +    }
    1011 | +    static void destruct()
    1012 | +    {
    1013 | +        LOCK(cs_wallets);
    1014 | +        for(CWallet *pWallet : wallets)
    


    MarcoFalke commented at 8:15 PM on September 20, 2016:

    μNit

    diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
    index 656d344..d690e48 100644
    --- a/src/wallet/wallet.cpp
    +++ b/src/wallet/wallet.cpp
    @@ -61 +61 @@ const uint256 CMerkleTx::ABANDON_HASH(uint256S("00000000000000000000000000000000
    -std::vector<CWallet *> CWallets::wallets = {};
    +std::vector<CWallet*> CWallets::wallets = {};
    diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
    index 390210d..4fb69dd 100644
    --- a/src/wallet/wallet.h
    +++ b/src/wallet/wallet.h
    @@ -991 +991,2 @@ private:
    -    static std::vector<CWallet *> wallets;
    +    static std::vector<CWallet*> wallets;
    +
    @@ -1000 +1001 @@ public:
    -    static void addWallet(CWallet *newWallet)
    +    static void addWallet(CWallet* newWallet)
    @@ -1008 +1009 @@ public:
    -        for(CWallet *pWallet : wallets)
    +        for (CWallet* pWallet : wallets)
    @@ -1014 +1015 @@ public:
    -        for(CWallet *pWallet : wallets)
    +        for (CWallet* pWallet : wallets)
    
  16. MarcoFalke approved
  17. MarcoFalke commented at 8:21 PM on September 20, 2016: member

    utACK 9cc96b59f4bcf708ae370679ef41c6f8f2af3a14

  18. in src/wallet/wallet.h:None in 9cc96b59f4 outdated
    1004 | +    }
    1005 | +    static void flushAllWallets(bool shutdown)
    1006 | +    {
    1007 | +        LOCK(cs_wallets);
    1008 | +        for(CWallet *pWallet : wallets)
    1009 | +            pWallet->Flush(shutdown);
    


    luke-jr commented at 8:48 PM on September 20, 2016:

    Does cs_wallets need to be held for access to (flushing) the wallet itself, or only for the wallets vector? If the former, then defaultWallet will fail to guard it; if the latter, the lock here is unnecessarily long-held.


    jonasschnelli commented at 9:09 AM on September 21, 2016:

    I decided to hold the lock because it would be possible that during iteration a addWallet (or later removeWallet) happens. But right,.. it results in holding the lock for probably a long time.

    I first though doing a copy of the wallets vector would be good, but the CWallet instance where the copy points to might be gone.

    Maybe we keep this for now because the flushAllWallets happens only on shutdown. Or what would be a better solution?

  19. luke-jr commented at 8:50 PM on September 20, 2016: member

    #8694 is already step-by-step, so this mostly just forces rebasing it... IMO better to do refactoring after #8694 gets merged. :)

  20. jonasschnelli force-pushed on Oct 19, 2016
  21. jonasschnelli force-pushed on Oct 19, 2016
  22. jonasschnelli commented at 3:32 PM on October 19, 2016: contributor

    Rebased and fixed code formatting (@MarcoFalkes micro-nit). The diff is relatively large, but it would finally eliminate the global pwalletMain in favor of a more flexible handler that would be "ready" for multiwallet.

  23. jonasschnelli force-pushed on Oct 21, 2016
  24. in src/wallet/wallet.h:None in 48b2338ea8 outdated
     991 | +    static std::vector<CWallet*> wallets;
     992 | +public:
     993 | +    static CWallet* defaultWallet()
     994 | +    {
     995 | +        LOCK(cs_wallets);
     996 | +        if (!wallets.size())
    


    dexX7 commented at 4:58 PM on October 22, 2016:

    Nit: I'd prefer if (wallets.empty()).

  25. MarcoFalke commented at 9:34 PM on November 2, 2016: member

    Needs rebase

  26. jonasschnelli force-pushed on Nov 4, 2016
  27. jonasschnelli force-pushed on Nov 4, 2016
  28. jonasschnelli force-pushed on Nov 4, 2016
  29. jonasschnelli force-pushed on Nov 4, 2016
  30. [Wallet] get rid of pwalletMain, add simple CWallets infrastructure c8b5b746ac
  31. jonasschnelli force-pushed on Nov 4, 2016
  32. jonasschnelli commented at 9:43 AM on November 4, 2016: contributor

    Rebased (now also covers importmulti).

  33. ryanofsky commented at 6:48 PM on February 27, 2017: member

    Seems like currently the first multiwallet commit is #8775, then #8694 comes next, and maybe this will be rebased on top of those at a later point?

  34. jonasschnelli commented at 3:48 PM on March 3, 2017: contributor

    Closing. Since #8775, this is to far away from being useful. Maybe a different similar approach can be worked out.

  35. jonasschnelli closed this on Mar 3, 2017

  36. fanquake removed this from the "In progress" column in a project

  37. MarcoFalke locked this on Sep 8, 2021

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-21 15:15 UTC

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