electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 77e9abc6552a71e2382c72a8a97aab782ba7e513
parent 0d4593eebf84a13a8c8e351b66e817cc8c53ddbd
Author: Janus <ysangkok@gmail.com>
Date:   Wed, 27 Jun 2018 18:33:55 +0200

ln: store HTLCStateMachine in lnworker.channels

Diffstat:
Melectrum/commands.py | 2+-
Mgui/qt/channels_list.py | 7++++---
Mlib/lnbase.py | 173++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlib/lnhtlc.py | 16++++++++++++++++
Mlib/lnwatcher.py | 2+-
Mlib/lnworker.py | 77++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mlib/tests/test_lnhtlc.py | 36++++++++++++++++++++++++++----------
7 files changed, 176 insertions(+), 137 deletions(-)

diff --git a/electrum/commands.py b/electrum/commands.py @@ -766,7 +766,7 @@ class Commands: # lightning network commands @command('wpn') def open_channel(self, node_id, amount, channel_push=0, password=None): - f = self.wallet.lnworker.open_channel(node_id, satoshis(amount), satoshis(channel_push), password) + f = self.wallet.lnworker.open_channel(bytes.fromhex(node_id), satoshis(amount), satoshis(channel_push), password) return f.result() @command('wn') diff --git a/gui/qt/channels_list.py b/gui/qt/channels_list.py @@ -33,7 +33,8 @@ class ChannelsList(MyTreeWidget): channel_id = self.currentItem().data(0, QtCore.Qt.UserRole) print('ID', bh2u(channel_id)) def close(): - self.parent.wallet.lnworker.close_channel(channel_id) + suc, msg = self.parent.wallet.lnworker.close_channel(channel_id) + assert suc # TODO show error message in dialog menu.addAction(_("Close channel"), close) menu.exec_(self.viewport().mapToGlobal(position)) @@ -49,8 +50,8 @@ class ChannelsList(MyTreeWidget): def do_update_rows(self): self.clear() for chan in self.parent.wallet.lnworker.channels.values(): - item = SortableTreeWidgetItem(self.format_fields(chan)) - item.setData(0, QtCore.Qt.UserRole, chan.channel_id) + item = SortableTreeWidgetItem(self.format_fields(chan.state)) + item.setData(0, QtCore.Qt.UserRole, chan.state.channel_id) self.insertTopLevelItem(0, item) def get_toolbar(self): diff --git a/lib/lnbase.py b/lib/lnbase.py @@ -776,7 +776,7 @@ class Peer(PrintError): def on_announcement_signatures(self, payload): channel_id = payload['channel_id'] chan = self.channels[payload['channel_id']] - if chan.local_state.was_announced: + if chan.state.local_state.was_announced: h, local_node_sig, local_bitcoin_sig = self.send_announcement_signatures(chan) else: self.announcement_signatures[channel_id].put_nowait(payload) @@ -927,22 +927,23 @@ class Peer(PrintError): # broadcast funding tx success, _txid = self.network.broadcast_transaction(funding_tx) assert success, success - return chan._replace(remote_state=chan.remote_state._replace(ctn=0),local_state=chan.local_state._replace(ctn=0, current_commitment_signature=remote_sig)) + m.state = chan._replace(remote_state=chan.remote_state._replace(ctn=0),local_state=chan.local_state._replace(ctn=0, current_commitment_signature=remote_sig)) + return m @aiosafe async def reestablish_channel(self, chan): await self.initialized - chan_id = chan.channel_id + chan_id = chan.state.channel_id self.channel_state[chan_id] = 'REESTABLISHING' self.network.trigger_callback('channel', chan) self.send_message(gen_msg("channel_reestablish", channel_id=chan_id, - next_local_commitment_number=chan.local_state.ctn+1, - next_remote_revocation_number=chan.remote_state.ctn + next_local_commitment_number=chan.state.local_state.ctn+1, + next_remote_revocation_number=chan.state.remote_state.ctn )) await self.channel_reestablished[chan_id] self.channel_state[chan_id] = 'OPENING' - if chan.local_state.funding_locked_received and chan.short_channel_id: + if chan.state.local_state.funding_locked_received and chan.state.short_channel_id: self.mark_open(chan) self.network.trigger_callback('channel', chan) @@ -955,26 +956,26 @@ class Peer(PrintError): return channel_reestablish_msg = payload remote_ctn = int.from_bytes(channel_reestablish_msg["next_local_commitment_number"], 'big') - if remote_ctn != chan.remote_state.ctn + 1: - raise Exception("expected remote ctn {}, got {}".format(chan.remote_state.ctn + 1, remote_ctn)) + if remote_ctn != chan.state.remote_state.ctn + 1: + raise Exception("expected remote ctn {}, got {}".format(chan.state.remote_state.ctn + 1, remote_ctn)) local_ctn = int.from_bytes(channel_reestablish_msg["next_remote_revocation_number"], 'big') - if local_ctn != chan.local_state.ctn: - raise Exception("expected local ctn {}, got {}".format(chan.local_state.ctn, local_ctn)) + if local_ctn != chan.state.local_state.ctn: + raise Exception("expected local ctn {}, got {}".format(chan.state.local_state.ctn, local_ctn)) their = channel_reestablish_msg["my_current_per_commitment_point"] - our = chan.remote_state.current_per_commitment_point + our = chan.state.remote_state.current_per_commitment_point if our is None: - our = chan.remote_state.next_per_commitment_point + our = chan.state.remote_state.next_per_commitment_point if our != their: raise Exception("Remote PCP mismatch: {} {}".format(bh2u(our), bh2u(their))) self.channel_reestablished[chan_id].set_result(True) def funding_locked(self, chan): - channel_id = chan.channel_id + channel_id = chan.state.channel_id per_commitment_secret_index = 2**48 - 2 per_commitment_point_second = secret_to_pubkey(int.from_bytes( - get_per_commitment_secret_from_seed(chan.local_state.per_commitment_secret_seed, per_commitment_secret_index), 'big')) + get_per_commitment_secret_from_seed(chan.state.local_state.per_commitment_secret_seed, per_commitment_secret_index), 'big')) self.send_message(gen_msg("funding_locked", channel_id=channel_id, next_per_commitment_point=per_commitment_point_second)) - if chan.local_state.funding_locked_received: + if chan.state.local_state.funding_locked_received: self.mark_open(chan) def on_funding_locked(self, payload): @@ -982,14 +983,14 @@ class Peer(PrintError): chan = self.channels.get(channel_id) if not chan: raise Exception("Got unknown funding_locked", channel_id) - if not chan.local_state.funding_locked_received: - our_next_point = chan.remote_state.next_per_commitment_point + if not chan.state.local_state.funding_locked_received: + our_next_point = chan.state.remote_state.next_per_commitment_point their_next_point = payload["next_per_commitment_point"] - new_remote_state = chan.remote_state._replace(next_per_commitment_point=their_next_point, current_per_commitment_point=our_next_point) - new_local_state = chan.local_state._replace(funding_locked_received = True) - chan = chan._replace(remote_state=new_remote_state, local_state=new_local_state) + new_remote_state = chan.state.remote_state._replace(next_per_commitment_point=their_next_point, current_per_commitment_point=our_next_point) + new_local_state = chan.state.local_state._replace(funding_locked_received = True) + chan.state = chan.state._replace(remote_state=new_remote_state, local_state=new_local_state) self.lnworker.save_channel(chan) - if chan.short_channel_id: + if chan.state.short_channel_id: self.mark_open(chan) def on_network_update(self, chan, funding_tx_depth): @@ -998,8 +999,8 @@ class Peer(PrintError): Runs on the Network thread. """ - if not chan.local_state.was_announced and funding_tx_depth >= 6: - chan = chan._replace(local_state=chan.local_state._replace(was_announced=True)) + if not chan.state.local_state.was_announced and funding_tx_depth >= 6: + chan.state = chan.state._replace(local_state=chan.state.local_state._replace(was_announced=True)) coro = self.handle_announcements(chan) self.lnworker.save_channel(chan) asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop) @@ -1007,10 +1008,10 @@ class Peer(PrintError): @aiosafe async def handle_announcements(self, chan): h, local_node_sig, local_bitcoin_sig = self.send_announcement_signatures(chan) - announcement_signatures_msg = await self.announcement_signatures[chan.channel_id].get() + announcement_signatures_msg = await self.announcement_signatures[chan.state.channel_id].get() remote_node_sig = announcement_signatures_msg["node_signature"] remote_bitcoin_sig = announcement_signatures_msg["bitcoin_signature"] - if not ecc.verify_signature(chan.remote_config.multisig_key.pubkey, remote_bitcoin_sig, h): + if not ecc.verify_signature(chan.state.remote_config.multisig_key.pubkey, remote_bitcoin_sig, h): raise Exception("bitcoin_sig invalid in announcement_signatures") if not ecc.verify_signature(self.pubkey, remote_node_sig, h): raise Exception("node_sig invalid in announcement_signatures") @@ -1018,7 +1019,7 @@ class Peer(PrintError): node_sigs = [local_node_sig, remote_node_sig] bitcoin_sigs = [local_bitcoin_sig, remote_bitcoin_sig] node_ids = [privkey_to_pubkey(self.privkey), self.pubkey] - bitcoin_keys = [chan.local_config.multisig_key.pubkey, chan.remote_config.multisig_key.pubkey] + bitcoin_keys = [chan.state.local_config.multisig_key.pubkey, chan.state.remote_config.multisig_key.pubkey] if node_ids[0] > node_ids[1]: node_sigs.reverse() @@ -1034,7 +1035,7 @@ class Peer(PrintError): len=0, #features not set (defaults to zeros) chain_hash=bytes.fromhex(rev_hex(constants.net.GENESIS)), - short_channel_id=chan.short_channel_id, + short_channel_id=chan.state.short_channel_id, node_id_1=node_ids[0], node_id_2=node_ids[1], bitcoin_key_1=bitcoin_keys[0], @@ -1046,23 +1047,23 @@ class Peer(PrintError): print("SENT CHANNEL ANNOUNCEMENT") def mark_open(self, chan): - if self.channel_state[chan.channel_id] == "OPEN": + if self.channel_state[chan.state.channel_id] == "OPEN": return - assert chan.local_state.funding_locked_received - self.channel_state[chan.channel_id] = "OPEN" - self.network.trigger_callback('channel', chan) + assert chan.state.local_state.funding_locked_received + self.channel_state[chan.state.channel_id] = "OPEN" + self.network.trigger_callback('channel', chan.state) # add channel to database sorted_keys = list(sorted([self.pubkey, self.lnworker.pubkey])) - self.channel_db.on_channel_announcement({"short_channel_id": chan.short_channel_id, "node_id_1": sorted_keys[0], "node_id_2": sorted_keys[1]}) - self.channel_db.on_channel_update({"short_channel_id": chan.short_channel_id, 'flags': b'\x01', 'cltv_expiry_delta': b'\x90', 'htlc_minimum_msat': b'\x03\xe8', 'fee_base_msat': b'\x03\xe8', 'fee_proportional_millionths': b'\x01'}) - self.channel_db.on_channel_update({"short_channel_id": chan.short_channel_id, 'flags': b'\x00', 'cltv_expiry_delta': b'\x90', 'htlc_minimum_msat': b'\x03\xe8', 'fee_base_msat': b'\x03\xe8', 'fee_proportional_millionths': b'\x01'}) + self.channel_db.on_channel_announcement({"short_channel_id": chan.state.short_channel_id, "node_id_1": sorted_keys[0], "node_id_2": sorted_keys[1]}) + self.channel_db.on_channel_update({"short_channel_id": chan.state.short_channel_id, 'flags': b'\x01', 'cltv_expiry_delta': b'\x90', 'htlc_minimum_msat': b'\x03\xe8', 'fee_base_msat': b'\x03\xe8', 'fee_proportional_millionths': b'\x01'}) + self.channel_db.on_channel_update({"short_channel_id": chan.state.short_channel_id, 'flags': b'\x00', 'cltv_expiry_delta': b'\x90', 'htlc_minimum_msat': b'\x03\xe8', 'fee_base_msat': b'\x03\xe8', 'fee_proportional_millionths': b'\x01'}) self.print_error("CHANNEL OPENING COMPLETED") def send_announcement_signatures(self, chan): - bitcoin_keys = [chan.local_config.multisig_key.pubkey, - chan.remote_config.multisig_key.pubkey] + bitcoin_keys = [chan.state.local_config.multisig_key.pubkey, + chan.state.remote_config.multisig_key.pubkey] node_ids = [privkey_to_pubkey(self.privkey), self.pubkey] @@ -1076,7 +1077,7 @@ class Peer(PrintError): len=0, #features not set (defaults to zeros) chain_hash=bytes.fromhex(rev_hex(constants.net.GENESIS)), - short_channel_id=chan.short_channel_id, + short_channel_id=chan.state.short_channel_id, node_id_1=node_ids[0], node_id_2=node_ids[1], bitcoin_key_1=bitcoin_keys[0], @@ -1084,11 +1085,11 @@ class Peer(PrintError): ) to_hash = chan_ann[256+2:] h = bitcoin.Hash(to_hash) - bitcoin_signature = ecc.ECPrivkey(chan.local_config.multisig_key.privkey).sign(h, sigencode_string_canonize, sigdecode_string) + bitcoin_signature = ecc.ECPrivkey(chan.state.local_config.multisig_key.privkey).sign(h, sigencode_string_canonize, sigdecode_string) node_signature = ecc.ECPrivkey(self.privkey).sign(h, sigencode_string_canonize, sigdecode_string) self.send_message(gen_msg("announcement_signatures", - channel_id=chan.channel_id, - short_channel_id=chan.short_channel_id, + channel_id=chan.state.channel_id, + short_channel_id=chan.state.short_channel_id, node_signature=node_signature, bitcoin_signature=bitcoin_signature )) @@ -1123,7 +1124,7 @@ class Peer(PrintError): @aiosafe async def pay(self, path, chan, amount_msat, payment_hash, pubkey_in_invoice, min_final_cltv_expiry): - assert self.channel_state[chan.channel_id] == "OPEN" + assert self.channel_state[chan.state.channel_id] == "OPEN" assert amount_msat > 0, "amount_msat is not greater zero" height = self.network.get_local_height() route = self.network.path_finder.create_route_from_path(path, self.lnworker.pubkey) @@ -1139,62 +1140,61 @@ class Peer(PrintError): self.secret_key = os.urandom(32) hops_data += [OnionHopsDataSingle(OnionPerHop(b"\x00"*8, amount_msat.to_bytes(8, "big"), (final_cltv_expiry_without_deltas).to_bytes(4, "big")))] onion = new_onion_packet([x.node_id for x in route], self.secret_key, hops_data, associated_data) - msat_local = chan.local_state.amount_msat - (amount_msat + total_fee) - msat_remote = chan.remote_state.amount_msat + (amount_msat + total_fee) + msat_local = chan.state.local_state.amount_msat - (amount_msat + total_fee) + msat_remote = chan.state.remote_state.amount_msat + (amount_msat + total_fee) htlc = UpdateAddHtlc(amount_msat, payment_hash, final_cltv_expiry_with_deltas, total_fee) amount_msat += total_fee - self.send_message(gen_msg("update_add_htlc", channel_id=chan.channel_id, id=chan.local_state.next_htlc_id, cltv_expiry=final_cltv_expiry_with_deltas, amount_msat=amount_msat, payment_hash=payment_hash, onion_routing_packet=onion.to_bytes())) + self.send_message(gen_msg("update_add_htlc", channel_id=chan.state.channel_id, id=chan.state.local_state.next_htlc_id, cltv_expiry=final_cltv_expiry_with_deltas, amount_msat=amount_msat, payment_hash=payment_hash, onion_routing_packet=onion.to_bytes())) - m = HTLCStateMachine(chan) - m.add_htlc(htlc) - self.attempted_route[(chan.channel_id, htlc.htlc_id)] = route + chan.add_htlc(htlc) + self.attempted_route[(chan.state.channel_id, htlc.htlc_id)] = route - sig_64, htlc_sigs = m.sign_next_commitment() + sig_64, htlc_sigs = chan.sign_next_commitment() htlc_sig = htlc_sigs[0] - self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=1, htlc_signature=htlc_sig)) + self.send_message(gen_msg("commitment_signed", channel_id=chan.state.channel_id, signature=sig_64, num_htlcs=1, htlc_signature=htlc_sig)) - await self.receive_revoke(m) + await self.receive_revoke(chan) - self.revoke(m) + self.revoke(chan) - fulfill_coro = asyncio.ensure_future(self.update_fulfill_htlc[chan.channel_id].get()) - failure_coro = asyncio.ensure_future(self.update_fail_htlc[chan.channel_id].get()) + fulfill_coro = asyncio.ensure_future(self.update_fulfill_htlc[chan.state.channel_id].get()) + failure_coro = asyncio.ensure_future(self.update_fail_htlc[chan.state.channel_id].get()) done, pending = await asyncio.wait([fulfill_coro, failure_coro], return_when=FIRST_COMPLETED) if failure_coro.done(): - sig_64, htlc_sigs = m.sign_next_commitment() - self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=1, htlc_signature=htlc_sigs[0])) - while (await self.commitment_signed[chan.channel_id].get())["htlc_signature"] != b"": - self.revoke(m) - await self.receive_revoke(m) - m.fail_htlc(htlc) - sig_64, htlc_sigs = m.sign_next_commitment() - self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=0)) - await self.receive_revoke(m) + sig_64, htlc_sigs = chan.sign_next_commitment() + self.send_message(gen_msg("commitment_signed", channel_id=chan.state.channel_id, signature=sig_64, num_htlcs=1, htlc_signature=htlc_sigs[0])) + while (await self.commitment_signed[chan.state.channel_id].get())["htlc_signature"] != b"": + self.revoke(chan) + await self.receive_revoke(chan) + chan.fail_htlc(htlc) + sig_64, htlc_sigs = chan.sign_next_commitment() + self.send_message(gen_msg("commitment_signed", channel_id=chan.state.channel_id, signature=sig_64, num_htlcs=0)) + await self.receive_revoke(chan) fulfill_coro.cancel() - self.lnworker.save_channel(m.state) + self.lnworker.save_channel(chan) return failure_coro.result() if fulfill_coro.done(): failure_coro.cancel() update_fulfill_htlc_msg = fulfill_coro.result() - m.receive_htlc_settle(update_fulfill_htlc_msg["payment_preimage"], int.from_bytes(update_fulfill_htlc_msg["id"], "big")) + chan.receive_htlc_settle(update_fulfill_htlc_msg["payment_preimage"], int.from_bytes(update_fulfill_htlc_msg["id"], "big")) - while (await self.commitment_signed[chan.channel_id].get())["htlc_signature"] != b"": - self.revoke(m) + while (await self.commitment_signed[chan.state.channel_id].get())["htlc_signature"] != b"": + self.revoke(chan) # TODO process above commitment transactions - bare_ctx = make_commitment_using_open_channel(m.state, m.state.remote_state.ctn + 1, False, m.state.remote_state.next_per_commitment_point, + bare_ctx = make_commitment_using_open_channel(chan.state, chan.state.remote_state.ctn + 1, False, chan.state.remote_state.next_per_commitment_point, msat_remote, msat_local) - sig_64 = sign_and_get_sig_string(bare_ctx, chan.local_config, chan.remote_config) - self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=0)) + sig_64 = sign_and_get_sig_string(bare_ctx, chan.state.local_config, chan.state.remote_config) + self.send_message(gen_msg("commitment_signed", channel_id=chan.state.channel_id, signature=sig_64, num_htlcs=0)) - await self.receive_revoke(m) + await self.receive_revoke(chan) - self.lnworker.save_channel(m.state) + self.lnworker.save_channel(chan) async def receive_revoke(self, m): revoke_and_ack_msg = await self.revoke_and_ack[m.state.channel_id].get() @@ -1217,10 +1217,10 @@ class Peer(PrintError): @aiosafe async def receive_commitment_revoke_ack(self, htlc, decoded, payment_preimage): chan = self.channels[htlc['channel_id']] - channel_id = chan.channel_id + channel_id = chan.state.channel_id expected_received_msat = int(decoded.amount * COIN * 1000) htlc_id = int.from_bytes(htlc["id"], 'big') - assert htlc_id == chan.remote_state.next_htlc_id, (htlc_id, chan.remote_state.next_htlc_id) + assert htlc_id == chan.state.remote_state.next_htlc_id, (htlc_id, chan.state.remote_state.next_htlc_id) assert self.channel_state[channel_id] == "OPEN" @@ -1232,19 +1232,17 @@ class Peer(PrintError): htlc = UpdateAddHtlc(amount_msat, payment_hash, cltv_expiry, 0) - m = HTLCStateMachine(chan) - - m.receive_htlc(htlc) + chan.receive_htlc(htlc) - assert (await self.receive_commitment(m)) == 1 + assert (await self.receive_commitment(chan)) == 1 - self.revoke(m) + self.revoke(chan) - sig_64, htlc_sigs = m.sign_next_commitment() + sig_64, htlc_sigs = chan.sign_next_commitment() htlc_sig = htlc_sigs[0] self.send_message(gen_msg("commitment_signed", channel_id=channel_id, signature=sig_64, num_htlcs=1, htlc_signature=htlc_sig)) - await self.receive_revoke(m) + await self.receive_revoke(chan) m.settle_htlc(payment_preimage, htlc_id) self.send_message(gen_msg("update_fulfill_htlc", channel_id=channel_id, id=htlc_id, payment_preimage=payment_preimage)) @@ -1255,19 +1253,20 @@ class Peer(PrintError): sig_64 = sign_and_get_sig_string(bare_ctx, m.state.local_config, m.state.remote_config) self.send_message(gen_msg("commitment_signed", channel_id=channel_id, signature=sig_64, num_htlcs=0)) - await self.receive_revoke(m) + await self.receive_revoke(chan) - assert (await self.receive_commitment(m)) == 0 + assert (await self.receive_commitment(chan)) == 0 - self.revoke(m) + self.revoke(chan) - self.lnworker.save_channel(m.state) + self.lnworker.save_channel(chan) def on_commitment_signed(self, payload): self.print_error("commitment_signed", payload) channel_id = payload['channel_id'] chan = self.channels[channel_id] - self.lnworker.save_channel(chan._replace(local_state=chan.local_state._replace(current_commitment_signature=payload['signature']))) + chan.state = chan.state._replace(local_state=chan.state.local_state._replace(current_commitment_signature=payload['signature'])) + self.lnworker.save_channel(chan) self.commitment_signed[channel_id].put_nowait(payload) def on_update_fulfill_htlc(self, payload): @@ -1298,6 +1297,10 @@ class Peer(PrintError): channel_id = payload["channel_id"] self.revoke_and_ack[channel_id].put_nowait(payload) + def on_update_fee(self, payload): + channel_id = payload["channel_id"] + self.channels[channel_id].update_fee(int.from_bytes(payload["feerate_per_kw"], "big")) + def count_trailing_zeros(index): """ BOLT-03 (where_to_put_secret) """ diff --git a/lib/lnhtlc.py b/lib/lnhtlc.py @@ -60,6 +60,7 @@ class HTLCStateMachine(PrintError): self.total_msat_sent = 0 self.total_msat_received = 0 + self.pending_feerate = None def add_htlc(self, htlc): """ @@ -188,9 +189,17 @@ class HTLCStateMachine(PrintError): last_secret, this_point, next_point = self.points + if self.pending_feerate is not None: + new_feerate = self.pending_feerate + else: + new_feerate = self.state.constraints.feerate + self.state = self.state._replace( local_state=self.state.local_state._replace( ctn=self.state.local_state.ctn + 1 + ), + constraints=self.state.constraints._replace( + feerate=new_feerate ) ) @@ -432,3 +441,10 @@ class HTLCStateMachine(PrintError): @property def local_commit_fee(self): return self.state.constraints.capacity - sum(x[2] for x in self.local_commitment.outputs()) + + def update_fee(self, fee): + self.pending_feerate = fee + + def receive_update_fee(self, fee): + self.pending_feerate = fee + diff --git a/lib/lnwatcher.py b/lib/lnwatcher.py @@ -15,7 +15,7 @@ class LNWatcher(PrintError): return response['params'], response['result'] def watch_channel(self, chan, callback): - script = funding_output_script(chan.local_config, chan.remote_config) + script = funding_output_script(chan.state.local_config, chan.state.remote_config) funding_address = redeem_script_to_address('p2wsh', script) self.watched_channels[funding_address] = chan, callback self.network.subscribe_to_addresses([funding_address], self.on_address_status) diff --git a/lib/lnworker.py b/lib/lnworker.py @@ -14,6 +14,7 @@ from .lnbase import Peer, Outpoint, ChannelConfig, LocalState, RemoteState, Keyp from .lightning_payencode.lnaddr import lnencode, LnAddr, lndecode from .ecc import ECPrivkey, CURVE_ORDER, der_sig_from_sig_string from .transaction import Transaction +from .lnhtlc import HTLCStateMachine is_key = lambda k: k.endswith("_basepoint") or k.endswith("_key") @@ -53,7 +54,7 @@ def serialize_channels(channels_dict): serialized_channels = [] for chan in channels_dict.values(): namedtuples_to_dict = lambda v: {i: j._asdict() if isinstance(j, tuple) else j for i, j in v._asdict().items()} - serialized_channels.append({k: namedtuples_to_dict(v) if isinstance(v, tuple) else v for k, v in chan._asdict().items()}) + serialized_channels.append({k: namedtuples_to_dict(v) if isinstance(v, tuple) else v for k, v in chan.state._asdict().items()}) class MyJsonEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, bytes): @@ -64,8 +65,8 @@ def serialize_channels(channels_dict): dumped = MyJsonEncoder().encode(serialized_channels) roundtripped = json.loads(dumped) reconstructed = set(reconstruct_namedtuples(x) for x in roundtripped) - if reconstructed != set(channels_dict.values()): - raise Exception("Channels did not roundtrip serialization without changes:\n" + repr(reconstructed) + "\n" + repr(channels)) + if reconstructed != set(x.state for x in channels_dict.values()): + raise Exception("Channels did not roundtrip serialization without changes:\n" + repr(reconstructed) + "\n" + repr(channels_dict)) return roundtripped @@ -90,10 +91,10 @@ class LNWorker(PrintError): self.pubkey = ECPrivkey(self.privkey).get_public_key_bytes() self.config = network.config self.peers = {} - self.channels = {x.channel_id: x for x in map(reconstruct_namedtuples, wallet.storage.get("channels", []))} + self.channels = {x.channel_id: HTLCStateMachine(x) for x in map(reconstruct_namedtuples, wallet.storage.get("channels", []))} self.invoices = wallet.storage.get('lightning_invoices', {}) peer_list = network.config.get('lightning_peers', node_list) - self.channel_state = {chan.channel_id: "DISCONNECTED" for chan in self.channels.values()} + self.channel_state = {chan.state.channel_id: "DISCONNECTED" for chan in self.channels.values()} for chan_id, chan in self.channels.items(): self.network.lnwatcher.watch_channel(chan, self.on_channel_utxos) for host, port, pubkey in peer_list: @@ -104,7 +105,7 @@ class LNWorker(PrintError): def channels_for_peer(self, node_id): assert type(node_id) is bytes - return {x: y for (x, y) in self.channels.items() if y.node_id == node_id} + return {x: y for (x, y) in self.channels.items() if y.state.node_id == node_id} def add_peer(self, host, port, node_id): peer = Peer(self, host, int(port), node_id, request_initial_sync=self.config.get("request_initial_sync", True)) @@ -113,17 +114,18 @@ class LNWorker(PrintError): self.lock = threading.Lock() def save_channel(self, openchannel): - if openchannel.channel_id not in self.channel_state: - self.channel_state[openchannel.channel_id] = "OPENING" - self.channels[openchannel.channel_id] = openchannel + assert type(openchannel) is HTLCStateMachine + if openchannel.state.channel_id not in self.channel_state: + self.channel_state[openchannel.state.channel_id] = "OPENING" + self.channels[openchannel.state.channel_id] = openchannel for node_id, peer in self.peers.items(): peer.channels = self.channels_for_peer(node_id) - if openchannel.remote_state.next_per_commitment_point == openchannel.remote_state.current_per_commitment_point: + if openchannel.state.remote_state.next_per_commitment_point == openchannel.state.remote_state.current_per_commitment_point: raise Exception("Tried to save channel with next_point == current_point, this should not happen") dumped = serialize_channels(self.channels) self.wallet.storage.put("channels", dumped) self.wallet.storage.write() - self.network.trigger_callback('channel', openchannel) + self.network.trigger_callback('channel', openchannel.state) def save_short_chan_id(self, chan): """ @@ -131,48 +133,48 @@ class LNWorker(PrintError): If the Funding TX has not been mined, return None """ - assert self.channel_state[chan.channel_id] in ["OPEN", "OPENING"] - peer = self.peers[chan.node_id] - conf = self.wallet.get_tx_height(chan.funding_outpoint.txid)[1] - if conf >= chan.constraints.funding_txn_minimum_depth: - block_height, tx_pos = self.wallet.get_txpos(chan.funding_outpoint.txid) + assert self.channel_state[chan.state.channel_id] in ["OPEN", "OPENING"] + peer = self.peers[chan.state.node_id] + conf = self.wallet.get_tx_height(chan.state.funding_outpoint.txid)[1] + if conf >= chan.state.constraints.funding_txn_minimum_depth: + block_height, tx_pos = self.wallet.get_txpos(chan.state.funding_outpoint.txid) if tx_pos == -1: self.print_error('funding tx is not yet SPV verified.. but there are ' 'already enough confirmations (currently {})'.format(conf)) - return None - chan = chan._replace(short_channel_id = calc_short_channel_id(block_height, tx_pos, chan.funding_outpoint.output_index)) + return False + chan.state = chan.state._replace(short_channel_id = calc_short_channel_id(block_height, tx_pos, chan.state.funding_outpoint.output_index)) self.save_channel(chan) - return chan - return None + return True + return False def on_channel_utxos(self, chan, utxos): outpoints = [Outpoint(x["tx_hash"], x["tx_pos"]) for x in utxos] - if chan.funding_outpoint not in outpoints: - self.channel_state[chan.channel_id] = "CLOSED" - elif self.channel_state[chan.channel_id] == 'DISCONNECTED': - peer = self.peers[chan.node_id] + if chan.state.funding_outpoint not in outpoints: + self.channel_state[chan.state.channel_id] = "CLOSED" + elif self.channel_state[chan.state.channel_id] == 'DISCONNECTED': + peer = self.peers[chan.state.node_id] coro = peer.reestablish_channel(chan) asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop) def on_network_update(self, event, *args): for chan in self.channels.values(): - peer = self.peers[chan.node_id] - if self.channel_state[chan.channel_id] == "OPENING": - chan = self.save_short_chan_id(chan) - if not chan: + peer = self.peers[chan.state.node_id] + if self.channel_state[chan.state.channel_id] == "OPENING": + res = self.save_short_chan_id(chan) + if not res: self.print_error("network update but funding tx is still not at sufficient depth") continue # this results in the channel being marked OPEN peer.funding_locked(chan) - elif self.channel_state[chan.channel_id] == "OPEN": - conf = self.wallet.get_tx_height(chan.funding_outpoint.txid)[1] + elif self.channel_state[chan.state.channel_id] == "OPEN": + conf = self.wallet.get_tx_height(chan.state.funding_outpoint.txid)[1] peer.on_network_update(chan, conf) async def _open_channel_coroutine(self, node_id, amount_sat, push_sat, password): if node_id not in self.peers: node = self.network.lightning_nodes.get(node_id) if node is None: - return False + return "node not found, peers available are: " + str(self.network.lightning_nodes.keys()) host, port = node['addresses'][0] self.add_peer(host, port, node_id) peer = self.peers[node_id] @@ -198,7 +200,7 @@ class LNWorker(PrintError): node_id, short_channel_id = path[0] peer = self.peers[node_id] for chan in self.channels.values(): - if chan.short_channel_id == short_channel_id: + if chan.state.short_channel_id == short_channel_id: break else: raise Exception("ChannelDB returned path with short_channel_id that is not in channel list") @@ -206,7 +208,6 @@ class LNWorker(PrintError): return asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop) def add_invoice(self, amount_sat, message): - is_open = lambda chan: self.channel_state[chan.channel_id] == "OPEN" payment_preimage = os.urandom(32) RHASH = sha256(payment_preimage) amount_btc = amount_sat/Decimal(COIN) if amount_sat else None @@ -230,13 +231,15 @@ class LNWorker(PrintError): return serialize_channels(self.channels) def close_channel(self, chan_id): - from .lnhtlc import HTLCStateMachine chan = self.channels[chan_id] # local_commitment always gives back the next expected local_commitment, # but in this case, we want the current one. So substract one ctn number - tx = HTLCStateMachine(chan._replace(local_state=chan.local_state._replace(ctn=chan.local_state.ctn - 1))).local_commitment - tx.sign({bh2u(chan.local_config.multisig_key.pubkey): (chan.local_config.multisig_key.privkey, True)}) - remote_sig = chan.local_state.current_commitment_signature + old_state = chan.state + chan.state = chan.state._replace(local_state=chan.state.local_state._replace(ctn=chan.state.local_state.ctn - 1)) + tx = chan.local_commitment + chan.state = old_state + tx.sign({bh2u(chan.state.local_config.multisig_key.pubkey): (chan.state.local_config.multisig_key.privkey, True)}) + remote_sig = chan.state.local_state.current_commitment_signature remote_sig = der_sig_from_sig_string(remote_sig) + b"\x01" none_idx = tx._inputs[0]["signatures"].index(None) tx.add_signature_to_txin(0, none_idx, bh2u(remote_sig)) diff --git a/lib/tests/test_lnhtlc.py b/lib/tests/test_lnhtlc.py @@ -250,28 +250,44 @@ class TestLNBaseHTLCStateMachine(unittest.TestCase): ) aliceHtlcIndex = alice_channel.add_htlc(htlc) - bobHtlcIndex = bob_channel.receive_htlc(htlc) - force_state_transition(alice_channel, bob_channel) - self.assertEqual(len(alice_channel.local_commitment.outputs()), 3) - self.assertEqual(len(bob_channel.local_commitment.outputs()), 2) - default_fee = calc_static_fee(0) - self.assertEqual(bob_channel.local_commit_fee, default_fee) - bob_channel.settle_htlc(paymentPreimage, htlc.htlc_id) alice_channel.receive_htlc_settle(paymentPreimage, aliceHtlcIndex) - force_state_transition(bob_channel, alice_channel) - self.assertEqual(len(alice_channel.local_commitment.outputs()), 2) - self.assertEqual(alice_channel.total_msat_sent // 1000, htlcAmt) + def test_UpdateFeeSenderCommits(self): + alice_channel, bob_channel = create_test_channels() + + paymentPreimage = b"\x01" * 32 + paymentHash = bitcoin.sha256(paymentPreimage) + htlc = lnhtlc.UpdateAddHtlc( + payment_hash = paymentHash, + amount_msat = one_bitcoin_in_msat, + cltv_expiry = 5, # also in create_test_channels + total_fee = 0 + ) + + aliceHtlcIndex = alice_channel.add_htlc(htlc) + bobHtlcIndex = bob_channel.receive_htlc(htlc) + + fee = 111 + alice_channel.update_fee(fee) + bob_channel.receive_update_fee(fee) + + alice_sig, alice_htlc_sigs = alice_channel.sign_next_commitment() + bob_channel.receive_new_commitment(alice_sig, alice_htlc_sigs) + self.assertNotEqual(fee, alice_channel.state.constraints.feerate) + rev, _ = alice_channel.revoke_current_commitment() + self.assertEqual(fee, alice_channel.state.constraints.feerate) + bob_channel.receive_revocation(rev) + def force_state_transition(chanA, chanB): chanB.receive_new_commitment(*chanA.sign_next_commitment()) rev, _ = chanB.revoke_current_commitment()