• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spesmilo / electrum / 4878529344569344

04 Mar 2025 10:05AM UTC coverage: 60.716% (-0.02%) from 60.731%
4878529344569344

Pull #9587

CirrusCI

f321x
disable mpp flags in invoice creation if jit channel is required, check against available liquidity if we need a jit channel
Pull Request #9587: Disable mpp flags in invoice creation if jit channel is required and consider available liquidity

5 of 15 new or added lines in 2 files covered. (33.33%)

847 existing lines in 6 files now uncovered.

20678 of 34057 relevant lines covered (60.72%)

3.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.46
/electrum/lnpeer.py
1
#!/usr/bin/env python3
2
#
3
# Copyright (C) 2018 The Electrum developers
4
# Distributed under the MIT software license, see the accompanying
5
# file LICENCE or http://www.opensource.org/licenses/mit-license.php
6

7
import zlib
5✔
8
from collections import OrderedDict, defaultdict
5✔
9
import asyncio
5✔
10
import os
5✔
11
import time
5✔
12
from typing import Tuple, Dict, TYPE_CHECKING, Optional, Union, Set, Callable, Awaitable
5✔
13
from datetime import datetime
5✔
14
import functools
5✔
15

16
import electrum_ecc as ecc
5✔
17
from electrum_ecc import ecdsa_sig64_from_r_and_s, ecdsa_der_sig_from_ecdsa_sig64, ECPubkey
5✔
18

19
import aiorpcx
5✔
20
from aiorpcx import ignore_after
5✔
21

22
from .crypto import sha256, sha256d, privkey_to_pubkey
5✔
23
from . import bitcoin, util
5✔
24
from . import constants
5✔
25
from .util import (bfh, log_exceptions, ignore_exceptions, chunks, OldTaskGroup,
5✔
26
                   UnrelatedTransactionException, error_text_bytes_to_safe_str, AsyncHangDetector)
27
from .util import event_listener, EventListener
5✔
28
from . import transaction
5✔
29
from .bitcoin import make_op_return, DummyAddress
5✔
30
from .transaction import PartialTxOutput, match_script_against_template, Sighash
5✔
31
from .logging import Logger
5✔
32
from .lnrouter import RouteEdge
5✔
33
from .lnonion import (new_onion_packet, OnionFailureCode, calc_hops_data_for_payment, process_onion_packet,
5✔
34
                      OnionPacket, construct_onion_error, obfuscate_onion_error, OnionRoutingFailure,
35
                      ProcessedOnionPacket, UnsupportedOnionPacketVersion, InvalidOnionMac, InvalidOnionPubkey,
36
                      OnionFailureCodeMetaFlag)
37
from .lnchannel import Channel, RevokeAndAck, RemoteCtnTooFarInFuture, ChannelState, PeerState, ChanCloseOption, CF_ANNOUNCE_CHANNEL
5✔
38
from . import lnutil
5✔
39
from .lnutil import (Outpoint, LocalConfig, RECEIVED, UpdateAddHtlc, ChannelConfig,
5✔
40
                     RemoteConfig, OnlyPubkeyKeypair, ChannelConstraints, RevocationStore,
41
                     funding_output_script, get_per_commitment_secret_from_seed,
42
                     secret_to_pubkey, PaymentFailure, LnFeatures,
43
                     LOCAL, REMOTE, HTLCOwner,
44
                     ln_compare_features, MIN_FINAL_CLTV_DELTA_ACCEPTED,
45
                     RemoteMisbehaving, ShortChannelID,
46
                     IncompatibleLightningFeatures, derive_payment_secret_from_payment_preimage,
47
                     ChannelType, LNProtocolWarning, validate_features,
48
                     IncompatibleOrInsaneFeatures, FeeBudgetExceeded)
49
from .lnutil import FeeUpdate, channel_id_from_funding_tx, PaymentFeeBudget
5✔
50
from .lnutil import serialize_htlc_key, Keypair
5✔
51
from .lntransport import LNTransport, LNTransportBase, LightningPeerConnectionClosed, HandshakeFailed
5✔
52
from .lnmsg import encode_msg, decode_msg, UnknownOptionalMsgType, FailedToParseMsg
5✔
53
from .interface import GracefulDisconnect
5✔
54
from .lnrouter import fee_for_edge_msat
5✔
55
from .json_db import StoredDict
5✔
56
from .invoices import PR_PAID
5✔
57
from .simple_config import FEE_LN_ETA_TARGET, FEERATE_PER_KW_MIN_RELAY_LIGHTNING
5✔
58
from .trampoline import decode_routing_info
5✔
59

60
if TYPE_CHECKING:
5✔
61
    from .lnworker import LNGossip, LNWallet
×
62
    from .lnrouter import LNPaymentRoute
×
63
    from .transaction import PartialTransaction
×
64

65

66
LN_P2P_NETWORK_TIMEOUT = 20
5✔
67

68

69
class Peer(Logger, EventListener):
5✔
70
    # note: in general this class is NOT thread-safe. Most methods are assumed to be running on asyncio thread.
71

72
    LOGGING_SHORTCUT = 'P'
5✔
73

74
    ORDERED_MESSAGES = (
5✔
75
        'accept_channel', 'funding_signed', 'funding_created', 'accept_channel', 'closing_signed')
76
    SPAMMY_MESSAGES = (
5✔
77
        'ping', 'pong', 'channel_announcement', 'node_announcement', 'channel_update',)
78

79
    DELAY_INC_MSG_PROCESSING_SLEEP = 0.01
5✔
80

81
    def __init__(
5✔
82
            self,
83
            lnworker: Union['LNGossip', 'LNWallet'],
84
            pubkey: bytes,
85
            transport: LNTransportBase,
86
            *, is_channel_backup= False):
87

88
        self.lnworker = lnworker
5✔
89
        self.network = lnworker.network
5✔
90
        self.asyncio_loop = self.network.asyncio_loop
5✔
91
        self.is_channel_backup = is_channel_backup
5✔
92
        self._sent_init = False  # type: bool
5✔
93
        self._received_init = False  # type: bool
5✔
94
        self.initialized = self.asyncio_loop.create_future()
5✔
95
        self.got_disconnected = asyncio.Event()
5✔
96
        self.querying = asyncio.Event()
5✔
97
        self.transport = transport
5✔
98
        self.pubkey = pubkey  # remote pubkey
5✔
99
        self.privkey = self.transport.privkey  # local privkey
5✔
100
        self.features = self.lnworker.features  # type: LnFeatures
5✔
101
        self.their_features = LnFeatures(0)  # type: LnFeatures
5✔
102
        self.node_ids = [self.pubkey, privkey_to_pubkey(self.privkey)]
5✔
103
        assert self.node_ids[0] != self.node_ids[1]
5✔
104
        self.last_message_time = 0
5✔
105
        self.pong_event = asyncio.Event()
5✔
106
        self.reply_channel_range = asyncio.Queue()
5✔
107
        # gossip uses a single queue to preserve message order
108
        self.gossip_queue = asyncio.Queue()
5✔
109
        self.ordered_message_queues = defaultdict(asyncio.Queue)  # type: Dict[bytes, asyncio.Queue] # for messages that are ordered
5✔
110
        self.temp_id_to_id = {}  # type: Dict[bytes, Optional[bytes]]   # to forward error messages
5✔
111
        self.funding_created_sent = set() # for channels in PREOPENING
5✔
112
        self.funding_signed_sent = set()  # for channels in PREOPENING
5✔
113
        self.shutdown_received = {} # chan_id -> asyncio.Future()
5✔
114
        self.channel_reestablish_msg = defaultdict(self.asyncio_loop.create_future)  # type: Dict[bytes, asyncio.Future]
5✔
115
        self._chan_reest_finished = defaultdict(asyncio.Event)  # type: Dict[bytes, asyncio.Event]
5✔
116
        self.orphan_channel_updates = OrderedDict()  # type: OrderedDict[ShortChannelID, dict]
5✔
117
        Logger.__init__(self)
5✔
118
        self.taskgroup = OldTaskGroup()
5✔
119
        # HTLCs offered by REMOTE, that we started removing but are still active:
120
        self.received_htlcs_pending_removal = set()  # type: Set[Tuple[Channel, int]]
5✔
121
        self.received_htlc_removed_event = asyncio.Event()
5✔
122
        self._htlc_switch_iterstart_event = asyncio.Event()
5✔
123
        self._htlc_switch_iterdone_event = asyncio.Event()
5✔
124
        self._received_revack_event = asyncio.Event()
5✔
125
        self.received_commitsig_event = asyncio.Event()
5✔
126
        self.downstream_htlc_resolved_event = asyncio.Event()
5✔
127
        self.register_callbacks()
5✔
128

129
    def send_message(self, message_name: str, **kwargs):
5✔
130
        assert util.get_running_loop() == util.get_asyncio_loop(), f"this must be run on the asyncio thread!"
5✔
131
        assert type(message_name) is str
5✔
132
        if message_name not in self.SPAMMY_MESSAGES:
5✔
133
            self.logger.debug(f"Sending {message_name.upper()}")
5✔
134
        if message_name.upper() != "INIT" and not self.is_initialized():
5✔
135
            raise Exception("tried to send message before we are initialized")
×
136
        raw_msg = encode_msg(message_name, **kwargs)
5✔
137
        self._store_raw_msg_if_local_update(raw_msg, message_name=message_name, channel_id=kwargs.get("channel_id"))
5✔
138
        self.transport.send_bytes(raw_msg)
5✔
139

140
    def _store_raw_msg_if_local_update(self, raw_msg: bytes, *, message_name: str, channel_id: Optional[bytes]):
5✔
141
        is_commitment_signed = message_name == "commitment_signed"
5✔
142
        if not (message_name.startswith("update_") or is_commitment_signed):
5✔
143
            return
5✔
144
        assert channel_id
5✔
145
        chan = self.get_channel_by_id(channel_id)
5✔
146
        if not chan:
5✔
147
            raise Exception(f"channel {channel_id.hex()} not found for peer {self.pubkey.hex()}")
×
148
        chan.hm.store_local_update_raw_msg(raw_msg, is_commitment_signed=is_commitment_signed)
5✔
149
        if is_commitment_signed:
5✔
150
            # saving now, to ensure replaying updates works (in case of channel reestablishment)
151
            self.lnworker.save_channel(chan)
5✔
152

153
    def maybe_set_initialized(self):
5✔
154
        if self.initialized.done():
5✔
155
            return
5✔
156
        if self._sent_init and self._received_init:
5✔
157
            self.initialized.set_result(True)
5✔
158

159
    def is_initialized(self) -> bool:
5✔
160
        return (self.initialized.done()
5✔
161
                and not self.initialized.cancelled()
162
                and self.initialized.exception() is None
163
                and self.initialized.result() is True)
164

165
    async def initialize(self):
5✔
166
        # If outgoing transport, do handshake now. For incoming, it has already been done.
167
        if isinstance(self.transport, LNTransport):
5✔
168
            await self.transport.handshake()
×
169
        self.logger.info(f"handshake done for {self.transport.peer_addr or self.pubkey.hex()}")
5✔
170
        features = self.features.for_init_message()
5✔
171
        b = int.bit_length(features)
5✔
172
        flen = b // 8 + int(bool(b % 8))
5✔
173
        self.send_message(
5✔
174
            "init", gflen=0, flen=flen,
175
            features=features,
176
            init_tlvs={
177
                'networks':
178
                {'chains': constants.net.rev_genesis_bytes()}
179
            })
180
        self._sent_init = True
5✔
181
        self.maybe_set_initialized()
5✔
182

183
    @property
5✔
184
    def channels(self) -> Dict[bytes, Channel]:
5✔
185
        return self.lnworker.channels_for_peer(self.pubkey)
5✔
186

187
    def get_channel_by_id(self, channel_id: bytes) -> Optional[Channel]:
5✔
188
        # note: this is faster than self.channels.get(channel_id)
189
        chan = self.lnworker.get_channel_by_id(channel_id)
5✔
190
        if not chan:
5✔
191
            return None
×
192
        if chan.node_id != self.pubkey:
5✔
193
            return None
×
194
        return chan
5✔
195

196
    def diagnostic_name(self):
5✔
197
        return self.lnworker.__class__.__name__ + ', ' + self.transport.name()
5✔
198

199
    async def ping_if_required(self):
5✔
200
        if time.time() - self.last_message_time > 30:
5✔
201
            self.send_message('ping', num_pong_bytes=4, byteslen=4)
×
202
            self.pong_event.clear()
×
203
            await self.pong_event.wait()
×
204

205
    async def _process_message(self, message: bytes) -> None:
5✔
206
        try:
5✔
207
            message_type, payload = decode_msg(message)
5✔
208
        except UnknownOptionalMsgType as e:
5✔
209
            self.logger.info(f"received unknown message from peer. ignoring: {e!r}")
5✔
210
            return
5✔
211
        except FailedToParseMsg as e:
5✔
212
            self.logger.info(
5✔
213
                f"failed to parse message from peer. disconnecting. "
214
                f"msg_type={e.msg_type_name}({e.msg_type_int}). exc={e!r}")
215
            #self.logger.info(f"failed to parse message: message(SECRET?)={message.hex()}")
216
            raise GracefulDisconnect() from e
5✔
217
        self.last_message_time = time.time()
5✔
218
        if message_type not in self.SPAMMY_MESSAGES:
5✔
219
            self.logger.debug(f"Received {message_type.upper()}")
5✔
220
        # only process INIT if we are a backup
221
        if self.is_channel_backup is True and message_type != 'init':
5✔
222
            return
×
223
        if message_type in self.ORDERED_MESSAGES:
5✔
224
            chan_id = payload.get('channel_id') or payload["temporary_channel_id"]
5✔
225
            self.ordered_message_queues[chan_id].put_nowait((message_type, payload))
5✔
226
        else:
227
            if message_type not in ('error', 'warning') and 'channel_id' in payload:
5✔
228
                chan = self.get_channel_by_id(payload['channel_id'])
5✔
229
                if chan is None:
5✔
230
                    self.logger.info(f"Received {message_type} for unknown channel {payload['channel_id'].hex()}")
×
231
                    return
×
232
                args = (chan, payload)
5✔
233
            else:
234
                args = (payload,)
5✔
235
            try:
5✔
236
                f = getattr(self, 'on_' + message_type)
5✔
237
            except AttributeError:
×
238
                #self.logger.info("Received '%s'" % message_type.upper(), payload)
239
                return
×
240
            # raw message is needed to check signature
241
            if message_type in ['node_announcement', 'channel_announcement', 'channel_update']:
5✔
242
                payload['raw'] = message
5✔
243
            # note: the message handler might be async or non-async. In either case, by default,
244
            #       we wait for it to complete before we return, i.e. before the next message is processed.
245
            if asyncio.iscoroutinefunction(f):
5✔
246
                async with AsyncHangDetector(
5✔
247
                    message=f"message handler still running for {message_type.upper()}",
248
                    logger=self.logger,
249
                ):
250
                    await f(*args)
5✔
251
            else:
252
                f(*args)
5✔
253

254
    def non_blocking_msg_handler(func):
5✔
255
        """Makes a message handler non-blocking: while processing the message,
256
        the message_loop keeps processing subsequent incoming messages asynchronously.
257
        """
258
        assert asyncio.iscoroutinefunction(func), 'func needs to be a coroutine'
5✔
259
        @functools.wraps(func)
5✔
260
        async def wrapper(self: 'Peer', *args, **kwargs):
5✔
261
            return await self.taskgroup.spawn(func(self, *args, **kwargs))
5✔
262
        return wrapper
5✔
263

264
    def on_warning(self, payload):
5✔
265
        chan_id = payload.get("channel_id")
5✔
266
        err_bytes = payload['data']
5✔
267
        is_known_chan_id = (chan_id in self.channels) or (chan_id in self.temp_id_to_id)
5✔
268
        self.logger.info(f"remote peer sent warning [DO NOT TRUST THIS MESSAGE]: "
5✔
269
                         f"{error_text_bytes_to_safe_str(err_bytes, max_len=None)}. chan_id={chan_id.hex()}. "
270
                         f"{is_known_chan_id=}")
271

272
    def on_error(self, payload):
5✔
273
        chan_id = payload.get("channel_id")
5✔
274
        err_bytes = payload['data']
5✔
275
        is_known_chan_id = (chan_id in self.channels) or (chan_id in self.temp_id_to_id)
5✔
276
        self.logger.info(f"remote peer sent error [DO NOT TRUST THIS MESSAGE]: "
5✔
277
                         f"{error_text_bytes_to_safe_str(err_bytes, max_len=None)}. chan_id={chan_id.hex()}. "
278
                         f"{is_known_chan_id=}")
279
        if chan_id in self.channels:
5✔
280
            self.schedule_force_closing(chan_id)
5✔
281
            self.ordered_message_queues[chan_id].put_nowait((None, {'error': err_bytes}))
5✔
282
        elif chan_id in self.temp_id_to_id:
×
283
            chan_id = self.temp_id_to_id[chan_id] or chan_id
×
284
            self.ordered_message_queues[chan_id].put_nowait((None, {'error': err_bytes}))
×
285
        elif chan_id == bytes(32):
×
286
            # if channel_id is all zero:
287
            # - MUST fail all channels with the sending node.
288
            for cid in self.channels:
×
289
                self.schedule_force_closing(cid)
×
290
                self.ordered_message_queues[cid].put_nowait((None, {'error': err_bytes}))
×
291
        else:
292
            # if no existing channel is referred to by channel_id:
293
            # - MUST ignore the message.
294
            return
×
295
        raise GracefulDisconnect
5✔
296

297
    async def send_warning(self, channel_id: bytes, message: str = None, *, close_connection=False):
5✔
298
        """Sends a warning and disconnects if close_connection.
299

300
        Note:
301
        * channel_id is the temporary channel id when the channel id is not yet available
302

303
        A sending node:
304
        MAY set channel_id to all zero if the warning is not related to a specific channel.
305

306
        when failure was caused by an invalid signature check:
307
        * SHOULD include the raw, hex-encoded transaction in reply to a funding_created,
308
          funding_signed, closing_signed, or commitment_signed message.
309
        """
310
        assert isinstance(channel_id, bytes)
5✔
311
        encoded_data = b'' if not message else message.encode('ascii')
5✔
312
        self.send_message('warning', channel_id=channel_id, data=encoded_data, len=len(encoded_data))
5✔
313
        if close_connection:
5✔
314
            raise GracefulDisconnect
5✔
315

316
    async def send_error(self, channel_id: bytes, message: str = None, *, force_close_channel=False):
5✔
317
        """Sends an error message and force closes the channel.
318

319
        Note:
320
        * channel_id is the temporary channel id when the channel id is not yet available
321

322
        A sending node:
323
        * SHOULD send error for protocol violations or internal errors that make channels
324
          unusable or that make further communication unusable.
325
        * SHOULD send error with the unknown channel_id in reply to messages of type
326
          32-255 related to unknown channels.
327
        * MUST fail the channel(s) referred to by the error message.
328
        * MAY set channel_id to all zero to indicate all channels.
329

330
        when failure was caused by an invalid signature check:
331
        * SHOULD include the raw, hex-encoded transaction in reply to a funding_created,
332
          funding_signed, closing_signed, or commitment_signed message.
333
        """
334
        assert isinstance(channel_id, bytes)
5✔
335
        encoded_data = b'' if not message else message.encode('ascii')
5✔
336
        self.send_message('error', channel_id=channel_id, data=encoded_data, len=len(encoded_data))
5✔
337
        # MUST fail the channel(s) referred to by the error message:
338
        #  we may violate this with force_close_channel
339
        if force_close_channel:
5✔
340
            if channel_id in self.channels:
5✔
341
                self.schedule_force_closing(channel_id)
5✔
342
            elif channel_id == bytes(32):
×
343
                for cid in self.channels:
×
344
                    self.schedule_force_closing(cid)
×
345
        raise GracefulDisconnect
5✔
346

347
    def on_ping(self, payload):
5✔
348
        l = payload['num_pong_bytes']
5✔
349
        self.send_message('pong', byteslen=l)
5✔
350

351
    def on_pong(self, payload):
5✔
352
        self.pong_event.set()
5✔
353

354
    async def wait_for_message(self, expected_name: str, channel_id: bytes):
5✔
355
        q = self.ordered_message_queues[channel_id]
5✔
356
        name, payload = await util.wait_for2(q.get(), LN_P2P_NETWORK_TIMEOUT)
5✔
357
        # raise exceptions for errors, so that the caller sees them
358
        if (err_bytes := payload.get("error")) is not None:
5✔
359
            err_text = error_text_bytes_to_safe_str(err_bytes)
×
360
            raise GracefulDisconnect(
×
361
                f"remote peer sent error [DO NOT TRUST THIS MESSAGE]: {err_text}")
362
        if name != expected_name:
5✔
363
            raise Exception(f"Received unexpected '{name}'")
×
364
        return payload
5✔
365

366
    def on_init(self, payload):
5✔
367
        if self._received_init:
5✔
368
            self.logger.info("ALREADY INITIALIZED BUT RECEIVED INIT")
5✔
369
            return
5✔
370
        _their_features = int.from_bytes(payload['features'], byteorder="big")
5✔
371
        _their_features |= int.from_bytes(payload['globalfeatures'], byteorder="big")
5✔
372
        try:
5✔
373
            self.their_features = validate_features(_their_features)
5✔
374
        except IncompatibleOrInsaneFeatures as e:
×
375
            raise GracefulDisconnect(f"remote sent insane features: {repr(e)}")
×
376
        # check if features are compatible, and set self.features to what we negotiated
377
        try:
5✔
378
            self.features = ln_compare_features(self.features, self.their_features)
5✔
379
        except IncompatibleLightningFeatures as e:
×
380
            self.initialized.set_exception(e)
×
381
            raise GracefulDisconnect(f"{str(e)}")
×
382
        self.logger.info(
5✔
383
            f"received INIT with features={str(self.their_features.get_names())}. "
384
            f"negotiated={str(self.features)}")
385
        # check that they are on the same chain as us, if provided
386
        their_networks = payload["init_tlvs"].get("networks")
5✔
387
        if their_networks:
5✔
388
            their_chains = list(chunks(their_networks["chains"], 32))
5✔
389
            if constants.net.rev_genesis_bytes() not in their_chains:
5✔
390
                raise GracefulDisconnect(f"no common chain found with remote. (they sent: {their_chains})")
×
391
        # all checks passed
392
        self.lnworker.on_peer_successfully_established(self)
5✔
393
        self._received_init = True
5✔
394
        self.maybe_set_initialized()
5✔
395

396
    def on_node_announcement(self, payload):
5✔
397
        if not self.lnworker.uses_trampoline():
×
398
            self.gossip_queue.put_nowait(('node_announcement', payload))
×
399

400
    def on_channel_announcement(self, payload):
5✔
401
        if not self.lnworker.uses_trampoline():
×
402
            self.gossip_queue.put_nowait(('channel_announcement', payload))
×
403

404
    def on_channel_update(self, payload):
5✔
405
        self.maybe_save_remote_update(payload)
5✔
406
        if not self.lnworker.uses_trampoline():
5✔
407
            self.gossip_queue.put_nowait(('channel_update', payload))
5✔
408

409
    def maybe_save_remote_update(self, payload):
5✔
410
        if not self.channels:
5✔
411
            return
×
412
        for chan in self.channels.values():
5✔
413
            if payload['short_channel_id'] in [chan.short_channel_id, chan.get_local_scid_alias()]:
5✔
414
                chan.set_remote_update(payload)
5✔
415
                self.logger.info(f"saved remote channel_update gossip msg for chan {chan.get_id_for_log()}")
5✔
416
                break
5✔
417
        else:
418
            # Save (some bounded number of) orphan channel updates for later
419
            # as it might be for our own direct channel with this peer
420
            # (and we might not yet know the short channel id for that)
421
            # Background: this code is here to deal with a bug in LND,
422
            # see https://github.com/lightningnetwork/lnd/issues/3651
423
            # and https://github.com/lightningnetwork/lightning-rfc/pull/657
424
            # This code assumes gossip_queries is set. BOLT7: "if the
425
            # gossip_queries feature is negotiated, [a node] MUST NOT
426
            # send gossip it did not generate itself"
427
            short_channel_id = ShortChannelID(payload['short_channel_id'])
×
428
            self.logger.info(f'received orphan channel update {short_channel_id}')
×
429
            self.orphan_channel_updates[short_channel_id] = payload
×
430
            while len(self.orphan_channel_updates) > 25:
×
431
                self.orphan_channel_updates.popitem(last=False)
×
432

433
    def on_announcement_signatures(self, chan: Channel, payload):
5✔
434
        h = chan.get_channel_announcement_hash()
×
435
        node_signature = payload["node_signature"]
×
436
        bitcoin_signature = payload["bitcoin_signature"]
×
437
        if not ECPubkey(chan.config[REMOTE].multisig_key.pubkey).ecdsa_verify(bitcoin_signature, h):
×
438
            raise Exception("bitcoin_sig invalid in announcement_signatures")
×
439
        if not ECPubkey(self.pubkey).ecdsa_verify(node_signature, h):
×
440
            raise Exception("node_sig invalid in announcement_signatures")
×
441
        chan.config[REMOTE].announcement_node_sig = node_signature
×
442
        chan.config[REMOTE].announcement_bitcoin_sig = bitcoin_signature
×
443
        self.lnworker.save_channel(chan)
×
444
        self.maybe_send_announcement_signatures(chan, is_reply=True)
×
445

446
    def handle_disconnect(func):
5✔
447
        @functools.wraps(func)
5✔
448
        async def wrapper_func(self, *args, **kwargs):
5✔
449
            try:
×
450
                return await func(self, *args, **kwargs)
×
451
            except GracefulDisconnect as e:
×
452
                self.logger.log(e.log_level, f"Disconnecting: {repr(e)}")
×
453
            except (LightningPeerConnectionClosed, IncompatibleLightningFeatures,
×
454
                    aiorpcx.socks.SOCKSError) as e:
455
                self.logger.info(f"Disconnecting: {repr(e)}")
×
456
            finally:
457
                self.close_and_cleanup()
×
458
        return wrapper_func
5✔
459

460
    @ignore_exceptions  # do not kill outer taskgroup
5✔
461
    @log_exceptions
5✔
462
    @handle_disconnect
5✔
463
    async def main_loop(self):
5✔
464
        async with self.taskgroup as group:
×
465
            await group.spawn(self._message_loop())
×
466
            await group.spawn(self.htlc_switch())
×
467
            await group.spawn(self.query_gossip())
×
468
            await group.spawn(self.process_gossip())
×
469
            await group.spawn(self.send_own_gossip())
×
470

471
    async def process_gossip(self):
5✔
472
        while True:
×
473
            await asyncio.sleep(5)
×
474
            if not self.network.lngossip:
×
475
                continue
×
476
            chan_anns = []
×
477
            chan_upds = []
×
478
            node_anns = []
×
479
            while True:
×
480
                name, payload = await self.gossip_queue.get()
×
481
                if name == 'channel_announcement':
×
482
                    chan_anns.append(payload)
×
483
                elif name == 'channel_update':
×
484
                    chan_upds.append(payload)
×
485
                elif name == 'node_announcement':
×
486
                    node_anns.append(payload)
×
487
                else:
488
                    raise Exception('unknown message')
×
489
                if self.gossip_queue.empty():
×
490
                    break
×
491
            if self.network.lngossip:
×
492
                await self.network.lngossip.process_gossip(chan_anns, node_anns, chan_upds)
×
493

494
    async def send_own_gossip(self):
5✔
495
        if self.lnworker == self.lnworker.network.lngossip:
×
496
            return
×
497
        await asyncio.sleep(10)
×
498
        while True:
×
499
            public_channels = [chan for chan in self.lnworker.channels.values() if chan.is_public()]
×
500
            if public_channels:
×
501
                alias = self.lnworker.config.LIGHTNING_NODE_ALIAS
×
502
                self.send_node_announcement(alias)
×
503
                for chan in public_channels:
×
504
                    if chan.is_open() and chan.peer_state == PeerState.GOOD:
×
505
                        self.maybe_send_channel_announcement(chan)
×
506
            await asyncio.sleep(600)
×
507

508
    async def query_gossip(self):
5✔
509
        try:
×
510
            await util.wait_for2(self.initialized, LN_P2P_NETWORK_TIMEOUT)
×
511
        except Exception as e:
×
512
            raise GracefulDisconnect(f"Failed to initialize: {e!r}") from e
×
513
        if self.lnworker == self.lnworker.network.lngossip:
×
514
            try:
×
515
                ids, complete = await util.wait_for2(self.get_channel_range(), LN_P2P_NETWORK_TIMEOUT)
×
516
            except asyncio.TimeoutError as e:
×
517
                raise GracefulDisconnect("query_channel_range timed out") from e
×
518
            self.logger.info('Received {} channel ids. (complete: {})'.format(len(ids), complete))
×
519
            await self.lnworker.add_new_ids(ids)
×
520
            while True:
×
521
                todo = self.lnworker.get_ids_to_query()
×
522
                if not todo:
×
523
                    await asyncio.sleep(1)
×
524
                    continue
×
525
                await self.get_short_channel_ids(todo)
×
526

527
    async def get_channel_range(self):
5✔
528
        first_block = constants.net.BLOCK_HEIGHT_FIRST_LIGHTNING_CHANNELS
×
529
        num_blocks = self.lnworker.network.get_local_height() - first_block
×
530
        self.query_channel_range(first_block, num_blocks)
×
531
        intervals = []
×
532
        ids = set()
×
533
        # note: implementations behave differently...
534
        # "sane implementation that follows BOLT-07" example:
535
        #   query_channel_range. <<< first_block 497000, num_blocks 79038
536
        #   on_reply_channel_range. >>> first_block 497000, num_blocks 39516, num_ids 4648, complete True
537
        #   on_reply_channel_range. >>> first_block 536516, num_blocks 19758, num_ids 5734, complete True
538
        #   on_reply_channel_range. >>> first_block 556274, num_blocks 9879, num_ids 13712, complete True
539
        #   on_reply_channel_range. >>> first_block 566153, num_blocks 9885, num_ids 18114, complete True
540
        # lnd example:
541
        #   query_channel_range. <<< first_block 497000, num_blocks 79038
542
        #   on_reply_channel_range. >>> first_block 497000, num_blocks 79038, num_ids 8000, complete False
543
        #   on_reply_channel_range. >>> first_block 497000, num_blocks 79038, num_ids 8000, complete False
544
        #   on_reply_channel_range. >>> first_block 497000, num_blocks 79038, num_ids 8000, complete False
545
        #   on_reply_channel_range. >>> first_block 497000, num_blocks 79038, num_ids 8000, complete False
546
        #   on_reply_channel_range. >>> first_block 497000, num_blocks 79038, num_ids 5344, complete True
547
        while True:
×
548
            index, num, complete, _ids = await self.reply_channel_range.get()
×
549
            ids.update(_ids)
×
550
            intervals.append((index, index+num))
×
551
            intervals.sort()
×
552
            while len(intervals) > 1:
×
553
                a,b = intervals[0]
×
554
                c,d = intervals[1]
×
555
                if not (a <= c and a <= b and c <= d):
×
556
                    raise Exception(f"insane reply_channel_range intervals {(a,b,c,d)}")
×
557
                if b >= c:
×
558
                    intervals = [(a,d)] + intervals[2:]
×
559
                else:
560
                    break
×
561
            if len(intervals) == 1 and complete:
×
562
                a, b = intervals[0]
×
563
                if a <= first_block and b >= first_block + num_blocks:
×
564
                    break
×
565
        return ids, complete
×
566

567
    def request_gossip(self, timestamp=0):
5✔
568
        if timestamp == 0:
×
569
            self.logger.info('requesting whole channel graph')
×
570
        else:
571
            self.logger.info(f'requesting channel graph since {datetime.fromtimestamp(timestamp).ctime()}')
×
572
        self.send_message(
×
573
            'gossip_timestamp_filter',
574
            chain_hash=constants.net.rev_genesis_bytes(),
575
            first_timestamp=timestamp,
576
            timestamp_range=b'\xff'*4)
577

578
    def query_channel_range(self, first_block, num_blocks):
5✔
579
        self.logger.info(f'query channel range {first_block} {num_blocks}')
×
580
        self.send_message(
×
581
            'query_channel_range',
582
            chain_hash=constants.net.rev_genesis_bytes(),
583
            first_blocknum=first_block,
584
            number_of_blocks=num_blocks)
585

586
    def decode_short_ids(self, encoded):
5✔
587
        if encoded[0] == 0:
×
588
            decoded = encoded[1:]
×
589
        elif encoded[0] == 1:
×
590
            decoded = zlib.decompress(encoded[1:])
×
591
        else:
592
            raise Exception(f'decode_short_ids: unexpected first byte: {encoded[0]}')
×
593
        ids = [decoded[i:i+8] for i in range(0, len(decoded), 8)]
×
594
        return ids
×
595

596
    def on_reply_channel_range(self, payload):
5✔
597
        first = payload['first_blocknum']
×
598
        num = payload['number_of_blocks']
×
599
        complete = bool(int.from_bytes(payload['sync_complete'], 'big'))
×
600
        encoded = payload['encoded_short_ids']
×
601
        ids = self.decode_short_ids(encoded)
×
602
        #self.logger.info(f"on_reply_channel_range. >>> first_block {first}, num_blocks {num}, num_ids {len(ids)}, complete {repr(payload['complete'])}")
603
        self.reply_channel_range.put_nowait((first, num, complete, ids))
×
604

605
    async def get_short_channel_ids(self, ids):
5✔
606
        self.logger.info(f'Querying {len(ids)} short_channel_ids')
×
607
        assert not self.querying.is_set()
×
608
        self.query_short_channel_ids(ids)
×
609
        await self.querying.wait()
×
610
        self.querying.clear()
×
611

612
    def query_short_channel_ids(self, ids):
5✔
613
        # compression MUST NOT be used according to updated bolt
614
        # (https://github.com/lightning/bolts/pull/981)
615
        ids = sorted(ids)
×
616
        s = b''.join(ids)
×
617
        prefix = b'\x00'  # uncompressed
×
618
        self.send_message(
×
619
            'query_short_channel_ids',
620
            chain_hash=constants.net.rev_genesis_bytes(),
621
            len=1+len(s),
622
            encoded_short_ids=prefix+s)
623

624
    async def _message_loop(self):
5✔
625
        try:
5✔
626
            await util.wait_for2(self.initialize(), LN_P2P_NETWORK_TIMEOUT)
5✔
627
        except (OSError, asyncio.TimeoutError, HandshakeFailed) as e:
×
628
            raise GracefulDisconnect(f'initialize failed: {repr(e)}') from e
×
629
        async for msg in self.transport.read_messages():
5✔
630
            await self._process_message(msg)
5✔
631
            if self.DELAY_INC_MSG_PROCESSING_SLEEP:
5✔
632
                # rate-limit message-processing a bit, to make it harder
633
                # for a single peer to bog down the event loop / cpu:
634
                await asyncio.sleep(self.DELAY_INC_MSG_PROCESSING_SLEEP)
5✔
635

636
    def on_reply_short_channel_ids_end(self, payload):
5✔
637
        self.querying.set()
×
638

639
    def close_and_cleanup(self):
5✔
640
        # note: This method might get called multiple times!
641
        #       E.g. if you call close_and_cleanup() to cause a disconnection from the peer,
642
        #       it will get called a second time in handle_disconnect().
643
        self.unregister_callbacks()
×
644
        try:
×
645
            if self.transport:
×
646
                self.transport.close()
×
647
        except Exception:
×
648
            pass
×
649
        self.lnworker.peer_closed(self)
×
650
        self.got_disconnected.set()
×
651

652
    def is_shutdown_anysegwit(self):
5✔
653
        return self.features.supports(LnFeatures.OPTION_SHUTDOWN_ANYSEGWIT_OPT)
5✔
654

655
    def is_channel_type(self):
5✔
656
        return self.features.supports(LnFeatures.OPTION_CHANNEL_TYPE_OPT)
×
657

658
    def accepts_zeroconf(self):
5✔
659
        return self.features.supports(LnFeatures.OPTION_ZEROCONF_OPT)
5✔
660

661
    def is_upfront_shutdown_script(self):
5✔
662
        return self.features.supports(LnFeatures.OPTION_UPFRONT_SHUTDOWN_SCRIPT_OPT)
5✔
663

664
    def use_anchors(self) -> bool:
5✔
665
        return self.features.supports(LnFeatures.OPTION_ANCHORS_ZERO_FEE_HTLC_OPT)
×
666

667
    def upfront_shutdown_script_from_payload(self, payload, msg_identifier: str) -> Optional[bytes]:
5✔
668
        if msg_identifier not in ['accept', 'open']:
×
669
            raise ValueError("msg_identifier must be either 'accept' or 'open'")
×
670

671
        uss_tlv = payload[msg_identifier + '_channel_tlvs'].get(
×
672
            'upfront_shutdown_script')
673

674
        if uss_tlv and self.is_upfront_shutdown_script():
×
675
            upfront_shutdown_script = uss_tlv['shutdown_scriptpubkey']
×
676
        else:
677
            upfront_shutdown_script = b''
×
678
        self.logger.info(f"upfront shutdown script received: {upfront_shutdown_script}")
×
679
        return upfront_shutdown_script
×
680

681
    def make_local_config(
5✔
682
        self,
683
        *,
684
        funding_sat: int,
685
        push_msat: int,
686
        initiator: HTLCOwner,
687
        channel_type: ChannelType,
688
        multisig_funding_keypair: Optional[Keypair],  # if None, will get derived from channel_seed
689
    ) -> LocalConfig:
690
        channel_seed = os.urandom(32)
×
691
        initial_msat = funding_sat * 1000 - push_msat if initiator == LOCAL else push_msat
×
692

693
        # sending empty bytes as the upfront_shutdown_script will give us the
694
        # flexibility to decide an address at closing time
695
        upfront_shutdown_script = b''
×
696

697
        if self.use_anchors():
×
698
            static_payment_key = self.lnworker.static_payment_key
×
699
            static_remotekey = None
×
700
        else:
701
            assert channel_type & channel_type.OPTION_STATIC_REMOTEKEY
×
702
            wallet = self.lnworker.wallet
×
703
            assert wallet.txin_type == 'p2wpkh'
×
704
            addr = wallet.get_new_sweep_address_for_channel()
×
705
            static_payment_key = None
×
706
            static_remotekey = bytes.fromhex(wallet.get_public_key(addr))
×
707

708
        if multisig_funding_keypair:
×
709
            for chan in self.lnworker.channels.values():  # check against all chans of lnworker, for sanity
×
710
                if multisig_funding_keypair.pubkey == chan.config[LOCAL].multisig_key.pubkey:
×
711
                    raise Exception(
×
712
                        "Refusing to reuse multisig_funding_keypair for new channel. "
713
                        "Wait one block before opening another channel with this peer."
714
                    )
715

716
        dust_limit_sat = bitcoin.DUST_LIMIT_P2PKH
×
717
        reserve_sat = max(funding_sat // 100, dust_limit_sat)
×
718
        # for comparison of defaults, see
719
        # https://github.com/ACINQ/eclair/blob/afa378fbb73c265da44856b4ad0f2128a88ae6c6/eclair-core/src/main/resources/reference.conf#L66
720
        # https://github.com/ElementsProject/lightning/blob/0056dd75572a8857cff36fcbdb1a2295a1ac9253/lightningd/options.c#L657
721
        # https://github.com/lightningnetwork/lnd/blob/56b61078c5b2be007d318673a5f3b40c6346883a/config.go#L81
722
        local_config = LocalConfig.from_seed(
×
723
            channel_seed=channel_seed,
724
            static_remotekey=static_remotekey,
725
            static_payment_key=static_payment_key,
726
            multisig_key=multisig_funding_keypair,
727
            upfront_shutdown_script=upfront_shutdown_script,
728
            to_self_delay=self.network.config.LIGHTNING_TO_SELF_DELAY_CSV,
729
            dust_limit_sat=dust_limit_sat,
730
            max_htlc_value_in_flight_msat=funding_sat * 1000,
731
            max_accepted_htlcs=30,
732
            initial_msat=initial_msat,
733
            reserve_sat=reserve_sat,
734
            funding_locked_received=False,
735
            current_commitment_signature=None,
736
            current_htlc_signatures=b'',
737
            htlc_minimum_msat=1,
738
            announcement_node_sig=b'',
739
            announcement_bitcoin_sig=b'',
740
        )
741
        local_config.validate_params(funding_sat=funding_sat, config=self.network.config, peer_features=self.features)
×
742
        return local_config
×
743

744
    def temporarily_reserve_funding_tx_change_address(func):
5✔
745
        # During the channel open flow, if we initiated, we might have used a change address
746
        # of ours in the funding tx. The funding tx is not part of the wallet history
747
        # at that point yet, but we should already consider this change address as 'used'.
748
        @functools.wraps(func)
5✔
749
        async def wrapper(self: 'Peer', *args, **kwargs):
5✔
750
            funding_tx = kwargs['funding_tx']  # type: PartialTransaction
×
751
            wallet = self.lnworker.wallet
×
752
            change_addresses = [txout.address for txout in funding_tx.outputs()
×
753
                                if wallet.is_change(txout.address)]
754
            for addr in change_addresses:
×
755
                wallet.set_reserved_state_of_address(addr, reserved=True)
×
756
            try:
×
757
                return await func(self, *args, **kwargs)
×
758
            finally:
759
                for addr in change_addresses:
×
760
                    self.lnworker.wallet.set_reserved_state_of_address(addr, reserved=False)
×
761
        return wrapper
5✔
762

763
    @temporarily_reserve_funding_tx_change_address
5✔
764
    async def channel_establishment_flow(
5✔
765
            self, *,
766
            funding_tx: 'PartialTransaction',
767
            funding_sat: int,
768
            push_msat: int,
769
            public: bool,
770
            zeroconf: bool = False,
771
            temp_channel_id: bytes,
772
            opening_fee: int = None,
773
    ) -> Tuple[Channel, 'PartialTransaction']:
774
        """Implements the channel opening flow.
775

776
        -> open_channel message
777
        <- accept_channel message
778
        -> funding_created message
779
        <- funding_signed message
780

781
        Channel configurations are initialized in this method.
782
        """
783
        # will raise if init fails
784
        await util.wait_for2(self.initialized, LN_P2P_NETWORK_TIMEOUT)
×
785
        # trampoline is not yet in features
786
        if self.lnworker.uses_trampoline() and not self.lnworker.is_trampoline_peer(self.pubkey):
×
787
            raise Exception('Not a trampoline node: ' + str(self.their_features))
×
788

789
        if public and not self.lnworker.config.EXPERIMENTAL_LN_FORWARD_PAYMENTS:
×
790
            raise Exception('Cannot create public channels')
×
791

792
        channel_flags = CF_ANNOUNCE_CHANNEL if public else 0
×
793
        feerate = self.lnworker.current_target_feerate_per_kw()
×
794
        # we set a channel type for internal bookkeeping
795
        open_channel_tlvs = {}
×
796
        assert self.their_features.supports(LnFeatures.OPTION_STATIC_REMOTEKEY_OPT)
×
797
        our_channel_type = ChannelType(ChannelType.OPTION_STATIC_REMOTEKEY)
×
798
        if self.use_anchors():
×
799
            our_channel_type |= ChannelType(ChannelType.OPTION_ANCHORS_ZERO_FEE_HTLC_TX)
×
800
        if zeroconf:
×
801
            our_channel_type |= ChannelType(ChannelType.OPTION_ZEROCONF)
×
802
        # We do not set the option_scid_alias bit in channel_type because LND rejects it.
803
        # Eclair accepts channel_type with that bit, but does not require it.
804

805
        # if option_channel_type is negotiated: MUST set channel_type
806
        if self.is_channel_type():
×
807
            # if it includes channel_type: MUST set it to a defined type representing the type it wants.
808
            open_channel_tlvs['channel_type'] = {
×
809
                'type': our_channel_type.to_bytes_minimal()
810
            }
811

812
        if self.use_anchors():
×
813
            multisig_funding_keypair = lnutil.derive_multisig_funding_key_if_we_opened(
×
814
                funding_root_secret=self.lnworker.funding_root_keypair.privkey,
815
                remote_node_id_or_prefix=self.pubkey,
816
                nlocktime=funding_tx.locktime,
817
            )
818
        else:
819
            multisig_funding_keypair = None
×
820
        local_config = self.make_local_config(
×
821
            funding_sat=funding_sat,
822
            push_msat=push_msat,
823
            initiator=LOCAL,
824
            channel_type=our_channel_type,
825
            multisig_funding_keypair=multisig_funding_keypair,
826
        )
827
        # if it includes open_channel_tlvs: MUST include upfront_shutdown_script.
828
        open_channel_tlvs['upfront_shutdown_script'] = {
×
829
            'shutdown_scriptpubkey': local_config.upfront_shutdown_script
830
        }
831
        if opening_fee:
×
832
            # todo: maybe add payment hash
833
            open_channel_tlvs['channel_opening_fee'] = {
×
834
                'channel_opening_fee': opening_fee
835
            }
836
        # for the first commitment transaction
837
        per_commitment_secret_first = get_per_commitment_secret_from_seed(
×
838
            local_config.per_commitment_secret_seed,
839
            RevocationStore.START_INDEX
840
        )
841
        per_commitment_point_first = secret_to_pubkey(
×
842
            int.from_bytes(per_commitment_secret_first, 'big'))
843

844
        # store the temp id now, so that it is recognized for e.g. 'error' messages
845
        # TODO: this is never cleaned up; the dict grows unbounded until disconnect
846
        self.temp_id_to_id[temp_channel_id] = None
×
847
        self.send_message(
×
848
            "open_channel",
849
            temporary_channel_id=temp_channel_id,
850
            chain_hash=constants.net.rev_genesis_bytes(),
851
            funding_satoshis=funding_sat,
852
            push_msat=push_msat,
853
            dust_limit_satoshis=local_config.dust_limit_sat,
854
            feerate_per_kw=feerate,
855
            max_accepted_htlcs=local_config.max_accepted_htlcs,
856
            funding_pubkey=local_config.multisig_key.pubkey,
857
            revocation_basepoint=local_config.revocation_basepoint.pubkey,
858
            htlc_basepoint=local_config.htlc_basepoint.pubkey,
859
            payment_basepoint=local_config.payment_basepoint.pubkey,
860
            delayed_payment_basepoint=local_config.delayed_basepoint.pubkey,
861
            first_per_commitment_point=per_commitment_point_first,
862
            to_self_delay=local_config.to_self_delay,
863
            max_htlc_value_in_flight_msat=local_config.max_htlc_value_in_flight_msat,
864
            channel_flags=channel_flags,
865
            channel_reserve_satoshis=local_config.reserve_sat,
866
            htlc_minimum_msat=local_config.htlc_minimum_msat,
867
            open_channel_tlvs=open_channel_tlvs,
868
        )
869

870
        # <- accept_channel
871
        payload = await self.wait_for_message('accept_channel', temp_channel_id)
×
872
        self.logger.debug(f"received accept_channel for temp_channel_id={temp_channel_id.hex()}. {payload=}")
×
873
        remote_per_commitment_point = payload['first_per_commitment_point']
×
874
        funding_txn_minimum_depth = payload['minimum_depth']
×
875
        if not zeroconf and funding_txn_minimum_depth <= 0:
×
876
            raise Exception(f"minimum depth too low, {funding_txn_minimum_depth}")
×
877
        if funding_txn_minimum_depth > 30:
×
878
            raise Exception(f"minimum depth too high, {funding_txn_minimum_depth}")
×
879

880
        upfront_shutdown_script = self.upfront_shutdown_script_from_payload(
×
881
            payload, 'accept')
882

883
        accept_channel_tlvs = payload.get('accept_channel_tlvs')
×
884
        their_channel_type = accept_channel_tlvs.get('channel_type') if accept_channel_tlvs else None
×
885
        if their_channel_type:
×
886
            their_channel_type = ChannelType.from_bytes(their_channel_type['type'], byteorder='big').discard_unknown_and_check()
×
887
            # if channel_type is set, and channel_type was set in open_channel,
888
            # and they are not equal types: MUST reject the channel.
889
            if open_channel_tlvs.get('channel_type') is not None and their_channel_type != our_channel_type:
×
890
                raise Exception("Channel type is not the one that we sent.")
×
891

892
        remote_config = RemoteConfig(
×
893
            payment_basepoint=OnlyPubkeyKeypair(payload['payment_basepoint']),
894
            multisig_key=OnlyPubkeyKeypair(payload["funding_pubkey"]),
895
            htlc_basepoint=OnlyPubkeyKeypair(payload['htlc_basepoint']),
896
            delayed_basepoint=OnlyPubkeyKeypair(payload['delayed_payment_basepoint']),
897
            revocation_basepoint=OnlyPubkeyKeypair(payload['revocation_basepoint']),
898
            to_self_delay=payload['to_self_delay'],
899
            dust_limit_sat=payload['dust_limit_satoshis'],
900
            max_htlc_value_in_flight_msat=payload['max_htlc_value_in_flight_msat'],
901
            max_accepted_htlcs=payload["max_accepted_htlcs"],
902
            initial_msat=push_msat,
903
            reserve_sat=payload["channel_reserve_satoshis"],
904
            htlc_minimum_msat=payload['htlc_minimum_msat'],
905
            next_per_commitment_point=remote_per_commitment_point,
906
            current_per_commitment_point=None,
907
            upfront_shutdown_script=upfront_shutdown_script,
908
            announcement_node_sig=b'',
909
            announcement_bitcoin_sig=b'',
910
        )
911
        ChannelConfig.cross_validate_params(
×
912
            local_config=local_config,
913
            remote_config=remote_config,
914
            funding_sat=funding_sat,
915
            is_local_initiator=True,
916
            initial_feerate_per_kw=feerate,
917
            config=self.network.config,
918
            peer_features=self.features,
919
            has_anchors=self.use_anchors(),
920
        )
921

922
        # -> funding created
923
        # replace dummy output in funding tx
924
        redeem_script = funding_output_script(local_config, remote_config)
×
925
        funding_address = bitcoin.redeem_script_to_address('p2wsh', redeem_script)
×
926
        funding_output = PartialTxOutput.from_address_and_value(funding_address, funding_sat)
×
927
        funding_tx.replace_output_address(DummyAddress.CHANNEL, funding_address)
×
928
        # find and encrypt op_return data associated to funding_address
929
        has_onchain_backup = self.lnworker and self.lnworker.has_recoverable_channels()
×
930
        if has_onchain_backup:
×
931
            backup_data = self.lnworker.cb_data(self.pubkey)
×
932
            dummy_scriptpubkey = make_op_return(backup_data)
×
933
            for o in funding_tx.outputs():
×
934
                if o.scriptpubkey == dummy_scriptpubkey:
×
935
                    encrypted_data = self.lnworker.encrypt_cb_data(backup_data, funding_address)
×
936
                    assert len(encrypted_data) == len(backup_data)
×
937
                    o.scriptpubkey = make_op_return(encrypted_data)
×
938
                    break
×
939
            else:
940
                raise Exception('op_return output not found in funding tx')
×
941
        # must not be malleable
942
        funding_tx.set_rbf(False)
×
943
        if not funding_tx.is_segwit():
×
944
            raise Exception('Funding transaction is not segwit')
×
945
        funding_txid = funding_tx.txid()
×
946
        assert funding_txid
×
947
        funding_index = funding_tx.outputs().index(funding_output)
×
948
        # build remote commitment transaction
949
        channel_id, funding_txid_bytes = channel_id_from_funding_tx(funding_txid, funding_index)
×
950
        outpoint = Outpoint(funding_txid, funding_index)
×
951
        constraints = ChannelConstraints(
×
952
            flags=channel_flags,
953
            capacity=funding_sat,
954
            is_initiator=True,
955
            funding_txn_minimum_depth=funding_txn_minimum_depth
956
        )
957
        storage = self.create_channel_storage(
×
958
            channel_id, outpoint, local_config, remote_config, constraints, our_channel_type)
959
        chan = Channel(
×
960
            storage,
961
            lnworker=self.lnworker,
962
            initial_feerate=feerate
963
        )
964
        chan.storage['funding_inputs'] = [txin.prevout.to_json() for txin in funding_tx.inputs()]
×
965
        chan.storage['has_onchain_backup'] = has_onchain_backup
×
966
        if isinstance(self.transport, LNTransport):
×
967
            chan.add_or_update_peer_addr(self.transport.peer_addr)
×
968
        sig_64, _ = chan.sign_next_commitment()
×
969
        self.temp_id_to_id[temp_channel_id] = channel_id
×
970

971
        self.send_message("funding_created",
×
972
            temporary_channel_id=temp_channel_id,
973
            funding_txid=funding_txid_bytes,
974
            funding_output_index=funding_index,
975
            signature=sig_64)
976
        self.funding_created_sent.add(channel_id)
×
977

978
        # <- funding signed
979
        payload = await self.wait_for_message('funding_signed', channel_id)
×
980
        self.logger.info('received funding_signed')
×
981
        remote_sig = payload['signature']
×
982
        try:
×
983
            chan.receive_new_commitment(remote_sig, [])
×
984
        except LNProtocolWarning as e:
×
985
            await self.send_warning(channel_id, message=str(e), close_connection=True)
×
986
        chan.open_with_first_pcp(remote_per_commitment_point, remote_sig)
×
987
        chan.set_state(ChannelState.OPENING)
×
988
        if zeroconf:
×
989
            chan.set_state(ChannelState.FUNDED)
×
990
            self.send_channel_ready(chan)
×
991
        self.lnworker.add_new_channel(chan)
×
992
        return chan, funding_tx
×
993

994
    def create_channel_storage(self, channel_id, outpoint, local_config, remote_config, constraints, channel_type):
5✔
995
        chan_dict = {
×
996
            "node_id": self.pubkey.hex(),
997
            "channel_id": channel_id.hex(),
998
            "short_channel_id": None,
999
            "funding_outpoint": outpoint,
1000
            "remote_config": remote_config,
1001
            "local_config": local_config,
1002
            "constraints": constraints,
1003
            "remote_update": None,
1004
            "state": ChannelState.PREOPENING.name,
1005
            'onion_keys': {},
1006
            'data_loss_protect_remote_pcp': {},
1007
            "log": {},
1008
            "unfulfilled_htlcs": {},
1009
            "revocation_store": {},
1010
            "channel_type": channel_type,
1011
        }
1012
        # set db to None, because we do not want to write updates until channel is saved
1013
        return StoredDict(chan_dict, None, [])
×
1014

1015
    @non_blocking_msg_handler
5✔
1016
    async def on_open_channel(self, payload):
5✔
1017
        """Implements the channel acceptance flow.
1018

1019
        <- open_channel message
1020
        -> accept_channel message
1021
        <- funding_created message
1022
        -> funding_signed message
1023

1024
        Channel configurations are initialized in this method.
1025
        """
1026
        if self.lnworker.has_recoverable_channels():
×
1027
            # FIXME: we might want to keep the connection open
1028
            raise Exception('not accepting channels')
×
1029
        # <- open_channel
1030
        if payload['chain_hash'] != constants.net.rev_genesis_bytes():
×
1031
            raise Exception('wrong chain_hash')
×
1032
        funding_sat = payload['funding_satoshis']
×
1033
        push_msat = payload['push_msat']
×
1034
        feerate = payload['feerate_per_kw']  # note: we are not validating this
×
1035
        temp_chan_id = payload['temporary_channel_id']
×
1036
        # store the temp id now, so that it is recognized for e.g. 'error' messages
1037
        # TODO: this is never cleaned up; the dict grows unbounded until disconnect
1038
        self.temp_id_to_id[temp_chan_id] = None
×
1039

1040
        open_channel_tlvs = payload.get('open_channel_tlvs')
×
1041
        channel_type = open_channel_tlvs.get('channel_type') if open_channel_tlvs else None
×
1042

1043
        channel_opening_fee = open_channel_tlvs.get('channel_opening_fee') if open_channel_tlvs else None
×
1044
        if channel_opening_fee:
×
1045
            # todo check that the fee is reasonable
1046
            pass
×
1047
        # The receiving node MAY fail the channel if:
1048
        # option_channel_type was negotiated but the message doesn't include a channel_type
1049
        if self.is_channel_type() and channel_type is None:
×
1050
            raise Exception("sender has advertised option_channel_type, but hasn't sent the channel type")
×
1051
        # MUST fail the channel if it supports channel_type,
1052
        # channel_type was set, and the type is not suitable.
1053
        elif self.is_channel_type() and channel_type is not None:
×
1054
            channel_type = ChannelType.from_bytes(channel_type['type'], byteorder='big').discard_unknown_and_check()
×
1055
            if not channel_type.complies_with_features(self.features):
×
1056
                raise Exception("sender has sent a channel type we don't support")
×
1057

1058
        if self.use_anchors():
×
1059
            multisig_funding_keypair = lnutil.derive_multisig_funding_key_if_they_opened(
×
1060
                funding_root_secret=self.lnworker.funding_root_keypair.privkey,
1061
                remote_node_id_or_prefix=self.pubkey,
1062
                remote_funding_pubkey=payload['funding_pubkey'],
1063
            )
1064
        else:
1065
            multisig_funding_keypair = None
×
1066
        local_config = self.make_local_config(
×
1067
            funding_sat=funding_sat,
1068
            push_msat=push_msat,
1069
            initiator=REMOTE,
1070
            channel_type=channel_type,
1071
            multisig_funding_keypair=multisig_funding_keypair,
1072
        )
1073

1074
        upfront_shutdown_script = self.upfront_shutdown_script_from_payload(
×
1075
            payload, 'open')
1076

1077
        remote_config = RemoteConfig(
×
1078
            payment_basepoint=OnlyPubkeyKeypair(payload['payment_basepoint']),
1079
            multisig_key=OnlyPubkeyKeypair(payload['funding_pubkey']),
1080
            htlc_basepoint=OnlyPubkeyKeypair(payload['htlc_basepoint']),
1081
            delayed_basepoint=OnlyPubkeyKeypair(payload['delayed_payment_basepoint']),
1082
            revocation_basepoint=OnlyPubkeyKeypair(payload['revocation_basepoint']),
1083
            to_self_delay=payload['to_self_delay'],
1084
            dust_limit_sat=payload['dust_limit_satoshis'],
1085
            max_htlc_value_in_flight_msat=payload['max_htlc_value_in_flight_msat'],
1086
            max_accepted_htlcs=payload['max_accepted_htlcs'],
1087
            initial_msat=funding_sat * 1000 - push_msat,
1088
            reserve_sat=payload['channel_reserve_satoshis'],
1089
            htlc_minimum_msat=payload['htlc_minimum_msat'],
1090
            next_per_commitment_point=payload['first_per_commitment_point'],
1091
            current_per_commitment_point=None,
1092
            upfront_shutdown_script=upfront_shutdown_script,
1093
            announcement_node_sig=b'',
1094
            announcement_bitcoin_sig=b'',
1095
        )
1096
        ChannelConfig.cross_validate_params(
×
1097
            local_config=local_config,
1098
            remote_config=remote_config,
1099
            funding_sat=funding_sat,
1100
            is_local_initiator=False,
1101
            initial_feerate_per_kw=feerate,
1102
            config=self.network.config,
1103
            peer_features=self.features,
1104
            has_anchors=self.use_anchors(),
1105
        )
1106

1107
        channel_flags = ord(payload['channel_flags'])
×
1108

1109
        # -> accept channel
1110
        # for the first commitment transaction
1111
        per_commitment_secret_first = get_per_commitment_secret_from_seed(
×
1112
            local_config.per_commitment_secret_seed,
1113
            RevocationStore.START_INDEX
1114
        )
1115
        per_commitment_point_first = secret_to_pubkey(
×
1116
            int.from_bytes(per_commitment_secret_first, 'big'))
1117

1118
        is_zeroconf = channel_type & channel_type.OPTION_ZEROCONF
×
1119
        if is_zeroconf and not self.network.config.ZEROCONF_TRUSTED_NODE.startswith(self.pubkey.hex()):
×
1120
            raise Exception(f"not accepting zeroconf from node {self.pubkey}")
×
1121
        min_depth = 0 if is_zeroconf else 3
×
1122

1123
        accept_channel_tlvs = {
×
1124
            'upfront_shutdown_script': {
1125
                'shutdown_scriptpubkey': local_config.upfront_shutdown_script
1126
            },
1127
        }
1128
        # The sender: if it sets channel_type: MUST set it to the channel_type from open_channel
1129
        if self.is_channel_type():
×
1130
            accept_channel_tlvs['channel_type'] = {
×
1131
                'type': channel_type.to_bytes_minimal()
1132
            }
1133

1134
        self.send_message(
×
1135
            'accept_channel',
1136
            temporary_channel_id=temp_chan_id,
1137
            dust_limit_satoshis=local_config.dust_limit_sat,
1138
            max_htlc_value_in_flight_msat=local_config.max_htlc_value_in_flight_msat,
1139
            channel_reserve_satoshis=local_config.reserve_sat,
1140
            htlc_minimum_msat=local_config.htlc_minimum_msat,
1141
            minimum_depth=min_depth,
1142
            to_self_delay=local_config.to_self_delay,
1143
            max_accepted_htlcs=local_config.max_accepted_htlcs,
1144
            funding_pubkey=local_config.multisig_key.pubkey,
1145
            revocation_basepoint=local_config.revocation_basepoint.pubkey,
1146
            payment_basepoint=local_config.payment_basepoint.pubkey,
1147
            delayed_payment_basepoint=local_config.delayed_basepoint.pubkey,
1148
            htlc_basepoint=local_config.htlc_basepoint.pubkey,
1149
            first_per_commitment_point=per_commitment_point_first,
1150
            accept_channel_tlvs=accept_channel_tlvs,
1151
        )
1152

1153
        # <- funding created
1154
        funding_created = await self.wait_for_message('funding_created', temp_chan_id)
×
1155

1156
        # -> funding signed
1157
        funding_idx = funding_created['funding_output_index']
×
1158
        funding_txid = funding_created['funding_txid'][::-1].hex()
×
1159
        channel_id, funding_txid_bytes = channel_id_from_funding_tx(funding_txid, funding_idx)
×
1160
        constraints = ChannelConstraints(
×
1161
            flags=channel_flags,
1162
            capacity=funding_sat,
1163
            is_initiator=False,
1164
            funding_txn_minimum_depth=min_depth,
1165
        )
1166
        outpoint = Outpoint(funding_txid, funding_idx)
×
1167
        chan_dict = self.create_channel_storage(
×
1168
            channel_id, outpoint, local_config, remote_config, constraints, channel_type)
1169
        chan = Channel(
×
1170
            chan_dict,
1171
            lnworker=self.lnworker,
1172
            initial_feerate=feerate,
1173
            opening_fee = channel_opening_fee,
1174
        )
1175
        chan.storage['init_timestamp'] = int(time.time())
×
1176
        if isinstance(self.transport, LNTransport):
×
1177
            chan.add_or_update_peer_addr(self.transport.peer_addr)
×
1178
        remote_sig = funding_created['signature']
×
1179
        try:
×
1180
            chan.receive_new_commitment(remote_sig, [])
×
1181
        except LNProtocolWarning as e:
×
1182
            await self.send_warning(channel_id, message=str(e), close_connection=True)
×
1183
        sig_64, _ = chan.sign_next_commitment()
×
1184
        self.send_message('funding_signed',
×
1185
            channel_id=channel_id,
1186
            signature=sig_64,
1187
        )
1188
        self.temp_id_to_id[temp_chan_id] = channel_id
×
1189
        self.funding_signed_sent.add(chan.channel_id)
×
1190
        chan.open_with_first_pcp(payload['first_per_commitment_point'], remote_sig)
×
1191
        chan.set_state(ChannelState.OPENING)
×
1192
        if is_zeroconf:
×
1193
            chan.set_state(ChannelState.FUNDED)
×
1194
            self.send_channel_ready(chan)
×
1195
        self.lnworker.add_new_channel(chan)
×
1196

1197
    async def request_force_close(self, channel_id: bytes):
5✔
1198
        """Try to trigger the remote peer to force-close."""
1199
        await self.initialized
5✔
1200
        self.logger.info(f"trying to get remote peer to force-close chan {channel_id.hex()}")
5✔
1201
        # First, we intentionally send a "channel_reestablish" msg with an old state.
1202
        # Many nodes (but not all) automatically force-close when seeing this.
1203
        latest_point = secret_to_pubkey(42) # we need a valid point (BOLT2)
5✔
1204
        self.send_message(
5✔
1205
            "channel_reestablish",
1206
            channel_id=channel_id,
1207
            next_commitment_number=0,
1208
            next_revocation_number=0,
1209
            your_last_per_commitment_secret=0,
1210
            my_current_per_commitment_point=latest_point)
1211
        # Newish nodes that have lightning/bolts/pull/950 force-close upon receiving an "error" msg,
1212
        # so send that too. E.g. old "channel_reestablish" is not enough for eclair 0.7+,
1213
        # but "error" is. see https://github.com/ACINQ/eclair/pull/2036
1214
        # The receiving node:
1215
        #   - upon receiving `error`:
1216
        #     - MUST fail the channel referred to by `channel_id`, if that channel is with the sending node.
1217
        self.send_message("error", channel_id=channel_id, data=b"", len=0)
5✔
1218

1219
    def schedule_force_closing(self, channel_id: bytes):
5✔
1220
        """ wrapper of lnworker's method, that raises if channel is not with this peer """
1221
        channels_with_peer = list(self.channels.keys())
5✔
1222
        channels_with_peer.extend(self.temp_id_to_id.values())
5✔
1223
        if channel_id not in channels_with_peer:
5✔
1224
            raise ValueError(f"channel {channel_id.hex()} does not belong to this peer")
×
1225
        chan = self.channels.get(channel_id)
5✔
1226
        if not chan:
5✔
1227
            self.logger.warning(f"tried to force-close channel {channel_id.hex()} but it is not in self.channels yet")
×
1228
        if ChanCloseOption.LOCAL_FCLOSE in chan.get_close_options():
5✔
1229
            self.lnworker.schedule_force_closing(channel_id)
5✔
1230
        else:
1231
            self.logger.info(f"tried to force-close channel {chan.get_id_for_log()} "
5✔
1232
                             f"but close option is not allowed. {chan.get_state()=!r}")
1233

1234
    async def on_channel_reestablish(self, chan: Channel, msg):
5✔
1235
        # Note: it is critical for this message handler to block processing of further messages,
1236
        #       until this msg is processed. If we are behind (lost state), and send chan_reest to the remote,
1237
        #       when the remote realizes we are behind, they might send an "error" message - but the spec mandates
1238
        #       they send chan_reest first. If we processed the error first, we might force-close and lose money!
1239
        their_next_local_ctn = msg["next_commitment_number"]
5✔
1240
        their_oldest_unrevoked_remote_ctn = msg["next_revocation_number"]
5✔
1241
        their_local_pcp = msg.get("my_current_per_commitment_point")
5✔
1242
        their_claim_of_our_last_per_commitment_secret = msg.get("your_last_per_commitment_secret")
5✔
1243
        self.logger.info(
5✔
1244
            f'channel_reestablish ({chan.get_id_for_log()}): received channel_reestablish with '
1245
            f'(their_next_local_ctn={their_next_local_ctn}, '
1246
            f'their_oldest_unrevoked_remote_ctn={their_oldest_unrevoked_remote_ctn})')
1247
        # sanity checks of received values
1248
        if their_next_local_ctn < 0:
5✔
1249
            raise RemoteMisbehaving(f"channel reestablish: their_next_local_ctn < 0")
×
1250
        if their_oldest_unrevoked_remote_ctn < 0:
5✔
1251
            raise RemoteMisbehaving(f"channel reestablish: their_oldest_unrevoked_remote_ctn < 0")
×
1252
        # ctns
1253
        oldest_unrevoked_local_ctn = chan.get_oldest_unrevoked_ctn(LOCAL)
5✔
1254
        latest_local_ctn = chan.get_latest_ctn(LOCAL)
5✔
1255
        next_local_ctn = chan.get_next_ctn(LOCAL)
5✔
1256
        oldest_unrevoked_remote_ctn = chan.get_oldest_unrevoked_ctn(REMOTE)
5✔
1257
        latest_remote_ctn = chan.get_latest_ctn(REMOTE)
5✔
1258
        next_remote_ctn = chan.get_next_ctn(REMOTE)
5✔
1259
        # compare remote ctns
1260
        we_are_ahead = False
5✔
1261
        they_are_ahead = False
5✔
1262
        we_must_resend_revoke_and_ack = False
5✔
1263
        if next_remote_ctn != their_next_local_ctn:
5✔
1264
            if their_next_local_ctn == latest_remote_ctn and chan.hm.is_revack_pending(REMOTE):
5✔
1265
                # We will replay the local updates (see reestablish_channel), which should contain a commitment_signed
1266
                # (due to is_revack_pending being true), and this should remedy this situation.
1267
                pass
5✔
1268
            else:
1269
                self.logger.warning(
5✔
1270
                    f"channel_reestablish ({chan.get_id_for_log()}): "
1271
                    f"expected remote ctn {next_remote_ctn}, got {their_next_local_ctn}")
1272
                if their_next_local_ctn < next_remote_ctn:
5✔
1273
                    we_are_ahead = True
5✔
1274
                else:
1275
                    they_are_ahead = True
5✔
1276
        # compare local ctns
1277
        if oldest_unrevoked_local_ctn != their_oldest_unrevoked_remote_ctn:
5✔
1278
            if oldest_unrevoked_local_ctn - 1 == their_oldest_unrevoked_remote_ctn:
5✔
1279
                # A node:
1280
                #    if next_revocation_number is equal to the commitment number of the last revoke_and_ack
1281
                #    the receiving node sent, AND the receiving node hasn't already received a closing_signed:
1282
                #        MUST re-send the revoke_and_ack.
1283
                we_must_resend_revoke_and_ack = True
5✔
1284
            else:
1285
                self.logger.warning(
5✔
1286
                    f"channel_reestablish ({chan.get_id_for_log()}): "
1287
                    f"expected local ctn {oldest_unrevoked_local_ctn}, got {their_oldest_unrevoked_remote_ctn}")
1288
                if their_oldest_unrevoked_remote_ctn < oldest_unrevoked_local_ctn:
5✔
1289
                    we_are_ahead = True
5✔
1290
                else:
1291
                    they_are_ahead = True
5✔
1292
        # option_data_loss_protect
1293
        assert self.features.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT)
5✔
1294
        def are_datalossprotect_fields_valid() -> bool:
5✔
1295
            if their_local_pcp is None or their_claim_of_our_last_per_commitment_secret is None:
5✔
1296
                return False
×
1297
            if their_oldest_unrevoked_remote_ctn > 0:
5✔
1298
                our_pcs, __ = chan.get_secret_and_point(LOCAL, their_oldest_unrevoked_remote_ctn - 1)
5✔
1299
            else:
1300
                assert their_oldest_unrevoked_remote_ctn == 0
5✔
1301
                our_pcs = bytes(32)
5✔
1302
            if our_pcs != their_claim_of_our_last_per_commitment_secret:
5✔
1303
                self.logger.error(
×
1304
                    f"channel_reestablish ({chan.get_id_for_log()}): "
1305
                    f"(DLP) local PCS mismatch: {our_pcs.hex()} != {their_claim_of_our_last_per_commitment_secret.hex()}")
1306
                return False
×
1307
            assert chan.is_static_remotekey_enabled()
5✔
1308
            return True
5✔
1309
        if not are_datalossprotect_fields_valid():
5✔
1310
            raise RemoteMisbehaving("channel_reestablish: data loss protect fields invalid")
×
1311
        fut = self.channel_reestablish_msg[chan.channel_id]
5✔
1312
        if they_are_ahead:
5✔
1313
            self.logger.warning(
5✔
1314
                f"channel_reestablish ({chan.get_id_for_log()}): "
1315
                f"remote is ahead of us! They should force-close. Remote PCP: {their_local_pcp.hex()}")
1316
            # data_loss_protect_remote_pcp is used in lnsweep
1317
            chan.set_data_loss_protect_remote_pcp(their_next_local_ctn - 1, their_local_pcp)
5✔
1318
            chan.set_state(ChannelState.WE_ARE_TOXIC)
5✔
1319
            self.lnworker.save_channel(chan)
5✔
1320
            chan.peer_state = PeerState.BAD
5✔
1321
            # raise after we send channel_reestablish, so the remote can realize they are ahead
1322
            # FIXME what if we have multiple chans with peer? timing...
1323
            fut.set_exception(GracefulDisconnect("remote ahead of us"))
5✔
1324
        elif we_are_ahead:
5✔
1325
            self.logger.warning(f"channel_reestablish ({chan.get_id_for_log()}): we are ahead of remote! trying to force-close.")
5✔
1326
            self.schedule_force_closing(chan.channel_id)
5✔
1327
            # FIXME what if we have multiple chans with peer? timing...
1328
            fut.set_exception(GracefulDisconnect("we are ahead of remote"))
5✔
1329
        else:
1330
            # all good
1331
            fut.set_result((we_must_resend_revoke_and_ack, their_next_local_ctn))
5✔
1332
            # Block processing of further incoming messages until we finished our part of chan-reest.
1333
            # This is needed for the replaying of our local unacked updates to be sane (if the peer
1334
            # also replays some messages we must not react to them until we finished replaying our own).
1335
            # (it would be sufficient to only block messages related to this channel, but this is easier)
1336
            await self._chan_reest_finished[chan.channel_id].wait()
5✔
1337
            # Note: if the above event is never set, we won't detect if the connection was closed by remote...
1338

1339
    def _send_channel_reestablish(self, chan: Channel):
5✔
1340
        assert self.is_initialized()
5✔
1341
        chan_id = chan.channel_id
5✔
1342
        # ctns
1343
        next_local_ctn = chan.get_next_ctn(LOCAL)
5✔
1344
        oldest_unrevoked_remote_ctn = chan.get_oldest_unrevoked_ctn(REMOTE)
5✔
1345
        # send message
1346
        assert chan.is_static_remotekey_enabled()
5✔
1347
        latest_secret, latest_point = chan.get_secret_and_point(LOCAL, 0)
5✔
1348
        if oldest_unrevoked_remote_ctn == 0:
5✔
1349
            last_rev_secret = 0
5✔
1350
        else:
1351
            last_rev_index = oldest_unrevoked_remote_ctn - 1
5✔
1352
            last_rev_secret = chan.revocation_store.retrieve_secret(RevocationStore.START_INDEX - last_rev_index)
5✔
1353
        self.send_message(
5✔
1354
            "channel_reestablish",
1355
            channel_id=chan_id,
1356
            next_commitment_number=next_local_ctn,
1357
            next_revocation_number=oldest_unrevoked_remote_ctn,
1358
            your_last_per_commitment_secret=last_rev_secret,
1359
            my_current_per_commitment_point=latest_point)
1360
        self.logger.info(
5✔
1361
            f'channel_reestablish ({chan.get_id_for_log()}): sent channel_reestablish with '
1362
            f'(next_local_ctn={next_local_ctn}, '
1363
            f'oldest_unrevoked_remote_ctn={oldest_unrevoked_remote_ctn})')
1364

1365
    async def reestablish_channel(self, chan: Channel):
5✔
1366
        await self.initialized
5✔
1367
        chan_id = chan.channel_id
5✔
1368
        if chan.should_request_force_close:
5✔
1369
            if chan.get_state() != ChannelState.WE_ARE_TOXIC:
×
1370
                chan.set_state(ChannelState.REQUESTED_FCLOSE)
×
1371
            await self.request_force_close(chan_id)
×
1372
            chan.should_request_force_close = False
×
1373
            return
×
1374
        if chan.get_state() == ChannelState.WE_ARE_TOXIC:
5✔
1375
            # Depending on timing, the remote might not know we are behind.
1376
            # We should let them know, so that they force-close.
1377
            # We do "request force-close" with ctn=0, instead of leaking our actual ctns,
1378
            # to decrease the remote's confidence of actual data loss on our part.
1379
            await self.request_force_close(chan_id)
5✔
1380
            return
5✔
1381
        if chan.get_state() == ChannelState.FORCE_CLOSING:
5✔
1382
            # We likely got here because we found out that we are ahead (i.e. remote lost state).
1383
            # Depending on timing, the remote might not know they are behind.
1384
            # We should let them know:
1385
            self._send_channel_reestablish(chan)
5✔
1386
            return
5✔
1387
        # if we get here, we will try to do a proper reestablish
1388
        if not (ChannelState.PREOPENING < chan.get_state() < ChannelState.FORCE_CLOSING):
5✔
1389
            raise Exception(f"unexpected {chan.get_state()=} for reestablish")
×
1390
        if chan.peer_state != PeerState.DISCONNECTED:
5✔
1391
            self.logger.info(
×
1392
                f'reestablish_channel was called but channel {chan.get_id_for_log()} '
1393
                f'already in peer_state {chan.peer_state!r}')
1394
            return
×
1395
        chan.peer_state = PeerState.REESTABLISHING
5✔
1396
        util.trigger_callback('channel', self.lnworker.wallet, chan)
5✔
1397
        # ctns
1398
        oldest_unrevoked_local_ctn = chan.get_oldest_unrevoked_ctn(LOCAL)
5✔
1399
        next_local_ctn = chan.get_next_ctn(LOCAL)
5✔
1400
        oldest_unrevoked_remote_ctn = chan.get_oldest_unrevoked_ctn(REMOTE)
5✔
1401
        # BOLT-02: "A node [...] upon disconnection [...] MUST reverse any uncommitted updates sent by the other side"
1402
        chan.hm.discard_unsigned_remote_updates()
5✔
1403
        # send message
1404
        self._send_channel_reestablish(chan)
5✔
1405
        # wait until we receive their channel_reestablish
1406
        fut = self.channel_reestablish_msg[chan_id]
5✔
1407
        await fut
5✔
1408
        we_must_resend_revoke_and_ack, their_next_local_ctn = fut.result()
5✔
1409

1410
        def replay_updates_and_commitsig():
5✔
1411
            # Replay un-acked local updates (including commitment_signed) byte-for-byte.
1412
            # If we have sent them a commitment signature that they "lost" (due to disconnect),
1413
            # we need to make sure we replay the same local updates, as otherwise they could
1414
            # end up with two (or more) signed valid commitment transactions at the same ctn.
1415
            # Multiple valid ctxs at the same ctn is a major headache for pre-signing spending txns,
1416
            # e.g. for watchtowers, hence we must ensure these ctxs coincide.
1417
            # We replay the local updates even if they were not yet committed.
1418
            unacked = chan.hm.get_unacked_local_updates()
5✔
1419
            replayed_msgs = []
5✔
1420
            for ctn, messages in unacked.items():
5✔
1421
                if ctn < their_next_local_ctn:
5✔
1422
                    # They claim to have received these messages and the corresponding
1423
                    # commitment_signed, hence we must not replay them.
1424
                    continue
5✔
1425
                for raw_upd_msg in messages:
5✔
1426
                    self.transport.send_bytes(raw_upd_msg)
5✔
1427
                    replayed_msgs.append(raw_upd_msg)
5✔
1428
            self.logger.info(f'channel_reestablish ({chan.get_id_for_log()}): replayed {len(replayed_msgs)} unacked messages. '
5✔
1429
                             f'{[decode_msg(raw_upd_msg)[0] for raw_upd_msg in replayed_msgs]}')
1430

1431
        def resend_revoke_and_ack():
5✔
1432
            last_secret, last_point = chan.get_secret_and_point(LOCAL, oldest_unrevoked_local_ctn - 1)
5✔
1433
            next_secret, next_point = chan.get_secret_and_point(LOCAL, oldest_unrevoked_local_ctn + 1)
5✔
1434
            self.send_message(
5✔
1435
                "revoke_and_ack",
1436
                channel_id=chan.channel_id,
1437
                per_commitment_secret=last_secret,
1438
                next_per_commitment_point=next_point)
1439

1440
        # We need to preserve relative order of last revack and commitsig.
1441
        # note: it is not possible to recover and reestablish a channel if we are out-of-sync by
1442
        # more than one ctns, i.e. we will only ever retransmit up to one commitment_signed message.
1443
        # Hence, if we need to retransmit a revack, without loss of generality, we can either replay
1444
        # it as the first message or as the last message.
1445
        was_revoke_last = chan.hm.was_revoke_last()
5✔
1446
        if we_must_resend_revoke_and_ack and not was_revoke_last:
5✔
1447
            self.logger.info(f'channel_reestablish ({chan.get_id_for_log()}): replaying a revoke_and_ack first.')
5✔
1448
            resend_revoke_and_ack()
5✔
1449
        replay_updates_and_commitsig()
5✔
1450
        if we_must_resend_revoke_and_ack and was_revoke_last:
5✔
1451
            self.logger.info(f'channel_reestablish ({chan.get_id_for_log()}): replaying a revoke_and_ack last.')
5✔
1452
            resend_revoke_and_ack()
5✔
1453

1454
        chan.peer_state = PeerState.GOOD
5✔
1455
        self._chan_reest_finished[chan.channel_id].set()
5✔
1456
        if chan.is_funded():
5✔
1457
            chan_just_became_ready = (their_next_local_ctn == next_local_ctn == 1)
5✔
1458
            if chan_just_became_ready or self.features.supports(LnFeatures.OPTION_SCID_ALIAS_OPT):
5✔
1459
                self.send_channel_ready(chan)
5✔
1460

1461
        self.maybe_send_announcement_signatures(chan)
5✔
1462
        self.maybe_update_fee(chan)  # if needed, update fee ASAP, to avoid force-closures from this
5✔
1463
        # checks done
1464
        util.trigger_callback('channel', self.lnworker.wallet, chan)
5✔
1465
        # if we have sent a previous shutdown, it must be retransmitted (Bolt2)
1466
        if chan.get_state() == ChannelState.SHUTDOWN:
5✔
1467
            await self.taskgroup.spawn(self.send_shutdown(chan))
×
1468

1469
    def send_channel_ready(self, chan: Channel):
5✔
1470
        assert chan.is_funded()
5✔
1471
        if chan.sent_channel_ready:
5✔
1472
            return
×
1473
        channel_id = chan.channel_id
5✔
1474
        per_commitment_secret_index = RevocationStore.START_INDEX - 1
5✔
1475
        second_per_commitment_point = secret_to_pubkey(int.from_bytes(
5✔
1476
            get_per_commitment_secret_from_seed(chan.config[LOCAL].per_commitment_secret_seed, per_commitment_secret_index), 'big'))
1477
        channel_ready_tlvs = {}
5✔
1478
        if self.features.supports(LnFeatures.OPTION_SCID_ALIAS_OPT):
5✔
1479
            # LND requires that we send an alias if the option has been negotiated in INIT.
1480
            # otherwise, the channel will not be marked as active.
1481
            # This does not apply if the channel was previously marked active without an alias.
1482
            channel_ready_tlvs['short_channel_id'] = {'alias': chan.get_local_scid_alias(create_new_if_needed=True)}
5✔
1483
        # note: if 'channel_ready' was not yet received, we might send it multiple times
1484
        self.send_message(
5✔
1485
            "channel_ready",
1486
            channel_id=channel_id,
1487
            second_per_commitment_point=second_per_commitment_point,
1488
            channel_ready_tlvs=channel_ready_tlvs)
1489
        chan.sent_channel_ready = True
5✔
1490
        self.maybe_mark_open(chan)
5✔
1491

1492
    def on_channel_ready(self, chan: Channel, payload):
5✔
1493
        self.logger.info(f"on_channel_ready. channel: {chan.channel_id.hex()}")
5✔
1494
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
1495
            raise Exception(f"received channel_ready in unexpected {chan.peer_state=!r}")
×
1496
        # save remote alias for use in invoices
1497
        scid_alias = payload.get('channel_ready_tlvs', {}).get('short_channel_id', {}).get('alias')
5✔
1498
        if scid_alias:
5✔
1499
            chan.save_remote_scid_alias(scid_alias)
5✔
1500
        if not chan.config[LOCAL].funding_locked_received:
5✔
1501
            their_next_point = payload["second_per_commitment_point"]
×
1502
            chan.config[REMOTE].next_per_commitment_point = their_next_point
×
1503
            chan.config[LOCAL].funding_locked_received = True
×
1504
            self.lnworker.save_channel(chan)
×
1505
        self.maybe_mark_open(chan)
5✔
1506

1507
    def send_node_announcement(self, alias:str):
5✔
1508
        from .channel_db import NodeInfo
×
1509
        timestamp = int(time.time())
×
1510
        node_id = privkey_to_pubkey(self.privkey)
×
1511
        features = self.features.for_node_announcement()
×
1512
        b = int.bit_length(features)
×
1513
        flen = b // 8 + int(bool(b % 8))
×
1514
        rgb_color = bytes.fromhex('000000')
×
1515
        alias = bytes(alias, 'utf8')
×
1516
        alias += bytes(32 - len(alias))
×
1517
        addr = self.lnworker.config.LIGHTNING_LISTEN
×
1518
        hostname, port = addr.split(':')
×
1519
        if port is None:  # use default port if not specified
×
1520
            port = 9735
×
1521
        addresses = NodeInfo.to_addresses_field(hostname, int(port))
×
1522
        raw_msg = encode_msg(
×
1523
            "node_announcement",
1524
            flen=flen,
1525
            features=features,
1526
            timestamp=timestamp,
1527
            rgb_color=rgb_color,
1528
            node_id=node_id,
1529
            alias=alias,
1530
            addrlen=len(addresses),
1531
            addresses=addresses)
1532
        h = sha256d(raw_msg[64+2:])
×
1533
        signature = ecc.ECPrivkey(self.privkey).ecdsa_sign(h, sigencode=ecdsa_sig64_from_r_and_s)
×
1534
        message_type, payload = decode_msg(raw_msg)
×
1535
        payload['signature'] = signature
×
1536
        raw_msg = encode_msg(message_type, **payload)
×
1537
        self.transport.send_bytes(raw_msg)
×
1538

1539
    def maybe_send_channel_announcement(self, chan: Channel):
5✔
1540
        node_sigs = [chan.config[REMOTE].announcement_node_sig, chan.config[LOCAL].announcement_node_sig]
×
1541
        bitcoin_sigs = [chan.config[REMOTE].announcement_bitcoin_sig, chan.config[LOCAL].announcement_bitcoin_sig]
×
1542
        if not bitcoin_sigs[0] or not bitcoin_sigs[1]:
×
1543
            return
×
1544
        raw_msg, is_reverse = chan.construct_channel_announcement_without_sigs()
×
1545
        if is_reverse:
×
1546
            node_sigs.reverse()
×
1547
            bitcoin_sigs.reverse()
×
1548
        message_type, payload = decode_msg(raw_msg)
×
1549
        payload['node_signature_1'] = node_sigs[0]
×
1550
        payload['node_signature_2'] = node_sigs[1]
×
1551
        payload['bitcoin_signature_1'] = bitcoin_sigs[0]
×
1552
        payload['bitcoin_signature_2'] = bitcoin_sigs[1]
×
1553
        raw_msg = encode_msg(message_type, **payload)
×
1554
        self.transport.send_bytes(raw_msg)
×
1555

1556
    def maybe_mark_open(self, chan: Channel):
5✔
1557
        if not chan.sent_channel_ready:
5✔
1558
            return
×
1559
        if not chan.config[LOCAL].funding_locked_received:
5✔
1560
            return
×
1561
        self.mark_open(chan)
5✔
1562

1563
    def mark_open(self, chan: Channel):
5✔
1564
        assert chan.is_funded()
5✔
1565
        # only allow state transition from "FUNDED" to "OPEN"
1566
        old_state = chan.get_state()
5✔
1567
        if old_state == ChannelState.OPEN:
5✔
1568
            return
5✔
1569
        if old_state != ChannelState.FUNDED:
5✔
1570
            self.logger.info(f"cannot mark open ({chan.get_id_for_log()}), current state: {repr(old_state)}")
×
1571
            return
×
1572
        assert chan.config[LOCAL].funding_locked_received
5✔
1573
        chan.set_state(ChannelState.OPEN)
5✔
1574
        util.trigger_callback('channel', self.lnworker.wallet, chan)
5✔
1575
        # peer may have sent us a channel update for the incoming direction previously
1576
        pending_channel_update = self.orphan_channel_updates.get(chan.short_channel_id)
5✔
1577
        if pending_channel_update:
5✔
1578
            chan.set_remote_update(pending_channel_update)
×
1579
        self.logger.info(f"CHANNEL OPENING COMPLETED ({chan.get_id_for_log()})")
5✔
1580
        forwarding_enabled = self.network.config.EXPERIMENTAL_LN_FORWARD_PAYMENTS
5✔
1581
        if forwarding_enabled and chan.short_channel_id:
5✔
1582
            # send channel_update of outgoing edge to peer,
1583
            # so that channel can be used to receive payments
1584
            self.logger.info(f"sending channel update for outgoing edge ({chan.get_id_for_log()})")
5✔
1585
            chan_upd = chan.get_outgoing_gossip_channel_update()
5✔
1586
            self.transport.send_bytes(chan_upd)
5✔
1587

1588
    def maybe_send_announcement_signatures(self, chan: Channel, is_reply=False):
5✔
1589
        if not chan.is_public():
5✔
1590
            return
5✔
1591
        if chan.sent_announcement_signatures:
×
1592
            return
×
1593
        if not is_reply and chan.config[REMOTE].announcement_node_sig:
×
1594
            return
×
1595
        h = chan.get_channel_announcement_hash()
×
1596
        bitcoin_signature = ecc.ECPrivkey(chan.config[LOCAL].multisig_key.privkey).ecdsa_sign(h, sigencode=ecdsa_sig64_from_r_and_s)
×
1597
        node_signature = ecc.ECPrivkey(self.privkey).ecdsa_sign(h, sigencode=ecdsa_sig64_from_r_and_s)
×
1598
        self.send_message(
×
1599
            "announcement_signatures",
1600
            channel_id=chan.channel_id,
1601
            short_channel_id=chan.short_channel_id,
1602
            node_signature=node_signature,
1603
            bitcoin_signature=bitcoin_signature
1604
        )
1605
        chan.config[LOCAL].announcement_node_sig = node_signature
×
1606
        chan.config[LOCAL].announcement_bitcoin_sig = bitcoin_signature
×
1607
        self.lnworker.save_channel(chan)
×
1608
        chan.sent_announcement_signatures = True
×
1609

1610
    def on_update_fail_htlc(self, chan: Channel, payload):
5✔
1611
        htlc_id = payload["id"]
5✔
1612
        reason = payload["reason"]
5✔
1613
        self.logger.info(f"on_update_fail_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}")
5✔
1614
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
1615
            raise Exception(f"received update_fail_htlc in unexpected {chan.peer_state=!r}")
×
1616
        chan.receive_fail_htlc(htlc_id, error_bytes=reason)  # TODO handle exc and maybe fail channel (e.g. bad htlc_id)
5✔
1617
        self.maybe_send_commitment(chan)
5✔
1618

1619
    def maybe_send_commitment(self, chan: Channel) -> bool:
5✔
1620
        assert util.get_running_loop() == util.get_asyncio_loop(), f"this must be run on the asyncio thread!"
5✔
1621
        if chan.is_closed():
5✔
1622
            return False
×
1623
        # REMOTE should revoke first before we can sign a new ctx
1624
        if chan.hm.is_revack_pending(REMOTE):
5✔
1625
            return False
5✔
1626
        # if there are no changes, we will not (and must not) send a new commitment
1627
        if not chan.has_pending_changes(REMOTE):
5✔
1628
            return False
5✔
1629
        self.logger.info(f'send_commitment. chan {chan.short_channel_id}. ctn: {chan.get_next_ctn(REMOTE)}.')
5✔
1630
        sig_64, htlc_sigs = chan.sign_next_commitment()
5✔
1631
        self.send_message("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=len(htlc_sigs), htlc_signature=b"".join(htlc_sigs))
5✔
1632
        return True
5✔
1633

1634
    def create_onion_for_route(
5✔
1635
            self, *,
1636
            route: 'LNPaymentRoute',
1637
            amount_msat: int,
1638
            total_msat: int,
1639
            payment_hash: bytes,
1640
            min_final_cltv_delta: int,
1641
            payment_secret: bytes,
1642
            trampoline_onion: Optional[OnionPacket] = None,
1643
    ):
1644
        # add features learned during "init" for direct neighbour:
1645
        route[0].node_features |= self.features
5✔
1646
        local_height = self.network.get_local_height()
5✔
1647
        final_cltv_abs = local_height + min_final_cltv_delta
5✔
1648
        hops_data, amount_msat, cltv_abs = calc_hops_data_for_payment(
5✔
1649
            route,
1650
            amount_msat,
1651
            final_cltv_abs=final_cltv_abs,
1652
            total_msat=total_msat,
1653
            payment_secret=payment_secret)
1654
        num_hops = len(hops_data)
5✔
1655
        self.logger.info(f"lnpeer.pay len(route)={len(route)}")
5✔
1656
        for i in range(len(route)):
5✔
1657
            self.logger.info(f"  {i}: edge={route[i].short_channel_id} hop_data={hops_data[i]!r}")
5✔
1658
        assert final_cltv_abs <= cltv_abs, (final_cltv_abs, cltv_abs)
5✔
1659
        session_key = os.urandom(32) # session_key
5✔
1660
        # if we are forwarding a trampoline payment, add trampoline onion
1661
        if trampoline_onion:
5✔
1662
            self.logger.info(f'adding trampoline onion to final payload')
5✔
1663
            trampoline_payload = hops_data[-1].payload
5✔
1664
            trampoline_payload["trampoline_onion_packet"] = {
5✔
1665
                "version": trampoline_onion.version,
1666
                "public_key": trampoline_onion.public_key,
1667
                "hops_data": trampoline_onion.hops_data,
1668
                "hmac": trampoline_onion.hmac
1669
            }
1670
            if t_hops_data := trampoline_onion._debug_hops_data:  # None if trampoline-forwarding
5✔
1671
                t_route = trampoline_onion._debug_route
5✔
1672
                assert t_route is not None
5✔
1673
                self.logger.info(f"lnpeer.pay len(t_route)={len(t_route)}")
5✔
1674
                for i in range(len(t_route)):
5✔
1675
                    self.logger.info(f"  {i}: t_node={t_route[i].end_node.hex()} hop_data={t_hops_data[i]!r}")
5✔
1676
        # create onion packet
1677
        payment_path_pubkeys = [x.node_id for x in route]
5✔
1678
        onion = new_onion_packet(payment_path_pubkeys, session_key, hops_data, associated_data=payment_hash) # must use another sessionkey
5✔
1679
        self.logger.info(f"starting payment. len(route)={len(hops_data)}.")
5✔
1680
        # create htlc
1681
        if cltv_abs > local_height + lnutil.NBLOCK_CLTV_DELTA_TOO_FAR_INTO_FUTURE:
5✔
1682
            raise PaymentFailure(f"htlc expiry too far into future. (in {cltv_abs-local_height} blocks)")
×
1683
        return onion, amount_msat, cltv_abs, session_key
5✔
1684

1685
    def send_htlc(
5✔
1686
        self,
1687
        *,
1688
        chan: Channel,
1689
        payment_hash: bytes,
1690
        amount_msat: int,
1691
        cltv_abs: int,
1692
        onion: OnionPacket,
1693
        session_key: Optional[bytes] = None,
1694
    ) -> UpdateAddHtlc:
1695
        htlc = UpdateAddHtlc(amount_msat=amount_msat, payment_hash=payment_hash, cltv_abs=cltv_abs, timestamp=int(time.time()))
5✔
1696
        htlc = chan.add_htlc(htlc)
5✔
1697
        if session_key:
5✔
1698
            chan.set_onion_key(htlc.htlc_id, session_key) # should it be the outer onion secret?
5✔
1699
        self.logger.info(f"starting payment. htlc: {htlc}")
5✔
1700
        self.send_message(
5✔
1701
            "update_add_htlc",
1702
            channel_id=chan.channel_id,
1703
            id=htlc.htlc_id,
1704
            cltv_expiry=htlc.cltv_abs,
1705
            amount_msat=htlc.amount_msat,
1706
            payment_hash=htlc.payment_hash,
1707
            onion_routing_packet=onion.to_bytes())
1708
        self.maybe_send_commitment(chan)
5✔
1709
        return htlc
5✔
1710

1711
    def pay(self, *,
5✔
1712
            route: 'LNPaymentRoute',
1713
            chan: Channel,
1714
            amount_msat: int,
1715
            total_msat: int,
1716
            payment_hash: bytes,
1717
            min_final_cltv_delta: int,
1718
            payment_secret: bytes,
1719
            trampoline_onion: Optional[OnionPacket] = None,
1720
        ) -> UpdateAddHtlc:
1721

1722
        assert amount_msat > 0, "amount_msat is not greater zero"
5✔
1723
        assert len(route) > 0
5✔
1724
        if not chan.can_send_update_add_htlc():
5✔
1725
            raise PaymentFailure("Channel cannot send update_add_htlc")
5✔
1726
        onion, amount_msat, cltv_abs, session_key = self.create_onion_for_route(
5✔
1727
            route=route,
1728
            amount_msat=amount_msat,
1729
            total_msat=total_msat,
1730
            payment_hash=payment_hash,
1731
            min_final_cltv_delta=min_final_cltv_delta,
1732
            payment_secret=payment_secret,
1733
            trampoline_onion=trampoline_onion
1734
        )
1735
        htlc = self.send_htlc(
5✔
1736
            chan=chan,
1737
            payment_hash=payment_hash,
1738
            amount_msat=amount_msat,
1739
            cltv_abs=cltv_abs,
1740
            onion=onion,
1741
            session_key=session_key,
1742
        )
1743
        return htlc
5✔
1744

1745
    def send_revoke_and_ack(self, chan: Channel):
5✔
1746
        if chan.is_closed():
5✔
1747
            return
×
1748
        self.logger.info(f'send_revoke_and_ack. chan {chan.short_channel_id}. ctn: {chan.get_oldest_unrevoked_ctn(LOCAL)}')
5✔
1749
        rev = chan.revoke_current_commitment()
5✔
1750
        self.lnworker.save_channel(chan)
5✔
1751
        self.send_message("revoke_and_ack",
5✔
1752
            channel_id=chan.channel_id,
1753
            per_commitment_secret=rev.per_commitment_secret,
1754
            next_per_commitment_point=rev.next_per_commitment_point)
1755
        self.maybe_send_commitment(chan)
5✔
1756

1757
    def on_commitment_signed(self, chan: Channel, payload):
5✔
1758
        if chan.peer_state == PeerState.BAD:
5✔
1759
            return
×
1760
        self.logger.info(f'on_commitment_signed. chan {chan.short_channel_id}. ctn: {chan.get_next_ctn(LOCAL)}.')
5✔
1761
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
1762
            raise Exception(f"received commitment_signed in unexpected {chan.peer_state=!r}")
×
1763
        # make sure there were changes to the ctx, otherwise the remote peer is misbehaving
1764
        if not chan.has_pending_changes(LOCAL):
5✔
1765
            # TODO if feerate changed A->B->A; so there were updates but the value is identical,
1766
            #      then it might be legal to send a commitment_signature
1767
            #      see https://github.com/lightningnetwork/lightning-rfc/pull/618
1768
            raise RemoteMisbehaving('received commitment_signed without pending changes')
×
1769
        # REMOTE should wait until we have revoked
1770
        if chan.hm.is_revack_pending(LOCAL):
5✔
1771
            raise RemoteMisbehaving('received commitment_signed before we revoked previous ctx')
×
1772
        data = payload["htlc_signature"]
5✔
1773
        htlc_sigs = list(chunks(data, 64))
5✔
1774
        chan.receive_new_commitment(payload["signature"], htlc_sigs)
5✔
1775
        self.send_revoke_and_ack(chan)
5✔
1776
        self.received_commitsig_event.set()
5✔
1777
        self.received_commitsig_event.clear()
5✔
1778

1779
    def on_update_fulfill_htlc(self, chan: Channel, payload):
5✔
1780
        preimage = payload["payment_preimage"]
5✔
1781
        payment_hash = sha256(preimage)
5✔
1782
        htlc_id = payload["id"]
5✔
1783
        self.logger.info(f"on_update_fulfill_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}")
5✔
1784
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
1785
            raise Exception(f"received update_fulfill_htlc in unexpected {chan.peer_state=!r}")
×
1786
        chan.receive_htlc_settle(preimage, htlc_id)  # TODO handle exc and maybe fail channel (e.g. bad htlc_id)
5✔
1787
        self.lnworker.save_preimage(payment_hash, preimage)
5✔
1788
        self.maybe_send_commitment(chan)
5✔
1789

1790
    def on_update_fail_malformed_htlc(self, chan: Channel, payload):
5✔
1791
        htlc_id = payload["id"]
×
1792
        failure_code = payload["failure_code"]
×
1793
        self.logger.info(f"on_update_fail_malformed_htlc. chan {chan.get_id_for_log()}. "
×
1794
                         f"htlc_id {htlc_id}. failure_code={failure_code}")
1795
        if chan.peer_state != PeerState.GOOD:  # should never happen
×
1796
            raise Exception(f"received update_fail_malformed_htlc in unexpected {chan.peer_state=!r}")
×
1797
        if failure_code & OnionFailureCodeMetaFlag.BADONION == 0:
×
1798
            self.schedule_force_closing(chan.channel_id)
×
1799
            raise RemoteMisbehaving(f"received update_fail_malformed_htlc with unexpected failure code: {failure_code}")
×
1800
        reason = OnionRoutingFailure(code=failure_code, data=payload["sha256_of_onion"])
×
1801
        chan.receive_fail_htlc(htlc_id, error_bytes=None, reason=reason)
×
1802
        self.maybe_send_commitment(chan)
×
1803

1804
    def on_update_add_htlc(self, chan: Channel, payload):
5✔
1805
        payment_hash = payload["payment_hash"]
5✔
1806
        htlc_id = payload["id"]
5✔
1807
        cltv_abs = payload["cltv_expiry"]
5✔
1808
        amount_msat_htlc = payload["amount_msat"]
5✔
1809
        onion_packet = payload["onion_routing_packet"]
5✔
1810
        htlc = UpdateAddHtlc(
5✔
1811
            amount_msat=amount_msat_htlc,
1812
            payment_hash=payment_hash,
1813
            cltv_abs=cltv_abs,
1814
            timestamp=int(time.time()),
1815
            htlc_id=htlc_id)
1816
        self.logger.info(f"on_update_add_htlc. chan {chan.short_channel_id}. htlc={str(htlc)}")
5✔
1817
        if chan.get_state() != ChannelState.OPEN:
5✔
1818
            raise RemoteMisbehaving(f"received update_add_htlc while chan.get_state() != OPEN. state was {chan.get_state()!r}")
×
1819
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
1820
            raise Exception(f"received update_add_htlc in unexpected {chan.peer_state=!r}")
×
1821
        if cltv_abs > bitcoin.NLOCKTIME_BLOCKHEIGHT_MAX:
5✔
1822
            self.schedule_force_closing(chan.channel_id)
×
1823
            raise RemoteMisbehaving(f"received update_add_htlc with {cltv_abs=} > BLOCKHEIGHT_MAX")
×
1824
        # add htlc
1825
        chan.receive_htlc(htlc, onion_packet)
5✔
1826
        util.trigger_callback('htlc_added', chan, htlc, RECEIVED)
5✔
1827

1828

1829
    async def maybe_forward_htlc(
5✔
1830
            self, *,
1831
            incoming_chan: Channel,
1832
            htlc: UpdateAddHtlc,
1833
            processed_onion: ProcessedOnionPacket,
1834
    ) -> str:
1835

1836
        # Forward HTLC
1837
        # FIXME: there are critical safety checks MISSING here
1838
        #        - for example; atm we forward first and then persist "forwarding_info",
1839
        #          so if we segfault in-between and restart, we might forward an HTLC twice...
1840
        #          (same for trampoline forwarding)
1841
        #        - we could check for the exposure to dust HTLCs, see:
1842
        #          https://github.com/ACINQ/eclair/pull/1985
1843

1844
        def log_fail_reason(reason: str):
5✔
1845
            self.logger.debug(
5✔
1846
                f"maybe_forward_htlc. will FAIL HTLC: inc_chan={incoming_chan.get_id_for_log()}. "
1847
                f"{reason}. inc_htlc={str(htlc)}. onion_payload={processed_onion.hop_data.payload}")
1848

1849
        forwarding_enabled = self.network.config.EXPERIMENTAL_LN_FORWARD_PAYMENTS
5✔
1850
        if not forwarding_enabled:
5✔
1851
            log_fail_reason("forwarding is disabled")
×
1852
            raise OnionRoutingFailure(code=OnionFailureCode.PERMANENT_CHANNEL_FAILURE, data=b'')
×
1853
        chain = self.network.blockchain()
5✔
1854
        if chain.is_tip_stale():
5✔
1855
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
×
1856
        try:
5✔
1857
            _next_chan_scid = processed_onion.hop_data.payload["short_channel_id"]["short_channel_id"]  # type: bytes
5✔
1858
            next_chan_scid = ShortChannelID(_next_chan_scid)
5✔
1859
        except Exception:
×
1860
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
×
1861
        try:
5✔
1862
            next_amount_msat_htlc = processed_onion.hop_data.payload["amt_to_forward"]["amt_to_forward"]
5✔
1863
        except Exception:
×
1864
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
×
1865
        try:
5✔
1866
            next_cltv_abs = processed_onion.hop_data.payload["outgoing_cltv_value"]["outgoing_cltv_value"]
5✔
1867
        except Exception:
×
1868
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
×
1869

1870
        next_chan = self.lnworker.get_channel_by_short_id(next_chan_scid)
5✔
1871

1872
        if self.lnworker.features.supports(LnFeatures.OPTION_ZEROCONF_OPT):
5✔
NEW
1873
            next_peer = self.lnworker.get_peer_by_static_jit_scid_alias(next_chan_scid)
×
1874
        else:
1875
            next_peer = None
5✔
1876

1877
        if not next_chan and next_peer and next_peer.accepts_zeroconf():
5✔
1878
            # check if an already existing channel can be used.
1879
            # todo: split the payment
1880
            for next_chan in next_peer.channels.values():
×
1881
                if next_chan.can_pay(next_amount_msat_htlc):
×
1882
                    break
×
1883
            else:
1884
                return await self.lnworker.open_channel_just_in_time(
×
1885
                    next_peer=next_peer,
1886
                    next_amount_msat_htlc=next_amount_msat_htlc,
1887
                    next_cltv_abs=next_cltv_abs,
1888
                    payment_hash=htlc.payment_hash,
1889
                    next_onion=processed_onion.next_packet)
1890

1891
        local_height = chain.height()
5✔
1892
        if next_chan is None:
5✔
1893
            log_fail_reason(f"cannot find next_chan {next_chan_scid}")
×
1894
            raise OnionRoutingFailure(code=OnionFailureCode.UNKNOWN_NEXT_PEER, data=b'')
×
1895
        outgoing_chan_upd = next_chan.get_outgoing_gossip_channel_update(scid=next_chan_scid)[2:]
5✔
1896
        outgoing_chan_upd_len = len(outgoing_chan_upd).to_bytes(2, byteorder="big")
5✔
1897
        outgoing_chan_upd_message = outgoing_chan_upd_len + outgoing_chan_upd
5✔
1898
        if not next_chan.can_send_update_add_htlc():
5✔
1899
            log_fail_reason(
×
1900
                f"next_chan {next_chan.get_id_for_log()} cannot send ctx updates. "
1901
                f"chan state {next_chan.get_state()!r}, peer state: {next_chan.peer_state!r}")
1902
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_CHANNEL_FAILURE, data=outgoing_chan_upd_message)
×
1903
        if not next_chan.can_pay(next_amount_msat_htlc):
5✔
1904
            log_fail_reason(f"transient error (likely due to insufficient funds): not next_chan.can_pay(amt)")
5✔
1905
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_CHANNEL_FAILURE, data=outgoing_chan_upd_message)
5✔
1906
        if htlc.cltv_abs - next_cltv_abs < next_chan.forwarding_cltv_delta:
5✔
1907
            log_fail_reason(
×
1908
                f"INCORRECT_CLTV_EXPIRY. "
1909
                f"{htlc.cltv_abs=} - {next_cltv_abs=} < {next_chan.forwarding_cltv_delta=}")
1910
            data = htlc.cltv_abs.to_bytes(4, byteorder="big") + outgoing_chan_upd_message
×
1911
            raise OnionRoutingFailure(code=OnionFailureCode.INCORRECT_CLTV_EXPIRY, data=data)
×
1912
        if htlc.cltv_abs - lnutil.MIN_FINAL_CLTV_DELTA_ACCEPTED <= local_height \
5✔
1913
                or next_cltv_abs <= local_height:
1914
            raise OnionRoutingFailure(code=OnionFailureCode.EXPIRY_TOO_SOON, data=outgoing_chan_upd_message)
×
1915
        if max(htlc.cltv_abs, next_cltv_abs) > local_height + lnutil.NBLOCK_CLTV_DELTA_TOO_FAR_INTO_FUTURE:
5✔
1916
            raise OnionRoutingFailure(code=OnionFailureCode.EXPIRY_TOO_FAR, data=b'')
×
1917
        forwarding_fees = fee_for_edge_msat(
5✔
1918
            forwarded_amount_msat=next_amount_msat_htlc,
1919
            fee_base_msat=next_chan.forwarding_fee_base_msat,
1920
            fee_proportional_millionths=next_chan.forwarding_fee_proportional_millionths)
1921
        if htlc.amount_msat - next_amount_msat_htlc < forwarding_fees:
5✔
1922
            data = next_amount_msat_htlc.to_bytes(8, byteorder="big") + outgoing_chan_upd_message
×
1923
            raise OnionRoutingFailure(code=OnionFailureCode.FEE_INSUFFICIENT, data=data)
×
1924
        if self._maybe_refuse_to_forward_htlc_that_corresponds_to_payreq_we_created(htlc.payment_hash):
5✔
1925
            log_fail_reason(f"RHASH corresponds to payreq we created")
5✔
1926
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
5✔
1927
        self.logger.info(
5✔
1928
            f"maybe_forward_htlc. will forward HTLC: inc_chan={incoming_chan.short_channel_id}. inc_htlc={str(htlc)}. "
1929
            f"next_chan={next_chan.get_id_for_log()}.")
1930

1931
        next_peer = self.lnworker.peers.get(next_chan.node_id)
5✔
1932
        if next_peer is None:
5✔
1933
            log_fail_reason(f"next_peer offline ({next_chan.node_id.hex()})")
×
1934
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_CHANNEL_FAILURE, data=outgoing_chan_upd_message)
×
1935
        try:
5✔
1936
            next_htlc = next_peer.send_htlc(
5✔
1937
                chan=next_chan,
1938
                payment_hash=htlc.payment_hash,
1939
                amount_msat=next_amount_msat_htlc,
1940
                cltv_abs=next_cltv_abs,
1941
                onion=processed_onion.next_packet,
1942
            )
1943
        except BaseException as e:
×
1944
            log_fail_reason(f"error sending message to next_peer={next_chan.node_id.hex()}")
×
1945
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_CHANNEL_FAILURE, data=outgoing_chan_upd_message)
×
1946
        htlc_key = serialize_htlc_key(next_chan.get_scid_or_local_alias(), next_htlc.htlc_id)
5✔
1947
        return htlc_key
5✔
1948

1949
    @log_exceptions
5✔
1950
    async def maybe_forward_trampoline(
5✔
1951
            self, *,
1952
            payment_hash: bytes,
1953
            inc_cltv_abs: int,
1954
            outer_onion: ProcessedOnionPacket,
1955
            trampoline_onion: ProcessedOnionPacket,
1956
            fw_payment_key: str,
1957
    ) -> None:
1958

1959
        forwarding_enabled = self.network.config.EXPERIMENTAL_LN_FORWARD_PAYMENTS
5✔
1960
        forwarding_trampoline_enabled = self.network.config.EXPERIMENTAL_LN_FORWARD_TRAMPOLINE_PAYMENTS
5✔
1961
        if not (forwarding_enabled and forwarding_trampoline_enabled):
5✔
1962
            self.logger.info(f"trampoline forwarding is disabled. failing htlc.")
×
1963
            raise OnionRoutingFailure(code=OnionFailureCode.PERMANENT_CHANNEL_FAILURE, data=b'')
×
1964
        payload = trampoline_onion.hop_data.payload
5✔
1965
        payment_data = payload.get('payment_data')
5✔
1966
        try:
5✔
1967
            payment_secret = payment_data['payment_secret'] if payment_data else os.urandom(32)
5✔
1968
            outgoing_node_id = payload["outgoing_node_id"]["outgoing_node_id"]
5✔
1969
            amt_to_forward = payload["amt_to_forward"]["amt_to_forward"]
5✔
1970
            out_cltv_abs = payload["outgoing_cltv_value"]["outgoing_cltv_value"]
5✔
1971
            if "invoice_features" in payload:
5✔
1972
                self.logger.info('forward_trampoline: legacy')
5✔
1973
                next_trampoline_onion = None
5✔
1974
                invoice_features = payload["invoice_features"]["invoice_features"]
5✔
1975
                invoice_routing_info = payload["invoice_routing_info"]["invoice_routing_info"]
5✔
1976
                r_tags = decode_routing_info(invoice_routing_info)
5✔
1977
                self.logger.info(f'r_tags {r_tags}')
5✔
1978
                # TODO legacy mpp payment, use total_msat from trampoline onion
1979
            else:
1980
                self.logger.info('forward_trampoline: end-to-end')
5✔
1981
                invoice_features = LnFeatures.BASIC_MPP_OPT
5✔
1982
                next_trampoline_onion = trampoline_onion.next_packet
5✔
1983
                r_tags = []
5✔
1984
        except Exception as e:
×
1985
            self.logger.exception('')
×
1986
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
×
1987

1988
        if self._maybe_refuse_to_forward_htlc_that_corresponds_to_payreq_we_created(payment_hash):
5✔
1989
            self.logger.debug(
5✔
1990
                f"maybe_forward_trampoline. will FAIL HTLC(s). "
1991
                f"RHASH corresponds to payreq we created. {payment_hash.hex()=}")
1992
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
5✔
1993

1994
        # these are the fee/cltv paid by the sender
1995
        # pay_to_node will raise if they are not sufficient
1996
        total_msat = outer_onion.hop_data.payload["payment_data"]["total_msat"]
5✔
1997
        budget = PaymentFeeBudget(
5✔
1998
            fee_msat=total_msat - amt_to_forward,
1999
            cltv=inc_cltv_abs - out_cltv_abs,
2000
        )
2001
        self.logger.info(f'trampoline forwarding. budget={budget}')
5✔
2002
        self.logger.info(f'trampoline forwarding. {inc_cltv_abs=}, {out_cltv_abs=}')
5✔
2003
        # To convert abs vs rel cltvs, we need to guess blockheight used by original sender as "current blockheight".
2004
        # Blocks might have been mined since.
2005
        # - if we skew towards the past, we decrease our own cltv_budget accordingly (which is ok)
2006
        # - if we skew towards the future, we decrease the cltv_budget for the subsequent nodes in the path,
2007
        #   which can result in them failing the payment.
2008
        # So we skew towards the past and guess that there has been 1 new block mined since the payment began:
2009
        local_height_of_onion_creator = self.network.get_local_height() - 1
5✔
2010
        cltv_budget_for_rest_of_route = out_cltv_abs - local_height_of_onion_creator
5✔
2011

2012
        if budget.fee_msat < 1000:
5✔
2013
            raise OnionRoutingFailure(code=OnionFailureCode.TRAMPOLINE_FEE_INSUFFICIENT, data=b'')
5✔
2014
        if budget.cltv < 576:
5✔
2015
            raise OnionRoutingFailure(code=OnionFailureCode.TRAMPOLINE_EXPIRY_TOO_SOON, data=b'')
×
2016

2017
        # do we have a connection to the node?
2018
        next_peer = self.lnworker.peers.get(outgoing_node_id)
5✔
2019
        if next_peer and next_peer.accepts_zeroconf():
5✔
2020
            self.logger.info(f'JIT: found next_peer')
×
2021
            for next_chan in next_peer.channels.values():
×
2022
                if next_chan.can_pay(amt_to_forward):
×
2023
                    # todo: detect if we can do mpp
2024
                    self.logger.info(f'jit: next_chan can pay')
×
2025
                    break
×
2026
            else:
2027
                scid_alias = self.lnworker._scid_alias_of_node(next_peer.pubkey)
×
2028
                route = [RouteEdge(
×
2029
                    start_node=next_peer.pubkey,
2030
                    end_node=outgoing_node_id,
2031
                    short_channel_id=scid_alias,
2032
                    fee_base_msat=0,
2033
                    fee_proportional_millionths=0,
2034
                    cltv_delta=144,
2035
                    node_features=0
2036
                )]
2037
                next_onion, amount_msat, cltv_abs, session_key = self.create_onion_for_route(
×
2038
                    route=route,
2039
                    amount_msat=amt_to_forward,
2040
                    total_msat=amt_to_forward,
2041
                    payment_hash=payment_hash,
2042
                    min_final_cltv_delta=cltv_budget_for_rest_of_route,
2043
                    payment_secret=payment_secret,
2044
                    trampoline_onion=next_trampoline_onion,
2045
                )
2046
                await self.lnworker.open_channel_just_in_time(
×
2047
                    next_peer=next_peer,
2048
                    next_amount_msat_htlc=amt_to_forward,
2049
                    next_cltv_abs=cltv_abs,
2050
                    payment_hash=payment_hash,
2051
                    next_onion=next_onion)
2052
                return
×
2053

2054
        try:
5✔
2055
            await self.lnworker.pay_to_node(
5✔
2056
                node_pubkey=outgoing_node_id,
2057
                payment_hash=payment_hash,
2058
                payment_secret=payment_secret,
2059
                amount_to_pay=amt_to_forward,
2060
                min_final_cltv_delta=cltv_budget_for_rest_of_route,
2061
                r_tags=r_tags,
2062
                invoice_features=invoice_features,
2063
                fwd_trampoline_onion=next_trampoline_onion,
2064
                budget=budget,
2065
                attempts=100,
2066
                fw_payment_key=fw_payment_key,
2067
            )
2068
        except OnionRoutingFailure as e:
5✔
2069
            raise
×
2070
        except FeeBudgetExceeded:
5✔
2071
            raise OnionRoutingFailure(code=OnionFailureCode.TRAMPOLINE_FEE_INSUFFICIENT, data=b'')
×
2072
        except PaymentFailure as e:
5✔
2073
            self.logger.debug(
5✔
2074
                f"maybe_forward_trampoline. PaymentFailure for {payment_hash.hex()=}, {payment_secret.hex()=}: {e!r}")
2075
            raise OnionRoutingFailure(code=OnionFailureCode.UNKNOWN_NEXT_PEER, data=b'')
5✔
2076

2077
    def _maybe_refuse_to_forward_htlc_that_corresponds_to_payreq_we_created(self, payment_hash: bytes) -> bool:
5✔
2078
        """Returns True if the HTLC should be failed.
2079
        We must not forward HTLCs with a matching payment_hash to a payment request we created.
2080
        Example attack:
2081
        - Bob creates payment request with HASH1, for 1 BTC; and gives the payreq to Alice
2082
        - Alice sends htlc A->B->C, for 1 sat, with HASH1
2083
        - Bob must not release the preimage of HASH1
2084
        """
2085
        payment_info = self.lnworker.get_payment_info(payment_hash)
5✔
2086
        is_our_payreq = payment_info and payment_info.direction == RECEIVED
5✔
2087
        # note: If we don't have the preimage for a payment request, then it must be a hold invoice.
2088
        #       Hold invoices are created by other parties (e.g. a counterparty initiating a submarine swap),
2089
        #       and it is the other party choosing the payment_hash. If we failed HTLCs with payment_hashes colliding
2090
        #       with hold invoices, then a party that can make us save a hold invoice for an arbitrary hash could
2091
        #       also make us fail arbitrary HTLCs.
2092
        return bool(is_our_payreq and self.lnworker.get_preimage(payment_hash))
5✔
2093

2094
    def check_accepted_htlc(
5✔
2095
            self, *,
2096
            chan: Channel,
2097
            htlc: UpdateAddHtlc,
2098
            processed_onion: ProcessedOnionPacket,
2099
            log_fail_reason: Callable,
2100
    ):
2101
        """
2102
        Perform checks that are invariant (results do not depend on height, network conditions, etc).
2103
        May raise OnionRoutingFailure
2104
        """
2105
        try:
5✔
2106
            amt_to_forward = processed_onion.hop_data.payload["amt_to_forward"]["amt_to_forward"]
5✔
2107
        except Exception:
×
2108
            log_fail_reason(f"'amt_to_forward' missing from onion")
×
2109
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
×
2110

2111
        exc_incorrect_or_unknown_pd = OnionRoutingFailure(
5✔
2112
            code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS,
2113
            data=amt_to_forward.to_bytes(8, byteorder="big")) # height will be added later
2114
        try:
5✔
2115
            cltv_abs_from_onion = processed_onion.hop_data.payload["outgoing_cltv_value"]["outgoing_cltv_value"]
5✔
2116
        except Exception:
×
2117
            log_fail_reason(f"'outgoing_cltv_value' missing from onion")
×
2118
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_PAYLOAD, data=b'\x00\x00\x00')
×
2119

2120
        if cltv_abs_from_onion > htlc.cltv_abs:
5✔
2121
            log_fail_reason(f"cltv_abs_from_onion != htlc.cltv_abs")
×
2122
            raise OnionRoutingFailure(
×
2123
                code=OnionFailureCode.FINAL_INCORRECT_CLTV_EXPIRY,
2124
                data=htlc.cltv_abs.to_bytes(4, byteorder="big"))
2125
        try:
5✔
2126
            total_msat = processed_onion.hop_data.payload["payment_data"]["total_msat"]
5✔
2127
        except Exception:
×
2128
            log_fail_reason(f"'total_msat' missing from onion")
×
2129
            raise exc_incorrect_or_unknown_pd
×
2130

2131
        if chan.opening_fee:
5✔
2132
            channel_opening_fee = chan.opening_fee['channel_opening_fee']
×
2133
            total_msat -= channel_opening_fee
×
2134
            amt_to_forward -= channel_opening_fee
×
2135
        else:
2136
            channel_opening_fee = 0
5✔
2137

2138
        if amt_to_forward > htlc.amount_msat:
5✔
2139
            log_fail_reason(f"amt_to_forward != htlc.amount_msat")
×
2140
            raise OnionRoutingFailure(
×
2141
                code=OnionFailureCode.FINAL_INCORRECT_HTLC_AMOUNT,
2142
                data=htlc.amount_msat.to_bytes(8, byteorder="big"))
2143

2144
        try:
5✔
2145
            payment_secret_from_onion = processed_onion.hop_data.payload["payment_data"]["payment_secret"]  # type: bytes
5✔
2146
        except Exception:
×
2147
            log_fail_reason(f"'payment_secret' missing from onion")
×
2148
            raise exc_incorrect_or_unknown_pd
×
2149

2150
        return payment_secret_from_onion, total_msat, channel_opening_fee, exc_incorrect_or_unknown_pd
5✔
2151

2152
    def check_mpp_is_waiting(self, *, payment_secret, short_channel_id, htlc, expected_msat, exc_incorrect_or_unknown_pd, log_fail_reason) -> bool:
5✔
2153
        from .lnworker import RecvMPPResolution
5✔
2154
        mpp_resolution = self.lnworker.check_mpp_status(
5✔
2155
            payment_secret=payment_secret,
2156
            short_channel_id=short_channel_id,
2157
            htlc=htlc,
2158
            expected_msat=expected_msat,
2159
        )
2160
        if mpp_resolution == RecvMPPResolution.WAITING:
5✔
2161
            return True
5✔
2162
        elif mpp_resolution == RecvMPPResolution.EXPIRED:
5✔
2163
            log_fail_reason(f"MPP_TIMEOUT")
5✔
2164
            raise OnionRoutingFailure(code=OnionFailureCode.MPP_TIMEOUT, data=b'')
5✔
2165
        elif mpp_resolution == RecvMPPResolution.FAILED:
5✔
2166
            log_fail_reason(f"mpp_resolution is FAILED")
5✔
2167
            raise exc_incorrect_or_unknown_pd
5✔
2168
        elif mpp_resolution == RecvMPPResolution.ACCEPTED:
5✔
2169
            return False
5✔
2170
        else:
2171
            raise Exception(f"unexpected {mpp_resolution=}")
×
2172

2173
    def maybe_fulfill_htlc(
5✔
2174
            self, *,
2175
            chan: Channel,
2176
            htlc: UpdateAddHtlc,
2177
            processed_onion: ProcessedOnionPacket,
2178
            onion_packet_bytes: bytes,
2179
            already_forwarded: bool = False,
2180
    ) -> Tuple[Optional[bytes], Optional[Tuple[str, Callable[[], Awaitable[Optional[str]]]]]]:
2181
        """
2182
        Decide what to do with an HTLC: return preimage if it can be fulfilled, forwarding callback if it can be forwarded.
2183
        Return (preimage, (payment_key, callback)) with at most a single element not None.
2184
        """
2185
        if not processed_onion.are_we_final:
5✔
2186
            if not self.lnworker.enable_htlc_forwarding:
5✔
2187
                return None, None
5✔
2188
            # use the htlc key if we are forwarding
2189
            payment_key = serialize_htlc_key(chan.get_scid_or_local_alias(), htlc.htlc_id)
5✔
2190
            callback = lambda: self.maybe_forward_htlc(
5✔
2191
                incoming_chan=chan,
2192
                htlc=htlc,
2193
                processed_onion=processed_onion)
2194
            return None, (payment_key, callback)
5✔
2195

2196
        def log_fail_reason(reason: str):
5✔
2197
            self.logger.info(
5✔
2198
                f"maybe_fulfill_htlc. will FAIL HTLC: chan {chan.short_channel_id}. "
2199
                f"{reason}. htlc={str(htlc)}. onion_payload={processed_onion.hop_data.payload}")
2200

2201
        chain = self.network.blockchain()
5✔
2202
        # Check that our blockchain tip is sufficiently recent so that we have an approx idea of the height.
2203
        # We should not release the preimage for an HTLC that its sender could already time out as
2204
        # then they might try to force-close and it becomes a race.
2205
        if chain.is_tip_stale() and not already_forwarded:
5✔
2206
            log_fail_reason(f"our chain tip is stale")
×
2207
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
×
2208
        local_height = chain.height()
5✔
2209

2210
        # parse parameters and perform checks that are invariant
2211
        payment_secret_from_onion, total_msat, channel_opening_fee, exc_incorrect_or_unknown_pd = self.check_accepted_htlc(
5✔
2212
            chan=chan,
2213
            htlc=htlc,
2214
            processed_onion=processed_onion,
2215
            log_fail_reason=log_fail_reason)
2216

2217
        # payment key for final onions
2218
        payment_hash = htlc.payment_hash
5✔
2219
        payment_key = (payment_hash + payment_secret_from_onion).hex()
5✔
2220

2221
        if self.check_mpp_is_waiting(
5✔
2222
                payment_secret=payment_secret_from_onion,
2223
                short_channel_id=chan.get_scid_or_local_alias(),
2224
                htlc=htlc,
2225
                expected_msat=total_msat,
2226
                exc_incorrect_or_unknown_pd=exc_incorrect_or_unknown_pd,
2227
                log_fail_reason=log_fail_reason,
2228
        ):
2229
            return None, None
5✔
2230

2231
        # TODO check against actual min_final_cltv_expiry_delta from invoice (and give 2-3 blocks of leeway?)
2232
        if local_height + MIN_FINAL_CLTV_DELTA_ACCEPTED > htlc.cltv_abs and not already_forwarded:
5✔
2233
            log_fail_reason(f"htlc.cltv_abs is unreasonably close")
×
2234
            raise exc_incorrect_or_unknown_pd
×
2235

2236
        # detect callback
2237
        # if there is a trampoline_onion, maybe_fulfill_htlc will be called again
2238
        # order is important: if we receive a trampoline onion for a hold invoice, we need to peel the onion first.
2239

2240
        if processed_onion.trampoline_onion_packet:
5✔
2241
            # TODO: we should check that all trampoline_onions are the same
2242
            trampoline_onion = self.process_onion_packet(
5✔
2243
                processed_onion.trampoline_onion_packet,
2244
                payment_hash=payment_hash,
2245
                onion_packet_bytes=onion_packet_bytes,
2246
                is_trampoline=True)
2247
            if trampoline_onion.are_we_final:
5✔
2248
                # trampoline- we are final recipient of HTLC
2249
                # note: the returned payment_key will contain the inner payment_secret
2250
                return self.maybe_fulfill_htlc(
5✔
2251
                    chan=chan,
2252
                    htlc=htlc,
2253
                    processed_onion=trampoline_onion,
2254
                    onion_packet_bytes=onion_packet_bytes,
2255
                    already_forwarded=already_forwarded,
2256
                )
2257
            else:
2258
                callback = lambda: self.maybe_forward_trampoline(
5✔
2259
                    payment_hash=payment_hash,
2260
                    inc_cltv_abs=htlc.cltv_abs, # TODO: use max or enforce same value across mpp parts
2261
                    outer_onion=processed_onion,
2262
                    trampoline_onion=trampoline_onion,
2263
                    fw_payment_key=payment_key)
2264
                return None, (payment_key, callback)
5✔
2265

2266
        # TODO don't accept payments twice for same invoice
2267
        # TODO check invoice expiry
2268
        info = self.lnworker.get_payment_info(payment_hash)
5✔
2269
        if info is None:
5✔
2270
            log_fail_reason(f"no payment_info found for RHASH {htlc.payment_hash.hex()}")
×
2271
            raise exc_incorrect_or_unknown_pd
×
2272

2273
        preimage = self.lnworker.get_preimage(payment_hash)
5✔
2274
        expected_payment_secrets = [self.lnworker.get_payment_secret(htlc.payment_hash)]
5✔
2275
        if preimage:
5✔
2276
            expected_payment_secrets.append(derive_payment_secret_from_payment_preimage(preimage)) # legacy secret for old invoices
5✔
2277
        if payment_secret_from_onion not in expected_payment_secrets:
5✔
2278
            log_fail_reason(f'incorrect payment secret {payment_secret_from_onion.hex()} != {expected_payment_secrets[0].hex()}')
×
2279
            raise exc_incorrect_or_unknown_pd
×
2280
        invoice_msat = info.amount_msat
5✔
2281
        if channel_opening_fee:
5✔
2282
            invoice_msat -= channel_opening_fee
×
2283

2284
        if not (invoice_msat is None or invoice_msat <= total_msat <= 2 * invoice_msat):
5✔
2285
            log_fail_reason(f"total_msat={total_msat} too different from invoice_msat={invoice_msat}")
×
2286
            raise exc_incorrect_or_unknown_pd
×
2287

2288
        hold_invoice_callback = self.lnworker.hold_invoice_callbacks.get(payment_hash)
5✔
2289
        if hold_invoice_callback and not preimage:
5✔
2290
            callback = lambda: hold_invoice_callback(payment_hash)
5✔
2291
            return None, (payment_key, callback)
5✔
2292

2293
        if not preimage:
5✔
2294
            if not already_forwarded:
5✔
2295
                log_fail_reason(f"missing preimage and no hold invoice callback {payment_hash.hex()}")
5✔
2296
                raise exc_incorrect_or_unknown_pd
5✔
2297
            else:
2298
                return None, None
×
2299

2300
        chan.opening_fee = None
5✔
2301
        self.logger.info(f"maybe_fulfill_htlc. will FULFILL HTLC: chan {chan.short_channel_id}. htlc={str(htlc)}")
5✔
2302
        return preimage, None
5✔
2303

2304
    def fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
5✔
2305
        self.logger.info(f"_fulfill_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}")
5✔
2306
        assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
5✔
2307
        assert chan.hm.is_htlc_irrevocably_added_yet(htlc_proposer=REMOTE, htlc_id=htlc_id)
5✔
2308
        self.received_htlcs_pending_removal.add((chan, htlc_id))
5✔
2309
        chan.settle_htlc(preimage, htlc_id)
5✔
2310
        self.send_message(
5✔
2311
            "update_fulfill_htlc",
2312
            channel_id=chan.channel_id,
2313
            id=htlc_id,
2314
            payment_preimage=preimage)
2315

2316
    def fail_htlc(self, *, chan: Channel, htlc_id: int, error_bytes: bytes):
5✔
2317
        self.logger.info(f"fail_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}.")
5✔
2318
        assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
5✔
2319
        self.received_htlcs_pending_removal.add((chan, htlc_id))
5✔
2320
        chan.fail_htlc(htlc_id)
5✔
2321
        self.send_message(
5✔
2322
            "update_fail_htlc",
2323
            channel_id=chan.channel_id,
2324
            id=htlc_id,
2325
            len=len(error_bytes),
2326
            reason=error_bytes)
2327

2328
    def fail_malformed_htlc(self, *, chan: Channel, htlc_id: int, reason: OnionRoutingFailure):
5✔
2329
        self.logger.info(f"fail_malformed_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}.")
×
2330
        assert chan.can_send_ctx_updates(), f"cannot send updates: {chan.short_channel_id}"
×
2331
        if not (reason.code & OnionFailureCodeMetaFlag.BADONION and len(reason.data) == 32):
×
2332
            raise Exception(f"unexpected reason when sending 'update_fail_malformed_htlc': {reason!r}")
×
2333
        self.received_htlcs_pending_removal.add((chan, htlc_id))
×
2334
        chan.fail_htlc(htlc_id)
×
2335
        self.send_message(
×
2336
            "update_fail_malformed_htlc",
2337
            channel_id=chan.channel_id,
2338
            id=htlc_id,
2339
            sha256_of_onion=reason.data,
2340
            failure_code=reason.code)
2341

2342
    def on_revoke_and_ack(self, chan: Channel, payload):
5✔
2343
        if chan.peer_state == PeerState.BAD:
5✔
2344
            return
×
2345
        self.logger.info(f'on_revoke_and_ack. chan {chan.short_channel_id}. ctn: {chan.get_oldest_unrevoked_ctn(REMOTE)}')
5✔
2346
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
2347
            raise Exception(f"received revoke_and_ack in unexpected {chan.peer_state=!r}")
×
2348
        rev = RevokeAndAck(payload["per_commitment_secret"], payload["next_per_commitment_point"])
5✔
2349
        chan.receive_revocation(rev)
5✔
2350
        self.lnworker.save_channel(chan)
5✔
2351
        self.maybe_send_commitment(chan)
5✔
2352
        self._received_revack_event.set()
5✔
2353
        self._received_revack_event.clear()
5✔
2354

2355
    @event_listener
5✔
2356
    async def on_event_fee(self, *args):
5✔
2357
        async def async_wrapper():
×
2358
            for chan in self.channels.values():
×
2359
                self.maybe_update_fee(chan)
×
2360
        await self.taskgroup.spawn(async_wrapper)
×
2361

2362
    def on_update_fee(self, chan: Channel, payload):
5✔
2363
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
2364
            raise Exception(f"received update_fee in unexpected {chan.peer_state=!r}")
×
2365
        feerate = payload["feerate_per_kw"]
5✔
2366
        chan.update_fee(feerate, False)
5✔
2367

2368
    def maybe_update_fee(self, chan: Channel):
5✔
2369
        """
2370
        called when our fee estimates change
2371
        """
2372
        if not chan.can_send_ctx_updates():
5✔
2373
            return
×
2374
        if chan.get_state() != ChannelState.OPEN:
5✔
2375
            return
×
2376
        feerate_per_kw = self.lnworker.current_target_feerate_per_kw()
5✔
2377
        def does_chan_fee_need_update(chan_feerate: Union[float, int]) -> bool:
5✔
2378
            # We raise fees more aggressively than we lower them. Overpaying is not too bad,
2379
            # but lowballing can be fatal if we can't even get into the mempool...
2380
            high_fee = 2 * feerate_per_kw  # type: Union[float, int]
5✔
2381
            low_fee = self.lnworker.current_low_feerate_per_kw()  # type: Union[float, int]
5✔
2382
            low_fee = max(low_fee, 0.75 * feerate_per_kw)
5✔
2383
            # make sure low_feerate and target_feerate are not too close to each other:
2384
            low_fee = min(low_fee, feerate_per_kw - FEERATE_PER_KW_MIN_RELAY_LIGHTNING)
5✔
2385
            assert low_fee < high_fee, (low_fee, high_fee)
5✔
2386
            return not (low_fee < chan_feerate < high_fee)
5✔
2387
        if not chan.constraints.is_initiator:
5✔
2388
            if constants.net is not constants.BitcoinRegtest:
5✔
2389
                chan_feerate = chan.get_latest_feerate(LOCAL)
5✔
2390
                ratio = chan_feerate / feerate_per_kw
5✔
2391
                if ratio < 0.5:
5✔
2392
                    # Note that we trust the Electrum server about fee rates
2393
                    # Thus, automated force-closing might not be a good idea
2394
                    # Maybe we should display something in the GUI instead
2395
                    self.logger.warning(
5✔
2396
                        f"({chan.get_id_for_log()}) feerate is {chan_feerate} sat/kw, "
2397
                        f"current recommended feerate is {feerate_per_kw} sat/kw, consider force closing!")
2398
            return
5✔
2399
        # it is our responsibility to update the fee
2400
        chan_fee = chan.get_next_feerate(REMOTE)
5✔
2401
        if does_chan_fee_need_update(chan_fee):
5✔
2402
            self.logger.info(f"({chan.get_id_for_log()}) onchain fees have changed considerably. updating fee.")
5✔
2403
        elif chan.get_latest_ctn(REMOTE) == 0:
×
2404
            # workaround eclair issue https://github.com/ACINQ/eclair/issues/1730
2405
            self.logger.info(f"({chan.get_id_for_log()}) updating fee to bump remote ctn")
×
2406
            if feerate_per_kw == chan_fee:
×
2407
                feerate_per_kw += 1
×
2408
        else:
2409
            return
×
2410
        self.logger.info(f"(chan: {chan.get_id_for_log()}) current pending feerate {chan_fee}. "
5✔
2411
                         f"new feerate {feerate_per_kw}")
2412
        chan.update_fee(feerate_per_kw, True)
5✔
2413
        self.send_message(
5✔
2414
            "update_fee",
2415
            channel_id=chan.channel_id,
2416
            feerate_per_kw=feerate_per_kw)
2417
        self.maybe_send_commitment(chan)
5✔
2418

2419
    @log_exceptions
5✔
2420
    async def close_channel(self, chan_id: bytes):
5✔
2421
        chan = self.channels[chan_id]
5✔
2422
        self.shutdown_received[chan_id] = self.asyncio_loop.create_future()
5✔
2423
        await self.send_shutdown(chan)
5✔
2424
        payload = await self.shutdown_received[chan_id]
5✔
2425
        try:
5✔
2426
            txid = await self._shutdown(chan, payload, is_local=True)
5✔
2427
            self.logger.info(f'({chan.get_id_for_log()}) Channel closed {txid}')
5✔
2428
        except asyncio.TimeoutError:
×
2429
            txid = chan.unconfirmed_closing_txid
×
2430
            self.logger.info(f'({chan.get_id_for_log()}) did not send closing_signed, {txid}')
×
2431
            if txid is None:
×
2432
                raise Exception('The remote peer did not send their final signature. The channel may not have been be closed')
×
2433
        return txid
5✔
2434

2435
    @non_blocking_msg_handler
5✔
2436
    async def on_shutdown(self, chan: Channel, payload):
5✔
2437
        # TODO: A receiving node: if it hasn't received a funding_signed (if it is a
2438
        #  funder) or a funding_created (if it is a fundee):
2439
        #  SHOULD send an error and fail the channel.
2440
        if chan.peer_state != PeerState.GOOD:  # should never happen
5✔
2441
            raise Exception(f"received shutdown in unexpected {chan.peer_state=!r}")
×
2442
        their_scriptpubkey = payload['scriptpubkey']
5✔
2443
        their_upfront_scriptpubkey = chan.config[REMOTE].upfront_shutdown_script
5✔
2444
        # BOLT-02 check if they use the upfront shutdown script they advertised
2445
        if self.is_upfront_shutdown_script() and their_upfront_scriptpubkey:
5✔
2446
            if not (their_scriptpubkey == their_upfront_scriptpubkey):
5✔
2447
                await self.send_warning(
5✔
2448
                    chan.channel_id,
2449
                    "remote didn't use upfront shutdown script it committed to in channel opening",
2450
                    close_connection=True)
2451
        else:
2452
            # BOLT-02 restrict the scriptpubkey to some templates:
2453
            if self.is_shutdown_anysegwit() and match_script_against_template(their_scriptpubkey, transaction.SCRIPTPUBKEY_TEMPLATE_ANYSEGWIT):
5✔
2454
                pass
×
2455
            elif match_script_against_template(their_scriptpubkey, transaction.SCRIPTPUBKEY_TEMPLATE_WITNESS_V0):
5✔
2456
                pass
5✔
2457
            else:
2458
                await self.send_warning(
×
2459
                    chan.channel_id,
2460
                    f'scriptpubkey in received shutdown message does not conform to any template: {their_scriptpubkey.hex()}',
2461
                    close_connection=True)
2462

2463
        chan_id = chan.channel_id
5✔
2464
        if chan_id in self.shutdown_received:
5✔
2465
            self.shutdown_received[chan_id].set_result(payload)
5✔
2466
        else:
2467
            chan = self.channels[chan_id]
5✔
2468
            await self.send_shutdown(chan)
5✔
2469
            txid = await self._shutdown(chan, payload, is_local=False)
5✔
2470
            self.logger.info(f'({chan.get_id_for_log()}) Channel closed by remote peer {txid}')
×
2471

2472
    def can_send_shutdown(self, chan: Channel):
5✔
2473
        if chan.get_state() >= ChannelState.OPENING:
5✔
2474
            return True
5✔
2475
        if chan.constraints.is_initiator and chan.channel_id in self.funding_created_sent:
×
2476
            return True
×
2477
        if not chan.constraints.is_initiator and chan.channel_id in self.funding_signed_sent:
×
2478
            return True
×
2479
        return False
×
2480

2481
    async def send_shutdown(self, chan: Channel):
5✔
2482
        if not self.can_send_shutdown(chan):
5✔
2483
            raise Exception('cannot send shutdown')
×
2484
        if chan.config[LOCAL].upfront_shutdown_script:
5✔
2485
            scriptpubkey = chan.config[LOCAL].upfront_shutdown_script
5✔
2486
        else:
2487
            scriptpubkey = bitcoin.address_to_script(chan.get_sweep_address())
5✔
2488
        assert scriptpubkey
5✔
2489
        # wait until no more pending updates (bolt2)
2490
        chan.set_can_send_ctx_updates(False)
5✔
2491
        while chan.has_pending_changes(REMOTE):
5✔
2492
            await asyncio.sleep(0.1)
×
2493
        self.send_message('shutdown', channel_id=chan.channel_id, len=len(scriptpubkey), scriptpubkey=scriptpubkey)
5✔
2494
        chan.set_state(ChannelState.SHUTDOWN)
5✔
2495
        # can fulfill or fail htlcs. cannot add htlcs, because state != OPEN
2496
        chan.set_can_send_ctx_updates(True)
5✔
2497

2498
    def get_shutdown_fee_range(self, chan, closing_tx, is_local):
5✔
2499
        """ return the closing fee and fee range we initially try to enforce """
2500
        config = self.network.config
5✔
2501
        our_fee = None
5✔
2502
        if config.TEST_SHUTDOWN_FEE:
5✔
2503
            our_fee = config.TEST_SHUTDOWN_FEE
5✔
2504
        else:
2505
            fee_rate_per_kb = config.eta_target_to_fee(FEE_LN_ETA_TARGET)
5✔
2506
            if fee_rate_per_kb is None:  # fallback
5✔
2507
                fee_rate_per_kb = self.network.config.fee_per_kb()
5✔
2508
            if fee_rate_per_kb is not None:
5✔
2509
                our_fee = fee_rate_per_kb * closing_tx.estimated_size() // 1000
5✔
2510
            # TODO: anchors: remove this, as commitment fee rate can be below chain head fee rate?
2511
            # BOLT2: The sending node MUST set fee less than or equal to the base fee of the final ctx
2512
            max_fee = chan.get_latest_fee(LOCAL if is_local else REMOTE)
5✔
2513
            if our_fee is None:  # fallback
5✔
2514
                self.logger.warning(f"got no fee estimates for co-op close! falling back to chan.get_latest_fee")
×
2515
                our_fee = max_fee
×
2516
            our_fee = min(our_fee, max_fee)
5✔
2517
        # config modern_fee_negotiation can be set in tests
2518
        if config.TEST_SHUTDOWN_LEGACY:
5✔
2519
            our_fee_range = None
5✔
2520
        elif config.TEST_SHUTDOWN_FEE_RANGE:
5✔
2521
            our_fee_range = config.TEST_SHUTDOWN_FEE_RANGE
5✔
2522
        else:
2523
            # we aim at a fee between next block inclusion and some lower value
2524
            our_fee_range = {'min_fee_satoshis': our_fee // 2, 'max_fee_satoshis': our_fee * 2}
5✔
2525
        self.logger.info(f"Our fee range: {our_fee_range} and fee: {our_fee}")
5✔
2526
        return our_fee, our_fee_range
5✔
2527

2528
    @log_exceptions
5✔
2529
    async def _shutdown(self, chan: Channel, payload, *, is_local: bool):
5✔
2530
        # wait until no HTLCs remain in either commitment transaction
2531
        while chan.has_unsettled_htlcs():
5✔
2532
            self.logger.info(f'(chan: {chan.short_channel_id}) waiting for htlcs to settle...')
5✔
2533
            await asyncio.sleep(1)
5✔
2534
        # if no HTLCs remain, we must not send updates
2535
        chan.set_can_send_ctx_updates(False)
5✔
2536
        their_scriptpubkey = payload['scriptpubkey']
5✔
2537
        if chan.config[LOCAL].upfront_shutdown_script:
5✔
2538
            our_scriptpubkey = chan.config[LOCAL].upfront_shutdown_script
5✔
2539
        else:
2540
            our_scriptpubkey = bitcoin.address_to_script(chan.get_sweep_address())
5✔
2541
        assert our_scriptpubkey
5✔
2542
        # estimate fee of closing tx
2543
        dummy_sig, dummy_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=0)
5✔
2544
        our_sig = None  # type: Optional[bytes]
5✔
2545
        closing_tx = None  # type: Optional[PartialTransaction]
5✔
2546
        is_initiator = chan.constraints.is_initiator
5✔
2547
        our_fee, our_fee_range = self.get_shutdown_fee_range(chan, dummy_tx, is_local)
5✔
2548

2549
        def send_closing_signed(our_fee, our_fee_range, drop_remote):
5✔
2550
            nonlocal our_sig, closing_tx
2551
            if our_fee_range:
5✔
2552
                closing_signed_tlvs = {'fee_range': our_fee_range}
5✔
2553
            else:
2554
                closing_signed_tlvs = {}
5✔
2555
            our_sig, closing_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=our_fee, drop_remote=drop_remote)
5✔
2556
            self.logger.info(f"Sending fee range: {closing_signed_tlvs} and fee: {our_fee}")
5✔
2557
            self.send_message(
5✔
2558
                'closing_signed',
2559
                channel_id=chan.channel_id,
2560
                fee_satoshis=our_fee,
2561
                signature=our_sig,
2562
                closing_signed_tlvs=closing_signed_tlvs,
2563
            )
2564

2565
        def verify_signature(tx: 'PartialTransaction', sig) -> bool:
5✔
2566
            their_pubkey = chan.config[REMOTE].multisig_key.pubkey
5✔
2567
            pre_hash = tx.serialize_preimage(0)
5✔
2568
            msg_hash = sha256d(pre_hash)
5✔
2569
            return ECPubkey(their_pubkey).ecdsa_verify(sig, msg_hash)
5✔
2570

2571
        async def receive_closing_signed():
5✔
2572
            nonlocal our_sig, closing_tx
2573
            try:
5✔
2574
                cs_payload = await self.wait_for_message('closing_signed', chan.channel_id)
5✔
2575
            except asyncio.exceptions.TimeoutError:
5✔
2576
                self.schedule_force_closing(chan.channel_id)
×
2577
                raise Exception("closing_signed not received, force closing.")
×
2578
            their_fee = cs_payload['fee_satoshis']
5✔
2579
            their_fee_range = cs_payload['closing_signed_tlvs'].get('fee_range')
5✔
2580
            their_sig = cs_payload['signature']
5✔
2581
            # perform checks
2582
            our_sig, closing_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=their_fee, drop_remote=False)
5✔
2583
            if verify_signature(closing_tx, their_sig):
5✔
2584
                drop_remote = False
5✔
2585
            else:
2586
                our_sig, closing_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=their_fee, drop_remote=True)
×
2587
                if verify_signature(closing_tx, their_sig):
×
2588
                    drop_remote = True
×
2589
                else:
2590
                    # this can happen if we consider our output too valuable to drop,
2591
                    # but the remote drops it because it violates their dust limit
2592
                    raise Exception('failed to verify their signature')
×
2593
            # at this point we know how the closing tx looks like
2594
            # check that their output is above their scriptpubkey's network dust limit
2595
            to_remote_set = closing_tx.get_output_idxs_from_scriptpubkey(their_scriptpubkey)
5✔
2596
            if not drop_remote and to_remote_set:
5✔
2597
                to_remote_idx = to_remote_set.pop()
5✔
2598
                to_remote_amount = closing_tx.outputs()[to_remote_idx].value
5✔
2599
                transaction.check_scriptpubkey_template_and_dust(their_scriptpubkey, to_remote_amount)
5✔
2600
            return their_fee, their_fee_range, their_sig, drop_remote
5✔
2601

2602
        def choose_new_fee(our_fee, our_fee_range, their_fee, their_fee_range, their_previous_fee):
5✔
2603
            assert our_fee != their_fee
5✔
2604
            fee_range_sent = our_fee_range and (is_initiator or (their_previous_fee is not None))
5✔
2605

2606
            # The sending node, if it is not the funder:
2607
            if our_fee_range and their_fee_range and not is_initiator and not self.network.config.TEST_SHUTDOWN_FEE_RANGE:
5✔
2608
                # SHOULD set max_fee_satoshis to at least the max_fee_satoshis received
2609
                our_fee_range['max_fee_satoshis'] = max(their_fee_range['max_fee_satoshis'], our_fee_range['max_fee_satoshis'])
5✔
2610
                # SHOULD set min_fee_satoshis to a fairly low value
2611
                our_fee_range['min_fee_satoshis'] = min(their_fee_range['min_fee_satoshis'], our_fee_range['min_fee_satoshis'])
5✔
2612
                # Note: the BOLT describes what the sending node SHOULD do.
2613
                # However, this assumes that we have decided to send 'funding_signed' in response to their fee_range.
2614
                # In practice, we might prefer to fail the channel in some cases (TODO)
2615

2616
            # the receiving node, if fee_satoshis matches its previously sent fee_range,
2617
            if fee_range_sent and (our_fee_range['min_fee_satoshis'] <= their_fee <= our_fee_range['max_fee_satoshis']):
5✔
2618
                # SHOULD reply with a closing_signed with the same fee_satoshis value if it is different from its previously sent fee_satoshis
2619
                our_fee = their_fee
5✔
2620

2621
            # the receiving node, if the message contains a fee_range
2622
            elif our_fee_range and their_fee_range:
5✔
2623
                overlap_min = max(our_fee_range['min_fee_satoshis'], their_fee_range['min_fee_satoshis'])
5✔
2624
                overlap_max = min(our_fee_range['max_fee_satoshis'], their_fee_range['max_fee_satoshis'])
5✔
2625
                # if there is no overlap between that and its own fee_range
2626
                if overlap_min > overlap_max:
5✔
2627
                    # TODO: the receiving node should first send a warning, and fail the channel
2628
                    # only if it doesn't receive a satisfying fee_range after a reasonable amount of time
2629
                    self.schedule_force_closing(chan.channel_id)
×
2630
                    raise Exception("There is no overlap between between their and our fee range.")
×
2631
                # otherwise, if it is the funder
2632
                if is_initiator:
5✔
2633
                    # if fee_satoshis is not in the overlap between the sent and received fee_range:
2634
                    if not (overlap_min <= their_fee <= overlap_max):
×
2635
                        # MUST fail the channel
2636
                        self.schedule_force_closing(chan.channel_id)
×
2637
                        raise Exception("Their fee is not in the overlap region, we force closed.")
×
2638
                    # otherwise, MUST reply with the same fee_satoshis.
2639
                    our_fee = their_fee
×
2640
                # otherwise (it is not the funder):
2641
                else:
2642
                    # if it has already sent a closing_signed:
2643
                    if fee_range_sent:
5✔
2644
                        # fee_satoshis is not the same as the value we sent, we MUST fail the channel
2645
                        self.schedule_force_closing(chan.channel_id)
×
2646
                        raise Exception("Expected the same fee as ours, we force closed.")
×
2647
                    # otherwise:
2648
                    # MUST propose a fee_satoshis in the overlap between received and (about-to-be) sent fee_range.
2649
                    our_fee = (overlap_min + overlap_max) // 2
5✔
2650
            else:
2651
                # otherwise, if fee_satoshis is not strictly between its last-sent fee_satoshis
2652
                # and its previously-received fee_satoshis, UNLESS it has since reconnected:
2653
                if their_previous_fee and not (min(our_fee, their_previous_fee) < their_fee < max(our_fee, their_previous_fee)):
5✔
2654
                    # SHOULD fail the connection.
2655
                    raise Exception('Their fee is not between our last sent and their last sent fee.')
×
2656
                # accept their fee if they are very close
2657
                if abs(their_fee - our_fee) < 2:
5✔
2658
                    our_fee = their_fee
5✔
2659
                else:
2660
                    # this will be "strictly between" (as in BOLT2) previous values because of the above
2661
                    our_fee = (our_fee + their_fee) // 2
5✔
2662

2663
            return our_fee, our_fee_range
5✔
2664

2665
        # Fee negotiation: both parties exchange 'funding_signed' messages.
2666
        # The funder sends the first message, the non-funder sends the last message.
2667
        # In the 'modern' case, at most 3 messages are exchanged, because choose_new_fee of the funder either returns their_fee or fails
2668
        their_fee = None
5✔
2669
        drop_remote = False  # does the peer drop its to_local output or not?
5✔
2670
        if is_initiator:
5✔
2671
            send_closing_signed(our_fee, our_fee_range, drop_remote)
5✔
2672
        while True:
5✔
2673
            their_previous_fee = their_fee
5✔
2674
            their_fee, their_fee_range, their_sig, drop_remote = await receive_closing_signed()
5✔
2675
            if our_fee == their_fee:
5✔
2676
                break
×
2677
            our_fee, our_fee_range = choose_new_fee(our_fee, our_fee_range, their_fee, their_fee_range, their_previous_fee)
5✔
2678
            if not is_initiator and our_fee == their_fee:
5✔
2679
                break
×
2680
            send_closing_signed(our_fee, our_fee_range, drop_remote)
5✔
2681
            if is_initiator and our_fee == their_fee:
5✔
2682
                break
5✔
2683
        if not is_initiator:
5✔
2684
            send_closing_signed(our_fee, our_fee_range, drop_remote)
×
2685

2686
        # add signatures
2687
        closing_tx.add_signature_to_txin(
5✔
2688
            txin_idx=0,
2689
            signing_pubkey=chan.config[LOCAL].multisig_key.pubkey,
2690
            sig=ecdsa_der_sig_from_ecdsa_sig64(our_sig) + Sighash.to_sigbytes(Sighash.ALL))
2691
        closing_tx.add_signature_to_txin(
5✔
2692
            txin_idx=0,
2693
            signing_pubkey=chan.config[REMOTE].multisig_key.pubkey,
2694
            sig=ecdsa_der_sig_from_ecdsa_sig64(their_sig) + Sighash.to_sigbytes(Sighash.ALL))
2695
        # save local transaction and set state
2696
        try:
5✔
2697
            self.lnworker.wallet.adb.add_transaction(closing_tx)
5✔
2698
        except UnrelatedTransactionException:
×
2699
            pass  # this can happen if (~all the balance goes to REMOTE)
×
2700
        chan.set_state(ChannelState.CLOSING)
5✔
2701
        # broadcast
2702
        await self.network.try_broadcasting(closing_tx, 'closing')
5✔
2703
        return closing_tx.txid()
5✔
2704

2705
    async def htlc_switch(self):
5✔
2706
        # In this loop, an item of chan.unfulfilled_htlcs may go through 4 stages:
2707
        # - 1. not forwarded yet: (None, onion_packet_hex)
2708
        # - 2. forwarded: (forwarding_key, onion_packet_hex)
2709
        # - 3. processed: (forwarding_key, None), not irrevocably removed yet
2710
        # - 4. done: (forwarding_key, None), irrevocably removed
2711

2712
        await self.initialized
5✔
2713
        while True:
5✔
2714
            await self.ping_if_required()
5✔
2715
            self._htlc_switch_iterdone_event.set()
5✔
2716
            self._htlc_switch_iterdone_event.clear()
5✔
2717
            # We poll every 0.1 sec to check if there is work to do,
2718
            # or we can also be triggered via events.
2719
            # When forwarding an HTLC originating from this peer (the upstream),
2720
            # we can get triggered for events that happen on the downstream peer.
2721
            # TODO: trampoline forwarding relies on the polling
2722
            async with ignore_after(0.1):
5✔
2723
                async with OldTaskGroup(wait=any) as group:
5✔
2724
                    await group.spawn(self._received_revack_event.wait())
5✔
2725
                    await group.spawn(self.downstream_htlc_resolved_event.wait())
5✔
2726
            self._htlc_switch_iterstart_event.set()
5✔
2727
            self._htlc_switch_iterstart_event.clear()
5✔
2728
            self._maybe_cleanup_received_htlcs_pending_removal()
5✔
2729
            for chan_id, chan in self.channels.items():
5✔
2730
                if not chan.can_send_ctx_updates():
5✔
2731
                    continue
3✔
2732
                self.maybe_send_commitment(chan)
5✔
2733
                done = set()
5✔
2734
                unfulfilled = chan.unfulfilled_htlcs
5✔
2735
                for htlc_id, (onion_packet_hex, forwarding_key) in unfulfilled.items():
5✔
2736
                    if not chan.hm.is_htlc_irrevocably_added_yet(htlc_proposer=REMOTE, htlc_id=htlc_id):
5✔
2737
                        continue
5✔
2738
                    htlc = chan.hm.get_htlc_by_id(REMOTE, htlc_id)
5✔
2739
                    if chan.hm.is_htlc_irrevocably_removed_yet(htlc_proposer=REMOTE, htlc_id=htlc_id):
5✔
2740
                        assert onion_packet_hex is None
5✔
2741
                        self.lnworker.maybe_cleanup_mpp(chan.get_scid_or_local_alias(), htlc)
5✔
2742
                        if forwarding_key:
5✔
2743
                            self.lnworker.maybe_cleanup_forwarding(forwarding_key)
5✔
2744
                        done.add(htlc_id)
5✔
2745
                        continue
5✔
2746
                    if onion_packet_hex is None:
5✔
2747
                        # has been processed already
2748
                        continue
5✔
2749
                    error_reason = None  # type: Optional[OnionRoutingFailure]
5✔
2750
                    error_bytes = None  # type: Optional[bytes]
5✔
2751
                    preimage = None
5✔
2752
                    onion_packet_bytes = bytes.fromhex(onion_packet_hex)
5✔
2753
                    onion_packet = None
5✔
2754
                    try:
5✔
2755
                        onion_packet = OnionPacket.from_bytes(onion_packet_bytes)
5✔
2756
                    except OnionRoutingFailure as e:
×
2757
                        error_reason = e
×
2758
                    else:
2759
                        try:
5✔
2760
                            preimage, _forwarding_key, error_bytes = self.process_unfulfilled_htlc(
5✔
2761
                                chan=chan,
2762
                                htlc=htlc,
2763
                                forwarding_key=forwarding_key,
2764
                                onion_packet_bytes=onion_packet_bytes,
2765
                                onion_packet=onion_packet)
2766
                            if _forwarding_key:
5✔
2767
                                assert forwarding_key is None
5✔
2768
                                unfulfilled[htlc_id] = onion_packet_hex, _forwarding_key
5✔
2769
                        except OnionRoutingFailure as e:
5✔
2770
                            error_bytes = construct_onion_error(e, onion_packet.public_key, self.privkey, self.network.get_local_height())
5✔
2771
                        if error_bytes:
5✔
2772
                            error_bytes = obfuscate_onion_error(error_bytes, onion_packet.public_key, our_onion_private_key=self.privkey)
5✔
2773

2774
                    if preimage or error_reason or error_bytes:
5✔
2775
                        if preimage:
5✔
2776
                            self.lnworker.set_request_status(htlc.payment_hash, PR_PAID)
5✔
2777
                            if not self.lnworker.enable_htlc_settle:
5✔
2778
                                continue
3✔
2779
                            self.fulfill_htlc(chan, htlc.htlc_id, preimage)
5✔
2780
                        elif error_bytes:
5✔
2781
                            self.fail_htlc(
5✔
2782
                                chan=chan,
2783
                                htlc_id=htlc.htlc_id,
2784
                                error_bytes=error_bytes)
2785
                        else:
2786
                            self.fail_malformed_htlc(
×
2787
                                chan=chan,
2788
                                htlc_id=htlc.htlc_id,
2789
                                reason=error_reason)
2790
                        # blank onion field to mark it as processed
2791
                        unfulfilled[htlc_id] = None, forwarding_key
5✔
2792

2793
                # cleanup
2794
                for htlc_id in done:
5✔
2795
                    unfulfilled.pop(htlc_id)
5✔
2796
                self.maybe_send_commitment(chan)
5✔
2797

2798
    def _maybe_cleanup_received_htlcs_pending_removal(self) -> None:
5✔
2799
        done = set()
5✔
2800
        for chan, htlc_id in self.received_htlcs_pending_removal:
5✔
2801
            if chan.hm.is_htlc_irrevocably_removed_yet(htlc_proposer=REMOTE, htlc_id=htlc_id):
5✔
2802
                done.add((chan, htlc_id))
5✔
2803
        if done:
5✔
2804
            for key in done:
5✔
2805
                self.received_htlcs_pending_removal.remove(key)
5✔
2806
            self.received_htlc_removed_event.set()
5✔
2807
            self.received_htlc_removed_event.clear()
5✔
2808

2809
    async def wait_one_htlc_switch_iteration(self) -> None:
5✔
2810
        """Waits until the HTLC switch does a full iteration or the peer disconnects,
2811
        whichever happens first.
2812
        """
2813
        async def htlc_switch_iteration():
5✔
2814
            await self._htlc_switch_iterstart_event.wait()
5✔
2815
            await self._htlc_switch_iterdone_event.wait()
5✔
2816

2817
        async with OldTaskGroup(wait=any) as group:
5✔
2818
            await group.spawn(htlc_switch_iteration())
5✔
2819
            await group.spawn(self.got_disconnected.wait())
5✔
2820

2821
    def process_unfulfilled_htlc(
5✔
2822
            self, *,
2823
            chan: Channel,
2824
            htlc: UpdateAddHtlc,
2825
            forwarding_key: Optional[str],
2826
            onion_packet_bytes: bytes,
2827
            onion_packet: OnionPacket) -> Tuple[Optional[bytes], Optional[str], Optional[bytes]]:
2828
        """
2829
        return (preimage, payment_key, error_bytes) with at most a single element that is not None
2830
        raise an OnionRoutingFailure if we need to fail the htlc
2831
        """
2832
        payment_hash = htlc.payment_hash
5✔
2833
        processed_onion = self.process_onion_packet(
5✔
2834
            onion_packet,
2835
            payment_hash=payment_hash,
2836
            onion_packet_bytes=onion_packet_bytes)
2837

2838
        preimage, forwarding_info = self.maybe_fulfill_htlc(
5✔
2839
            chan=chan,
2840
            htlc=htlc,
2841
            processed_onion=processed_onion,
2842
            onion_packet_bytes=onion_packet_bytes,
2843
            already_forwarded=bool(forwarding_key))
2844

2845
        if not forwarding_key:
5✔
2846
            if forwarding_info:
5✔
2847
                # HTLC we are supposed to forward, but haven't forwarded yet
2848
                payment_key, forwarding_callback = forwarding_info
5✔
2849
                if not self.lnworker.enable_htlc_forwarding:
5✔
2850
                    return None, None, None
×
2851
                if payment_key not in self.lnworker.active_forwardings:
5✔
2852
                    async def wrapped_callback():
5✔
2853
                        forwarding_coro = forwarding_callback()
5✔
2854
                        try:
5✔
2855
                            next_htlc = await forwarding_coro
5✔
2856
                            if next_htlc:
5✔
2857
                                htlc_key = serialize_htlc_key(chan.get_scid_or_local_alias(), htlc.htlc_id)
5✔
2858
                                self.lnworker.active_forwardings[payment_key].append(next_htlc)
5✔
2859
                                self.lnworker.downstream_to_upstream_htlc[next_htlc] = htlc_key
5✔
2860
                        except OnionRoutingFailure as e:
5✔
2861
                            if len(self.lnworker.active_forwardings[payment_key]) == 0:
5✔
2862
                                self.lnworker.save_forwarding_failure(payment_key, failure_message=e)
5✔
2863
                        # TODO what about other errors? e.g. TxBroadcastError for a swap.
2864
                        #        - malicious electrum server could fake TxBroadcastError
2865
                        #      Could we "catch-all Exception" and fail back the htlcs with e.g. TEMPORARY_NODE_FAILURE?
2866
                        #        - we don't want to fail the inc-HTLC for a syntax error that happens in the callback
2867
                        #      If we don't call save_forwarding_failure(), the inc-HTLC gets stuck until expiry
2868
                        #      and then the inc-channel will get force-closed.
2869
                        #      => forwarding_callback() could have an API with two exceptions types:
2870
                        #        - type1, such as OnionRoutingFailure, that signals we need to fail back the inc-HTLC
2871
                        #        - type2, such as TxBroadcastError, that signals we want to retry the callback
2872
                    # add to list
2873
                    assert len(self.lnworker.active_forwardings.get(payment_key, [])) == 0
5✔
2874
                    self.lnworker.active_forwardings[payment_key] = []
5✔
2875
                    fut = asyncio.ensure_future(wrapped_callback())
5✔
2876
                # return payment_key so this branch will not be executed again
2877
                return None, payment_key, None
5✔
2878
            elif preimage:
5✔
2879
                return preimage, None, None
5✔
2880
            else:
2881
                # we are waiting for mpp consolidation or preimage
2882
                return None, None, None
5✔
2883
        else:
2884
            # HTLC we are supposed to forward, and have already forwarded
2885
            # for final trampoline onions, forwarding failures are stored with forwarding_key (which is the inner key)
2886
            payment_key = forwarding_key
5✔
2887
            preimage = self.lnworker.get_preimage(payment_hash)
5✔
2888
            error_bytes, error_reason = self.lnworker.get_forwarding_failure(payment_key)
5✔
2889
            if error_bytes:
5✔
2890
                return None, None, error_bytes
5✔
2891
            if error_reason:
5✔
2892
                raise error_reason
5✔
2893
            if preimage:
5✔
2894
                return preimage, None, None
5✔
2895
            return None, None, None
5✔
2896

2897
    def process_onion_packet(
5✔
2898
            self,
2899
            onion_packet: OnionPacket, *,
2900
            payment_hash: bytes,
2901
            onion_packet_bytes: bytes,
2902
            is_trampoline: bool = False) -> ProcessedOnionPacket:
2903

2904
        failure_data = sha256(onion_packet_bytes)
5✔
2905
        try:
5✔
2906
            processed_onion = process_onion_packet(
5✔
2907
                onion_packet,
2908
                our_onion_private_key=self.privkey,
2909
                associated_data=payment_hash,
2910
                is_trampoline=is_trampoline)
2911
        except UnsupportedOnionPacketVersion:
×
2912
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_VERSION, data=failure_data)
×
2913
        except InvalidOnionPubkey:
×
2914
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_KEY, data=failure_data)
×
2915
        except InvalidOnionMac:
×
2916
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_HMAC, data=failure_data)
×
2917
        except Exception as e:
×
2918
            self.logger.info(f"error processing onion packet: {e!r}")
×
2919
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_VERSION, data=failure_data)
×
2920
        if self.network.config.TEST_FAIL_HTLCS_AS_MALFORMED:
5✔
2921
            raise OnionRoutingFailure(code=OnionFailureCode.INVALID_ONION_VERSION, data=failure_data)
×
2922
        if self.network.config.TEST_FAIL_HTLCS_WITH_TEMP_NODE_FAILURE:
5✔
2923
            raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
5✔
2924
        return processed_onion
5✔
2925

2926
    def on_onion_message(self, payload):
5✔
2927
        if hasattr(self.lnworker, 'onion_message_manager'):  # only on LNWallet
×
2928
            self.lnworker.onion_message_manager.on_onion_message(payload)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc