Skip to content

refactor: Make adjusted time type safe #25786

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

Merged
merged 2 commits into from
Aug 22, 2022

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Aug 5, 2022

This makes follow-ups easier to review. Also, it makes sense by itself.

MacroFake added 2 commits August 5, 2022 14:45
To be used in the next commit
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 5, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25862 (refactor, kernel: Remove gArgs accesses from dbwrapper and txdb by ryanofsky)
  • #25781 (Remove almost all blockstorage globals by MarcoFalke)
  • #25704 (refactor: Remove almost all validation option globals by MarcoFalke)
  • #25623 ([kernel 3e/n] Decouple CDBWrapper and its kernel users from ArgsManager by dongcarl)

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.

Copy link
Member

@dergoegge dergoegge left a comment

Choose a reason for hiding this comment

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

Concept ACK

@@ -275,6 +276,11 @@ class CBlockIndex
*/
bool HaveTxsDownloaded() const { return nChainTx != 0; }

NodeSeconds Time() const
Copy link
Member

Choose a reason for hiding this comment

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

nit: This will end up renaming GetBlockTime(), right? (assuming that in the long run GetBlockTime() would be entirely replaced by Time())

we could avoid that with something like the following:

    template <typename T = int64_t>
    auto GetBlockTime() const -> T
    {
        static_assert(std::is_same<T, int64_t>::value || std::is_same<T, NodeSeconds>::value);

        if constexpr (std::is_same<T, int64_t>::value) {
            return (T)nTime;
        } else {
            return NodeSeconds{std::chrono::seconds{nTime}};
        }
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

I picked a different name because it felt weird to repeat the class type in the method again. That is, calling block.GetBlockTime() seems better replaced by block.GetTime() or block.Time().

Copy link
Contributor

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

Code review ACK eeee5ad. Confirmed type changes and equivalent code changes only.

It's pretty hard to keep tract of all the different time functions. I think it would be really nice if all of the deprecated time functions could just be eliminated. Also if there are time functions that are never used (like ::Now()?) or only infrequently used I think it would be nice to drop them from util/time.h and just declare them locally close to where they are used to avoid having so many confusing similarly named util functions.

@maflcko
Copy link
Member Author

maflcko commented Aug 18, 2022

if there are time functions that are never used (like ::Now()?) ...

::Now is used, but can only be used by passing the template argument, so you'd have to grep for: git grep '\<Now<'

... or only infrequently used I think it would be nice to drop them from util/time.h and just declare them locally close to where they are used

I am doing this in other pull requests. For example, GetTimeMillis in #25499.

@fanquake fanquake merged commit 0f35f4d into bitcoin:master Aug 22, 2022
@maflcko maflcko deleted the 2208-time-type-💷 branch August 22, 2022 09:08
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 22, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Aug 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants