Skip to content

Commit

Permalink
Allow the default key to be unavailable
Browse files Browse the repository at this point in the history
This solves the issue where no default key can be added after -salvagewallet.
  • Loading branch information
sipa authored and Pieter Wuille committed Apr 25, 2013
1 parent 77a1e12 commit 360cfe1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 22 additions & 17 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "");
}
}
}
}
Expand Down Expand Up @@ -1199,8 +1201,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();
// 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
Expand Down Expand Up @@ -1737,22 +1739,25 @@ set< set<CTxDestination> > CWallet::GetAddressGroupings()
return ret;
}

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()
Expand Down
2 changes: 1 addition & 1 deletion src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class CReserveKey
}

void ReturnKey();
CPubKey GetReservedKey();
bool GetReservedKey(CPubKey &pubkey);
void KeepKey();
};

Expand Down

0 comments on commit 360cfe1

Please sign in to comment.