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
Makes sense. utACK 406e9759478e86d37cd4fca255841fd3291417c3
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",
You can use __func__ here.
Thanks, done.
utACK 022267ac634b13690414252c4deba7f499f98c63
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())
The old code is testing hashUnset() of wtxIn. But the new code is checking hashUnset() of wtx. Is it the same?
Yes, the change replaces this and other uses of 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)
utACK 406e975
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
Would it be an idea to put this on CWalletTx instead? A quick scan of the code doesn't reveal any accesses to CWallet fields.
I think this makes most sense here next to CWallet::GetKeyBirthTimes. It accesses CWallet::mapBlockIndex and scans the CWallet::wtxOrdered field.
(Never mind part of comment above about mapBlockIndex, which is a global.)
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.
Renamed method to ComputeTimeSmart in 1c474c253170ffa7ebc50abca93bf673c0ae0f4f and squashed 1c474c253170ffa7ebc50abca93bf673c0ae0f4f -> b75a9b95a43bf88e9e977fb9ad5bcb5e3266bbd9.
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.
I think an additional comment below this describing what a smart timestamp is would be helpful, even if the reader may deduce this from the logic section below. (It's not super clear to me, but seems to be a way to adapt the time stamp depending on various hints deduced from the state of the transaction.)
I don't think I understand how this should change. The comment on 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.
It just felt like I didn't have a good grasp what it was by reading the code. You're probably right that it's sufficient if it's described in the variable comment, though.
3333 | + } 3334 | + } 3335 | + 3336 | + int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); 3337 | + nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); 3338 | + } else
Nit / coding standard: E.g. Google coding style guide advises against if (..) { .. } else .., recommending instead if (..) { .. } else { .. }, i.e. use braces in else if braces were used in if.
Our own coding style document says the same.
Added braces in cb48cd7d720d7af466614652c1e2a56dcce068bc
Slight ACK b75a9b9 -- would be useful with some test(s) to check this still works as intended, but not sure if possible.
would be useful with some test(s) to check this still works as intended, but not sure if possible.
Added tests in ed8a586d0e97c5432c6d2b46c8cf4e3d4b203a5a
Rearranged commits to put tests before refactoring cb48cd7d720d7af466614652c1e2a56dcce068bc -> 6587aa3b293b89c81dd7a4a8cc142d6e0a5670f8 (atw-timesmart.6 -> atw-timesmart.7)
Rebased 6587aa3b293b89c81dd7a4a8cc142d6e0a5670f8 -> 2ffffb36e138a5e060b6b3eaada3080ee3c4de75 (atw-timesmart.7 -> atw-timesmart.8) because of conflict in wallet.h with #9108.
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());
I mildly dislike that this log (which is about adding a transaction to the wallet and was originally in AddToWallet()) is now in ComputeTimeSmart() and will print with the name of the function, since it's not really anything to do with computing nTimeSmart. Does it make sense to leave this log in the AddToWallet() function somehow?
3510 | + } 3511 | + } 3512 | + 3513 | + int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); 3514 | + nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); 3515 | + } else {
Style preference: consider not initializing nTimeSmart to wtx.nTimeReceived and only setting it here (to emphasise that nTimeSmart is only set to nTimeReceived when the transaction is received outside a block.
A couple of minor nits.
utACK 630fc549e28cb33eb11df1b4f951339bf8152e4f