-
Notifications
You must be signed in to change notification settings - Fork 37.7k
test: Add and use ElapseTime helper #32430
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
base: master
Are you sure you want to change the base?
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32430. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
fa123d4
to
fae29c5
Compare
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.
Concept ACK - was thinking of something similar when reviewing 41479ed
(#32414)
Sorry for the force-pushes, but this uncovered pre-existing issues in the fuzz tests that were not using mocktime during init. So I went ahead and fixed those as well in a separate commit. Should be good now, hopefully 😅 |
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.
ACK fae29c5
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
Went ahead and cherry-picked out the fuzz-fixes into #32927. Should be easy to (re-)review that one. |
fad3690
to
fa1dde3
Compare
rebased to drop merged commits and included some minimal fixups |
fa55137
to
fafcff1
Compare
fafcff1
to
faaaaac
Compare
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 for fixing these, left quite a few comments, hope you'll find them useful
src/test/fuzz/headerssync.cpp
Outdated
|
||
CBlockHeader genesis_header{Params().GenesisBlock()}; | ||
CBlockIndex start_index(genesis_header); | ||
|
||
if (mock_time < start_index.GetMedianTimePast()) return; | ||
SetMockTime(mock_time); | ||
SetMockTime(ConsumeTime(fuzzed_data_provider, /*min=*/start_index.GetMedianTimePast())); |
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.
👍 for using the built-in min
instead.
Could we add it to the commit message that this isn't just a simple refactor (and not just about the return value), but as @hodlinator stated in a different PR where he did the same:
That way we can always proceed instead of just exiting the fuzz target, wasting cycles. Also reduces code complexity.
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.
thx, split up into a new commit
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.
typo in commit message:
no state is leaked between test cases
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.
thx, fixed
src/test/util/time.h
Outdated
void operator()(std::chrono::milliseconds d) | ||
{ | ||
Assert(d >= 0s); // Steady time can only move forward. |
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: the code isn't in exact alignment with the comment - and since we are calling it with 0
, maybe something like:
Assert(d >= 0s); // Steady time can only move forward. | |
Assert(d >= 0ms); // Steady time cannot move backward. |
(nit2: I know it's the same, but to clarify that we don't just mean that time-jumps cannot be 0.5 seconds, we might want to use 0ms
instead)
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.
thx, reworded comment a bit
src/test/util/time.h
Outdated
NodeSeconds m_t{std::chrono::seconds::max()}; | ||
|
||
public: | ||
/** Initialize with initial time */ |
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: I understand that it's meant to be symmetric with ElapseSteady
, but I don't find the comment helpful as it is.
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.
thx, reworded comment
@@ -167,7 +168,7 @@ BOOST_FIXTURE_TEST_CASE(stale_tip_peer_management, OutboundTest) | |||
BOOST_CHECK(node->fDisconnect == false); | |||
} | |||
|
|||
SetMockTime(time_later); | |||
elapse_time(delta); |
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.
any reason for defining the delta
so far away from first usage?
elapse_time(delta); | |
const auto delta{3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s}; | |
elapse_time(delta); |
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.
Just to make review easier, because you agree that the commit is already large in #32430 (comment). Will leave as-is for now.
@@ -44,7 +45,7 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize) | |||
|
|||
auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman); | |||
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman); | |||
SetMockTime(1610000000); // any time to successfully reset ibd | |||
ElapseTime elapse_time{1610000000s}; // any time to successfully reset ibd |
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.
do we still need to keep these constants or is there maybe a cleaner way now?
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.
seems unrelated? But i am happy to review a pull request changing it
src/test/util/time.h
Outdated
|
||
public: | ||
/** Initialize with initial time */ | ||
ElapseTime(NodeSeconds init_time) { set(init_time); } |
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: we might as well make the constructors explicit
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.
thx, done
src/bench/util_time.cpp
Outdated
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.
faaaaac is quite big...
And some of the changes are non-trivial, if you can, please consider doing it in multiple commits
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 is just 50 lines, so it should be manageable. Anyone is free to review it file-by-file, if they want to split it up, but I am not sure about splitting it up too much?
This is less code and also required for the next commit.
This is a bit more type-safe than a raw i64.
This makes it easier to use mock-time in tests. Also, it resets the global mocktime, so that no state is leaked between test cases. Also, it resets the global steady mock-time for the same reason.
This is required in the next commit.
faaaaac
to
fa80e75
Compare
Force pushed with some minor doc-changes and small refactoring in |
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.
code review ACK fa80e75
🐙 This pull request conflicts with the target branch and needs rebase. |
Currently mocktime is written to a global, which may leak between sub-tests (albeit some tests try to reset the mocktime on a best-effort basis). Also, when advancing it, one has to keep a counter variable around.
Fix both issues by a new
ElapseTime
helper, which resets the mocktime once it goes out of scope. Also, it has a method to advance the mocktime by a delta.