Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit fb65ead

Browse files
authored
Merge pull request #26 from johncantrell97/error-handling
Add missing error handling
2 parents 4bfaa9a + 5210d59 commit fb65ead

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

src/jit_channel/channel_manager.rs

+51
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use crate::jit_channel::msgs::{
4141
LSPS2_BUY_REQUEST_INVALID_VERSION_ERROR_CODE,
4242
LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE,
4343
LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE,
44+
LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE,
45+
LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
4446
};
4547

4648
const SUPPORTED_SPEC_VERSIONS: [u16; 1] = [1];
@@ -516,6 +518,39 @@ where
516518
}
517519
}
518520

521+
pub fn invalid_token_provided(
522+
&self, counterparty_node_id: PublicKey, request_id: RequestId,
523+
) -> Result<(), APIError> {
524+
let outer_state_lock = self.per_peer_state.read().unwrap();
525+
526+
match outer_state_lock.get(&counterparty_node_id) {
527+
Some(inner_state_lock) => {
528+
let mut peer_state = inner_state_lock.lock().unwrap();
529+
530+
match peer_state.pending_requests.remove(&request_id) {
531+
Some(LSPS2Request::GetInfo(_)) => {
532+
let response = LSPS2Response::GetInfoError(ResponseError {
533+
code: LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
534+
message: "an unrecognized or stale token was provided".to_string(),
535+
data: None,
536+
});
537+
self.enqueue_response(counterparty_node_id, request_id, response);
538+
Ok(())
539+
}
540+
_ => Err(APIError::APIMisuseError {
541+
err: format!(
542+
"No pending get_info request for request_id: {:?}",
543+
request_id
544+
),
545+
}),
546+
}
547+
}
548+
None => Err(APIError::APIMisuseError {
549+
err: format!("No state for the counterparty exists: {:?}", counterparty_node_id),
550+
}),
551+
}
552+
}
553+
519554
pub fn opening_fee_params_generated(
520555
&self, counterparty_node_id: PublicKey, request_id: RequestId,
521556
opening_fee_params_menu: Vec<RawOpeningFeeParams>,
@@ -875,6 +910,22 @@ where
875910
fn handle_get_info_request(
876911
&self, request_id: RequestId, counterparty_node_id: &PublicKey, params: GetInfoRequest,
877912
) -> Result<(), LightningError> {
913+
if !SUPPORTED_SPEC_VERSIONS.contains(&params.version) {
914+
self.enqueue_response(
915+
*counterparty_node_id,
916+
request_id,
917+
LSPS2Response::GetInfoError(ResponseError {
918+
code: LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE,
919+
message: format!("version {} is not supported", params.version),
920+
data: Some(format!("Supported versions are {:?}", SUPPORTED_SPEC_VERSIONS)),
921+
}),
922+
);
923+
return Err(LightningError {
924+
err: format!("client requested unsupported version {}", params.version),
925+
action: ErrorAction::IgnoreAndLog(Level::Info),
926+
});
927+
}
928+
878929
let mut outer_state_lock = self.per_peer_state.write().unwrap();
879930
let inner_state_lock: &mut Mutex<PeerState> = outer_state_lock
880931
.entry(*counterparty_node_id)

src/jit_channel/event.rs

+4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ pub enum LSPS2Event {
2020
/// You must calculate the parameters for this client and pass them to
2121
/// [`LiquidityManager::opening_fee_params_generated`].
2222
///
23+
/// If an unrecognized or stale token is provided you can use
24+
/// `[LiquidityManager::invalid_token_provided`] to error the request.
25+
///
2326
/// [`LiquidityManager::opening_fee_params_generated`]: crate::LiquidityManager::opening_fee_params_generated
27+
/// [`LiquidityManager::invalid_token_provided`]: crate::LiquidityManager::invalid_token_provided
2428
GetInfo {
2529
/// An identifier that must be passed to [`LiquidityManager::opening_fee_params_generated`].
2630
///

src/jit_channel/msgs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub(crate) const LSPS2_GET_VERSIONS_METHOD_NAME: &str = "lsps2.get_versions";
1313
pub(crate) const LSPS2_GET_INFO_METHOD_NAME: &str = "lsps2.get_info";
1414
pub(crate) const LSPS2_BUY_METHOD_NAME: &str = "lsps2.buy";
1515

16+
pub(crate) const LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE: i32 = 1;
17+
pub(crate) const LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE: i32 = 2;
18+
1619
pub(crate) const LSPS2_BUY_REQUEST_INVALID_VERSION_ERROR_CODE: i32 = 1;
1720
pub(crate) const LSPS2_BUY_REQUEST_INVALID_OPENING_FEE_PARAMS_ERROR_CODE: i32 = 2;
1821
pub(crate) const LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE: i32 = 3;

src/transport/message_handler.rs

+18
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,24 @@ where {
361361
}
362362
}
363363

364+
/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.
365+
///
366+
/// Should be called in response to receiving a [`LSPS2Event::GetInfo`] event.
367+
///
368+
/// [`LSPS2Event::GetInfo`]: crate::jit_channel::LSPS2Event::GetInfo
369+
pub fn invalid_token_provided(
370+
&self, counterparty_node_id: PublicKey, request_id: RequestId,
371+
) -> Result<(), APIError> {
372+
if let Some(lsps2_message_handler) = &self.lsps2_message_handler {
373+
lsps2_message_handler.invalid_token_provided(counterparty_node_id, request_id)
374+
} else {
375+
Err(APIError::APIMisuseError {
376+
err: "JIT Channels were not configured when LSPManager was instantiated"
377+
.to_string(),
378+
})
379+
}
380+
}
381+
364382
/// Used by LSP to provide fee parameters to a client requesting a JIT Channel.
365383
///
366384
/// Should be called in response to receiving a [`LSPS2Event::GetInfo`] event.

0 commit comments

Comments
 (0)