Commit graph

8865 commits

Author SHA1 Message Date
Jeffrey Czyz
8da348e376 Expand macro to add compile-time type check 2025-02-25 18:40:46 +05:30
Matt Corallo
c9fd3a5a1e
Merge pull request #3616 from TheBlueMatt/2025-02-scoring-overflow
Fix overflow in historical scoring model point count summation
2025-02-24 19:39:04 +00:00
Matt Corallo
7378e06313
Merge pull request #3614 from TheBlueMatt/2025-02-rustfmt-wire-types 2025-02-24 17:07:43 +00:00
Matt Corallo
43d0964474 Fix overflow in historical scoring model point count summation
In adb0afc523 we started raising
bucket weights to the power four in the historical model. This
improved our model's accuracy greatly, but resulted in a much
larger `total_valid_points_tracked`. In the same commit we
converted `total_valid_points_tracked` to a float, but retained the
64-bit integer math to build it out of integer bucket values.

Sadly, 64 bits are not enough to sum 1024 bucket pairs of 16-bit
integers multiplied together and then squared (we need 16*4 + 10 =
74 bits to avoid overflow). Thus, here we replace the summation
with 128-bit integers.
2025-02-23 03:00:21 +00:00
Matt Corallo
136cac6387 Use a rustdoc link for URL in public features documentation
...addressing a warning that rustc always prints when generating
docs.
2025-02-21 23:59:56 +00:00
Matt Corallo
b167aabaab Remove lightning/src/ln/types.rs from rustfmt exclusions 2025-02-21 23:59:56 +00:00
Matt Corallo
36f713dfeb Run rustfmt on lightning/src/ln/types.rs 2025-02-21 23:59:56 +00:00
Matt Corallo
2ffcdf9d7f Clean up types.rs hex deser to use more intermediate variables
...preventing rustfmt from making a mockery of our code.
2025-02-21 23:59:56 +00:00
Matt Corallo
a979d08e99 Remove lightning/src/ln/wire.rs from rustfmt exclusions 2025-02-21 23:57:20 +00:00
Matt Corallo
88a2fb6e59 Run rustfmt on lightning/src/ln/wire.rs 2025-02-21 23:57:20 +00:00
Matt Corallo
4b62c412a6 Reduce overuse of where clauses in wire.rs
rustfmt blows up `where` clauses even for trivial lines, so we
should generally avoid them where possible. Here we remove some
extra ones from `wire.rs`.
2025-02-21 23:11:02 +00:00
Matt Corallo
c9a7bfe40f
Merge pull request #3487 from wvanlint/anchor_channel_reserves
Define anchor channel reserve requirements
2025-02-21 22:06:22 +00:00
Matt Corallo
81f8b6786c
Merge pull request #3572 from tankyleo/25-01-htlc-dust-exposure
Tweak htlc dust exposure due to excess fees
2025-02-21 20:46:54 +00:00
Matt Corallo
609f89de43
Merge pull request #3588 from wpaulino/quiescence
Implement quiescence protocol
2025-02-21 14:52:31 +00:00
Leo Nash
f931db5572 Test the accounting of dust exposure due to excess fees
This test checks to a 1msat precision the accounting of dust exposure
due to excess fees on counterparty commmitment and htlc transactions,
for both inbound and outbound htlcs.
2025-02-21 01:31:00 +00:00
Leo Nash
8ed5a2345e Assert dust exposure exhaustion in the excess fees are dust test
The payments in this test previously failed for reasons other
than exhausting the dust exposure limit with excess fees. Upon payment
failures, we now check the logs to assert failures due to dust
exposure exhaustion.
2025-02-21 01:28:33 +00:00
Leo Nash
177e51d25a For the candidate outbound htlc, sum weights, then sum fees
Previously, we calculated the fee of the commitment transaction with n
htlcs, and the fee due to the candidate htlc, rounded the two fees to
the lower satoshi, and then summed the fees. This is not equal to how
fees of commitment transactions are calculated, which is to add up the
total weight of the (n+1) htlc commitment transaction, convert to fee,
then round to the lower satoshi.

This commit corrects this delta by running the full fee calculation
twice, once for the n htlc, and once for the (n+1) htlc counterparty
commitment transactions.
2025-02-21 01:28:33 +00:00
Leo Nash
86ffea1c39 Remove extra sum of tx fee dust on the counterparty tx dust exposure
Previously, `get_pending_htlc_stats` did not account for the inbound
htlc because `can_accept_incoming_htlc` was called before the htlc was
irrevocably committed. But after commit d8d9dc7,
`can_accept_incoming_htlc` is called only when the htlc is irrevocably
committed, hence `get_pending_htlc_stats` does account for the inbound
htlc.

Nonetheless, in the case of a non-dust htlc, our calculation of the
counterparty tx dust exposure still assumed that
`get_pending_htlc_stats` did not account for the inbound htlc, causing
us to add the dust exposure due to that inbound htlc twice. This commit
removes this extra sum.
2025-02-21 01:28:33 +00:00
Wilmer Paulino
570ddaeb89
Add fuzzing coverage for quiescence 2025-02-20 10:22:25 -08:00
Wilmer Paulino
3b758e7ec3
Enforce disconnect timeout during quiescence
Since new updates are not allowed during quiescence (local updates enter
the holding cell), we want to ensure quiescence eventually terminates if
the handshake takes too long or our counterparty is uncooperative.
Disconnecting implicitly terminates quiescence, so the holding cell can
be freed upon re-establishing the channel (assuming quiescence is not
requested again).
2025-02-20 10:22:20 -08:00
Wilmer Paulino
c0e01290fd
Track message timeout ticks based on internal states
With the introduction of `has_pending_channel_update`, we can now
determine whether any messages are owed to irrevocably commit HTLC
updates based on the current channel state. We prefer using the channel
state, over manually tracking as previously done, to have a single
source of truth. We also gain the ability to expect to receive multiple
messages at once, which will become relevant with the quiescence
protocol, where we may be waiting on a counterparty `revoke_and_ack` and
`stfu`.
2025-02-20 10:22:17 -08:00
Wilmer Paulino
20877b3e22
Consider quiescence state when freeing holding cells upon revoke_and_ack
We previously would avoid freeing our holding cells upon a
`revoke_and_ack` if a monitor update was in progress, which we checked
explicitly. With quiescence, if we've already sent `stfu`, we're not
allowed to make further commitment updates, so we must also avoid
freeing our holding cells in such cases.

Along the way, we also remove the special handling of in-progress
monitor updates now that it behaves the same as the handling of being
quiescent.
2025-02-20 10:22:16 -08:00
Wilmer Paulino
99670ecd0e
Implement quiescence protocol
Quiescence is a new protocol feature that allows for channels to undergo
"fundamental" changes (i.e., protocol upgrade) while there are no
pending updates on either side. Its first use case will be to carry out
channel splices, to ensure new HTLC/fee updates are not made while a
splice is being negotiated.

Each side of the channel is allowed to send a `stfu` message if any of
their outbound updates are not pending for either side (i.e.,
irrevocably committed on both commitment transactions). Once both sides
exchange `stfu`, the channel becomes quiescent. A message timeout is
enforced during the quiescence handshake to ensure we can eventually
re-establish the channel and propose new HTLC/fee updates again.

Several new state flags have been added to `ChannelState::ChannelReady`
to track the progress of the quiescence handshake. Once the channel
becomes quiescent, all flags related to the handshake are cleared, and
the `QUIESCENT` flag is enabled. While quiescence is not a persistent
protocol (it implicitly terminates upon peer disconnection), and updates
cannot be made, we still need to track `MONITOR_UPDATE_IN_PROGRESS` as
it may be required by the quiescence-dependent protocol, like in the
case of splicing.
2025-02-20 10:22:15 -08:00
Wilmer Paulino
506367e8a9
Add ChannelError::WarnAndDisconnect variant
The existing `ChannelError::Warn` variant only sends the warning and
does not disconnect. There are certain cases where we want to just send
a warning, and other cases where we want to also disconnect, so we
keep both variants around.
2025-02-20 10:22:12 -08:00
Wilmer Paulino
5d6e759613
Change Stfu::initiator to bool type 2025-02-20 10:22:08 -08:00
Wilmer Paulino
6cf270dbf5
Merge pull request #3592 from jkczyz/2025-02-channel-funding-scope
Refactor `ChannelContext` value fields into `FundingScope`
2025-02-20 10:03:28 -08:00
Jeffrey Czyz
79ce104e68
Move next_*_commitment_tx_fee_info_cached to FundingScope 2025-02-20 08:47:16 -06:00
Jeffrey Czyz
2d452fba1c
Move *_max_commitment_tx_output to FundingScope 2025-02-20 08:47:16 -06:00
Jeffrey Czyz
6bfb5fb028
Move *_selected_channel_reserve_satoshis to FundedScope 2025-02-20 08:47:16 -06:00
Jeffrey Czyz
5c7cf11d34
Move value_to_self_msat to FundingScope 2025-02-20 08:47:15 -06:00
Jeffrey Czyz
770f90ee3e
Move channel_value_satoshis to FundingScope 2025-02-20 08:47:15 -06:00
Matt Corallo
ef8868b495
Merge pull request #3607 from arik-so/arik/trampoline/inbound-prefactors-03
Move shared secret calculation into decode_next_payment_hop
2025-02-20 01:39:07 +00:00
Matt Corallo
cdc8e2130d
Merge pull request #3596 from joostjager/inbound-channel-config-override
Allow to override config defaults for inbound channels on a per-channel basis
2025-02-19 23:05:59 +00:00
Arik Sosman
5291445ad1
Calculate shared secret within hop decode function
For Trampoline, we'll need to keep track of both the outer and inner
onion's shared secrets. To this end, we're moving the secret
calculation inside `decode_next_payment_hop` such that, when applicable,
it can return both.
2025-02-19 13:22:22 -08:00
Arik Sosman
1518e67639
Clean up Hop match arms
Essentially a follow-up to 38284a0d, deduplicating some additional code.
2025-02-19 09:39:39 -08:00
Joost Jager
02861dde01 Allow config defaults override for inbound channels
This commit introduces a config override struct parameter to the
accept_inbound_channel methods. With manual channel acceptance enabled,
users can modify the default configuration as needed.
2025-02-19 17:17:50 +01:00
Joost Jager
2a3e0021a3 Allow channel update for accept_underpaying_htlcs
Extends partial channel updates to optionally include the
accept_underpaying_htlcs flag.
2025-02-19 13:19:13 +01:00
Willem Van Lint
7354ebee7a Define anchor channel reserve requirements
This change defines anchor reserve requirements by calculating weights
and fees for the transactions that need to be confirmed on-chain in the
event of a unilateral closure. The calculation is given a set of
parameters as input, including the expected fee rate and number of
in-flight HTLCs.
2025-02-18 23:14:51 -08:00
Jeffrey Czyz
c7aa6b770e
Introduce a channel FundingScope
When establishing a channel, the funding transaction may be replaced
either:
- after the funding transaction has confirmed using splicing,
- before the funding transaction has confirmed for v2 channel
  establishment using tx_init_rbf, or
- before the splice's funding transaction has confirmed using
  tx_init_rbf.

In each of these cases, fields in ChannelContext will need to be updated
once the funding transaction confirms. Additionally, the same fields for
a pending attempt may need to be considered instead of a previously
confirmed funding.

This commit introduces a FundingScope to hold the aforementioned fields.
It lives next to ChannelContext and will be needed whenever these fields
are accessed. The next few commits will move the relevant fields to
FundingScope and provide access to them whenever needed, allowing to
swap in another FundingScope when necessary.
2025-02-18 14:10:10 -06:00
Matt Corallo
2d2c542a90
Merge pull request #3605 from vincenzopalazzo/macros/fix-warnings
[RFC] remove the cfg test that it is not needed in a error code path
2025-02-16 16:32:28 +00:00
Vincenzo Palazzo
0a5a7d9045 remove the cfg test that it is not needed
looking at the code, the #[cfg(not(test))] is not needed, so I remove it
because the CI is failing for some reason on the following code path
with rustc 1.63

     Running `rustc --crate-name lightning --edition=2021 lightning/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --emit=dep-info,link -C opt-level=1 -C lto=off -C embed-bitcode=no -C debuginfo=2 -C debug-assertions=on --test --cfg 'feature="default"' --cfg 'feature="grind_signatures"' --cfg 'feature="std"' -C metadata=7f2d308b97eed1fd -C extra-filename=-7f2d308b97eed1fd --out-dir /home/runner/work/rust-lightning/rust-lightning/target/debug/deps -C incremental=/home/runner/work/rust-lightning/rust-lightning/target/debug/incremental -L dependency=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps --extern bech32=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/libbech32-16d69ba236ca57d8.rlib --extern bitcoin=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/libbitcoin-eca0fe041fbfc4dc.rlib --extern dnssec_prover=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/libdnssec_prover-1dd31ceabfa87dd5.rlib --extern hashbrown=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/libhashbrown-d9958110f8162c77.rlib --extern libm=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/liblibm-274dde3847e6cc8d.rlib --extern lightning_invoice=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/liblightning_invoice-56ed15b130ee9288.rlib --extern lightning_macros=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/liblightning_macros-528cdf9e072c4caf.so --extern lightning_types=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/liblightning_types-2a1be369ae37a72c.rlib --extern possiblyrandom=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/libpossiblyrandom-5fb85e240a23737f.rlib --extern regex=/home/runner/work/rust-lightning/rust-lightning/target/debug/deps/libregex-187d4eaedfe307ba.rlib -D warnings -L native=/home/runner/work/rust-lightning/rust-lightning/target/debug/build/bitcoinconsensus-9dfaf2ad2edfe5f6/out -L native=/home/runner/work/rust-lightning/rust-lightning/target/debug/build/bitcoinconsensus-9dfaf2ad2edfe5f6/out -L native=/home/runner/work/rust-lightning/rust-lightning/target/debug/build/secp256k1-sys-44754c91469ec6ec/out`
error: constant `MAX_PEER_STORAGE_SIZE` is never used
    --> lightning/src/ln/channelmanager.rs:2882:1
     |
2882 | const MAX_PEER_STORAGE_SIZE: usize = 1024;
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: `-D dead-code` implied by `-D warnings`

error: could not compile `lightning` due to previous error

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2025-02-16 14:42:47 +01:00
Matt Corallo
bce5db7f2e
Merge pull request #3575 from adi2011/peer-storage/channel-manager
PeerStorage: Add feature and store peer storage in ChannelManager
2025-02-15 20:26:54 +00:00
Matt Corallo
ec19ba1db2
Merge pull request #3602 from jkczyz/2025-02-unset-funding-info
Fix debug panic in `full_stack` fuzz test
2025-02-14 22:30:39 +00:00
Matt Corallo
1613f87966
Merge pull request #3598 from arik-so/arik/trampoline/inbound-prefactors-02
Eliminate invalid Hop/InboundOnionPayload combinations
2025-02-14 22:01:48 +00:00
Aditya Sharma
6c8e7e40bd Add test for peer storage handling
This test ensures that PeerStorage & PeerStorageRetrieval handling behaves as expected. It simulates
receiving a dummy peer storage message, disconnecting and reconnecting peers, and
validates that the correct messages are exchanged during reestablishment.

- Added a test case `test_peer_storage` to verify the handling of `PeerStorageMessage`
  and the validation of warning messages in the event of invalid peer storage retrieval.

- Simulated peer storage retrieval upon reconnection between nodes.

- Validated that a warning message is generated when `PeerStorageRetrievalMessage`
  is received.

- Ensured the warning message contains the expected error description.
2025-02-15 03:31:09 +05:30
Aditya Sharma
068549def9 Handle PeerStorage Message and its Persistence
This commit introduces the handling and persistence of PeerStorage messages on a per-peer basis.
The peer storage is stored within the PeerState to simplify management, ensuring we do not need to remove it
when there are no active channels with the peer.

Key changes include:

 - Add PeerStorage to PeerState for persistent storage.
 - Implement internal_peer_storage to manage PeerStorage and its updates.
 - Add resend logic in peer_connected() to resend PeerStorage before sending the channel reestablish message upon reconnection.
 - Update PeerState's write() and read() methods to support PeerStorage persistence.
2025-02-15 03:31:09 +05:30
Aditya Sharma
81e89d82fd Add Message Handlers for Peer Storage
Introduce message types and handlers to enable the exchange of peer storage data between nodes.
PeerStorageMessage: Used to send encrypted backups to peers.
PeerStorageRetrievalMessage: Used to return a peer's stored data upon reconnection.

- Define two new message types: PeerStorageMessage and PeerStorageRetrievalMessage.
- Implement handlers for these messages in ChannelMessageHandler.
- Add SendPeerStorageMessage and SendPeerStorageRetrievalMessage to MessageSendEvent.
2025-02-15 01:17:07 +05:30
Aditya Sharma
5a63b2c1d6 Add 'ProvideStorage' to 'InitContext' and 'NodeContext'
Introduce the 'ProvideStorage' feature to enable nodes to distribute
and store peer storage backups for channel partners. This functionality enhances
resilience by ensuring critical peer data is backed up and can be retrieved if needed.

- Added 'ProvideStorage' to the 'InitContext' & 'NodeContext'.
- Set feature bit for this feature inside 'provide_init_features()'
2025-02-15 01:17:07 +05:30
Jeffrey Czyz
9164f7e13f
Add debug assertion in Channel::funding_signed
This is a sanity check that ChannelPhase and ChannelState do not go out
of sync.
2025-02-14 12:18:52 -06:00
Arik Sosman
38284a0d5d
Remove invalid state options from Hop
Now that each `InboundOnionPayload` variant corresponds to its own
struct, we can reference these same types inside `Hop` and thereby
avoid nesting that allowed invalid combinations, and instead store
supplemental data as each variant calls for.
2025-02-14 10:09:54 -08:00