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

Commit b080f59

Browse files
get_info unrecognized_or_stale_token error
1 parent 64cf801 commit b080f59

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/jit_channel/channel_manager.rs

+34
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use crate::jit_channel::msgs::{
4242
LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE,
4343
LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE,
4444
LSPS2_GET_INFO_REQUEST_INVALID_VERSION_ERROR_CODE,
45+
LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
4546
};
4647

4748
const SUPPORTED_SPEC_VERSIONS: [u16; 1] = [1];
@@ -531,6 +532,39 @@ where
531532
}
532533
}
533534

535+
pub fn invalid_token_provided(
536+
&self, counterparty_node_id: PublicKey, request_id: RequestId,
537+
) -> Result<(), APIError> {
538+
let outer_state_lock = self.per_peer_state.read().unwrap();
539+
540+
match outer_state_lock.get(&counterparty_node_id) {
541+
Some(inner_state_lock) => {
542+
let mut peer_state = inner_state_lock.lock().unwrap();
543+
544+
match peer_state.pending_requests.remove(&request_id) {
545+
Some(LSPS2Request::GetInfo(_)) => {
546+
let response = LSPS2Response::GetInfoError(ResponseError {
547+
code: LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE,
548+
message: "an unrecognized or stale token was provided".to_string(),
549+
data: None,
550+
});
551+
self.enqueue_response(counterparty_node_id, request_id, response);
552+
Ok(())
553+
}
554+
_ => Err(APIError::APIMisuseError {
555+
err: format!(
556+
"No pending get_info request for request_id: {:?}",
557+
request_id
558+
),
559+
}),
560+
}
561+
}
562+
None => Err(APIError::APIMisuseError {
563+
err: format!("No state for the counterparty exists: {:?}", counterparty_node_id),
564+
}),
565+
}
566+
}
567+
534568
pub fn opening_fee_params_generated(
535569
&self, counterparty_node_id: PublicKey, request_id: RequestId,
536570
opening_fee_params_menu: Vec<RawOpeningFeeParams>,

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

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub(crate) const LSPS2_GET_INFO_METHOD_NAME: &str = "lsps2.get_info";
1414
pub(crate) const LSPS2_BUY_METHOD_NAME: &str = "lsps2.buy";
1515

1616
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;
1718

1819
pub(crate) const LSPS2_BUY_REQUEST_INVALID_VERSION_ERROR_CODE: i32 = 1;
1920
pub(crate) const LSPS2_BUY_REQUEST_INVALID_OPENING_FEE_PARAMS_ERROR_CODE: i32 = 2;

src/transport/message_handler.rs

+18
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ where {
265265
}
266266
}
267267

268+
/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.
269+
///
270+
/// Should be called in response to receiving a [`LSPS2Event::GetInfo`] event.
271+
///
272+
/// [`LSPS2Event::GetInfo`]: crate::jit_channel::LSPS2Event::GetInfo
273+
pub fn invalid_token_provided(
274+
&self, counterparty_node_id: PublicKey, request_id: RequestId,
275+
) -> Result<(), APIError> {
276+
if let Some(lsps2_message_handler) = &self.lsps2_message_handler {
277+
lsps2_message_handler.invalid_token_provided(counterparty_node_id, request_id)
278+
} else {
279+
Err(APIError::APIMisuseError {
280+
err: "JIT Channels were not configured when LSPManager was instantiated"
281+
.to_string(),
282+
})
283+
}
284+
}
285+
268286
/// Used by LSP to provide fee parameters to a client requesting a JIT Channel.
269287
///
270288
/// Should be called in response to receiving a [`LSPS2Event::GetInfo`] event.

0 commit comments

Comments
 (0)