Commit graph

2099 commits

Author SHA1 Message Date
Elias Rohrer
97e6682d58 Fix 'not a hyperlink' doc warning 2022-07-20 14:57:31 +02:00
Matt Corallo
ff8d3f7ba4 Reduce default max_channel_saturation_power_of_half to 2 (max 1/4)
Saturating a channel beyond 1/4 of its capacity seems like a more
reasonable threshold for avoiding a path than 1/2, especially given
we should still be willing to send a payment with a lower
saturation limit if it comes to that.

This requires an (obvious) change to some router tests, but also
requires a change to the `fake_network_test`, opting to simply
remove some over-limit test code there - `fake_network_test` was
our first ever functional test, and while it worked great to ensure
LDK worked at all on day one, we now have a rather large breadth
of functional tests, and a broad "does it work at all" test is no
longer all that useful.
2022-07-19 15:16:35 +00:00
Matt Corallo
985153e128 Make route path selection optimize strictly for cost / amount
Currently, after we've selected a number of candidate paths, we
construct a route from a random set of paths repeatedly, and then
select the route with the lowest total cost. In the vast majority
of cases this ends up doing a bunch of additional work in order to
select the path(s) with the total lowest cost, with some vague
attempt at randomization that doesn't actually work.

Instead, here, we simply sort available paths by `cost / amount`
and select the top paths. This ends up in practice having the same
end result with substantially less complexity. In some rare cases
it gets a better result, which also would have been achieved
through more random trials. This implies there may in such cases be
a potential privacy loss, but not a substantial one, given our path
selection is ultimately mostly deterministic in many cases (or, if
it is not, then privacy is achieved through randomization at the
scorer level).
2022-07-19 15:16:35 +00:00
Matt Corallo
0c3a47c016 Fix tracking of collected value across pathfinding iterations
If we end up "paying" for an `htlc_minimum_msat` with fees, we
increment `already_collected_value_msat` by more than the amount
of the path that we collected (who's `value_contribution_msat` is
higher than the total payment amount, despite having been reduced
down to the payment amount).

This throws off our total value collection target, though in the
coming commit(s) it would also throw off our path selection
calculations.
2022-07-19 15:16:35 +00:00
Matt Corallo
a3547e29e5 Change default "impossibility penalty" to one Bitcoin
In general we should avoid taking paths that we are confident will
not work as much possible, but we should be willing to try each
payment at least once, even if its over a channel that failed
recently. A full Bitcoin penalty for such a channel seems
reasonable - lightning fees are unlikely to ever reach that point
so such channels will be scored much worse than any other potential
path, while still being below `u64::max_value()`.
2022-07-14 18:37:26 +00:00
Matt Corallo
ec2b1ced07 Make the ProbabilisticScorer impossibility penalty configurable
When we consider sending an HTLC over a given channel impossible
due to our current knowledge of the channel's liquidity, we
currently always assign a penalty of `u64::max_value()`. However,
because we now refuse to retry a payment along the same path in
the router itself, we can now make this value configurable. This
allows users to have a relatively high knowledge decay interval
without the side-effect of refusing to try the only available path
in cases where a channel is intermittently available.
2022-07-14 18:37:25 +00:00
Matt Corallo
93e645daf4 Track channels which a given payment part failed to traverse
When an HTLC fails, we currently rely on the scorer learning the
failed channel and assigning an infinite (`u64::max_value()`)
penalty to the channel so as to avoid retrying over the exact same
path (if there's only one available path). This is common when
trying to pay a mobile client behind an LSP if the mobile client is
currently offline.

This leads to the scorer being overly conservative in some cases -
returning `u64::max_value()` when a given path hasn't been tried
for a given payment may not be the best decision, even if that
channel failed 50 minutes ago.

By tracking channels which failed on a payment part level and
explicitly refusing to route over them we can relax the
requirements on the scorer, allowing it to make different decisions
on how to treat channels that failed relatively recently without
causing payments to retry the same path forever.

This does have the drawback that it could allow two separate part
of a payment to traverse the same path even though that path just
failed, however this should only occur if the payment is going to
fail anyway, at least as long as the scorer is properly learning.

Closes #1241, superseding #1252.
2022-07-14 18:37:25 +00:00
Matt Corallo
5cca9a0696
Merge pull request #1605 from TheBlueMatt/2022-07-smaller-mpp-parts
Avoid saturating channels before we split payments
2022-07-14 18:33:53 +00:00
Jeffrey Czyz
b76040718f
Merge pull request #1543 from jkczyz/2022-06-network-graph-bindings
Look-up functions for `ReadOnlyNetworkGraph`
2022-07-14 11:57:08 -05:00
Jeffrey Czyz
2da49530e7
Look-up functions for ReadOnlyNetworkGraph
ReadOnlyNetworkGraph uses BTreeMap to store its nodes and channels, but
these data structures are not supported by C bindings. Expose look-up
functions on these maps in lieu of such support.
2022-07-14 10:26:42 -05:00
Matt Corallo
0627c0c88a Fix some test theoretical lock inversions
In the next commit we add lockorder testing based on the line each
mutex was created on rather than the particular mutex instance.
This causes some additional test failure because of lockorder
inversions for the same mutex across different tests, which is
fixed here.
2022-07-13 19:28:29 +00:00
Matt Corallo
e6d40a7c0e Add a test of the new channel saturation limit 2022-07-13 18:36:50 +00:00
Matt Corallo
a02982fbba Relax the channel saturation limit if we can't find enough paths
In order to avoid failing to find paths due to the new channel
saturation limit, if we fail to find enough paths, we simply
disable the saturation limit for further path finding iterations.

Because we can now increase the maximum sent over a given channel
during routefinding, we may now generate redundant paths for the
same payment. Because this is wasteful in the network, we add an
additional pass during routefinding to merge redundant paths.

Note that two tests which previously attempted to send exactly the
available liquidity over a channel which charged an absolute fee
need updating - in those cases the router will first collect a path
that is saturation-limited, then attempt to collect a second path
without a saturation limit while stil honoring the existing
utilized capacity on the channel, causing failure as the absolute
fee must be included.
2022-07-13 18:36:50 +00:00
Matt Corallo
bd6b710328 Avoid saturating channels before we split payments
Currently we only opt to split a payment into an MPP if we have
completely and totally used a channel's available capacity (up to
the announced htlc_max or on-chain capacity, whichever is lower).
This is obviously destined to fail as channels are unlikely to have
their full capacity available.

Here we do the minimum viable fix by simply limiting channels to
only using up to a configurable power-of-1/2. We default this new
configuration knob to 1 (1/2 of the channel) so as to avoid a
substantial change but in the future we may consider changing this
to 2 (1/4) or even 3 (1/8).
2022-07-13 18:36:50 +00:00
Duncan Dean
7bc6d0e606
Make all internal signatures accept LowerBoundedFeeEstimator 2022-07-13 15:00:51 +02:00
Duncan Dean
9c0c3b0c95
Add LowerBoundedFeeEstimator to set FeeEstimator min rates
`LowerBoundedFeeEstimator` is a wrapper for `Deref`s to `FeeEstimator`s
that limits the get_est_sat_per_1000_weight() method to no less than 253
sats/kW.
2022-07-13 15:00:50 +02:00
Matt Corallo
fda3819699
Merge pull request #1542 from ViktorTigerstrom/2022-06-prepare-maps-for-channels-per-peer
Preparations for storing channels per peer
2022-07-12 18:03:11 -07:00
Viktor Tigerström
fa7f170a73 Add ChannelManager:id_to_peer map coverage test 2022-07-12 17:47:08 +02:00
Viktor Tigerström
4058861730 Add id_to_peer map 2022-07-12 17:47:08 +02:00
Matt Corallo
5c06d1d9c8
Merge pull request #1603 from TheBlueMatt/2022-07-no-backwards-time
Avoid panicking on wallclock time going backwards across restart
2022-07-11 14:07:18 -07:00
Jeffrey Czyz
29e34c8a10
Merge pull request #1596 from jurvis/jurvis/give-chanmon-counterparty-id
Make ChannelMonitor aware of counterparty's node id
2022-07-11 13:51:55 -05:00
Matt Corallo
497fd65ae9 Avoid panicking on wallclock time going backwards across restart
Because we serialize `Instant`s using wallclock time in
`ProbabilisticScorer`, if time goes backwards across restarts we
may end up with `Instant`s in the future, which causes rustc prior
to 1.60 to panic when calculating durations. Here we simply avoid
this by setting the time to `now` if we get a time in the future.
2022-07-11 18:49:22 +00:00
jurvis
2d493364db
Add counterparty_node_id to ChannelMonitor 2022-07-09 12:47:29 -07:00
Matt Corallo
ad2e92a3fb
Merge pull request #1592 from tnull/2022-07-manual-penalty
Allow to set manual node penalties
2022-07-08 12:29:25 -07:00
Jeffrey Czyz
4e5f74a6f3
Merge pull request #1567 from tnull/2022-06-send-probe
Add simple probing API
2022-07-07 09:27:14 -05:00
Viktor Tigerström
872c0378f7 Rename short_to_id map to short_to_chan_info
As the map values are no longer only `channel_id`s, but also a
`counterparty_node_id`s, the map is renamed to better correspond to
whats actually stored in the map.
2022-07-07 13:38:31 +02:00
Viktor Tigerström
908e898dcd Add counterparty_node_id to short_to_id map 2022-07-07 13:34:18 +02:00
Elias Rohrer
eb8bce0d16 Add send_probe and introduce probing cookies
When we send payment probes, we generate the [`PaymentHash`] based on a
probing cookie secret and a random [`PaymentId`]. This allows us to
discern probes from real payments, without keeping additional state.
2022-07-07 09:02:29 +02:00
Elias Rohrer
790abc540d Refactor max_mpp_path_count to max_path_count
Using this field just for MPP doesn't make sense when it could
intuitively also be used to indicate single-path payments. We therefore
rename `max_mpp_path_count` to `max_path_count` and make sure that a
value of 1 ensures MPP is not even tried.
2022-07-06 08:06:35 +02:00
Matt Corallo
e403999ffd
Merge pull request #1588 from TheBlueMatt/2022-06-ffs-dumb-ser
Do not execute the default_value expr until we need it in TLV deser
2022-07-05 13:46:43 -07:00
Matt Corallo
f1b9bd34b8 Do not execute the default_value expr until we need it in TLV deser
This fixes an insta-panic in `ChannelMonitor` deserialization where
we always `unwrap` a previous value to determine the default value
of a later field. However, because we always ran the `unwrap`
before the previous field is read, we'd always panic.

The fix is rather simple - use a `OptionDeserWrapper` for
`default_value` fields and only fill in the default value if no
value was read while walking the TLV stream.

The only complexity comes from our desire to support
`read_tlv_field` calls that use an explicit field rather than an
`Option` of some sort, which requires some statement which can
assign both an `OptionDeserWrapper<T>` variable and a `T` variable.
We settle on `x = t.into()` and implement `From<T> for
OptionDeserWrapper<T>` which works, though it requires users to
specify types explicitly due to Rust determining expression types
prior to macro execution, completely guessing with no knowlege for
integer expressions (see
https://github.com/rust-lang/rust/issues/91369).
2022-07-05 17:32:21 +00:00
Elias Rohrer
1ddc6f1089 Allow to set manual node penalties
A user might want to explicitly penalize or prioritize a particular
node. We now allow them to do so by specifying a manual penalty
override for a given node that is then returned by the scorer.
2022-07-05 16:39:53 +02:00
Matt Corallo
daeb5a6291
Merge pull request #1553 from wvanlint/dns_hostname
Adds DNS hostname to NetAddress
2022-07-05 07:24:17 -07:00
Willem Van Lint
c30dcf183c Adds DNS hostname to NetAddress 2022-07-04 10:19:16 -07:00
Matt Corallo
156cc77753 Bump crate versions to 0.0.109/invoice 0.17 2022-07-01 16:05:33 +00:00
Matt Corallo
6c480ae887 Fix spurious panic on bogus funding txn that confirm and are spent
In c02b6a3807 we moved the
`payment_preimage` copy from inside the macro which only runs if we
are spending an output we know is an HTLC output to doing it for
any script that matches our expected length. This can panic if an
inbound channel is created with a bogus funding transaction that
has a witness program of the HTLC-Success/-Offered length but which
does not have a second-to-last witness element which is 32 bytes.

Luckily this panic is relatively simple for downstream users to
work around - if an invalid-length-copy panic occurs, simply remove
the ChannelMonitor from the bogus channel on startup and run
without it. Because the channel must be funded by a bogus script in
order to reach this panic, the channel will already have closed by
the time the funding transaction is spent, and there can be no
local funds in such a channel, so removing the `ChannelMonitor`
wholesale is completely safe.

In order to test this we have to disable an in-line assertion that
checks that our transactions match expected scripts which we do by
checking for the specific bogus script that we now use in
`test_invalid_funding_tx`.

Thanks to Eugene Siegel for reporting this issue.
2022-07-01 14:47:17 +00:00
Matt Corallo
87a6e013f7 Have find_route take a NetworkGraph instead of a ReadOnly one
Because downstream languages are often garbage-collected, having
the user directly allocate a `ReadOnlyNetworkGraph` and pass a
reference to it to `find_route` often results in holding a read
lock long in excess of the `find_route` call. Worse, some languages
(like JavaScript) tend to only garbage collect when other code is
not running, possibly leading to deadlocks.
2022-06-29 17:45:49 +00:00
Matt Corallo
a600eee87c
Merge pull request #1564 from TheBlueMatt/2022-06-panic-on-behind
Panic if we're running with outdated state instead of force-closing
2022-06-27 09:34:26 -07:00
Elias Rohrer
800ccec0ed Add anti-probing penalty to ProbabilisticScorer
Currently, channel balances may be rather easily discovered through
probing. This however poses a privacy risk, since the analysis of
balance changes over adjacent channels could in the worst case empower an adversary to
mount an end-to-end deanonymization attack, i.e., track who payed whom.

The penalty added here is applied so we prefer nodes with a smaller `htlc_maximum_msat`, which makes
balance discovery attacks harder to execute. As this improves privacy network-wide, we
treat such nodes preferentially and hence create an incentive to restrict
`htlc_maximum_msat`.
2022-06-25 20:06:04 +02:00
Matt Corallo
caa2a9a55b Panic if we're running with outdated state instead of force-closing
When we receive a `channel_reestablish` with a `data_loss_protect`
that proves we're running with a stale state, instead of
force-closing the channel, we immediately panic. This lines up with
our refusal to run if we find a `ChannelMonitor` which is stale
compared to our `ChannelManager` during `ChannelManager`
deserialization. Ultimately both are an indication of the same
thing - that the API requirements on `chain::Watch` were violated.

In the "running with outdated state but ChannelMonitor(s) and
ChannelManager lined up" case specifically its likely we're running
off of an old backup, in which case connecting to peers with
channels still live is explicitly dangerous. That said, because
this could be an operator error that is correctable, panicing
instead of force-closing may allow for normal operation again in
the future (cc #1207).

In any case, we provide instructions in the panic message for how
to force-close channels prior to peer connection, as well as a note
on how to broadcast the latest state if users are willing to take
the risk.

Note that this is still somewhat unsafe until we resolve #1563.
2022-06-25 02:25:32 +00:00
Matt Corallo
5ed3f25b21 Add ChannelManager methods to force close without broadcasting
If a user restores from a backup that they know is stale, they'd
like to force-close all of their channels (or at least the ones
they know are stale) *without* broadcasting the latest state,
asking their peers to do so instead. This simply adds methods to do
so, renaming the existing `force_close_channel` and
`force_close_all_channels` methods to disambiguate further.
2022-06-25 02:25:32 +00:00
Elias Rohrer
57d8257a0b Allow nodes to be avoided during pathfinding
Users may want to - for whatever reasons - prevent payments to be routed
over certain nodes. This change therefore allows to add `NodeId`s to a
list of banned nodes, which then will be avoided during path finding.
2022-06-24 08:31:41 +02:00
Matt Corallo
3676a056c8
Merge pull request #1518 from valentinewallace/2022-06-OMs-prefactor
Onion messages v1 pre-refactor
2022-06-21 16:13:37 -07:00
Valentine Wallace
ee805e97ff
Enable simultaneous deserialization+decryption of a ChaChaPoly stream
In the upcoming onion messages PR, this will allow us to avoid decrypting onion
message encrypted data in an intermediate Vec before decoding it. Instead we
decrypt and decode it at the same time using this new ChaChaPolyReadAdapter object.

In doing so, we need to adapt the decode_tlv_stream macro such that it will
decode a LengthReadableArgs, which is a new trait as well. This trait is
necessary because ChaChaPoly needs to know the total length ahead of time to
separate out the tag at the end.
2022-06-21 17:08:28 -04:00
valentinewallace
10071b51e2
Merge pull request #1556 from danielgranhao/2022-06-improve-docs
Clarify description of get_node_secret() method
2022-06-21 15:59:31 -04:00
Valentine Wallace
945cec3027
chacha20poly1305: enable simultaneous writing+encryption
In the upcoming onion messages PR, this will allow us to avoid encoding onion
message encrypted data into an intermediate Vec before encrypting it.  Instead
we encode and encrypt at the same time using this new ChaChaPolyWriteAdapter object.
2022-06-21 15:09:25 -04:00
Matt Corallo
c502e8d101
Merge pull request #1486 from TheBlueMatt/2022-05-revoked-txn-edge-cases
Fix two edge cases in handling of counterparty revoked commitment txn
2022-06-21 11:47:15 -07:00
Daniel Granhão
526d38169e Change description of get_node_secret() 2022-06-21 17:38:57 +01:00
Matt Corallo
70ae45fea0 Don't fail HTLCs in revoked commitment txn until we spend them
When we see a counterparty revoked commitment transaction on-chain
we shouldn't immediately queue up HTLCs present in it for
resolution until we have spent the HTLC outputs in some kind of
claim transaction.

In order to do so, we first have to change the
`fail_unbroadcast_htlcs!()` call to provide it with the HTLCs which
are present in the (revoked) commitment transaction which was
broadcast. However, this is not sufficient - because all of those
HTLCs had their `HTLCSource` removed when the commitment
transaction was revoked, we also have to update
`fail_unbroadcast_htlcs` to check the payment hash and amount when
the `HTLCSource` is `None`.

Somewhat surprisingly, several tests actually explicitly tested for
the old behavior, which required amending to pass with the new
changes.

Finally, this adds a debug assertion when writing `ChannelMonitor`s
to ensure `HTLCSource`s do not leak.
2022-06-21 16:14:55 +00:00
Matt Corallo
90541c2690
Merge pull request #1527 from wpaulino/update-htlc-relay-policy
Expose API to update a channel's ChannelConfig
2022-06-21 09:02:29 -07:00