Skip to content

Commit cd398dc

Browse files
committed
f dry somewhat
1 parent 6958d22 commit cd398dc

File tree

1 file changed

+33
-56
lines changed

1 file changed

+33
-56
lines changed

lightning/src/ln/channel.rs

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,11 +3300,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33003300
}
33013301
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply.",
33023302
log_bytes!(self.channel_id));
3303-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3304-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3305-
update: monitor_update, blocked: !release_monitor
3306-
});
3307-
return Ok(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None });
3303+
return Ok(self.push_ret_blockable_mon_update(monitor_update));
33083304
}
33093305

33103306
let need_commitment_signed = if need_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
@@ -3321,12 +3317,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33213317

33223318
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack.",
33233319
log_bytes!(self.channel_id()), if need_commitment_signed { " our own commitment_signed and" } else { "" });
3324-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3325-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3326-
update: monitor_update, blocked: !release_monitor,
3327-
});
33283320
self.monitor_updating_paused(true, need_commitment_signed, false, Vec::new(), Vec::new(), Vec::new());
3329-
return Ok(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None });
3321+
return Ok(self.push_ret_blockable_mon_update(monitor_update));
33303322
}
33313323

33323324
/// Public version of the below, checking relevant preconditions first.
@@ -3441,12 +3433,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
34413433
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len());
34423434

34433435
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
3444-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3445-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3446-
update: monitor_update, blocked: !release_monitor,
3447-
});
3448-
(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None },
3449-
htlcs_to_fail)
3436+
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
34503437
} else {
34513438
(None, Vec::new())
34523439
}
@@ -3654,12 +3641,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36543641
self.monitor_pending_failures.append(&mut revoked_htlcs);
36553642
self.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
36563643
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
3657-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3658-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3659-
update: monitor_update, blocked: !release_monitor,
3660-
});
3661-
return Ok((Vec::new(),
3662-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }));
3644+
return Ok((Vec::new(), self.push_ret_blockable_mon_update(monitor_update)));
36633645
}
36643646

36653647
match self.free_holding_cell_htlcs(logger) {
@@ -3671,12 +3653,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36713653
monitor_update.updates.append(&mut additional_update.updates);
36723654

36733655
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3674-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3675-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3676-
update: monitor_update, blocked: !release_monitor,
3677-
});
3678-
Ok((htlcs_to_fail,
3679-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
3656+
Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
36803657
},
36813658
(None, htlcs_to_fail) => {
36823659
if require_commitment {
@@ -3690,21 +3667,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36903667
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
36913668
log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
36923669
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3693-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3694-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3695-
update: monitor_update, blocked: !release_monitor,
3696-
});
3697-
Ok((htlcs_to_fail,
3698-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
3670+
Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
36993671
} else {
37003672
log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
37013673
self.monitor_updating_paused(false, false, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3702-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3703-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3704-
update: monitor_update, blocked: !release_monitor,
3705-
});
3706-
Ok((htlcs_to_fail,
3707-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
3674+
Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
37083675
}
37093676
}
37103677
}
@@ -4441,11 +4408,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44414408
}],
44424409
};
44434410
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
4444-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
4445-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
4446-
update: monitor_update, blocked: !release_monitor,
4447-
});
4448-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
4411+
if self.push_blockable_mon_update(monitor_update) {
4412+
self.pending_monitor_updates.last().map(|upd| &upd.update)
4413+
} else { None }
44494414
} else { None };
44504415
let shutdown = if send_shutdown {
44514416
Some(msgs::Shutdown {
@@ -5030,6 +4995,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50304995
None
50314996
}
50324997

4998+
/// Pushes a new monitor update into our monitor update queue, returning whether it should be
4999+
/// immediately given to the user for persisting or if it should be held as blocked.
5000+
fn push_blockable_mon_update(&mut self, update: ChannelMonitorUpdate) -> bool {
5001+
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
5002+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
5003+
update, blocked: !release_monitor
5004+
});
5005+
release_monitor
5006+
}
5007+
5008+
/// Pushes a new monitor update into our monitor update queue, returning a reference to it if
5009+
/// it should be immediately given to the user for persisting or `None` if it should be held as
5010+
/// blocked.
5011+
fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
5012+
-> Option<&ChannelMonitorUpdate> {
5013+
let release_monitor = self.push_blockable_mon_update(update);
5014+
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
5015+
}
5016+
50335017
pub fn no_monitor_updates_pending(&self) -> bool {
50345018
self.pending_monitor_updates.is_empty()
50355019
}
@@ -6083,12 +6067,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
60836067
Some(_) => {
60846068
let monitor_update = self.build_commitment_no_status_check(logger);
60856069
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
6086-
6087-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
6088-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
6089-
update: monitor_update, blocked: !release_monitor,
6090-
});
6091-
Ok(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None })
6070+
Ok(self.push_ret_blockable_mon_update(monitor_update))
60926071
},
60936072
None => Ok(None)
60946073
}
@@ -6177,11 +6156,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
61776156
}],
61786157
};
61796158
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
6180-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
6181-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
6182-
update: monitor_update, blocked: !release_monitor,
6183-
});
6184-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
6159+
if self.push_blockable_mon_update(monitor_update) {
6160+
self.pending_monitor_updates.last().map(|upd| &upd.update)
6161+
} else { None }
61856162
} else { None };
61866163
let shutdown = msgs::Shutdown {
61876164
channel_id: self.channel_id,

0 commit comments

Comments
 (0)