| 3155 | } |
| 3156 | |
| 3157 | std::shared_ptr<CWallet> CWallet::LoadExisting(WalletContext& context, const std::string& name, std::unique_ptr<WalletDatabase> database, bilingual_str& error, std::vector<bilingual_str>& warnings) |
| 3158 | { |
| 3159 | interfaces::Chain* chain = context.chain; |
| 3160 | const std::string& walletFile = database->Filename(); |
| 3161 | |
| 3162 | const auto start{SteadyClock::now()}; |
| 3163 | std::shared_ptr<CWallet> walletInstance(new CWallet(chain, name, std::move(database)), FlushAndDeleteWallet); |
| 3164 | |
| 3165 | if (!LoadWalletArgs(walletInstance, context, error, warnings)) { |
| 3166 | return nullptr; |
| 3167 | } |
| 3168 | |
| 3169 | // Load wallet |
| 3170 | auto nLoadWalletRet = walletInstance->PopulateWalletFromDB(error, warnings); |
| 3171 | bool rescan_required = nLoadWalletRet == DBErrors::NEED_RESCAN; |
| 3172 | if (nLoadWalletRet != DBErrors::LOAD_OK && nLoadWalletRet != DBErrors::NONCRITICAL_ERROR && !rescan_required) { |
| 3173 | return nullptr; |
| 3174 | } |
| 3175 | |
| 3176 | if (walletInstance->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { |
| 3177 | for (auto spk_man : walletInstance->GetActiveScriptPubKeyMans()) { |
| 3178 | if (spk_man->HavePrivateKeys()) { |
| 3179 | warnings.push_back(strprintf(_("Warning: Private keys detected in wallet {%s} with disabled private keys"), walletFile)); |
| 3180 | break; |
| 3181 | } |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | walletInstance->WalletLogPrintf("Wallet completed loading in %15dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - start)); |
| 3186 | |
| 3187 | // Try to top up keypool. No-op if the wallet is locked. |
| 3188 | walletInstance->TopUpKeyPool(); |
| 3189 | |
| 3190 | if (chain && !AttachChain(walletInstance, *chain, rescan_required, error, warnings)) { |
| 3191 | walletInstance->DisconnectChainNotifications(); |
| 3192 | return nullptr; |
| 3193 | } |
| 3194 | |
| 3195 | WITH_LOCK(walletInstance->cs_wallet, walletInstance->LogStats()); |
| 3196 | |
| 3197 | return walletInstance; |
| 3198 | } |
| 3199 | |
| 3200 | |
| 3201 | bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interfaces::Chain& chain, const bool rescan_required, bilingual_str& error, std::vector<bilingual_str>& warnings) |
nothing calls this directly
no test coverage detected