From f9ee7a032b14e5c78ef11446458156680fccf858 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 2 Oct 2013 11:15:51 -0400 Subject: [PATCH 1/3] init.cpp: cosmetic indent changes, preparing for no-wallet mode --- src/init.cpp | 222 ++++++++++++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 109 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 6749064ed..19353c369 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -587,49 +587,51 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) // ********************************************************* Step 5: verify wallet database integrity - uiInterface.InitMessage(_("Verifying wallet...")); + if (1) { + uiInterface.InitMessage(_("Verifying wallet...")); - if (!bitdb.Open(GetDataDir())) - { - // try moving the database env out of the way - boost::filesystem::path pathDatabase = GetDataDir() / "database"; - boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%"PRId64".bak", GetTime()); - try { - boost::filesystem::rename(pathDatabase, pathDatabaseBak); - LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str()); - } catch(boost::filesystem::filesystem_error &error) { - // failure is ok (well, not really, but it's not worse than what we started with) - } + if (!bitdb.Open(GetDataDir())) + { + // try moving the database env out of the way + boost::filesystem::path pathDatabase = GetDataDir() / "database"; + boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%"PRId64".bak", GetTime()); + try { + boost::filesystem::rename(pathDatabase, pathDatabaseBak); + LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str()); + } catch(boost::filesystem::filesystem_error &error) { + // failure is ok (well, not really, but it's not worse than what we started with) + } - // try again - if (!bitdb.Open(GetDataDir())) { - // if it still fails, it probably means we can't even create the database env - string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str()); - return InitError(msg); + // try again + if (!bitdb.Open(GetDataDir())) { + // if it still fails, it probably means we can't even create the database env + string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str()); + return InitError(msg); + } } - } - if (GetBoolArg("-salvagewallet", false)) - { - // Recover readable keypairs: - if (!CWalletDB::Recover(bitdb, strWalletFile, true)) - return false; - } + if (GetBoolArg("-salvagewallet", false)) + { + // Recover readable keypairs: + if (!CWalletDB::Recover(bitdb, strWalletFile, true)) + return false; + } - if (filesystem::exists(GetDataDir() / strWalletFile)) - { - CDBEnv::VerifyResult r = bitdb.Verify(strWalletFile, CWalletDB::Recover); - if (r == CDBEnv::RECOVER_OK) + if (filesystem::exists(GetDataDir() / strWalletFile)) { - string msg = strprintf(_("Warning: wallet.dat corrupt, data salvaged!" - " Original wallet.dat saved as wallet.{timestamp}.bak in %s; if" - " your balance or transactions are incorrect you should" - " restore from a backup."), strDataDir.c_str()); - InitWarning(msg); + CDBEnv::VerifyResult r = bitdb.Verify(strWalletFile, CWalletDB::Recover); + if (r == CDBEnv::RECOVER_OK) + { + string msg = strprintf(_("Warning: wallet.dat corrupt, data salvaged!" + " Original wallet.dat saved as wallet.{timestamp}.bak in %s; if" + " your balance or transactions are incorrect you should" + " restore from a backup."), strDataDir.c_str()); + InitWarning(msg); + } + if (r == CDBEnv::RECOVER_FAIL) + return InitError(_("wallet.dat corrupt, salvage failed")); } - if (r == CDBEnv::RECOVER_FAIL) - return InitError(_("wallet.dat corrupt, salvage failed")); - } + } // (1) // ********************************************************* Step 6: network initialization @@ -898,92 +900,94 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) // ********************************************************* Step 8: load wallet - uiInterface.InitMessage(_("Loading wallet...")); + if (1) { + uiInterface.InitMessage(_("Loading wallet...")); - nStart = GetTimeMillis(); - bool fFirstRun = true; - pwalletMain = new CWallet(strWalletFile); - DBErrors nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun); - if (nLoadWalletRet != DB_LOAD_OK) - { - if (nLoadWalletRet == DB_CORRUPT) - strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n"; - else if (nLoadWalletRet == DB_NONCRITICAL_ERROR) + nStart = GetTimeMillis(); + bool fFirstRun = true; + pwalletMain = new CWallet(strWalletFile); + DBErrors nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun); + if (nLoadWalletRet != DB_LOAD_OK) { - string msg(_("Warning: error reading wallet.dat! All keys read correctly, but transaction data" - " or address book entries might be missing or incorrect.")); - InitWarning(msg); + if (nLoadWalletRet == DB_CORRUPT) + strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n"; + else if (nLoadWalletRet == DB_NONCRITICAL_ERROR) + { + string msg(_("Warning: error reading wallet.dat! All keys read correctly, but transaction data" + " or address book entries might be missing or incorrect.")); + InitWarning(msg); + } + else if (nLoadWalletRet == DB_TOO_NEW) + strErrors << _("Error loading wallet.dat: Wallet requires newer version of Bitcoin") << "\n"; + else if (nLoadWalletRet == DB_NEED_REWRITE) + { + strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n"; + LogPrintf("%s", strErrors.str().c_str()); + return InitError(strErrors.str()); + } + else + strErrors << _("Error loading wallet.dat") << "\n"; } - else if (nLoadWalletRet == DB_TOO_NEW) - strErrors << _("Error loading wallet.dat: Wallet requires newer version of Bitcoin") << "\n"; - else if (nLoadWalletRet == DB_NEED_REWRITE) + + if (GetBoolArg("-upgradewallet", fFirstRun)) { - strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n"; - LogPrintf("%s", strErrors.str().c_str()); - return InitError(strErrors.str()); + int nMaxVersion = GetArg("-upgradewallet", 0); + if (nMaxVersion == 0) // the -upgradewallet without argument case + { + LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST); + nMaxVersion = CLIENT_VERSION; + pwalletMain->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately + } + else + LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion); + if (nMaxVersion < pwalletMain->GetVersion()) + strErrors << _("Cannot downgrade wallet") << "\n"; + pwalletMain->SetMaxVersion(nMaxVersion); } - else - strErrors << _("Error loading wallet.dat") << "\n"; - } - if (GetBoolArg("-upgradewallet", fFirstRun)) - { - int nMaxVersion = GetArg("-upgradewallet", 0); - if (nMaxVersion == 0) // the -upgradewallet without argument case + if (fFirstRun) { - LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST); - nMaxVersion = CLIENT_VERSION; - pwalletMain->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately - } - else - LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion); - if (nMaxVersion < pwalletMain->GetVersion()) - strErrors << _("Cannot downgrade wallet") << "\n"; - pwalletMain->SetMaxVersion(nMaxVersion); - } + // Create new keyUser and set as default key + RandAddSeedPerfmon(); + + CPubKey newDefaultKey; + if (pwalletMain->GetKeyFromPool(newDefaultKey)) { + pwalletMain->SetDefaultKey(newDefaultKey); + if (!pwalletMain->SetAddressBook(pwalletMain->vchDefaultKey.GetID(), "", "receive")) + strErrors << _("Cannot write default address") << "\n"; + } - if (fFirstRun) - { - // Create new keyUser and set as default key - RandAddSeedPerfmon(); - - CPubKey newDefaultKey; - if (pwalletMain->GetKeyFromPool(newDefaultKey)) { - pwalletMain->SetDefaultKey(newDefaultKey); - if (!pwalletMain->SetAddressBook(pwalletMain->vchDefaultKey.GetID(), "", "receive")) - strErrors << _("Cannot write default address") << "\n"; + pwalletMain->SetBestChain(chainActive.GetLocator()); } - pwalletMain->SetBestChain(chainActive.GetLocator()); - } - - LogPrintf("%s", strErrors.str().c_str()); - LogPrintf(" wallet %15"PRId64"ms\n", GetTimeMillis() - nStart); + LogPrintf("%s", strErrors.str().c_str()); + LogPrintf(" wallet %15"PRId64"ms\n", GetTimeMillis() - nStart); - RegisterWallet(pwalletMain); + RegisterWallet(pwalletMain); - CBlockIndex *pindexRescan = chainActive.Tip(); - if (GetBoolArg("-rescan", false)) - pindexRescan = chainActive.Genesis(); - else - { - CWalletDB walletdb(strWalletFile); - CBlockLocator locator; - if (walletdb.ReadBestBlock(locator)) - pindexRescan = chainActive.FindFork(locator); - else + CBlockIndex *pindexRescan = chainActive.Tip(); + if (GetBoolArg("-rescan", false)) pindexRescan = chainActive.Genesis(); - } - if (chainActive.Tip() && chainActive.Tip() != pindexRescan) - { - uiInterface.InitMessage(_("Rescanning...")); - LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight); - nStart = GetTimeMillis(); - pwalletMain->ScanForWalletTransactions(pindexRescan, true); - LogPrintf(" rescan %15"PRId64"ms\n", GetTimeMillis() - nStart); - pwalletMain->SetBestChain(chainActive.GetLocator()); - nWalletDBUpdated++; - } + else + { + CWalletDB walletdb(strWalletFile); + CBlockLocator locator; + if (walletdb.ReadBestBlock(locator)) + pindexRescan = chainActive.FindFork(locator); + else + pindexRescan = chainActive.Genesis(); + } + if (chainActive.Tip() && chainActive.Tip() != pindexRescan) + { + uiInterface.InitMessage(_("Rescanning...")); + LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight); + nStart = GetTimeMillis(); + pwalletMain->ScanForWalletTransactions(pindexRescan, true); + LogPrintf(" rescan %15"PRId64"ms\n", GetTimeMillis() - nStart); + pwalletMain->SetBestChain(chainActive.GetLocator()); + nWalletDBUpdated++; + } + } // (1) // ********************************************************* Step 9: import blocks From e6b7e3dc79e91db7a9e20ec8fd593145f6c3ac41 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 2 Oct 2013 11:19:10 -0400 Subject: [PATCH 2/3] Add -disablewallet option, to disable wallet support (and BDB) --- src/init.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 19353c369..d930f6f69 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -499,6 +499,7 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) fPrintToConsole = GetBoolArg("-printtoconsole", false); fPrintToDebugger = GetBoolArg("-printtodebugger", false); fLogTimestamps = GetBoolArg("-logtimestamps", true); + bool fDisableWallet = GetBoolArg("-disablewallet", false); if (mapArgs.count("-timeout")) { @@ -587,7 +588,7 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) // ********************************************************* Step 5: verify wallet database integrity - if (1) { + if (!fDisableWallet) { uiInterface.InitMessage(_("Verifying wallet...")); if (!bitdb.Open(GetDataDir())) @@ -631,7 +632,7 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) if (r == CDBEnv::RECOVER_FAIL) return InitError(_("wallet.dat corrupt, salvage failed")); } - } // (1) + } // (!fDisableWallet) // ********************************************************* Step 6: network initialization @@ -900,7 +901,10 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) // ********************************************************* Step 8: load wallet - if (1) { + if (fDisableWallet) { + pwalletMain = NULL; + LogPrintf("Wallet disabled!\n"); + } else { uiInterface.InitMessage(_("Loading wallet...")); nStart = GetTimeMillis(); @@ -987,7 +991,7 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer) pwalletMain->SetBestChain(chainActive.GetLocator()); nWalletDBUpdated++; } - } // (1) + } // (!fDisableWallet) // ********************************************************* Step 9: import blocks From 146ba964e4b784171c29600e959a0fae3e2e0c03 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 12 Nov 2013 14:54:43 +0100 Subject: [PATCH 3/3] qt: GUI support for -disablewallet mode --- src/qt/bitcoin.cpp | 19 ++++++++++++++----- src/qt/bitcoingui.cpp | 21 +++++++++++++++++++++ src/qt/bitcoingui.h | 3 +++ src/qt/walletframe.cpp | 29 ++++++++++++++++++++--------- src/qt/walletframe.h | 2 ++ 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index 54f96f442..57ce7da36 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -313,11 +313,16 @@ int main(int argc, char *argv[]) splash.finish(&window); ClientModel clientModel(&optionsModel); - WalletModel walletModel(pwalletMain, &optionsModel); + WalletModel *walletModel = 0; + if(pwalletMain) + walletModel = new WalletModel(pwalletMain, &optionsModel); window.setClientModel(&clientModel); - window.addWallet("~Default", &walletModel); - window.setCurrentWallet("~Default"); + if(walletModel) + { + window.addWallet("~Default", walletModel); + window.setCurrentWallet("~Default"); + } // If -min option passed, start window minimized. if(GetBoolArg("-min", false)) @@ -335,8 +340,11 @@ int main(int argc, char *argv[]) &window, SLOT(handlePaymentRequest(SendCoinsRecipient))); QObject::connect(&window, SIGNAL(receivedURI(QString)), paymentServer, SLOT(handleURIOrFile(QString))); - QObject::connect(&walletModel, SIGNAL(coinsSent(CWallet*,SendCoinsRecipient,QByteArray)), - paymentServer, SLOT(fetchPaymentACK(CWallet*,const SendCoinsRecipient&,QByteArray))); + if(walletModel) + { + QObject::connect(walletModel, SIGNAL(coinsSent(CWallet*,SendCoinsRecipient,QByteArray)), + paymentServer, SLOT(fetchPaymentACK(CWallet*,const SendCoinsRecipient&,QByteArray))); + } QObject::connect(paymentServer, SIGNAL(message(QString,QString,unsigned int)), guiref, SLOT(message(QString,QString,unsigned int))); QTimer::singleShot(100, paymentServer, SLOT(uiReady())); @@ -347,6 +355,7 @@ int main(int argc, char *argv[]) window.setClientModel(0); window.removeAllWallets(); guiref = 0; + delete walletModel; } // Shutdown the core and its threads, but don't exit Bitcoin-Qt here threadGroup.interrupt_all(); diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 8b0aba1b5..a1becc12e 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -163,6 +163,9 @@ BitcoinGUI::BitcoinGUI(bool fIsTestnet, QWidget *parent) : // Install event filter to be able to catch status tip events (QEvent::StatusTip) this->installEventFilter(this); + + // Initially wallet actions should be disabled + setWalletActionsEnabled(false); } BitcoinGUI::~BitcoinGUI() @@ -352,6 +355,7 @@ void BitcoinGUI::setClientModel(ClientModel *clientModel) bool BitcoinGUI::addWallet(const QString& name, WalletModel *walletModel) { + setWalletActionsEnabled(true); return walletFrame->addWallet(name, walletModel); } @@ -362,9 +366,26 @@ bool BitcoinGUI::setCurrentWallet(const QString& name) void BitcoinGUI::removeAllWallets() { + setWalletActionsEnabled(false); walletFrame->removeAllWallets(); } +void BitcoinGUI::setWalletActionsEnabled(bool enabled) +{ + overviewAction->setEnabled(enabled); + sendCoinsAction->setEnabled(enabled); + receiveCoinsAction->setEnabled(enabled); + historyAction->setEnabled(enabled); + encryptWalletAction->setEnabled(enabled); + backupWalletAction->setEnabled(enabled); + changePassphraseAction->setEnabled(enabled); + signMessageAction->setEnabled(enabled); + verifyMessageAction->setEnabled(enabled); + usedSendingAddressesAction->setEnabled(enabled); + usedReceivingAddressesAction->setEnabled(enabled); + openAction->setEnabled(enabled); +} + void BitcoinGUI::createTrayIcon(bool fIsTestnet) { #ifndef Q_OS_MAC diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index acbc38c89..b9a8a0313 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -108,6 +108,9 @@ private: /** Create system tray menu (or setup the dock menu) */ void createTrayIconMenu(); + /** Enable or disable all wallet-related actions */ + void setWalletActionsEnabled(bool enabled); + signals: /** Signal raised when a URI was entered or dragged to the GUI */ void receivedURI(const QString &uri); diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp index 04eea5916..fead02292 100644 --- a/src/qt/walletframe.cpp +++ b/src/qt/walletframe.cpp @@ -10,6 +10,7 @@ #include #include +#include WalletFrame::WalletFrame(BitcoinGUI *_gui) : QFrame(_gui), @@ -21,6 +22,10 @@ WalletFrame::WalletFrame(BitcoinGUI *_gui) : walletStack = new QStackedWidget(this); walletFrameLayout->setContentsMargins(0,0,0,0); walletFrameLayout->addWidget(walletStack); + + QLabel *noWallet = new QLabel(tr("No wallet has been loaded.")); + noWallet->setAlignment(Qt::AlignCenter); + walletStack->addWidget(noWallet); } WalletFrame::~WalletFrame() @@ -85,7 +90,7 @@ void WalletFrame::removeAllWallets() bool WalletFrame::handlePaymentRequest(const SendCoinsRecipient &recipient) { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (!walletView) return false; @@ -130,56 +135,62 @@ void WalletFrame::gotoSendCoinsPage(QString addr) void WalletFrame::gotoSignMessageTab(QString addr) { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->gotoSignMessageTab(addr); } void WalletFrame::gotoVerifyMessageTab(QString addr) { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->gotoVerifyMessageTab(addr); } void WalletFrame::encryptWallet(bool status) { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->encryptWallet(status); } void WalletFrame::backupWallet() { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->backupWallet(); } void WalletFrame::changePassphrase() { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->changePassphrase(); } void WalletFrame::unlockWallet() { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->unlockWallet(); } void WalletFrame::usedSendingAddresses() { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->usedSendingAddresses(); } void WalletFrame::usedReceivingAddresses() { - WalletView *walletView = (WalletView*)walletStack->currentWidget(); + WalletView *walletView = currentWalletView(); if (walletView) walletView->usedReceivingAddresses(); } + +WalletView *WalletFrame::currentWalletView() +{ + return qobject_cast(walletStack->currentWidget()); +} + diff --git a/src/qt/walletframe.h b/src/qt/walletframe.h index 8cfaa0e1d..f1830a0d6 100644 --- a/src/qt/walletframe.h +++ b/src/qt/walletframe.h @@ -45,6 +45,8 @@ private: bool bOutOfSync; + WalletView *currentWalletView(); + public slots: /** Switch to overview (home) page */ void gotoOverviewPage();