Skip to content

Commit 34710b0

Browse files
committed
fix(liquidity): Eq for LspNode config; in-place update 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 the prior LspNode if connect/discover fails. Eq ignores discovered protocols. Signed-off-by: Bartok9 <259807879+Bartok9@users.noreply.github.com>
1 parent 9af614b commit 34710b0

2 files changed

Lines changed: 62 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: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,59 @@ 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+
&& existing.token == token
168+
&& existing.trust_peer_0conf == trust_peer_0conf
169+
{
170+
log_info!(self.logger, "LSP node {} already added, skipping.", node_id);
171+
return Ok(());
172+
}
173+
log_info!(
174+
self.logger,
175+
"Updating existing LSP node {} address/config and reconnecting.",
176+
node_id
177+
);
178+
let prev_address = std::mem::replace(&mut existing.address, address.clone());
179+
let prev_token = std::mem::replace(&mut existing.token, token.clone());
180+
let prev_trust =
181+
std::mem::replace(&mut existing.trust_peer_0conf, trust_peer_0conf);
182+
// Force rediscovery after config/address change.
183+
let prev_protocols = std::mem::take(&mut existing.supported_protocols);
184+
previous = Some((prev_address, prev_token, prev_trust, prev_protocols));
185+
} else {
186+
lsp_nodes.push(LspNode {
187+
node_id,
188+
address: address.clone(),
189+
token: token.clone(),
190+
trust_peer_0conf,
191+
supported_protocols: None,
192+
});
167193
}
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-
});
176194
}
177195

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

184212
let con_cm = Arc::clone(&self.connection_manager);
@@ -225,6 +253,7 @@ pub(crate) struct LspConfig {
225253
pub trust_peer_0conf: bool,
226254
}
227255

256+
#[derive(Clone)]
228257
pub(crate) struct LspNode {
229258
node_id: PublicKey,
230259
address: SocketAddress,
@@ -234,6 +263,17 @@ pub(crate) struct LspNode {
234263
supported_protocols: Option<Vec<u16>>,
235264
}
236265

266+
impl PartialEq for LspNode {
267+
fn eq(&self, other: &Self) -> bool {
268+
self.node_id == other.node_id
269+
&& self.address == other.address
270+
&& self.token == other.token
271+
&& self.trust_peer_0conf == other.trust_peer_0conf
272+
}
273+
}
274+
275+
impl Eq for LspNode {}
276+
237277
pub(crate) struct LiquiditySourceBuilder<L: Deref>
238278
where
239279
L::Target: LdkLogger,

0 commit comments

Comments
 (0)