From 595766f81971606c162a1fda85280d8cb8dcf3c7 Mon Sep 17 00:00:00 2001 From: xstoicunicornx Date: Mon, 9 Nov 2026 20:54:18 +0000 Subject: [PATCH 1/2] Make TransactionType upgradable before release TransactionType was serialized with impl_writeable_tlv_based_enum, which fails to read any variant it doesn't know, odd or even. A node reading a payment classified with a later variant would fail to decode the whole PaymentDetails, and with it the payment store. No release writes tx_type yet, so the reader can still change for free. Serialize TransactionType with impl_writeable_tlv_based_enum_upgradable and read both fields holding one, PaymentKind::Onchain's tx_type and ChannelTxFacts' self_role, as upgradable_option, as we already do for LDK's ClosureReason and PaymentFailureReason. An unknown odd variant now reads as no classification, while an unknown even one is still required. The written bytes are unchanged. Written with the help of Claude Code. --- src/payment/store.rs | 6 +++--- src/wallet/provenance.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/payment/store.rs b/src/payment/store.rs index 99ac6a7725..405f7f4360 100644 --- a/src/payment/store.rs +++ b/src/payment/store.rs @@ -16,7 +16,7 @@ use lightning::offers::offer::OfferId; use lightning::util::ser::{Readable, Writeable}; use lightning::{ _init_and_read_len_prefixed_tlv_fields, impl_writeable_tlv_based, - impl_writeable_tlv_based_enum, write_tlv_fields, + impl_writeable_tlv_based_enum, impl_writeable_tlv_based_enum_upgradable, write_tlv_fields, }; use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning_types::string::UntrustedString; @@ -481,7 +481,7 @@ pub enum TransactionType { }, } -impl_writeable_tlv_based_enum!(TransactionType, +impl_writeable_tlv_based_enum_upgradable!(TransactionType, (0, Funding) => { (0, channels, optional_vec), }, @@ -641,7 +641,7 @@ impl PaymentKind { impl_writeable_tlv_based_enum!(PaymentKind, (0, Onchain) => { (0, txid, required), - (1, tx_type, option), + (1, tx_type, upgradable_option), (2, status, required), }, (2, Bolt11) => { diff --git a/src/wallet/provenance.rs b/src/wallet/provenance.rs index 819e5ac498..6c52ec9615 100644 --- a/src/wallet/provenance.rs +++ b/src/wallet/provenance.rs @@ -149,7 +149,7 @@ pub(crate) struct ChannelTxFacts { impl_writeable_tlv_based!(ChannelTxFacts, { (0, txid, required), (2, outputs, optional_vec), - (4, self_role, option), + (4, self_role, upgradable_option), (6, local_figures, option), (8, recorded_at_height, required), (10, locked_inputs, optional_vec), From 1dad1a318a2d7f4e814532b993fa915ddd7d1733 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 18 Nov 2026 09:46:21 +0100 Subject: [PATCH 2/2] Make LeaseCacheTargetId upgradable before release LeaseCacheTargetId was serialized with impl_writeable_tlv_based_enum, which fails on any variant it doesn't know. A downgraded node reading a cache target written with a later variant would fail to read all targets, and with it fail to build. No release writes cache targets yet, so the reader can still change. Serialize LeaseCacheTargetId as upgradable and persist targets through a private stored form whose id is read as upgradable_option, dropping targets with an unknown odd variant while an unknown even one still fails. This keeps the in-memory id non-optional without hand-writing serialization, and the written bytes are unchanged. Written with the help of Claude Code. Co-Authored-By: HAL 9000 --- src/liquidity/client/lsps2/state.rs | 121 ++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 16 deletions(-) diff --git a/src/liquidity/client/lsps2/state.rs b/src/liquidity/client/lsps2/state.rs index 4030059047..98b98a7afc 100644 --- a/src/liquidity/client/lsps2/state.rs +++ b/src/liquidity/client/lsps2/state.rs @@ -31,7 +31,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; use bitcoin::secp256k1::PublicKey; use lightning::util::persist::KVStore; use lightning::util::ser::{Readable, Writeable}; -use lightning::{impl_writeable_tlv_based, impl_writeable_tlv_based_enum}; +use lightning::{impl_writeable_tlv_based, impl_writeable_tlv_based_enum_upgradable}; use lightning_liquidity::lsps2::msgs::LSPS2OpeningFeeParams; use lightning_liquidity::lsps2::utils::compute_opening_fee; use tokio::sync::Mutex as AsyncMutex; @@ -71,7 +71,7 @@ impl LeaseCacheTargetId { } } -impl_writeable_tlv_based_enum!(LeaseCacheTargetId, +impl_writeable_tlv_based_enum_upgradable!(LeaseCacheTargetId, (0, Fixed) => { (1, amount_msat, required), }, @@ -95,21 +95,57 @@ impl LeaseCacheTarget { } } -impl_writeable_tlv_based!(LeaseCacheTarget, { - (1, id, required), +/// The persisted form of a [`LeaseCacheTarget`]. +/// +/// `id` is `None` if it was written with a [`LeaseCacheTargetId`] variant we don't know, in which +/// case the target is dropped on read rather than failing to read all targets. +#[derive(Clone, Debug, PartialEq, Eq)] +struct StoredLeaseCacheTarget { + id: Option, + absolute_expiry: Option, + last_accessed: u64, +} + +impl_writeable_tlv_based!(StoredLeaseCacheTarget, { + (1, id, upgradable_option), (3, absolute_expiry, option), (5, last_accessed, required), }); #[derive(Clone, Debug, PartialEq, Eq)] struct LeaseCacheTargets { - targets: Vec, + targets: Vec, } impl_writeable_tlv_based!(LeaseCacheTargets, { (1, targets, required_vec), }); +impl LeaseCacheTargets { + fn into_targets(self) -> Vec { + self.targets + .into_iter() + .filter_map(|target| { + let StoredLeaseCacheTarget { id, absolute_expiry, last_accessed } = target; + Some(LeaseCacheTarget { id: id?, absolute_expiry, last_accessed }) + }) + .collect() + } +} + +impl From> for LeaseCacheTargets { + fn from(targets: Vec) -> Self { + let targets = + targets + .into_iter() + .map(|LeaseCacheTarget { id, absolute_expiry, last_accessed }| { + StoredLeaseCacheTarget { id: Some(id), absolute_expiry, last_accessed } + }) + .collect(); + Self { targets } + } +} + #[derive(Clone)] pub(crate) struct LeaseCacheTargetState { targets: HashMap, @@ -254,7 +290,7 @@ where } async fn persist_state(&self, state: &LeaseCacheTargetState) -> Result<(), Error> { - let data = LeaseCacheTargets { targets: state.targets() }.encode(); + let data = LeaseCacheTargets::from(state.targets()).encode(); KVStore::write( &*self.kv_store, LSPS2_CACHE_TARGET_PERSISTENCE_PRIMARY_NAMESPACE, @@ -284,15 +320,15 @@ where ) .await { - Ok(data) => LeaseCacheTargets::read(&mut &data[..]).map(|targets| targets.targets).map_err( - |error| { + Ok(data) => LeaseCacheTargets::read(&mut &data[..]) + .map(LeaseCacheTargets::into_targets) + .map_err(|error| { log_error!(logger, "Failed to deserialize LSPS2 lease cache targets: {}", error); lightning::io::Error::new( lightning::io::ErrorKind::InvalidData, "Failed to deserialize LSPS2 lease cache targets", ) - }, - ), + }), Err(error) if error.kind() == lightning::io::ErrorKind::NotFound => Ok(Vec::new()), Err(error) => Err(error), } @@ -884,9 +920,62 @@ mod tests { fn cache_targets_roundtrip() { let target = cache_target(LeaseCacheTargetId::Fixed { amount_msat: 1_000 }, Some(42), 7); - let encoded = target.encode(); - let decoded = LeaseCacheTarget::read(&mut &encoded[..]).unwrap(); - assert_eq!(decoded, target); + let encoded = LeaseCacheTargets::from(vec![target.clone()]).encode(); + let decoded = LeaseCacheTargets::read(&mut &encoded[..]).unwrap().into_targets(); + assert_eq!(decoded, vec![target]); + } + + #[test] + fn cache_targets_skip_unknown_odd_id_variant() { + use lightning::impl_writeable_tlv_based_enum; + + #[derive(Clone, Copy)] + enum FutureTargetId { + Variable, + Future, + } + impl_writeable_tlv_based_enum!(FutureTargetId, + (2, Variable) => {}, + (5, Future) => {}, + ); + struct FutureTarget { + id: FutureTargetId, + absolute_expiry: Option, + last_accessed: u64, + } + impl_writeable_tlv_based!(FutureTarget, { + (1, id, required), + (3, absolute_expiry, option), + (5, last_accessed, required), + }); + struct FutureTargets { + targets: Vec, + } + impl_writeable_tlv_based!(FutureTargets, { + (1, targets, required_vec), + }); + + let encoded = FutureTargets { + targets: vec![ + FutureTarget { + id: FutureTargetId::Future, + absolute_expiry: None, + last_accessed: 3, + }, + FutureTarget { + id: FutureTargetId::Variable, + absolute_expiry: Some(42), + last_accessed: 7, + }, + ], + } + .encode(); + + let decoded = LeaseCacheTargets::read(&mut &encoded[..]).unwrap(); + assert_eq!( + decoded.into_targets(), + vec![cache_target(LeaseCacheTargetId::Variable, Some(42), 7)] + ); } #[tokio::test] @@ -1022,7 +1111,7 @@ mod tests { LSPS2_CACHE_TARGET_PERSISTENCE_PRIMARY_NAMESPACE, LSPS2_CACHE_TARGET_PERSISTENCE_SECONDARY_NAMESPACE, LSPS2_CACHE_TARGET_PERSISTENCE_KEY, - LeaseCacheTargets { targets }.encode(), + LeaseCacheTargets::from(targets).encode(), ) .await .unwrap(); @@ -1150,7 +1239,7 @@ mod tests { LSPS2_CACHE_TARGET_PERSISTENCE_PRIMARY_NAMESPACE, LSPS2_CACHE_TARGET_PERSISTENCE_SECONDARY_NAMESPACE, LSPS2_CACHE_TARGET_PERSISTENCE_KEY, - LeaseCacheTargets { targets: targets.clone() }.encode(), + LeaseCacheTargets::from(targets.clone()).encode(), ) .await .unwrap(); @@ -1185,7 +1274,7 @@ mod tests { )) .unwrap(); assert_eq!( - LeaseCacheTargets::read(&mut &data[..]).unwrap().targets, + LeaseCacheTargets::read(&mut &data[..]).unwrap().into_targets(), targets, "a dropped cache target was persisted as removed before its leases were removed" );