Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove versioning and cleanup warnings #86

Merged
merged 7 commits into from
Jan 5, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- name: Check formatting
if: matrix.check-fmt
run: rustup component add rustfmt && cargo fmt --all -- --check
- name: Set RUSTFLAGS to deny warnings
if: "matrix.toolchain == 'stable'"
run: echo "RUSTFLAGS=-D warnings" >> "$GITHUB_ENV"
- name: Test on Rust ${{ matrix.toolchain }}
run: |
cargo test
Expand Down
7 changes: 3 additions & 4 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ impl Future for EventFuture {

#[cfg(test)]
mod tests {
use super::*;
use crate::lsps0::event::LSPS0ClientEvent;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};

#[tokio::test]
#[cfg(feature = "std")]
async fn event_queue_works() {
use super::*;
use crate::lsps0::event::LSPS0ClientEvent;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use core::sync::atomic::{AtomicU16, Ordering};
use std::sync::Arc;
use std::time::Duration;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ compile_error!("at least one of the `std` or `no-std` features must be enabled")
extern crate alloc;

mod prelude {
#![allow(unused_imports)]
#[cfg(feature = "hashbrown")]
extern crate hashbrown;

Expand All @@ -41,6 +42,8 @@ mod prelude {
pub mod events;
pub mod lsps0;
#[cfg(lsps1)]
// TODO: disallow warnings once the implementation is finished
#[allow(warnings)]
pub mod lsps1;
pub mod lsps2;
mod manager;
Expand Down
2 changes: 1 addition & 1 deletion src/lsps0/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ mod tests {
use alloc::sync::Arc;

use crate::lsps0::msgs::{LSPSMessage, RequestId};
use crate::tests::utils::TestEntropy;
use crate::tests::utils::{self, TestEntropy};

use super::*;

Expand Down
27 changes: 0 additions & 27 deletions src/lsps0/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::lsps1::msgs::{
};
use crate::lsps2::msgs::{
LSPS2Message, LSPS2Request, LSPS2Response, LSPS2_BUY_METHOD_NAME, LSPS2_GET_INFO_METHOD_NAME,
LSPS2_GET_VERSIONS_METHOD_NAME,
};
use crate::prelude::{HashMap, String, ToString, Vec};

Expand Down Expand Up @@ -294,9 +293,6 @@ impl Serialize for LSPSMessage {
jsonrpc_object.serialize_field(JSONRPC_METHOD_FIELD_KEY, request.method())?;

match request {
LSPS2Request::GetVersions(params) => {
jsonrpc_object.serialize_field(JSONRPC_PARAMS_FIELD_KEY, params)?
}
LSPS2Request::GetInfo(params) => {
jsonrpc_object.serialize_field(JSONRPC_PARAMS_FIELD_KEY, params)?
}
Expand All @@ -309,9 +305,6 @@ impl Serialize for LSPSMessage {
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;

match response {
LSPS2Response::GetVersions(result) => {
jsonrpc_object.serialize_field(JSONRPC_RESULT_FIELD_KEY, result)?
}
LSPS2Response::GetInfo(result) => {
jsonrpc_object.serialize_field(JSONRPC_RESULT_FIELD_KEY, result)?
}
Expand Down Expand Up @@ -423,14 +416,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
LSPS1Request::GetOrder(request),
)))
}
LSPS2_GET_VERSIONS_METHOD_NAME => {
let request = serde_json::from_value(params.unwrap_or(json!({})))
.map_err(de::Error::custom)?;
Ok(LSPSMessage::LSPS2(LSPS2Message::Request(
RequestId(id),
LSPS2Request::GetVersions(request),
)))
}
LSPS2_GET_INFO_METHOD_NAME => {
let request = serde_json::from_value(params.unwrap_or(json!({})))
.map_err(de::Error::custom)?;
Expand Down Expand Up @@ -471,18 +456,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
}
}
LSPS2_GET_VERSIONS_METHOD_NAME => {
if let Some(result) = result {
let response =
serde_json::from_value(result).map_err(de::Error::custom)?;
Ok(LSPSMessage::LSPS2(LSPS2Message::Response(
RequestId(id),
LSPS2Response::GetVersions(response),
)))
} else {
Err(de::Error::custom("Received invalid lsps2.get_versions response."))
}
}
#[cfg(lsps1)]
LSPS1_CREATE_ORDER_METHOD_NAME => {
if let Some(error) = error {
Expand Down
2 changes: 1 addition & 1 deletion src/lsps0/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl ProtocolMessageHandler for LSPS0ServiceHandler {
mod tests {

use crate::lsps0::msgs::{LSPSMessage, ListProtocolsRequest};
use crate::utils;
use crate::tests::utils;
use alloc::string::ToString;
use alloc::sync::Arc;

Expand Down
66 changes: 23 additions & 43 deletions src/lsps1/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is Copyright its original authors, visible in version contror
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
Expand Down Expand Up @@ -35,8 +35,6 @@ use bitcoin::secp256k1::PublicKey;

use core::ops::Deref;

const SUPPORTED_SPEC_VERSIONS: [u16; 1] = [1];

/// Client-side configuration options for LSPS1 channel requests.
#[derive(Clone, Debug)]
pub struct LSPS1ClientConfig {
Expand All @@ -55,43 +53,30 @@ impl From<ChannelStateError> for LightningError {
#[derive(PartialEq, Debug)]
enum InboundRequestState {
InfoRequested,
OptionsSupport { version: u16, options_supported: OptionsSupported },
OrderRequested { version: u16, order: OrderParams },
OptionsSupport { options_supported: OptionsSupported },
OrderRequested { order: OrderParams },
PendingPayment { order_id: OrderId },
AwaitingConfirmation { id: u128, order_id: OrderId },
}

impl InboundRequestState {
fn info_received(
&self, versions: Vec<u16>, options: OptionsSupported,
) -> Result<Self, ChannelStateError> {
let max_shared_version = versions
.iter()
.filter(|version| SUPPORTED_SPEC_VERSIONS.contains(version))
.max()
.cloned()
.ok_or(ChannelStateError(format!(
"LSP does not support any of our specification versions. ours = {:?}. theirs = {:?}",
SUPPORTED_SPEC_VERSIONS, versions
)))?;

fn info_received(&self, options: OptionsSupported) -> Result<Self, ChannelStateError> {
match self {
InboundRequestState::InfoRequested => Ok(InboundRequestState::OptionsSupport {
version: max_shared_version,
options_supported: options,
}),
InboundRequestState::InfoRequested => {
Ok(InboundRequestState::OptionsSupport { options_supported: options })
}
state => Err(ChannelStateError(format!(
"Received unexpected get_versions response. Channel was in state: {:?}",
"Received unexpected get_info response. Channel was in state: {:?}",
state
))),
}
}

fn order_requested(&self, order: OrderParams) -> Result<Self, ChannelStateError> {
match self {
InboundRequestState::OptionsSupport { version, options_supported } => {
InboundRequestState::OptionsSupport { options_supported } => {
if is_valid(&order, options_supported) {
Ok(InboundRequestState::OrderRequested { version: *version, order })
Ok(InboundRequestState::OrderRequested { order })
} else {
return Err(ChannelStateError(format!(
"The order created does not match options supported by LSP. Options Supported by LSP are {:?}. The order created was {:?}",
Expand All @@ -110,7 +95,7 @@ impl InboundRequestState {
&self, response_order: &OrderParams, order_id: OrderId,
) -> Result<Self, ChannelStateError> {
match self {
InboundRequestState::OrderRequested { version, order } => {
InboundRequestState::OrderRequested { order } => {
if response_order == order {
Ok(InboundRequestState::PendingPayment { order_id })
} else {
Expand Down Expand Up @@ -153,25 +138,23 @@ impl InboundCRChannel {
Self { id, state: InboundRequestState::InfoRequested }
}

fn info_received(
&mut self, versions: Vec<u16>, options: OptionsSupported,
) -> Result<u16, LightningError> {
self.state = self.state.info_received(versions, options)?;
fn info_received(&mut self, options: OptionsSupported) -> Result<(), LightningError> {
self.state = self.state.info_received(options)?;

match self.state {
InboundRequestState::OptionsSupport { version, .. } => Ok(version),
InboundRequestState::OptionsSupport { .. } => Ok(()),
_ => Err(LightningError {
action: ErrorAction::IgnoreAndLog(Level::Error),
err: "impossible state transition".to_string(),
}),
}
}

fn order_requested(&mut self, order: OrderParams) -> Result<u16, LightningError> {
fn order_requested(&mut self, order: OrderParams) -> Result<(), LightningError> {
self.state = self.state.order_requested(order)?;

match self.state {
InboundRequestState::OrderRequested { version, .. } => Ok(version),
InboundRequestState::OrderRequested { .. } => Ok(()),
_ => {
return Err(LightningError {
action: ErrorAction::IgnoreAndLog(Level::Error),
Expand Down Expand Up @@ -301,10 +284,8 @@ where
action: ErrorAction::IgnoreAndLog(Level::Info),
})?;

let version = match inbound_channel
.info_received(result.supported_versions, result.options.clone())
{
Ok(version) => version,
match inbound_channel.info_received(result.options.clone()) {
Ok(()) => (),
Err(e) => {
peer_state_lock.remove_inbound_channel(channel_id);
return Err(e);
Expand All @@ -315,7 +296,6 @@ where
id: channel_id,
request_id,
counterparty_node_id: *counterparty_node_id,
version,
website: result.website,
options_supported: result.options,
}))
Expand Down Expand Up @@ -349,8 +329,8 @@ where
err: format!("Channel with id {} not found", channel_id),
})?;

let version = match inbound_channel.order_requested(order.clone()) {
Ok(version) => version,
match inbound_channel.order_requested(order.clone()) {
Ok(()) => (),
Err(e) => {
peer_state_lock.remove_inbound_channel(channel_id);
return Err(APIError::APIMisuseError { err: e.err });
Expand All @@ -364,7 +344,7 @@ where
counterparty_node_id,
LSPS1Message::Request(
request_id,
LSPS1Request::CreateOrder(CreateOrderRequest { order, version }),
LSPS1Request::CreateOrder(CreateOrderRequest { order }),
)
.into(),
);
Expand Down Expand Up @@ -540,7 +520,7 @@ where
let channel_id =
peer_state_lock.request_to_cid.remove(&request_id).ok_or(LightningError {
err: format!(
"Received get_versions response for an unknown request: {:?}",
"Received get_order response for an unknown request: {:?}",
request_id
),
action: ErrorAction::IgnoreAndLog(Level::Info),
Expand All @@ -551,7 +531,7 @@ where
.get_mut(&channel_id)
.ok_or(LightningError {
err: format!(
"Received get_versions response for an unknown channel: {:?}",
"Received get_order response for an unknown channel: {:?}",
channel_id
),
action: ErrorAction::IgnoreAndLog(Level::Info),
Expand Down
2 changes: 0 additions & 2 deletions src/lsps1/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ pub enum LSPS1ClientEvent {
/// TODO
counterparty_node_id: PublicKey,
/// TODO
version: u16,
/// TODO
website: String,
/// TODO
options_supported: OptionsSupported,
Expand Down
7 changes: 0 additions & 7 deletions src/lsps1/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub(crate) const LSPS1_GET_ORDER_METHOD_NAME: &str = "lsps1.get_order";
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_INVALID_PARAMS_ERROR_CODE: i32 = -32602;
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE: i32 = 1000;
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_CLIENT_REJECTED_ERROR_CODE: i32 = 1001;
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_INVALID_VERSION_ERROR_CODE: i32 = 1;
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_INVALID_TOKEN_ERROR_CODE: i32 = 2;

/// The identifier of an order.
Expand Down Expand Up @@ -62,8 +61,6 @@ pub struct OptionsSupported {
/// A response to an [`GetInfoRequest`].
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct GetInfoResponse {
/// A list of all supported API versions by the LSP.
pub supported_versions: Vec<u16>,
/// The website of the LSP.
pub website: String,
/// All options supported by the LSP.
Expand All @@ -76,17 +73,13 @@ pub struct GetInfoResponse {
/// for more information.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct CreateOrderRequest {
/// TODO: this is superfluous and should likely be removed.
pub version: u16,
/// The order made.
pub order: OrderParams,
}

/// An object representing an LSPS1 channel order.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct OrderParams {
/// The API version that the client wants to work with.
pub api_version: u16,
/// Indicates how many satoshi the LSP will provide on their side.
pub lsp_balance_sat: u64,
/// Indicates how many satoshi the client will provide on their side.
Expand Down
Loading
Loading