No change in behavior, this change just pulls some code out of CWallet::AddToWallet that was making it very long into a separate method.
A followup commit also adds documentation describing CWallet::GetTimeSmart
and CWalletTx::nTimeSmart
No change in behavior, this change just pulls some code out of CWallet::AddToWallet that was making it very long into a separate method.
A followup commit also adds documentation describing CWallet::GetTimeSmart
and CWalletTx::nTimeSmart
3289+ }
3290+
3291+ int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
3292+ nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
3293+ } else
3294+ LogPrintf("GetTimeSmart(): found %s in block %s not in index\n",
__func__
here.
843@@ -844,51 +844,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
844 wtx.nTimeReceived = GetAdjustedTime();
845 wtx.nOrderPos = IncOrderPosNext(&walletdb);
846 wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
847-
848- wtx.nTimeSmart = wtx.nTimeReceived;
849- if (!wtxIn.hashUnset())
hashUnset()
of wtxIn
. But the new code is checking hashUnset()
of wtx
. Is it the same?
wtxIn
with wtx
. Because this code is running in the case where fInsertedNew
is true, wtx
at this point is just a copy of wtxIn
with two extra fields set (nTimeReceived
and nOrderPos
)
3255@@ -3300,6 +3256,46 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
3256 mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
3257 }
3258
3259+unsigned int CWallet::GetTimeSmart(const CWalletTx& wtx) const
I think code style wise this would be improved by naming GetTimeSmart to RecomputeTimeSmart or ComputeTimeSmart because it’s not a simple accessor. If someone were to decide to call GetTimeSmart in place of accessing the cached value, the name wouldn’t imply the overhead otherwise.
Otherwise, makes sense to me, utACK as is for 212a686.
3279@@ -3324,6 +3280,67 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
3280 mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
3281 }
3282
3283+/**
3284+ * Compute smart timestamp for a transaction being added to the wallet.
nTimeSmart
describes what the smart timestamp is and the comment on ComputeTimeSmart
describes how a smart timestamp is computed, and both comments reference each other.
3333+ }
3334+ }
3335+
3336+ int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
3337+ nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
3338+ } else
if (..) { .. } else ..
, recommending instead if (..) { .. } else { .. }
, i.e. use braces in else
if braces were used in if
.
would be useful with some test(s) to check this still works as intended, but not sure if possible.
Added tests in ed8a586d0e97c5432c6d2b46c8cf4e3d4b203a5a
No change in behavior, this change just pulls some code out of
CWallet::AddToWallet that was making it very long into a separate method.
Most of the text comes from the 2012 Luke Dashjr <luke-jr+git@utopios.org>
c3f95ef commit message.
Fixed mocktime test bug in 819696357bb407196e6f01175f2d4459848c7d9b and rebased 819696357bb407196e6f01175f2d4459848c7d9b -> 79b25edd74053d7a655882d4c16bb5dba43c90a2 (pr/atw-timesmart.9 -> pr/atw-timesmart.10) because of conflicts with pwalletMain renames in #9775.
Reduced test boilerplate in 5975523d1f647fe7de96dd16d89e363052dc38cb and squashed 5975523d1f647fe7de96dd16d89e363052dc38cb -> 630fc549e28cb33eb11df1b4f951339bf8152e4f (pr/atw-timesmart.11 -> pr/atw-timesmart.12)
3511+ }
3512+
3513+ int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
3514+ nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
3515+ } else {
3516+ LogPrintf("%s: found %s in block %s not in index\n", __func__, wtx.GetHash().ToString(), wtx.hashBlock.ToString());
3510+ }
3511+ }
3512+
3513+ int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
3514+ nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
3515+ } else {
A couple of minor nits.
utACK 630fc549e28cb33eb11df1b4f951339bf8152e4f