@@ -3405,16 +3405,8 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
3405
3405
return strUsage;
3406
3406
}
3407
3407
3408
- bool CWallet::InitLoadWallet ( )
3408
+ CWallet* CWallet::CreateWalletFromFile ( const std::string walletFile )
3409
3409
{
3410
- if (GetBoolArg (" -disablewallet" , DEFAULT_DISABLE_WALLET)) {
3411
- pwalletMain = NULL ;
3412
- LogPrintf (" Wallet disabled!\n " );
3413
- return true ;
3414
- }
3415
-
3416
- std::string walletFile = GetArg (" -wallet" , DEFAULT_WALLET_DAT);
3417
-
3418
3410
// needed to restore wallet transaction meta data after -zapwallettxes
3419
3411
std::vector<CWalletTx> vWtx;
3420
3412
@@ -3424,7 +3416,8 @@ bool CWallet::InitLoadWallet()
3424
3416
CWallet *tempWallet = new CWallet (walletFile);
3425
3417
DBErrors nZapWalletRet = tempWallet->ZapWalletTx (vWtx);
3426
3418
if (nZapWalletRet != DB_LOAD_OK) {
3427
- return InitError (strprintf (_ (" Error loading %s: Wallet corrupted" ), walletFile));
3419
+ InitError (strprintf (_ (" Error loading %s: Wallet corrupted" ), walletFile));
3420
+ return NULL ;
3428
3421
}
3429
3422
3430
3423
delete tempWallet;
@@ -3439,23 +3432,29 @@ bool CWallet::InitLoadWallet()
3439
3432
DBErrors nLoadWalletRet = walletInstance->LoadWallet (fFirstRun );
3440
3433
if (nLoadWalletRet != DB_LOAD_OK)
3441
3434
{
3442
- if (nLoadWalletRet == DB_CORRUPT)
3443
- return InitError (strprintf (_ (" Error loading %s: Wallet corrupted" ), walletFile));
3435
+ if (nLoadWalletRet == DB_CORRUPT) {
3436
+ InitError (strprintf (_ (" Error loading %s: Wallet corrupted" ), walletFile));
3437
+ return NULL ;
3438
+ }
3444
3439
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
3445
3440
{
3446
3441
InitWarning (strprintf (_ (" Error reading %s! All keys read correctly, but transaction data"
3447
3442
" or address book entries might be missing or incorrect." ),
3448
3443
walletFile));
3449
3444
}
3450
- else if (nLoadWalletRet == DB_TOO_NEW)
3451
- return InitError (strprintf (_ (" Error loading %s: Wallet requires newer version of %s" ),
3452
- walletFile, _ (PACKAGE_NAME)));
3445
+ else if (nLoadWalletRet == DB_TOO_NEW) {
3446
+ InitError (strprintf (_ (" Error loading %s: Wallet requires newer version of %s" ), walletFile, _ (PACKAGE_NAME)));
3447
+ return NULL ;
3448
+ }
3453
3449
else if (nLoadWalletRet == DB_NEED_REWRITE)
3454
3450
{
3455
- return InitError (strprintf (_ (" Wallet needed to be rewritten: restart %s to complete" ), _ (PACKAGE_NAME)));
3451
+ InitError (strprintf (_ (" Wallet needed to be rewritten: restart %s to complete" ), _ (PACKAGE_NAME)));
3452
+ return NULL ;
3453
+ }
3454
+ else {
3455
+ InitError (strprintf (_ (" Error loading %s" ), walletFile));
3456
+ return NULL ;
3456
3457
}
3457
- else
3458
- return InitError (strprintf (_ (" Error loading %s" ), walletFile));
3459
3458
}
3460
3459
3461
3460
if (GetBoolArg (" -upgradewallet" , fFirstRun ))
@@ -3471,7 +3470,8 @@ bool CWallet::InitLoadWallet()
3471
3470
LogPrintf (" Allowing wallet upgrade up to %i\n " , nMaxVersion);
3472
3471
if (nMaxVersion < walletInstance->GetVersion ())
3473
3472
{
3474
- return InitError (_ (" Cannot downgrade wallet" ));
3473
+ InitError (_ (" Cannot downgrade wallet" ));
3474
+ return NULL ;
3475
3475
}
3476
3476
walletInstance->SetMaxVersion (nMaxVersion);
3477
3477
}
@@ -3488,18 +3488,24 @@ bool CWallet::InitLoadWallet()
3488
3488
CPubKey newDefaultKey;
3489
3489
if (walletInstance->GetKeyFromPool (newDefaultKey)) {
3490
3490
walletInstance->SetDefaultKey (newDefaultKey);
3491
- if (!walletInstance->SetAddressBook (walletInstance->vchDefaultKey .GetID (), " " , " receive" ))
3492
- return InitError (_ (" Cannot write default address" ) += " \n " );
3491
+ if (!walletInstance->SetAddressBook (walletInstance->vchDefaultKey .GetID (), " " , " receive" )) {
3492
+ InitError (_ (" Cannot write default address" ) += " \n " );
3493
+ return NULL ;
3494
+ }
3493
3495
}
3494
3496
3495
3497
walletInstance->SetBestChain (chainActive.GetLocator ());
3496
3498
}
3497
3499
else if (IsArgSet (" -usehd" )) {
3498
3500
bool useHD = GetBoolArg (" -usehd" , DEFAULT_USE_HD_WALLET);
3499
- if (walletInstance->IsHDEnabled () && !useHD)
3500
- return InitError (strprintf (_ (" Error loading %s: You can't disable HD on a already existing HD wallet" ), walletFile));
3501
- if (!walletInstance->IsHDEnabled () && useHD)
3502
- return InitError (strprintf (_ (" Error loading %s: You can't enable HD on a already existing non-HD wallet" ), walletFile));
3501
+ if (walletInstance->IsHDEnabled () && !useHD) {
3502
+ InitError (strprintf (_ (" Error loading %s: You can't disable HD on a already existing HD wallet" ), walletFile));
3503
+ return NULL ;
3504
+ }
3505
+ if (!walletInstance->IsHDEnabled () && useHD) {
3506
+ InitError (strprintf (_ (" Error loading %s: You can't enable HD on a already existing non-HD wallet" ), walletFile));
3507
+ return NULL ;
3508
+ }
3503
3509
}
3504
3510
3505
3511
LogPrintf (" wallet %15dms\n " , GetTimeMillis () - nStart);
@@ -3529,8 +3535,10 @@ bool CWallet::InitLoadWallet()
3529
3535
while (block && block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA) && block->pprev ->nTx > 0 && pindexRescan != block)
3530
3536
block = block->pprev ;
3531
3537
3532
- if (pindexRescan != block)
3533
- return InitError (_ (" Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)" ));
3538
+ if (pindexRescan != block) {
3539
+ InitError (_ (" Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)" ));
3540
+ return NULL ;
3541
+ }
3534
3542
}
3535
3543
3536
3544
uiInterface.InitMessage (_ (" Rescanning..." ));
@@ -3575,19 +3583,40 @@ bool CWallet::InitLoadWallet()
3575
3583
LogPrintf (" mapAddressBook.size() = %u\n " , walletInstance->mapAddressBook .size ());
3576
3584
}
3577
3585
3578
- pwalletMain = walletInstance;
3586
+ return walletInstance;
3587
+ }
3588
+
3589
+ bool CWallet::InitLoadWallet ()
3590
+ {
3591
+ if (GetBoolArg (" -disablewallet" , DEFAULT_DISABLE_WALLET)) {
3592
+ pwalletMain = NULL ;
3593
+ LogPrintf (" Wallet disabled!\n " );
3594
+ return true ;
3595
+ }
3596
+
3597
+ std::string walletFile = GetArg (" -wallet" , DEFAULT_WALLET_DAT);
3598
+
3599
+ CWallet * const pwallet = CreateWalletFromFile (walletFile);
3600
+ if (!pwallet) {
3601
+ return false ;
3602
+ }
3603
+ pwalletMain = pwallet;
3579
3604
3580
3605
return true ;
3581
3606
}
3582
3607
3608
+ std::atomic<bool > CWallet::fFlushThreadRunning (false );
3609
+
3583
3610
void CWallet::postInitProcess (boost::thread_group& threadGroup)
3584
3611
{
3585
3612
// Add wallet transactions that aren't already in a block to mempool
3586
3613
// Do this here as mempool requires genesis block to be loaded
3587
3614
ReacceptWalletTransactions ();
3588
3615
3589
3616
// Run a thread to flush wallet periodically
3590
- threadGroup.create_thread (boost::bind (&ThreadFlushWalletDB, boost::ref (this ->strWalletFile )));
3617
+ if (!CWallet::fFlushThreadRunning .exchange (true )) {
3618
+ threadGroup.create_thread (ThreadFlushWalletDB);
3619
+ }
3591
3620
}
3592
3621
3593
3622
bool CWallet::ParameterInteraction ()
0 commit comments