Commit graph

966 commits

Author SHA1 Message Date
Matt Corallo
3996eaab6e Merge pull request #935 from TheBlueMatt/2021-05-enum-tlvs
TLV-ize enum serialization and a few additional structs
2021-06-01 21:53:15 +00:00
Matt Corallo
73576574a9 Update network graph sample used in benchmarks 2021-06-01 21:53:06 +00:00
Matt Corallo
c56265d225 Convert SpendableOutputDescriptor to new TLV-based serialization 2021-06-01 21:53:06 +00:00
Matt Corallo
3c93967cd2 Clean up ChannelMonitor object serialization to use TLVs/versions 2021-06-01 21:53:06 +00:00
Matt Corallo
9e5d9516dd Use TLVs inside of serialization of Event variants 2021-06-01 21:53:06 +00:00
Matt Corallo
66784e32fe Convert remaining channel inner structs and enums to TLV-based ser 2021-06-01 21:53:06 +00:00
Matt Corallo
86641ea680 Convert most chain::* inner structs and enums to TLV-based ser macros 2021-06-01 21:53:06 +00:00
Matt Corallo
68313da695 Add generic serialization implementations for Boxs and 3-tuples
These are used in the next commit(s).
2021-06-01 21:53:06 +00:00
Matt Corallo
b385a40e4a Use TLV instead of explicit length for VecReadWrapper termination
VecReadWrapper is only used in TLVs so there is no need to prepend
a length before writing/reading the objects - we can instead simply
read until we reach the end of the TLV stream.
2021-06-01 21:53:06 +00:00
Matt Corallo
ccc828412e Add a macro to implement serialization of enums using TLVs 2021-06-01 21:53:06 +00:00
Devrandom
29bad58ff4 Expose test_process_background_events 2021-06-01 19:00:00 +02:00
Matt Corallo
0e41bba506 Give OnchainEvent::HTLCUpdate individual fields instead of a pair 2021-06-01 16:11:53 +00:00
Matt Corallo
77bdc32bbd
Merge pull request #933 from TheBlueMatt/2021-05-ser-fast
Speed up deserialization of secp256k1 objects significantly
2021-06-01 16:11:24 +00:00
Matt Corallo
7231b4a245 Add additional inline hints in serialization methods
This substantially improves deserialization performance when LLVM
decides not to inline many short methods, eg when not building
with LTO/codegen-units=1.

Even with the default bench params of LTO/codegen-units=1, the
serialization benchmarks on an Intel 2687W v3 take:

test routing::network_graph::benches::read_network_graph  ... bench: 1,955,616,225 ns/iter (+/- 4,135,777)
test routing::network_graph::benches::write_network_graph ... bench: 165,905,275 ns/iter (+/- 118,798)
2021-06-01 15:47:01 +00:00
Matt Corallo
583e6ac6f0 Impl serialized_length() without LengthCalculatingWriter
With the new `serialized_length()` method potentially being
significantly more efficient than `LengthCalculatingWriter`, this
commit ensures we call `serialized_length()` when calculating
length of a larger struct.

Specifically, prior to this commit a call to
`serialized_length()` on a large object serialized with
`impl_writeable`, `impl_writeable_len_match`, or
`encode_varint_length_prefixed_tlv` (and
`impl_writeable_tlv_based`) would always serialize all inner fields
of that object using `LengthCalculatingWriter`. This would ignore
any `serialized_length()` overrides by inner fields. Instead, we
override `serialized_length()` on all of the above by calculating
the serialized size using calls to `serialized_length()` on inner
fields.

Further, writes to `LengthCalculatingWriter` should never fail as
its `write` method never returns an error. Thus, any write failures
indicate a bug in an object's write method or in our
object-creation sanity checking. We `.expect()` such write calls
here.

As of this commit, on an Intel 2687W v3, the serialization
benchmarks take:

test routing::network_graph::benches::read_network_graph  ... bench: 2,039,451,296 ns/iter (+/- 4,329,821)
test routing::network_graph::benches::write_network_graph ... bench: 166,685,412 ns/iter (+/- 352,537)
2021-06-01 15:47:01 +00:00
Matt Corallo
c632fffc3d Avoid calling libsecp serialization fns when calculating length
When writing out libsecp256k1 objects during serialization in a
TLV, we potentially calculate the TLV length twice before
performing the actual serialization (once when calculating the
total TLV-stream length and once when calculating the length of the
secp256k1-object-containing TLV). Because the lengths of secp256k1
objects is a constant, we'd ideally like LLVM to entirely optimize
out those calls and simply know the expected length. However,
without cross-language LTO, there is no way for LLVM to verify that
there are no side-effects of the calls to libsecp256k1, leaving
LLVM with no way to optimize them out.

This commit adds a new method to `Writeable` which returns the
length of an object once serialized. It is implemented by default
using `LengthCalculatingWriter` (which LLVM generally optimizes out
for Rust objects) and overrides it for libsecp256k1 objects.

As of this commit, on an Intel 2687W v3, the serialization
benchmarks take:

test routing::network_graph::benches::read_network_graph  ... bench: 2,035,402,164 ns/iter (+/- 1,855,357)
test routing::network_graph::benches::write_network_graph ... bench: 308,235,267 ns/iter (+/- 140,202)
2021-06-01 15:47:01 +00:00
Matt Corallo
615419d525 Drop byte_utils in favor of native to/from_be_bytes methods
Now that our MSRV supports the native methods, we have no need
for the helpers anymore. Because LLVM was already matching our
byte_utils methods as byteswap functions, this should have no
impact on generated (optimzied) code.

This removes most of the byte_utils usage, though some remains to
keep the patch size reasonable.
2021-06-01 15:47:01 +00:00
Matt Corallo
c7113bcd92 Add benchmark of deserializing a NetworkGraph.
NetworkGraph is one of the largest structures we generally
deserialize, so it makes for a good benchmark, even if it isn't the
most complicated one.

As of this commit, on an Intel 2687W v3, these benchmarks take:

test routing::network_graph::benches::read_network_graph  ... bench: 2,101,420,078 ns/iter (+/- 6,649,020)
test routing::network_graph::benches::write_network_graph ... bench: 344,696,835 ns/iter (+/- 229,061)
2021-06-01 15:47:01 +00:00
Matt Corallo
c05347f48a
Merge pull request #892 from TheBlueMatt/2021-04-fix-htlc-ser
Correct Channel outbound HTLC serialization and expand fuzzing coverage
2021-05-31 23:52:22 +00:00
Matt Corallo
25dbd0d7e0 Correct Channel outbound HTLC serialization
Channel serialization should happen "as if
remove_uncommitted_htlcs_and_mark_paused had just been called".

This is true for the most part, but outbound RemoteRemoved HTLCs
were being serialized as normal, even though
`remote_uncommitted_htlcs_and_mark_paused` resets them to
`Committed`.

This led to a bug identified by the `chanmon_consistency_target`
fuzzer wherein, if we receive a update_*_htlc message bug not the
corresponding commitment_signed prior to a serialization roundtrip,
we'd force-close the channel due to the peer "attempting to
fail/claim an HTLC which was already failed/claimed".
2021-05-31 18:20:22 +00:00
Matt Corallo
27c05fdd7f Add new (C-not implementable) tag on EventsProvider
This makes it so that users cannot usefully implement their own
`EventsProvider`, which would require substantial new logic in the
bindings generator (for generic methods). In the case of
`EventsProvider`, because there are no Rust methods which accept an
`EventsProvider` as an argument, this is perfectly OK as the
generated code would be entirely unused anyway.
2021-05-30 17:04:21 +00:00
Matt Corallo
d51d606ba3 panic if locktime is violated when broadcasting in tests 2021-05-28 23:58:07 +00:00
Matt Corallo
f61676ea15 Dont broadcast HTLC-Timeouts when closing a channel until locktime 2021-05-28 23:58:07 +00:00
Matt Corallo
00bbc51cad Skip transactions which are locktime'd when broadcasting in test 2021-05-28 23:58:07 +00:00
Matt Corallo
90e984e797 Track the blocks a node has connected in the TestBroadcaster 2021-05-28 23:56:44 +00:00
Matt Corallo
29a780e9e0 Delay broadcast of PackageTemplate packages until their locktime
This stores transaction templates temporarily until their locktime
is reached, avoiding broadcasting (or RBF bumping) transactions
prior to their locktime. For those broadcasting transactions
(potentially indirectly) via Bitcoin Core RPC, this ensures no
automated rebroadcast of transactions on the client side is
required to get transactions confirmed.
2021-05-28 23:56:44 +00:00
Matt Corallo
79ffc5eac9 Always pass height to OnchainTxHandler::update_claims_view
This simplifies logic somewhat and avoids duplicating the storage
of the current height in OnchainTxHandler.
2021-05-28 23:56:44 +00:00
Matt Corallo
9bd87e9c9d Expose the timelock of transaction packages 2021-05-28 23:56:44 +00:00
Matt Corallo
7d6938a43a Simplify tx checks in functional tests to make later commits simpler
This cleans up some of the transaction format verification and docs
to make it easier when we delay CLTV-locked transactions to update
the tests.
2021-05-28 23:56:44 +00:00
Matt Corallo
56491d91bb Simplify HolderHTLCOutput constructor and track CLTV expiry
This allows us to interrogate a PackageTemplate for the CLTV
timelock of the resulting transaction.
2021-05-28 23:56:44 +00:00
Matt Corallo
f492a192ff Add assertions to ensure we don't use an invalid package_amount
This somewhat cleans up the public API of PackageSolvingData to
make it harder to get an invalid amount and use it, adding further
debug assertion to check it at test-time.
2021-05-28 23:56:44 +00:00
Matt Corallo
df829a8526
Merge pull request #928 from TheBlueMatt/2021-05-really-tlv-ser
Migrate some inner structs to TLVs
2021-05-27 23:05:47 +00:00
Matt Corallo
ad20bc7f5a Update net_graph used for benchmarks to use new ser format. 2021-05-27 21:46:38 +00:00
Matt Corallo
45d86ead64 Migrate packages sub-fields to TLV serialization 2021-05-27 21:41:24 +00:00
Matt Corallo
c8bc1b6d3d Use TLV serialization for Commitment transaction structures 2021-05-27 21:41:24 +00:00
Matt Corallo
a7494228c9 Add TLV suffix to PackageTemplate 2021-05-27 21:41:07 +00:00
Matt Corallo
321b19c4d9 Move NetworkGraph inner structs to TLV storage 2021-05-27 21:41:07 +00:00
Matt Corallo
9ec0f04b74 Move ChannelManager-inner structs to TLV storage
Note that enums are left alone as we can use the type byte already
present for future compatibility.
2021-05-27 21:41:07 +00:00
Matt Corallo
557aa81619 Add dummy OutPoint constructor to be used when reading it in a TLV 2021-05-27 21:41:07 +00:00
Matt Corallo
cf430029c4 Add a macro which implements Readable/Writeable using TLVs only
This also includes a `VecWriteWrapper` and `VecReadWrapper` which
implements serialization for any `Readable`/`Writeable` type that is
in a Vec. We do this instead of implementing `Readable`/`Writeable`
directly as there isn't always a univerally-defined way to serialize
a Vec and this makes things more explicit.

Finally, this tweaks existing macros (and in the new macros) to
support a trailing `,` after a list, eg
`write_tlv_fields!(stream, {(0, a),}, {});` whereas previously the
trailing `,` after the `(0, a)` would be a compile-error.
2021-05-27 21:41:07 +00:00
Gene Ferneau
12461fcba1
Use alloc for no_std builds
Replace std structs with alloc equivalents to support no_std builds

f use prelude::* credit @devrandom
2021-05-27 17:35:20 +00:00
Matt Corallo
c9f6a35756 Fix compile warnings reading type-0 TLVs 2021-05-27 01:05:26 +00:00
Antoine Riard
c40ebf18e5 Add package template unit tests 2021-05-25 23:18:56 -04:00
Antoine Riard
881c0f58e1 Move compute_output_value as part of package member functions 2021-05-25 19:59:40 -04:00
Antoine Riard
bb0e33f383 Move get_height_timer out of OnchainTxHandler 2021-05-25 19:59:40 -04:00
Antoine Riard
4363f965d6 Move onchain* to chain/ 2021-05-25 19:59:40 -04:00
Antoine Riard
5ccb07554b Integrate PackageTemplate
This commit replaces InputMaterial in both ChannelMonitor/
OnchainTxHandler.

This doesn't change behavior.
2021-05-25 19:59:40 -04:00
Antoine Riard
2d451b8622 Duplicate fee computation utilities out of OnchainTxHandler
Duplicated code in onchain.rs is removed in next commits.
2021-05-25 19:54:57 -04:00
Antoine Riard
c9bd7b3f2d Introduce PackageTemplae, a replacement of InputMaterial
PackageTemplate aims to replace InputMaterial, introducing a clean
interface to manipulate a wide range of claim types without
OnchainTxHandler aware of special content of each.

This is used in next commits.
2021-05-25 19:54:57 -04:00
Antoine Riard
0ea7dd8bfc Add package.rs file
Package.rs aims to gather interfaces to communicate between
onchain channel transactions parser (ChannelMonitor) and outputs
claiming logic (OnchainTxHandler). These interfaces are data
structures, generated per-case by ChannelMonitor and consumed
blindly by OnchainTxHandler.
2021-05-25 19:54:57 -04:00