From 76b601eb48780a1e0de5de0d186b8087ade13790 Mon Sep 17 00:00:00 2001 From: Duncan Dean Date: Tue, 20 Aug 2024 09:38:35 +0200 Subject: [PATCH] Test ChannelDetails serialization to catch mutants --- lightning/src/ln/channel_state.rs | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lightning/src/ln/channel_state.rs b/lightning/src/ln/channel_state.rs index 7a9bbf9bac4..5d75f9a4f92 100644 --- a/lightning/src/ln/channel_state.rs +++ b/lightning/src/ln/channel_state.rs @@ -715,3 +715,68 @@ impl_writeable_tlv_based_enum!(ChannelShutdownState, (6, NegotiatingClosingFee) => {}, (8, ShutdownComplete) => {}, ); + +#[cfg(test)] +mod tests { + use bitcoin::{hashes::Hash as _, secp256k1::PublicKey}; + use lightning_types::features::Features; + + use crate::{ + chain::transaction::OutPoint, + ln::types::ChannelId, + util::ser::{Readable, Writeable}, + }; + + use super::{ChannelCounterparty, ChannelDetails, ChannelShutdownState}; + + #[test] + fn test_channel_details_serialization() { + #[allow(deprecated)] + let channel_details = ChannelDetails { + channel_id: ChannelId::new_zero(), + counterparty: ChannelCounterparty { + features: Features::empty(), + node_id: PublicKey::from_slice(&[2; 33]).unwrap(), + unspendable_punishment_reserve: 0, + forwarding_info: None, + outbound_htlc_minimum_msat: None, + outbound_htlc_maximum_msat: None, + }, + funding_txo: Some(OutPoint { + txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), + index: 0, + }), + channel_type: None, + short_channel_id: None, + outbound_scid_alias: None, + inbound_scid_alias: None, + channel_value_satoshis: 0, + user_channel_id: (u64::MAX as u128) + 1, // Gets us into the high bytes + balance_msat: 0, + outbound_capacity_msat: 0, + next_outbound_htlc_limit_msat: 0, + next_outbound_htlc_minimum_msat: 0, + inbound_capacity_msat: 42, + unspendable_punishment_reserve: None, + confirmations_required: None, + confirmations: None, + force_close_spend_delay: None, + is_outbound: true, + is_channel_ready: true, + is_usable: true, + is_public: true, + inbound_htlc_minimum_msat: None, + inbound_htlc_maximum_msat: None, + config: None, + feerate_sat_per_1000_weight: None, + channel_shutdown_state: Some(ChannelShutdownState::NotShuttingDown), + pending_inbound_htlcs: Vec::new(), + pending_outbound_htlcs: Vec::new(), + }; + let mut buffer = Vec::new(); + channel_details.write(&mut buffer).unwrap(); + let deser_channel_details = ChannelDetails::read(&mut buffer.as_slice()).unwrap(); + + assert_eq!(deser_channel_details, channel_details); + } +}