Skip to content

refactor: make Get{Quorum, MinedCommitment, InstantSendLock*, *MN*} and friends return std::optional #6446

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

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

kwvg
Copy link
Collaborator

@kwvg kwvg commented Dec 3, 2024

Additional Information

Breaking Changes

Work in progress

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)

kwvg added 4 commits December 3, 2024 07:43
`GetQuorum` is a fail-able activity and some portions of the codebase
handle the fail state but others implicitly assume that they won't
fail (dereferencing without nullptr checks) or make the assumption
explicit (through placing `assert`s).

As some of these assumptions were within loops, care must be taken that
partial changes are not committed (unless the entity is discarded by the
caller after reporting the fail state).

By making it a `std::optional`, we separate fetching and using the value
as two separate steps with error handling in between, assumptions have
been documented through code comments but shouldn't cause the client
to crash altogether (crashes are harder to extract debug information
from if they're sporadic and occur on nodes running release binaries).
`std::move` is required as we're dealing with `std::unique_ptr` objects
@kwvg kwvg modified the milestones: 22, 22.1 Dec 3, 2024
Copy link

github-actions bot commented Dec 9, 2024

This pull request has conflicts, please rebase.

@kwvg kwvg modified the milestones: 22.1, 23 Jan 14, 2025
@DashCoreAutoGuix
Copy link

✅ Backport Verification - Issues Fixed

I found and fixed 1 issue:

  • Missing include: Added #include <optional> to src/evo/deterministicmns.h → Fixed CI build failures

Original Bitcoin commit: N/A (Dash-internal refactoring)
Fix commit pushed: c65a52fe0c
Reviewed commit hash: a197cb7bcf-verify-1753727224

✅ Analysis Summary

This PR refactors Dash's masternode and quorum management functions to return std::optional instead of raw pointers, which is a good practice for safer null handling. The main issue was that src/evo/deterministicmns.h was using std::optional without including the required header, causing build failures across multiple CI configurations.

Fix Applied:

  • Added #include <optional> to src/evo/deterministicmns.h

This should resolve the catastrophic CI failures (8 failures) that were occurring due to the missing include.

Note: This is a Dash-internal refactoring PR, not a Bitcoin backport, which is why no Bitcoin commit hash is available.

This fix should resolve the CI build failures. ✅

@@ -92,58 +92,58 @@ bool CDeterministicMNList::IsMNPoSeBanned(const CDeterministicMN& dmn)
return dmn.pdmnState->IsBanned();
}

CDeterministicMNCPtr CDeterministicMNList::GetMN(const uint256& proTxHash) const
std::optional<CDeterministicMNCPtr> CDeterministicMNList::GetMN(const uint256& proTxHash) const
Copy link
Collaborator

@knst knst Jul 28, 2025

Choose a reason for hiding this comment

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

UPD: I see, it's old PR, bad bot confused me!

why? Could you justify this refactoring?
std::optional to shared_ptr is not really good solution.

shared_ptr can be nullptr and std::optional can be nullopt.

You still need to check if it is "null" or not; you still get UB if there's null and you haven't checked it.
But there are 2 downsides - std::optional adds overhead (extra bool flag, constructor, etc); and 2nd (which is worse), you need to compare not only for nullopt, but for nullptr also.

auto mn =  GetMN(); if (mn != nullopt && mn.value() != nullptr) .... 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This PR has been conceptually abandoned, am looking to rework it someday but agree in principle it's currently not in a great shape. Bot's taken everyone for a loop, eh?

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.

conceptual NACK; shared_ptr & std::optional should not contain each other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants