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

Remove recursion in semantic module #612

Merged
merged 16 commits into from
Jan 10, 2024

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Oct 11, 2023

Remove most of the recursion in the semantic module. Does not do normalized and associated functions (ones that either call it or take in a normalized policy).

Includes 4 trivial preparatory clean up patches at the front.

@tcharding tcharding force-pushed the 10-10-rm-recursion-semantic branch 3 times, most recently from 803424f to 79f9583 Compare October 11, 2023 03:58
@tcharding
Copy link
Member Author

Needs #613

@tcharding tcharding force-pushed the 10-10-rm-recursion-semantic branch 2 times, most recently from 6ab8308 to 9507538 Compare October 11, 2023 23:24
@tcharding
Copy link
Member Author

Ooooo, the first two cleanup patches should not technically be part of this PR because I never ended up being able to do normalized - I'm attacking it in a separate PR, building on this one - please excuse me for leaving them in this PR.

@@ -415,9 +415,11 @@ impl<Pk: MiniscriptKey> Policy<Pk> {

let n = subs.len() - unsatisfied_count - trivial_count; // remove all true/false
let m = k.checked_sub(trivial_count).map_or(0, |x| x); // satisfy all trivial
// m == n denotes `and` and m == 1 denotes `or`

// m == n denotes `and` and m == 1 denotes `or`
let is_and = m == n;
let is_or = m == 1;
Copy link
Member

Choose a reason for hiding this comment

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

In 939695d:

We can just delete this comment I think. I can't imagine what value it provides beyond the code itself.

@@ -380,7 +380,7 @@ impl_from_tree!(
// thresh(1) and thresh(n) are disallowed in semantic policies
if thresh <= 1 || thresh >= (nsubs as u32 - 1) {
return Err(errstr(
"Semantic Policy thresh cannot have k = 1 or k =n, use `and`/`or` instead",
"Semantic Policy thresh cannot have k=1 or k==n, use `and`/`or` instead",
Copy link
Member

Choose a reason for hiding this comment

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

In 939695d:

I think single equality was fine, but I don't feel strongly about it. But you need to the same = operator in both cases :).

Copy link
Member Author

@tcharding tcharding Oct 12, 2023

Choose a reason for hiding this comment

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

lol, its hard to get good help. Just added the space into the original line. I've been chided recently about doing trivial patches and slowing the dev process down, this is definitely a case of that :(

cc @Kixunil - woops.

subs.iter().map(|s| s.node.lift()).collect();
let semantic_subs = semantic_subs?
.into_iter()
.map(|sub| Arc::new(sub))
Copy link
Member

Choose a reason for hiding this comment

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

In 9de17fc:

This map should just be map(Arc::new). (Though I assume you're going to remove this in a later commit, since otherwise clippy would've complained about it.) Same in many other cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't run clippy in CI :(

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed them all, I think.

@tcharding tcharding force-pushed the 10-10-rm-recursion-semantic branch 3 times, most recently from ae6975e to e85b046 Compare October 12, 2023 23:53
@apoelstra
Copy link
Member

Looks like we need a rebase after #616. But e85b046 looks good.

@tcharding tcharding force-pushed the 10-10-rm-recursion-semantic branch 2 times, most recently from 81055a4 to 333bfb4 Compare October 13, 2023 21:13
@tcharding
Copy link
Member Author

Rebased and removed the _translate_pk helper as we did in concrete.

@apoelstra
Copy link
Member

cbbc582 onward does not compile after the rebase.

@tcharding
Copy link
Member Author

cargo test --workspace --all-features --all-targets runs cleanly on all those commits locally for me. What invocation is failing for you?

@apoelstra
Copy link
Member

@tcharding just cargo test.

camus 2023-10-17 13:58:39  ~/code/rust-bitcoin/miniscript/pr-review   ➦ cbbc5825  ±  git reset --hard cbbc582
HEAD is now at cbbc5825 Add Arc to the semantic::Policy::Thresh vector
 camus 2023-10-17 13:58:44  ~/code/rust-bitcoin/miniscript/pr-review   ➦ cbbc5825  ±  cargo test
   Compiling miniscript v10.0.0 (/store/home/apoelstra/code/rust-bitcoin/miniscript/pr-review)
error[E0599]: no method named `_translate_pk` found for reference `&Arc<semantic::Policy<Pk>>` in the current scope
   --> src/policy/semantic.rs:147:47
    |
147 |                     subs.iter().map(|sub| sub._translate_pk(t)).collect();
    |                                               ^^^^^^^^^^^^^ help: there is a method with a similar name: `translate_pk`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `miniscript` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...
error[E0599]: no method named `_translate_pk` found for reference `&std::sync::Arc<semantic::Policy<Pk>>` in the current scope
   --> src/policy/semantic.rs:147:47
    |
147 |                     subs.iter().map(|sub| sub._translate_pk(t)).collect();
    |                                               ^^^^^^^^^^^^^ help: there is a method with a similar name: `translate_pk`

error: could not compile `miniscript` (lib test) due to previous error

Same error with --all-features.

@tcharding
Copy link
Member Author

tcharding commented Oct 17, 2023

Bizarre, I rebased and ran cargo on each commit. Will re-try today.

@tcharding tcharding force-pushed the 10-10-rm-recursion-semantic branch 2 times, most recently from 80b8e2c to 0421aef Compare October 17, 2023 22:19
@tcharding
Copy link
Member Author

Had to rebase to fix merge conflicts. Corrected the removal of _translate_pk ... hopefully.

apoelstra
apoelstra previously approved these changes Oct 18, 2023
Copy link
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 0421aef

@sanket1729
Copy link
Member

sanket1729 commented Dec 7, 2023

@tcharding Sorry for the long absence, I can review this after a rebase.

Remove unnecessary code comment, the local variables names are clear enough.
This function is fiendishly hard to understand, add a line of whitespace
as a tiny baby step towards making it clearer.
Currently we are using `map_or` in a convoluted manner, we can just use
`unwrap_or` to get the inner value or default to 0.

Refactor only, no logic changes.
We have a little typo in an error string, fix it up.
Use standard from for import statements. (Note `Arc` is already in scope
in `super`.)
When we implemented the `TreeLike` trait for the concrete `Policy` we
put it in the `iter` module, it doesn't really live there, better to put
the impl in the same place as the definition of the implementor.

Move the impl of `TreeLike` for `concrete::Policy` to the `concrete`
module.
In preparation for removing recursive algorithms in the `semantic`
module add an `Arc` wrapper around the policies in the `Thresh` vector.

Note we use the more explicit `Arc::new` instead of `into` because a lot
of these should be removed when we remove recursion, later it may be
necessary to check that we are not creating new references when a clone
will suffice, the explicit `Arc::new` is easier to check. In tests
however just use `into`.
In preparation for removing recursive algorithms in the `semantic`
module implement the `TreeLike` trait to enable iteration over policy
nodes.

This is a direct copy of the `concrete::Policy` impl with the `And` and
`Or` variants removed.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::for_each_key`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::translate_pk`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::n_terminals`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in the `semantic::Policy::real_*_timelocks` functions.

Done together because they are basically identical.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::at_age` and
`semantic::Policy::at_age`.

Done together because they are basically identical.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::n_keys`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::minimum_n_keys`.
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::sorted`.
@tcharding tcharding force-pushed the 10-10-rm-recursion-semantic branch from 14ec788 to f9307c8 Compare December 7, 2023 19:56
@tcharding
Copy link
Member Author

@sanket1729! Good to have you back. Rebased, no changes required.

Copy link
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 f9307c8

Copy link
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.

utACK f9307c8

@sanket1729 sanket1729 merged commit 469c113 into rust-bitcoin:master Jan 10, 2024
16 checks passed
@tcharding tcharding deleted the 10-10-rm-recursion-semantic branch February 7, 2024 05:35
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.

3 participants