electrum

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

commit 7f0568d83a011cff8c4e0fe5d2ae6137d457667b
parent a5b44d25b01be8aac2e6f661ba42f8e9930f97ff
Author: Janus <ysangkok@gmail.com>
Date:   Mon, 23 Jul 2018 20:16:03 +0200

special case receiving payment after fee update, fee update injector

Diffstat:
Melectrum/lnbase.py | 42++++++++++++++++++++++++++++++++++++------
Melectrum/lnhtlc.py | 11++++++-----
Melectrum/lnworker.py | 3+++
3 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/electrum/lnbase.py b/electrum/lnbase.py @@ -411,7 +411,7 @@ class Peer(PrintError): f(payload) def on_error(self, payload): - self.print_error("error", payload) + self.print_error("error", payload["data"].decode("ascii")) def on_ping(self, payload): l = int.from_bytes(payload['num_pong_bytes'], 'big') @@ -897,8 +897,7 @@ class Peer(PrintError): self.revoke(chan) - while (await self.commitment_signed[chan.channel_id].get())["htlc_signature"] != b"": - pass + commitment_signed_msg = await self.commitment_signed[chan.channel_id].get() # TODO process above commitment transactions bare_ctx = chan.make_commitment(chan.remote_state.ctn + 1, False, chan.remote_state.next_per_commitment_point, @@ -909,6 +908,12 @@ class Peer(PrintError): await self.receive_revoke(chan) + if commitment_signed_msg["htlc_signature"] == b"": + # this happens when we send dust amounts + print("CHANNEL WILL BREAK") # TODO + else: + commitment_signed_msg = await self.commitment_signed[chan.channel_id].get() + self.lnworker.save_channel(chan) return bh2u(preimage) @@ -969,11 +974,36 @@ class Peer(PrintError): sig_64 = sign_and_get_sig_string(bare_ctx, chan.local_config, chan.remote_config) self.send_message(gen_msg("commitment_signed", channel_id=channel_id, signature=sig_64, num_htlcs=0)) - await self.receive_revoke(chan) + revoke_coro = asyncio.ensure_future(self.revoke_and_ack[chan.channel_id].get()) + commit_coro = asyncio.ensure_future(self.commitment_signed[chan.channel_id].get()) + _done, _pending = await asyncio.wait([revoke_coro, commit_coro], return_when=FIRST_COMPLETED) - assert (await self.receive_commitment(chan)) == 0 + def process_commit(commitment_signed_msg): + data = commitment_signed_msg["htlc_signature"] + htlc_sigs = [data[i:i+64] for i in range(0, len(data), 64)] + chan.receive_new_commitment(commitment_signed_msg["signature"], htlc_sigs) + def process_revoke(revoke_and_ack_msg): + chan.receive_revocation(RevokeAndAck(revoke_and_ack_msg["per_commitment_secret"], revoke_and_ack_msg["next_per_commitment_point"])) - self.revoke(chan) + if commit_coro.done(): + # this branch is taken with lnd after a fee update (initiated by us, of course) + + # TODO process_commit(commit_coro.result()) + await asyncio.wait([revoke_coro]) + process_revoke(revoke_coro.result()) + self.revoke(chan) + await self.receive_commitment(chan) + self.revoke(chan) + sig_64, htlc_sigs = chan.sign_next_commitment() + + self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=len(htlc_sigs), htlc_signature=b"".join(htlc_sigs))) + await self.receive_revoke(chan) + elif revoke_coro.done(): + process_revoke(revoke_coro.result()) + + await asyncio.wait([commit_coro]) + process_commit(commit_coro.result()) + self.revoke(chan) self.lnworker.save_channel(chan) diff --git a/electrum/lnhtlc.py b/electrum/lnhtlc.py @@ -287,17 +287,15 @@ class HTLCStateMachine(PrintError): last_secret, this_point, next_point = self.points new_local_feerate = self.local_state.feerate + new_remote_feerate = self.remote_state.feerate if self.pending_fee is not None: if not self.constraints.is_initiator and (self.pending_fee.progress & FUNDEE_SIGNED): - new_local_feerate = self.pending_fee.rate - self.remote_state=self.remote_state._replace( - feerate=self.pending_fee.rate - ) + new_local_feerate = new_remote_feerate = self.pending_fee.rate self.pending_fee = None print("FEERATE CHANGE COMPLETE (non-initiator)") if self.constraints.is_initiator and (self.pending_fee.progress & FUNDER_SIGNED): - new_local_feerate = self.pending_fee.rate + new_local_feerate = new_remote_feerate = self.pending_fee.rate self.pending_fee = None print("FEERATE CHANGE COMPLETE (initiator)") @@ -305,6 +303,9 @@ class HTLCStateMachine(PrintError): ctn=self.local_state.ctn + 1, feerate=new_local_feerate ) + self.remote_state=self.remote_state._replace( + feerate=new_remote_feerate + ) self.local_commitment = self.pending_local_commitment diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -146,6 +146,9 @@ class LNWorker(PrintError): async def _open_channel_coroutine(self, node_id, local_amount_sat, push_sat, password): peer = self.peers[node_id] openingchannel = await peer.channel_establishment_flow(self.wallet, self.config, password, local_amount_sat + push_sat, push_sat * 1000, temp_channel_id=os.urandom(32)) + if not openingchannel: + self.print_error("Channel_establishment_flow returned None") + return self.save_channel(openingchannel) self.network.lnwatcher.watch_channel(openingchannel, self.on_channel_utxos) self.on_channels_updated()