Skip to content

Commit

Permalink
Merge bitcoin#7061 jonas/2015/11/wallet_rescan_rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Jun 28, 2016
2 parents bb7b476 + 349f78b commit 8c521c7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
uiInterface.InitMessage(_("Rescanning..."));
LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight);
nStart = GetTimeMillis();
pwalletMain->ScanForWalletTransactions(pindexRescan, true);
pwalletMain->ScanForWalletTransactions(pindexRescan, NULL, true);
LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
pwalletMain->SetBestChain(chainActive.GetLocator());
nWalletDBUpdated++;
Expand Down
2 changes: 2 additions & 0 deletions src/rpcclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "prioritisetransaction", 2 },
{ "setban", 2 },
{ "setban", 3 },
{ "rescanblockchain", 0 },
{ "rescanblockchain", 1 },
};

class CRPCConvertTable
Expand Down
1 change: 1 addition & 0 deletions src/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ static const CRPCCommand vRPCCommands[] =
{ "wallet", "listunspent", &listunspent, false },
{ "wallet", "lockunspent", &lockunspent, true },
{ "wallet", "move", &movecmd, false },
{ "wallet", "rescanblockchain", &rescanblockchain, true },
{ "wallet", "sendfrom", &sendfrom, false },
{ "wallet", "sendmany", &sendmany, false },
{ "wallet", "sendtoaddress", &sendtoaddress, false },
Expand Down
1 change: 1 addition & 0 deletions src/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ extern UniValue importaddress(const UniValue& params, bool fHelp);
extern UniValue importpubkey(const UniValue& params, bool fHelp);
extern UniValue dumpwallet(const UniValue& params, bool fHelp);
extern UniValue importwallet(const UniValue& params, bool fHelp);
extern UniValue rescanblockchain(const UniValue& params, bool fHelp);

extern UniValue getgenerate(const UniValue& params, bool fHelp); // in rpcmining.cpp
extern UniValue setgenerate(const UniValue& params, bool fHelp);
Expand Down
55 changes: 51 additions & 4 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'

if (fRescan) {
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), NULL, true);
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@ UniValue importaddress(const UniValue& params, bool fHelp)

if (fRescan)
{
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), NULL, true);
pwalletMain->ReacceptWalletTransactions();
}

Expand Down Expand Up @@ -294,7 +294,7 @@ UniValue importpubkey(const UniValue& params, bool fHelp)

if (fRescan)
{
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), NULL, true);
pwalletMain->ReacceptWalletTransactions();
}

Expand Down Expand Up @@ -400,7 +400,7 @@ UniValue importwallet(const UniValue& params, bool fHelp)
pwalletMain->nTimeFirstKey = nTimeBegin;

LogPrintf("Rescanning last %i blocks\n", chainActive.Height() - pindex->nHeight + 1);
pwalletMain->ScanForWalletTransactions(pindex);
pwalletMain->ScanForWalletTransactions(pindex, NULL);
pwalletMain->MarkDirty();

if (!fGood)
Expand Down Expand Up @@ -511,3 +511,50 @@ UniValue dumpwallet(const UniValue& params, bool fHelp)
file.close();
return NullUniValue;
}

UniValue rescanblockchain(const UniValue& params, bool fHelp)
{
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;

if (fHelp || params.size() > 2)
throw runtime_error(
"rescanblockchain \"start-height\" \"stop-height\"\n"
"\nRescan the local blockchain for wallet related transactions.\n"
"\nArguments:\n"
"1. \"start-height\" (number, optional) blockheight where the rescan should start\n"
"2. \"stop-height\" (number, optional) blockheight where the rescan should stop\n"
"\nExamples:\n"
+ HelpExampleCli("rescanblockchain", "\"100000 120000\"")
+ HelpExampleRpc("rescanblockchain", "\"100000 120000\"")
);

LOCK2(cs_main, pwalletMain->cs_wallet);

CBlockIndex *pIndexStart = NULL;
CBlockIndex *pIndexStop = NULL;
if (params.size() > 0 && params[0].isNum())
pIndexStart = chainActive[params[0].get_int()];

if (params.size() > 1 && params[1].isNum())
pIndexStop = chainActive[params[1].get_int()];

if (!pIndexStart)
pIndexStart = chainActive.Genesis();

//We can't rescan beyond non-pruned blocks, stop and throw an error
if (fPruneMode)
{
CBlockIndex *block = chainActive.Tip();
while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA))
block = block->pprev;

if (pIndexStart->nHeight < block->nHeight)
throw JSONRPCError(RPC_WALLET_ERROR, "Can't rescan beyond pruned data.");
}

if (pwalletMain)
pwalletMain->ScanForWalletTransactions(pIndexStart, pIndexStop, true);

return NullUniValue;
}
7 changes: 6 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,15 @@ bool CWalletTx::WriteToDisk(CWalletDB *pwalletdb)
* from or to us. If fUpdate is true, found transactions that already
* exist in the wallet will be updated.
*/
int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, CBlockIndex* pindexStop, bool fUpdate)
{
int ret = 0;
int64_t nNow = GetTime();
const CChainParams& chainParams = Params();

if (pindexStop && pindexStop->nHeight < pindexStart->nHeight)
return ret;

CBlockIndex* pindex = pindexStart;
{
LOCK2(cs_main, cs_wallet);
Expand All @@ -1225,6 +1228,8 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
ret++;
}
if (pindex == pindexStop)
break;
pindex = chainActive.Next(pindex);
if (GetTime() >= nNow + 60) {
nNow = GetTime();
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
int ScanForWalletTransactions(CBlockIndex* pindexStart, CBlockIndex* pindexStop, bool fUpdate = false);
void ReacceptWalletTransactions();
void ResendWalletTransactions(int64_t nBestBlockTime);
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime);
Expand Down

0 comments on commit 8c521c7

Please sign in to comment.