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

Add missing error handling #26

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/jit_channel/channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ use crate::jit_channel::msgs::{
LSPS2_BUY_REQUEST_INVALID_VERSION_ERROR_CODE,
LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE,
LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE,
LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE,
LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
};

const SUPPORTED_SPEC_VERSIONS: [u16; 1] = [1];
Expand Down Expand Up @@ -516,6 +518,39 @@ where
}
}

pub fn invalid_token_provided(
&self, counterparty_node_id: PublicKey, request_id: RequestId,
) -> Result<(), APIError> {
let outer_state_lock = self.per_peer_state.read().unwrap();

match outer_state_lock.get(&counterparty_node_id) {
Some(inner_state_lock) => {
let mut peer_state = inner_state_lock.lock().unwrap();

match peer_state.pending_requests.remove(&request_id) {
Some(LSPS2Request::GetInfo(_)) => {
let response = LSPS2Response::GetInfoError(ResponseError {
code: LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
message: "an unrecognized or stale token was provided".to_string(),
data: None,
});
self.enqueue_response(counterparty_node_id, request_id, response);
Ok(())
}
_ => Err(APIError::APIMisuseError {
err: format!(
"No pending get_info request for request_id: {:?}",
request_id
),
}),
}
}
None => Err(APIError::APIMisuseError {
err: format!("No state for the counterparty exists: {:?}", counterparty_node_id),
}),
}
}

pub fn opening_fee_params_generated(
&self, counterparty_node_id: PublicKey, request_id: RequestId,
opening_fee_params_menu: Vec<RawOpeningFeeParams>,
Expand Down Expand Up @@ -875,6 +910,22 @@ where
fn handle_get_info_request(
&self, request_id: RequestId, counterparty_node_id: &PublicKey, params: GetInfoRequest,
) -> Result<(), LightningError> {
if !SUPPORTED_SPEC_VERSIONS.contains(&params.version) {
self.enqueue_response(
*counterparty_node_id,
request_id,
LSPS2Response::GetInfoError(ResponseError {
code: LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE,
message: format!("version {} is not supported", params.version),
data: Some(format!("Supported versions are {:?}", SUPPORTED_SPEC_VERSIONS)),
}),
);
return Err(LightningError {
err: format!("client requested unsupported version {}", params.version),
action: ErrorAction::IgnoreAndLog(Level::Info),
});
}

let mut outer_state_lock = self.per_peer_state.write().unwrap();
let inner_state_lock: &mut Mutex<PeerState> = outer_state_lock
.entry(*counterparty_node_id)
Expand Down
4 changes: 4 additions & 0 deletions src/jit_channel/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ pub enum LSPS2Event {
/// You must calculate the parameters for this client and pass them to
/// [`LiquidityManager::opening_fee_params_generated`].
///
/// If an unrecognized or stale token is provided you can use
/// `[LiquidityManager::invalid_token_provided`] to error the request.
///
/// [`LiquidityManager::opening_fee_params_generated`]: crate::LiquidityManager::opening_fee_params_generated
/// [`LiquidityManager::invalid_token_provided`]: crate::LiquidityManager::invalid_token_provided
GetInfo {
/// An identifier that must be passed to [`LiquidityManager::opening_fee_params_generated`].
///
Expand Down
3 changes: 3 additions & 0 deletions src/jit_channel/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub(crate) const LSPS2_GET_VERSIONS_METHOD_NAME: &str = "lsps2.get_versions";
pub(crate) const LSPS2_GET_INFO_METHOD_NAME: &str = "lsps2.get_info";
pub(crate) const LSPS2_BUY_METHOD_NAME: &str = "lsps2.buy";

pub(crate) const LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE: i32 = 1;
pub(crate) const LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE: i32 = 2;

pub(crate) const LSPS2_BUY_REQUEST_INVALID_VERSION_ERROR_CODE: i32 = 1;
pub(crate) const LSPS2_BUY_REQUEST_INVALID_OPENING_FEE_PARAMS_ERROR_CODE: i32 = 2;
pub(crate) const LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE: i32 = 3;
Expand Down
18 changes: 18 additions & 0 deletions src/transport/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ where {
}
}

/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.
///
/// Should be called in response to receiving a [`LSPS2Event::GetInfo`] event.
///
/// [`LSPS2Event::GetInfo`]: crate::jit_channel::LSPS2Event::GetInfo
pub fn invalid_token_provided(
&self, counterparty_node_id: PublicKey, request_id: RequestId,
) -> Result<(), APIError> {
if let Some(lsps2_message_handler) = &self.lsps2_message_handler {
lsps2_message_handler.invalid_token_provided(counterparty_node_id, request_id)
} else {
Err(APIError::APIMisuseError {
err: "JIT Channels were not configured when LSPManager was instantiated"
.to_string(),
})
}
}

/// Used by LSP to provide fee parameters to a client requesting a JIT Channel.
///
/// Should be called in response to receiving a [`LSPS2Event::GetInfo`] event.
Expand Down