Commit graph

4246 commits

Author SHA1 Message Date
Matt Corallo
ada0df2d9f Test a full no-std build in the no-std-check crate in CI
Rust is incredibly forgiving in attempts to access `std`, making it
rather difficult to test `no-std` properly. In practice, the only
decent way to do so is to actually build for a platform that does
not have `std`, which we do here by building the `no-std-check`
crate for `arm-thumbv7m-none-eabi`.
2022-10-07 17:19:12 +00:00
Matt Corallo
4cd9901427
Merge pull request #1625 from TheBlueMatt/2022-07-history-tracking 2022-10-07 16:32:10 +00:00
valentinewallace
d9f5c407de
Merge pull request #1751 from TheBlueMatt/2022-10-fix-score-log-direction
Correct the directionality of liquidity non-update messages
2022-10-07 12:28:23 -04:00
Matt Corallo
5f8d7c5762
Merge pull request #1760 from TheBlueMatt/2022-10-invoice-builder-round 2022-10-07 16:25:25 +00:00
Valentine Wallace
6817762841
Fix outdated README sections 2022-10-07 12:03:02 -04:00
Duncan Dean
63d7b2bf3e
Remove and track perm failed nodes & channels
We never had the `NetworkGraph::node_failed` method implemented. The
scorer now handles non-permanent failures by downgrading nodes, so we
don't want that implemented.

The method is renamed to `node_failed_permanent` to explicitly indicate
that this is the only case it handles. We also add tracking in the form
of two maps as fields of `NetworkGraph`, namely, `removed_nodes` and
`removed_channels`. We track these removed graph entries to ensure we
don't just resync them right away from gossip soon after removing them.
We stop tracking these removed nodes whenever `remove_stale_channels_and_tracking()`
is called and the entries have been tracked for longer than
`REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS` which is currently set to one
week.
2022-10-07 12:00:39 +02:00
Matt Corallo
008da77263 Drop the subsecond part of timestamps when constructing invoices
We had a user who was confused why an invoice failed to round-trip
(i.e. was not `PartialEq` with itself after write/read) when a
subsecond creation time was used (e.g. via the `from_system_time`
constructor).

This commit should address this confusion by dropping subsecond
parts in easily-accessible constructors when creating BOLT 11
invoices.

Fixes #1759.
2022-10-07 02:05:46 +00:00
Matt Corallo
92963ebe52 Support platforms with only 32-bit atomics
Given there is only one instance in our code of `AtomicU64` its
simplest to just remove it rather than try to add some kind of
wrapper.
2022-10-06 23:59:30 +00:00
Matt Corallo
e2e8847254 Test rapid-gossip-sync in no-std in CI 2022-10-06 23:59:30 +00:00
Matt Corallo
3852d52801 Update rapid-gossip-sync docs to not reference std-only methods 2022-10-06 23:59:30 +00:00
Matt Corallo
cf3471f8ad Fix (and test) Future creation after a Notifier was notified
After a `Notifier` has been `notify`'d, attempts to `get_future`
should return a `Future` which is pre-completed, however this was
not the case.

This commit simply fixes the behavior, adding a test to demonstrate
the issue.
2022-10-06 23:54:52 +00:00
Matt Corallo
c8fb859ad6 Decay historical liquidity tracking when no new data is added
To avoid scoring based on incredibly old historical liquidity data,
we add a new half-life here which is used to (very slowly) decay
historical liquidity tracking buckets.
2022-10-06 21:10:23 +00:00
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