Skip to content
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

refactor: use more gsl::not_null in utils.h and deterministicmns.h #5651

Merged
merged 1 commit into from
Nov 10, 2023

Conversation

PastaPastaPasta
Copy link
Member

Issue being fixed or feature implemented

Use not_null if the function would crash if given a nullptr

What was done?

Refactored to use gsl::not_null

How Has This Been Tested?

Compiled

Breaking Changes

Should be none

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

UdjinM6
UdjinM6 previously approved these changes Oct 27, 2023
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK (with a few notes)

if constexpr (std::is_same<T, CBLSPublicKey>()) {
assert(false);
}
static_assert(!std::is_same<T, CBLSPublicKey>(), "GetUniquePropertyHash cannot be templated against CBLSPublicKey");
Copy link

Choose a reason for hiding this comment

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

nit: unrelated

src/evo/deterministicmns.cpp Show resolved Hide resolved
src/llmq/utils.cpp Show resolved Hide resolved
@knst
Copy link
Collaborator

knst commented Oct 27, 2023

Conflicting PRs: [5590]

Please, review #5590 first

@github-actions
Copy link

This pull request has conflicts, please rebase.

@@ -46,38 +47,38 @@ namespace utils
{

// includes members which failed DKG
std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, bool reset_cache = false);
std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool reset_cache = false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

so far as this and many other places requires not-null anyway, why even don't change const CBlockIndex*> to the const CBlockIndex&` ?

For reference, couple PR with bitcoin's similar changes of CBlockIndex* to CBlockIndex&. Bitcoin doesn't change it everywhere, but only when it is obviously non-nullptr, such as:
https://github.com/bitcoin/bitcoin/pull/25677/files
https://github.com/bitcoin/bitcoin/pull/25016/files

Copy link
Member Author

Choose a reason for hiding this comment

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

Well; so one benefit imo is that dereferencing a nullptr is undefined behavior not guaranteed to be caught at runtime; additionally, gsl::not_null doesn't require additional syntax to pass to a later func that needs a ptr or additional syntax when passing in a ptr. In bitcoin PR you mention it appears they force to use something like *Assert(m_chain.Tip() which is significantly more typing :D

Copy link
Collaborator

@knst knst Oct 30, 2023

Choose a reason for hiding this comment

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

well, *Assert(...) is 7 bytes shorter than gsl::not_null<...>(...)

And indeed, you can avoid UB by validating it before converting to reference.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah; but *Assert you have to have at every call site instead of just function definition. They're both generally valid approaches imo

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, but once you convert pointer to reference, you can assume that all recursive calls; submethods; helpers, etc are never have nullptr anymore; but for case of pointer - you need to add gsl:not_null to every method do show that is not nullptr.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well yes; same as every method needs to be declared as a reference. Once a not_null is constructed it is zero cost to pass it to another not_null function

knst
knst previously approved these changes Oct 30, 2023
Copy link
Collaborator

@knst knst left a comment

Choose a reason for hiding this comment

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

utACK, need to rebase

gsl::not_null actually looks good for most of usages in this PR

Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK

Copy link
Collaborator

@knst knst left a comment

Choose a reason for hiding this comment

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

utACK

@PastaPastaPasta PastaPastaPasta merged commit 6e639c7 into dashpay:develop Nov 10, 2023
5 of 10 checks passed
@PastaPastaPasta PastaPastaPasta deleted the more-not-null branch November 10, 2023 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants