Skip to content

Commit 393160c

Browse files
committed
Merge #10208: [wallet] Rescan abortability
9141622 [rpc] Add abortrescan command to RPC interface. (Kalle Alm) 75a08e7 [wallet] Add support for aborting wallet transaction rescans. (Kalle Alm) Tree-SHA512: 18545a1dc48c6dc112993f971f3adc7c0341fa621186c6d70bef1052e1d127ca116c5769595d721a209d502ca2019f2ad33876fe35d2b17216393404607a6c76
2 parents d86bb07 + 9141622 commit 393160c

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/wallet/rpcdump.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,31 @@ UniValue importprivkey(const JSONRPCRequest& request)
156156
return NullUniValue;
157157
}
158158

159+
UniValue abortrescan(const JSONRPCRequest& request)
160+
{
161+
CWallet* const pwallet = GetWalletForJSONRPCRequest(request);
162+
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
163+
return NullUniValue;
164+
}
165+
166+
if (request.fHelp || request.params.size() > 0)
167+
throw std::runtime_error(
168+
"abortrescan\n"
169+
"\nStops current wallet rescan triggered e.g. by an importprivkey call.\n"
170+
"\nExamples:\n"
171+
"\nImport a private key\n"
172+
+ HelpExampleCli("importprivkey", "\"mykey\"") +
173+
"\nAbort the running wallet rescan\n"
174+
+ HelpExampleCli("abortrescan", "") +
175+
"\nAs a JSON-RPC call\n"
176+
+ HelpExampleRpc("abortrescan", "")
177+
);
178+
179+
if (!pwallet->IsScanning() || pwallet->IsAbortingRescan()) return false;
180+
pwallet->AbortRescan();
181+
return true;
182+
}
183+
159184
void ImportAddress(CWallet*, const CBitcoinAddress& address, const std::string& strLabel);
160185
void ImportScript(CWallet* const pwallet, const CScript& script, const std::string& strLabel, bool isRedeemScript)
161186
{

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,7 @@ UniValue bumpfee(const JSONRPCRequest& request)
29142914
return result;
29152915
}
29162916

2917+
extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp
29172918
extern UniValue dumpprivkey(const JSONRPCRequest& request); // in rpcdump.cpp
29182919
extern UniValue importprivkey(const JSONRPCRequest& request);
29192920
extern UniValue importaddress(const JSONRPCRequest& request);
@@ -2930,6 +2931,7 @@ static const CRPCCommand commands[] =
29302931
{ "rawtransactions", "fundrawtransaction", &fundrawtransaction, false, {"hexstring","options"} },
29312932
{ "hidden", "resendwallettransactions", &resendwallettransactions, true, {} },
29322933
{ "wallet", "abandontransaction", &abandontransaction, false, {"txid"} },
2934+
{ "wallet", "abortrescan", &abortrescan, false, {} },
29332935
{ "wallet", "addmultisigaddress", &addmultisigaddress, true, {"nrequired","keys","account"} },
29342936
{ "wallet", "addwitnessaddress", &addwitnessaddress, true, {"address"} },
29352937
{ "wallet", "backupwallet", &backupwallet, true, {"destination"} },

src/wallet/wallet.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,8 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
15541554
CBlockIndex* pindex = pindexStart;
15551555
{
15561556
LOCK2(cs_main, cs_wallet);
1557+
fAbortRescan = false;
1558+
fScanningWallet = true;
15571559

15581560
// no need to read and scan block, if block was created before
15591561
// our wallet birthday (as adjusted for block time variability)
@@ -1563,7 +1565,7 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
15631565
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
15641566
double dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
15651567
double dProgressTip = GuessVerificationProgress(chainParams.TxData(), chainActive.Tip());
1566-
while (pindex)
1568+
while (pindex && !fAbortRescan)
15671569
{
15681570
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
15691571
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((GuessVerificationProgress(chainParams.TxData(), pindex) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
@@ -1585,7 +1587,12 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
15851587
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
15861588
}
15871589
}
1590+
if (pindex && fAbortRescan) {
1591+
LogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
1592+
}
15881593
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
1594+
1595+
fScanningWallet = false;
15891596
}
15901597
return ret;
15911598
}

src/wallet/wallet.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
651651
{
652652
private:
653653
static std::atomic<bool> fFlushScheduled;
654+
std::atomic<bool> fAbortRescan;
655+
std::atomic<bool> fScanningWallet;
654656

655657
/**
656658
* Select a set of coins such that nValueRet >= nTargetValue and at least
@@ -779,6 +781,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
779781
nTimeFirstKey = 0;
780782
fBroadcastTransactions = false;
781783
nRelockTime = 0;
784+
fAbortRescan = false;
785+
fScanningWallet = false;
782786
}
783787

784788
std::map<uint256, CWalletTx> mapWallet;
@@ -823,6 +827,13 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
823827
void UnlockAllCoins();
824828
void ListLockedCoins(std::vector<COutPoint>& vOutpts);
825829

830+
/*
831+
* Rescan abort properties
832+
*/
833+
void AbortRescan() { fAbortRescan = true; }
834+
bool IsAbortingRescan() { return fAbortRescan; }
835+
bool IsScanning() { return fScanningWallet; }
836+
826837
/**
827838
* keystore implementation
828839
* Generate a new key

0 commit comments

Comments
 (0)