mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Return a status enum from ScanForWalletTransactions
Return the failed block as an out var. This clarifies the outcome as the prior return value could be null due to user abort or failure.
This commit is contained in:
@@ -1613,8 +1613,9 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
|
||||
}
|
||||
|
||||
if (startBlock) {
|
||||
const CBlockIndex* const failedBlock = ScanForWalletTransactions(startBlock, nullptr, reserver, update);
|
||||
if (failedBlock) {
|
||||
const CBlockIndex* failedBlock;
|
||||
// TODO: this should take into account failure by ScanResult::USER_ABORT
|
||||
if (ScanResult::FAILURE == ScanForWalletTransactions(startBlock, nullptr, reserver, failedBlock, update)) {
|
||||
return failedBlock->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1;
|
||||
}
|
||||
}
|
||||
@@ -1626,18 +1627,20 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
|
||||
* from or to us. If fUpdate is true, found transactions that already
|
||||
* exist in the wallet will be updated.
|
||||
*
|
||||
* Returns null if scan was successful. Otherwise, if a complete rescan was not
|
||||
* possible (due to pruning or corruption), returns pointer to the most recent
|
||||
* block that could not be scanned.
|
||||
* @param[in] pindexStop if not a nullptr, the scan will stop at this block-index
|
||||
* @param[out] failed_block if FAILURE is returned, will be set to the most
|
||||
* recent block that could not be scanned, otherwise nullptr
|
||||
*
|
||||
* If pindexStop is not a nullptr, the scan will stop at the block-index
|
||||
* defined by pindexStop
|
||||
* @return ScanResult indicating success or failure of the scan. SUCCESS if
|
||||
* scan was successful. FAILURE if a complete rescan was not possible (due to
|
||||
* pruning or corruption). USER_ABORT if the rescan was aborted before it
|
||||
* could complete.
|
||||
*
|
||||
* Caller needs to make sure pindexStop (and the optional pindexStart) are on
|
||||
* @pre Caller needs to make sure pindexStop (and the optional pindexStart) are on
|
||||
* the main chain after to the addition of any new keys you want to detect
|
||||
* transactions for.
|
||||
*/
|
||||
const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const pindexStart, const CBlockIndex* const pindexStop, const WalletRescanReserver& reserver, bool fUpdate)
|
||||
CWallet::ScanResult CWallet::ScanForWalletTransactions(const CBlockIndex* const pindexStart, const CBlockIndex* const pindexStop, const WalletRescanReserver& reserver, const CBlockIndex*& failed_block, bool fUpdate)
|
||||
{
|
||||
int64_t nNow = GetTime();
|
||||
const CChainParams& chainParams = Params();
|
||||
@@ -1648,7 +1651,7 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
|
||||
}
|
||||
|
||||
const CBlockIndex* pindex = pindexStart;
|
||||
const CBlockIndex* ret = nullptr;
|
||||
failed_block = nullptr;
|
||||
|
||||
if (pindex) WalletLogPrintf("Rescan started from block %d...\n", pindex->nHeight);
|
||||
|
||||
@@ -1669,8 +1672,7 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
|
||||
}
|
||||
}
|
||||
double progress_current = progress_begin;
|
||||
while (pindex && !fAbortRescan && !ShutdownRequested())
|
||||
{
|
||||
while (pindex && !fAbortRescan && !ShutdownRequested()) {
|
||||
if (pindex->nHeight % 100 == 0 && progress_end - progress_begin > 0.0) {
|
||||
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), std::max(1, std::min(99, (int)((progress_current - progress_begin) / (progress_end - progress_begin) * 100))));
|
||||
}
|
||||
@@ -1686,14 +1688,14 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
|
||||
if (pindex && !chainActive.Contains(pindex)) {
|
||||
// Abort scan if current block is no longer active, to prevent
|
||||
// marking transactions as coming from the wrong block.
|
||||
ret = pindex;
|
||||
failed_block = pindex;
|
||||
break;
|
||||
}
|
||||
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
|
||||
SyncTransaction(block.vtx[posInBlock], pindex, posInBlock, fUpdate);
|
||||
}
|
||||
} else {
|
||||
ret = pindex;
|
||||
failed_block = pindex;
|
||||
}
|
||||
if (pindex == pindexStop) {
|
||||
break;
|
||||
@@ -1709,14 +1711,20 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
|
||||
}
|
||||
}
|
||||
}
|
||||
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 100); // hide progress dialog in GUI
|
||||
if (pindex && fAbortRescan) {
|
||||
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, progress_current);
|
||||
return ScanResult::USER_ABORT;
|
||||
} else if (pindex && ShutdownRequested()) {
|
||||
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", pindex->nHeight, progress_current);
|
||||
return ScanResult::USER_ABORT;
|
||||
}
|
||||
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 100); // hide progress dialog in GUI
|
||||
}
|
||||
return ret;
|
||||
if (failed_block) {
|
||||
return ScanResult::FAILURE;
|
||||
} else {
|
||||
return ScanResult::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
void CWallet::ReacceptWalletTransactions()
|
||||
@@ -4166,11 +4174,11 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
||||
nStart = GetTimeMillis();
|
||||
{
|
||||
WalletRescanReserver reserver(walletInstance.get());
|
||||
if (!reserver.reserve()) {
|
||||
const CBlockIndex* stop_block;
|
||||
if (!reserver.reserve() || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions(pindexRescan, nullptr, reserver, stop_block, true))) {
|
||||
InitError(_("Failed to rescan the wallet during initialization"));
|
||||
return nullptr;
|
||||
}
|
||||
walletInstance->ScanForWalletTransactions(pindexRescan, nullptr, reserver, true);
|
||||
}
|
||||
walletInstance->WalletLogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nStart);
|
||||
walletInstance->ChainStateFlushed(chainActive.GetLocator());
|
||||
|
||||
Reference in New Issue
Block a user