| 1 |
import random |
| 2 |
import unittest |
| 3 |
from math import inf |
| 4 |
from unittest import mock |
| 5 |
from typing import Optional |
| 6 |
from os import urandom |
| 7 |
|
| 8 |
from electrum import util |
| 9 |
from electrum.channel_db import NodeInfo |
| 10 |
from electrum.onion_message import is_onion_message_node |
| 11 |
from electrum.trampoline import (create_trampoline_onion, _allocate_fee_budget_among_route, PLACEHOLDER_FEE, |
| 12 |
get_trampoline_budget) |
| 13 |
from electrum.util import bfh |
| 14 |
from electrum.lnutil import ShortChannelID, LnFeatures, PaymentFeeBudget |
| 15 |
from electrum.lnonion import (OnionHopsDataSingle, new_onion_packet, |
| 16 |
process_onion_packet, _decode_onion_error, decode_onion_error, |
| 17 |
OnionFailureCode) |
| 18 |
from electrum import bitcoin, lnrouter |
| 19 |
from electrum.constants import BitcoinTestnet |
| 20 |
from electrum.simple_config import SimpleConfig |
| 21 |
from electrum.lnrouter import (PathEdge, LiquidityHintMgr, DEFAULT_PENALTY_PROPORTIONAL_MILLIONTH, |
| 22 |
DEFAULT_PENALTY_BASE_MSAT, fee_for_edge_msat, LNPaymentTRoute, TrampolineEdge, |
| 23 |
HINT_DURATION) |
| 24 |
|
| 25 |
from . import ElectrumTestCase |
| 26 |
from .test_bitcoin import needs_test_with_all_chacha20_implementations |
| 27 |
|
| 28 |
|
| 29 |
def channel(number: int) -> ShortChannelID: |
| 30 |
return ShortChannelID(bfh(format(number, '016x'))) |
| 31 |
|
| 32 |
|
| 33 |
def node(character: str) -> bytes: |
| 34 |
return b'\x02' + f'{character}'.encode() * 32 |
| 35 |
|
| 36 |
|
| 37 |
def alias(character: str) -> bytes: |
| 38 |
return (character * 8).encode('utf-8') |
| 39 |
|
| 40 |
|
| 41 |
def node_features(extra: LnFeatures = None) -> bytes: |
| 42 |
lnf = LnFeatures(0) | LnFeatures.VAR_ONION_OPT |
| 43 |
if extra: |
| 44 |
lnf |= extra |
| 45 |
return lnf.to_bytes(8, 'big') |
| 46 |
|
| 47 |
|
| 48 |
class Test_LNRouter(ElectrumTestCase): |
| 49 |
TESTNET = True |
| 50 |
|
| 51 |
cdb = None |
| 52 |
|
| 53 |
def setUp(self): |
| 54 |
super().setUp() |
| 55 |
self.config = SimpleConfig({'electrum_path': self.electrum_path}) |
| 56 |
self.assertIsNone(self.cdb) |
| 57 |
|
| 58 |
async def asyncTearDown(self): |
| 59 |
|
| 60 |
if self.cdb: |
| 61 |
self.cdb.stop() |
| 62 |
await self.cdb.stopped_event.wait() |
| 63 |
await super().asyncTearDown() |
| 64 |
|
| 65 |
def prepare_graph(self): |
| 66 |
""" |
| 67 |
Network topology with channel ids: |
| 68 |
3 |
| 69 |
A --- B |
| 70 |
| 2/ | |
| 71 |
6 | E | 1 |
| 72 |
| /5 \7 | |
| 73 |
D --- C |
| 74 |
4 |
| 75 |
valid routes from A -> E: |
| 76 |
A -3-> B -2-> E |
| 77 |
A -6-> D -5-> E |
| 78 |
A -6-> D -4-> C -7-> E |
| 79 |
A -3-> B -1-> C -7-> E |
| 80 |
A -6-> D -4-> C -1-> B -2-> E |
| 81 |
A -3-> B -1-> C -4-> D -5-> E |
| 82 |
""" |
| 83 |
|
| 84 |
class fake_network: |
| 85 |
config = self.config |
| 86 |
asyncio_loop = util.get_asyncio_loop() |
| 87 |
trigger_callback = lambda *args: None |
| 88 |
register_callback = lambda *args: None |
| 89 |
interface = None |
| 90 |
|
| 91 |
fake_network.channel_db = lnrouter.ChannelDB(fake_network()) |
| 92 |
fake_network.channel_db.data_loaded.set() |
| 93 |
self.cdb = fake_network.channel_db |
| 94 |
self.path_finder = lnrouter.LNPathFinder(self.cdb) |
| 95 |
self.assertEqual(self.cdb.num_channels, 0) |
| 96 |
self.cdb.add_channel_announcements({ |
| 97 |
'node_id_1': node('b'), 'node_id_2': node('c'), |
| 98 |
'bitcoin_key_1': node('b'), 'bitcoin_key_2': node('c'), |
| 99 |
'short_channel_id': channel(1), |
| 100 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 101 |
'len': 0, 'features': b'' |
| 102 |
}, trusted=True) |
| 103 |
self.assertEqual(self.cdb.num_channels, 1) |
| 104 |
self.cdb.add_channel_announcements({ |
| 105 |
'node_id_1': node('b'), 'node_id_2': node('e'), |
| 106 |
'bitcoin_key_1': node('b'), 'bitcoin_key_2': node('e'), |
| 107 |
'short_channel_id': channel(2), |
| 108 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 109 |
'len': 0, 'features': b'' |
| 110 |
}, trusted=True) |
| 111 |
self.cdb.add_channel_announcements({ |
| 112 |
'node_id_1': node('a'), 'node_id_2': node('b'), |
| 113 |
'bitcoin_key_1': node('a'), 'bitcoin_key_2': node('b'), |
| 114 |
'short_channel_id': channel(3), |
| 115 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 116 |
'len': 0, 'features': b'' |
| 117 |
}, trusted=True) |
| 118 |
self.cdb.add_channel_announcements({ |
| 119 |
'node_id_1': node('c'), 'node_id_2': node('d'), |
| 120 |
'bitcoin_key_1': node('c'), 'bitcoin_key_2': node('d'), |
| 121 |
'short_channel_id': channel(4), |
| 122 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 123 |
'len': 0, 'features': b'' |
| 124 |
}, trusted=True) |
| 125 |
self.cdb.add_channel_announcements({ |
| 126 |
'node_id_1': node('d'), 'node_id_2': node('e'), |
| 127 |
'bitcoin_key_1': node('d'), 'bitcoin_key_2': node('e'), |
| 128 |
'short_channel_id': channel(5), |
| 129 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 130 |
'len': 0, 'features': b'' |
| 131 |
}, trusted=True) |
| 132 |
self.cdb.add_channel_announcements({ |
| 133 |
'node_id_1': node('a'), 'node_id_2': node('d'), |
| 134 |
'bitcoin_key_1': node('a'), 'bitcoin_key_2': node('d'), |
| 135 |
'short_channel_id': channel(6), |
| 136 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 137 |
'len': 0, 'features': b'' |
| 138 |
}, trusted=True) |
| 139 |
self.cdb.add_channel_announcements({ |
| 140 |
'node_id_1': node('c'), 'node_id_2': node('e'), |
| 141 |
'bitcoin_key_1': node('c'), 'bitcoin_key_2': node('e'), |
| 142 |
'short_channel_id': channel(7), |
| 143 |
'chain_hash': BitcoinTestnet.rev_genesis_bytes(), |
| 144 |
'len': 0, 'features': b'' |
| 145 |
}, trusted=True) |
| 146 |
|
| 147 |
self.cdb.add_node_announcements({ |
| 148 |
'node_id': node('a'), |
| 149 |
'alias': alias('a'), |
| 150 |
'addresses': [], |
| 151 |
'features': node_features(LnFeatures.OPTION_ONION_MESSAGE_OPT), |
| 152 |
'timestamp': 0 |
| 153 |
}) |
| 154 |
self.cdb.add_node_announcements({ |
| 155 |
'node_id': node('b'), |
| 156 |
'alias': alias('b'), |
| 157 |
'addresses': [], |
| 158 |
'features': node_features(), |
| 159 |
'timestamp': 0 |
| 160 |
}) |
| 161 |
self.cdb.add_node_announcements({ |
| 162 |
'node_id': node('c'), |
| 163 |
'alias': alias('c'), |
| 164 |
'addresses': [], |
| 165 |
'features': node_features(LnFeatures.OPTION_ONION_MESSAGE_OPT), |
| 166 |
'timestamp': 0 |
| 167 |
}) |
| 168 |
self.cdb.add_node_announcements({ |
| 169 |
'node_id': node('d'), |
| 170 |
'alias': alias('d'), |
| 171 |
'addresses': [], |
| 172 |
'features': node_features(LnFeatures.OPTION_ONION_MESSAGE_OPT), |
| 173 |
'timestamp': 0 |
| 174 |
}) |
| 175 |
self.cdb.add_node_announcements({ |
| 176 |
'node_id': node('e'), |
| 177 |
'alias': alias('e'), |
| 178 |
'addresses': [], |
| 179 |
'features': node_features(), |
| 180 |
'timestamp': 0 |
| 181 |
}) |
| 182 |
|
| 183 |
def add_chan_upd(payload): |
| 184 |
self.cdb.add_channel_update(payload, verify=False) |
| 185 |
|
| 186 |
add_chan_upd({'short_channel_id': channel(1), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 187 |
add_chan_upd({'short_channel_id': channel(1), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 188 |
add_chan_upd({'short_channel_id': channel(2), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 99, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 189 |
add_chan_upd({'short_channel_id': channel(2), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 190 |
add_chan_upd({'short_channel_id': channel(3), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 191 |
add_chan_upd({'short_channel_id': channel(3), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 192 |
add_chan_upd({'short_channel_id': channel(4), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 193 |
add_chan_upd({'short_channel_id': channel(4), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 194 |
add_chan_upd({'short_channel_id': channel(5), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 195 |
add_chan_upd({'short_channel_id': channel(5), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 999, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 196 |
add_chan_upd({'short_channel_id': channel(6), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 200, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 197 |
add_chan_upd({'short_channel_id': channel(6), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 198 |
add_chan_upd({'short_channel_id': channel(7), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 199 |
add_chan_upd({'short_channel_id': channel(7), 'message_flags': b'\x00', 'channel_flags': b'\x01', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 150, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) |
| 200 |
|
| 201 |
async def test_find_path_for_payment(self): |
| 202 |
self.prepare_graph() |
| 203 |
amount_to_send = 100000 |
| 204 |
|
| 205 |
path = self.path_finder.find_path_for_payment( |
| 206 |
nodeA=node('a'), |
| 207 |
nodeB=node('e'), |
| 208 |
invoice_amount_msat=amount_to_send) |
| 209 |
self.assertEqual([ |
| 210 |
PathEdge(start_node=node('a'), end_node=node('b'), short_channel_id=channel(3)), |
| 211 |
PathEdge(start_node=node('b'), end_node=node('e'), short_channel_id=channel(2)), |
| 212 |
], path) |
| 213 |
|
| 214 |
route = self.path_finder.create_route_from_path(path) |
| 215 |
self.assertEqual(node('b'), route[0].node_id) |
| 216 |
self.assertEqual(channel(3), route[0].short_channel_id) |
| 217 |
|
| 218 |
async def test_find_path_for_payment_with_node_filter(self): |
| 219 |
self.prepare_graph() |
| 220 |
amount_to_send = 100000 |
| 221 |
|
| 222 |
def node_filter(node_id: bytes, node_info: 'NodeInfo'): |
| 223 |
return node_info.node_id != node('b') |
| 224 |
|
| 225 |
path = self.path_finder.find_path_for_payment( |
| 226 |
nodeA=node('a'), |
| 227 |
nodeB=node('e'), |
| 228 |
invoice_amount_msat=amount_to_send, |
| 229 |
node_filter=node_filter) |
| 230 |
self.assertEqual([ |
| 231 |
PathEdge(start_node=node('a'), end_node=node('d'), short_channel_id=channel(6)), |
| 232 |
PathEdge(start_node=node('d'), end_node=node('e'), short_channel_id=channel(5)), |
| 233 |
], path) |
| 234 |
|
| 235 |
route = self.path_finder.create_route_from_path(path) |
| 236 |
self.assertEqual(node('d'), route[0].node_id) |
| 237 |
self.assertEqual(channel(6), route[0].short_channel_id) |
| 238 |
|
| 239 |
async def test_find_path_liquidity_hints(self): |
| 240 |
self.prepare_graph() |
| 241 |
amount_to_send = 100000 |
| 242 |
|
| 243 |
""" |
| 244 |
assume failure over channel 2, B -> E |
| 245 |
A -3-> B |-2-> E |
| 246 |
A -6-> D -5-> E <= chosen path |
| 247 |
A -6-> D -4-> C -7-> E |
| 248 |
A -3-> B -1-> C -7-> E |
| 249 |
A -6-> D -4-> C -1-> B -2-> E |
| 250 |
A -3-> B -1-> C -4-> D -5-> E |
| 251 |
""" |
| 252 |
self.path_finder.liquidity_hints.update_cannot_send(node('b'), node('e'), channel(2), amount_msat=amount_to_send - 1) |
| 253 |
path = self.path_finder.find_path_for_payment( |
| 254 |
nodeA=node('a'), |
| 255 |
nodeB=node('e'), |
| 256 |
invoice_amount_msat=amount_to_send) |
| 257 |
self.assertEqual(channel(6), path[0].short_channel_id) |
| 258 |
self.assertEqual(channel(5), path[1].short_channel_id) |
| 259 |
|
| 260 |
""" |
| 261 |
assume failure over channel 5, D -> E |
| 262 |
A -3-> B |-2-> E |
| 263 |
A -6-> D |-5-> E |
| 264 |
A -6-> D -4-> C -7-> E |
| 265 |
A -3-> B -1-> C -7-> E <= chosen path |
| 266 |
A -6-> D -4-> C -1-> B |-2-> E |
| 267 |
A -3-> B -1-> C -4-> D |-5-> E |
| 268 |
""" |
| 269 |
self.path_finder.liquidity_hints.update_cannot_send(node('d'), node('e'), channel(5), amount_msat=amount_to_send - 1) |
| 270 |
path = self.path_finder.find_path_for_payment( |
| 271 |
nodeA=node('a'), |
| 272 |
nodeB=node('e'), |
| 273 |
invoice_amount_msat=amount_to_send) |
| 274 |
self.assertEqual(channel(3), path[0].short_channel_id) |
| 275 |
self.assertEqual(channel(1), path[1].short_channel_id) |
| 276 |
self.assertEqual(channel(7), path[2].short_channel_id) |
| 277 |
|
| 278 |
""" |
| 279 |
assume success over channel 4, D -> C |
| 280 |
A -3-> B |-2-> E |
| 281 |
A -6-> D |-5-> E |
| 282 |
A -6-> D -4-> C -7-> E <= smaller penalty: chosen path |
| 283 |
A -3-> B -1-> C -7-> E |
| 284 |
A -6-> D -4-> C -1-> B |-2-> E |
| 285 |
A -3-> B -1-> C -4-> D |-5-> E |
| 286 |
""" |
| 287 |
self.path_finder.liquidity_hints.update_can_send(node('d'), node('c'), channel(4), amount_msat=amount_to_send + 1000) |
| 288 |
path = self.path_finder.find_path_for_payment( |
| 289 |
nodeA=node('a'), |
| 290 |
nodeB=node('e'), |
| 291 |
invoice_amount_msat=amount_to_send) |
| 292 |
self.assertEqual(channel(6), path[0].short_channel_id) |
| 293 |
self.assertEqual(channel(4), path[1].short_channel_id) |
| 294 |
self.assertEqual(channel(7), path[2].short_channel_id) |
| 295 |
|
| 296 |
async def test_find_path_liquidity_hints_inflight_htlcs(self): |
| 297 |
self.prepare_graph() |
| 298 |
amount_to_send = 100000 |
| 299 |
|
| 300 |
""" |
| 301 |
add inflight htlc to channel 2, B -> E |
| 302 |
A -3-> B -2(1)-> E |
| 303 |
A -6-> D -5-> E <= chosen path |
| 304 |
A -6-> D -4-> C -7-> E |
| 305 |
A -3-> B -1-> C -7-> E |
| 306 |
A -6-> D -4-> C -1-> B -2-> E |
| 307 |
A -3-> B -1-> C -4-> D -5-> E |
| 308 |
""" |
| 309 |
self.path_finder.liquidity_hints.add_htlc(node('b'), node('e'), channel(2)) |
| 310 |
path = self.path_finder.find_path_for_payment( |
| 311 |
nodeA=node('a'), |
| 312 |
nodeB=node('e'), |
| 313 |
invoice_amount_msat=amount_to_send) |
| 314 |
self.assertEqual(channel(6), path[0].short_channel_id) |
| 315 |
self.assertEqual(channel(5), path[1].short_channel_id) |
| 316 |
|
| 317 |
""" |
| 318 |
remove inflight htlc from channel 2, B -> E |
| 319 |
A -3-> B -2(0)-> E <= chosen path |
| 320 |
A -6-> D -5-> E |
| 321 |
A -6-> D -4-> C -7-> E |
| 322 |
A -3-> B -1-> C -7-> E |
| 323 |
A -6-> D -4-> C -1-> B -2-> E |
| 324 |
A -3-> B -1-> C -4-> D -5-> E |
| 325 |
""" |
| 326 |
self.path_finder.liquidity_hints.remove_htlc(node('b'), node('e'), channel(2)) |
| 327 |
path = self.path_finder.find_path_for_payment( |
| 328 |
nodeA=node('a'), |
| 329 |
nodeB=node('e'), |
| 330 |
invoice_amount_msat=amount_to_send) |
| 331 |
self.assertEqual(channel(3), path[0].short_channel_id) |
| 332 |
self.assertEqual(channel(2), path[1].short_channel_id) |
| 333 |
|
| 334 |
def test_liquidity_hints(self): |
| 335 |
liquidity_hints = LiquidityHintMgr() |
| 336 |
node_from = bytes(0) |
| 337 |
node_to = bytes(1) |
| 338 |
channel_id = ShortChannelID.from_components(0, 0, 0) |
| 339 |
amount_to_send = 1_000_000 |
| 340 |
|
| 341 |
|
| 342 |
self.assertEqual( |
| 343 |
fee_for_edge_msat(amount_to_send, DEFAULT_PENALTY_BASE_MSAT, DEFAULT_PENALTY_PROPORTIONAL_MILLIONTH), |
| 344 |
liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=amount_to_send) |
| 345 |
) |
| 346 |
liquidity_hints.update_can_send(node_from, node_to, channel_id, amount_msat=1_000_000) |
| 347 |
liquidity_hints.update_cannot_send(node_from, node_to, channel_id, amount_msat=2_000_000) |
| 348 |
hint = liquidity_hints.get_hint(channel_id) |
| 349 |
self.assertEqual(1_000_000, hint.can_send(node_from < node_to)) |
| 350 |
self.assertEqual(None, hint.cannot_send(node_to < node_from)) |
| 351 |
self.assertEqual(2_000_000, hint.cannot_send(node_from < node_to)) |
| 352 |
|
| 353 |
self.assertEqual(2_000_000, hint.can_send(node_to < node_from)) |
| 354 |
|
| 355 |
|
| 356 |
self.assertEqual(0., liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=1_000_000)) |
| 357 |
self.assertEqual(650, liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=1_500_000)) |
| 358 |
self.assertEqual(inf, liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=2_000_000)) |
| 359 |
|
| 360 |
|
| 361 |
liquidity_hints.update_can_send(node_from, node_to, channel_id, amount_msat=500_000) |
| 362 |
hint = liquidity_hints.get_hint(channel_id) |
| 363 |
self.assertEqual(1_000_000, hint.can_send(node_from < node_to)) |
| 364 |
|
| 365 |
|
| 366 |
liquidity_hints.update_can_send(node_from, node_to, channel_id, amount_msat=3_000_000) |
| 367 |
hint = liquidity_hints.get_hint(channel_id) |
| 368 |
self.assertEqual(3_000_000, hint.can_send(node_from < node_to)) |
| 369 |
self.assertEqual(None, hint.cannot_send(node_from < node_to)) |
| 370 |
|
| 371 |
|
| 372 |
liquidity_hints.reset_liquidity_hints() |
| 373 |
liquidity_hints.add_htlc(node_from, node_to, channel_id) |
| 374 |
liquidity_hints.get_hint(channel_id) |
| 375 |
|
| 376 |
self.assertEqual(1200, liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=1_000_000)) |
| 377 |
|
| 378 |
def test_liquidity_hints_expiry(self): |
| 379 |
liquidity_hints = LiquidityHintMgr() |
| 380 |
node_from = bytes(0) |
| 381 |
node_to = bytes(1) |
| 382 |
channel_id = ShortChannelID.from_components(0, 0, 0) |
| 383 |
mock_time = 1_000_000 |
| 384 |
with mock.patch.object(lnrouter, 'now', lambda: mock_time): |
| 385 |
liquidity_hints.update_cannot_send(node_from, node_to, channel_id, amount_msat=1_000_000) |
| 386 |
self.assertEqual(inf, liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=1_000_000)) |
| 387 |
|
| 388 |
mock_time += HINT_DURATION + 1 |
| 389 |
liquidity_hints.update_can_send(node_from, node_to, channel_id, amount_msat=10_000) |
| 390 |
hint = liquidity_hints.get_hint(channel_id) |
| 391 |
self.assertEqual(10_000, hint.can_send(node_from < node_to)) |
| 392 |
self.assertEqual(None, hint.cannot_send(node_from < node_to)) |
| 393 |
self.assertNotEqual(inf, liquidity_hints.penalty(node_from, node_to, channel_id, amount_msat=1_000_000)) |
| 394 |
|
| 395 |
mock_time += HINT_DURATION + 1 |
| 396 |
liquidity_hints.update_can_send(node_from, node_to, channel_id, amount_msat=5_000) |
| 397 |
hint = liquidity_hints.get_hint(channel_id) |
| 398 |
self.assertEqual(5_000, hint.can_send(node_from < node_to)) |
| 399 |
|
| 400 |
def test_reset_liquidity_hints_clears_inflight_htlcs(self): |
| 401 |
liquidity_hints = LiquidityHintMgr() |
| 402 |
node_from, node_to = bytes(0), bytes(1) |
| 403 |
channel_id = ShortChannelID.from_components(0, 0, 0) |
| 404 |
liquidity_hints.add_htlc(node_from, node_to, channel_id) |
| 405 |
liquidity_hints.add_htlc(node_to, node_from, channel_id) |
| 406 |
hint = liquidity_hints.get_hint(channel_id) |
| 407 |
self.assertEqual(1, hint.num_inflight_htlcs(node_from < node_to)) |
| 408 |
self.assertEqual(1, hint.num_inflight_htlcs(node_to < node_from)) |
| 409 |
liquidity_hints.reset_liquidity_hints() |
| 410 |
self.assertEqual(0, hint.num_inflight_htlcs(node_from < node_to)) |
| 411 |
self.assertEqual(0, hint.num_inflight_htlcs(node_to < node_from)) |
| 412 |
|
| 413 |
@needs_test_with_all_chacha20_implementations |
| 414 |
def test_new_onion_packet(self): |
| 415 |
|
| 416 |
payment_path_pubkeys = [ |
| 417 |
bfh('02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619'), |
| 418 |
bfh('0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c'), |
| 419 |
bfh('027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007'), |
| 420 |
bfh('032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991'), |
| 421 |
bfh('02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145'), |
| 422 |
] |
| 423 |
session_key = bfh('4141414141414141414141414141414141414141414141414141414141414141') |
| 424 |
associated_data = bfh('4242424242424242424242424242424242424242424242424242424242424242') |
| 425 |
hops_data = [ |
| 426 |
OnionHopsDataSingle( |
| 427 |
_raw_bytes_payload=bfh("1202023a98040205dc06080000000000000001"), |
| 428 |
), |
| 429 |
OnionHopsDataSingle( |
| 430 |
_raw_bytes_payload=bfh("52020236b00402057806080000000000000002fd02013c0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f"), |
| 431 |
), |
| 432 |
OnionHopsDataSingle( |
| 433 |
_raw_bytes_payload=bfh("12020230d4040204e206080000000000000003"), |
| 434 |
), |
| 435 |
OnionHopsDataSingle( |
| 436 |
_raw_bytes_payload=bfh("1202022710040203e806080000000000000004"), |
| 437 |
), |
| 438 |
OnionHopsDataSingle( |
| 439 |
_raw_bytes_payload=bfh("fd011002022710040203e8082224a33562c54507a9334e79f0dc4f17d407e6d7c61f0e2f3d0d38599502f617042710fd012de02a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a"), |
| 440 |
), |
| 441 |
] |
| 442 |
packet = new_onion_packet(payment_path_pubkeys, session_key, hops_data, associated_data=associated_data) |
| 443 |
self.assertEqual(bfh('0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619f7f3416a5aa36dc7eeb3ec6d421e9615471ab870a33ac07fa5d5a51df0a8823aabe3fea3f90d387529d4f72837f9e687230371ccd8d263072206dbed0234f6505e21e282abd8c0e4f5b9ff8042800bbab065036eadd0149b37f27dde664725a49866e052e809d2b0198ab9610faa656bbf4ec516763a59f8f42c171b179166ba38958d4f51b39b3e98706e2d14a2dafd6a5df808093abfca5aeaaca16eded5db7d21fb0294dd1a163edf0fb445d5c8d7d688d6dd9c541762bf5a5123bf9939d957fe648416e88f1b0928bfa034982b22548e1a4d922690eecf546275afb233acf4323974680779f1a964cfe687456035cc0fba8a5428430b390f0057b6d1fe9a8875bfa89693eeb838ce59f09d207a503ee6f6299c92d6361bc335fcbf9b5cd44747aadce2ce6069cfdc3d671daef9f8ae590cf93d957c9e873e9a1bc62d9640dc8fc39c14902d49a1c80239b6c5b7fd91d05878cbf5ffc7db2569f47c43d6c0d27c438abff276e87364deb8858a37e5a62c446af95d8b786eaf0b5fcf78d98b41496794f8dcaac4eef34b2acfb94c7e8c32a9e9866a8fa0b6f2a06f00a1ccde569f97eec05c803ba7500acc96691d8898d73d8e6a47b8f43c3d5de74458d20eda61474c426359677001fbd75a74d7d5db6cb4feb83122f133206203e4e2d293f838bf8c8b3a29acb321315100b87e80e0edb272ee80fda944e3fb6084ed4d7f7c7d21c69d9da43d31a90b70693f9b0cc3eac74c11ab8ff655905688916cfa4ef0bd04135f2e50b7c689a21d04e8e981e74c6058188b9b1f9dfc3eec6838e9ffbcf22ce738d8a177c19318dffef090cee67e12de1a3e2a39f61247547ba5257489cbc11d7d91ed34617fcc42f7a9da2e3cf31a94a210a1018143173913c38f60e62b24bf0d7518f38b5bab3e6a1f8aeb35e31d6442c8abb5178efc892d2e787d79c6ad9e2fc271792983fa9955ac4d1d84a36c024071bc6e431b625519d556af38185601f70e29035ea6a09c8b676c9d88cf7e05e0f17098b584c4168735940263f940033a220f40be4c85344128b14beb9e75696db37014107801a59b13e89cd9d2258c169d523be6d31552c44c82ff4bb18ec9f099f3bf0e5b1bb2ba9a87d7e26f98d294927b600b5529c47e04d98956677cbcee8fa2b60f49776d8b8c367465b7c626da53700684fb6c918ead0eab8360e4f60edd25b4f43816a75ecf70f909301825b512469f8389d79402311d8aecb7b3ef8599e79485a4388d87744d899f7c47ee644361e17040a7958c8911be6f463ab6a9b2afacd688ec55ef517b38f1339efc54487232798bb25522ff4572ff68567fe830f92f7b8113efce3e98c3fffbaedce4fd8b50e41da97c0c08e423a72689cc68e68f752a5e3a9003e64e35c957ca2e1c48bb6f64b05f56b70b575ad2f278d57850a7ad568c24a4d32a3d74b29f03dc125488bc7c637da582357f40b0a52d16b3b40bb2c2315d03360bc24209e20972c200566bcf3bbe5c5b0aedd83132a8a4d5b4242ba370b6d67d9b67eb01052d132c7866b9cb502e44796d9d356e4e3cb47cc527322cd24976fe7c9257a2864151a38e568ef7a79f10d6ef27cc04ce382347a2488b1f404fdbf407fe1ca1c9d0d5649e34800e25e18951c98cae9f43555eef65fee1ea8f15828807366c3b612cd5753bf9fb8fced08855f742cddd6f765f74254f03186683d646e6f09ac2805586c7cf11998357cafc5df3f285329366f475130c928b2dceba4aa383758e7a9d20705c4bb9db619e2992f608a1ba65db254bb389468741d0502e2588aeb54390ac600c19af5c8e61383fc1bebe0029e4474051e4ef908828db9cca13277ef65db3fd47ccc2179126aaefb627719f421e20'), |
| 444 |
packet.to_bytes()) |
| 445 |
|
| 446 |
@needs_test_with_all_chacha20_implementations |
| 447 |
def test_process_onion_packet(self): |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
payment_path_pubkeys = [ |
| 452 |
bfh('02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619'), |
| 453 |
bfh('0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c'), |
| 454 |
bfh('027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007'), |
| 455 |
bfh('032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991'), |
| 456 |
bfh('02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145'), |
| 457 |
] |
| 458 |
payment_path_privkeys = [ |
| 459 |
bfh('4141414141414141414141414141414141414141414141414141414141414141'), |
| 460 |
bfh('4242424242424242424242424242424242424242424242424242424242424242'), |
| 461 |
bfh('4343434343434343434343434343434343434343434343434343434343434343'), |
| 462 |
bfh('4444444444444444444444444444444444444444444444444444444444444444'), |
| 463 |
bfh('4545454545454545454545454545454545454545454545454545454545454545'), |
| 464 |
] |
| 465 |
session_key = bfh('4141414141414141414141414141414141414141414141414141414141414141') |
| 466 |
associated_data = bfh('4242424242424242424242424242424242424242424242424242424242424242') |
| 467 |
hops_data = [ |
| 468 |
OnionHopsDataSingle(payload={ |
| 469 |
'amt_to_forward': {'amt_to_forward': 15000}, |
| 470 |
'outgoing_cltv_value': {'outgoing_cltv_value': 1500}, |
| 471 |
'short_channel_id': {'short_channel_id': bfh('0000000000000001')}}), |
| 472 |
OnionHopsDataSingle(payload={ |
| 473 |
'amt_to_forward': {'amt_to_forward': 14000}, |
| 474 |
'outgoing_cltv_value': {'outgoing_cltv_value': 1400}, |
| 475 |
'short_channel_id': {'short_channel_id': bfh('0000000000000002')}}), |
| 476 |
OnionHopsDataSingle(payload={ |
| 477 |
'amt_to_forward': {'amt_to_forward': 12500}, |
| 478 |
'outgoing_cltv_value': {'outgoing_cltv_value': 1250}, |
| 479 |
'short_channel_id': {'short_channel_id': bfh('0000000000000003')}}), |
| 480 |
OnionHopsDataSingle(payload={ |
| 481 |
'amt_to_forward': {'amt_to_forward': 10000}, |
| 482 |
'outgoing_cltv_value': {'outgoing_cltv_value': 1000}, |
| 483 |
'short_channel_id': {'short_channel_id': bfh('0000000000000004')}}), |
| 484 |
OnionHopsDataSingle(payload={ |
| 485 |
'amt_to_forward': {'amt_to_forward': 10000}, |
| 486 |
'outgoing_cltv_value': {'outgoing_cltv_value': 1000}, |
| 487 |
'payment_data': {'payment_secret': bfh('24a33562c54507a9334e79f0dc4f17d407e6d7c61f0e2f3d0d38599502f61704'), 'total_msat': 10000}}), |
| 488 |
] |
| 489 |
packet = new_onion_packet(payment_path_pubkeys, session_key, hops_data, associated_data=associated_data) |
| 490 |
self.assertEqual(bfh('0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619f7f3416a5aa36dc7eeb3ec6d421e9615471ab858ba970cd3cceb768b44e692be2f390c0b7fe70122abae84d7801db070dfb1638cd8d263072206dbed0234f6505e21e282abd8587124c572aad8de04610a136d6c71a7648c0ef66f1b3655d8a9eea1f92349132c93befbd6c37dbfc55615814ae09e4cbef721c01b487007811bbbfdc1fc7bd869aeb70eb08b4140ff5f501394b3653ada2a3b36a263535ea421d26818afb278df46abcec093305b715cac22b0b03645f8f4797cf2987b1bf4bfdd9ed8648ed42ed1a831fc36ccd45416a132580281ddac4e7470e4d2afd675baad9282ec6335403a73e1391427e330996c834db93848b4ae29dd975f678b2f5155ad6865ca23190725d4b7238fb44f0e3762dd59091b45c97d45df8164a15d9ca0329ec76f957b0a0e49ae372154620708df5c0fa991f0dd12b6bff1ebaf9e2376bb64bc24713f7c57da569bcd9c43a50c088416564b786a87d1f40936a051a3dbfe023bd867a5e66148b61cdd24a79f8c18682150e55aa6969ce9becf51f7c69e72deafcd0659f6be4f78463eaef8716e56615c77b3fbea8190806359909dcbec13c1592523b3d2985ec3e83d42cb7286a66a22f58704ddf6979ceb6883ab4ad8ac99d30251035189ffd514e03ce1576844513d66965d4adfc2523f4eee0dede229ab96303e31348c72bc0c8c816c666a904e5ccbabadf5a919720438f4a14dbd4a802f8d4b942f0ca8572f59644c9ac1912c8c8efefc4afa7f19e27411d46b7541c55985e28ce5cd7620b335fea51de55fa00ef977e8522181ad19e5e04f93bcfc83a36edd7e96fe48e846f2e54fe7a7090fe8e46ba72123e1cdee0667777c38c4930e50401074d8ab31a9717457fcefaa46323003af553bee2b49ea7f907eb2ff3301463e64a8c53975c853bbdd2956b9001b5ce1562264963fce84201daaf752de6df7ca31291226969c9851d1fc4ea88ca67d38c38587c2cdd8bc4d3f7bdf705497a1e054246f684554b3b8dfac43194f1eadec7f83b711e663b5645bde6d7f8cefb59758303599fed25c3b4d2e4499d439c915910dd283b3e7118320f1c6e7385009fbcb9ae79bab72a85e644182b4dafc0a173241f2ae68ae6a504f17f102da1e91de4548c7f5bc1c107354519077a4e83407f0d6a8f0975b4ac0c2c7b30637a998dda27b56b56245371296b816876b859677bcf3473a07e0f300e788fdd60c51b1626b46050b182457c6d716994847aaef667ca45b2cede550c92d336ff29ce6effd933b875f81381cda6e59e9727e728a58c0b3e74035beeeb639ab7463744322bf40138b81895e9a8e8850c9513782dc7a79f04380c216cb177951d8940d576486b887a232fcd382adcbd639e70af0c1a08bcf1405496606fce4645aef10d769dc0c010a8a433d8cd24d5943843a89cdbc8d16531db027b312ab2c03a7f1fdb7f2bcb128639c49e86705c948137fd42d0080fda4be4e9ee812057c7974acbf0162730d3b647b355ac1a5adbb2993832eba443b7c9b5a0ae1fc00a6c0c2b0b65b9019690565739d6439bf602066a3a9bd9c67b83606de51792d25ae517cbbdf6e1827fa0e8b2b5c6023cbb1e9f0e10b786dc6fa154e282fd9c90b8d46ca685d0f4434760035073c92d131564b6845ef57457488add4f709073bbb41f5f31f8226904875a9fd9e1b7a2901e71426104d7a298a05af0d4ab549fbd69c539ebe64949a9b6088f16e2e4bc827c305cb8d64536b8364dc3d5f7519c3b431faa38b47a958cf0c6dcabf205280693abf747c262f44cd6ffa11b32fc38d4f9c3631d554d8b57389f1390ac65c06357843ee6d9f289bb054ef25de45c5149c090fe6ddcd4095696dcc9a5cfc09c8bdfd5b83a153'), |
| 491 |
packet.to_bytes()) |
| 492 |
for i, privkey in enumerate(payment_path_privkeys): |
| 493 |
processed_packet = process_onion_packet(packet, privkey, associated_data=associated_data) |
| 494 |
self.assertEqual(hops_data[i].to_bytes(), processed_packet.hop_data.to_bytes()) |
| 495 |
packet = processed_packet.next_packet |
| 496 |
|
| 497 |
def test_create_legacy_trampoline_onion_multiple_rtags(self): |
| 498 |
"""Test to verify we don't overfill the trampoline onion with r_tags if there are more tags than available space""" |
| 499 |
dummy_route: LNPaymentTRoute = [ |
| 500 |
TrampolineEdge( |
| 501 |
invoice_routing_info=[ |
| 502 |
bfh("010305061295fa30847df41ae6ee809b560e78d65c2a7337a41c725ea3920b65e08a03b62b00003a0002000003e8000000010050"), |
| 503 |
bfh("01037414fe3dcfedc4a0a0e153205d9a973af5096d1cd1c8c53d07ed12d7dd966f19f424000000000020000003e8000008ca0050"), |
| 504 |
bfh("01038550162fa86287884a6a052471934abb5cb261c5a2b15386df8104d3c7bcb85dddd92ee1898ee15c000003e8000000010090"), |
| 505 |
bfh("010244bb7ba2392ab2d493ad04ad4afcd482ca44a2bfe5b42bcc830bfe00e5b08082f424000000000029000003e8000008ca0050") |
| 506 |
], |
| 507 |
invoice_features=LnFeatures.VAR_ONION_REQ | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.BASIC_MPP_OPT, |
| 508 |
short_channel_id=ShortChannelID.from_str("0x0x0"), |
| 509 |
start_node=node('a'), |
| 510 |
end_node=node('b'), |
| 511 |
fee_base_msat=0, |
| 512 |
fee_proportional_millionths=0, |
| 513 |
cltv_delta=0, |
| 514 |
node_features=0 |
| 515 |
), |
| 516 |
TrampolineEdge( |
| 517 |
invoice_routing_info=[], |
| 518 |
invoice_features=None, |
| 519 |
short_channel_id=ShortChannelID.from_str("0x0x0"), |
| 520 |
start_node=node('b'), |
| 521 |
end_node=node('c'), |
| 522 |
fee_base_msat=0, |
| 523 |
fee_proportional_millionths=0, |
| 524 |
cltv_delta=0, |
| 525 |
node_features=0 |
| 526 |
), |
| 527 |
] |
| 528 |
|
| 529 |
create_trampoline_onion( |
| 530 |
route=dummy_route, |
| 531 |
amount_msat=0, |
| 532 |
final_cltv_abs=0, |
| 533 |
total_msat=0, |
| 534 |
payment_hash=urandom(32), |
| 535 |
payment_secret=urandom(32), |
| 536 |
) |
| 537 |
|
| 538 |
@needs_test_with_all_chacha20_implementations |
| 539 |
def test_decode_onion_error(self): |
| 540 |
|
| 541 |
payment_path_pubkeys = [ |
| 542 |
bfh('02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619'), |
| 543 |
bfh('0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c'), |
| 544 |
bfh('027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007'), |
| 545 |
bfh('032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991'), |
| 546 |
bfh('02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145'), |
| 547 |
] |
| 548 |
session_key = bfh('4141414141414141414141414141414141414141414141414141414141414141') |
| 549 |
error_packet_for_node_0 = bfh('9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d') |
| 550 |
|
| 551 |
decoded_error, index_of_sender = _decode_onion_error(error_packet_for_node_0, payment_path_pubkeys, session_key) |
| 552 |
self.assertEqual(bfh('4c2fc8bc08510334b6833ad9c3e79cd1b52ae59dfe5c2a4b23ead50f09f7ee0b0002200200fe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), |
| 553 |
decoded_error) |
| 554 |
self.assertEqual(4, index_of_sender) |
| 555 |
|
| 556 |
failure_msg, index_of_sender = decode_onion_error(error_packet_for_node_0, payment_path_pubkeys, session_key) |
| 557 |
self.assertEqual(4, index_of_sender) |
| 558 |
self.assertEqual(OnionFailureCode.TEMPORARY_NODE_FAILURE, failure_msg.code) |
| 559 |
self.assertEqual(b'', failure_msg.data) |
| 560 |
|
| 561 |
async def test_find_path_for_onion_message(self): |
| 562 |
self.prepare_graph() |
| 563 |
|
| 564 |
path = self.path_finder.find_path_for_onion_message(nodeA=node('a'), nodeB=node('c')) |
| 565 |
self.assertEqual([ |
| 566 |
PathEdge(start_node=node('a'), end_node=node('d'), short_channel_id=channel(6)), |
| 567 |
PathEdge(start_node=node('d'), end_node=node('c'), short_channel_id=channel(4)), |
| 568 |
], path) |
| 569 |
|
| 570 |
|
| 571 |
path = self.path_finder.find_path_for_onion_message(nodeA=node('a'), nodeB=node('e')) |
| 572 |
self.assertIsNone(path) |
| 573 |
|
| 574 |
async def test_find_path_for_onion_message_ignores_amount_constraints(self): |
| 575 |
self.prepare_graph() |
| 576 |
|
| 577 |
|
| 578 |
key = (node('d'), channel(4)) |
| 579 |
self.cdb._policies[key] = self.cdb._policies[key]._replace(htlc_minimum_msat=10_000_000) |
| 580 |
|
| 581 |
|
| 582 |
path = self.path_finder.find_path_for_payment( |
| 583 |
nodeA=node('a'), |
| 584 |
nodeB=node('c'), |
| 585 |
invoice_amount_msat=1000, |
| 586 |
node_filter=is_onion_message_node, |
| 587 |
) |
| 588 |
self.assertIsNone(path) |
| 589 |
|
| 590 |
|
| 591 |
path = self.path_finder.find_path_for_onion_message(nodeA=node('a'), nodeB=node('c')) |
| 592 |
self.assertEqual([ |
| 593 |
PathEdge(start_node=node('a'), end_node=node('d'), short_channel_id=channel(6)), |
| 594 |
PathEdge(start_node=node('d'), end_node=node('c'), short_channel_id=channel(4)), |
| 595 |
], path) |
| 596 |
|
| 597 |
|
| 598 |
def _tramp_edge(start: str, end: str, *, fee_base=PLACEHOLDER_FEE, fee_prop=PLACEHOLDER_FEE, cltv=576) -> TrampolineEdge: |
| 599 |
return TrampolineEdge( |
| 600 |
start_node=node(start), |
| 601 |
end_node=node(end), |
| 602 |
short_channel_id=ShortChannelID.from_str("0x0x0"), |
| 603 |
fee_base_msat=fee_base, |
| 604 |
fee_proportional_millionths=fee_prop, |
| 605 |
cltv_delta=cltv, |
| 606 |
node_features=LnFeatures.VAR_ONION_OPT, |
| 607 |
) |
| 608 |
|
| 609 |
|
| 610 |
class TestAllocateFeeBudget(ElectrumTestCase): |
| 611 |
"""Tests for _allocate_fee_budget_among_route (backward-walk allocator).""" |
| 612 |
|
| 613 |
AMOUNT = 1_000_000 |
| 614 |
BUDGET = PaymentFeeBudget(fee_msat=10_000, cltv=144) |
| 615 |
|
| 616 |
def _allocate(self, route, *, amount=None, budget=None, level=6): |
| 617 |
budget = budget.fee_msat if budget else self.BUDGET.fee_msat |
| 618 |
budget_to_use = get_trampoline_budget(level, budget) |
| 619 |
return _allocate_fee_budget_among_route( |
| 620 |
route, |
| 621 |
usable_budget_msat=budget_to_use, |
| 622 |
amount_msat_for_dest=amount if amount is not None else self.AMOUNT, |
| 623 |
) |
| 624 |
|
| 625 |
def _realized_fee(self, route, amount=None) -> int: |
| 626 |
amt = amount if amount is not None else self.AMOUNT |
| 627 |
for edge in reversed(route[1:]): |
| 628 |
amt += edge.fee_for_edge(amt) |
| 629 |
return amt - (amount if amount is not None else self.AMOUNT) |
| 630 |
|
| 631 |
def test_all_placeholders_matches_even_split(self): |
| 632 |
|
| 633 |
route = [ |
| 634 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 635 |
_tramp_edge('a', 'b'), |
| 636 |
_tramp_edge('b', 'c'), |
| 637 |
] |
| 638 |
self._allocate(route) |
| 639 |
|
| 640 |
self.assertEqual(5_000, route[1].fee_base_msat) |
| 641 |
self.assertEqual(5_000, route[2].fee_base_msat) |
| 642 |
self.assertEqual(0, route[1].fee_proportional_millionths) |
| 643 |
self.assertEqual(0, route[2].fee_proportional_millionths) |
| 644 |
self.assertLessEqual(self._realized_fee(route), self.BUDGET.fee_msat) |
| 645 |
|
| 646 |
def test_known_fee_base_only_deducted_from_budget(self): |
| 647 |
|
| 648 |
route = [ |
| 649 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 650 |
_tramp_edge('a', 'b', fee_base=2_000, fee_prop=0), |
| 651 |
_tramp_edge('b', 'c'), |
| 652 |
] |
| 653 |
self._allocate(route) |
| 654 |
|
| 655 |
self.assertEqual(8_000, route[2].fee_base_msat) |
| 656 |
self.assertLessEqual(self._realized_fee(route), self.BUDGET.fee_msat) |
| 657 |
|
| 658 |
def test_known_proportional_amplifies_placeholder_cost(self): |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
route = [ |
| 666 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 667 |
_tramp_edge('a', 'b', fee_base=0, fee_prop=100_000), |
| 668 |
_tramp_edge('b', 'c'), |
| 669 |
] |
| 670 |
self._allocate(route) |
| 671 |
self.assertEqual(0, route[2].fee_base_msat) |
| 672 |
|
| 673 |
def test_known_proportional_small_fits_budget(self): |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
5. |
| 679 |
route = [ |
| 680 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 681 |
_tramp_edge('a', 'b', fee_base=0, fee_prop=5_000), |
| 682 |
_tramp_edge('b', 'c'), |
| 683 |
] |
| 684 |
self._allocate(route) |
| 685 |
f = route[2].fee_base_msat |
| 686 |
self.assertGreater(f, 0) |
| 687 |
self.assertLessEqual(self._realized_fee(route), self.BUDGET.fee_msat) |
| 688 |
|
| 689 |
def test_known_fee_exceeds_budget_yields_zero_placeholder(self): |
| 690 |
|
| 691 |
|
| 692 |
route = [ |
| 693 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 694 |
_tramp_edge('a', 'b', fee_base=50_000, fee_prop=0), |
| 695 |
_tramp_edge('b', 'c'), |
| 696 |
] |
| 697 |
self._allocate(route) |
| 698 |
self.assertEqual(0, route[2].fee_base_msat) |
| 699 |
self.assertEqual(0, route[2].fee_proportional_millionths) |
| 700 |
|
| 701 |
def test_no_placeholders_noop(self): |
| 702 |
|
| 703 |
route = [ |
| 704 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 705 |
_tramp_edge('a', 'b', fee_base=1_000, fee_prop=0), |
| 706 |
_tramp_edge('b', 'c', fee_base=2_000, fee_prop=0), |
| 707 |
] |
| 708 |
self._allocate(route) |
| 709 |
self.assertEqual(1_000, route[1].fee_base_msat) |
| 710 |
self.assertEqual(2_000, route[2].fee_base_msat) |
| 711 |
|
| 712 |
def test_amount_at_each_hop(self): |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
route = [ |
| 717 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 718 |
_tramp_edge('a', 'b'), |
| 719 |
_tramp_edge('b', 'c', fee_base=1_000, fee_prop=1500), |
| 720 |
_tramp_edge('c', 'd'), |
| 721 |
_tramp_edge('d', 'e', fee_base=1_000, fee_prop=1500), |
| 722 |
] |
| 723 |
self._allocate(route) |
| 724 |
amount = self.AMOUNT |
| 725 |
amounts_from_destination = iter(( |
| 726 |
1000000, |
| 727 |
1002500, |
| 728 |
1004996, |
| 729 |
1007503, |
| 730 |
1009999 |
| 731 |
)) |
| 732 |
for edge in reversed(route[1:]): |
| 733 |
self.assertEqual(amount, next(amounts_from_destination)) |
| 734 |
amount += edge.fee_for_edge(amount) |
| 735 |
self.assertEqual([0, 2496, 1000, 2496, 1000], [e.fee_base_msat for e in route]) |
| 736 |
|
| 737 |
@unittest.skip(reason="is a bit slow") |
| 738 |
def test_fuzz(self): |
| 739 |
invoice_amount = 1_000_000 |
| 740 |
|
| 741 |
for round_ in range(10**5): |
| 742 |
budget = PaymentFeeBudget(fee_msat=random.randint(1000, 10*invoice_amount), cltv=144) |
| 743 |
route = [ |
| 744 |
_tramp_edge('x', 'x', fee_base=0, fee_prop=0), |
| 745 |
] |
| 746 |
num_edges = random.randint(1, 10) |
| 747 |
fee_base_min = random.randint(1000, 50000) |
| 748 |
fee_base_max = random.randint(fee_base_min, 50000) |
| 749 |
fee_prop_min = random.randint(1000, 50000) |
| 750 |
fee_prop_max = random.randint(fee_prop_min, 50000) |
| 751 |
for e in range(num_edges): |
| 752 |
if random.random() < 0.5: |
| 753 |
route.append(_tramp_edge('x', 'x')) |
| 754 |
else: |
| 755 |
fee_base = random.randint(fee_base_min, fee_base_max) |
| 756 |
fee_prop = random.randint(fee_prop_min, fee_prop_max) |
| 757 |
route.append(_tramp_edge('x', 'x', fee_base=fee_base, fee_prop=fee_prop)) |
| 758 |
placeholder_fee = self._allocate(route, amount=invoice_amount, budget=budget) |
| 759 |
|
| 760 |
actual_fees = [] |
| 761 |
fwd_amt = invoice_amount |
| 762 |
for e in route[::-1]: |
| 763 |
actual_fee = e.fee_for_edge(fwd_amt) |
| 764 |
fwd_amt += actual_fee |
| 765 |
actual_fees.append(actual_fee) |
| 766 |
actual_fees = actual_fees[::-1] |
| 767 |
|
| 768 |
no_solution = placeholder_fee == 0 |
| 769 |
solution_is_exact = (budget.fee_msat - num_edges <= sum(actual_fees) <= budget.fee_msat) |
| 770 |
assert no_solution or solution_is_exact |
| 771 |
|
| 772 |
def test_level_zero_gives_zero_placeholders(self): |
| 773 |
route = [ |
| 774 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 775 |
_tramp_edge('a', 'b'), |
| 776 |
_tramp_edge('b', 'c'), |
| 777 |
] |
| 778 |
self._allocate(route, level=0) |
| 779 |
self.assertEqual(0, route[1].fee_base_msat) |
| 780 |
self.assertEqual(0, route[2].fee_base_msat) |
| 781 |
|
| 782 |
def test_placeholder_upstream_of_known(self): |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
route = [ |
| 788 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 789 |
_tramp_edge('a', 'b'), |
| 790 |
_tramp_edge('b', 'c', fee_base=1_000, fee_prop=10_000), |
| 791 |
] |
| 792 |
self._allocate(route) |
| 793 |
self.assertEqual(0, route[1].fee_base_msat) |
| 794 |
|
| 795 |
def test_placeholder_upstream_of_small_known_fits(self): |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
route = [ |
| 801 |
_tramp_edge('m', 'a', fee_base=0, fee_prop=0, cltv=0), |
| 802 |
_tramp_edge('a', 'b'), |
| 803 |
_tramp_edge('b', 'c', fee_base=500, fee_prop=0), |
| 804 |
] |
| 805 |
self._allocate(route) |
| 806 |
self.assertEqual(9_500, route[1].fee_base_msat) |
| 807 |
self.assertLessEqual(self._realized_fee(route), self.BUDGET.fee_msat) |