Commit graph

2047 commits

Author SHA1 Message Date
Matt Corallo
7dd9d6f40c [bindings] Move to manual write-out for Vec, too 2021-02-10 22:25:10 -05:00
Matt Corallo
c5c2d464ba [bindings] Concretize Result types without type aliasing
While the type aliasing trick works great for cbindgen,
wasm_bindgen doesn't support it and requires fully-concrete types.
In order to better support wasm_bindgen in the future, we do so
here, adding a function which manually writes out almost the exact
thing which was templated previously in concrete form.

As a nice side-effect, we no longer have to allocate and free a u8
for generic parameters which were `()` (though we still do in some
conversion functions, which we can get rid of when we similarly
concretize all generics fully).
2021-02-10 22:24:16 -05:00
Matt Corallo
f5e0e228fd [bindings] Keep track of all types which derive[Clone] 2021-02-10 22:24:16 -05:00
Matt Corallo
ee07a73b82 [bindings] Add additional clone methods on manually-defined types 2021-02-10 22:24:16 -05:00
Matt Corallo
c35002fa9c
Merge pull request #787 from TheBlueMatt/2021-02-check-close-source 2021-02-10 09:09:53 -08:00
Matt Corallo
bd8382a4d3 Fix documentation on PeerHandleError 2021-02-09 19:06:00 -05:00
Matt Corallo
d3f61c0ad7 Add test for error message hangline resulting in force-close 2021-02-09 19:04:54 -05:00
Matt Corallo
61164afb2b Check the PK of the source of an error before closing chans from it
When we receive an error message from a peer, it can indicate a
channel which we should close. However, we previously did not
check that the counterparty who sends us such a message is the
counterparty with whom we have the channel, allowing any
connected peer to make us force-close any channel we have as long
as they know the channel id.

This commit simply changes the force-close logic to check that the
sender matches the channel's counterparty node_id, though as noted
in #105, we eventually need to change the indexing anyway to allow
absurdly terrible peers to open channels with us.

Found during review of #777.
2021-02-09 19:04:54 -05:00
Matt Corallo
6dcb7c4d05
Merge pull request #793 from galderz/t_double_validation_792 2021-02-05 06:17:46 -08:00
Galder Zamarreño
048e862a4b Unknown bits feature only needs to be validated once #792 2021-02-05 13:29:56 +01:00
Matt Corallo
89be6254ae
Merge pull request #790 from bmancini55/sync_complete
Interpret sync_complete in reply_channel_range
2021-02-04 09:18:52 -08:00
Matt Corallo
b67ec5a273
Merge pull request #774 from jkczyz/2021-01-http-client
HTTP-based block source clients
2021-02-04 09:16:14 -08:00
Jeffrey Czyz
85fdfaaa9c
Implement BlockSource for REST and RPC clients
Interprets HTTP responses as either binary or JSON format, which are
then converted to the appropriate data types.
2021-02-04 07:38:42 -08:00
Jeffrey Czyz
6ca3b8ed49
Add REST and RPC clients to lightning-block-sync
Implements a simple HTTP client that can issue GET and POST requests.
Used to implement REST and RPC clients, respectively. Both clients
support either blocking or non-blocking I/O.
2021-02-04 07:38:42 -08:00
bmancini55
77690fada2 Interpret sync_complete in reply_channel_range
LN Spec PR #826 changes full_information to indicate completion of a
sequence of reply_channel_range messages.
2021-02-04 09:19:02 -05:00
Jeffrey Czyz
9860646e73
Add lightning-block-sync package and library
Defines an interface and related types for fetching block headers and
data from a block source (e.g., Bitcoin Core). Used to keep lightning in
sync with chain activity.
2021-02-03 17:45:54 -08:00
Matt Corallo
e4b516dd8a
Merge pull request #768 from TheBlueMatt/2020-12-chanman-bindings-deser
Expose ChannelManager/Monitor read methods in bindings
2021-02-03 07:43:34 -08:00
Matt Corallo
5d045de8be [bindings] Fix genbindings.sh compile issues on OSX
There were two issues on OSX - we need to give gcc the clang
warnings flags because `gcc` *is* clang on OSX and we missed an
`-std=c++11` on one of the clang++ calls, causing compile failures.
2021-02-03 10:11:35 -05:00
Matt Corallo
3efd641ca4 Update C++ bindings demo to use ChannelManager de/serialization
This demonstrates (and tests) the newly-exposed `ChannelManager`
de/serialization functions. Best revewied with -b --color-moved.
2021-02-03 10:11:35 -05:00
Matt Corallo
78bc724091 [C++ bindings demo] Add now-missing std::moves 2021-02-03 10:11:35 -05:00
Matt Corallo
64aa8baf98 Auto-generated bindings updates 2021-02-03 10:11:35 -05:00
Matt Corallo
e57c225e0f [C++ bindings] Add move-assign operator, require rvalue for move
This adds a move-assignment operator (`A& operator=(A&& o)`) to our
C++ wrapper classes as well as requiring an rvalue for the move
auto-convert operator (`operator CStruct()() &&`).

The second makes the C++ wrapper classes much easier to work with
by requiring an explicit `std::move` when the bindings will
automatically move a C++-wrapper object into a C object.
2021-02-03 10:11:28 -05:00
Matt Corallo
1e06274765 [bindings] Use references in a few places instead of pointers
Previously, references and pointers ended up identical in C, so
there was little reason to differentiate. With the addition of
nullability annotations, there is a (very slight) reason to prefer
references, so use them in a few places where its a trivial change.
2021-02-03 10:11:28 -05:00
Matt Corallo
53f2e25a96 [bindings] Use new non-null annotation feature in cbindgen
This adds a new annotation for objects we take by reference in the
C header indicating the pointers must not be null. We have to
disable some warning clang now dumps that we haven't annotated all
pointers, as cbindgen is not yet able to add a nullable annotation.
2021-02-03 10:11:28 -05:00
Matt Corallo
ac078c103c [bindings] Don't require trait impl for-structs to have no generics
This (finally) exposes `ChannelManager`/`ChannelMonitor` _write
methods, which were (needlessly) excluded as the structs themselves
have generic parameters. Sadly, we also now need to parse
`(C-not exported)` doc comments on impl blocks as we otherwise try
to expose _write methods for `&Vec<RouteHop>`, which doesn't work
(and isn't particularly interesting for users anyway). We add such
doc comments there.
2021-02-03 10:11:28 -05:00
Matt Corallo
56134a2bd1 [bindings] Implement ReadableArgs mapping, try impl mapping for ()s
This is most of the code to expose `ChannelManager`/`ChannelMonitor`
deserialization in our C bindings, using the new infrastructure to
map types in `maybe_convert_trait_impl` and passing generics in
from the callsites.

We also call `maybe_convert_trait_impl` for tuple types, as the
`ChannelManager`/`ChannelMonitor` deserialization returns a
`(BlockHash, T)` to indicate the block hash at which users need to
start resyncing the chain.

The final step to expose them is in the next commit.
2021-02-03 10:11:28 -05:00
Matt Corallo
ef2b321fdb [bindings] Allow write_rust_type to handle leading-colon paths
It just stubs out to `write_rust_path` in this case anyway, which
handles leading-colons just fine, so no need to panic on them.
2021-02-03 10:11:28 -05:00
Matt Corallo
dfef21c666 [bindings] Use common conv in _write impls, drop type restrictions
This expands the manual implementation logic for `*_write` and
`*_read` methods to most types, converting the `*_write` path to
the common type-conversion logic to ensure it works.

Note that `*_write_void` is still only implemented for has-inner
types, as its unclear what the `void*` would point to for others.
2021-02-03 10:11:28 -05:00
Matt Corallo
e36b51400f [bindings] Convert manual _read implementations to return Results
Previously, manual `*_read` implementations were only defined for
types with inner fields, which were set to NULL to indicate read
errors. This prevents exposing `*_read` for several other types,
including tuples (which are needed for `ChannelManager`/
`ChannelMonitors`) and enums (which includes `Event`s, though users
likely never need to call that directly). Further, this means we
don't expose the actual error enum (which is likely no big deal,
but is still nice).

Here, we instead create the `Result<Object, DecodeError>` type and
then pass it through the normal type conversion functions, giving
us access to any types which we can convert normally.
2021-02-02 22:51:52 -05:00
Matt Corallo
0a6c9e85ff [bindings] Pipe errors back from write_template_constructor
We can fail to resolve a part of a tuple, resulting in a panic in
write_template_constructor even if we're calling
`understood_c_type` with the intent of figuring out whether we can
print a type at all. Instead, we should pipe errors back and let
`understood_c_type` return false as a result.
2021-02-02 22:51:52 -05:00
Matt Corallo
c7ddcd3867
Merge pull request #767 from TheBlueMatt/2020-12-chansigner-read-bindings
Handle `read_chan_signer` in bindings (761 bindings updates)
2021-02-02 19:49:28 -08:00
Matt Corallo
1ce1902614
Merge pull request #781 from TheBlueMatt/2021-01-tokio-1
Update tokio to 1.0
2021-02-02 14:10:05 -08:00
Matt Corallo
93e562f5bf [bindings] Update C++ demo for new NetGraphMsgHandler::new() params 2021-02-02 17:04:31 -05:00
Matt Corallo
30a42f4597 Update auto-generated bindings, including bumping cbindgen version 2021-02-02 17:04:31 -05:00
Matt Corallo
b243c65acc [bindings] Allow unused fns that the bindings can call but don't
We no longer have any public `Option<Signatures>` in our code, and
thus get warnings that the two functions which support it are
unused. Instead of removing support for them (which we may need in
the future), we add `#[allow(unused)]`.
2021-02-02 17:04:31 -05:00
Matt Corallo
1479016331 [bindings] Drop useless #[no_mangle] from pub type definitions
Newer rustc complains that "attribute should be applied to a function or
static"
2021-02-02 17:04:31 -05:00
Matt Corallo
554af1efb3 [bindings] Be explicit with take_ptr calls
If you try to call take_ptr on a pointer to an object which
implements Deref, rustc hits the deref recursion limit.

To avoid this, we can explicitly tell rustc that we want to treat
the pointer as a pointer and call take_ptr on it directly.
2021-02-02 17:04:31 -05:00
Matt Corallo
734c0a6236 [bindings] Separate take_ptr and take_inner
It is confusing to have two utility methods on different classes
of types which do two different, but related, things with the same
name.
2021-02-02 17:04:31 -05:00
Matt Corallo
78c2c48ea3 [bindings] Support exposing bitcoin::OutPoints as our common type 2021-02-02 17:04:31 -05:00
Matt Corallo
ce56152cf5 [bindings] Figure out in-file structs and enums before processing
Previously, types which were declared and used in the same file
would fail if the use was before the declaration. This makes sense
in a few cases where a "parent" class returns a reference to a
"child" class and there's no reason we shouldn't support it.

This change adds a second pass to our file processing which gathers
the structs and enums whicha re declared in the file and adds them
to the type resolver first, before doing the real conversion.
2021-02-02 17:04:31 -05:00
Matt Corallo
b2bf57eb82 [bindings] Don't export new functions with unexportable types
`CommitmentTransaction::new_with_auxiliary_htlc_data()` includes a
unbounded generic parameter which we can't concretize and it's of
limited immediate use for users in any case. We should eventually
add a non-generic version which uses `()` for the generic but that
can come later.

`CommitmentTransaction::htlcs()` returns a reference to a Vec,
which we cannot currently map. It should, however, be exposed to
users, so in the future we'll need to have a duplication function
which returns Vec of references or a cloned Vec.
2021-02-02 17:04:31 -05:00
Matt Corallo
bec92d3eaa [bindings] Expose secp256k1::Message as ThirtyTwoBytes 2021-02-02 17:04:31 -05:00
Matt Corallo
c6d2697703 [bindings] Add support for mapping Write as a supertrait 2021-02-02 17:04:31 -05:00
Matt Corallo
dcfd95a700 [bindings] Handle MessageSendEventsProvider impl blocks in a util fn
Instead of having manually-written lightning-specific code in a
supertrait walk in the middle of a large function, move it to a
utility function up next to the other manually-written-impl-block
functions.
2021-02-02 17:04:31 -05:00
Matt Corallo
4edf514a05 [bindings] Always resolve supertrait types during supertrait walks
This is a rather trivial cleanup to ensure we always have the full
path when we walk supertraits even if the supertrait is specified
with only a single ident.
2021-02-02 17:04:31 -05:00
Matt Corallo
af3ad4b029 [bindings] Pass GenericTypes through to write_template_generic
With this we support types like `Result<Self::AssociatedType, ()>`.
2021-02-02 17:04:31 -05:00
Matt Corallo
3f5a287ca8 [bindings] Un-special-case returning an associated type
In the case that we return an associated type to C (ie when
implementing a trait which returns an associated type, we had to
convert the Rust-returned concrete Rust type to the C trait struct),
we had code to manually create the neccessary trait struct at the
return site.

This was special-cased in the method-body-writing function instead
of letting the type conversion logic handle it. As a result, we are
unable to do the same conversion when it appears in a different
context, for example inside of a generic like
`Result<Self::AssocType, ErrorType>`.

To solve this, we do the actual work in a
`impl From<nativeType> for CTraitStruct` implementation and then
call `into()` from within the type conversion logic.
2021-02-02 17:04:31 -05:00
Matt Corallo
086434f0c5 [bindings] Replace associated_types HashMaps with common Generics
Instead of handling associated types separately, we can just shove
them into the same generics resolution logic we use for template
types. While we should probably have some precedence logic,
aliasing type names seems like a bad idea anyway so no effort is
made to handle it.

This removes a good chunk of code and, more importantly, tees us up
for supporting `Type<Self::AssociatedType>`-style generics.
2021-02-02 17:04:31 -05:00
Matt Corallo
32abba7201 Drop spurious semicolon that new rustc complains about 2021-02-02 16:43:07 -05:00
Matt Corallo
70440a529e [bindings] Use consistent imports for MessageSendEvents traits
Our bindings generator is braindead with respect to the idents
used in a trait definition - it treats them as if they were used
where the trait is being used, instead of where the trait is
defined. Thus, if the idents used in a trait definition are not
also imported the same in the files where the traits are used, we
will claim the idents are bogus.

I spent some time trying to track the TypeResolvers globally
through the entire conversion run so that we could use the original
file's TypeResolver later when using the trait, but it is somewhat
of a lifetime mess. While likely possible, import consistency is
generally the case anyway, so unless it becomes more of an issue in
the future, it likely makes the most sense to just keep imports
consistent.

This commit keeps imports consistent across trait definition files
around `MessageSendEvent` and `MessageSendEventsProvider`.
2021-02-01 16:52:57 -05:00