Commit graph

3413 commits

Author SHA1 Message Date
Jeffrey Czyz
2e730cdc9c
Decay channel liquidity balance offsets
ProbabilisticScorer uses successful and unsuccessful payments to gain
more certainty of a channel's liquidity balance. Decay this knowledge
over time to indicate decreasing certainty about the liquidity balance.
2022-02-02 20:22:27 -06:00
Jeffrey Czyz
68d791ad84
Probabilistic channel scoring
Add a Score implementation based on "Optimally Reliable & Cheap Payment
Flows on the Lightning Network" by Rene Pickhardt and Stefan Richter[1].
Given the uncertainty of channel liquidity balances, probability
distributions are defined based on knowledge learned from successful and
unsuccessful attempts. Then the negative log of the success probability
is used to determine the cost of routing a specific HTLC amount through
a channel.

[1]: https://arxiv.org/abs/2107.05322
2022-02-02 19:47:12 -06:00
Jeffrey Czyz
1aaf5fc5d0
Effective channel capacity for router and scoring
A channel's capacity may be inferred or learned and is used to make
routing decisions, including as a parameter to channel scoring. Define
an EffectiveCapacity for this purpose. Score::channel_penalty_msat takes
the effective capacity (less in-flight HTLCs for the same payment), and
never None. Thus, for hops given in an invoice, the effective capacity
is now considered (near) infinite if over a private channel or based on
learned information if over a public channel.

If a Score implementations needs the effective capacity when updating a
channel's score, i.e. in payment_path_failed or payment_path_successful,
it can access the channel's EffectiveCapacity via the NetworkGraph by
first looking up the channel and then specifying which direction is
desired using ChannelInfo::as_directed.
2022-02-02 19:46:58 -06:00
Matt Corallo
649af07205 Store override counterparty handshake limits until we enforce them
We currently allow users to provide an `override_config` in
`ChannelManager::create_channel` which it seems should apply to the
channel. However, because we don't store any of it, the only parts
which we apply to the channel are those which are set in the
`Channel` object immediately in `Channel::new_outbound` and used
from there.

This is great in most cases, however the
`UserConfig::peer_channel_config_limits` `ChannelHandshakeLimits`
object is used in `accept_channel` to bound what is acceptable in
our peer's `AcceptChannel` message. Thus, for outbound channels, we
are given a full `UserConfig` object to "override" the default
config, but we don't use any of the handshake limits specified in
it.

Here, we move to storing the `ChannelHandshakeLimits` explicitly
and applying it when we receive our peer's `AcceptChannel`. Note
that we don't need to store it anywhere because if we haven't
received an `AcceptChannel` from our peer when we reload from disk
we will forget the channel entirely anyway.
2022-02-01 21:40:56 +00:00
valentinewallace
482a2b9250
Merge pull request #1282 from TheBlueMatt/2022-01-fuzz-overflow
Avoid overflow in addition when checking counterparty feerates
2022-01-27 11:42:05 -05:00
Matt Corallo
cc88ae6d8d Remove stale reference to incomplete BOLT compliance
The referenced issue was closed some time ago with a PR to amend
the BOLTs to be more restrictive, which we are in compliance with.
2022-01-26 23:28:45 +00:00
Matt Corallo
457e48e102
Merge pull request #1179 from TheBlueMatt/2021-11-fix-announce-sigs-broadcast-time
Disconnect announcement_signatures sending from funding_locked
2022-01-26 23:27:04 +00:00
Matt Corallo
ed1163a5bf Make Channel::get_announcement_sigs return an Option and log
Channel::get_announcement_sigs is only used in contexts where we
have a logger already, and the error returned is always ignored, so
instead of returning an ignored error message we return an `Option`
directly and log when it won't be too verbose.
2022-01-26 18:20:26 +00:00
Matt Corallo
ee7cfa59d1 Swap loop and condition order to avoid looping unnecessarily 2022-01-26 18:20:26 +00:00
Matt Corallo
a265fc2062 Disconect announcement_signatures sending from funding_locked
The spec actually requires we never send `announcement_signatures`
(and, thus, `channel_announcement`s) until after six confirmations.
However, we would happily have sent them prior to that as long as
we exchange `funding_locked` messages with our countarparty. Thanks
to re-broadcasting this issue is largely harmless, however it could
have some negative interactions with less-robust peers. Much more
importantly, this represents an important step towards supporting
0-conf channels, where `funding_locked` messages may be exchanged
before we even have an SCID to construct the messages with.

Because there is no ACK mechanism for `announcement_signatures` we
rely on existing channel updates to stop rebroadcasting them - if
we sent a `commitment_signed` after an `announcement_signatures`
and later receive a `revoke_and_ack`, we know our counterparty also
received our `announcement_signatures`. This may resolve some rare
edge-cases where we send a `funding_locked` which our counterparty
receives, but lose connection before the `announcement_signatures`
(usually the very next message) arrives.

Sadly, because the set of places where an `announcement_signatures`
may now be generated more closely mirrors where `funding_locked`
messages may be generated, but they are now separate, there is a
substantial amount of code motion providing relevant parameters
about current block information and ensuring we can return new
`announcement_signatures` messages.
2022-01-26 18:20:26 +00:00
Matt Corallo
e7facb1b66 Unset Channel::is_usable if mon update is blocking funding_locked
If we have not yet sent `funding_locked` only because of a pending
channel monitor update, we shouldn't consider a channel
`is_usable`. This has a number of downstream effects, including
not attempting to route payments through the channel, not sending
private `channel_update` messages to our counterparty, or sending
channel_announcement messages if our couterparty has already signed
for it.

We further gate generation of `node_announcement`s on `is_usable`,
preventing generation of those or `announcement_signatures` until
we've sent our `funding_locked`.

Finally, `during_funding_monitor_fail` is updated to test a case
where we see the funding transaction lock in but have a pending
monitor update failure, then receive `funding_locked` from our
counterparty and ensure we don't generate the above messages until
after the monitor update completes.
2022-01-26 18:20:26 +00:00
Matt Corallo
0243f21160 Do not Send FundingLocked messages while disconnected
While its generally harmless to do so (the messages will simply be
dropped in `PeerManager`) there is a potential race condition where
the FundingLocked message enters the outbound message queue, then
the peer reconnects, and then the FundingLocked message is
delivered prior to the normal ChannelReestablish flow.

We also take this opportunity to rewrite
`test_funding_peer_disconnect` to be explicit instead of using
`reconnect_peers`. This allows it to check each message being sent
carefully, whereas `reconnect_peers` is rather lazy and accepts
that sometimes signatures will be exchanged, and sometimes not.
2022-01-26 18:20:26 +00:00
Matt Corallo
a6ddb973ea Return struct, not long tuple, from Channel::channel_reestablish
This improves readability and makes it easier to add additional
return fields.
2022-01-26 18:20:26 +00:00
valentinewallace
dfc93b4341
Merge pull request #1283 from TheBlueMatt/2022-01-correct-req-feature-handling
Correct handling of `UnknownRequiredFeature` deserialization
2022-01-26 11:30:43 -05:00
valentinewallace
f49662caa4
Merge pull request #1273 from jkczyz/2022-01-invoice-expiry
Support invoice expiry over a year
2022-01-26 09:51:14 -05:00
Matt Corallo
94639137c3 Correct handling of UnknownRequiredFeature deserialization
Quite some time ago, `UnknownRequiredFeature` was only used when a
gossip message has a missing required feature. These days, its also
used for any required TLV which we do not understand in any
message. However, the handling of it was never updated in
`PeerManager`, leaving it printing a warning about gossip and
ignoring the message entirely.

Instead, we send a warning message and disconnect.

Closes #1236, as caught by @jkczyz.
2022-01-26 02:12:35 +00:00
Matt Corallo
b54fe5fcc7 Avoid overflow in addition when checking counterparty feerates
This is harmless outside of debug builds - the feerate will
overflow causing it to either spuriously fail the first check, or
correctly pass it and fail the second check. In debug builds,
however, it panics due to integer overflow.

Found by the `full_stack_target` fuzz test in the
Chaincode-provided continuous fuzzing. Thanks Chaincode!
2022-01-26 00:10:19 +00:00
Jeffrey Czyz
3b14a76d01
Support invoice expiry over a year
The lightning-invoice crate represents timestamps as Duration since the
UNIX epoch rather than a SystemTime. Therefore, internal calculations
are in terms of u64-based Durations. This allows for relaxing the one
year maximum expiry.
2022-01-25 15:34:10 -06:00
Matt Corallo
d62edd58ab Move node_id signing of ChannelAnnouncement into Signer
This removes one more place where we directly access the node_id
secret key in `ChannelManager`, slowly marching towards allowing
the node_id secret key to be offline in the signer.

More importantly, it allows more ChannelAnnouncement logic to move
into the `Channel` without having to pass the node secret key
around, avoiding the announcement logic being split across two
files.
2022-01-25 18:25:56 +00:00
valentinewallace
1a24dcc3e9
Merge pull request #1275 from jkczyz/2022-01-benchmark-improvements
Router benchmark improvements
2022-01-25 12:18:17 -05:00
Matt Corallo
3baaebe7dd
Merge pull request #1251 from lightning-signer/2022-01-signer-preimages
Provide payment preimages to signer on HTLC success
2022-01-25 17:12:05 +00:00
Jeffrey Czyz
d924c5d6ed
Benchmark zero-penalty scorer 2022-01-24 19:32:53 -06:00
Jeffrey Czyz
4512fd3685
Benchmark router using a scorer seeded with data
Scorers may have different performance characteristics after seeing
failed and successful paths. Seed the scorer with some random data
before executing the benchmark in order to exercise such behavior.
2022-01-24 19:32:47 -06:00
Jeffrey Czyz
486df783e9
Add first_hops to generate_routes benchmarks
Passing first_hops to get_route increases the coverage of the benchmark
test. For scorers needing the sending node, it allows for using a single
scorer in the benchmark rather than re-initializing on each iteration.
As a consequence, the scorer can be seeded with success and failure
data.
2022-01-24 19:05:49 -06:00
Jeffrey Czyz
06053fc21c
Remove duplicate generate_routes benchmark code
Refactor generate_routes and generate_mpp_routes into a single utility
for benchmarking. The utility is parameterized with features in order to
test both single path and multi-path routing. Additionally, it is
parameterized with a Score to be used with other scorers.
2022-01-24 18:52:36 -06:00
Devrandom
6e19d1f523 Provide preimages to signer 2022-01-24 21:53:03 +01:00
Devrandom
9aa786cfbb Keep track of preimage in OutboundHTLCState on success 2022-01-24 21:53:03 +01:00
valentinewallace
35d4ebb208
Merge pull request #1272 from lightning-signer/2022-01-sign-invoice-api
Improve KeysInterface::sign_invoice API
2022-01-24 11:39:58 -05:00
valentinewallace
b19c56b78a
Merge pull request #1271 from tnull/rename_payee_struct
Rename `Payee` to `PaymentParameters`
2022-01-24 11:34:48 -05:00
valentinewallace
07776d3dab
Merge pull request #1253 from TheBlueMatt/2022-01-background-persist-exit
Persist `ChannelManager` before `BackgroundProcessor` exits
2022-01-24 11:23:12 -05:00
Devrandom
803d6b6e2f Improve KeysInterface::sign_invoice API
split hrp from invoice data, to allow parsing, since there is no delimiter between the two parts
2022-01-24 09:48:56 +01:00
Matt Corallo
19fdde26d7
Merge pull request #1250 from vss96/sanity_check
Sanity check for ChannelManager and KeysInterface
2022-01-22 18:19:35 +00:00
vss96
2f01d68148 Sanity check for ChannelManager and KeysInterface
Fix build errors

Create script using p2wsh for comparison

Using p2wpkh for generating the payment script

spendable_outputs sanity check

Return err in spendable_outputs

Doc updates in keysinterface
2022-01-22 10:12:42 +05:30
valentinewallace
b1bdba5265
Merge pull request #1269 from TheBlueMatt/022-01-no-disconnect-on-slow-persist
Avoid disconnecting all peers if user code is slow
2022-01-21 11:52:05 -05:00
Elias Rohrer
808477a5ce Rename Payee to PaymentParameters 2022-01-21 10:39:01 +01:00
Matt Corallo
2d3a210897 Increase our PING_TIMER to ten seconds, from five.
Because many lightning nodes can take quite some time to respond to
pings, the five second ping timer can sometimes cause spurious
disconnects even though a peer is online. However, in part as a
response to mobile users where a connection may be lost as result
of only a short time with the app in a "paused" state, we had a
rather aggressive ping time to ensure we would disconnect quickly.

However, since we now just used a fixed time for the "went to
sleep" detection, we can somewhat increase the ping timer. We still
want to be fairly aggressive to avoid sending HTLCs to a peer that
is offline, but the tradeoff between spurious disconnections and
stuck payments is likely doesn't need to be quite as aggressive.
2022-01-21 00:36:59 +00:00
Matt Corallo
8f5023a006 Avoid disconnecting all peers if user code is slow
In the sample client (and likely other downstream users), event
processing may block on slow operations (e.g. Bitcoin Core RPCs)
and ChannelManager persistence may take some time. This should be
fine, except that we consider this a case of possible backgrounding
and disconnect all of our peers when it happens.

Instead, we here avoid considering event processing time in the
time between PeerManager events.
2022-01-21 00:36:59 +00:00
Jeffrey Czyz
d741fb14fa
Merge pull request #1234 from tnull/limit_max_cltv_delta
Limit maximum CLTV delta during routing
2022-01-20 10:36:22 -06:00
Matt Corallo
581a800ce2
Merge pull request #1248 from naveensrinivasan/naveen/feat/update-readme
Docs: Updated README to include crates information
2022-01-20 16:34:01 +00:00
Elias Rohrer
367ed31dc9 Limit maximum total CLTV expiry delta during routing. 2022-01-20 16:00:00 +01:00
naveen
f29e4855f3 Docs: Updated README to include crates information
Included crates information into README.
2022-01-19 19:27:11 +00:00
valentinewallace
e4387fa8af
Merge pull request #1258 from lightningdevkit/dependabot/cargo/hex-0.4
Update hex requirement from 0.3 to 0.4
2022-01-19 13:19:59 -05:00
Elias Rohrer
689518520e Fixed some typos 2022-01-19 18:15:47 +01:00
Matt Corallo
ce82a33186 Persist ChannelManager before BackgroundProcessor exits
Fixes #1237.
2022-01-18 22:07:15 +00:00
dependabot[bot]
f39fa69b68
Update hex requirement from 0.3 to 0.4
Updates the requirements on [hex](https://github.com/KokaKiwi/rust-hex) to permit the latest version.
- [Release notes](https://github.com/KokaKiwi/rust-hex/releases)
- [Commits](https://github.com/KokaKiwi/rust-hex/compare/v0.3...v0.4.3)

---
updated-dependencies:
- dependency-name: hex
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-01-18 22:05:49 +00:00
Arik Sosman
10204f706f
Merge pull request #1243 from naveensrinivasan/naveensrinivasan/dependabot
Enable Dependabot
2022-01-18 14:04:38 -08:00
Naveen
126771daf8 Create dependabot.yml
Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
2022-01-18 19:39:53 +00:00
valentinewallace
7b6a7bbc72
Merge pull request #1249 from tnull/fix_compile_warnings
Fix compiler warning during building/testing.
2022-01-18 11:18:05 -05:00
Elias Rohrer
5490366a95 Fix unused return warning for 'transmute_copy'. 2022-01-18 14:45:43 +01:00
Matt Corallo
34cdca91ba
Merge pull request #1238 from TheBlueMatt/2022-01-lockorder-checks
Fix and test lockorder
2022-01-14 16:59:42 +00:00