Skip to content

Commit 0541b4a

Browse files
committed
improve closechannel checks
1 parent bc08248 commit 0541b4a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub enum APIError {
3636
#[error("Batch transfer not found")]
3737
BatchTransferNotFound,
3838

39+
#[error("Cannot close channel")]
40+
CannotCloseChannel(String),
41+
3942
#[error("Cannot estimate fees")]
4043
CannotEstimateFees,
4144

@@ -270,6 +273,9 @@ pub enum APIError {
270273
#[error("Unexpected error: {0}")]
271274
Unexpected(String),
272275

276+
#[error("Unknown channel ID")]
277+
UnknownChannelId,
278+
273279
#[error("Unknown RGB contract ID")]
274280
UnknownContractId,
275281

@@ -457,6 +463,7 @@ impl IntoResponse for APIError {
457463
| APIError::AlreadyUnlocked
458464
| APIError::AuthenticationDisabled
459465
| APIError::BatchTransferNotFound
466+
| APIError::CannotCloseChannel(_)
460467
| APIError::CannotEstimateFees
461468
| APIError::CannotFailBatchTransfer
462469
| APIError::ChangingState
@@ -483,6 +490,7 @@ impl IntoResponse for APIError {
483490
| APIError::RecipientIDAlreadyUsed
484491
| APIError::SwapNotFound(_)
485492
| APIError::TemporaryChannelIdAlreadyUsed
493+
| APIError::UnknownChannelId
486494
| APIError::UnknownContractId
487495
| APIError::UnknownLNInvoice
488496
| APIError::UnknownTemporaryChannelId

src/routes.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,24 @@ pub(crate) async fn close_channel(
14381438
Err(_) => return Err(APIError::InvalidPubkey),
14391439
};
14401440

1441+
if let Some(chan_details) = unlocked_state
1442+
.channel_manager
1443+
.list_channels()
1444+
.iter()
1445+
.find(|c| c.channel_id == requested_cid)
1446+
{
1447+
match chan_details.channel_shutdown_state {
1448+
Some(ChannelShutdownState::NotShuttingDown) => {}
1449+
_ => {
1450+
return Err(APIError::CannotCloseChannel(s!(
1451+
"Channel is already being closed"
1452+
)))
1453+
}
1454+
}
1455+
} else {
1456+
return Err(APIError::UnknownChannelId);
1457+
}
1458+
14411459
if payload.force {
14421460
match unlocked_state
14431461
.channel_manager
@@ -1447,15 +1465,25 @@ pub(crate) async fn close_channel(
14471465
"Manually force-closed".to_string(),
14481466
) {
14491467
Ok(()) => tracing::info!("EVENT: initiating channel force-close"),
1450-
Err(e) => return Err(APIError::FailedClosingChannel(format!("{e:?}"))),
1468+
Err(e) => match e {
1469+
LDKAPIError::APIMisuseError { err } => {
1470+
return Err(APIError::FailedClosingChannel(err))
1471+
}
1472+
_ => return Err(APIError::CannotCloseChannel(format!("{e:?}"))),
1473+
},
14511474
}
14521475
} else {
14531476
match unlocked_state
14541477
.channel_manager
14551478
.close_channel(&requested_cid, &peer_pubkey)
14561479
{
14571480
Ok(()) => tracing::info!("EVENT: initiating channel close"),
1458-
Err(e) => return Err(APIError::FailedClosingChannel(format!("{e:?}"))),
1481+
Err(e) => match e {
1482+
LDKAPIError::APIMisuseError { err } => {
1483+
return Err(APIError::FailedClosingChannel(err))
1484+
}
1485+
_ => return Err(APIError::CannotCloseChannel(format!("{e:?}"))),
1486+
},
14591487
}
14601488
}
14611489

0 commit comments

Comments
 (0)