Permalink
Browse files

Don't fail if default key is unavailable

This solves the issue where no default key can be added after
-salvagewallet.
  • Loading branch information...
Tranz5 committed Jul 15, 2014
1 parent e2a25ef commit a033f57e0cc1a63adc6ef4e388998b868728848e
Showing with 32 additions and 23 deletions.
  1. +4 −1 src/miner.cpp
  2. +27 −21 src/wallet.cpp
  3. +1 −1 src/wallet.h
@@ -128,7 +128,10 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake)
if (!fProofOfStake)
{
CReserveKey reservekey(pwallet);
txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG;
CPubKey pubkey;
if (!reservekey.GetReservedKey(pubkey))
return NULL;
txNew.vout[0].scriptPubKey.SetDestination(pubkey.GetID());
}
else
txNew.vout[0].SetEmpty();
@@ -508,17 +508,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(), "");
}
}
}
}
@@ -1499,7 +1501,8 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CW
// post-backup change.
// Reserve a new key pair from key pool
CPubKey vchPubKey = reservekey.GetReservedKey();
CPubKey vchPubKey;
assert(reservekey.GetReservedKey(vchPubKey)); // should never fail, as we just unlocked
scriptChange.SetDestination(vchPubKey.GetID());
}
@@ -2507,22 +2510,25 @@ void CWalletLockJob::Run()
pWallet->ResetLockTime();
}
CPubKey CReserveKey::GetReservedKey()
bool CReserveKey::GetReservedKey(CPubKey& pubkey)
{
if (nIndex == -1)
{
CKeyPool keypool;
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()
@@ -2784,11 +2790,11 @@ bool CWalletManager::LoadWallet(const string& strName, ostringstream& strErrors,
RandAddSeedPerfmon();
CPubKey newDefaultKey;
if (!pWallet->GetKeyFromPool(newDefaultKey, false))
strErrors << _("Cannot initialize keypool") << "\n";
pWallet->SetDefaultKey(newDefaultKey);
if (!pWallet->SetAddressBookName(pWallet->vchDefaultKey.GetID(), ""))
strErrors << _("Cannot write default address") << "\n";
if (pWallet->GetKeyFromPool(newDefaultKey, false)) {
pWallet->SetDefaultKey(newDefaultKey);
if (!pWallet->SetAddressBookName(pWallet->vchDefaultKey.GetID(), ""))
strErrors << _("Cannot write default address") << "\n";
}
}
printf("%s", strErrors.str().c_str());
@@ -460,7 +460,7 @@ class CReserveKey
}
void ReturnKey();
CPubKey GetReservedKey();
bool GetReservedKey(CPubKey &pubkey);
void KeepKey();
};

0 comments on commit a033f57

Please sign in to comment.