Skip to content

Separate out merge method into public trait#842

Merged
sanket1729 merged 1 commit intorust-bitcoin:masterfrom
tcharding:psbt-merge
Feb 28, 2022
Merged

Separate out merge method into public trait#842
sanket1729 merged 1 commit intorust-bitcoin:masterfrom
tcharding:psbt-merge

Conversation

@tcharding
Copy link
Copy Markdown
Member

Recently we (cough Tobin) made the Map trait private and neglected
to add a public API for merging together two PSBTs. Doing so broke the
psbt module.

Add a public trait Merge and implement it for
PartiallySignedTransaction using the code currently in the merge
method of the now private Map trait.

Motivated by #841

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Feb 22, 2022

What is the reason this is a trait and not inherent methods? Do people generally need to abstract over merging different items?

@JeremyRubin
Copy link
Copy Markdown
Contributor

I think it makes sense to have it as a trait, but I would envision the trait to be something more like Add.

trait Merge<With> {
    fn merge(&mut self, other: With) -> Result<(), Error>;
}

So that we might be able to define more structured Merging (e.g., merge raw tx into PSBT, PSBT V1 into V2 but not V2 into V1, etc).

However this is likely overkill v.s. as @Kixunil points out, an inherent method for it (which can also just be a wrapper around the Map type).

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Feb 22, 2022

@JeremyRubin if someone actually has need for that (which I won't believe until I see an actual real-life example of code abstractly working on those types) then we can add With backward-compatibly by defaulting it to Self.

@apoelstra
Copy link
Copy Markdown
Member

Seconding "let's make this an intrinsic method".

Also, I'd like to call it combine instead of merge.

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Feb 23, 2022

Updated PR as suggested:

  • Used 'combine' instead of 'merge', this makes the diff a bit bigger to change a bunch of identifiers that use 'merge'.
  • Implemented methods directly on the types instead of a trait.

Also improved the unit tests

  • Used files to hold the hex strings
  • Added a comutative combine test

Recently we (*cough* Tobin) made the `Map` trait private and neglected
to add a public API for combining together two PSBTs. Doing so broke the
`psbt` module.

Pull the merge logic out of the `Map` trait and put it in methods on
each individual type (`Input`, `Output`, `PartiallySignedTransaction`).
Doing so allows for simplification of return types since combining
inputs/outputs never errors.

Use the term 'combine' instead of 'merge' since that is the term used in
BIP 174.
@JeremyRubin
Copy link
Copy Markdown
Contributor

i'm kinda meh on changing the name from merge / combine since it's a breaking change for no great reason.

Can you add a deprecated wrapper around combine called merge? We can then remove it after a release or two...

@apoelstra
Copy link
Copy Markdown
Member

The old name was confusing and inconsistent with PSBT terminology, which is how it got deleted originally without anybody raising their eyebrows.

But yes, we should put a deprecated wrapper around it.

@tcharding
Copy link
Copy Markdown
Member Author

I don't think we can deprecate it because the API break was making Map private. We can't use deprecate on that change. Unless I"m missing something?

@tcharding
Copy link
Copy Markdown
Member Author

If we really want to use deprecate we could make Map public again then add deprecate massage to each of the trait methods individually then make Map private after another release? Seems like a lot of hooh-hah considering the taproot release is so massively breaking anyways.

@apoelstra
Copy link
Copy Markdown
Member

Ah, ok, sounds good.

If the merge method was actually just part of the Map trait, which made no sense from an API perspective anyway, I'm fine dropping it without ta deprecation.

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 5e24499

@JeremyRubin
Copy link
Copy Markdown
Contributor

JeremyRubin commented Feb 24, 2022 via email

@apoelstra
Copy link
Copy Markdown
Member

@JeremyRubin it would be weird to add a new deprecated method that didn't exist before, even if it had the same name as a method on a deleted trait.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Feb 24, 2022

Yeah, users would still get failure on non-existing trait.

@JeremyRubin
Copy link
Copy Markdown
Contributor

ah it used to exist as an inherent in 0.26 https://docs.rs/bitcoin/0.26.2/bitcoin/util/psbt/struct.PartiallySignedTransaction.html#method.merge, i guess in 0.27 it was moved to the trait, I wasn't sure when the refactor to Map was (altho it looks like it was a part of 0.27)

@apoelstra
Copy link
Copy Markdown
Member

Lol, given that it existed in 0.26 I actually wouldn't mind re-adding and deprecating it now. But I would mildly prefer not to.

@JeremyRubin
Copy link
Copy Markdown
Contributor

whatever works!

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

ACK 5e24499. Also verified that the vectors are same of that of BIP174

@sanket1729 sanket1729 merged commit 1ec9e87 into rust-bitcoin:master Feb 28, 2022
@tcharding tcharding deleted the psbt-merge branch March 1, 2022 16:15
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.

5 participants