Skip to content

Check Received Proposal with Typestate - #27

Closed
DanGould wants to merge 2 commits into
Kixunil:masterfrom
DanGould:receive-checklist
Closed

DanGould wants to merge 2 commits into
Kixunil:masterfrom
DanGould:receive-checklist

Conversation

@DanGould

@DanGould DanGould commented Jul 6, 2022 •

Copy link
Copy Markdown

The first patch does Receive Checklist to support sync/async. It uses typestate to guide the downstream wallet through each check in opinionated order.

The second patch is unit tests

The third patch shares MockHeaders in receiver::test_utils, since it was used in integration tests too.

closes #26

All GitHub Actions checks passed: payjoin#1

@DanGould
DanGould force-pushed the receive-checklist branch 2 times, most recently from c78646a to 704929d Compare July 8, 2022 05:22
Comment thread bip78/src/receiver/mod.rs Resolved
Comment thread bip78/src/receiver/mod.rs Resolved
@DanGould
DanGould force-pushed the receive-checklist branch from 704929d to 47567c5 Compare July 8, 2022 05:59
@DanGould
DanGould marked this pull request as ready for review July 8, 2022 06:09
@RCasatta

RCasatta commented Jul 8, 2022

Copy link
Copy Markdown
Collaborator

IMHO #23 helps implementors by having just the method they need on the "verification stage" they are, for example
PsbtState<MaybeUnbroadcastable> has tx()
while PsbtState<MaybeInputsOwned> has script_pubkeys()...

Even though you are required to verify the steps in order

@Kixunil

Kixunil commented Jul 8, 2022

Copy link
Copy Markdown
Owner

Yeah, I prefer type state. AFAIR some checks need additional information anyway.

If you strongly want to allow doing the checks in any order use type-based flags:

pub enum Checked {}
pub enum NotChecked {}

pub trait CheckState: sealed::CheckState {}

impl<T: sealed::CheckState> CheckState for T {}
impl sealed::CheckState for Checked {}
impl sealed::CheckState for NotChecked {}

// use meaningful names, this is just example
pub struct UncheckedProposal<Check1: CheckState, Check2: CheckState, Check3: CheckState> {
    // the fields
    _phantom: PhantomData<(Check1, Check2, Check3)>,
}

impl<C2: CheckState, C3: CheckState> UncheckedProposal<Unchecked, C2, C3> {
    fn assume_check1_verified(self) -> UncheckedProposal<Checked, C2, C3> {
        // ...
    }
}

Anyway, even if we decided to go for runtime flags, the bitflags crate would be better than hand-rolling it.

@DanGould

DanGould commented Jul 8, 2022

Copy link
Copy Markdown
Author

Ok, makes sense to me. I took another approach since #23 closed. Must have been another reason.

@RCasatta why was it closed?

@Kixunil

Kixunil commented Jul 8, 2022

Copy link
Copy Markdown
Owner

Because it had only a single state and for each state there'd still need to be a new struct so it didn't add much value. Having individual checks in type state at least provides something tangible: reorderable checks. Still not sure if worth the effort TBH.

@DanGould
DanGould force-pushed the receive-checklist branch 2 times, most recently from 955f540 to 3c71214 Compare July 11, 2022 13:11
@DanGould

Copy link
Copy Markdown
Author

So now this has 4 states that don't need new structs. Looks like the whole proposal might be best served with a Proposal<S: State>. The whatever trait is used for state still needs to be sealed

@Kixunil

Kixunil commented Jul 11, 2022

Copy link
Copy Markdown
Owner

I don't understand this change, it looks like you re-did #23 instead of making checks type-level flags.

@DanGould

DanGould commented Jul 11, 2022 •

Copy link
Copy Markdown
Author

Well, unlike #23 this doesn't require a new struct for each state nor rely on a single state which were the stated flaws of #23. It's also 1/2 the changes including tests which 23 lacks.

What do you mean by type-level flags? to remove the generic? I understood your comments to say that reorderable checks were not particularly desirable because they were extra work, so I didn't bother in this PR.

This does exactly the proposal stated in 23:

I don't understand what problem you're trying to solve by putting data into type state. It looks like you only do it to get specific errors of the same type, so that user doesn't need to define an error type. This could be solved by just providing the error type.

Note that my idea was for checking code to be written like this:

let proposal = UncheckedProposal::from_request(...)?;
let tx = proposal.get_transaction_to_check_broadcast();
// do testmempoolaccept call with tx
if !broadcastable {
    // We could simply allow writing return Err(proposal.not_broadcastable()) to generate error with context.
    return Err(MyError::NotBroadcastableError);
}
let proposal = proposal.assume_broadcastability_was_verified();
// ... same style for other checks

Comment thread bip78/src/receiver/error.rs Resolved
@Kixunil

Kixunil commented Jul 11, 2022

Copy link
Copy Markdown
Owner

doesn't require a new struct for each state

It does require a new enum. Adding types is the issue.

What do you mean by type-level flags?

Check my example above - tracking individual checks in type using generics. This allows reordering. But yes, it's a bunch of extra code and being reorderable doesn't seem very important.

@DanGould
DanGould force-pushed the receive-checklist branch from 3c71214 to b169d53 Compare July 12, 2022 02:02
@DanGould

DanGould commented Jul 12, 2022 •

Copy link
Copy Markdown
Author

Adding types for typestate is an issue because ...?

What will it take to get checks merged? I'm grateful for the reviews from a high rust wizard.

@Kixunil

Kixunil commented Jul 12, 2022

Copy link
Copy Markdown
Owner

Implementing typestate using generics adds more types to the API that the consumer sees and they are not helping. To simplify:

pub struct StateA {
    // field: Type,
}

pub struct StateB {
    // field: Type,
}

Consumer see two types: StateA, StateB

pub enum StateA {}
pub enum StateB {}

pub trait State: selaed::State {}

pub struct Thing<S: State> {
    // field: Type,
    _phantom: PahntomData<S>,
}

Consumer sees 3 types and 1 trait - this is strictly more complex and doesn't help the consumer with anything.

However, using flags:

pub enum Yes {}
pub enum No {}

pub trait Check: selaed::Check {}

pub struct Thing<A: Check, B: Check, C: Check> {
    // field: Type,
    _phantom: PahntomData<(A, B ,C)>,
}

Models 8 possible states that can be traversed in any order with just 3 types and one traits - same number of items needed for 4 states.

@DanGould
DanGould force-pushed the receive-checklist branch from b169d53 to 52fe533 Compare July 14, 2022 15:10
@DanGould

Copy link
Copy Markdown
Author

I understand the tradeoff now. Thank you. I implemented non-generic typestate with the methods to supply downstream relevant data and documented them too.

@DanGould

Copy link
Copy Markdown
Author

@Kixunil @RCasatta May you please take another look at this for review? It's pruned to just the typestate changes (no actual psbt checks for this PR). I think the interface makes more sense than the generic typestate machine I also implemented as an exercise.

An opinionated interface like this is easier to use. Each check only has 1 data check method to call. There's also less code for us to maintain.

@Kixunil Kixunil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Concept ACK, just some nits/questions.

Comment thread bip78/src/receiver/mod.rs
}

pub struct MaybeInputsOwned {
psbt: Psbt,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We will also have to carry around information from HTTP request.

@DanGould DanGould Jul 31, 2022 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

certainly, receiver parse & application of sender proposal params are in scope for the params pr

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ah, I now think I understood what you intended in the other PR. You wanted to reuse the Params struct here.

I suggest:

  • Keep Params pub(crate) and move to a common module
  • What currently is exposed as Params should be named Configuration, it can hold Params inside
  • Consumers can manipulate Configuration via builder API but not Params directly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This was the comment I looking for. I feel like I'm getting the ultimate trial by fire rust experience here :D

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Yeah, I can see many details. :) Hope it helps you get more skilled.

Comment thread bip78/src/receiver/mod.rs Resolved
Comment thread bip78/src/receiver/mod.rs Resolved
Comment thread bip78/src/receiver/mod.rs Resolved
Comment thread bip78/src/receiver/mod.rs Resolved
Comment thread bip78/src/receiver/mod.rs
/// Call this after checking downstream.
///
/// Note: mixed spends are not necessarily indicative of distinct wallet fingerprints but they can be.
/// This check is intended to prevent some types of wallet fingerprinting.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I believe the check is actually an error in the BIP itself :) See bitcoin/bips#1244

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm fine dropping the check for the time being, which I did. However, a 1.0 release we should match the spec (either they change or we change)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I actually wanted to keep it according to spec the whole time. I just clarified what I meant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok, I re-introduced this check

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Do you want it removed since we can do it internally, or do you want it as part of the checklist? based on your next comment, posted below, I think we should remove it

We can actually do this check internally, no need to burden the consumer with it. The consumer only needs to get one address type which is the same for all inputs and check that one.
#30 (comment)

@DanGould
DanGould force-pushed the receive-checklist branch from e580600 to 301ca25 Compare July 31, 2022 02:35
@DanGould DanGould changed the title Do Receive Checklist Check Received Proposal with Typestate Jul 31, 2022
@Kixunil Kixunil mentioned this pull request Jul 31, 2022
@DanGould
DanGould force-pushed the receive-checklist branch from 301ca25 to fff6062 Compare August 1, 2022 03:13
@DanGould

DanGould commented Aug 1, 2022 •

Copy link
Copy Markdown
Author

All 4 checks included according to your changes. Changed "MaybePrevoutsSeen" to "MaybeInputsSeen" with Outpoint {txid, vout} in the name signature to stay consistent with the rust-bitcoin dependency. It's the same as utxos to lock in the next step. Removed immaterial e.g. spacing changes

@DanGould
DanGould force-pushed the receive-checklist branch 2 times, most recently from 8bcd838 to 39b275a Compare August 1, 2022 03:34
@RCasatta

RCasatta commented Aug 1, 2022

Copy link
Copy Markdown
Collaborator

Concept ACK from me

@DanGould

DanGould commented Aug 8, 2022

Copy link
Copy Markdown
Author

Looks like this has consensus. May we merge it?

@Kixunil

Kixunil commented Aug 8, 2022

Copy link
Copy Markdown
Owner

I want to give it one more review pass, but have to go rn.

@DanGould

Copy link
Copy Markdown
Author

Removed sharing MockHeaders because of this comment in the params pr. The functionality has not changed.

@DanGould

Copy link
Copy Markdown
Author

@Kixunil I think this is a beautiful start. It includes changes to doc comments from your review on #30. WRT assume_no_mixed_input_scripts, that decision is still in discussion in #30. I sincerely hope that one decision does not hold back these 2 commits and we can have it our way in that PR or the one which implements the actual data checks. Let's go!

@Kixunil

Kixunil commented Oct 28, 2022

Copy link
Copy Markdown
Owner

#30 is quite big so it'll take time. This one is smaller but can't review rn.

@DanGould

Copy link
Copy Markdown
Author

fyi they're about the same size, #30 is really 10 files changed, 18 insertions(+), 183 deletions(-) built on this PR

@DanGould

Copy link
Copy Markdown
Author

@Kixunil may you please take that second look?

@Kixunil

Kixunil commented Nov 11, 2022

Copy link
Copy Markdown
Owner

Damn, I'm very short on time now. Hopefully no later than the next week.

@DanGould

Copy link
Copy Markdown
Author

Don't sweat it, you've been extremely generous with your time. I have patience.

I spoke with @cryptoquick about wrapping and re-exporting this crate from https://github.com/chaincase-app/rust-payjoin so we can have a semver dependency downstream and to keep your desire for bip78 logic ultimately to end up in one place

@Kixunil

Kixunil commented Nov 11, 2022

Copy link
Copy Markdown
Owner

so we can have a semver dependency downstream

Interesting, is it really important in prototyping phase? What is the motivation? I'm mainly curious because it could inform broader Rust community about these things, looks like we can learn something new here.

@cryptoquick

Copy link
Copy Markdown

@Kixunil Just that with conditional compilation, crate features, and the traits system, there's no reason to bloat or fork dependencies; code simply needs to be reorganized. This keeps projects focused, maintainers aren't stuck with keeping up code they don't use or need, and the boundaries between core code and application code help improve code stability, which is especially important when doing security reviews.

DanGould referenced this pull request in DanGould/rust-payjoin Feb 10, 2023
@DanGould DanGould closed this Feb 2, 2024
@DanGould
DanGould deleted the receive-checklist branch February 2, 2024 22:42
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.

Receive: Do Checklist

4 participants