@@ -870,51 +870,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
870
870
wtx.nTimeReceived = GetAdjustedTime ();
871
871
wtx.nOrderPos = IncOrderPosNext (&walletdb);
872
872
wtxOrdered.insert (make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
873
-
874
- wtx.nTimeSmart = wtx.nTimeReceived ;
875
- if (!wtxIn.hashUnset ())
876
- {
877
- if (mapBlockIndex.count (wtxIn.hashBlock ))
878
- {
879
- int64_t latestNow = wtx.nTimeReceived ;
880
- int64_t latestEntry = 0 ;
881
- {
882
- // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
883
- int64_t latestTolerated = latestNow + 300 ;
884
- const TxItems & txOrdered = wtxOrdered;
885
- for (TxItems::const_reverse_iterator it = txOrdered.rbegin (); it != txOrdered.rend (); ++it)
886
- {
887
- CWalletTx *const pwtx = (*it).second .first ;
888
- if (pwtx == &wtx)
889
- continue ;
890
- CAccountingEntry *const pacentry = (*it).second .second ;
891
- int64_t nSmartTime;
892
- if (pwtx)
893
- {
894
- nSmartTime = pwtx->nTimeSmart ;
895
- if (!nSmartTime)
896
- nSmartTime = pwtx->nTimeReceived ;
897
- }
898
- else
899
- nSmartTime = pacentry->nTime ;
900
- if (nSmartTime <= latestTolerated)
901
- {
902
- latestEntry = nSmartTime;
903
- if (nSmartTime > latestNow)
904
- latestNow = nSmartTime;
905
- break ;
906
- }
907
- }
908
- }
909
-
910
- int64_t blocktime = mapBlockIndex[wtxIn.hashBlock ]->GetBlockTime ();
911
- wtx.nTimeSmart = std::max (latestEntry, std::min (blocktime, latestNow));
912
- }
913
- else
914
- LogPrintf (" AddToWallet(): found %s in block %s not in index\n " ,
915
- wtxIn.GetHash ().ToString (),
916
- wtxIn.hashBlock .ToString ());
917
- }
873
+ wtx.nTimeSmart = ComputeTimeSmart (wtx);
918
874
AddToSpends (hash);
919
875
}
920
876
@@ -3472,6 +3428,71 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
3472
3428
mapKeyBirth[it->first ] = it->second ->GetBlockTime () - TIMESTAMP_WINDOW; // block times can be 2h off
3473
3429
}
3474
3430
3431
+ /* *
3432
+ * Compute smart timestamp for a transaction being added to the wallet.
3433
+ *
3434
+ * Logic:
3435
+ * - If sending a transaction, assign its timestamp to the current time.
3436
+ * - If receiving a transaction outside a block, assign its timestamp to the
3437
+ * current time.
3438
+ * - If receiving a block with a future timestamp, assign all its (not already
3439
+ * known) transactions' timestamps to the current time.
3440
+ * - If receiving a block with a past timestamp, before the most recent known
3441
+ * transaction (that we care about), assign all its (not already known)
3442
+ * transactions' timestamps to the same timestamp as that most-recent-known
3443
+ * transaction.
3444
+ * - If receiving a block with a past timestamp, but after the most recent known
3445
+ * transaction, assign all its (not already known) transactions' timestamps to
3446
+ * the block time.
3447
+ *
3448
+ * For more information see CWalletTx::nTimeSmart,
3449
+ * https://bitcointalk.org/?topic=54527, or
3450
+ * https://github.com/bitcoin/bitcoin/pull/1393.
3451
+ */
3452
+ unsigned int CWallet::ComputeTimeSmart (const CWalletTx& wtx) const
3453
+ {
3454
+ unsigned int nTimeSmart = wtx.nTimeReceived ;
3455
+ if (!wtx.hashUnset ()) {
3456
+ if (mapBlockIndex.count (wtx.hashBlock )) {
3457
+ int64_t latestNow = wtx.nTimeReceived ;
3458
+ int64_t latestEntry = 0 ;
3459
+
3460
+ // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
3461
+ int64_t latestTolerated = latestNow + 300 ;
3462
+ const TxItems& txOrdered = wtxOrdered;
3463
+ for (auto it = txOrdered.rbegin (); it != txOrdered.rend (); ++it) {
3464
+ CWalletTx* const pwtx = it->second .first ;
3465
+ if (pwtx == &wtx) {
3466
+ continue ;
3467
+ }
3468
+ CAccountingEntry* const pacentry = it->second .second ;
3469
+ int64_t nSmartTime;
3470
+ if (pwtx) {
3471
+ nSmartTime = pwtx->nTimeSmart ;
3472
+ if (!nSmartTime) {
3473
+ nSmartTime = pwtx->nTimeReceived ;
3474
+ }
3475
+ } else {
3476
+ nSmartTime = pacentry->nTime ;
3477
+ }
3478
+ if (nSmartTime <= latestTolerated) {
3479
+ latestEntry = nSmartTime;
3480
+ if (nSmartTime > latestNow) {
3481
+ latestNow = nSmartTime;
3482
+ }
3483
+ break ;
3484
+ }
3485
+ }
3486
+
3487
+ int64_t blocktime = mapBlockIndex[wtx.hashBlock ]->GetBlockTime ();
3488
+ nTimeSmart = std::max (latestEntry, std::min (blocktime, latestNow));
3489
+ } else {
3490
+ LogPrintf (" %s: found %s in block %s not in index\n " , __func__, wtx.GetHash ().ToString (), wtx.hashBlock .ToString ());
3491
+ }
3492
+ }
3493
+ return nTimeSmart;
3494
+ }
3495
+
3475
3496
bool CWallet::AddDestData (const CTxDestination &dest, const std::string &key, const std::string &value)
3476
3497
{
3477
3498
if (boost::get<CNoDestination>(&dest))
0 commit comments