Skip to content

Commit dc9c4f4

Browse files
committed
Fix rpc-error due to quick transaction lookup
This pr addresses an error that occurs due to rpc client looking up the transaction right before it's on chain . Error: Transient error: Implementation error: Obtained failure status(500): Internal Server Error The delay introduced allows the transaction to broadcasted on chain before the lookup fix format issue fix long pooling test Implement pooling mechanism for transaction check Implement pooling mechanism for transaction check fix rpc error fix rpc error fix lint error
1 parent 84fdf03 commit dc9c4f4

1 file changed

Lines changed: 48 additions & 15 deletions

File tree

payjoin-cli/src/app/v2/mod.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -738,21 +738,54 @@ impl App {
738738
persister: &ReceiverPersister,
739739
) -> Result<()> {
740740
// On a session resumption, the receiver will resume again in this state.
741-
let _ = proposal
742-
.check_payment(
743-
|txid| {
744-
self.wallet()
745-
.get_raw_transaction(&txid)
746-
.map_err(|e| ImplementationError::from(e.into_boxed_dyn_error()))
747-
},
748-
|outpoint| {
749-
self.wallet()
750-
.is_outpoint_spent(&outpoint)
751-
.map_err(|e| ImplementationError::from(e.into_boxed_dyn_error()))
752-
},
753-
)
754-
.save(persister)?;
755-
Ok(())
741+
let poll_interval = tokio::time::Duration::from_millis(200);
742+
let timeout_duration = tokio::time::Duration::from_secs(5);
743+
744+
let mut interval = tokio::time::interval(poll_interval);
745+
interval.tick().await;
746+
747+
tracing::debug!("Polling for payment confirmation");
748+
749+
let result = tokio::time::timeout(timeout_duration, async {
750+
loop {
751+
interval.tick().await;
752+
let check_result = proposal
753+
.check_payment(
754+
|txid| {
755+
self.wallet()
756+
.get_raw_transaction(&txid)
757+
.map_err(|e| ImplementationError::from(e.into_boxed_dyn_error()))
758+
},
759+
|outpoint| {
760+
self.wallet()
761+
.is_outpoint_spent(&outpoint)
762+
.map_err(|e| ImplementationError::from(e.into_boxed_dyn_error()))
763+
},
764+
)
765+
.save(persister);
766+
767+
match check_result {
768+
Ok(_) => {
769+
println!("Payjoin transaction detected in the mempool!");
770+
return Ok(());
771+
}
772+
Err(_) => {
773+
// keep polling
774+
775+
continue;
776+
}
777+
}
778+
}
779+
})
780+
.await;
781+
782+
match result {
783+
Ok(ok) => ok,
784+
Err(_) => Err(anyhow!(
785+
"Timeout waiting for payment confirmation after {:?}",
786+
timeout_duration
787+
)),
788+
}
756789
}
757790

758791
async fn unwrap_relay_or_else_fetch(

0 commit comments

Comments
 (0)