Jeffrey Czyz [Thu, 2 May 2024 16:43:04 +0000 (11:43 -0500)]
Only require description when offer has an amount
The spec was changed to allow excluding an offer description if the
offer doesn't have an amount. However, it is still required when the
amount is set.
Matt Corallo [Tue, 30 Apr 2024 21:30:26 +0000 (21:30 +0000)]
Wake `background-processor` from `ChainMonitor` on new blocks
When we receive a new block we may generate
`Event::SpendableOutputs` in `ChannelMonitor`s which then need to
be processed by the background processor. While it will do so
eventually when its normal loop goes around, this may cause user
tests to be delayed in finding events, so we should notify the BP
immediately to wake it on new blocks.
We implement that here, unconditionally notifying the
`background-processor` whenever we receive a new block or confirmed
transactions.
Matt Corallo [Wed, 1 May 2024 15:49:59 +0000 (15:49 +0000)]
Move `[u8; 32]` wrapper types to a common module
The `PaymentHash`, `PaymentSecret`, `PaymentPreimage`, and
`ChannelId` types are all small wrappers around `[u8; 32]` and are
used throughout the codebase but were defined in the top-level
`ln/mod.rs` file and the relatively sparsely-populated
`ln/channel_id.rs` file.
Here we move them to a common `types` module and go ahead and
update all our in-crate `use` statements to refer to the new
module for bindings. We do, however, leave a `pub use` alias for
the old paths to avoid upgrade hassle for users.
Matt Corallo [Wed, 1 May 2024 15:19:20 +0000 (15:19 +0000)]
Add bindings no-export tags to doc test types
We don't actually intend these to be public as they're just for
docs but the bindings don't currently parse `#[doc(hidden)]` as
"no-export" so we add manual no-export tags as well.
Matt Corallo [Tue, 23 Apr 2024 15:56:13 +0000 (15:56 +0000)]
Rename and expose message-specific `NextHop`
`onion::message::messenger::PeeledOnion` is a public enum which
included the private enum `NextHop`, which is not acceptable. Thus,
we here expose `NextHop` but rename it `NextMessageHop` to make
clear that it is specific to messages.
Matt Corallo [Tue, 30 Apr 2024 13:45:45 +0000 (13:45 +0000)]
Fix new rustc `#[macro_export]` warning in doctests
rustc now warns any time a `#[macro_export]` is used inside a
function as it generates surprising results. Because doctests are
run inside implicit test functions this means any use of
`#[macro_export]` inside a doctest will now warn. We do this in
`lightning-custom-message` to demonstrated how to use the crate,
which now fails to compile.
Here we fix this by adding an `fn main() {}` to the doctest, which
causes doctests to be compiled as freestanding code rather than
inside a test function.
Note that while discussing this upstream it came up that rustc is
also planning on changing the way doctests are compiled to compile
an entire crate's doctests in one go rather than in separate
crates, causing doctests to have a shared namespace which may
generate future surprising outcomes.
Matt Corallo [Mon, 29 Apr 2024 21:05:52 +0000 (21:05 +0000)]
Send peers error messages for failures due to invalid batch funding
If we fail to fund a batch open we'll force-close all channels in
the batch, however would previously fail to send error messages to
our peers for any channels we were due to test after the one that
failed.
This commit fixes that issue, sending the required error messages
to allow our peers to clean up their state.
Matt Corallo [Mon, 29 Apr 2024 18:12:59 +0000 (18:12 +0000)]
Close channels when `find_funding_output` fails to find an output
In `funding_transaction_generated_intern`, if `find_funding_output`
fails (e.g. due to the require output not being present in the
provided funding transaction) we'd previously not generated a
`ChannelClosed` event which leaves users possibly in a confused
state.
Here we fix this, also fixing the relevant tests to check for the
new event.
InvoiceRequestFeatures may contain a large, odd bit. Including this in
InvoiceRequestFields, which is in each BlindedPath of a Bolt12Invoice,
could cause the invoice's onion message to exceed the maximum size. The
features are already checked before sending an invoice.
Event::PaymentClaimable and Event::PaymentClaimed already contain the
paid amount, so there's no need to included the requested amount in
InvoiceRequestFields.
PaymentContext is already stored in ClaimablePayment via PaymentPurpose,
so there is no need to repeat it in each ClaimableHTLC via OnionPayload.
This avoids cloning the PaymentContext each time an HTLC is received,
other than for the first HTLC for a payment.
Refunds currently require a description, though this may change to be
optional. Remove the description requirement from the API, setting and
empty string by default.
Offers currently require a description, though this may change to be
optional. Remove the description requirement from the API, setting and
empty string by default.
When parsing a Bolt12Invoice use both the Offer's signing_pubkey and
paths to determine if it is for an Offer or a Refund. Previously, an
Offer was required to have a signing_pubkey. But now that it is
optional, the Offers paths can be used to make the determination.
Additionally, check that the invoice matches one of the blinded node ids
from the paths' last hops.
Instead of reusing OfferTlvStream::paths, add a dedicated paths TLV to
InvoiceRequestTlvStream such that it can be used in Refund. This allows
for an Offer without a signing_pubkey and still be able to differentiate
whether an invoice is for an offer or a refund.
If an offer has at least one path, it may omit the signing pubkey and
use the blinded node id of the last hop of a path to sign an invoice.
Allow parsing such offers but not yet creating them.
If an Offer contains a path, the blinded_node_id of the path's final hop
can be used as the signing pubkey. Make Offer::signing_pubkey and
OfferContents::signing_pubkey return an Option to support this. Upcoming
commits will implement this behavior.
Matt Corallo [Thu, 25 Apr 2024 14:30:05 +0000 (14:30 +0000)]
Drop completed blocked `ChannelMonitorUpdate`s on startup
If a user receives a payment preimage for an outbound payment, the
`PaymentSent` event will block any eventual RAA
`ChannelMonitorUpdate` from the same channel, assuming it comes in
before the event can be processed. If this blocking kicks in, but
the flow eventually completes with the RAA `ChannelMonitorUpdate`
being persisted, but the `ChannelManager` is only persisted prior
to the event being handled, on startup we'll have a fully
up-to-date `ChannelMonitor` but a pending, blocked
`ChannelMonitorUpdate`. When the `PaymentSent` event is replayed
we'll end up trying to apply a redundant `ChannelMonitorUpdate`
which will panic.
See the test added in this commit for an implementation of this
situation.
In this commit we fix this issue by simply dropping blocked
`ChannelMonitorUpdate`s the same as we do pending ones.
Matt Corallo [Mon, 22 Apr 2024 13:03:14 +0000 (13:03 +0000)]
Drop the rustup-specific calling in `ci/rustfmt.sh`
The +rustversion call semantics are specific to rustup and do not
work with standard rust toolchains. However, because rustfmt
formatting differs slightly between stable and our 1.63 target, we
need to keep the +1.63.0 for rustup users.
Thus, here, we check for the presence of a `rustup` command and
pass the "+1.63.0" argument if we find one.
Matt Corallo [Mon, 22 Apr 2024 12:50:58 +0000 (12:50 +0000)]
Only include `derive_add_tweak` for base key types that use it
Specifically `RevocationBasepoint` has a different derivation, so
shouldn't have a `derive_add_tweak` at all. We also use this
opportunity to link to the `from_basepoint` function in the
`derive_add_tweak` docs.
Matt Corallo [Fri, 19 Apr 2024 00:36:52 +0000 (00:36 +0000)]
Use `Sha256`s for tweaks in `sign` to enforce randomness
We assume that tweaks are the output of a SHA-256 hash function
(and thus that failing to create a private key from the has
negligible probability) in `add_public_key_tweak` and elsewhere.
Thus, we really shouldn't be taking byte arrays in the public API
but rather `Sha256` objects, and communicating in the docs for
`add_public_key_tweak` that we can panic if its not the output of
a hash function, both of which we do here.
Matt Corallo [Thu, 11 Apr 2024 21:53:44 +0000 (21:53 +0000)]
Debug-log monitor update starts and completions in `ChainMonitor`
When a `ChannelMonitor[Update]` persistence completes, we rely on
logging in `ChannelManager` to hear about it. However, this won't
happen at all if there's still pending updates as no `MonitorEvent`
will be generated.
Thus, here, we add logging directly in `ChainMonitor`, ensuring we
can deduce when individual updates completed from debug logs.
olegkubrakov [Tue, 19 Dec 2023 00:54:59 +0000 (16:54 -0800)]
Implement Script for Witness and Add Tweak in PSBT.
Adding Witness Script and key tweaks makes
a Partially Signed Bitcoin Transaction the single data
source needed for a Signer to produce valid signatures.
A Signer is not required to be able to generate L2 keys,
e.g delayed payment basepoint.
Archives fully resolved channel monitors by adding them to a backup
location and removing them from the primary storage & the monitor set.
This is useful for pruning fully resolved monitors from the monitor
set and primary storage so they are not reloaded on every new new
block connection.
We also add a new function, `archive_persisted_channel` to the
`Persist` trait that writes the monitor to an archive storage and
removes it from the primary storage.
Elias Rohrer [Fri, 12 Jan 2024 16:46:37 +0000 (17:46 +0100)]
Add basic `OutputSweeper` test in BP
.. we simply check that the `OutputSweeper` generates a spending tx and
that the `TrackedSpendableOutput` is pruned once it reaches
`ANTI_REORG_DELAY`.
Elias Rohrer [Fri, 12 Jan 2024 10:36:08 +0000 (11:36 +0100)]
Add `OutputSweeper` utility persisting and sweeping spendable outputs
We add a `OutputSweeper` utility that allows to track the state of
spendable output descriptors as emitted by `Event::SpendableOutputs`.
To this end, the `OutputSweeper` persists the necessary information in
our `KVStore` and regularly tries to sweep the spendable outputs,
removing them after reaching threshold confirmations, i.e.,
`ANTI_REORG_DELAY`.
Jeffrey Czyz [Fri, 29 Mar 2024 02:31:10 +0000 (21:31 -0500)]
Include InvoiceRequest fields in PaymentContext
When receiving a payment, it's useful to know information about the
InvoiceRequest. Include this data in PaymentContext::Bolt12Offer so
users can display information about an inbound payment (e.g., the payer
note).
When constructing a PaymentPurpose in ChannelManager, use the
PaymentContext from OnionPayload to determine which variant to
construct, including those for BOLT 12 payments.
In order to provide more context in PaymentClaimable and PaymentClaimed
events, introduce new variants of PaymentPurpose for use with BOLT 12
payments. Separate variants are used for offers and refunds. An unknown
variant is used for backwards compatibility and ease of testing, but is
otherwise not publicly constructable.
Jeffrey Czyz [Tue, 26 Mar 2024 23:57:57 +0000 (18:57 -0500)]
Include Refund context in blinded payment paths
When requesting a payment for a refund, include a context in the
Bolt12Invoice's blinded payment paths indicated it is for such. When the
eventual payment is received, the user can use the payment hash to
correlate it with the corresponding Refund.
Jeffrey Czyz [Tue, 26 Mar 2024 23:48:39 +0000 (18:48 -0500)]
Include Offer context in blinded payment paths
When constructing blinded payment paths for a Bolt12Invoice response,
include the OfferId so that the eventual payment can be correlated with
the Offer.
Jeffrey Czyz [Tue, 26 Mar 2024 23:39:06 +0000 (18:39 -0500)]
Include OfferId in VerifiedInvoiceRequest
Extract the OfferId from the offer metadata sent back in the
InvoiceRequest and include it in VerifiedInvoiceRequest. This can be
used to correspond the eventual payment for the invoice response by
including it in the invoice's blinded payment paths.
Jeffrey Czyz [Mon, 25 Mar 2024 21:10:36 +0000 (16:10 -0500)]
Define an OfferId for BOLT 12 Offers
Use a merkle root hash of the offer TLV records to define an offer id.
Will be included in a BOLT 12 invoice's blinded payment paths in order
for the recipient to identify which offer the payment is for.
Jeffrey Czyz [Tue, 26 Mar 2024 18:10:04 +0000 (13:10 -0500)]
Pass PaymentContext through HTLC processing
PendingInboundPayment::BlindedReceive includes a PaymentContext, which
the recipient includes in each blinded path. Included this when
processing HTLCs in ChannelManager, first into PendingHTLCRouting and
then to OnionPayload. Later, this will be included in the PaymentPurpose
when surfaced to user in PaymentClaimable and PaymentClaimed events.
Jeffrey Czyz [Tue, 26 Mar 2024 14:48:03 +0000 (09:48 -0500)]
Add PaymentContext to payment::ReceiveTlvs
Providing LDK-specific data in a BlindedPath allows for the data to be
received with the corresponding payment. This is useful as it can then
be surfaced in PaymentClaimable events where the user may correlated
with an Offer, for instance. Define a PaymentContext enum for including
various context (e.g., offer, refund, static invoice).
Jeffrey Czyz [Tue, 12 Mar 2024 00:00:40 +0000 (19:00 -0500)]
Return the invoice when requesting a refund
When sending an invoice for a refund, information from the invoice may
be useful for caller. For instance, the payment_hash can be used to
track whether the refund was paid. Return the invoice to facilitate this
use case.
Elias Rohrer [Wed, 17 Apr 2024 09:54:25 +0000 (11:54 +0200)]
Acquire `per_peer_state` `write` lock earlier in `ChannelManager::write`
Previously, we would hold the `per_peer_state` `read` lock during the
serialization of channel objects. While this might have reduced lock
congestion minimally, we also calculated the number of serializable
peers in this phase, only to use it later after dropping and reacquiring
the `per_peer_state` `write` lock.
This could potentially result in inconsistiencies if the
`serializable_peer_count` would change after we dropped the `read` lock
but before we acquired the `write` lock.
To mitigate the issue we just acquire the `write` lock ealier and hold
it for the remainder of `ChannelManager::write`.
Jeffrey Czyz [Mon, 11 Mar 2024 23:31:50 +0000 (18:31 -0500)]
Implement Eq and Hash for Bolt12Invoice
Bolt12Invoice will be added to a new Event::InvoiceGenerated variant.
These traits along with PartialEq are required to be implemented for any
type used in an Event.
Jeffrey Czyz [Mon, 11 Mar 2024 22:03:26 +0000 (17:03 -0500)]
Refactor handling of Bolt12Invoice
In order to provide an InvoiceGenerated event, it would be cleaner to
have one location where a Bolt12Invoice is successfully created.
Refactor the handling code to this end and clean-up line length by
making some of the type conversions more streamlined.
Jeffrey Czyz [Sun, 10 Mar 2024 00:58:01 +0000 (18:58 -0600)]
Refactor handling of InvoiceRequest
In order to generate and InvoiceSent event, it would be cleaner to have
one location where a Bolt12Invoice is successfully generated. Refactor
the handling code to this end and clean-up line length by making some of
the type conversions more streamlined.
Add a version of the create_onion_message utility function that attempts
to resolved the introduction node of the destination's BlindedPath using
a ReadOnlyNetworkGraph`. Otherwise, if given a path using the compact
representation, using create_onion_message would fail. Keep the current
version for use internally and for external uses where the blinded path
is known to be resolved.