@@ -267,7 +267,7 @@ enum HTLCUpdateAwaitingACK {
267267}
268268
269269macro_rules! define_state_flags {
270- ($flag_type_doc: expr, $flag_type: ident, [$(($flag_doc: expr, $flag: ident, $value: expr)),+], $extra_flags: expr) => {
270+ ($flag_type_doc: expr, $flag_type: ident, [$(($flag_doc: expr, $flag: ident, $value: expr, $get: ident, $set: ident, $clear: ident )),+], $extra_flags: expr) => {
271271 #[doc = $flag_type_doc]
272272 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq)]
273273 struct $flag_type(u32);
@@ -296,7 +296,6 @@ macro_rules! define_state_flags {
296296
297297 #[allow(unused)]
298298 fn is_empty(&self) -> bool { self.0 == 0 }
299-
300299 #[allow(unused)]
301300 fn is_set(&self, flag: Self) -> bool { *self & flag == flag }
302301 #[allow(unused)]
@@ -305,10 +304,10 @@ macro_rules! define_state_flags {
305304 fn clear(&mut self, flag: Self) -> Self { self.0 &= !flag.0; *self }
306305 }
307306
308- impl core::ops::Not for $flag_type {
309- type Output = Self ;
310- fn not(self) -> Self::Output { Self(!self.0) }
311- }
307+ $(
308+ define_state_flags!($flag_type, Self::$flag, $get, $set, $clear) ;
309+ )*
310+
312311 impl core::ops::BitOr for $flag_type {
313312 type Output = Self;
314313 fn bitor(self, rhs: Self) -> Self::Output { Self(self.0 | rhs.0) }
@@ -339,6 +338,16 @@ macro_rules! define_state_flags {
339338 };
340339 ($flag_type_doc: expr, FUNDED_STATE, $flag_type: ident, $flags: tt) => {
341340 define_state_flags!($flag_type_doc, $flag_type, $flags, FundedStateFlags::ALL.0);
341+
342+ define_state_flags!($flag_type, FundedStateFlags::PEER_DISCONNECTED,
343+ is_peer_disconnected, set_peer_disconnected, clear_peer_disconnected);
344+ define_state_flags!($flag_type, FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS,
345+ is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress);
346+ define_state_flags!($flag_type, FundedStateFlags::REMOTE_SHUTDOWN_SENT,
347+ is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent);
348+ define_state_flags!($flag_type, FundedStateFlags::LOCAL_SHUTDOWN_SENT,
349+ is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent);
350+
342351 impl core::ops::BitOr<FundedStateFlags> for $flag_type {
343352 type Output = Self;
344353 fn bitor(self, rhs: FundedStateFlags) -> Self::Output { Self(self.0 | rhs.0) }
@@ -385,25 +394,29 @@ define_state_flags!(
385394 "Flags that apply to all [`ChannelState`] variants in which the channel is funded.",
386395 FundedStateFlags, [
387396 ("Indicates the remote side is considered \"disconnected\" and no updates are allowed \
388- until after we've done a `channel_reestablish` dance.", PEER_DISCONNECTED, state_flags::PEER_DISCONNECTED),
397+ until after we've done a `channel_reestablish` dance.", PEER_DISCONNECTED, state_flags::PEER_DISCONNECTED,
398+ is_peer_disconnected, set_peer_disconnected, clear_peer_disconnected),
389399 ("Indicates the user has told us a `ChannelMonitor` update is pending async persistence \
390400 somewhere and we should pause sending any outbound messages until they've managed to \
391- complete it.", MONITOR_UPDATE_IN_PROGRESS, state_flags::MONITOR_UPDATE_IN_PROGRESS),
401+ complete it.", MONITOR_UPDATE_IN_PROGRESS, state_flags::MONITOR_UPDATE_IN_PROGRESS,
402+ is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress),
392403 ("Indicates we received a `shutdown` message from the remote end. If set, they may not add \
393404 any new HTLCs to the channel, and we are expected to respond with our own `shutdown` \
394- message when possible.", REMOTE_SHUTDOWN_SENT, state_flags::REMOTE_SHUTDOWN_SENT),
405+ message when possible.", REMOTE_SHUTDOWN_SENT, state_flags::REMOTE_SHUTDOWN_SENT,
406+ is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent),
395407 ("Indicates we sent a `shutdown` message. At this point, we may not add any new HTLCs to \
396- the channel.", LOCAL_SHUTDOWN_SENT, state_flags::LOCAL_SHUTDOWN_SENT)
408+ the channel.", LOCAL_SHUTDOWN_SENT, state_flags::LOCAL_SHUTDOWN_SENT,
409+ is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent)
397410 ]
398411);
399412
400413define_state_flags!(
401414 "Flags that only apply to [`ChannelState::NegotiatingFunding`].",
402415 NegotiatingFundingFlags, [
403416 ("Indicates we have (or are prepared to) send our `open_channel`/`accept_channel` message.",
404- OUR_INIT_SENT, state_flags::OUR_INIT_SENT),
417+ OUR_INIT_SENT, state_flags::OUR_INIT_SENT, is_our_init_sent, set_our_init_sent, clear_our_init_sent ),
405418 ("Indicates we have received their `open_channel`/`accept_channel` message.",
406- THEIR_INIT_SENT, state_flags::THEIR_INIT_SENT)
419+ THEIR_INIT_SENT, state_flags::THEIR_INIT_SENT, is_their_init_sent, set_their_init_sent, clear_their_init_sent )
407420 ]
408421);
409422
@@ -412,13 +425,16 @@ define_state_flags!(
412425 FUNDED_STATE, AwaitingChannelReadyFlags, [
413426 ("Indicates they sent us a `channel_ready` message. Once both `THEIR_CHANNEL_READY` and \
414427 `OUR_CHANNEL_READY` are set, our state moves on to `ChannelReady`.",
415- THEIR_CHANNEL_READY, state_flags::THEIR_CHANNEL_READY),
428+ THEIR_CHANNEL_READY, state_flags::THEIR_CHANNEL_READY,
429+ is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready),
416430 ("Indicates we sent them a `channel_ready` message. Once both `THEIR_CHANNEL_READY` and \
417431 `OUR_CHANNEL_READY` are set, our state moves on to `ChannelReady`.",
418- OUR_CHANNEL_READY, state_flags::OUR_CHANNEL_READY),
432+ OUR_CHANNEL_READY, state_flags::OUR_CHANNEL_READY,
433+ is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready),
419434 ("Indicates the channel was funded in a batch and the broadcast of the funding transaction \
420435 is being held until all channels in the batch have received `funding_signed` and have \
421- their monitors persisted.", WAITING_FOR_BATCH, state_flags::WAITING_FOR_BATCH)
436+ their monitors persisted.", WAITING_FOR_BATCH, state_flags::WAITING_FOR_BATCH,
437+ is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch)
422438 ]
423439);
424440
@@ -429,7 +445,8 @@ define_state_flags!(
429445 `revoke_and_ack` message. During this period, we can't generate new `commitment_signed` \
430446 messages as we'd be unable to determine which HTLCs they included in their `revoke_and_ack` \
431447 implicit ACK, so instead we have to hold them away temporarily to be sent later.",
432- AWAITING_REMOTE_REVOKE, state_flags::AWAITING_REMOTE_REVOKE)
448+ AWAITING_REMOTE_REVOKE, state_flags::AWAITING_REMOTE_REVOKE,
449+ is_awaiting_remote_revoke, set_awaiting_remote_revoke, clear_awaiting_remote_revoke)
433450 ]
434451);
435452
@@ -455,12 +472,12 @@ enum ChannelState {
455472}
456473
457474macro_rules! impl_state_flag {
458- ($get: ident, $set: ident, $clear: ident, $state_flag: expr, [$($state: ident),+]) => {
475+ ($get: ident, $set: ident, $clear: ident, [$($state: ident),+]) => {
459476 #[allow(unused)]
460477 fn $get(&self) -> bool {
461478 match self {
462479 $(
463- ChannelState::$state(flags) => flags.is_set($state_flag.into() ),
480+ ChannelState::$state(flags) => flags.$get( ),
464481 )*
465482 _ => false,
466483 }
@@ -469,7 +486,7 @@ macro_rules! impl_state_flag {
469486 fn $set(&mut self) {
470487 match self {
471488 $(
472- ChannelState::$state(flags) => * flags |= $state_flag ,
489+ ChannelState::$state(flags) => flags.$set() ,
473490 )*
474491 _ => debug_assert!(false, "Attempted to set flag on unexpected ChannelState"),
475492 }
@@ -478,17 +495,17 @@ macro_rules! impl_state_flag {
478495 fn $clear(&mut self) {
479496 match self {
480497 $(
481- ChannelState::$state(flags) => *flags &= !($state_flag) ,
498+ ChannelState::$state(flags) => { let _ = flags.$clear(); } ,
482499 )*
483500 _ => debug_assert!(false, "Attempted to clear flag on unexpected ChannelState"),
484501 }
485502 }
486503 };
487- ($get: ident, $set: ident, $clear: ident, $state_flag: expr, FUNDED_STATES) => {
488- impl_state_flag!($get, $set, $clear, $state_flag, [AwaitingChannelReady, ChannelReady]);
504+ ($get: ident, $set: ident, $clear: ident, FUNDED_STATES) => {
505+ impl_state_flag!($get, $set, $clear, [AwaitingChannelReady, ChannelReady]);
489506 };
490- ($get: ident, $set: ident, $clear: ident, $state_flag: expr, $ state: ident) => {
491- impl_state_flag!($get, $set, $clear, $state_flag, [$state]);
507+ ($get: ident, $set: ident, $clear: ident, $state: ident) => {
508+ impl_state_flag!($get, $set, $clear, [$state]);
492509 };
493510}
494511
@@ -552,22 +569,14 @@ impl ChannelState {
552569 }
553570 }
554571
555- impl_state_flag!(is_peer_disconnected, set_peer_disconnected, clear_peer_disconnected,
556- FundedStateFlags::PEER_DISCONNECTED, FUNDED_STATES);
557- impl_state_flag!(is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress,
558- FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS, FUNDED_STATES);
559- impl_state_flag!(is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent,
560- FundedStateFlags::LOCAL_SHUTDOWN_SENT, FUNDED_STATES);
561- impl_state_flag!(is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent,
562- FundedStateFlags::REMOTE_SHUTDOWN_SENT, FUNDED_STATES);
563- impl_state_flag!(is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready,
564- AwaitingChannelReadyFlags::OUR_CHANNEL_READY, AwaitingChannelReady);
565- impl_state_flag!(is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready,
566- AwaitingChannelReadyFlags::THEIR_CHANNEL_READY, AwaitingChannelReady);
567- impl_state_flag!(is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch,
568- AwaitingChannelReadyFlags::WAITING_FOR_BATCH, AwaitingChannelReady);
569- impl_state_flag!(is_awaiting_remote_revoke, set_awaiting_remote_revoke, clear_awaiting_remote_revoke,
570- ChannelReadyFlags::AWAITING_REMOTE_REVOKE, ChannelReady);
572+ impl_state_flag!(is_peer_disconnected, set_peer_disconnected, clear_peer_disconnected, FUNDED_STATES);
573+ impl_state_flag!(is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress, FUNDED_STATES);
574+ impl_state_flag!(is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent, FUNDED_STATES);
575+ impl_state_flag!(is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent, FUNDED_STATES);
576+ impl_state_flag!(is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready, AwaitingChannelReady);
577+ impl_state_flag!(is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready, AwaitingChannelReady);
578+ impl_state_flag!(is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch, AwaitingChannelReady);
579+ impl_state_flag!(is_awaiting_remote_revoke, set_awaiting_remote_revoke, clear_awaiting_remote_revoke, ChannelReady);
571580}
572581
573582pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
0 commit comments