ttest_lnrouter.py - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- ttest_lnrouter.py (34448B) --- 1 import unittest 2 import tempfile 3 import shutil 4 import asyncio 5 6 from electrum.util import bh2u, bfh, create_and_start_event_loop 7 from electrum.lnonion import (OnionHopsDataSingle, new_onion_packet, 8 process_onion_packet, _decode_onion_error, decode_onion_error, 9 OnionFailureCode, OnionPacket) 10 from electrum import bitcoin, lnrouter 11 from electrum.constants import BitcoinTestnet 12 from electrum.simple_config import SimpleConfig 13 from electrum.lnrouter import PathEdge 14 15 from . import TestCaseForTestnet 16 from .test_bitcoin import needs_test_with_all_chacha20_implementations 17 18 19 class Test_LNRouter(TestCaseForTestnet): 20 21 def setUp(self): 22 super().setUp() 23 self.asyncio_loop, self._stop_loop, self._loop_thread = create_and_start_event_loop() 24 self.config = SimpleConfig({'electrum_path': self.electrum_path}) 25 26 def tearDown(self): 27 self.asyncio_loop.call_soon_threadsafe(self._stop_loop.set_result, 1) 28 self._loop_thread.join(timeout=1) 29 super().tearDown() 30 31 def test_find_path_for_payment(self): 32 class fake_network: 33 config = self.config 34 asyncio_loop = asyncio.get_event_loop() 35 trigger_callback = lambda *args: None 36 register_callback = lambda *args: None 37 interface = None 38 fake_network.channel_db = lnrouter.ChannelDB(fake_network()) 39 fake_network.channel_db.data_loaded.set() 40 cdb = fake_network.channel_db 41 path_finder = lnrouter.LNPathFinder(cdb) 42 self.assertEqual(cdb.num_channels, 0) 43 cdb.add_channel_announcement({'node_id_1': b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'node_id_2': b'\x02cccccccccccccccccccccccccccccccc', 44 'bitcoin_key_1': b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'bitcoin_key_2': b'\x02cccccccccccccccccccccccccccccccc', 45 'short_channel_id': bfh('0000000000000001'), 46 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 47 'len': 0, 'features': b''}, trusted=True) 48 self.assertEqual(cdb.num_channels, 1) 49 cdb.add_channel_announcement({'node_id_1': b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'node_id_2': b'\x02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 50 'bitcoin_key_1': b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'bitcoin_key_2': b'\x02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 51 'short_channel_id': bfh('0000000000000002'), 52 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 53 'len': 0, 'features': b''}, trusted=True) 54 cdb.add_channel_announcement({'node_id_1': b'\x02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'node_id_2': b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 55 'bitcoin_key_1': b'\x02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bitcoin_key_2': b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 56 'short_channel_id': bfh('0000000000000003'), 57 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 58 'len': 0, 'features': b''}, trusted=True) 59 cdb.add_channel_announcement({'node_id_1': b'\x02cccccccccccccccccccccccccccccccc', 'node_id_2': b'\x02dddddddddddddddddddddddddddddddd', 60 'bitcoin_key_1': b'\x02cccccccccccccccccccccccccccccccc', 'bitcoin_key_2': b'\x02dddddddddddddddddddddddddddddddd', 61 'short_channel_id': bfh('0000000000000004'), 62 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 63 'len': 0, 'features': b''}, trusted=True) 64 cdb.add_channel_announcement({'node_id_1': b'\x02dddddddddddddddddddddddddddddddd', 'node_id_2': b'\x02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 65 'bitcoin_key_1': b'\x02dddddddddddddddddddddddddddddddd', 'bitcoin_key_2': b'\x02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 66 'short_channel_id': bfh('0000000000000005'), 67 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 68 'len': 0, 'features': b''}, trusted=True) 69 cdb.add_channel_announcement({'node_id_1': b'\x02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'node_id_2': b'\x02dddddddddddddddddddddddddddddddd', 70 'bitcoin_key_1': b'\x02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bitcoin_key_2': b'\x02dddddddddddddddddddddddddddddddd', 71 'short_channel_id': bfh('0000000000000006'), 72 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 73 'len': 0, 'features': b''}, trusted=True) 74 cdb.add_channel_update({'short_channel_id': bfh('0000000000000001'), '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}) 75 cdb.add_channel_update({'short_channel_id': bfh('0000000000000001'), '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}) 76 cdb.add_channel_update({'short_channel_id': bfh('0000000000000002'), '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}) 77 cdb.add_channel_update({'short_channel_id': bfh('0000000000000002'), '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}) 78 cdb.add_channel_update({'short_channel_id': bfh('0000000000000003'), '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}) 79 cdb.add_channel_update({'short_channel_id': bfh('0000000000000003'), '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}) 80 cdb.add_channel_update({'short_channel_id': bfh('0000000000000004'), '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}) 81 cdb.add_channel_update({'short_channel_id': bfh('0000000000000004'), '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}) 82 cdb.add_channel_update({'short_channel_id': bfh('0000000000000005'), '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}) 83 cdb.add_channel_update({'short_channel_id': bfh('0000000000000005'), '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}) 84 cdb.add_channel_update({'short_channel_id': bfh('0000000000000006'), 'message_flags': b'\x00', 'channel_flags': b'\x00', 'cltv_expiry_delta': 10, 'htlc_minimum_msat': 250, 'fee_base_msat': 100, 'fee_proportional_millionths': 99999999, 'chain_hash': BitcoinTestnet.rev_genesis_bytes(), 'timestamp': 0}) 85 cdb.add_channel_update({'short_channel_id': bfh('0000000000000006'), '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}) 86 path = path_finder.find_path_for_payment( 87 nodeA=b'\x02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 88 nodeB=b'\x02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 89 invoice_amount_msat=100000) 90 self.assertEqual([PathEdge(start_node=b'\x02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', end_node=b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', short_channel_id=bfh('0000000000000003')), 91 PathEdge(start_node=b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', end_node=b'\x02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', short_channel_id=bfh('0000000000000002')), 92 ], path) 93 route = path_finder.create_route_from_path(path) 94 self.assertEqual(b'\x02bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', route[0].node_id) 95 self.assertEqual(bfh('0000000000000003'), route[0].short_channel_id) 96 97 cdb.stop() 98 asyncio.run_coroutine_threadsafe(cdb.stopped_event.wait(), self.asyncio_loop).result() 99 100 @needs_test_with_all_chacha20_implementations 101 def test_new_onion_packet_legacy(self): 102 # test vector from bolt-04 103 payment_path_pubkeys = [ 104 bfh('02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619'), 105 bfh('0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c'), 106 bfh('027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007'), 107 bfh('032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991'), 108 bfh('02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145'), 109 ] 110 session_key = bfh('4141414141414141414141414141414141414141414141414141414141414141') 111 associated_data = bfh('4242424242424242424242424242424242424242424242424242424242424242') 112 hops_data = [ 113 OnionHopsDataSingle(is_tlv_payload=False, payload={ 114 "amt_to_forward": {"amt_to_forward": 0}, 115 "outgoing_cltv_value": {"outgoing_cltv_value": 0}, 116 "short_channel_id": {"short_channel_id": bfh('0000000000000000')}, 117 }), 118 OnionHopsDataSingle(is_tlv_payload=False, payload={ 119 "amt_to_forward": {"amt_to_forward": 1}, 120 "outgoing_cltv_value": {"outgoing_cltv_value": 1}, 121 "short_channel_id": {"short_channel_id": bfh('0101010101010101')}, 122 }), 123 OnionHopsDataSingle(is_tlv_payload=False, payload={ 124 "amt_to_forward": {"amt_to_forward": 2}, 125 "outgoing_cltv_value": {"outgoing_cltv_value": 2}, 126 "short_channel_id": {"short_channel_id": bfh('0202020202020202')}, 127 }), 128 OnionHopsDataSingle(is_tlv_payload=False, payload={ 129 "amt_to_forward": {"amt_to_forward": 3}, 130 "outgoing_cltv_value": {"outgoing_cltv_value": 3}, 131 "short_channel_id": {"short_channel_id": bfh('0303030303030303')}, 132 }), 133 OnionHopsDataSingle(is_tlv_payload=False, payload={ 134 "amt_to_forward": {"amt_to_forward": 4}, 135 "outgoing_cltv_value": {"outgoing_cltv_value": 4}, 136 "short_channel_id": {"short_channel_id": bfh('0404040404040404')}, 137 }), 138 ] 139 packet = new_onion_packet(payment_path_pubkeys, session_key, hops_data, associated_data) parazyd.org:70 /git/electrum/file/electrum/tests/test_lnrouter.py.gph:149: line too long