Skip to content

Commit

Permalink
Automatically rescan after restoring wallet.dat
Browse files Browse the repository at this point in the history
Information about the best known chain is added to wallet.dat. If this
information does not match the data in blkindex.dat, a rescan is automatically
performed, starting from the the last known block. When upgrading from a wallet
which does not have this information, no rescan is done automatically.
  • Loading branch information
sipa authored and gavinandresen committed Apr 18, 2011
1 parent 657cfe7 commit 6a76c60
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions db.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CAddress;
class CWalletTx;
class CAccount;
class CAccountingEntry;
class CBlockLocator;

extern map<string, string> mapAddressBook;
extern CCriticalSection cs_mapAddressBook;
Expand Down Expand Up @@ -405,6 +406,17 @@ class CWalletDB : public CDB
return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);
}

bool WriteBestBlock(const CBlockLocator& locator)
{
nWalletDBUpdated++;
return Write(string("bestblock"), locator);
}

bool ReadBestBlock(CBlockLocator& locator)
{
return Read(string("bestblock"), locator);
}

bool ReadDefaultKey(vector<unsigned char>& vchPubKey)
{
vchPubKey.clear();
Expand Down
13 changes: 12 additions & 1 deletion init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,21 @@ bool AppInit2(int argc, char* argv[])
strErrors += _("Error loading wallet.dat \n");
printf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);

CBlockIndex *pindexRescan = pindexBest;
if (GetBoolArg("-rescan"))
pindexRescan = pindexGenesisBlock;
else
{
CWalletDB walletdb;
CBlockLocator locator;
if (walletdb.ReadBestBlock(locator))
pindexRescan = locator.GetBlockIndex();
}
if (pindexBest != pindexRescan)
{
printf("Rescanning last %i blocks (from block %i)...\n", pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
nStart = GetTimeMillis();
ScanForWalletTransactions(pindexGenesisBlock);
ScanForWalletTransactions(pindexRescan);
printf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
}

Expand Down
9 changes: 9 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,15 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
}
}

// Update best block in wallet (so we can detect restored wallets)
if (!IsInitialBlockDownload())
{
CWalletDB walletdb;
const CBlockLocator locator(pindexNew);
if (!walletdb.WriteBestBlock(locator))
return error("SetBestChain() : WriteWalletBest failed");
}

// New best block
hashBestChain = hash;
pindexBest = pindexNew;
Expand Down

0 comments on commit 6a76c60

Please sign in to comment.