Skip to content

Conversation

@ryanofsky
Copy link
Contributor

@ryanofsky ryanofsky commented Dec 16, 2016

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

Copy link
Contributor

@jonasschnelli jonasschnelli left a 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

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",
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done.

@sipa
Copy link
Member

sipa commented Dec 16, 2016

utACK 022267a

wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));

wtx.nTimeSmart = wtx.nTimeReceived;
if (!wtxIn.hashUnset())
Copy link
Contributor

@paveljanik paveljanik Dec 17, 2016

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?

Copy link
Contributor Author

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)

@paveljanik
Copy link
Contributor

ACK 022267a

@fanquake
Copy link
Member

utACK 406e975

mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
}

unsigned int CWallet::GetTimeSmart(const CWalletTx& wtx) const
Copy link
Member

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.

Copy link
Contributor Author

@ryanofsky ryanofsky Dec 19, 2016

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.

Copy link
Contributor Author

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.)

@JeremyRubin
Copy link
Contributor

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.

ryanofsky added a commit to ryanofsky/bitcoin that referenced this pull request Jan 19, 2017
Rename GetTimeSmart method to ComputeTimeSmart as suggested by @JeremyRubin in
bitcoin#9369 (comment)
@ryanofsky
Copy link
Contributor Author

Renamed method to ComputeTimeSmart in 1c474c2 and squashed 1c474c2 -> b75a9b9.

Copy link
Contributor

@kallewoof kallewoof left a 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.
Copy link
Contributor

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.)

Copy link
Contributor Author

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.

Copy link
Contributor

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.


int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
} else
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor Author

@ryanofsky ryanofsky Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added braces in cb48cd7

Copy link
Contributor Author

@ryanofsky ryanofsky left a 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.
Copy link
Contributor Author

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.


int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
} else
Copy link
Contributor Author

@ryanofsky ryanofsky Feb 10, 2017

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
Copy link
Contributor Author

Rearranged commits to put tests before refactoring cb48cd7 -> 6587aa3 (atw-timesmart.6 -> atw-timesmart.7)

@ryanofsky
Copy link
Contributor Author

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.
@ryanofsky
Copy link
Contributor Author

ryanofsky commented Mar 3, 2017

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)

@laanwj laanwj merged commit 630fc54 into bitcoin:master Mar 7, 2017
laanwj added a commit that referenced this pull request Mar 7, 2017
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
Copy link
Contributor

@jnewbery jnewbery left a 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());
Copy link
Contributor

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 {
Copy link
Contributor

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.

PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Feb 5, 2019
… 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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants