Skip to content

Replace duration casts with chrono literals

SlowRiot requested to merge SlowRiot/bitcoin-cash-node:chrono_literals into master

This MR is on top of !1501 (merged) - please ensure that is merged before attempting to review this set of changes.

Motivation

Having moved from Boost to std::chrono, we can make use of chrono literals, which make working with chrono durations far cleaner, and more intuitive than explicit duration casts. When using chrono literals, it is far easier to reason about durations while reading the code, and accidental mistakes due to incorrect units are far less likely.

This also presents an opportunity to clean up code which casts back and forth between integer representations and chrono durations, simplifying logic using durations.

This MR

  • Replace verbose formulations such as std::chrono::seconds(2 * 60) with chrono literals like 2min where appropriate.
  • Clean up scheduler tests:
    • Replace the MicroSleep function (used only once) with the equally clear this_thread::sleep_for using a microsecond literal.
    • The random microsecond function actually returns microseconds rather than integers
    • RNG is captured in the random microsecond lambda rather than passed as a parameter

Future work

A number of functions dealing with time in the code, especially the scheduler, accept durations in fixed integer format. Ideally this would be adapted to accept chrono durations directly, which would improve clarity of intention, and reduce the need for chrono casts, and casting back to integer with .counts. Such a change is out of scope for this MR.

Test plan

ninja all check_bitcoin
Edited by SlowRiot

Merge request reports