@@ -267,7 +267,7 @@ enum HTLCUpdateAwaitingACK {
267
267
}
268
268
269
269
macro_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) => {
271
271
#[doc = $flag_type_doc]
272
272
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq)]
273
273
struct $flag_type(u32);
@@ -296,7 +296,6 @@ macro_rules! define_state_flags {
296
296
297
297
#[allow(unused)]
298
298
fn is_empty(&self) -> bool { self.0 == 0 }
299
-
300
299
#[allow(unused)]
301
300
fn is_set(&self, flag: Self) -> bool { *self & flag == flag }
302
301
#[allow(unused)]
@@ -305,10 +304,10 @@ macro_rules! define_state_flags {
305
304
fn clear(&mut self, flag: Self) -> Self { self.0 &= !flag.0; *self }
306
305
}
307
306
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
+
312
311
impl core::ops::BitOr for $flag_type {
313
312
type Output = Self;
314
313
fn bitor(self, rhs: Self) -> Self::Output { Self(self.0 | rhs.0) }
@@ -339,6 +338,16 @@ macro_rules! define_state_flags {
339
338
};
340
339
($flag_type_doc: expr, FUNDED_STATE, $flag_type: ident, $flags: tt) => {
341
340
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
+
342
351
impl core::ops::BitOr<FundedStateFlags> for $flag_type {
343
352
type Output = Self;
344
353
fn bitor(self, rhs: FundedStateFlags) -> Self::Output { Self(self.0 | rhs.0) }
@@ -385,25 +394,29 @@ define_state_flags!(
385
394
"Flags that apply to all [`ChannelState`] variants in which the channel is funded.",
386
395
FundedStateFlags, [
387
396
("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),
389
399
("Indicates the user has told us a `ChannelMonitor` update is pending async persistence \
390
400
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),
392
403
("Indicates we received a `shutdown` message from the remote end. If set, they may not add \
393
404
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),
395
407
("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)
397
410
]
398
411
);
399
412
400
413
define_state_flags!(
401
414
"Flags that only apply to [`ChannelState::NegotiatingFunding`].",
402
415
NegotiatingFundingFlags, [
403
416
("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 ),
405
418
("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 )
407
420
]
408
421
);
409
422
@@ -412,13 +425,16 @@ define_state_flags!(
412
425
FUNDED_STATE, AwaitingChannelReadyFlags, [
413
426
("Indicates they sent us a `channel_ready` message. Once both `THEIR_CHANNEL_READY` and \
414
427
`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),
416
430
("Indicates we sent them a `channel_ready` message. Once both `THEIR_CHANNEL_READY` and \
417
431
`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),
419
434
("Indicates the channel was funded in a batch and the broadcast of the funding transaction \
420
435
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)
422
438
]
423
439
);
424
440
@@ -429,7 +445,8 @@ define_state_flags!(
429
445
`revoke_and_ack` message. During this period, we can't generate new `commitment_signed` \
430
446
messages as we'd be unable to determine which HTLCs they included in their `revoke_and_ack` \
431
447
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)
433
450
]
434
451
);
435
452
@@ -455,12 +472,12 @@ enum ChannelState {
455
472
}
456
473
457
474
macro_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),+]) => {
459
476
#[allow(unused)]
460
477
fn $get(&self) -> bool {
461
478
match self {
462
479
$(
463
- ChannelState::$state(flags) => flags.is_set($state_flag.into() ),
480
+ ChannelState::$state(flags) => flags.$get( ),
464
481
)*
465
482
_ => false,
466
483
}
@@ -469,7 +486,7 @@ macro_rules! impl_state_flag {
469
486
fn $set(&mut self) {
470
487
match self {
471
488
$(
472
- ChannelState::$state(flags) => * flags |= $state_flag ,
489
+ ChannelState::$state(flags) => flags.$set() ,
473
490
)*
474
491
_ => debug_assert!(false, "Attempted to set flag on unexpected ChannelState"),
475
492
}
@@ -478,17 +495,17 @@ macro_rules! impl_state_flag {
478
495
fn $clear(&mut self) {
479
496
match self {
480
497
$(
481
- ChannelState::$state(flags) => *flags &= !($state_flag) ,
498
+ ChannelState::$state(flags) => { let _ = flags.$clear(); } ,
482
499
)*
483
500
_ => debug_assert!(false, "Attempted to clear flag on unexpected ChannelState"),
484
501
}
485
502
}
486
503
};
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]);
489
506
};
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]);
492
509
};
493
510
}
494
511
@@ -552,22 +569,14 @@ impl ChannelState {
552
569
}
553
570
}
554
571
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);
571
580
}
572
581
573
582
pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
0 commit comments