Skip to content

Commit 3ec8ac4

Browse files
committed
f dry somewhat
1 parent 754d235 commit 3ec8ac4

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
@@ -3302,11 +3302,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33023302
}
33033303
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply.",
33043304
log_bytes!(self.channel_id));
3305-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3306-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3307-
update: monitor_update, blocked: !release_monitor
3308-
});
3309-
return Ok(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None });
3305+
return Ok(self.push_ret_blockable_mon_update(monitor_update));
33103306
}
33113307

33123308
let need_commitment_signed = if need_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
@@ -3323,12 +3319,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33233319

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

33343326
/// Public version of the below, checking relevant preconditions first.
@@ -3443,12 +3435,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
34433435
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len());
34443436

34453437
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
3446-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3447-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3448-
update: monitor_update, blocked: !release_monitor,
3449-
});
3450-
(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None },
3451-
htlcs_to_fail)
3438+
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
34523439
} else {
34533440
(None, Vec::new())
34543441
}
@@ -3656,12 +3643,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36563643
self.monitor_pending_failures.append(&mut revoked_htlcs);
36573644
self.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
36583645
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
3659-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3660-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3661-
update: monitor_update, blocked: !release_monitor,
3662-
});
3663-
return Ok((Vec::new(),
3664-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }));
3646+
return Ok((Vec::new(), self.push_ret_blockable_mon_update(monitor_update)));
36653647
}
36663648

36673649
match self.free_holding_cell_htlcs(logger) {
@@ -3673,12 +3655,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36733655
monitor_update.updates.append(&mut additional_update.updates);
36743656

36753657
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3676-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3677-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3678-
update: monitor_update, blocked: !release_monitor,
3679-
});
3680-
Ok((htlcs_to_fail,
3681-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
3658+
Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
36823659
},
36833660
(None, htlcs_to_fail) => {
36843661
if require_commitment {
@@ -3692,21 +3669,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36923669
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
36933670
log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
36943671
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3695-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3696-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3697-
update: monitor_update, blocked: !release_monitor,
3698-
});
3699-
Ok((htlcs_to_fail,
3700-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
3672+
Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
37013673
} else {
37023674
log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
37033675
self.monitor_updating_paused(false, false, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3704-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
3705-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3706-
update: monitor_update, blocked: !release_monitor,
3707-
});
3708-
Ok((htlcs_to_fail,
3709-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
3676+
Ok((htlcs_to_fail, self.push_ret_blockable_mon_update(monitor_update)))
37103677
}
37113678
}
37123679
}
@@ -4443,11 +4410,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44434410
}],
44444411
};
44454412
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
4446-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
4447-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
4448-
update: monitor_update, blocked: !release_monitor,
4449-
});
4450-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
4413+
if self.push_blockable_mon_update(monitor_update) {
4414+
self.pending_monitor_updates.last().map(|upd| &upd.update)
4415+
} else { None }
44514416
} else { None };
44524417
let shutdown = if send_shutdown {
44534418
Some(msgs::Shutdown {
@@ -5032,6 +4997,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50324997
None
50334998
}
50344999

5000+
/// Pushes a new monitor update into our monitor update queue, returning whether it should be
5001+
/// immediately given to the user for persisting or if it should be held as blocked.
5002+
fn push_blockable_mon_update(&mut self, update: ChannelMonitorUpdate) -> bool {
5003+
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
5004+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
5005+
update, blocked: !release_monitor
5006+
});
5007+
release_monitor
5008+
}
5009+
5010+
/// Pushes a new monitor update into our monitor update queue, returning a reference to it if
5011+
/// it should be immediately given to the user for persisting or `None` if it should be held as
5012+
/// blocked.
5013+
fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
5014+
-> Option<&ChannelMonitorUpdate> {
5015+
let release_monitor = self.push_blockable_mon_update(update);
5016+
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
5017+
}
5018+
50355019
pub fn no_monitor_updates_pending(&self) -> bool {
50365020
self.pending_monitor_updates.is_empty()
50375021
}
@@ -6085,12 +6069,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
60856069
Some(_) => {
60866070
let monitor_update = self.build_commitment_no_status_check(logger);
60876071
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
6088-
6089-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
6090-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
6091-
update: monitor_update, blocked: !release_monitor,
6092-
});
6093-
Ok(if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None })
6072+
Ok(self.push_ret_blockable_mon_update(monitor_update))
60946073
},
60956074
None => Ok(None)
60966075
}
@@ -6179,11 +6158,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
61796158
}],
61806159
};
61816160
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
6182-
let release_monitor = self.pending_monitor_updates.iter().all(|upd| !upd.blocked);
6183-
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
6184-
update: monitor_update, blocked: !release_monitor,
6185-
});
6186-
if release_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
6161+
if self.push_blockable_mon_update(monitor_update) {
6162+
self.pending_monitor_updates.last().map(|upd| &upd.update)
6163+
} else { None }
61876164
} else { None };
61886165
let shutdown = msgs::Shutdown {
61896166
channel_id: self.channel_id,

0 commit comments

Comments
 (0)