Add prefer_async support via post body#887
Add prefer_async support via post body#887robwoodgate wants to merge 12 commits intocashubtc:mainfrom
Conversation
Return pending immediately and settle melts in background when prefer_async is set, with regression coverage. Note: cashu/mint/ledger.py line endings normalized to LF.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #887 +/- ##
==========================================
+ Coverage 50.02% 51.73% +1.70%
==========================================
Files 89 88 -1
Lines 10545 10313 -232
==========================================
+ Hits 5275 5335 +60
+ Misses 5270 4978 -292 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
a1denvalu3
left a comment
There was a problem hiding this comment.
Thank you for the PR, but prefer_async is being worked on in #801 . I'll update that one.
| try: | ||
| PublicKey(bytes.fromhex(data)) | ||
| except Exception as exc: | ||
| raise ValueError("Invalid pubkey") from exc |
There was a problem hiding this comment.
Related in the sense that CI was intermittently failing tests, and I wanted to remove some of the flakey-ness and get a green run. It's part of the "Incidental changes" noted in the PR summary
There was a problem hiding this comment.
Looks like the clanker replaced the whole file, making diff impossible.
There was a problem hiding this comment.
I mentioned the line endings in the PR notes. It was using non-standard line endings, so I normalized it.
Use --ignore-space-changes to see the raw change. But if you are already working on this feature in a different PR (sorry, didn't see, and the change in spec is < 1 week old) then no need.
There was a problem hiding this comment.
Here you go:
robw:nutshell [prefer-async-post-body] $ git diff 9128bc7 --ignore-space-change cashu/mint/ledger.py
diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py
index b60a1f2..422e837 100644
--- a/cashu/mint/ledger.py
+++ b/cashu/mint/ledger.py
@@ -867,6 +867,7 @@ class Ledger(
proofs: List[Proof],
quote: str,
outputs: Optional[List[BlindedMessage]] = None,
+ prefer_async: Optional[bool] = None,
) -> PostMeltQuoteResponse:
"""Invalidates proofs and pays a Lightning invoice.
@@ -939,6 +940,69 @@ class Ledger(
if outputs:
await self._store_blinded_messages(outputs, melt_id=melt_quote.quote)
+ if prefer_async:
+ pending_response = PostMeltQuoteResponse.from_melt_quote(
+ melt_quote.model_copy()
+ )
+ asyncio.create_task(
+ self._melt_background_task(
+ melt_quote=melt_quote,
+ proofs=proofs,
+ outputs=outputs,
+ previous_state=previous_state,
+ fee_reserve_provided=fee_reserve_provided,
+ unit=unit,
+ method=method,
+ )
+ )
+ return pending_response
+ return await self._settle_melt_after_pending(
+ melt_quote=melt_quote,
+ proofs=proofs,
+ outputs=outputs,
+ previous_state=previous_state,
+ fee_reserve_provided=fee_reserve_provided,
+ unit=unit,
+ method=method,
+ )
+
+ async def _melt_background_task(
+ self,
+ *,
+ melt_quote: MeltQuote,
+ proofs: List[Proof],
+ outputs: Optional[List[BlindedMessage]],
+ previous_state: MeltQuoteState,
+ fee_reserve_provided: int,
+ unit: Unit,
+ method: Method,
+ ) -> None:
+ try:
+ await self._settle_melt_after_pending(
+ melt_quote=melt_quote,
+ proofs=proofs,
+ outputs=outputs,
+ previous_state=previous_state,
+ fee_reserve_provided=fee_reserve_provided,
+ unit=unit,
+ method=method,
+ )
+ except Exception as e:
+ logger.error(
+ f"Async melt task failed for quote {melt_quote.quote}: {e}"
+ )
+
+ async def _settle_melt_after_pending(
+ self,
+ *,
+ melt_quote: MeltQuote,
+ proofs: List[Proof],
+ outputs: Optional[List[BlindedMessage]],
+ previous_state: MeltQuoteState,
+ fee_reserve_provided: int,
+ unit: Unit,
+ method: Method,
+ ) -> PostMeltQuoteResponse:
# if the melt corresponds to an internal mint, mark both as paid
melt_quote = await self.melt_mint_settle_internally(melt_quote, proofs)
# quote not paid yet (not internal), pay it with the backend
(END)
| await conn.execute( | ||
| """ | ||
| SELECT pg_terminate_backend(pid) | ||
| FROM pg_stat_activity | ||
| WHERE datname = current_database() | ||
| AND pid <> pg_backend_pid(); | ||
| """ | ||
| ) |
There was a problem hiding this comment.
This fixes a CI abnormal exit where the DB is torn down with active threads.
In fact, every commit in this PR after a96882d is trying to fix up CI, as noted in the PR notes
| ) | ||
| await wallet.async_init() | ||
| yield wallet | ||
| await wallet.db.engine.dispose() |
There was a problem hiding this comment.
Maybe this deserves a PR on its own?
There was a problem hiding this comment.
Please be my guest - use or discard anything as you wish
|
@a1denvalu3 - before you bin this PR out of hand, I have tested it against cashu-ts integration tests running cashubtc/cashu-ts#500 |
Related NUT: cashubtc/nuts#339
PR Summary
incidental Changes for CI test stability
The first 4 commits in this PR implement the feature, the rest are aimed at stabilizing CI to make this PR green.