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

P2tr fixes #707

Merged
merged 1 commit into from
Nov 23, 2021
Merged

P2tr fixes #707

merged 1 commit into from
Nov 23, 2021

Conversation

sanket1729
Copy link
Member

@sanket1729 sanket1729 commented Nov 17, 2021

This was my bad for not clearly stating the expected spec #687 . Changed values to references so that we only take ownership where it is required.

This should simplify the #697

@sanket1729 sanket1729 requested review from dr-orlovsky and Kixunil and removed request for dr-orlovsky November 17, 2021 16:05
@sanket1729
Copy link
Member Author

I don't know why it says that I removed the review request from @dr-orlovsky. Obviously, I want you to review this :) .

@sanket1729
Copy link
Member Author

sanket1729 commented Nov 17, 2021

There might be some discussion here since dr-orlovsky previously had (perhaps intentionally?) schnorr::PublicKey by value instead of reference.
I think we should only pass by value when you give up ownership and I see no reason to do it here. And passing by reference when you temporarily relinquish ownership. I know it's tricky when things are Copy, but that's how it's currently implemented for the rest of the address APIs.

As an extereme, I don't think passing an [0u8; 100000] by value makes sense if we just want to access it's elements without any ownership.

@Kixunil
Copy link
Collaborator

Kixunil commented Nov 17, 2021

To the best of my knowledge, Copy types should be passed by value and it should be the job of the compiler to figure out whether using a pointer internally is better. If it doesn't do that, I'd consider it a compiler bug.

I'd agree with making exception for Option<&Large> as I don't expect the compiler to be able to also do null optimization.

If you have some resources that indicate something else I'd be happy to learn.

@sanket1729
Copy link
Member Author

By asking on rust-lang discord I am getting mixed responses. From https://stackoverflow.com/questions/38065331/should-i-borrow-or-copy-my-small-data-types, it also looks like it is opinion-based.

All of secp API is full of references for types that are Copy, so is rust-bitcoin.

@sanket1729
Copy link
Member Author

There is another argument that references is that they are consistent with other APIs. Maybe I will to write a benchmark

@sanket1729
Copy link
Member Author

My benchmarks say that it does not matter. They are very close in performance and within error of measurement to say if it really matters or not. However semantically, it makes sense to not consume the data because none of the underlying methods require references.

I would 100% support this provided we have underlying APIs that require passing by value. What do you think?

@sanket1729
Copy link
Member Author

Created a new issue to get new input. People might miss this in the taproot PRs

@sanket1729 sanket1729 force-pushed the pt2r_fixes branch 2 times, most recently from 4002c78 to 347bd11 Compare November 19, 2021 18:34
@sanket1729
Copy link
Member Author

In the interest of moving things forward (even though I disagree), I have made this pass by value.

@Kixunil
Copy link
Collaborator

Kixunil commented Nov 19, 2021

Based on your last comment on #708, I don't care whether it's by reference anymore but let's not turn this into refactoring ping-pong. :)

Kixunil
Kixunil previously approved these changes Nov 19, 2021
Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 347bd11

@@ -1196,7 +1196,8 @@ mod tests {
fn p2tr_from_untweaked(){
//Test case from BIP-086
let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap();
let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin);
let secp = Secp256k1::verification_only();
let address = Address::p2tr(&secp, internal_key,None, Network::Bitcoin);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If there's another round of changes, there could be space added before None but please don't bother with it otherwise.

@@ -1196,7 +1196,8 @@ mod tests {
fn p2tr_from_untweaked(){
//Test case from BIP-086
let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap();
let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin);
let secp = Secp256k1::verification_only();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably long term - use some kind of lazy static?

/// Returns a reference to underlying public key
pub fn as_inner(&self) -> &PublicKey {
&self.0
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

If there's another round a newline would be nice.

@sanket1729
Copy link
Member Author

Sorry for the delay. Fixed the nits

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK e4774e7

Copy link
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

utACK e4774e7

Sorry for the delay. Will merge in several minutes

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

Successfully merging this pull request may close these issues.

None yet

3 participants