context.h raw

   1  // Copyright (c) 2020-present 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  #ifndef BITCOIN_WALLET_CONTEXT_H
   6  #define BITCOIN_WALLET_CONTEXT_H
   7  
   8  #include <sync.h>
   9  
  10  #include <functional>
  11  #include <list>
  12  #include <memory>
  13  #include <vector>
  14  
  15  class ArgsManager;
  16  class CScheduler;
  17  namespace interfaces {
  18  class Chain;
  19  class Wallet;
  20  } // namespace interfaces
  21  
  22  namespace wallet {
  23  class CWallet;
  24  using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;
  25  
  26  //! WalletContext struct containing references to state shared between CWallet
  27  //! instances, like the reference to the chain interface, and the list of opened
  28  //! wallets.
  29  //!
  30  //! Future shared state can be added here as an alternative to adding global
  31  //! variables.
  32  //!
  33  //! The struct isn't intended to have any member functions. It should just be a
  34  //! collection of state pointers that doesn't pull in dependencies or implement
  35  //! behavior.
  36  struct WalletContext {
  37      interfaces::Chain* chain{nullptr};
  38      CScheduler* scheduler{nullptr};
  39      ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
  40      // It is unsafe to lock this after locking a CWallet::cs_wallet mutex because
  41      // this could introduce inconsistent lock ordering and cause deadlocks.
  42      Mutex wallets_mutex;
  43      std::vector<std::shared_ptr<CWallet>> wallets GUARDED_BY(wallets_mutex);
  44      std::list<LoadWalletFn> wallet_load_fns GUARDED_BY(wallets_mutex);
  45  
  46      //! Declare default constructor and destructor that are not inline, so code
  47      //! instantiating the WalletContext struct doesn't need to #include class
  48      //! definitions for smart pointer and container members.
  49      WalletContext();
  50      ~WalletContext();
  51  };
  52  } // namespace wallet
  53  
  54  #endif // BITCOIN_WALLET_CONTEXT_H
  55