-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: Update XOnlyPubKey::GetKeyIDs()
to return a pair of pubkeys
#32332
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
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32332. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was nerd-sniped by this PR decreasing heap usage. Otherwise these kinds of refactorings are a bit frowned upon.
src/pubkey.cpp
Outdated
CPubKey fullpubkey; | ||
fullpubkey.Set(b, b + 33); | ||
out.push_back(fullpubkey.GetID()); | ||
CKeyID idEvenPubKey = fullpubkey.GetID(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New code should use snake_case
for variables according to developer-notes.md.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks.
src/pubkey.h
Outdated
* This is needed for key lookups since keys are indexed by CKeyID. | ||
*/ | ||
std::vector<CKeyID> GetKeyIDs() const; | ||
std::pair<CKeyID, CKeyID> GetKeyIDs() const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you instead return std::array<CKeyID, 2>
the data is still on the stack and you don't need to touch signingprovider.h.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks.
feca203
to
4041cbf
Compare
@hodlinator yes, this decreases heap usage, but I don't think there are any relevant performance gains. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 4041cbf
src/pubkey.cpp
Outdated
@@ -197,21 +197,24 @@ constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{ | |||
[]() consteval { return XOnlyPubKey{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8}; }(), | |||
}; | |||
|
|||
std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const | |||
std::array<CKeyID,2> XOnlyPubKey::GetKeyIDs() const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Grepped for std::array<\w+,\w+
and only found 2 other occurrences (vs 89 for std::array<\w+, \w+
).
std::array<CKeyID,2> XOnlyPubKey::GetKeyIDs() const | |
std::array<CKeyID, 2> XOnlyPubKey::GetKeyIDs() const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in acf73d3
src/pubkey.cpp
Outdated
out.push_back(fullpubkey.GetID()); | ||
|
||
CPubKey full_pubkey; | ||
full_pubkey.Set(b, b + 33); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Could use cbegin
/cend
here and below:
full_pubkey.Set(b, b + 33); | |
full_pubkey.Set(std::cbegin(b), std::cend(b)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in acf73d3
src/pubkey.h
Outdated
@@ -282,10 +282,10 @@ class XOnlyPubKey | |||
/** Construct a Taproot tweaked output point with this point as internal key. */ | |||
std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const; | |||
|
|||
/** Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey. | |||
/** Returns two CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Could be more precise:
/** Returns two CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey. | |
/** Returns CKeyIDs for the even and odd CPubKeys that could have been used to create this XOnlyPubKey. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in acf73d3
src/pubkey.cpp
Outdated
CKeyID id_odd_pubKey{full_pubkey.GetID()}; | ||
|
||
// Return [0] = even-Y, [1] = odd-Y | ||
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubKey }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - casing inconsistency:
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubKey }; | |
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubkey }; |
Edit: could just drop _pub[kK]ey
as we are in the XOnlyPubKey
:
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubKey }; | |
return std::array<CKeyID, 2>{ id_even, id_odd }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 1718177
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK 1718177
Tiny refactor that clarifies the XOnlyPubKey::GetKeyIDs()
interface by explicitly returning 2 ids (making heap-usage less likely as a bonus). Also documents even/odd Y values.
Only trivial changes since first ACK (please resolve).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 1718177
Usually minor refactoring changes are avoided but I don't seem to mind this change. It does increase the readability marginally as I like seeing an array of size two in the return type instead of vector
while going through the callers of GetKeyIDs
function.
Both array & vector satisfy the requirements of the CPP Container
as well.
-0 I don't think such refactorings are meaningfully useful. While it may improve readability a bit, I don't think the difference is enough to justify making this change. Such refactorings both can cause merge conflicts with other higher priority work, and also make git blame a little bit harder. |
🐙 This pull request conflicts with the target branch and needs rebase. |
Currently,
XOnlyPubKey::GetKeyIDs()
hasvector
as return type, but always returns a pair of public key IDs.This PR changes the code for readability reasons.
There may be negligible efficiency gains, but the goal is to make the code more readable and to make the intent of the function clear.
There is no change in behavior.