-
-
Notifications
You must be signed in to change notification settings - Fork 175
feat(lightning): add CLN NUT-15 MPP partial payment support #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christoph865
wants to merge
3
commits into
cashubtc:main
Choose a base branch
from
christoph865:fix/cln-mpp-nut15
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| # Skip the global mint_server fixture for lightning-specific tests | ||
| @pytest.fixture | ||
| def mint_server(): | ||
| yield None | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def mint(): | ||
| yield None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| from unittest.mock import AsyncMock, Mock, patch | ||
|
|
||
| import pytest | ||
| from bolt11 import Bolt11 | ||
|
|
||
| from cashu.core.base import MeltQuote, MeltQuoteState, Unit | ||
| from cashu.lightning.base import PaymentResult | ||
| from cashu.lightning.clnrest import CLNRestWallet | ||
|
|
||
|
|
||
| def create_melt_quote( | ||
| amount: int, unit: str = "msat", request: str = "lnbc10u1p0example" | ||
| ) -> MeltQuote: | ||
| return MeltQuote( | ||
| quote="quote_test_123", | ||
| method="bolt11", | ||
| request=request, | ||
| checking_id="checking_id_test", | ||
| unit=unit, | ||
| amount=amount, | ||
| fee_reserve=1000, | ||
| state=MeltQuoteState.unpaid, | ||
| created_time=None, | ||
| paid_time=None, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def wallet(monkeypatch: pytest.MonkeyPatch) -> CLNRestWallet: | ||
| monkeypatch.setattr( | ||
| "cashu.lightning.clnrest.settings.mint_clnrest_url", "https://localhost:3010" | ||
| ) | ||
| monkeypatch.setattr( | ||
| "cashu.lightning.clnrest.settings.mint_clnrest_rune", "test_rune" | ||
| ) | ||
| monkeypatch.setattr("cashu.lightning.clnrest.settings.mint_clnrest_cert", False) | ||
| monkeypatch.setattr( | ||
| "cashu.lightning.clnrest.settings.mint_clnrest_enable_mpp", True | ||
| ) | ||
| monkeypatch.setattr( | ||
| "cashu.lightning.clnrest.settings.mint_retry_exponential_backoff_base_delay", 1 | ||
| ) | ||
| monkeypatch.setattr( | ||
| "cashu.lightning.clnrest.settings.mint_retry_exponential_backoff_max_delay", 10 | ||
| ) | ||
|
|
||
| mock_client = Mock() | ||
| mock_client.post = AsyncMock() | ||
| monkeypatch.setattr( | ||
| "cashu.lightning.clnrest.httpx.AsyncClient", Mock(return_value=mock_client) | ||
| ) | ||
|
|
||
| wallet = CLNRestWallet(unit=Unit.sat) | ||
| wallet.supports_mpp = True | ||
| return wallet | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_mpp_detection_routes_to_partial(wallet: CLNRestWallet): | ||
| with patch("cashu.lightning.clnrest.decode") as mock_decode: | ||
| mock_invoice = Mock(spec=Bolt11) | ||
| mock_invoice.payment_hash = "hash789" | ||
| mock_invoice.amount_msat = 1000000 | ||
| mock_decode.return_value = mock_invoice | ||
|
|
||
| wallet.client.post = AsyncMock( | ||
| return_value=Mock( | ||
| is_error=False, | ||
| json=lambda: { | ||
| "payment_hash": "hash789", | ||
| "payment_preimage": "preimage_mpp", | ||
| "amount_sent_msat": 600100, | ||
| "amount_msat": 600000, | ||
| "status": "complete", | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| quote = create_melt_quote(amount=600000, unit="msat") | ||
| fee_limit_msat = 1000 | ||
|
|
||
| result = await wallet.pay_invoice(quote, fee_limit_msat) | ||
|
|
||
| assert result.result == PaymentResult.SETTLED | ||
| assert result.preimage == "preimage_mpp" | ||
| call_data = wallet.client.post.call_args.kwargs["data"] | ||
| assert "partial_msat" in call_data, "partial_msat must be sent to CLN for MPP" | ||
| assert call_data["partial_msat"] == 600000 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_mpp_disabled_returns_error(wallet: CLNRestWallet): | ||
| wallet.supports_mpp = False | ||
|
|
||
| with patch("cashu.lightning.clnrest.decode") as mock_decode: | ||
| mock_invoice = Mock(spec=Bolt11) | ||
| mock_invoice.payment_hash = "hash123" | ||
| mock_invoice.amount_msat = 1000000 | ||
| mock_decode.return_value = mock_invoice | ||
|
|
||
| quote = create_melt_quote(amount=600000, unit="msat") | ||
|
|
||
| result = await wallet.pay_invoice(quote, 1000) | ||
|
|
||
| assert result.result == PaymentResult.FAILED | ||
| assert result.error_message is not None | ||
| assert "does not support MPP" in result.error_message | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_full_payment_no_mpp(wallet: CLNRestWallet): | ||
| with patch("cashu.lightning.clnrest.decode") as mock_decode: | ||
| mock_invoice = Mock(spec=Bolt11) | ||
| mock_invoice.payment_hash = "full_payment_hash" | ||
| mock_invoice.amount_msat = 1000000 | ||
| mock_decode.return_value = mock_invoice | ||
|
|
||
| quote = create_melt_quote(amount=1000000, unit="msat") | ||
|
|
||
| wallet.client.post = AsyncMock( | ||
| return_value=Mock( | ||
| is_error=False, | ||
| json=lambda: { | ||
| "payment_hash": "full_payment_hash", | ||
| "payment_preimage": "preimage123", | ||
| "amount_sent_msat": 1000100, | ||
| "amount_msat": 1000000, | ||
| "status": "complete", | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| result = await wallet.pay_invoice(quote, 1000) | ||
|
|
||
| assert result.result == PaymentResult.SETTLED | ||
| assert result.preimage == "preimage123" | ||
| call_data = wallet.client.post.call_args.kwargs["data"] | ||
| assert "partial_msat" not in call_data, "partial_msat must not be sent for full payments" | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done on trimming the PR. Looking at the existing tests though, there's already an established pattern for testing Lightning backends in
tests/lightning/test_lightning_backends_mocked.py, including a test for CLNRest without MPP support.These new files duplicate some of the tests there with a different approach as well.
Could you have a look at the existing file and follow the pattern there to avoid fragmenting the testing convention?