ledger-core
WalletPool.hpp
1 /*
2  *
3  * WalletPool
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 10/05/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_WALLETPOOL_HPP
32 #define LEDGER_CORE_WALLETPOOL_HPP
33 
34 #include <string>
35 #include <utils/Option.hpp>
36 #include <api/HttpClient.hpp>
37 #include <api/WebSocketClient.hpp>
38 #include <api/PathResolver.hpp>
39 #include <api/LogPrinter.hpp>
40 #include <api/ThreadDispatcher.hpp>
41 #include <api/RandomNumberGenerator.hpp>
42 #include <api/DatabaseBackend.hpp>
43 #include <api/WalletPoolCallback.hpp>
44 
45 #include <memory>
46 #include <unordered_map>
47 
48 #include <net/HttpClient.hpp>
49 #include <async/DedicatedContext.hpp>
50 #include <preferences/Preferences.hpp>
51 #include <api/DynamicObject.hpp>
52 #include <database/DatabaseSessionPool.hpp>
53 #include <collections/DynamicObject.hpp>
54 #include <wallet/bitcoin/factories/BitcoinLikeWalletFactory.hpp>
55 #include <wallet/common/AbstractWalletFactory.hpp>
56 #include <events/EventPublisher.hpp>
57 #include <net/WebSocketClient.h>
58 #include <utils/TTLCache.h>
59 namespace ledger {
60  namespace core {
61  class BitcoinLikeWalletFactory;
62 
63  class WalletPool : public DedicatedContext, public std::enable_shared_from_this<WalletPool> {
64  public:
65  std::shared_ptr<HttpClient> getHttpClient(const std::string& baseUrl);
66  std::shared_ptr<WebSocketClient> getWebSocketClient() const;
67  std::shared_ptr<Preferences> getExternalPreferences() const;
68  std::shared_ptr<Preferences> getInternalPreferences() const;
69  std::shared_ptr<api::PathResolver> getPathResolver() const;
70  std::shared_ptr<api::RandomNumberGenerator> rng() const;
71  std::shared_ptr<api::ThreadDispatcher> getDispatcher() const;
72  std::shared_ptr<spdlog::logger> logger() const;
73  std::shared_ptr<DatabaseSessionPool> getDatabaseSessionPool() const;
74  std::shared_ptr<DynamicObject> getConfiguration() const;
75  std::shared_ptr<api::EventBus> getEventBus() const;
76  const std::string& getName() const;
77  const std::string getPassword() const;
78 
79  std::shared_ptr<AbstractWalletFactory> getFactory(const std::string& currencyName) const;
80 
81  // Fetch wallet
82  Future<int64_t> getWalletCount();
83  Future<std::vector<std::shared_ptr<AbstractWallet>>> getWallets(int64_t from, int64_t size);
84  FuturePtr<AbstractWallet> getWallet(const std::string& name);
85  Future<api::ErrorCode> updateWalletConfig(const std::string &name,
86  const std::shared_ptr<api::DynamicObject> &configuration);
87  Future<std::vector<std::string>> getWalletNames(int64_t from, int64_t size) const;
88 
89  // Create wallet
90  FuturePtr<AbstractWallet> createWallet(const std::string& name,
91  const std::string& currencyName,
92  const std::shared_ptr<api::DynamicObject>& configuration);
93 
94 
95  // Delete wallet
96  Future<Unit> deleteWallet(const std::string& name);
97 
98  Future<api::Block> getLastBlock(const std::string& currencyName);
99  Future<api::ErrorCode> eraseDataSince(const std::chrono::system_clock::time_point & date);
100 
101  // Password management
102  Future<api::ErrorCode> changePassword(
103  const std::string& oldPassword,
104  const std::string& newPassword
105  );
106 
107  // Currencies management
108  Option<api::Currency> getCurrency(const std::string& name) const;
109  const std::vector<api::Currency>& getCurrencies() const;
110  Future<Unit> addCurrency(const api::Currency& currency);
111  Future<Unit> removeCurrency(const std::string& currencyName);
112  static std::shared_ptr<WalletPool> newInstance(
113  const std::string &name,
114  const std::string &password,
115  const std::shared_ptr<api::HttpClient> &httpClient,
116  const std::shared_ptr<api::WebSocketClient> &webSocketClient,
117  const std::shared_ptr<api::PathResolver> &pathResolver,
118  const std::shared_ptr<api::LogPrinter> &logPrinter,
119  const std::shared_ptr<api::ThreadDispatcher> &dispatcher,
120  const std::shared_ptr<api::RandomNumberGenerator>& rng,
121  const std::shared_ptr<api::DatabaseBackend> &backend,
122  const std::shared_ptr<api::DynamicObject>& configuration
123  );
124 
125  ~WalletPool() = default;
126 
142 
143  Option<api::Block> getBlockFromCache(const std::string &currencyName);
144  std::shared_ptr<api::ExecutionContext> getThreadPoolExecutionContext() const;
145  private:
146  WalletPool(
147  const std::string &name,
148  const std::string &password,
149  const std::shared_ptr<api::HttpClient> &httpClient,
150  const std::shared_ptr<api::WebSocketClient> &webSocketClient,
151  const std::shared_ptr<api::PathResolver> &pathResolver,
152  const std::shared_ptr<api::LogPrinter> &logPrinter,
153  const std::shared_ptr<api::ThreadDispatcher> &dispatcher,
154  const std::shared_ptr<api::RandomNumberGenerator>& rng,
155  const std::shared_ptr<api::DatabaseBackend> &backend,
156  const std::shared_ptr<api::DynamicObject>& configuration
157  );
158 
159  void initializeCurrencies();
160 
161  void createFactory(const api::Currency& currency);
162 
163  void initializeFactories();
164  std::shared_ptr<AbstractWallet> buildWallet(const WalletDatabaseEntry& entry);
165 
166  static Option<WalletDatabaseEntry> getWalletEntryFromDatabase(const std::shared_ptr<WalletPool> &walletPool,
167  const std::string &name);
168 
169  // General
170  std::string _poolName;
171  std::string _password;
172  std::shared_ptr<DynamicObject> _configuration;
173 
174  // File system management
175  std::shared_ptr<api::PathResolver> _pathResolver;
176 
177  // HTTP management
178  std::shared_ptr<api::HttpClient> _httpEngine;
179  std::unordered_map<std::string, std::weak_ptr<HttpClient>> _httpClients;
180 
181  // WS management
182  std::shared_ptr<WebSocketClient> _wsClient;
183 
184  // Preferences management
185  std::shared_ptr<PreferencesBackend> _externalPreferencesBackend;
186  std::shared_ptr<PreferencesBackend> _internalPreferencesBackend;
187 
188  // Database management
189  std::shared_ptr<DatabaseSessionPool> _database;
190 
191  // Logger
192  std::shared_ptr<spdlog::logger> _logger;
193  std::shared_ptr<api::LogPrinter> _logPrinter;
194 
195  // Threading management
196  std::shared_ptr<api::ThreadDispatcher> _threadDispatcher;
197 
198  // RNG management
199  std::shared_ptr<api::RandomNumberGenerator> _rng;
200 
201  // Currencies management
202  std::vector<api::Currency> _currencies;
203 
204  // Event publisher
205  std::shared_ptr<EventPublisher> _publisher;
206 
207  // Factories management
208  std::vector<std::shared_ptr<AbstractWalletFactory>> _factories;
209 
210  // Wallets
211  std::unordered_map<std::string, std::shared_ptr<AbstractWallet>> _wallets;
212 
213  // Event filter variables
214  std::mutex _eventFilterMutex;
215  std::unordered_map<std::string, int64_t> _lastEmittedBlocks;
216 
217  std::shared_ptr<api::ExecutionContext> _threadPoolExecutionContext;
218  //Here the key is the currency name
220  };
221  }
222 }
223 
224 #endif //LEDGER_CORE_WALLETPOOL_HPP
Future< api::ErrorCode > freshResetAll()
Definition: WalletPool.cpp:551
Definition: Option.hpp:49
Definition: Deffered.hpp:49
Definition: WalletPool.hpp:63
Definition: WalletDatabaseEntry.hpp:39
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: DedicatedContext.hpp:39
Definition: TTLCache.h:45
Definition: logger.hpp:44