Skip to content

Commit f4c46e3

Browse files
fixup: refactor wip and e2e test wip
1 parent d5d8f92 commit f4c46e3

File tree

5 files changed

+209
-63
lines changed

5 files changed

+209
-63
lines changed

lightning-liquidity/src/lsps2/event.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,16 @@ pub enum LSPS2ServiceEvent {
160160
/// The intercept short channel id to use in the route hint.
161161
intercept_scid: u64,
162162
},
163+
/// You should broadcast the funding transaction for the channel you opened.
164+
///
165+
/// On a client_trusts_lsp context, the client has claimed the payment, so now
166+
/// you must broadcast the funding transaction.
167+
BroadcastFundingTransaction {
168+
/// The node id of the counterparty.
169+
counterparty_node_id: PublicKey,
170+
/// The user channel id that was used to open the channel.
171+
user_channel_id: u128,
172+
/// The funding transaction to broadcast.
173+
funding_tx: bitcoin::Transaction,
174+
},
163175
}

lightning-liquidity/src/lsps2/service.rs

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,15 @@ enum TrustModel {
118118
}
119119

120120
impl TrustModel {
121-
fn should_broadcast(&self) -> bool {
121+
fn should_manually_broadcast(&self) -> bool {
122122
match self {
123123
TrustModel::ClientTrustsLsp {
124124
funding_tx_broadcast_safe,
125125
payment_claimed,
126126
funding_tx,
127127
} => *funding_tx_broadcast_safe && *payment_claimed && funding_tx.is_some(),
128-
TrustModel::LspTrustsClient => true,
128+
// in lsp-trusts-client, the broadcast is automatic, so we never need to manually broadcast.
129+
TrustModel::LspTrustsClient => false,
129130
}
130131
}
131132

@@ -180,6 +181,13 @@ impl TrustModel {
180181
TrustModel::LspTrustsClient => None,
181182
}
182183
}
184+
185+
fn is_client_trusts_lsp(&self) -> bool {
186+
match self {
187+
TrustModel::ClientTrustsLsp { .. } => true,
188+
TrustModel::LspTrustsClient => false,
189+
}
190+
}
183191
}
184192

185193
/// The different states a requested JIT channel can be in.
@@ -459,8 +467,7 @@ struct OutboundJITChannel {
459467
user_channel_id: u128,
460468
opening_fee_params: LSPS2OpeningFeeParams,
461469
payment_size_msat: Option<u64>,
462-
client_trusts_lsp: bool,
463-
trust_model: Option<TrustModel>,
470+
trust_model: TrustModel,
464471
}
465472

466473
impl OutboundJITChannel {
@@ -473,23 +480,15 @@ impl OutboundJITChannel {
473480
state: OutboundJITChannelState::new(),
474481
opening_fee_params,
475482
payment_size_msat,
476-
client_trusts_lsp,
477-
trust_model: None,
483+
trust_model: TrustModel::new(client_trusts_lsp),
478484
}
479485
}
480486

481487
fn htlc_intercepted(
482488
&mut self, htlc: InterceptedHTLC,
483489
) -> Result<Option<HTLCInterceptedAction>, LightningError> {
484-
let was_initial =
485-
matches!(self.state, OutboundJITChannelState::PendingInitialPayment { .. });
486490
let action =
487491
self.state.htlc_intercepted(&self.opening_fee_params, &self.payment_size_msat, htlc)?;
488-
if was_initial && self.trust_model.is_none() {
489-
if !matches!(self.state, OutboundJITChannelState::PendingInitialPayment { .. }) {
490-
self.trust_model = Some(TrustModel::new(self.client_trusts_lsp));
491-
}
492-
}
493492
Ok(action)
494493
}
495494

@@ -508,9 +507,7 @@ impl OutboundJITChannel {
508507
fn payment_forwarded(&mut self) -> Result<Option<ForwardHTLCsAction>, LightningError> {
509508
let action = self.state.payment_forwarded()?;
510509
if action.is_some() {
511-
if let Some(tm) = &mut self.trust_model {
512-
tm.set_payment_claimed(true);
513-
}
510+
self.trust_model.set_payment_claimed(true);
514511
}
515512
Ok(action)
516513
}
@@ -526,43 +523,24 @@ impl OutboundJITChannel {
526523
self.is_pending_initial_payment() && is_expired
527524
}
528525

529-
fn set_funding_tx(&mut self, funding_tx: Transaction) -> Result<(), LightningError> {
530-
if let Some(tm) = &mut self.trust_model {
531-
tm.set_funding_tx(funding_tx);
532-
Ok(())
533-
} else {
534-
Err(LightningError::from(ChannelStateError(
535-
"Store funding transaction when JIT Channel was in invalid state".to_string(),
536-
)))
537-
}
526+
fn set_funding_tx(&mut self, funding_tx: Transaction) {
527+
self.trust_model.set_funding_tx(funding_tx);
538528
}
539529

540-
fn set_funding_tx_broadcast_safe(
541-
&mut self, funding_tx_broadcast_safe: bool,
542-
) -> Result<(), LightningError> {
543-
if let Some(tm) = &mut self.trust_model {
544-
tm.set_funding_tx_broadcast_safe(funding_tx_broadcast_safe);
545-
Ok(())
546-
} else {
547-
Err(LightningError::from(ChannelStateError(
548-
"Store funding transaction broadcast safe when JIT Channel was in invalid state"
549-
.to_string(),
550-
)))
551-
}
530+
fn set_funding_tx_broadcast_safe(&mut self, funding_tx_broadcast_safe: bool) {
531+
self.trust_model.set_funding_tx_broadcast_safe(funding_tx_broadcast_safe);
552532
}
553533

554534
fn should_broadcast_funding_transaction(&self) -> bool {
555-
self.trust_model.as_ref().map_or(false, |tm| tm.should_broadcast())
535+
self.trust_model.should_manually_broadcast()
556536
}
557537

558538
fn get_funding_tx(&self) -> Option<Transaction> {
559-
self.trust_model.as_ref().and_then(|tm| tm.get_funding_tx())
539+
self.trust_model.get_funding_tx()
560540
}
561541

562542
fn client_trusts_lsp(&self) -> bool {
563-
self.trust_model
564-
.as_ref()
565-
.map_or(false, |tm| matches!(tm, TrustModel::ClientTrustsLsp { .. }))
543+
self.trust_model.is_client_trusts_lsp()
566544
}
567545
}
568546

@@ -1065,7 +1043,10 @@ where
10651043
},
10661044
}
10671045

1068-
self.broadcast_transaction_if_applies(&jit_channel);
1046+
self.emit_broadcast_funding_transaction_event_if_applies(
1047+
jit_channel,
1048+
counterparty_node_id,
1049+
);
10691050
}
10701051
} else {
10711052
return Err(APIError::APIMisuseError {
@@ -1616,11 +1597,9 @@ where
16161597
),
16171598
})?;
16181599

1619-
jit_channel
1620-
.set_funding_tx(funding_tx)
1621-
.map_err(|e| APIError::APIMisuseError { err: e.err.to_string() })?;
1600+
jit_channel.set_funding_tx(funding_tx);
16221601

1623-
self.broadcast_transaction_if_applies(jit_channel);
1602+
self.emit_broadcast_funding_transaction_event_if_applies(jit_channel, counterparty_node_id);
16241603
Ok(())
16251604
}
16261605

@@ -1654,20 +1633,26 @@ where
16541633
),
16551634
})?;
16561635

1657-
jit_channel
1658-
.set_funding_tx_broadcast_safe(true)
1659-
.map_err(|e| APIError::APIMisuseError { err: e.err.to_string() })?;
1636+
jit_channel.set_funding_tx_broadcast_safe(true);
16601637

1661-
self.broadcast_transaction_if_applies(jit_channel);
1638+
self.emit_broadcast_funding_transaction_event_if_applies(jit_channel, counterparty_node_id);
16621639
Ok(())
16631640
}
16641641

1665-
fn broadcast_transaction_if_applies(&self, jit_channel: &OutboundJITChannel) {
1642+
fn emit_broadcast_funding_transaction_event_if_applies(
1643+
&self, jit_channel: &OutboundJITChannel, counterparty_node_id: &PublicKey,
1644+
) {
16661645
if jit_channel.should_broadcast_funding_transaction() {
16671646
let funding_tx = jit_channel.get_funding_tx();
16681647

16691648
if let Some(funding_tx) = funding_tx {
1670-
self.channel_manager.get_cm().broadcast_transaction(&funding_tx);
1649+
let event_queue_notifier = self.pending_events.notifier();
1650+
let event = LSPS2ServiceEvent::BroadcastFundingTransaction {
1651+
counterparty_node_id: *counterparty_node_id,
1652+
user_channel_id: jit_channel.user_channel_id,
1653+
funding_tx,
1654+
};
1655+
event_queue_notifier.enqueue(event);
16711656
}
16721657
}
16731658
}

lightning-liquidity/tests/common/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ pub(crate) fn create_service_and_client_nodes<'a, 'b, 'c>(
5353
let mut iter = nodes.into_iter();
5454
let service_node = LiquidityNode::new(iter.next().unwrap(), service_lm);
5555
let client_node = LiquidityNode::new(iter.next().unwrap(), client_lm);
56-
let payer_node_optional = iter.next();
5756

58-
LSPSNodes { service_node, client_node, payer_node_optional }
57+
LSPSNodes { service_node, client_node, payer_node_optional: iter.next() }
5958
}
6059

6160
pub(crate) struct LiquidityNode<'a, 'b, 'c> {

0 commit comments

Comments
 (0)