rust-lightning/lightning-transaction-sync
Matt Corallo 16119967df Add support for mapping old fields to new ones in TLV read macros
As we've grown, we regularly face a question of whether to "break
out" of our nice TLV-based struct/enum reading/writing macros in
order to handle mapping legacy fields to new ones, or deal with
keeping the legacy fields and handling at runtime what should be
hanlded at (de-)serialization time.

This attempts to address this tradeoff by adding support for a
"legacy" TLV type. This TLV style allows us to write a a value
which is not in the struct/enum's layout in memory but can be
calculated from its contents at write-time. The field will also be
read and can be use to calculate other fiels defined with
`static_value` or `default_value`.

It takes a type and a `$write` expression.

They are always read as `option`s to retain a future ability to
remove the `legacy` fields.

Sadly, there's two issues with doing this trivially which force us
into `proc-macro` land:

(a) when matching the original struct we want to list the fields
    in the match arm so that we have them available to write.
    Sadly, we can't call a macro to have it write out the field
    name based on the field type, so instead need to pass the whole
    match to a proc-macro and have it walk through to find the
    types and skip fields that are `legacy`.
(b) when building a final struct/enum after reading, we need to
    list a few `$field: $expr`s and cannot decide whether to
    include a field based on a regular macro.

The proc-macros to do so aren't trivial, but they aren't that bad
either. We could instead try to rewrite our TLV stream processing
macros to handle a new set of TLVs which are passed via a separate
argument, but as TLVs are required to in ordered by type this
requires a good chunk of additional generated code in each TLV
write. It also would result in a somewhat less ergonomic callsite
as it would no longer fit into our existing list of TLVs.
2025-02-05 22:18:18 +00:00
..
src Update lightning-transaction-sync to use macros crate 2024-10-17 16:26:42 +02:00
tests Update lightning-transaction-sync to use macros crate 2024-10-17 16:26:42 +02:00
Cargo.toml Add support for mapping old fields to new ones in TLV read macros 2025-02-05 22:18:18 +00:00