Skip to content

Commit 1eb0965

Browse files
committed
Track pending channel opens to prevent duplicates
After the previous commit moved usability checks to execute time, the timer can call process_htlcs_for_peer repeatedly while a channel is still opening. calculate_htlc_actions_for_peer sees no is_channel_ready channels and requests a new one each time, producing duplicate OpenChannel events. Add a pending_channel_opens set (RwLock<HashSet<PublicKey>>). execute_htlc_actions inserts the peer when it emits OpenChannel, and channel_ready removes it. If the set already contains the peer, the OpenChannel is suppressed. calculate_htlc_actions_for_peer now filters by is_channel_ready instead of including all channels. Channels still opening (is_channel_ready=false) report outbound_capacity_msat but reject forwards with "Channel is still opening", consuming the InterceptId and losing the HTLC. These are zero-conf channels, so on-chain confirmation is not the issue; the channel simply hasn't finished its opening handshake yet. Reestablishing channels (is_channel_ready=true, is_usable=false) can forward once reestablish completes and are included, preserving the spurious-open fix from the previous commit.
1 parent 331d1bd commit 1eb0965

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

lightning-liquidity/src/lsps4/service.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ where
101101
scid_store: ScidStore<L, K>,
102102
htlc_store: HTLCStore<L, K>,
103103
connected_peers: RwLock<HashSet<PublicKey>>,
104+
pending_channel_opens: RwLock<HashSet<PublicKey>>,
104105
config: LSPS4ServiceConfig,
105106
}
106107

@@ -128,6 +129,7 @@ where
128129
config,
129130
logger,
130131
connected_peers: RwLock::new(HashSet::new()),
132+
pending_channel_opens: RwLock::new(HashSet::new()),
131133
})
132134
}
133135

@@ -257,6 +259,7 @@ where
257259
pub fn channel_ready(
258260
&self, counterparty_node_id: &PublicKey,
259261
) -> Result<(), APIError> {
262+
self.pending_channel_opens.write().unwrap().remove(counterparty_node_id);
260263
let is_connected = self.is_peer_connected(counterparty_node_id);
261264

262265
log_info!(
@@ -581,8 +584,17 @@ where
581584
self.channel_manager.get_cm().list_channels_with_counterparty(&their_node_id);
582585
let channel_count = channels.len();
583586

587+
// Include channels that finished opening (is_channel_ready) in capacity
588+
// decisions, even if still reestablishing (is_usable=false). This prevents
589+
// spurious channel opens during reestablish. Channels still opening
590+
// (is_channel_ready=false) are excluded: they report outbound_capacity_msat
591+
// but reject forwards, consuming the InterceptId and losing the HTLC.
592+
// execute_htlc_actions checks usability before forwarding.
584593
let mut channel_capacity_map: HashMap<ChannelId, u64> = new_hash_map();
585594
for channel in &channels {
595+
if !channel.is_channel_ready {
596+
continue;
597+
}
586598
channel_capacity_map.insert(channel.channel_id, channel.outbound_capacity_msat);
587599
log_info!(
588600
self.logger,
@@ -773,21 +785,30 @@ where
773785
}
774786
}
775787

776-
// Handle new channel opening
788+
// Handle new channel opening — skip if one is already in flight.
777789
if let Some(channel_size_msat) = actions.new_channel_needed_msat {
790+
if self.pending_channel_opens.read().unwrap().contains(&their_node_id) {
791+
log_info!(
792+
self.logger,
793+
"[LSPS4] execute_htlc_actions: peer {} needs a new channel but one is \
794+
already opening, skipping duplicate OpenChannel",
795+
their_node_id
796+
);
797+
} else {
778798
log_info!(
779799
self.logger,
780800
"Need a new channel with peer {} for {}msat to forward HTLCs",
781801
their_node_id,
782802
channel_size_msat
783803
);
784-
804+
self.pending_channel_opens.write().unwrap().insert(their_node_id);
785805
let mut event_queue_notifier = self.pending_events.notifier();
786806
event_queue_notifier.enqueue(crate::events::LiquidityEvent::LSPS4Service(LSPS4ServiceEvent::OpenChannel {
787807
their_network_key: their_node_id,
788808
amt_to_forward_msat: channel_size_msat,
789809
channel_count: actions.channel_count,
790810
}));
811+
}
791812
}
792813
}
793814

0 commit comments

Comments
 (0)