Skip to content

Commit 2f4d0a8

Browse files
siparecursive-rat4
authored andcommitted
Allow the default key to be unavailable
This solves the issue where no default key can be added after -salvagewallet. Conflicts: src/main.cpp src/wallet.cpp
1 parent d0479b4 commit 2f4d0a8

4 files changed

Lines changed: 32 additions & 23 deletions

File tree

src/init.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,11 +813,11 @@ bool AppInit2()
813813
RandAddSeedPerfmon();
814814

815815
CPubKey newDefaultKey;
816-
if (!pwalletMain->GetKeyFromPool(newDefaultKey, false))
817-
strErrors << _("Cannot initialize keypool") << "\n";
818-
pwalletMain->SetDefaultKey(newDefaultKey);
819-
if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), ""))
820-
strErrors << _("Cannot write default address") << "\n";
816+
if (pwalletMain->GetKeyFromPool(newDefaultKey, false)) {
817+
pwalletMain->SetDefaultKey(newDefaultKey);
818+
if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), ""))
819+
strErrors << _("Cannot write default address") << "\n";
820+
}
821821
}
822822

823823
printf("%s", strErrors.str().c_str());

src/miner.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake, int64_t* pFees)
125125
if (!fProofOfStake)
126126
{
127127
CReserveKey reservekey(pwallet);
128-
txNew.vout[0].scriptPubKey.SetDestination(reservekey.GetReservedKey().GetID());
128+
CPubKey pubkey;
129+
if (!reservekey.GetReservedKey(pubkey))
130+
return NULL;
131+
txNew.vout[0].scriptPubKey.SetDestination(pubkey.GetID());
129132
}
130133
else
131134
{

src/wallet.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,19 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
509509
return false;
510510
#ifndef QT_GUI
511511
// If default receiving address gets used, replace it with a new one
512-
CScript scriptDefaultKey;
513-
scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
514-
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
515-
{
516-
if (txout.scriptPubKey == scriptDefaultKey)
512+
if (vchDefaultKey.IsValid()) {
513+
CScript scriptDefaultKey;
514+
scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
515+
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
517516
{
518-
CPubKey newDefaultKey;
519-
if (GetKeyFromPool(newDefaultKey, false))
517+
if (txout.scriptPubKey == scriptDefaultKey)
520518
{
521-
SetDefaultKey(newDefaultKey);
522-
SetAddressBookName(vchDefaultKey.GetID(), "");
519+
CPubKey newDefaultKey;
520+
if (GetKeyFromPool(newDefaultKey, false))
521+
{
522+
SetDefaultKey(newDefaultKey);
523+
SetAddressBookName(vchDefaultKey.GetID(), "");
524+
}
523525
}
524526
}
525527
}
@@ -1437,7 +1439,8 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
14371439
// post-backup change.
14381440

14391441
// Reserve a new key pair from key pool
1440-
CPubKey vchPubKey = reservekey.GetReservedKey();
1442+
CPubKey vchPubKey;
1443+
assert(reservekey.GetReservedKey(vchPubKey)); // should never fail, as we just unlocked
14411444

14421445
scriptChange.SetDestination(vchPubKey.GetID());
14431446
}
@@ -2321,22 +2324,25 @@ void CWallet::DisableTransaction(const CTransaction &tx)
23212324
}
23222325
}
23232326

2324-
CPubKey CReserveKey::GetReservedKey()
2327+
bool CReserveKey::GetReservedKey(CPubKey& pubkey)
23252328
{
23262329
if (nIndex == -1)
23272330
{
23282331
CKeyPool keypool;
23292332
pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
23302333
if (nIndex != -1)
23312334
vchPubKey = keypool.vchPubKey;
2332-
else
2333-
{
2334-
printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
2335-
vchPubKey = pwallet->vchDefaultKey;
2335+
else {
2336+
if (pwallet->vchDefaultKey.IsValid()) {
2337+
printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
2338+
vchPubKey = pwallet->vchDefaultKey;
2339+
} else
2340+
return false;
23362341
}
23372342
}
23382343
assert(vchPubKey.IsValid());
2339-
return vchPubKey;
2344+
pubkey = vchPubKey;
2345+
return true;
23402346
}
23412347

23422348
void CReserveKey::KeepKey()

src/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class CReserveKey
345345
}
346346

347347
void ReturnKey();
348-
CPubKey GetReservedKey();
348+
bool GetReservedKey(CPubKey &pubkey);
349349
void KeepKey();
350350
};
351351

0 commit comments

Comments
 (0)