Add note and test regarding ChannelState's implicit ordering
authorWilmer Paulino <wilmer@wilmerpaulino.com>
Fri, 8 Dec 2023 18:20:20 +0000 (10:20 -0800)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Thu, 11 Jan 2024 02:28:56 +0000 (18:28 -0800)
lightning/src/ln/channel.rs

index b3657701a4a4b876c80694bd2496e3662a78937a..ab3bdb98c0f5c21a1afefad0d7f2932aa1437831 100644 (file)
@@ -419,6 +419,8 @@ define_state_flags!(
        ]
 );
 
+// Note that the order of this enum is implicitly defined by where each variant is placed. Take this
+// into account when introducing new states and update `test_channel_state_order` accordingly.
 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq)]
 enum ChannelState {
        /// We are negotiating the parameters required for the channel prior to funding it.
@@ -8374,6 +8376,18 @@ mod tests {
        use bitcoin::address::{WitnessProgram, WitnessVersion};
        use crate::prelude::*;
 
+       #[test]
+       fn test_channel_state_order() {
+               use crate::ln::channel::NegotiatingFundingFlags;
+               use crate::ln::channel::AwaitingChannelReadyFlags;
+               use crate::ln::channel::ChannelReadyFlags;
+
+               assert!(ChannelState::NegotiatingFunding(NegotiatingFundingFlags::new()) < ChannelState::FundingNegotiated);
+               assert!(ChannelState::FundingNegotiated < ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()));
+               assert!(ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()) < ChannelState::ChannelReady(ChannelReadyFlags::new()));
+               assert!(ChannelState::ChannelReady(ChannelReadyFlags::new()) < ChannelState::ShutdownComplete);
+       }
+
        struct TestFeeEstimator {
                fee_est: u32
        }