Commit graph

1809 commits

Author SHA1 Message Date
Matt Corallo
4e82003261
Merge pull request #611 from valentinewallace/fix-missing-htlc-claim
Tell ChannelMonitors about HTLCs fulfilled after channel close
2020-11-16 18:15:02 -08:00
Valentine Wallace
6f1a0bf0e4
Claim HTLC output on-chain if preimage is recv'd after force-close
If we receive a preimage for an outgoing HTLC that solves an output on a
backwards force-closed channel, we need to claim the output on-chain.

Note that this commit also gets rid of the channel monitor redundantly setting
`self.counterparty_payment_script` in `check_spend_counterparty_transaction`.

Co-authored-by: Antoine Riard <ariard@student.42.fr>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2020-11-16 15:41:31 -05:00
Valentine Wallace
e70f485011
Split channelmonitor's broadcast_by_holder_state
Now callers will separately retrieve the claim requests/
holder revokable script and the new watched holder outputs.
This will be used in the next commit for times when we
need to get holder claim requests, but don't have access to
the holder commitment transaction.
2020-11-16 15:41:31 -05:00
Valentine Wallace
a3e4f9c967
Extend update_monitor logging
Helpful for debugging. I also included the change in the provide_preimage method
signature which will be used in an upcoming commit, because commit-wise it was
easier to combine the changes.
2020-11-16 15:41:30 -05:00
Valentine Wallace
4ece5fd0f6
Update monitor with preimage after channel close
If the channel is hitting the chain right as we receive a preimage,
previous to this commit the relevant ChannelMonitor would never
learn of this preimage.
2020-11-16 15:41:28 -05:00
Valentine Wallace
50ad627426
Add prev_channel_outpoint to previous hop data
This will be used in upcoming commits to allow us to update a channel
monitor with a preimage after its channel has closed.
2020-11-12 18:52:06 -05:00
Matt Corallo
23a1d7aab5
Merge pull request #721 from TheBlueMatt/2020-09-649-bindings
Bindings Updates for #649
2020-11-12 13:22:54 -08:00
Matt Corallo
3aa0253f8a
Merge pull request #745 from ariard/2020-11-getting-started
Add a Getting Started in CONTRIBUTING.md
2020-11-12 09:59:07 -08:00
Matt Corallo
68827199a2
Merge pull request #746 from valentinewallace/remove-todo
Remove unnecessary todo
2020-11-10 08:34:19 -08:00
Antoine Riard
7e7635d559 Add a Getting Started
Fix by Steve Lee <moneyball@users.noreply.github.com>
2020-11-09 15:53:36 -05:00
Valentine Wallace
8e7b29160b
Remove unnecessary todo
The ChannelMonitor already monitors the chain for counterparties
revealing preimages, and will give the HTLCSources back to the
ChannelManager for claiming. Thus it's unnecessary for the ChannelManager
to monitor these HTLCs itself.

See is_resolving_htlc_output:
- if the counterparty broadcasted and then claimed one of the HTLCs we
  offered them, line 2015 is where the ChannelMonitor gives the ChannelManager
  the HTLC source
- if we broadcasted and they claimed an HTLC we offered them, line 2025 is
  where the ChannelMonitor gives the ChannelManager the HTLC source
2020-11-08 17:22:20 -05:00
Matt Corallo
5988789a2d [bindings] update generated code 2020-10-21 14:54:51 -04:00
Matt Corallo
7e18104414 Update demo.cpp for new tuple code 2020-10-21 14:54:51 -04:00
Matt Corallo
2d0cdbd33e [bindings] Un-Box Tuple mapping
Because the C++ wrappers require being able to memset(0) the C
structs to skip free(), we'd previously mapped tuples with two
pointer indirections. However, because all other types already
support memset(0)'ing to disable free() logic, we can skip the
pointer indirections and the behavior is still correct.
2020-10-21 14:54:51 -04:00
Matt Corallo
65884fffee [bindings] Fix typo in opaque struct docs found by Val 2020-10-21 14:54:51 -04:00
Matt Corallo
e12215ca8a [bindings] Use the same SipHash keys to make C++ header stable 2020-10-21 14:54:51 -04:00
Matt Corallo
35e48cf479 [bindings] Use enum to describe deref'ing needed for Option<> inners 2020-10-21 14:54:51 -04:00
Matt Corallo
d773151e7f Update bindings to latest upstream code 2020-10-21 14:54:51 -04:00
Matt Corallo
353e29aedd Drop the now-unused usizeslice bindings struct 2020-10-21 14:50:22 -04:00
Matt Corallo
a5e7671d1e Update bindings demo apps for new code upstream 2020-10-21 14:50:22 -04:00
Matt Corallo
2342550af5 Move a struct in bindings up to define it before it is used
This is a limitations in the bindings crate, but not one that's
going to be fixed right now.
2020-10-21 14:50:22 -04:00
Matt Corallo
6df3aa76c3 [bindings] Drop one static-lifetime restriction and check success
In general we should stop enforcing that all lifetimes are static
- we may take references from C and its up to reviewing the diff on
the bindings changes and the user(s) to ensure lifetimes are valid.

Also asserts a success criteria that was missed before.
2020-10-21 14:50:22 -04:00
Matt Corallo
4d0cf680ab [bindings] Handle type X = Y aliasing in type resolution
For non-generic type aliases which are meant as convinient aliases
for more complex types, we need to store the aliased type (with all
paths made absolute) and use that in type resolution.

The most code by far is just making all the paths in a type absolute
but its not too bad either.
2020-10-21 14:50:22 -04:00
Matt Corallo
00fb152758 [bindings] Handle ::-prefixed paths in a few places 2020-10-21 14:50:22 -04:00
Matt Corallo
eb7faa85e4 [bindings] Remove some uneccessary no-multi-ident path restrictions 2020-10-21 14:50:22 -04:00
Matt Corallo
bb4115effd [bindings] Avoid guessing whether resolved type is a ref in blocks
In some cases, things which are a Rust Reference (ie slices), we
may still want to map them as a non-reference and need to put a
"mut " in front of the variable name in a function decl. This
worked fine by just checking for the slice case, except that we
are about to add support for type aliases, which no longer match
the naive case.

Instead, we can just have the types module print out the C type and
check if it begins with a '&' to figure out if it is a reference.
2020-10-21 14:50:22 -04:00
Matt Corallo
a14e63e0b3 [bindings] Support mapping slices which contain tuples (with refs)
New work upstream puts tuples in slices, which is a very reasonable
thing to expect, however we don't know how to generate conversions
for such objects. Making it more complicated, upstream changes also
include references to things inside such slices, which requires
special handling to avoid creating dangling references.

This adds support for converting such objects, noting that slices
need to be converted first into Vecs which own their underlying
objects and then need to map any reference types into references.
2020-10-21 14:50:22 -04:00
Matt Corallo
d9a38d1846 [bindings] Give Transaction objects a buffer-is-owned flag.
A lot of our container mapping depends on the `is_owned` flag
which we have for in-crate mapped objects to map references and
non-references into the same container type. Transaction was
mapped to two completely different types (a slice and a Vec type),
which led to a number of edge cases in the bindings generation.
Specifically, I spent a few days trying to map
`[(A, &Transaction)]` properly and came up empty - we map slices
into the same types as Vecs (and rely on the `is_owned` flag to
avoid double-free) and the lack of one for `Transaction` would have
required a special-case in numerous functions.

Instead, we just add a flag in `Transaction` to mirror what we do
for in-crate types and check it before free-ing any underlying
memory.

Note that, sadly, because the c_types objects aren't mapped as a
part of our C++ bindings generation, you have to manually call
`Transaction_free()` even in C++.
2020-10-21 14:50:22 -04:00
Matt Corallo
9c7c3b9921
Merge pull request #681 from valentinewallace/data-persister-refactor
Add ChannelDataPersister trait and point ChainMonitor to it.
2020-10-16 14:35:33 -07:00
Valentine Wallace
cda8fb7f6f
Fix clippy 2020-10-16 13:41:40 -04:00
Valentine Wallace
fc68afb21b
Rename ChannelMonitor::write_for_disk --> serialize_for_disk
This function does not necessarily write to disk, it can serialize to anything
that implements Writer.
2020-10-16 13:41:39 -04:00
Valentine Wallace
a8e82cb3fb
Test that Persist temp and perm failures behave as expected.
If a persister returns a temporary failure, the channel monitor should be able
to be put on ice and then revived later. If a persister returns a permanent
failure, the channel should be force closed.
2020-10-16 13:41:39 -04:00
Valentine Wallace
82f5a1cbda
Add a sample module FilesystemPersister.
Intended to be a cross-platform implementation of the
channelmonitor::Persist trait.

This adds a new lightning-persister crate, that uses the
newly exposed lightning crate's test utilities.

Notably, this crate is pretty small right now. However, due to
future plans to add more data persistence (e.g. persisting the
ChannelManager, etc) and a desire to avoid pulling in filesystem
usage into the core lightning package, it is best for it to be
separated out.

Note: Windows necessitates the use of OpenOptions with the `write`
permission enabled to `sync_all` on a newly opened channel's
data file.
2020-10-16 13:41:36 -04:00
Valentine Wallace
9c3f3e76e5
Integrate Persist into ChainMonitor.
- The ChainMonitor should:
  Whenever a new channel is added or updated, these updates
  should be conveyed to the persister and persisted to disk.
  Even if the update errors while it's being applied, the
  updated monitor still needs to be persisted.
2020-10-16 11:30:34 -04:00
Valentine Wallace
ec4715c09b
Add Persist trait.
Implementors of Persist are responsible for persisting new ChannelMonitors
and ChannelMonitor updates to disk/backups.
2020-10-16 11:30:33 -04:00
Valentine Wallace
4350cc615c
Put test utilities behind a feature flag.
The utilities will still be part of cfg(test). The purpose of this
is to enable other crates in the workspace to use the test utilities.
2020-10-16 11:30:33 -04:00
Matt Corallo
8a798776b4
Merge pull request #653 from ariard/2020-06-fix-outputs-tracking
Add outpoint index in watch_outputs to fix tracking
2020-10-14 17:45:33 -07:00
Antoine Riard
27ee1150dd Assert on correct registeration of outputs index
We remove test_no_failure_dust_htlc_local_commitment from our test
framework as this test deliberately throwing junk transaction in
our monitoring parsing code is hitting new assertions.

This test was added in #333, but it sounds as an oversight as the
correctness intention of this test (i.e verifying lack of dust
HTLCs canceling back in case of junk commitment transaction) doesn't
currently break.
2020-10-14 09:23:56 -04:00
Matt Corallo
0719f9af00 [bindings] Add support for Option<T> where T is a mapped trait
When mapping an `Option<T>` where T is a mapped trait, we need to
move out of the `*mut T`, however the current generation results in
a `*const T` and a conversion that just takes a reference to the
pointed-to object. This is because the only place this code was
previously used was for slices, which *do* need a reference.

Additionally, we need to know how to convert `Option` containers
which do not contain an opaque type.

Sadly, the easiest way to get the desired result is to add another
special case in container mapping, keeping the current behavior for
slices, but moving out of the pointed-to object for other types.
2020-10-12 12:17:26 -04:00
Matt Corallo
36e732879a Ignore cbindgen version in latest-bindings-in-git check.
Currently the check_binidngs GitHub CI job always fails when there
is a new cbindgen release because the cbindgen version is in the
generated header file. When the new version doesn't change the
generated header except for the version number, we should ignore
the difference.
2020-10-12 12:17:26 -04:00
Matt Corallo
a1d0dde507 [bindings] Use == null() instead of is_null() to avoid ambiguity
When we have a `Trait` wrapped as an `Option<Trait>`, we called
`<*const Trait>.is_null()` which resulted in rustc trying to take
the most braindead option of dereferencing the whole thing and
hitting a recursive dereference since we
`impl Deref<Target=Trait> for Trait` for all our traits.

Instead, we can be explicit and just compare the pointer directly
with `std::ptr::null()` which avoids this.
2020-10-12 12:17:26 -04:00
Matt Corallo
fe279c4034 [bindings] Include a GenericTypes context in more places
A few places got a None in the previous commit to avoid increasing
the diff size. However, it makes sense to have GenericTypes contexts
there, so we pipe them through the neccessary places.
2020-10-12 12:17:26 -04:00
Matt Corallo
8a2513f84b [bindings] Push generic resolution into resolve_path
Like the previous commit pushing into maybe_resolve_path, this
makes generic resolution a part of type resolution everywhere.
2020-10-12 12:17:26 -04:00
Matt Corallo
b7f7aba2e7 [bindings] Push generic resolution into maybe_resolve_path
This pushes down the logic to check if a given Path is, in fact,
a reference to a generic into the common maybe_resolve_path instead
of doing it redundantly in a few different places. Net loss of LoC.
2020-10-12 12:17:26 -04:00
Matt Corallo
16b9309e7c Support use ident; in bindings
Somehow we'd never had any cases of it and it requires an extra
(trivial) match arm.
2020-10-12 12:17:26 -04:00
Matt Corallo
8b8a97c6aa Add README note about key validity assertions at the FFI boundary 2020-10-12 12:17:26 -04:00
Antoine Riard
613ac6e5f0 Add test_htlc_no_detection
This test is a mutation to underscore the detetection logic bug
we had before #653. HTLC value routed is above the remaining
balance, thus inverting HTLC and `to_remote` output. HTLC
will come second and it wouldn't be seen by pre-#653 detection
as we were eneumerate()'ing on a watched outputs vector (Vec<TxOut>)
thus implictly relying on outputs order detection for correct
spending children filtering.
2020-10-10 18:51:05 -04:00
Antoine Riard
30aad79df8 Add transaction index in watched_outputs
Previously, outputs were monitored based on txid and an index yelled
from an enumeration over the returned selected outputs by monitoring
code. This is always have been broken but was only discovered while
introducing anchor outputs as those ones rank always first per BIP69.
We didn't have test cases where a HTLC was bigger than a party balance
on a holder commitment and thus not ranking first.

Next commit introduce test coverage.
2020-10-10 18:51:05 -04:00
Matt Corallo
df778b605a
Merge pull request #738 from TheBlueMatt/2020-10-opt-test
Fix passing -O1 to build from `cargo test`
2020-10-10 15:21:11 -07:00
Matt Corallo
04dbf1f2ba
Merge pull request #740 from rloomba/rloomba/trivial-clippy-fixes
trivial clippy warning fixes
2020-10-09 08:29:56 -07:00