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 Span in random.* #24213

Merged
merged 1 commit into from
Apr 21, 2022
Merged

Conversation

PastaPastaPasta
Copy link
Contributor

@PastaPastaPasta PastaPastaPasta commented Jan 31, 2022

This PR does two things

  1. use a Span for GetRandBytes and GetStrongRandBytes

2. make GetRand a template for which any integral type can be used, where the default behavior is to return a random integral up to the max of the integral unless a max is provided.
This simplifies a lot of code from GetRand(std::numeric_limits<uint64_t>::max() -> GetRand<uint64_t>()

MarcoFalke this was inspired by your comment here: #24185 (comment) about using Span, so hopefully I'll be able to get this PR done and merged 😂

Also, if requested I could revert the GetRand(std::numeric_limits<uint64_t>::max() -> GetRand<uint64_t>() related changes if it ends up causing too many conflicts

@maflcko
Copy link
Member

maflcko commented Jan 31, 2022

This PR does two things

This needs to be split into two commits for review

@PastaPastaPasta
Copy link
Contributor Author

This PR does two things

This needs to be split into two commits for review

Done, thanks

@PastaPastaPasta PastaPastaPasta force-pushed the refactor-random branch 2 times, most recently from b401960 to 79deab2 Compare January 31, 2022 12:46
@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 31, 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:

  • #24185 (refactor: only use explicit reinterpret/const casts, not implicit by PastaPastaPasta)

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.

@PastaPastaPasta
Copy link
Contributor Author

Any opinion on this? @MarcoFalke and others?

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

Approach ACK, debug-builds cleanly, unit tests pass, feature_taproot.py passes (cf the CI failure)

@@ -227,7 +227,7 @@ const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
{
std::vector<uint8_t> ret(OBFUSCATE_KEY_NUM_BYTES);
GetRandBytes(ret.data(), OBFUSCATE_KEY_NUM_BYTES);
GetRandBytes(ret);
Copy link
Member

@jonatack jonatack Mar 6, 2022

Choose a reason for hiding this comment

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

eeae84c missing span headers here in src/dbwrapper.cpp and in src/key.cpp, src/random.h, src/rpc/request.cpp, maybe reverify if didn't miss any others

Copy link
Contributor Author

@PastaPastaPasta PastaPastaPasta Mar 7, 2022

Choose a reason for hiding this comment

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

Should it really be included in files like dbwrapper.cpp and key.cpp and request.cpp? It seems to be that it makes more sense to just explicitly include it in src/random.h.

If this is in developer notes or a strong opinion of yours, I'll happily do it, but seems better to just do it in random.h (applied and squashed random.h include)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the developer notes specify the guideline and reason:

- Every `.cpp` and `.h` file should `#include` every header file it directly uses classes, functions or other
  definitions from, even if those headers are already included indirectly through other headers.

  - *Rationale*: Excluding headers because they are already indirectly included results in compilation
    failures when those indirect dependencies change. Furthermore, it obscures what the real code
    dependencies are.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with the rule, but I'm not sure I agree with the application. I'm not really "using" span in these cpp files, I'm simply passing something to a function. If we change random.h to not use span, then we need to change the calling files anyway.

My local CLion for instance sees these includes are completely unused when I add them, so I see no reason to add them right now

Copy link
Member

Choose a reason for hiding this comment

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

Oh, you're right, now that you've added the header to random.h. Though key.cpp uses Span (not in your changes), so could add the header in 32b6173 if you're so inclined. Otherwise LGTM.

src/addrdb.cpp Outdated Show resolved Hide resolved
uint64_t nonce;
do {
nonce = GetRand<uint64_t>();
} while (nonce == 0);
Copy link
Member

Choose a reason for hiding this comment

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

d350195 what is the reason for the do/while style change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It avoids a redundant assignment, and I think it makes more sense, although I would be happy to revert this change if you would strongly want that

Copy link
Member

@jonatack jonatack Mar 9, 2022

Choose a reason for hiding this comment

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

I wouldn't needlessly change it unless other reviewers prefer the change, but no strong opinion.

Copy link
Member

Choose a reason for hiding this comment

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

I think the old code is slightly more elegant. There's no need to distinguish the first iteration here, so let's not.

@jonatack
Copy link
Member

ACK b989b5a

@PastaPastaPasta
Copy link
Contributor Author

I have rebased. @jonatack please re-review.

@jonatack
Copy link
Member

re-ACK 19f2c35 per git range-diff 8234cda b989b5a 19f2c35, rebase only

@PastaPastaPasta
Copy link
Contributor Author

@MarcoFalke would you be able to review this PR?

@laanwj
Copy link
Member

laanwj commented Apr 6, 2022

Concept and code review ACK on the span commit.
I find the other one changing GetRandInt more risky and harder to review.

Edit: might want to split this into different PRs as the changes, apart from both being related to random, are orthogonal.

src/net.cpp Outdated
@@ -2168,7 +2168,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)

if (fFeeler) {
// Add small amount of random noise before connection to avoid synchronization.
int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
int randsleep = GetRand<int>(FEELER_SLEEP_WINDOW * 1000);
if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
Copy link
Member

Choose a reason for hiding this comment

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

This can probably use the exiting native chrono random stuff.

Further, I am wondering if it makes sense to add a FastRandomContext member to connman and use that for all noise generation instead of generating a new one each time. A commit like fa73e9d can then be used to provide the chrono random stuff.

src/init.cpp Outdated
@@ -1257,7 +1257,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
assert(!node.banman);
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
assert(!node.connman);
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), *node.addrman, args.GetBoolArg("-networkactive", true));
node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(), GetRand<uint64_t>(), *node.addrman, args.GetBoolArg("-networkactive", true));
Copy link
Member

Choose a reason for hiding this comment

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

Maybe the fast context could then also be used to remove those args?

}
uint64_t nonce;
do {
nonce = GetRand<uint64_t>();
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it makes sense to use a single FastRandomContext member for all entropy drawn inside PeerManagerImpl, instead of creating a new context for each call.

@PastaPastaPasta PastaPastaPasta changed the title refactor: use Span in random.*; make GetRand a template, remove GetRandInt refactor: use Span in random.* Apr 19, 2022
@PastaPastaPasta
Copy link
Contributor Author

@laanwj Thanks for the review, I've dropped that other commit from this PR. Please re-ACK

@laanwj
Copy link
Member

laanwj commented Apr 21, 2022

Thank you! Code review re-ACK 3ae7791

@laanwj laanwj merged commit 43bb106 into bitcoin:master Apr 21, 2022
@PastaPastaPasta PastaPastaPasta deleted the refactor-random branch April 21, 2022 15:07
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 22, 2022
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 21, 2023
Summary:
This is a backport of [[bitcoin/bitcoin#24213 | core#24213]]

Depends on D14826

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D14827
@bitcoin bitcoin locked and limited conversation to collaborators Jan 10, 2024
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