Skip to content

Commit 02fbddd

Browse files
authored
Merge pull request #124 from bitwalt/private-channel-swap-routing
Fix private-channel swap routing
2 parents ffdcb31 + 21cc5cf commit 02fbddd

8 files changed

Lines changed: 97 additions & 2 deletions

src/routes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,7 @@ pub(crate) async fn maker_execute(
28122812
let first_leg = get_route(
28132813
&unlocked_state.channel_manager,
28142814
&unlocked_state.router,
2815+
&state.static_state.ldk_data_dir,
28152816
unlocked_state.channel_manager.get_our_node_id(),
28162817
taker_pk,
28172818
if swap_info.is_to_btc() {
@@ -2829,6 +2830,7 @@ pub(crate) async fn maker_execute(
28292830
let second_leg = get_route(
28302831
&unlocked_state.channel_manager,
28312832
&unlocked_state.router,
2833+
&state.static_state.ldk_data_dir,
28322834
taker_pk,
28332835
unlocked_state.channel_manager.get_our_node_id(),
28342836
if swap_info.is_to_btc() || swap_info.is_asset_asset() {

src/test/drop_funding_signed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ async fn drop_funding_signed() {
4141
None,
4242
None,
4343
true,
44+
true,
4445
)
4546
.await
4647
.unwrap();
@@ -94,6 +95,7 @@ async fn drop_funding_signed() {
9495
None,
9596
None,
9697
true,
98+
true,
9799
)
98100
.await
99101
.unwrap();

src/test/missing_acceptor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async fn missing_acceptor() {
4242
None,
4343
None,
4444
true,
45+
true,
4546
)
4647
.await
4748
.unwrap();

src/test/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ async fn open_channel_with_retry(
12221222
None,
12231223
None,
12241224
true,
1225+
true,
12251226
)
12261227
.await;
12271228

@@ -1259,6 +1260,7 @@ async fn open_channel_funded_raw(
12591260
fee_proportional_millionths: Option<u32>,
12601261
temporary_channel_id: Option<&str>,
12611262
with_anchors: bool,
1263+
public: bool,
12621264
) -> Result<Channel, Response> {
12631265
open_channel_raw(
12641266
node_address,
@@ -1273,6 +1275,7 @@ async fn open_channel_funded_raw(
12731275
fee_proportional_millionths,
12741276
temporary_channel_id,
12751277
with_anchors,
1278+
public,
12761279
)
12771280
.await?;
12781281

@@ -1352,6 +1355,7 @@ async fn open_channel_raw(
13521355
fee_proportional_millionths: Option<u32>,
13531356
temporary_channel_id: Option<&str>,
13541357
with_anchors: bool,
1358+
public: bool,
13551359
) -> Result<OpenChannelResponse, Response> {
13561360
println!(
13571361
"opening channel with {asset_amount:?} of asset {asset_id:?} from node {node_address} \
@@ -1383,7 +1387,7 @@ async fn open_channel_raw(
13831387
asset_amount,
13841388
asset_id: asset_id.map(|a| a.to_string()),
13851389
push_asset_amount,
1386-
public: true,
1390+
public,
13871391
with_anchors,
13881392
fee_base_msat,
13891393
fee_proportional_millionths,
@@ -1432,6 +1436,7 @@ async fn open_channel_with_custom_data(
14321436
fee_proportional_millionths,
14331437
temporary_channel_id,
14341438
with_anchors,
1439+
true,
14351440
)
14361441
.await
14371442
.expect("channel opening should succeed")

src/test/openchannel_fail.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async fn openchannel_fail() {
3535
None,
3636
None,
3737
true,
38+
true,
3839
)
3940
.await;
4041
check_response_is_nok(
@@ -65,6 +66,7 @@ async fn openchannel_fail() {
6566
None,
6667
None,
6768
true,
69+
true,
6870
)
6971
.await;
7072
check_response_is_nok(
@@ -95,6 +97,7 @@ async fn openchannel_fail() {
9597
None,
9698
None,
9799
true,
100+
true,
98101
)
99102
.await;
100103
check_response_is_nok(

src/test/openchannel_no_indexer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async fn openchannel_no_indexer() {
3636
None,
3737
None,
3838
true,
39+
true,
3940
)
4041
.await;
4142

src/test/swap_reverse_same_channel.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,66 @@ async fn swap_reverse_same_channel() {
186186
assert_eq!(swaps_taker.taker.len(), 1);
187187
let swap_taker = swaps_taker.taker.first().unwrap();
188188
assert_eq!(swap_taker.status, SwapStatus::Succeeded);
189+
190+
let private_asset_id = issue_asset_nia(node1_addr).await.asset_id;
191+
open_channel_funded_raw(
192+
node1_addr,
193+
&node2_pubkey,
194+
Some(NODE2_PEER_PORT),
195+
Some(100000),
196+
Some(50000000),
197+
Some(100),
198+
Some(&private_asset_id),
199+
None,
200+
None,
201+
None,
202+
None,
203+
true,
204+
false,
205+
)
206+
.await
207+
.expect("private channel opening should succeed");
208+
209+
println!("\nsetup private channel swap");
210+
let maker_addr = node1_addr;
211+
let taker_addr = node2_addr;
212+
let qty_from = 25000;
213+
let qty_to = 10;
214+
let maker_init_response = maker_init(
215+
maker_addr,
216+
qty_from,
217+
None,
218+
qty_to,
219+
Some(&private_asset_id),
220+
3600,
221+
)
222+
.await;
223+
taker(taker_addr, maker_init_response.swapstring.clone()).await;
224+
225+
println!("\nexecute private channel swap");
226+
maker_execute(
227+
maker_addr,
228+
maker_init_response.swapstring,
229+
maker_init_response.payment_secret,
230+
node2_pubkey,
231+
)
232+
.await;
233+
234+
let swaps_maker = list_swaps(maker_addr).await;
235+
assert_eq!(swaps_maker.maker.len(), 2);
236+
let swap_maker = swaps_maker
237+
.maker
238+
.iter()
239+
.find(|s| s.payment_hash == maker_init_response.payment_hash)
240+
.unwrap();
241+
assert_eq!(swap_maker.status, SwapStatus::Pending);
242+
wait_for_swap_status(
243+
taker_addr,
244+
&maker_init_response.payment_hash,
245+
SwapStatus::Pending,
246+
)
247+
.await;
248+
249+
wait_for_ln_balance(maker_addr, &private_asset_id, 90).await;
250+
wait_for_ln_balance(taker_addr, &private_asset_id, 10).await;
189251
}

src/utils.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,16 +405,35 @@ pub(crate) fn get_max_local_rgb_amount<'r>(
405405
max_balance
406406
}
407407

408+
#[allow(clippy::too_many_arguments)]
408409
pub(crate) fn get_route(
409410
channel_manager: &crate::ldk::ChannelManager,
410411
router: &crate::ldk::Router,
412+
ldk_data_dir_path: &Path,
411413
start: PublicKey,
412414
dest: PublicKey,
413415
final_value_msat: Option<u64>,
414416
rgb_payment: Option<(ContractId, u64)>,
415417
hints: Vec<RouteHint>,
416418
) -> Option<Route> {
417419
let inflight_htlcs = channel_manager.compute_inflight_htlcs();
420+
let usable_channels;
421+
let first_hops = if start == channel_manager.get_our_node_id() {
422+
usable_channels = channel_manager.list_usable_channels();
423+
let first_hops = usable_channels
424+
.iter()
425+
.filter(|channel| match rgb_payment {
426+
Some((contract_id, _)) => {
427+
get_rgb_channel_info_optional(&channel.channel_id, ldk_data_dir_path, false)
428+
.is_some_and(|(rgb_info, _)| rgb_info.contract_id == contract_id)
429+
}
430+
None => true,
431+
})
432+
.collect::<Vec<_>>();
433+
(!first_hops.is_empty()).then_some(first_hops)
434+
} else {
435+
None
436+
};
418437
let payment_params = PaymentParameters {
419438
payee: Payee::Clear {
420439
node_id: dest,
@@ -438,7 +457,7 @@ pub(crate) fn get_route(
438457
max_total_routing_fee_msat: None,
439458
rgb_payment,
440459
},
441-
None,
460+
first_hops.as_deref(),
442461
inflight_htlcs,
443462
);
444463

0 commit comments

Comments
 (0)