MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_live_spendable

Function test_live_spendable

tests/test_askrene.py:1346–1409  ·  view source on GitHub ↗

Test we don't exceed spendable limits on a real network on nodes

(node_factory, bitcoind)

Source from the content-addressed store, hash-verified

1344
1345
1346def test_live_spendable(node_factory, bitcoind):
1347 """Test we don't exceed spendable limits on a real network on nodes"""
1348 l1, l2, l3 = node_factory.get_nodes(3)
1349 l1.fundwallet(10_000_000)
1350 l2.fundwallet(10_000_000)
1351 l1.rpc.connect(l2.info['id'], 'localhost', port=l2.port)
1352 l2.rpc.connect(l3.info['id'], 'localhost', port=l3.port)
1353
1354 capacities = (100_000, 100_000, 200_000, 300_000, 400_000)
1355 for capacity in capacities:
1356 l1.rpc.fundchannel(l2.info["id"], capacity, mindepth=1)
1357 l2.rpc.fundchannel(l3.info["id"], capacity, mindepth=1)
1358
1359 bitcoind.generate_block(1, wait_for_mempool=2)
1360 sync_blockheight(bitcoind, [l1, l2])
1361
1362 bitcoind.generate_block(5)
1363 wait_for(lambda: len(l1.rpc.listchannels()["channels"]) == 2 * 2 * len(capacities))
1364
1365 routes = l1.rpc.getroutes(
1366 source=l1.info["id"],
1367 destination=l3.info["id"],
1368 amount_msat=800_000_001,
1369 layers=["auto.localchans", "auto.sourcefree"],
1370 maxfee_msat=50_000_000,
1371 final_cltv=10,
1372 )
1373
1374 # Don't exceed spendable_msat
1375 maxes = {}
1376 for chan in l1.rpc.listpeerchannels()["channels"]:
1377 maxes["{}/{}".format(chan["short_channel_id"], chan["direction"])] = chan[
1378 "spendable_msat"
1379 ]
1380
1381 path_total = {}
1382 num_htlcs = {}
1383 for r in routes["routes"]:
1384 key = r["path"][0]["short_channel_id_dir"]
1385 path_total[key] = path_total.get(key, 0) + r["path"][0]["amount_in_msat"]
1386 num_htlcs[key] = num_htlcs.get(key, 0) + 1
1387
1388 # Take into account 645000msat (3750 feerate x 172 weight) per-HTLC reduction in capacity.
1389 for k in path_total.keys():
1390 if k in maxes:
1391 maxes[k] -= (3750 * 172) * (num_htlcs[k] - 1)
1392
1393 exceeded = {}
1394 for scidd in maxes.keys():
1395 if scidd in path_total:
1396 if path_total[scidd] > maxes[scidd]:
1397 exceeded[scidd] = f"Path total {path_total[scidd]} > spendable {maxes[scidd]}"
1398
1399 assert exceeded == {}
1400
1401 # No duplicate paths!
1402 for i in range(0, len(routes["routes"])):
1403 path_i = [p['short_channel_id_dir'] for p in routes["routes"][i]['path']]

Callers

nothing calls this directly

Calls 11

sync_blockheightFunction · 0.90
wait_forFunction · 0.90
get_nodesMethod · 0.80
fundwalletMethod · 0.80
generate_blockMethod · 0.80
getroutesMethod · 0.80
listpeerchannelsMethod · 0.80
connectMethod · 0.45
fundchannelMethod · 0.45
listchannelsMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected