Skip to content

Commit 7eb07ce

Browse files
committed
fix(liquidity): update LspNode in place on re-add
Builder and runtime add_liquidity_source update address/token/0conf when the same LSP node_id is re-added. Runtime reconnects and restores prior fields if connect/discover fails. Skip when node_id+address already match. Signed-off-by: Bartok9 <259807879+Bartok9@users.noreply.github.com>
1 parent 9af614b commit 7eb07ce

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

‎src/builder.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl NodeBuilder {
522522
/// 0-confirmation channels opened by this LSP. If `false`, 0-confirmation
523523
/// acceptance for this peer falls back to [`Config::trusted_peers_0conf`].
524524
///
525-
/// May be called multiple times to register several LSPs. Duplicate `node_id`s are ignored.
525+
/// May be called multiple times to register several LSPs. Re-adding an existing `node_id` updates its address, token, and 0conf trust settings.
526526
///
527527
/// [bLIP-50 / LSPS0]: https://github.com/lightning/blips/blob/master/blip-0050.md
528528
pub fn add_liquidity_source(
@@ -532,7 +532,12 @@ impl NodeBuilder {
532532
let liquidity_source_config =
533533
self.liquidity_source_config.get_or_insert(LiquiditySourceConfig::default());
534534

535-
if liquidity_source_config.lsp_nodes.iter().any(|n| n.node_id == node_id) {
535+
if let Some(existing) =
536+
liquidity_source_config.lsp_nodes.iter_mut().find(|n| n.node_id == node_id)
537+
{
538+
existing.address = address;
539+
existing.token = token;
540+
existing.trust_peer_0conf = trust_peer_0conf;
536541
return self;
537542
}
538543

@@ -1176,7 +1181,7 @@ impl Builder {
11761181
/// 0-confirmation channels opened by this LSP. If `false`, 0-confirmation
11771182
/// acceptance for this peer falls back to [`Config::trusted_peers_0conf`].
11781183
///
1179-
/// May be called multiple times to register several LSPs. Duplicate `node_id`s are ignored.
1184+
/// May be called multiple times to register several LSPs. Re-adding an existing `node_id` updates its address, token, and 0conf trust settings.
11801185
///
11811186
/// [bLIP-50 / LSPS0]: https://github.com/lightning/blips/blob/master/blip-0050.md
11821187
pub fn add_liquidity_source(

‎src/liquidity/mod.rs‎

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,56 @@ impl Liquidity {
154154
/// The given `token` will be used by the LSP to authenticate the user.
155155
/// `trust_peer_0conf` controls whether the node will accept 0-confirmation channels opened by this
156156
/// LSP. Note this supersedes [`Config::trusted_peers_0conf`] for this peer.
157-
/// Duplicate `node_id`s are ignored.
157+
/// Re-adding an existing `node_id` updates its address/token/0conf settings and reconnects.
158158
pub fn add_liquidity_source(
159159
&self, node_id: PublicKey, address: SocketAddress, token: Option<String>,
160160
trust_peer_0conf: bool,
161161
) -> Result<(), Error> {
162+
let mut previous: Option<(SocketAddress, Option<String>, bool, Option<Vec<u16>>)> = None;
162163
{
163164
let mut lsp_nodes = self.liquidity_source.lsp_nodes.write().expect("lock");
164-
if lsp_nodes.iter().any(|n| n.node_id == node_id) {
165-
log_info!(self.logger, "LSP node {} already added, skipping.", node_id);
166-
return Ok(());
165+
if let Some(existing) = lsp_nodes.iter_mut().find(|n| n.node_id == node_id) {
166+
if existing.address == address {
167+
log_info!(self.logger, "LSP node {} already added, skipping.", node_id);
168+
return Ok(());
169+
}
170+
log_info!(
171+
self.logger,
172+
"Updating existing LSP node {} address/config and reconnecting.",
173+
node_id
174+
);
175+
let prev_address = std::mem::replace(&mut existing.address, address.clone());
176+
let prev_token = std::mem::replace(&mut existing.token, token.clone());
177+
let prev_trust =
178+
std::mem::replace(&mut existing.trust_peer_0conf, trust_peer_0conf);
179+
// Force rediscovery after config/address change.
180+
let prev_protocols = std::mem::take(&mut existing.supported_protocols);
181+
previous = Some((prev_address, prev_token, prev_trust, prev_protocols));
182+
} else {
183+
lsp_nodes.push(LspNode {
184+
node_id,
185+
address: address.clone(),
186+
token: token.clone(),
187+
trust_peer_0conf,
188+
supported_protocols: None,
189+
});
167190
}
168-
169-
lsp_nodes.push(LspNode {
170-
node_id,
171-
address: address.clone(),
172-
token: token.clone(),
173-
trust_peer_0conf,
174-
supported_protocols: None,
175-
});
176191
}
177192

178-
// If anything below fails, drop the half-initialized entry so the user can retry cleanly.
193+
// On failure: remove half-initialized inserts; restore prior config for updates.
179194
let lsp_nodes = Arc::clone(&self.liquidity_source.lsp_nodes);
180195
let cleanup = move || {
181-
lsp_nodes.write().expect("lock").retain(|n| n.node_id != node_id);
196+
let mut nodes = lsp_nodes.write().expect("lock");
197+
if let Some((prev_address, prev_token, prev_trust, prev_protocols)) = previous {
198+
if let Some(existing) = nodes.iter_mut().find(|n| n.node_id == node_id) {
199+
existing.address = prev_address;
200+
existing.token = prev_token;
201+
existing.trust_peer_0conf = prev_trust;
202+
existing.supported_protocols = prev_protocols;
203+
}
204+
} else {
205+
nodes.retain(|n| n.node_id != node_id);
206+
}
182207
};
183208

184209
let con_cm = Arc::clone(&self.connection_manager);

0 commit comments

Comments
 (0)