Commit graph

4384 commits

Author SHA1 Message Date
Matt Corallo
ec68f1326d Track a reference to scoring parameters in DirectedChannelLiquidity
This simplifies adding additional half lives in
DirectedChannelLiquidity by simply storing a reference to the full
parameter set rather than only the single current half-life.
2022-10-06 21:10:23 +00:00
Matt Corallo
6852ea9741 Calculate a new penalty based on historical channel liquidity range
Our current `ProbabilisticScorer` attempts to build a model of the
current liquidity across the payment channel network. This works
fine to ignore channels we *just* tried to pay through, but it
fails to remember patterns over longer time horizons.

Specifically, there are *many* channels within the network that are
very often either fully saturated in one direction, or are
regularly rebalanced and rarely saturated. While our model may
discover that, when it decays its offsets or if there is a
temporary change in liquidity, it effectively forgets the "normal"
state of the channel.

This causes substantially suboptimal routing in practice, and
avoiding discarding older knowledge when new datapoints come in is
a potential solution to this.

Here, we implement one such design, using the decaying buckets
added in the previous commit to calculate a probability of payment
success based on a weighted average of recent liquidity estimates
for a channel.

For each min/max liquidity bucket pair (where the min liquidity is
less than the max liquidity), we can calculate the probability that
a payment succeeds using our traditional `amount / capacity`
formula. From there, we weigh the probability by the number of
points in each bucket pair, calculating a total probability for the
payment, and assigning a penalty using the same log-probability
calculation used for the non-historical penalties.
2022-10-06 21:10:23 +00:00
Matt Corallo
c5eab6e11f Track history of where channel liquidities have been in the past
This introduces two new fields to the `ChannelLiquidity` struct -
`min_liquidity_offset_history` and `max_liquidity_offset_history`,
both an array of 8 `u16`s. Each entry represents the proportion of
time that we spent with the min or max liquidity offset in the
given 1/8th of the channel's liquidity range. ie the first bucket
in `min_liquidity_offset_history` represents the proportion of time
we've thought the channel's minimum liquidity is lower than 1/8th's
the channel's capacity.

Each bucket is stored, effectively, as a fixed-point number with
5 bits for the fractional part, which is incremented by one (ie 32)
each time we update our liquidity estimates and decide our
estimates are in that bucket. We then decay each bucket by
2047/2048.

Thus, memory of a payment sticks around for more than 8,000
data points, though the majority of that memory decays after 1,387
data points.
2022-10-06 21:10:23 +00:00
Matt Corallo
5d0deacc3d Correct the directionality of liquidity non-update messages
When we log liquidity updates, if we decline to update anything as
the new bounds are already within the old bounds, the
directionality of the log entries was reversed.

While we're at it, we also log the old values.
2022-10-06 17:54:28 +00:00
Matt Corallo
f9acac8fec Correct rapid-gossip-sync no-std build and test
While `rapid-gossip-sync` recently gained a `no-std` feature, it
didn't actually work, as there were still dangling references to
`std` and prelude assumptions. This makes `rapid-gossip-sync`
build (and test) properly in `no-std`.
2022-10-06 17:51:12 +00:00
Matt Corallo
7544030bb6
Merge pull request #1106 from TheBlueMatt/2021-10-no-perm-err-broadcast
Do not broadcast commitment txn on Permanent mon update failure
2022-09-29 22:02:07 +00:00
Matt Corallo
52934e0fff Clarify ambiguous comment in persist methods 2022-09-29 20:27:53 +00:00
Matt Corallo
f961daef33 Rename APIError::MonitorUpdateFailed to MonitorUpdateInProgress
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
2022-09-29 20:27:53 +00:00
Matt Corallo
fb0a481696 Rename handle_monitor_err!() handle_monitor_update_res! 2022-09-29 20:27:53 +00:00
Matt Corallo
721a858533 Rename Channel::monitor_update_failed to monitor_updating_paused 2022-09-29 20:27:53 +00:00
Matt Corallo
72416b951e Add a TODO for an important issue for making async mon updates safe
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
2022-09-29 20:27:53 +00:00
Matt Corallo
12fa0b11a6 Rework chain::Watch return types to make async updates less scary
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
 (a) persist the monitor update immediately and return success,
 (b) fail to persist the monitor update immediately and return
     failure,
 (c) return a flag indicating the monitor update is in progress and
     will complete in the future.

(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.

Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
2022-09-29 20:27:53 +00:00
valentinewallace
7bc52aa62c
Merge pull request #1740 from TheBlueMatt/2022-09-invoice-bindings-nostd
Don't make references to `std` in `lightning-invoice` in bindings
2022-09-26 09:22:01 -04:00
valentinewallace
ed7e30e16c
Merge pull request #1742 from tnull/2022-09-remove-done-todo
Remove completed TODO
2022-09-26 09:19:41 -04:00
Matt Corallo
54fa6280cf Don't make references to std in lightning-invoice in bindings
As we support `no-std` for `lightning-invoice` builds, we should
support them in `c_bindings` as well, which we add a test for in
CI here.
2022-09-26 10:39:14 +00:00
Elias Rohrer
5f386bedaf
Remove done TODO
The migration to `LockTime` was already done in
7e05623bef, which however did not also
remove the TODO.
2022-09-26 11:20:50 +02:00
Matt Corallo
969574c3bf
Merge pull request #1737 from TheBlueMatt/2022-09-future-trait
Add a bindings-only version of `Future::register_callback`
2022-09-25 07:51:40 +00:00
Matt Corallo
1beb3bb421 Add a bindings-only version of Future::register_callback
While we could, in theory, add support to the bindings logic to map
`Box<dyn Trait>`, there isn't a whole lot of use doing so when its
incredibly trivial to do directly.

This adds a trivial wrapper around `Future::register_callback` that
is only built in bindings and which is linked in the
`register_callback` docs for visibility.
2022-09-23 21:12:04 +00:00
valentinewallace
3b7859f496
Merge pull request #1734 from TheBlueMatt/2022-09-better-lock-name
Rename `MultiThreadedLockableScoreLock` `MultiThreadedScoreLock`
2022-09-23 13:05:17 -04:00
Matt Corallo
ba021f80c0 Rename MultiThreadedLockableScoreLock to MultiThreadedScoreLock
as the first is quite a mouthful, and the second isn't materially
less specific.
2022-09-23 15:57:48 +00:00
valentinewallace
5211bfd3f6
Merge pull request #1733 from TheBlueMatt/2022-09-downgrade-hashbrown
Downgrade `hashbrown` to meet MSRV
2022-09-22 12:48:39 -04:00
valentinewallace
c614a2755b
Merge pull request #1731 from valentinewallace/2022-09-router-test-utils-mod
Move router test utils into their own module
2022-09-22 11:51:47 -04:00
Matt Corallo
2e7d924d9b Downgrade hashbrown to meet MSRV
`hashbrown` depends on `ahash` which depends on `once_cell`. Sadly,
in https://github.com/matklad/once_cell/issues/201 the `once_cell`
maintainer decided they didn't want to do the work of having an
MSRV policy for `once_cell`, making `ahash`, and thus `hashbrown`
require the latest compiler. I've reached out to `ahash` to suggest
they drop the dependency (as they could trivially work around not
having it), but until then we simply downgrade `hashbrown`.

`rust-bitcoin` also requires an older `hashbrown` so we're actually
reducing our total `no-std` code here anyway.
2022-09-22 15:06:42 +00:00
Valentine Wallace
95a4173b66
Import util::test_utils as ::ln_test_utils in router
To disambiguate between util::test_utils and router::test_utils
2022-09-21 15:09:55 -04:00
Valentine Wallace
feabd61bfe
Rename routing::router::test_utils to ::bench_utils
To disambiguate it from routing::test_utils
2022-09-20 10:08:57 -04:00
Matt Corallo
af03788db0
Merge pull request #1656 from ViktorTigerstrom/2022-08-forward-htlcs-as-standalone-lock
Move `forward_htlcs` into standalone lock
2022-09-20 10:19:32 +00:00
Valentine Wallace
615b401f7b
Move router test utils into their own module
Useful for testing onion message pathfinding in upcoming PRs and any future
routing modules we add
2022-09-19 14:16:37 -04:00
Viktor Tigerström
fc12d43856 Add lock order docs to ChannelManager fields 2022-09-19 18:43:24 +02:00
Matt Corallo
d6988989f8
Merge pull request #1729 from TheBlueMatt/2022-09-lockablescore-bindings
Add a `MutexGuard` wrapper for the bindings-only `LockableScore`
2022-09-19 14:45:07 +00:00
Jeffrey Czyz
18ac915456
Merge pull request #1728 from TheBlueMatt/2022-09-slices-not-vecs
Swap `Vec<&RouteHop>` parameters for slices
2022-09-19 08:30:07 -05:00
Matt Corallo
85eb1fde56 Avoid returning a reference to a u64.
In c353c3ed7c an accessor method was
added which returns an `Option<&u64>`. While this allows Rust to
return an 8-byte object, returning a reference to something
pointer-sized is a somewhat strange API.

Instead, we opt for a straight `Option<u64>`, which is sadly
somewhat larger on the stack, but is simpler and already supported
in the bindings generation.
2022-09-19 09:23:26 +00:00
Matt Corallo
4910c7cffe Swap Vec<&RouteHop> parameters for slices
In c353c3ed7c, new scorer-updating
methods were added to the `Router` object, however they were
passed as a `Vec` of references. We use the list-of-references
pattern to make bindings simpler (by not requiring allocations per
entry), however there's no reason prefer to passing a `Vec` over
a slice, given the `Vec` doesn't hold ownership of the objects
anyway.
2022-09-19 09:23:26 +00:00
Matt Corallo
56b07e52aa Add a MutexGuard wrapper for the bindings-only LockableScore
In the bindings, we don't directly export any `std` types. Instead,
we provide trivial wrappers around them which expose only a
bindings-compatible API (and which is code in-crate, where the
bindings generator can see it).

We never quite finished this for `MultiThreadedLockableScore` - due
to some limitations in the bindings generator and the way the
scores are used in `lightning-invoice`, the scoring API ended up
being further concretized in patches for the bindings. Luckily the
scoring interface has been rewritten somewhat, and it so happens
that we can now generate bindings with the upstream code.

The final piece of the puzzle is done here, where we add a struct
which wraps `MutexGuard` so that we can expose the lock for
`MultiThreadedLockableScore`.
2022-09-19 09:21:56 +00:00
Viktor Tigerström
3379f8c492 Remove forward_htlc after channel_state lock order
The `forward_htlc` was prior to this commit only held at the same time
as the `channel_state` lock during the write process of the
`ChannelManager`. This commit removes the lock order dependency, by
taking the `channel_state`lock temporarily during the write process.
2022-09-18 23:13:56 +02:00
Viktor Tigerström
e8854a9ce2 Remove unnecessary aquiring of the channel_state lock 2022-09-18 23:13:56 +02:00
Viktor Tigerström
df12df354e Move forward_htlcs into standalone lock
As we are eventually removing the `channel_state` lock, this commit
moves the `forward_htlcs` map out of the `channel_state` lock, to ease
that process.
2022-09-18 23:13:56 +02:00
valentinewallace
dc28f9bb88
Merge pull request #1725 from TheBlueMatt/2022-09-no-bench-lockorder
Stop building with lockorder debugging in benchmarks
2022-09-16 17:12:49 -04:00
Jeffrey Czyz
ca76d0675b
Merge pull request #1694 from jurvis/jurvis/2022-08-move-scorer-from-router
Move `LockableScore` requirement away from `Router` trait
2022-09-16 14:01:38 -05:00
Jurvis Tan
c353c3ed7c
Move Scorer requirement away from Router trait
We do this to enable users to create routers that do not need a scorer.
This can be useful if they are running a node the delegates pathfinding.

* Move `Score` type parameterization from `InvoicePayer` and `Router` to
`DefaultRouter`
* Adds a new field, `scorer`, to `DefaultRouter`
* Move `AccountsForInFlightHtlcs` to `DefaultRouter`, which we
will use to wrap the new `scorer` field, so scoring only happens in
`DefaultRouter` explicitly.
* Add scoring related functions to `Router` trait that we used to call
directly from `InvoicePayer`.
* Instead of parameterizing `scorer` in `find_route`, we replace it with
inflight_map so `InvoicePayer` can pass on information about inflight
HTLCs to the router.
* Introduced a new tuple struct, InFlightHtlcs, that wraps functionality
for querying used liquidity.
2022-09-16 08:38:56 -07:00
Matt Corallo
070dd74602 Stop building with lockorder debugging in benchmarks
`cargo bench` sets `#[cfg(test)]` so our current checks for
enabling our lockorder debugging end up matching when we're trying
to build performance benchmarks.

This adds explicit checks to our debug_lockorder logic to filter
out `feature = "_bench_unstable"` builds.
2022-09-16 14:44:20 +00:00
Matt Corallo
313810ebdc Do not broadcast commitment txn on Permanent mon update failure
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
2022-09-15 18:18:06 +00:00
Arik
8886d1dc76
Merge pull request #1708 from tnull/2022-09-rgs-unpub-modules
Improve RGS documentation
2022-09-15 09:55:39 -07:00
Elias Rohrer
ad8c955bd4
Add no-std support for RGS 2022-09-15 09:27:34 +02:00
Elias Rohrer
484c0e89f7
Rework RGS crate-level docs 2022-09-15 09:27:31 +02:00
Matt Corallo
48d21bad7b
Merge pull request #1707 from TheBlueMatt/2022-09-no-global-features
Move supported-feature-set logic into the supporting modules
2022-09-15 00:28:32 +00:00
Matt Corallo
9170804ca4 Assert that all defined features are in the known features set
Now that the features contexts track the full set of all known
features, rather than the set of supported features, all defined
features should be listed in the context definition macro.

This adds a compile-time assertion to check that all bits for known
features are set in the context known set.
2022-09-14 20:10:17 +00:00
Matt Corallo
b83e93c799 Stop tracking feature bits as known or required in features.rs
Now that the `*Features::known` constructor has been removed, there
is no reason to define feature bits as either optional required in
`features.rs` - that logic now belongs in the modules that are
responsible for the given features.

Instead, we only list all features in each context.
2022-09-14 20:10:17 +00:00
Matt Corallo
bec8bf10aa Remove the *Features::known constructor
As we move towards specify supported/required feature bits in the
module(s) where they are supported, the global `known` feature set
constructors no longer make sense.

Here we (finally) remove the `known` constructor entirely,
modifying tests in the `features` module as required.
2022-09-14 20:10:17 +00:00
Matt Corallo
b93fe327d2 Remove all remaining references to *Features::known
As we move towards specify supported/required feature bits in the
module(s) where they are supported, the global `known` feature set
constructors no longer make sense.

In anticipation of removing the `known` constructor, this commit
removes all remaining references to it outside of features.rs.
2022-09-14 20:09:36 +00:00
Matt Corallo
f6fa624da0 Stop relying on *Features::known in fuzzing tests
As we move towards specify supported/required feature bits in the
module(s) where they are supported, the global `known` feature set
constructors no longer make sense.

Here we stop relying on the `known` method in our fuzz tests.
2022-09-14 20:09:36 +00:00