Skip to content

Commit

Permalink
Add extra state to distinguish shutdown negotiation from post-
Browse files Browse the repository at this point in the history
negotiation, where channel should not be reestablished. See #6182
  • Loading branch information
ecdsa committed May 29, 2020
1 parent 680502c commit 2adbbee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
48 changes: 27 additions & 21 deletions electrum/lnchannel.py
Expand Up @@ -77,10 +77,11 @@ class ChannelState(IntEnum):
# - Non-funding node: has sent the funding_signed message.
FUNDED = 2 # Funding tx was mined (requires min_depth and tx verification)
OPEN = 3 # both parties have sent funding_locked
CLOSING = 4 # shutdown has been sent, and closing tx is unconfirmed.
FORCE_CLOSING = 5 # we force-closed, and closing tx is unconfirmed. (otherwise we remain OPEN)
CLOSED = 6 # closing tx has been mined
REDEEMED = 7 # we can stop watching
SHUTDOWN = 4 # shutdown has been sent.
CLOSING = 5 # closing negotiation done. we have a fully signed tx.
FORCE_CLOSING = 6 # we force-closed, and closing tx is unconfirmed. (otherwise we remain OPEN)
CLOSED = 7 # closing tx has been mined
REDEEMED = 8 # we can stop watching


class PeerState(IntEnum):
Expand All @@ -95,18 +96,25 @@ class PeerState(IntEnum):
(cs.PREOPENING, cs.OPENING),
(cs.OPENING, cs.FUNDED),
(cs.FUNDED, cs.OPEN),
(cs.OPENING, cs.CLOSING),
(cs.FUNDED, cs.CLOSING),
(cs.OPEN, cs.CLOSING),
(cs.OPENING, cs.FORCE_CLOSING),
(cs.FUNDED, cs.FORCE_CLOSING),
(cs.OPEN, cs.FORCE_CLOSING),
(cs.CLOSING, cs.FORCE_CLOSING),
(cs.OPENING, cs.CLOSED),
(cs.FUNDED, cs.CLOSED),
(cs.OPEN, cs.CLOSED),
(cs.CLOSING, cs.CLOSING), # if we reestablish
(cs.CLOSING, cs.CLOSED),
(cs.OPENING, cs.SHUTDOWN),
(cs.FUNDED, cs.SHUTDOWN),
(cs.OPEN, cs.SHUTDOWN),
(cs.SHUTDOWN, cs.SHUTDOWN), # if we reestablish
(cs.SHUTDOWN, cs.CLOSING),
(cs.CLOSING, cs.CLOSING),
# we can force close almost any time
(cs.OPENING, cs.FORCE_CLOSING),
(cs.FUNDED, cs.FORCE_CLOSING),
(cs.OPEN, cs.FORCE_CLOSING),
(cs.SHUTDOWN, cs.FORCE_CLOSING),
(cs.CLOSING, cs.FORCE_CLOSING),
# we can get force closed almost any time
(cs.OPENING, cs.CLOSED),
(cs.FUNDED, cs.CLOSED),
(cs.OPEN, cs.CLOSED),
(cs.SHUTDOWN, cs.CLOSED),
(cs.CLOSING, cs.CLOSED),
#
(cs.FORCE_CLOSING, cs.FORCE_CLOSING), # allow multiple attempts
(cs.FORCE_CLOSING, cs.CLOSED),
(cs.FORCE_CLOSING, cs.REDEEMED),
Expand Down Expand Up @@ -174,11 +182,11 @@ def is_open(self):
return self.get_state() == ChannelState.OPEN

def is_closing(self):
return self.get_state() in [ChannelState.CLOSING, ChannelState.FORCE_CLOSING]
return ChannelState.SHUTDOWN <= self.get_state() <= ChannelState.FORCE_CLOSING

def is_closed(self):
# the closing txid has been saved
return self.get_state() >= ChannelState.CLOSED
return self.get_state() >= ChannelState.CLOSING

def is_redeemed(self):
return self.get_state() == ChannelState.REDEEMED
Expand Down Expand Up @@ -707,8 +715,6 @@ def _assert_can_add_htlc(self, *, htlc_proposer: HTLCOwner, amount_msat: int,
# and the constraints are the ones imposed by their config
ctn = self.get_next_ctn(htlc_receiver)
chan_config = self.config[htlc_receiver]
if self.is_closed():
raise PaymentFailure('Channel closed')
if self.get_state() != ChannelState.OPEN:
raise PaymentFailure('Channel not open', self.get_state())
if htlc_proposer == LOCAL:
Expand Down Expand Up @@ -777,7 +783,7 @@ def can_receive(self, amount_msat: int, *, check_frozen=False,
return True

def should_try_to_reestablish_peer(self) -> bool:
return ChannelState.PREOPENING < self._state < ChannelState.FORCE_CLOSING and self.peer_state == PeerState.DISCONNECTED
return ChannelState.PREOPENING < self._state < ChannelState.CLOSING and self.peer_state == PeerState.DISCONNECTED

def get_funding_address(self):
script = funding_output_script(self.config[LOCAL], self.config[REMOTE])
Expand Down
24 changes: 15 additions & 9 deletions electrum/lnpeer.py
Expand Up @@ -87,7 +87,7 @@ def __init__(self, lnworker: Union['LNGossip', 'LNWallet'], pubkey:bytes, transp
self.temp_id_to_id = {} # to forward error messages
self.funding_created_sent = set() # for channels in PREOPENING
self.funding_signed_sent = set() # for channels in PREOPENING
self.shutdown_received = {}
self.shutdown_received = {} # chan_id -> asyncio.Future()
self.announcement_signatures = defaultdict(asyncio.Queue)
self.orphan_channel_updates = OrderedDict()
Logger.__init__(self)
Expand Down Expand Up @@ -933,7 +933,8 @@ def are_datalossprotect_fields_valid() -> bool:
if chan.is_funded() and chan.config[LOCAL].funding_locked_received:
self.mark_open(chan)
util.trigger_callback('channel', chan)
if chan.get_state() == ChannelState.CLOSING:
# if we have sent a previous shutdown, it must be retransmitted (Bolt2)
if chan.get_state() == ChannelState.SHUTDOWN:
await self.send_shutdown(chan)

def send_funding_locked(self, chan: Channel):
Expand Down Expand Up @@ -1429,7 +1430,7 @@ async def send_shutdown(self, chan: Channel):
while chan.has_pending_changes(REMOTE):
await asyncio.sleep(0.1)
self.send_message('shutdown', channel_id=chan.channel_id, len=len(scriptpubkey), scriptpubkey=scriptpubkey)
chan.set_state(ChannelState.CLOSING)
chan.set_state(ChannelState.SHUTDOWN)
# can fullfill or fail htlcs. cannot add htlcs, because of CLOSING state
chan.set_can_send_ctx_updates(True)

Expand Down Expand Up @@ -1492,12 +1493,17 @@ def verify_signature(tx, sig):
if not chan.constraints.is_initiator:
send_closing_signed()
# add signatures
closing_tx.add_signature_to_txin(txin_idx=0,
signing_pubkey=chan.config[LOCAL].multisig_key.pubkey.hex(),
sig=bh2u(der_sig_from_sig_string(our_sig) + b'\x01'))
closing_tx.add_signature_to_txin(txin_idx=0,
signing_pubkey=chan.config[REMOTE].multisig_key.pubkey.hex(),
sig=bh2u(der_sig_from_sig_string(their_sig) + b'\x01'))
closing_tx.add_signature_to_txin(
txin_idx=0,
signing_pubkey=chan.config[LOCAL].multisig_key.pubkey.hex(),
sig=bh2u(der_sig_from_sig_string(our_sig) + b'\x01'))
closing_tx.add_signature_to_txin(
txin_idx=0,
signing_pubkey=chan.config[REMOTE].multisig_key.pubkey.hex(),
sig=bh2u(der_sig_from_sig_string(their_sig) + b'\x01'))
# save local transaction and set state
self.lnworker.wallet.add_transaction(closing_tx)
chan.set_state(ChannelState.CLOSING)
# broadcast
await self.network.try_broadcasting(closing_tx, 'closing')
return closing_tx.txid()
Expand Down
7 changes: 7 additions & 0 deletions electrum/tests/test_lnpeer.py
Expand Up @@ -90,13 +90,20 @@ def is_tip_stale(self):


class MockWallet:

def set_label(self, x, y):
pass

def save_db(self):
pass

def add_transaction(self, tx):
pass

def is_lightning_backup(self):
return False


class MockLNWallet(Logger, NetworkRetryManager[LNPeerAddr]):
def __init__(self, *, local_keypair: Keypair, chans: Iterable['Channel'], tx_queue):
Logger.__init__(self)
Expand Down

0 comments on commit 2adbbee

Please sign in to comment.