@@ -884,7 +884,7 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
884
884
return false ;
885
885
}
886
886
887
- CWalletTx* CWallet::AddToWallet (CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx, bool fFlushOnClose )
887
+ CWalletTx* CWallet::AddToWallet (CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx, bool fFlushOnClose , bool rescanning_old_block )
888
888
{
889
889
LOCK (cs_wallet);
890
890
@@ -914,7 +914,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
914
914
wtx.nTimeReceived = chain ().getAdjustedTime ();
915
915
wtx.nOrderPos = IncOrderPosNext (&batch);
916
916
wtx.m_it_wtxOrdered = wtxOrdered.insert (std::make_pair (wtx.nOrderPos , &wtx));
917
- wtx.nTimeSmart = ComputeTimeSmart (wtx);
917
+ wtx.nTimeSmart = ComputeTimeSmart (wtx, rescanning_old_block );
918
918
AddToSpends (hash, &batch);
919
919
}
920
920
@@ -1031,7 +1031,7 @@ bool CWallet::LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx
1031
1031
return true ;
1032
1032
}
1033
1033
1034
- bool CWallet::AddToWalletIfInvolvingMe (const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool fUpdate )
1034
+ bool CWallet::AddToWalletIfInvolvingMe (const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool fUpdate , bool rescanning_old_block )
1035
1035
{
1036
1036
const CTransaction& tx = *ptx;
1037
1037
{
@@ -1069,7 +1069,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Co
1069
1069
1070
1070
// Block disconnection override an abandoned tx as unconfirmed
1071
1071
// which means user may have to call abandontransaction again
1072
- return AddToWallet (MakeTransactionRef (tx), confirm, /* update_wtx= */ nullptr , /* fFlushOnClose= */ false );
1072
+ return AddToWallet (MakeTransactionRef (tx), confirm, /* update_wtx= */ nullptr , /* fFlushOnClose= */ false , rescanning_old_block );
1073
1073
}
1074
1074
}
1075
1075
return false ;
@@ -1198,9 +1198,9 @@ void CWallet::MarkConflicted(const uint256& hashBlock, int conflicting_height, c
1198
1198
}
1199
1199
}
1200
1200
1201
- void CWallet::SyncTransaction (const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool update_tx)
1201
+ void CWallet::SyncTransaction (const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool update_tx, bool rescanning_old_block )
1202
1202
{
1203
- if (!AddToWalletIfInvolvingMe (ptx, confirm, update_tx))
1203
+ if (!AddToWalletIfInvolvingMe (ptx, confirm, update_tx, rescanning_old_block ))
1204
1204
return ; // Not one of ours
1205
1205
1206
1206
// If a transaction changes 'conflicted' state, that changes the balance
@@ -1643,7 +1643,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
1643
1643
break ;
1644
1644
}
1645
1645
for (size_t posInBlock = 0 ; posInBlock < block.vtx .size (); ++posInBlock) {
1646
- SyncTransaction (block.vtx [posInBlock], {CWalletTx::Status::CONFIRMED, block_height, block_hash, (int )posInBlock}, fUpdate );
1646
+ SyncTransaction (block.vtx [posInBlock], {CWalletTx::Status::CONFIRMED, block_height, block_hash, (int )posInBlock}, fUpdate , /* rescanning_old_block */ true );
1647
1647
}
1648
1648
// scan succeeded, record block as most recent successfully scanned
1649
1649
result.last_scanned_block = block_hash;
@@ -2398,38 +2398,43 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const {
2398
2398
* https://bitcointalk.org/?topic=54527, or
2399
2399
* https://github.com/bitcoin/bitcoin/pull/1393.
2400
2400
*/
2401
- unsigned int CWallet::ComputeTimeSmart (const CWalletTx& wtx) const
2401
+ unsigned int CWallet::ComputeTimeSmart (const CWalletTx& wtx, bool rescanning_old_block ) const
2402
2402
{
2403
2403
unsigned int nTimeSmart = wtx.nTimeReceived ;
2404
2404
if (!wtx.isUnconfirmed () && !wtx.isAbandoned ()) {
2405
2405
int64_t blocktime;
2406
- if (chain ().findBlock (wtx.m_confirm .hashBlock , FoundBlock ().time (blocktime))) {
2407
- int64_t latestNow = wtx.nTimeReceived ;
2408
- int64_t latestEntry = 0 ;
2409
-
2410
- // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
2411
- int64_t latestTolerated = latestNow + 300 ;
2412
- const TxItems& txOrdered = wtxOrdered;
2413
- for (auto it = txOrdered.rbegin (); it != txOrdered.rend (); ++it) {
2414
- CWalletTx* const pwtx = it->second ;
2415
- if (pwtx == &wtx) {
2416
- continue ;
2417
- }
2418
- int64_t nSmartTime;
2419
- nSmartTime = pwtx->nTimeSmart ;
2420
- if (!nSmartTime) {
2421
- nSmartTime = pwtx->nTimeReceived ;
2422
- }
2423
- if (nSmartTime <= latestTolerated) {
2424
- latestEntry = nSmartTime;
2425
- if (nSmartTime > latestNow) {
2426
- latestNow = nSmartTime;
2406
+ int64_t block_max_time;
2407
+ if (chain ().findBlock (wtx.m_confirm .hashBlock , FoundBlock ().time (blocktime).maxTime (block_max_time))) {
2408
+ if (rescanning_old_block) {
2409
+ nTimeSmart = block_max_time;
2410
+ } else {
2411
+ int64_t latestNow = wtx.nTimeReceived ;
2412
+ int64_t latestEntry = 0 ;
2413
+
2414
+ // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
2415
+ int64_t latestTolerated = latestNow + 300 ;
2416
+ const TxItems& txOrdered = wtxOrdered;
2417
+ for (auto it = txOrdered.rbegin (); it != txOrdered.rend (); ++it) {
2418
+ CWalletTx* const pwtx = it->second ;
2419
+ if (pwtx == &wtx) {
2420
+ continue ;
2421
+ }
2422
+ int64_t nSmartTime;
2423
+ nSmartTime = pwtx->nTimeSmart ;
2424
+ if (!nSmartTime) {
2425
+ nSmartTime = pwtx->nTimeReceived ;
2426
+ }
2427
+ if (nSmartTime <= latestTolerated) {
2428
+ latestEntry = nSmartTime;
2429
+ if (nSmartTime > latestNow) {
2430
+ latestNow = nSmartTime;
2431
+ }
2432
+ break ;
2427
2433
}
2428
- break ;
2429
2434
}
2430
- }
2431
2435
2432
- nTimeSmart = std::max (latestEntry, std::min (blocktime, latestNow));
2436
+ nTimeSmart = std::max (latestEntry, std::min (blocktime, latestNow));
2437
+ }
2433
2438
} else {
2434
2439
WalletLogPrintf (" %s: found %s in block %s not in index\n " , __func__, wtx.GetHash ().ToString (), wtx.m_confirm .hashBlock .ToString ());
2435
2440
}
0 commit comments