Skip to content

Demo sender fallback - #1510

Merged
DanGould merged 2 commits into
payjoin:masterfrom
arminsabouri:demo-sender-fallback
May 6, 2026
Merged

DanGould merged 2 commits into
payjoin:masterfrom
arminsabouri:demo-sender-fallback

Conversation

@arminsabouri

@arminsabouri arminsabouri commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Code migrated from #1164. There were many conflicts.

The tricky thing with v2 is we don't want to fallback when there is a transient error. Luckly, our session outcome abstracts all that away. If after a replay a session is closed due to either a failure or cancellation we broadcast the fallback. But this requires a replay. Say your payjoin proposal was faulty, currently you have to resume again for the fallback to be broadasted which I think defeats the purpose of a "demo". i.e what we wants devs to replicate in their apps.

Instead, internally in Pj-cli we can replay again if an error occured (or if #1197 we can avoid disk reads) check if the session is closed and fallback accordingly.

cc @zealsham @chavic

Pull Request Checklist

Please confirm the following before requesting review:

@coveralls

coveralls commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 25380214465

Coverage increased (+0.05%) to 85.14%

Details

  • Coverage increased (+0.05%) from the base build.
  • Patch coverage: 5 uncovered changes across 2 files (6 of 11 lines covered, 54.55%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
payjoin-cli/src/app/v2/mod.rs 5 2 40.0%
payjoin-cli/src/app/v1.rs 2 0 0.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 13526
Covered Lines: 11516
Line Coverage: 85.14%
Coverage Strength: 399.75 hits per line

💛 - Coveralls

@DanGould

Copy link
Copy Markdown
Member

What's left to do here? Just a v2 fallback test? Or we're still not handling the v2 case completely and a decision needs to be made on the experience?

@arminsabouri
arminsabouri force-pushed the demo-sender-fallback branch 2 times, most recently from 88effb0 to 72c21a6 Compare April 30, 2026 13:03
@arminsabouri

Copy link
Copy Markdown
Contributor Author

What's left to do here? Just a v2 fallback test? Or we're still not handling the v2 case completely and a decision needs to be made on the experience?

I decided a second resume was not the way forward. PJ cli will replay and check for the fallback condition before exiting the app. Fall back test was added as a integration test in pj cli as opposed to a e2e test. Creating e2e conditions for protocol failure was tricky without manually updating the db. Integration test ended up being simpler .

@arminsabouri
arminsabouri marked this pull request as ready for review April 30, 2026 13:04
@DanGould
DanGould self-requested a review May 4, 2026 04:19

@DanGould DanGould left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wallet integrators (Bull Bitcoin, Cake pilots) need to see the failure-handling boundary explicitly rather than finding it hidden inside the CLI's send loop. Bull Bitcoin gives this manual fallback control to users today. The way v1 is handled here may match BTCPay & Sparrow 's BIP 78 sender implementations but doesn't demonstrate the behavior we want implemented.

When POST fails, the receiver never sees the PSBT, so only the sender can broadcast. That's the manual-intervention case we want to demo rather than auto-broadcast. Only if the POST succeeded and the response processing failed could I see auto-broadcast being argued for, and even then I think we're better off with manual intervention for the sake of simplicity and demonstration.

For v2 senders I'd like a manual fallback <session-id> subcommand trigger over maybe_broadcast_sender_fallback as reference. This can then be used to model the non-automated broadcast experience we'd like Bull Bitcoin Mobile & Cake Wallet pilots to deliver. We can demo a v2 receiver experience in the BTCPayServer plugin as a reference for automated systems. Even though session-ids are auto-incremented per table, this PR only demos sender fallback, so we don't need to disambiguate between sender/receiver session-ids until we introduce the receiver.

(At that point we can decide if we want to expose shortId as the 'real' session-id or just use an s, r prefix but it doesn't need to be done to get this PR done)

What I request to be done:

  1. Remove auto-broadcast from v2 send (v2/mod.rs:255-265) and resume (v2/mod.rs:530-534); replace with a hint message pointing to fallback .
  2. Replace v1 auto-broadcast in both error arms of v1.rs with a print of the fallback hex and a pointer to bitcoin-cli sendrawtransaction.
  3. Add payjoin-cli fallback <session-id> subcommand calling maybe_broadcast_sender_fallback in auto-broadcast's place
  4. Add a test that runs send to failure, asserts no broadcast, then runs fallback and asserts broadcast (to assert send fails → original tx NOT broadcast → user runs fallback → original tx IS broadcast: no broadcast after send fails, broadcast after fallback)

Ultimately, my wish is for our reference to also use the same payjoin-cli fallback <session-id> for v2 receivers.

Receiver-side fallback can land in a follow-up; once it does, we'll need to disambiguate IDs (shortId or prefix), but that's a separate decision.

Comment thread payjoin-cli/src/app/v2/mod.rs Outdated
Comment on lines 194 to 196
@@ -196,10 +196,18 @@ impl AppTrait for App {
"Sent fallback transaction hex: {:#}",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is printed before the broadcast. "Sent" is an ambiguous word. To make println! more precise I suggest:

Suggested change
"Sent fallback transaction hex: {:#}",
println!("Fallback transaction txid: {}", fallback_tx.compute_txid());
println!(
"Fallback transaction hex: {:#}",

Comment thread payjoin-cli/src/app/v1.rs Outdated
Comment on lines 76 to 80
@@ -86,11 +78,32 @@ impl AppTrait for App {
"Sent fallback transaction hex: {:#}",
payjoin::bitcoin::consensus::encode::serialize_hex(&fallback_tx)
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same issue as v2:

This is printed before any broadcast. "Sent" is an ambiguous word. To make println! more precise I suggest:

Suggested change
);
println!("Fallback transaction txid: {}", fallback_tx.compute_txid());
println!(
"Fallback transaction hex: {:#}",
payjoin::bitcoin::consensus::encode::serialize_hex(&fallback_tx)
);

Comment thread payjoin-cli/src/app/v2/mod.rs Outdated
Comment on lines +203 to +207
self.wallet().broadcast_tx(&fallback_tx)?;
println!(
"Broadcasted fallback transaction txid: {}",
fallback_tx.compute_txid()
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than auto broadcast, I believe manual broadcast is more appropriate, perhaps from a helper method that encapsulates the following.

Suggested change
self.wallet().broadcast_tx(&fallback_tx)?;
println!(
"Broadcasted fallback transaction txid: {}",
fallback_tx.compute_txid()
);
format!("To broadcast the fallback, run: payjoin-cli {FALLBACK_CMD} {session_id}")

Comment thread payjoin-cli/src/app/v2/mod.rs Outdated
Comment on lines +256 to +265
match self.maybe_broadcast_sender_fallback(&persister) {
Ok(true) => return Ok(()),
Ok(false) => return Err(err),
Err(broadcast_err) => {
return Err(err.context(format!(
"Failed to auto-broadcast fallback after sender session error: {}",
broadcast_err
)));
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same request for manual broadcast here:

Suggested change
match self.maybe_broadcast_sender_fallback(&persister) {
Ok(true) => return Ok(()),
Ok(false) => return Err(err),
Err(broadcast_err) => {
return Err(err.context(format!(
"Failed to auto-broadcast fallback after sender session error: {}",
broadcast_err
)));
}
}
format!("To broadcast the fallback, run: payjoin-cli {FALLBACK_CMD} {session_id}")

Comment thread payjoin-cli/src/app/v1.rs Outdated
Comment on lines +91 to +93
tracing::error!("HTTP request failed: {e}");
self.wallet().broadcast_tx(&fallback_tx)?;
println!("Broadcasted fallback transaction txid: {}", fallback_tx.compute_txid());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Prefer manual intervention here. The receiver has no Fallback TX to broadcast on our behalf and it's v1 so we can keep it stupid simple. I prefer the simplicity of referencing bitcoin-cli this is just legacy v1 after all.

Suggested change
tracing::error!("HTTP request failed: {e}");
self.wallet().broadcast_tx(&fallback_tx)?;
println!("Broadcasted fallback transaction txid: {}", fallback_tx.compute_txid());
tracing::error!("HTTP request failed: {e}");
println!("Payjoin failed. To broadcast the fallback transaction, run:");
println!(" bitcoin-cli -rpcwallet=<wallet> sendrawtransaction \\");
println!(" {}", serialize_hex(&fallback_tx));
return Err(anyhow!("Payjoin failed; fallback transaction not broadcast"));

Comment thread payjoin-cli/src/app/v1.rs Outdated
Comment on lines +102 to +103
self.wallet().broadcast_tx(&fallback_tx)?;
println!("Broadcasted fallback transaction txid: {}", fallback_tx.compute_txid());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

~Same as above

Suggested change
self.wallet().broadcast_tx(&fallback_tx)?;
println!("Broadcasted fallback transaction txid: {}", fallback_tx.compute_txid());
println!("Payjoin failed. To broadcast the fallback transaction, run:");
println!(" bitcoin-cli -rpcwallet=<wallet> sendrawtransaction \\");
println!(" {}", serialize_hex(&fallback_tx));
return Err(anyhow!("Payjoin failed; fallback transaction not broadcast"));

@arminsabouri

Copy link
Copy Markdown
Contributor Author

For v2 senders I'd like a manual fallback <session-id>

Any reason v1 senders wouldn't have this sub command?

@DanGould

DanGould commented May 4, 2026

Copy link
Copy Markdown
Member

Any reason v1 senders wouldn't have this sub command?

  1. it's additional complexity to add to have broadcasting for legacy
  2. There's no state or session-id for v1s

@arminsabouri
arminsabouri force-pushed the demo-sender-fallback branch 2 times, most recently from 5af0456 to 080ab2c Compare May 4, 2026 18:02
Comment thread payjoin-cli/src/app/v2/mod.rs Resolved
Comment thread payjoin-cli/src/app/v1.rs
let uri = uri.check_pj_supported().map_err(|_| anyhow!("URI does not support Payjoin"))?;
let amount = uri.amount.ok_or_else(|| anyhow!("please specify the amount in the Uri"))?;
let psbt = self.create_original_psbt(&uri.address, amount, fee_rate)?;
let fallback_tx = psbt.clone().extract_tx()?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Previously this was obtained by serializing the psbt from body and then extracting.

Comment thread payjoin-cli/src/app/v2/mod.rs Resolved
@arminsabouri
arminsabouri force-pushed the demo-sender-fallback branch 2 times, most recently from 1cbb78e to 138b25e Compare May 4, 2026 18:19

@DanGould DanGould left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missing fallback prints path on v2-to-v1 path, but other than that this definitely addresses the core concern. I'd prefer to fix now so that we're confident in our sender fallback experience even if I left comments about our API that we might still want to adjust.

Everything else to do with this actual change is a nit.

There are a few open threads to fix beyond this PR before we can say sender fallback is payjoin-cli-1.0 reference-ready or even payjoin-1.0-ready.

Comment thread payjoin-cli/src/app/v2/mod.rs Resolved
Comment thread payjoin-cli/src/app/mod.rs Outdated
#[cfg(feature = "v2")]
async fn history(&self) -> Result<()>;
#[cfg(feature = "v2")]
async fn fallback_sender(&self, session_id: i64) -> Result<()>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could this take SessionId directly? It's already constructible across the crate, and v2/mod.rs does the wrap immediately on entry: SenderPersister::from_id(self.db.clone(), SessionId(session_id)). Taking SessionId here lets main.rs materialize it once at the CLI boundary, and lets the session_id() accessor return the domain type instead of punching back to i64. The trait method is already #[cfg(feature = "v2")]-gated so it's v2-specific anyway.

Sketch on my fork: DanGould@ed4615f (5 files, +10/-6), ((+6/-6) if you use fully-qualfied names instead of imports (my preference)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread payjoin-cli/src/app/v2/mod.rs Resolved
Comment thread payjoin-cli/src/app/v1.rs Outdated
.create_v1_post_request();
let http = http_agent(&self.config)?;
let body = String::from_utf8(req.body.clone()).unwrap();
println!("Sending fallback request to {}", req.url);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is really "Sending Original PSBT" in BIP 78 parlance -- noting to turn into its own issue/pr

Comment thread payjoin-cli/src/app/v2/mod.rs Resolved
Co-authored-by: zealsham <[email protected]>
Co-authored-by: DanGould <[email protected]>

The command broadcasts the fallback tx for a specific sender session.
If a session fails or is canceled, pj-cli outputs the fallback command
for that particular session.
Co-authored-by: DanGould <[email protected]>

Explicit fallback commands when either the http request or the psbt
processing fails.
@arminsabouri
arminsabouri force-pushed the demo-sender-fallback branch from b28a826 to a927035 Compare May 5, 2026 13:45
@DanGould
DanGould self-requested a review May 6, 2026 06:18

@DanGould DanGould left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the very quick turnaround here and attention to detail. I'll take care of the follow-ups. ACK a927035

@DanGould
DanGould merged commit 46f26b0 into payjoin:master May 6, 2026
14 checks passed
@benalleng benalleng mentioned this pull request Jun 7, 2026
17 tasks done
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