Skip to content

Merge parallel feature flag local certificate logic in cli ohttp#726

Merged
spacebear21 merged 2 commits intopayjoin:masterfrom
benalleng:ohttp-cli-feature-merge
Jun 6, 2025
Merged

Merge parallel feature flag local certificate logic in cli ohttp#726
spacebear21 merged 2 commits intopayjoin:masterfrom
benalleng:ohttp-cli-feature-merge

Conversation

@benalleng
Copy link
Collaborator

These ohttp functions had parallel methods when running tests vs running in production. This made it difficult to read as there were two functions that appeared to do completely different things when a feature flag was present.

Because localhost relays are not actually able to be proxies here we still need to opportunistically select them from the config when running our tests instead of just allowing local cert validation.

[2025-05-30T17:17:53Z DEBUG payjoin_cli::app::v2::ohttp] Failed to connect to relay: http://localhost:34391/, Internal(InternalError(Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("payjo.in")), port: None, path: "/.well-known/ohttp-gateway", query: None, fragment: None }, source: hyper_util::client::legacy::Error(Connect, "unsuccessful tunnel") })))

@benalleng benalleng changed the title Merge fetch keys and validate relay in payjoin cli Merge parallel feature flag functions in cli ohttp May 30, 2025
@benalleng benalleng changed the title Merge parallel feature flag functions in cli ohttp Merge parallel feature flag local certificate logic in cli ohttp May 30, 2025
@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch from 1c10ec6 to f9151ba Compare May 30, 2025 17:24
@coveralls
Copy link
Collaborator

coveralls commented May 30, 2025

Pull Request Test Coverage Report for Build 15476179133

Details

  • 36 of 36 (100.0%) changed or added relevant lines in 2 files are covered.
  • 12 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.1%) to 85.159%

Files with Coverage Reduction New Missed Lines %
payjoin-cli/src/app/v2/ohttp.rs 12 81.25%
Totals Coverage Status
Change from base Build 15449709719: 0.1%
Covered Lines: 6811
Relevant Lines: 7998

💛 - Coveralls

}
}

///Local relays are incapable of acting as proxies so we must opportunistically fetch keys from the config
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This earlier comment was placed in an incorrect place based on my misunderstanding of where the logic was failing

@benalleng

This comment was marked as resolved.

@benalleng benalleng marked this pull request as draft May 30, 2025 18:49
@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 5 times, most recently from 857ae15 to 12b12d7 Compare June 2, 2025 16:22
@benalleng
Copy link
Collaborator Author

Ok resolved, as it turns out the problem was that the subsequent steps in the test when initializing were falling back to the default payjoin directory payjo.in and not the localhost directory, by passing --pj-directory as a global arg we are enforcing that we are using the same directory through the e2e test.

This was not a problem before as the ohttp-keys flag was overriding any checks and skipping this logic in the test.

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 2 times, most recently from 5245da8 to bef6610 Compare June 2, 2025 16:32
@benalleng benalleng marked this pull request as ready for review June 2, 2025 16:36
@benalleng benalleng requested a review from DanGould June 2, 2025 16:36
Copy link
Contributor

@DanGould DanGould left a comment

Choose a reason for hiding this comment

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

The simplification is much greater than the diff count suggests. Great change.

3 things

  1. needs rebase
  2. validate_relay and fetch_keys are almost exact now. Can they be deduplicated while you're rebase
  3. It seems completely incorrect to require --pj-directory for a sender. The BIP21 defines the directory. If we're always defaulting to payjo.in that's a bug.

@benalleng
Copy link
Collaborator Author

benalleng commented Jun 2, 2025

It seems completely incorrect to require --pj-directory for a sender. The BIP21 defines the directory. If we're always defaulting to payjo.in that's a bug.

I think you're right that its actually a bug. The sender would just seemingly ignore the directory listed in the bip21 and use the payjo.in directory as a fallback. I'd like to make the directory based on the provided bip21 if possible at least in the send command, that still leaves an open question for me about doing that for the resume command, but I assume the bip21 is still accessible somehow.

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 5 times, most recently from 2ed1621 to 7e1476b Compare June 3, 2025 01:24
@benalleng
Copy link
Collaborator Author

I opted to keep the --pj-directory as a flag for the resume command. Is that ok or is there another place where I could access the directory from within the saved state as initialization is happening?

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 4 times, most recently from c33b213 to 5c86251 Compare June 3, 2025 02:09
@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 2 times, most recently from f023303 to 028282d Compare June 3, 2025 13:46
@DanGould
Copy link
Contributor

DanGould commented Jun 3, 2025

It doesn't make much sense for resume to take a --pj-directory flag at all because senders will use the directory from the bitcoin URI they're sending to and receivers ought to have saved a directory as part of their initialization since a session is tied to a specific directory. extract_req should encapsulate the directory url inside the OHTTP request.

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 2 times, most recently from 59bf8ce to bd364ac Compare June 4, 2025 00:45
@benalleng
Copy link
Collaborator Author

Ok so I created a way for the directory to be correctly chosen across each payjoin-cli command.

  1. receive still uses the --pj-directory flag as the primary way to select what directory to use and will ultimately be what creates the bip21 to be used in the sender
  2. send checks the bip21 arg already present and parses out the pj-directory from there
  3. resume initializes without any flags or args so the initialization occurs and defaults are set before the resume session has access to any information about what directory to use so I modified the pj_directory to be mutable at the step where the session is established.

@DanGould
Copy link
Contributor

DanGould commented Jun 4, 2025

resume initializes without any flags or args so the initialization occurs and defaults are set before the resume session has access to any information about what directory to use so I modified the pj_directory to be mutable at the step where the session is established.

An existing session should have its own pj_directory that overrides any configured payjoin directory. I can't think of any instance where application code should mutate the config once it is initialized.

@benalleng
Copy link
Collaborator Author

benalleng commented Jun 4, 2025

The issue I ran into is that the session isn't accessed until after the config defaults for that session are already written

@benalleng
Copy link
Collaborator Author

If I make a clone db at the config builder step how should I make sure that I am choosing the right session for the config with the get_recv_sessions() returning a Vec<Receiver>

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 3 times, most recently from c8fd1f0 to 315a4f5 Compare June 4, 2025 18:20
@DanGould
Copy link
Contributor

DanGould commented Jun 4, 2025

you shouldn't need to edit the config or apply defaults even on resume. Each session should already have an associated pj_directory that can be used, no? The config should be irrelevant.

@benalleng
Copy link
Collaborator Author

benalleng commented Jun 4, 2025

Ok, I can just go direct for the session directory in the fetch function I wasn't sure if we wanted some consistency with he config but it makes sense to skip over it for this 👍

I definitely wasn't aware you could do this for anything beyond resume

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 3 times, most recently from ba87a41 to 7f72302 Compare June 5, 2025 13:12
Comment on lines 80 to 88
payjoin::io::fetch_ohttp_keys_with_cert(
selected_relay.clone(),
payjoin_directory.clone(),
cert_der,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

These clones are unnecessary and we can use references instead when calling fetch_ohttp_keys:

Suggested change
payjoin::io::fetch_ohttp_keys_with_cert(
selected_relay.clone(),
payjoin_directory.clone(),
cert_der,
)
payjoin::io::fetch_ohttp_keys_with_cert(
&selected_relay,
&payjoin_directory,
cert_der,
)

Comment on lines 35 to 39
let config_keys = config.v2()?.ohttp_keys.clone();
let mut fetched_keys = fetch_ohttp_keys(config, directory, relay_state).await?;

if let Some(keys) = config_keys {
fetched_keys.ohttp_keys = keys;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor optimization: we shouldn't have to fetch_ohttp_keys if the keys are going to be overridden by the config anyways.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, isn't this error-prone if the config ohttp_keys doesn't correspond to the randomly selected relay in fetch_ohttp_keys?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I guess that makes sense, I still need to return the relay but in those instances I think it is safe to just skip the fetching and use the first default relay

@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch 2 times, most recently from 24874ef to 8e38602 Compare June 5, 2025 20:02
benalleng added 2 commits June 5, 2025 16:03
These ohttp functions had parallel methods when running tests vs running
in production. This made it difficult to read as there were two
functions that appeared to do completely different things when a feature
flag was present.
when merging the ohttp-key fetching and validation functions I found
that there was some logic that was skipping the correct directory
selection.

By ensuring that the directory is chosen per session we can ensure that
the correct directory is used no matter the initial config at
intialization.
@benalleng benalleng force-pushed the ohttp-cli-feature-merge branch from 8e38602 to 6e03ab1 Compare June 5, 2025 20:04
Copy link
Collaborator

@spacebear21 spacebear21 left a comment

Choose a reason for hiding this comment

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

tACK 6e03ab1

Ran the cli receiver and sender and interrupted both sides to continue with the resume command. My v2 config only includes a single relay fwiw:

[v2]
ohttp_relays=["https://pj.bobspacebkk.com"]

@spacebear21 spacebear21 merged commit c35b20a into payjoin:master Jun 6, 2025
7 checks passed
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.

4 participants