-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rework translate API to allow associated types to be translated #426
Conversation
397b38e
to
acbef2f
Compare
@tcharding, you may be interested in the latest commit. |
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.
utACK 2e9bd32
As discussed in another channel, I am happy with this approach. I agree the |
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.
Nice work bro! I love the simplification of all the translate traits. I had a good long play with this. I pushed 5 patches to a branch on my fork with the same name rework_translate
. Feel free to use them, ignore them, or steal whatever parts you like. 4 of them are trivial, docs and things. In one patch I remove the PkTranslator
trait, includes full justification in the commit log.
Cheers
src/macros.rs
Outdated
}; | ||
} | ||
|
||
/// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds |
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.
/// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds | |
/// Macro for implementing FromStr trait. This avoids copying all the Pk::Associated type bounds |
If we want to be super fussy could add code backticks also to this and the comment on impl_from_tree
.
src/macros.rs
Outdated
impl<Pk $(, $gen)*> core::str::FromStr for $name | ||
where | ||
Pk: MiniscriptKey + core::str::FromStr, | ||
Pk::Hash : core::str::FromStr, |
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.
Beep deep dot, I'm a bot, filling in for rustfmt :)
Pk::Hash : core::str::FromStr, | |
Pk::Hash: core::str::FromStr, |
Here and in the other macro too.
src/test_utils.rs
Outdated
/// Translate from a String MiniscriptKey type to bitcoin::PublicKey | ||
/// If the hashmap is populated, this will lookup for keys in HashMap | ||
/// Otherwise, this will return a translation to a random key | ||
/// Only used in testing |
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.
This line is a bit redundant, this whole module is only used in testing.
src/test_utils.rs
Outdated
} | ||
|
||
fn f_sha256(&mut self, _sha256: &String) -> Result<sha256::Hash, ()> { | ||
// hard coded value |
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.
This comment does not add any value :)
@tcharding, I looked at the branch. I will pick the four commits modulo but the one that removes the The purpose of the macros is to avoid 20-line pre-conditions on each function. Every time we add a new associated type, I needed to add the bounds in multiple places across many files. |
No sweat, cheers. |
Thanks a lot for the suggestions and improvements. Will address them shortly. |
34a3e18
to
a086861
Compare
Addressed all suggestions and cherry-picked the 4 commits as mentioned above. |
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.
I like the result of these macros, not having the trait bounds everywhere, but they are not pretty. I had a play around and could not come up with anything better though. Someone with better macro skills than me can probably get rid of the trailing comma requirements. A couple of meta comments:
- The changes in commit
f483089 Fix some macro bugs for 1.41.1
are not specific to Rust 1.41.1 as the title suggests, probably should be squashed. - Commit
2ffa5ea Rename Sha256Hash type to Sha256
could be squashed - All the commits I did can be squashed into the original work, I don't need attribution
- The rustfmt change would be cleaner at the front of the PR because its totally unrelated
So I'd probably expect this PR to be three patches
- The rustfmt one
- Introduce impl macros
- Introduce
Translator
trait
src/descriptor/tr.rs
Outdated
fn parse_tr_script_spend(tree: &expression::Tree) -> Result<TapTree<Pk>, Error> { | ||
fn parse_tr_script_spend(tree: &expression::Tree,) -> Result<TapTree<Pk>, Error> { |
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.
It would be nice if we could work out how to make the arg list not require a trailing comma, my macro-foo is not strong enough though.
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.
I also tried and couldn't do it.
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.
Should be possible. Will work on it
src/macros.rs
Outdated
}; | ||
} | ||
|
||
/// Macro for implementing FromTree trait. This avoids copying all the Pk::Associated type bounds |
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.
This comment has cut'n'paste glitch - FromTree
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 a086861
a086861
to
b7899b6
Compare
Maybe rustfmt nightly changed something. But it looks like both versions are accepted by --check check
b7899b6
to
4193e5a
Compare
It is not possible to add a supertrait(and a blanket implemnetation) that captures associated types currently. The where clauses must be carried around everywhere. Use a macro instead
Overhaul TranslatePk triat This is merely fixing compiler errors and creating structs wherever necessary. This in preperation to allow associated enums for Hashes and Timelocks. Users can then write "hash(H)" and translate API(TBD in future commits will deal with it) Also adds fields for Sha256 associated data
4193e5a
to
8e5b09b
Compare
Squashed and force pushed. Only one line diff fixing the nit. @apoelstra
|
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 8e5b09b
Its clean now! Thanks for putting up with my picky reviews :) |
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 8e5b09b
This is again a breaking change because we remove the trait impl from `Terminal`, but again I don't think the trait impl should've existed. This also exposed an "off-label" use of `real_translate_pk` to convert context types as well as pk types, which I apparently explicitly reviewed and ACKed when reviewing rust-bitcoin#426, but which in retrospect looks a little funny. Rename this function to translate_pk_ctx so it's clear that it's doing two jobs.
This is again a breaking change because we remove the trait impl from `Terminal`, but again I don't think the trait impl should've existed. This also exposed an "off-label" use of `real_translate_pk` to convert context types as well as pk types, which I apparently explicitly reviewed and ACKed when reviewing rust-bitcoin#426, but which in retrospect looks a little funny. Rename this function to translate_pk_ctx so it's clear that it's doing two jobs.
This is again a breaking change because we remove the trait impl from `Terminal`, but again I don't think the trait impl should've existed. This also exposed an "off-label" use of `real_translate_pk` to convert context types as well as pk types, which I apparently explicitly reviewed and ACKed when reviewing rust-bitcoin#426, but which in retrospect looks a little funny. Rename this function to translate_pk_ctx so it's clear that it's doing two jobs.
This is again a breaking change because we remove the trait impl from `Terminal`, but again I don't think the trait impl should've existed. This also exposed an "off-label" use of `real_translate_pk` to convert context types as well as pk types, which I apparently explicitly reviewed and ACKed when reviewing rust-bitcoin#426, but which in retrospect looks a little funny. Rename this function to translate_pk_ctx so it's clear that it's doing two jobs.
This is again a breaking change because we remove the trait impl from `Terminal`, but again I don't think the trait impl should've existed. This also exposed an "off-label" use of `real_translate_pk` to convert context types as well as pk types, which I apparently explicitly reviewed and ACKed when reviewing rust-bitcoin#426, but which in retrospect looks a little funny. Rename this function to translate_pk_ctx so it's clear that it's doing two jobs.
The Translate API currently does not support translating the hashes and timelocks. Ideally, we want to write an HTLC as
or( and( alice_key, older(timeout), and(bob_key, sha256(hash)))
.But it is impossible to use placeholders for
hash
, andtimelock
. It is only possible for keys.This is a significant API change, but I think it is for good. The diff is large, but reviewing
lib.rs
is the main diff, other diffs are just chasing compiler errors