pytest: neaten test_cln_sendpay_weirdness, rename.

1. Allow 'any' as an option to zeroconf-selective.py plugin so we can use
   it in line_graph where we don't know ids yet.
2. Use modern helpers like line_graph and remove debugging statement.
3. Don't use listchannels(): it's true that it shows local channels as well,
   but that's a quirk I'd like to remove.
4. Make flake8 happy.
5. Rename to be more specific now it's a more narrow test.

I manually tested that the test still failed with the fixes removed, too,
so it is still the same test!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-02-02 12:56:40 +10:30
parent 9e9686df20
commit ff1d537b87
2 changed files with 16 additions and 66 deletions

View File

@@ -12,7 +12,7 @@ def on_openchannel(openchannel, plugin, **kwargs):
plugin.log(repr(openchannel)) plugin.log(repr(openchannel))
mindepth = int(plugin.options['zeroconf-mindepth']['value']) mindepth = int(plugin.options['zeroconf-mindepth']['value'])
if openchannel['id'] == plugin.options['zeroconf-allow']['value']: if openchannel['id'] == plugin.options['zeroconf-allow']['value'] or plugin.options['zeroconf-allow']['value'] == 'any':
plugin.log(f"This peer is in the zeroconf allowlist, setting mindepth={mindepth}") plugin.log(f"This peer is in the zeroconf allowlist, setting mindepth={mindepth}")
return {'result': 'continue', 'mindepth': mindepth} return {'result': 'continue', 'mindepth': mindepth}
else: else:

View File

@@ -1,5 +1,6 @@
from fixtures import * # noqa: F401,F403 from fixtures import * # noqa: F401,F403
from fixtures import TEST_NETWORK from fixtures import TEST_NETWORK
from pathlib import Path
from io import BytesIO from io import BytesIO
from pyln.client import RpcError, Millisatoshi from pyln.client import RpcError, Millisatoshi
from pyln.proto.onion import TlvPayload from pyln.proto.onion import TlvPayload
@@ -10,7 +11,6 @@ from utils import (
) )
import copy import copy
import os import os
import json
import pytest import pytest
import random import random
import re import re
@@ -5305,83 +5305,33 @@ def test_payerkey(node_factory):
assert n.rpc.decode(b12)['invreq_payer_id'] == k assert n.rpc.decode(b12)['invreq_payer_id'] == k
def test_cln_sendpay_weirdness(bitcoind, node_factory): def test_pay_multichannel_use_zeroconf(bitcoind, node_factory):
# 0. Setup """Check that we use the zeroconf direct channel to pay when we need to"""
# 0. Setup normal channel, 200k sats.
# 0.1 Setup the payer node
l1 = node_factory.get_node()
print("CARL: DONE l1 setup")
# 0.2 Setup the receiver node
zeroconf_plugin = Path(__file__).parent / "plugins" / "zeroconf-selective.py" zeroconf_plugin = Path(__file__).parent / "plugins" / "zeroconf-selective.py"
l2 = node_factory.get_node( l1, l2 = node_factory.line_graph(2, wait_for_announce=False,
options={ fundamount=200_000,
'plugin': zeroconf_plugin, opts=[{},
'zeroconf-allow': l1.info['id'], {'plugin': zeroconf_plugin,
}, 'zeroconf-allow': 'any'}])
)
print("CARL: DONE l2 setup")
# 0.3 Connect the nodes
l1.connect(l2)
# 0.4 Open a normal channel l1 -> l2
normal_sats = 200_000
print("CARL: Opening normal channel btw l1 and l2")
l1.fundchannel(l2, amount=normal_sats) # This will mine a block!
print("CARL: DONE: Opening normal channel btw l1 and l2")
# 0.5 Define a helper that syncs nodes to bitcoind and returns the blockheight
def synced_blockheight(nodes):
blockcount = bitcoind.rpc.getblockcount()
print(f"CARL: bitcoind blockcount {blockcount}")
wait_for(lambda: all([node.rpc.getinfo()['blockheight'] == blockcount for node in nodes]))
return blockcount
# 1. Open a zeoconf channel l1 -> l2 # 1. Open a zeoconf channel l1 -> l2
zeroconf_sats = 1_000_000 zeroconf_sats = 1_000_000
# 1.1 Add funds to l1's wallet for the channel open # 1.1 Add funds to l1's wallet for the channel open
l1.fundwallet(zeroconf_sats * 2) # This will mine a block! l1.fundwallet(zeroconf_sats * 2) # This will mine a block!
fundchannel_blockheight = synced_blockheight([l1, l2]) sync_blockheight(bitcoind, [l1, l2])
print(f"CARL: blockheight before fundchannel: {fundchannel_blockheight}")
# 1.2 Open the zeroconf channel # 1.2 Open the zeroconf channel
print("CARL: Opening zeroconf channel btw l1 and l2")
l1.rpc.fundchannel(l2.info['id'], zeroconf_sats, announce=False, mindepth=0) l1.rpc.fundchannel(l2.info['id'], zeroconf_sats, announce=False, mindepth=0)
print("CARL: DONE Opening zeroconf channel btw l1 and l2")
# 1.3 Wait until the channel becomes active
print("CARL: Waiting until channel becomes active")
num_channels = 4
wait_for(lambda: len(l1.rpc.listchannels()['channels']) == num_channels)
wait_for(lambda: len(l2.rpc.listchannels()['channels']) == num_channels)
print("CARL: DONE Waiting until channel becomes active")
# 1.4 Print out channels as seen from both nodes
channels = l1.rpc.listchannels()
print(f"CARL: l1's channels after zeroconf channel open:\n{json.dumps(channels, indent=4)}")
channels = l2.rpc.listchannels()
print(f"CARL: l2's channels after zeroconf channel open:\n{json.dumps(channels, indent=4)}")
# 1.3 Wait until all channels active.
wait_for(lambda: all([c['state'] == 'CHANNELD_NORMAL' for c in l1.rpc.listpeerchannels()['channels'] + l2.rpc.listpeerchannels()['channels']]))
# 2. Have l2 generate an invoice to be paid # 2. Have l2 generate an invoice to be paid
invoice_sats = 500_000 invoice_sats = "500000sat"
inv = l2.rpc.invoice(invoice_sats * 1_000, "test", "test") inv = l2.rpc.invoice(invoice_sats, "test", "test")
psecret = l1.rpc.decodepay(inv['bolt11'])['payment_secret']
rhash = inv['payment_hash']
# 3. Send a payment over the zeroconf channel # 3. Send a payment over the zeroconf channel
riskfactor = 0 riskfactor = 0
## 3.1 Sanity check that we're at the block height we expect
payment_blockheight = synced_blockheight([l1, l2])
print(f"CARL: blockheight before payment: {payment_blockheight}")
assert(fundchannel_blockheight == payment_blockheight)
## 3.2 Send the payment via PAY
print("CARL: PAY via l1's route to l2 for invoice")
l1.rpc.pay(inv['bolt11'], riskfactor=riskfactor) l1.rpc.pay(inv['bolt11'], riskfactor=riskfactor)
print("CARL: DONE: PAY via l1's route to l2 for invoice")