Repository navigation
Demo sender fallback - #1510
Demo sender fallback#1510
Conversation
Coverage Report for CI Build 25380214465Coverage increased (+0.05%) to 85.14%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
|
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? |
88effb0 to
72c21a6
Compare
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 . |
DanGould
left a comment
There was a problem hiding this comment.
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:
- 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 .
- 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.
- Add
payjoin-cli fallback <session-id>subcommand callingmaybe_broadcast_sender_fallbackin auto-broadcast's place - 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.
| @@ -196,10 +196,18 @@ impl AppTrait for App { | |||
| "Sent fallback transaction hex: {:#}", | |||
There was a problem hiding this comment.
This is printed before the broadcast. "Sent" is an ambiguous word. To make println! more precise I suggest:
| "Sent fallback transaction hex: {:#}", | |
| println!("Fallback transaction txid: {}", fallback_tx.compute_txid()); | |
| println!( | |
| "Fallback transaction hex: {:#}", |
| @@ -86,11 +78,32 @@ impl AppTrait for App { | |||
| "Sent fallback transaction hex: {:#}", | |||
| payjoin::bitcoin::consensus::encode::serialize_hex(&fallback_tx) | |||
| ); | |||
There was a problem hiding this comment.
Same issue as v2:
This is printed before any broadcast. "Sent" is an ambiguous word. To make println! more precise I suggest:
| ); | |
| println!("Fallback transaction txid: {}", fallback_tx.compute_txid()); | |
| println!( | |
| "Fallback transaction hex: {:#}", | |
| payjoin::bitcoin::consensus::encode::serialize_hex(&fallback_tx) | |
| ); |
| self.wallet().broadcast_tx(&fallback_tx)?; | ||
| println!( | ||
| "Broadcasted fallback transaction txid: {}", | ||
| fallback_tx.compute_txid() | ||
| ); |
There was a problem hiding this comment.
Rather than auto broadcast, I believe manual broadcast is more appropriate, perhaps from a helper method that encapsulates the following.
| 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}") |
| 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 | ||
| ))); | ||
| } | ||
| } |
There was a problem hiding this comment.
Same request for manual broadcast here:
| 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}") |
| tracing::error!("HTTP request failed: {e}"); | ||
| self.wallet().broadcast_tx(&fallback_tx)?; | ||
| println!("Broadcasted fallback transaction txid: {}", fallback_tx.compute_txid()); |
There was a problem hiding this comment.
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.
| 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")); |
| self.wallet().broadcast_tx(&fallback_tx)?; | ||
| println!("Broadcasted fallback transaction txid: {}", fallback_tx.compute_txid()); |
There was a problem hiding this comment.
~Same as above
| 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")); |
Any reason v1 senders wouldn't have this sub command? |
|
5af0456 to
080ab2c
Compare
| 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()?; |
There was a problem hiding this comment.
Previously this was obtained by serializing the psbt from body and then extracting.
1cbb78e to
138b25e
Compare
DanGould
left a comment
There was a problem hiding this comment.
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.
| #[cfg(feature = "v2")] | ||
| async fn history(&self) -> Result<()>; | ||
| #[cfg(feature = "v2")] | ||
| async fn fallback_sender(&self, session_id: i64) -> Result<()>; |
There was a problem hiding this comment.
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)
| .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); |
There was a problem hiding this comment.
This is really "Sending Original PSBT" in BIP 78 parlance -- noting to turn into its own issue/pr
138b25e to
b28a826
Compare
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.
b28a826 to
a927035
Compare
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:
AI
in the body of this PR.