From 360cfe142c552ac5c4d904a1e970390188151ca8 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 25 Apr 2013 19:30:28 +0200 Subject: [PATCH] Allow the default key to be unavailable This solves the issue where no default key can be added after -salvagewallet. --- src/init.cpp | 10 +++++----- src/main.cpp | 5 ++++- src/wallet.cpp | 39 ++++++++++++++++++++++----------------- src/wallet.h | 2 +- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index f6485c3b1..f35979bc0 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -939,11 +939,11 @@ bool AppInit2(boost::thread_group& threadGroup) RandAddSeedPerfmon(); CPubKey newDefaultKey; - if (!pwalletMain->GetKeyFromPool(newDefaultKey, false)) - strErrors << _("Cannot initialize keypool") << "\n"; - pwalletMain->SetDefaultKey(newDefaultKey); - if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), "")) - strErrors << _("Cannot write default address") << "\n"; + if (pwalletMain->GetKeyFromPool(newDefaultKey, false)) { + pwalletMain->SetDefaultKey(newDefaultKey); + if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), "")) + strErrors << _("Cannot write default address") << "\n"; + } } printf("%s", strErrors.str().c_str()); diff --git a/src/main.cpp b/src/main.cpp index aace382d8..45fb7af00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4158,7 +4158,10 @@ CBlockTemplate* CreateNewBlock(CReserveKey& reservekey) txNew.vin.resize(1); txNew.vin[0].prevout.SetNull(); txNew.vout.resize(1); - txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG; + CPubKey pubkey; + if (!reservekey.GetReservedKey(pubkey)) + return NULL; + txNew.vout[0].scriptPubKey << pubkey << OP_CHECKSIG; // Add our coinbase tx as first transaction pblock->vtx.push_back(txNew); diff --git a/src/wallet.cpp b/src/wallet.cpp index c7eb4f74e..0672a3669 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -457,17 +457,19 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) return false; #ifndef QT_GUI // If default receiving address gets used, replace it with a new one - CScript scriptDefaultKey; - scriptDefaultKey.SetDestination(vchDefaultKey.GetID()); - BOOST_FOREACH(const CTxOut& txout, wtx.vout) - { - if (txout.scriptPubKey == scriptDefaultKey) + if (vchDefaultKey.IsValid()) { + CScript scriptDefaultKey; + scriptDefaultKey.SetDestination(vchDefaultKey.GetID()); + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { - CPubKey newDefaultKey; - if (GetKeyFromPool(newDefaultKey, false)) + if (txout.scriptPubKey == scriptDefaultKey) { - SetDefaultKey(newDefaultKey); - SetAddressBookName(vchDefaultKey.GetID(), ""); + CPubKey newDefaultKey; + if (GetKeyFromPool(newDefaultKey, false)) + { + SetDefaultKey(newDefaultKey); + SetAddressBookName(vchDefaultKey.GetID(), ""); + } } } } @@ -1199,8 +1201,8 @@ bool CWallet::CreateTransaction(const vector >& vecSend, CW // post-backup change. // Reserve a new key pair from key pool - CPubKey vchPubKey = reservekey.GetReservedKey(); - // assert(mapKeys.count(vchPubKey)); + CPubKey vchPubKey; + assert(reservekey.GetReservedKey(vchPubKey)); // should never fail, as we just unlocked // Fill a vout to ourself // TODO: pass in scriptChange instead of reservekey so @@ -1737,7 +1739,7 @@ set< set > CWallet::GetAddressGroupings() return ret; } -CPubKey CReserveKey::GetReservedKey() +bool CReserveKey::GetReservedKey(CPubKey& pubkey) { if (nIndex == -1) { @@ -1745,14 +1747,17 @@ CPubKey CReserveKey::GetReservedKey() pwallet->ReserveKeyFromKeyPool(nIndex, keypool); if (nIndex != -1) vchPubKey = keypool.vchPubKey; - else - { - printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!"); - vchPubKey = pwallet->vchDefaultKey; + else { + if (pwallet->vchDefaultKey.IsValid()) { + printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!"); + vchPubKey = pwallet->vchDefaultKey; + } else + return false; } } assert(vchPubKey.IsValid()); - return vchPubKey; + pubkey = vchPubKey; + return true; } void CReserveKey::KeepKey() diff --git a/src/wallet.h b/src/wallet.h index 2e007557b..55200476a 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -331,7 +331,7 @@ public: } void ReturnKey(); - CPubKey GetReservedKey(); + bool GetReservedKey(CPubKey &pubkey); void KeepKey(); };