@@ -1613,8 +1613,9 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
1613
1613
}
1614
1614
1615
1615
if (startBlock) {
1616
- const CBlockIndex* const failedBlock = ScanForWalletTransactions (startBlock, nullptr , reserver, update);
1617
- if (failedBlock) {
1616
+ const CBlockIndex* failedBlock;
1617
+ // TODO: this should take into account failure by ScanResult::USER_ABORT
1618
+ if (ScanResult::FAILURE == ScanForWalletTransactions (startBlock, nullptr , reserver, failedBlock, update)) {
1618
1619
return failedBlock->GetBlockTimeMax () + TIMESTAMP_WINDOW + 1 ;
1619
1620
}
1620
1621
}
@@ -1626,18 +1627,20 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
1626
1627
* from or to us. If fUpdate is true, found transactions that already
1627
1628
* exist in the wallet will be updated.
1628
1629
*
1629
- * Returns null if scan was successful. Otherwise, if a complete rescan was not
1630
- * possible (due to pruning or corruption), returns pointer to the most recent
1631
- * block that could not be scanned.
1630
+ * @param[in] pindexStop if not a nullptr, the scan will stop at this block-index
1631
+ * @param[out] failed_block if FAILURE is returned, will be set to the most
1632
+ * recent block that could not be scanned, otherwise nullptr
1632
1633
*
1633
- * If pindexStop is not a nullptr, the scan will stop at the block-index
1634
- * defined by pindexStop
1634
+ * @return ScanResult indicating success or failure of the scan. SUCCESS if
1635
+ * scan was successful. FAILURE if a complete rescan was not possible (due to
1636
+ * pruning or corruption). USER_ABORT if the rescan was aborted before it
1637
+ * could complete.
1635
1638
*
1636
- * Caller needs to make sure pindexStop (and the optional pindexStart) are on
1639
+ * @pre Caller needs to make sure pindexStop (and the optional pindexStart) are on
1637
1640
* the main chain after to the addition of any new keys you want to detect
1638
1641
* transactions for.
1639
1642
*/
1640
- const CBlockIndex* CWallet::ScanForWalletTransactions (const CBlockIndex* const pindexStart, const CBlockIndex* const pindexStop, const WalletRescanReserver& reserver, bool fUpdate )
1643
+ CWallet::ScanResult CWallet::ScanForWalletTransactions (const CBlockIndex* const pindexStart, const CBlockIndex* const pindexStop, const WalletRescanReserver& reserver, const CBlockIndex*& failed_block , bool fUpdate )
1641
1644
{
1642
1645
int64_t nNow = GetTime ();
1643
1646
const CChainParams& chainParams = Params ();
@@ -1648,7 +1651,7 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
1648
1651
}
1649
1652
1650
1653
const CBlockIndex* pindex = pindexStart;
1651
- const CBlockIndex* ret = nullptr ;
1654
+ failed_block = nullptr ;
1652
1655
1653
1656
if (pindex) WalletLogPrintf (" Rescan started from block %d...\n " , pindex->nHeight );
1654
1657
@@ -1669,8 +1672,7 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
1669
1672
}
1670
1673
}
1671
1674
double progress_current = progress_begin;
1672
- while (pindex && !fAbortRescan && !ShutdownRequested ())
1673
- {
1675
+ while (pindex && !fAbortRescan && !ShutdownRequested ()) {
1674
1676
if (pindex->nHeight % 100 == 0 && progress_end - progress_begin > 0.0 ) {
1675
1677
ShowProgress (strprintf (" %s " + _ (" Rescanning..." ), GetDisplayName ()), std::max (1 , std::min (99 , (int )((progress_current - progress_begin) / (progress_end - progress_begin) * 100 ))));
1676
1678
}
@@ -1686,14 +1688,14 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
1686
1688
if (pindex && !chainActive.Contains (pindex)) {
1687
1689
// Abort scan if current block is no longer active, to prevent
1688
1690
// marking transactions as coming from the wrong block.
1689
- ret = pindex;
1691
+ failed_block = pindex;
1690
1692
break ;
1691
1693
}
1692
1694
for (size_t posInBlock = 0 ; posInBlock < block.vtx .size (); ++posInBlock) {
1693
1695
SyncTransaction (block.vtx [posInBlock], pindex, posInBlock, fUpdate );
1694
1696
}
1695
1697
} else {
1696
- ret = pindex;
1698
+ failed_block = pindex;
1697
1699
}
1698
1700
if (pindex == pindexStop) {
1699
1701
break ;
@@ -1709,14 +1711,20 @@ const CBlockIndex* CWallet::ScanForWalletTransactions(const CBlockIndex* const p
1709
1711
}
1710
1712
}
1711
1713
}
1714
+ ShowProgress (strprintf (" %s " + _ (" Rescanning..." ), GetDisplayName ()), 100 ); // hide progress dialog in GUI
1712
1715
if (pindex && fAbortRescan ) {
1713
1716
WalletLogPrintf (" Rescan aborted at block %d. Progress=%f\n " , pindex->nHeight , progress_current);
1717
+ return ScanResult::USER_ABORT;
1714
1718
} else if (pindex && ShutdownRequested ()) {
1715
1719
WalletLogPrintf (" Rescan interrupted by shutdown request at block %d. Progress=%f\n " , pindex->nHeight , progress_current);
1720
+ return ScanResult::USER_ABORT;
1716
1721
}
1717
- ShowProgress (strprintf (" %s " + _ (" Rescanning..." ), GetDisplayName ()), 100 ); // hide progress dialog in GUI
1718
1722
}
1719
- return ret;
1723
+ if (failed_block) {
1724
+ return ScanResult::FAILURE;
1725
+ } else {
1726
+ return ScanResult::SUCCESS;
1727
+ }
1720
1728
}
1721
1729
1722
1730
void CWallet::ReacceptWalletTransactions ()
@@ -4166,11 +4174,11 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
4166
4174
nStart = GetTimeMillis ();
4167
4175
{
4168
4176
WalletRescanReserver reserver (walletInstance.get ());
4169
- if (!reserver.reserve ()) {
4177
+ const CBlockIndex* stop_block;
4178
+ if (!reserver.reserve () || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions (pindexRescan, nullptr , reserver, stop_block, true ))) {
4170
4179
InitError (_ (" Failed to rescan the wallet during initialization" ));
4171
4180
return nullptr ;
4172
4181
}
4173
- walletInstance->ScanForWalletTransactions (pindexRescan, nullptr , reserver, true );
4174
4182
}
4175
4183
walletInstance->WalletLogPrintf (" Rescan completed in %15dms\n " , GetTimeMillis () - nStart);
4176
4184
walletInstance->ChainStateFlushed (chainActive.GetLocator ());
0 commit comments