Test that spendable_msat is accurate
(node_factory, bitcoind, anchors)
| 2582 | |
| 2583 | @pytest.mark.parametrize("anchors", [False, True]) |
| 2584 | def test_channel_spendable(node_factory, bitcoind, anchors): |
| 2585 | """Test that spendable_msat is accurate""" |
| 2586 | sats = 10**6 |
| 2587 | opts = {'plugin': os.path.join(os.getcwd(), 'tests/plugins/hold_invoice.py')} |
| 2588 | if anchors is False: |
| 2589 | opts['dev-force-features'] = "-23" |
| 2590 | l1, l2 = node_factory.line_graph(2, fundamount=sats, wait_for_announce=True, |
| 2591 | opts=opts) |
| 2592 | |
| 2593 | inv = l2.rpc.invoice('any', 'inv', 'for testing') |
| 2594 | payment_hash = inv['payment_hash'] |
| 2595 | |
| 2596 | # We should be able to spend this much, and not one msat more! |
| 2597 | amount = l1.rpc.listpeerchannels()['channels'][0]['spendable_msat'] |
| 2598 | # auto.localchans would refuse amount+1, so bypass it to test sendpay failure |
| 2599 | route = only_one(l1.rpc.getroutes(source=l1.info['id'], |
| 2600 | destination=l2.info['id'], |
| 2601 | amount_msat=amount + 1, |
| 2602 | layers=["auto.sourcefree"], |
| 2603 | maxfee_msat=10000000, |
| 2604 | final_cltv=9, |
| 2605 | maxparts=1)['routes'])['path'] |
| 2606 | l1.rpc.sendpay(route, payment_hash, payment_secret=inv['payment_secret']) |
| 2607 | |
| 2608 | # This should fail locally with "capacity exceeded" |
| 2609 | with pytest.raises(RpcError, match=r"Capacity exceeded.*'erring_index': 0"): |
| 2610 | l1.rpc.waitsendpay(payment_hash, TIMEOUT) |
| 2611 | |
| 2612 | # Exact amount should succeed. |
| 2613 | route = l1.single_route(l2.info['id'], amount) |
| 2614 | l1.rpc.sendpay(route, payment_hash, payment_secret=inv['payment_secret']) |
| 2615 | |
| 2616 | # Amount should drop to 0 once HTLC is sent; we have time, thanks to |
| 2617 | # hold_invoice.py plugin. |
| 2618 | wait_for(lambda: len(l1.rpc.listpeerchannels()['channels'][0]['htlcs']) == 1) |
| 2619 | assert l1.rpc.listpeerchannels()['channels'][0]['spendable_msat'] == Millisatoshi(0) |
| 2620 | # Tell hold_invoice.py to release hold |
| 2621 | open(os.path.join(l2.daemon.lightning_dir, TEST_NETWORK, "unhold"), "w").close() |
| 2622 | l1.rpc.waitsendpay(payment_hash, TIMEOUT) |
| 2623 | |
| 2624 | # Make sure l2 thinks it's all over. |
| 2625 | wait_for(lambda: len(l2.rpc.listpeerchannels()['channels'][0]['htlcs']) == 0) |
| 2626 | # Now, reverse should work similarly. |
| 2627 | inv = l1.rpc.invoice('any', 'inv', 'for testing') |
| 2628 | payment_hash = inv['payment_hash'] |
| 2629 | amount = l2.rpc.listpeerchannels()['channels'][0]['spendable_msat'] |
| 2630 | |
| 2631 | # Turns out we won't route this, as it's over max - reserve: |
| 2632 | # auto.localchans would refuse amount+1, so bypass it to test sendpay failure |
| 2633 | route = only_one(l2.rpc.getroutes(source=l2.info['id'], |
| 2634 | destination=l1.info['id'], |
| 2635 | amount_msat=amount + 1, |
| 2636 | layers=["auto.sourcefree"], |
| 2637 | maxfee_msat=10000000, |
| 2638 | final_cltv=9, |
| 2639 | maxparts=1)['routes'])['path'] |
| 2640 | l2.rpc.sendpay(route, payment_hash, payment_secret=inv['payment_secret']) |
| 2641 |
nothing calls this directly
no test coverage detected