-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Factor out CWallet::nTimeSmart computation into a method. #9369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
jonasschnelli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
utACK 406e975
src/wallet/wallet.cpp
Outdated
| int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); | ||
| nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | ||
| } else | ||
| LogPrintf("GetTimeSmart(): found %s in block %s not in index\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use __func__ here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done.
406e975 to
022267a
Compare
|
utACK 022267a |
| wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); | ||
|
|
||
| wtx.nTimeSmart = wtx.nTimeReceived; | ||
| if (!wtxIn.hashUnset()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code is testing hashUnset() of wtxIn. But the new code is checking hashUnset() of wtx. Is it the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
|
ACK 022267a |
|
utACK 406e975 |
src/wallet/wallet.cpp
Outdated
| mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off | ||
| } | ||
|
|
||
| unsigned int CWallet::GetTimeSmart(const CWalletTx& wtx) const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes most sense here next to CWallet::GetKeyBirthTimes. It accesses CWallet::mapBlockIndex and scans the CWallet::wtxOrdered field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Never mind part of comment above about mapBlockIndex, which is a global.)
a283b62 to
212a686
Compare
|
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. |
Rename GetTimeSmart method to ComputeTimeSmart as suggested by @JeremyRubin in bitcoin#9369 (comment)
1c474c2 to
b75a9b9
Compare
kallewoof
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slight ACK b75a9b9 -- would be useful with some test(s) to check this still works as intended, but not sure if possible.
| } | ||
|
|
||
| /** | ||
| * Compute smart timestamp for a transaction being added to the wallet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
src/wallet/wallet.cpp
Outdated
|
|
||
| int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); | ||
| nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | ||
| } else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our own coding style document says the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added braces in cb48cd7
ryanofsky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be useful with some test(s) to check this still works as intended, but not sure if possible.
Added tests in ed8a586
| } | ||
|
|
||
| /** | ||
| * Compute smart timestamp for a transaction being added to the wallet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
src/wallet/wallet.cpp
Outdated
|
|
||
| int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); | ||
| nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | ||
| } else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added braces in cb48cd7
cb48cd7 to
6587aa3
Compare
|
Rearranged commits to put tests before refactoring cb48cd7 -> 6587aa3 (atw-timesmart.6 -> atw-timesmart.7) |
6587aa3 to
2ffffb3
Compare
|
Rebased 6587aa3 -> 2ffffb3 (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 <[email protected]> c3f95ef commit message.
8196963 to
79b25ed
Compare
|
Fixed mocktime test bug in 8196963 and rebased 8196963 -> 79b25ed (pr/atw-timesmart.9 -> pr/atw-timesmart.10) because of conflicts with pwalletMain renames in #9775. Reduced test boilerplate in 5975523 and squashed 5975523 -> 630fc54 (pr/atw-timesmart.11 -> pr/atw-timesmart.12) |
5975523 to
630fc54
Compare
630fc54 Clean up braces in CWallet::ComputeTimeSmart (Russell Yanofsky) 6c996c2 Add documentation describing CWallet::nTimeSmart. (Russell Yanofsky) 1f98abe Factor out CWallet::nTimeSmart computation into a method. (Russell Yanofsky) c6b82d1 Add tests for CWalletTx::nTimeSmart (Russell Yanofsky) Tree-SHA512: 457a30251e572cf20dac0198af1a94128d269b1e0ce6605a213d56fc14d85c84a0a494e3dcbb18c201c4f39e6f7b000bd9cb6f283930d8452e4bb93ba406f8d4
jnewbery
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of minor nits.
utACK 630fc54
| int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); | ||
| nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | ||
| } else { | ||
| LogPrintf("%s: found %s in block %s not in index\n", __func__, wtx.GetHash().ToString(), wtx.hashBlock.ToString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
|
|
||
| int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); | ||
| nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
… method. 630fc54 Clean up braces in CWallet::ComputeTimeSmart (Russell Yanofsky) 6c996c2 Add documentation describing CWallet::nTimeSmart. (Russell Yanofsky) 1f98abe Factor out CWallet::nTimeSmart computation into a method. (Russell Yanofsky) c6b82d1 Add tests for CWalletTx::nTimeSmart (Russell Yanofsky) Tree-SHA512: 457a30251e572cf20dac0198af1a94128d269b1e0ce6605a213d56fc14d85c84a0a494e3dcbb18c201c4f39e6f7b000bd9cb6f283930d8452e4bb93ba406f8d4
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::GetTimeSmartandCWalletTx::nTimeSmart