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

Commit c80eb75

Browse files
authored
Merge pull request #140 from tnull/2024-07-update-lsps1
LSPS1: Update message format and get client working
2 parents 5a6c4fb + 4a9af52 commit c80eb75

File tree

14 files changed

+557
-624
lines changed

14 files changed

+557
-624
lines changed

.github/workflows/build.yml

-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ jobs:
3737
run: |
3838
cargo doc --release
3939
cargo doc --no-default-features --features no-std
40-
RUSTFLAGS="--cfg lsps1" RUSTDOCFLAGS="--cfg lsps1" cargo doc --release
41-
RUSTFLAGS="--cfg lsps1" RUSTDOCFLAGS="--cfg lsps1" cargo doc --no-default-features --features no-std
4240
- name: Build on Rust ${{ matrix.toolchain }}
4341
run: cargo build --verbose --color always
4442
- name: Check formatting
@@ -50,8 +48,6 @@ jobs:
5048
- name: Test on Rust ${{ matrix.toolchain }}
5149
run: |
5250
cargo test
53-
RUSTFLAGS="--cfg lsps1" cargo test
5451
- name: Test on Rust ${{ matrix.toolchain }} with no-std support
5552
run: |
5653
cargo test --no-default-features --features no-std
57-
RUSTFLAGS="--cfg lsps1" cargo test --no-default-features --features no-std

Cargo.toml

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ std = ["lightning/std", "bitcoin/std", "lightning-invoice/std"]
1919
no-std = ["hashbrown", "lightning/no-std"]
2020

2121
[dependencies]
22-
lightning = { version = "0.0.125", default-features = false }
22+
lightning = { version = "0.0.125", default-features = false, features = ["max_level_trace"] }
2323
lightning-types = { version = "0.1", default-features = false }
2424
lightning-invoice = { version = "0.32.0", default-features = false, features = ["serde"] }
25+
#lightning = { path = "../rust-lightning/lightning", default-features = false, features = ["max_level_trace"] }
26+
#lightning-types = { path = "../rust-lightning/lightning-types", default-features = false }
27+
#lightning-invoice = { path = "../rust-lightning/lightning-invoice", default-features = false, features = ["serde"] }
28+
2529
bitcoin = { version = "0.32.2", default-features = false, features = ["serde"] }
2630
hashbrown = { version = "0.8", optional = true }
2731

@@ -33,12 +37,16 @@ serde_json = "1.0"
3337
lightning = { version = "0.0.125", default-features = false, features = ["_test_utils"] }
3438
lightning-persister = { version = "0.0.125", default-features = false }
3539
lightning-background-processor = { version = "0.0.125", default-features = false, features = ["std"] }
40+
# lightning = { path = "../rust-lightning/lightning", default-features = false, features = ["max_level_trace", "_test_utils"] }
41+
# lightning-persister = { path = "../rust-lightning/lightning-persister", default-features = false }
42+
# lightning-background-processor = { path = "../rust-lightning/lightning-background-processor", default-features = false, features = ["std"] }
43+
3644
proptest = "1.0.0"
3745
tokio = { version = "1.35", default-features = false, features = [ "rt-multi-thread", "time", "sync", "macros" ] }
3846

3947
[lints.rust.unexpected_cfgs]
4048
level = "forbid"
4149
# When adding a new cfg attribute, ensure that it is added to this list.
4250
check-cfg = [
43-
"cfg(lsps1)",
51+
"cfg(lsps1_service)",
4452
]

src/events.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
//! [`LiquidityManager::get_and_clear_pending_events`]: crate::LiquidityManager::get_and_clear_pending_events
1717
1818
use crate::lsps0;
19-
#[cfg(lsps1)]
2019
use crate::lsps1;
2120
use crate::lsps2;
2221
use crate::prelude::{Vec, VecDeque};
@@ -98,10 +97,9 @@ pub enum Event {
9897
/// An LSPS0 client event.
9998
LSPS0Client(lsps0::event::LSPS0ClientEvent),
10099
/// An LSPS1 (Channel Request) client event.
101-
#[cfg(lsps1)]
102100
LSPS1Client(lsps1::event::LSPS1ClientEvent),
103101
/// An LSPS1 (Channel Request) server event.
104-
#[cfg(lsps1)]
102+
#[cfg(lsps1_service)]
105103
LSPS1Service(lsps1::event::LSPS1ServiceEvent),
106104
/// An LSPS2 (JIT Channel) client event.
107105
LSPS2Client(lsps2::event::LSPS2ClientEvent),

src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ mod prelude {
4141

4242
pub mod events;
4343
pub mod lsps0;
44-
#[cfg(lsps1)]
45-
// TODO: disallow warnings once the implementation is finished
46-
#[allow(warnings)]
4744
pub mod lsps1;
4845
pub mod lsps2;
4946
mod manager;

src/lsps0/msgs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ impl TryFrom<LSPSMessage> for LSPS0Message {
7676
match message {
7777
LSPSMessage::Invalid(_) => Err(()),
7878
LSPSMessage::LSPS0(message) => Ok(message),
79-
#[cfg(lsps1)]
8079
LSPSMessage::LSPS1(_) => Err(()),
8180
LSPSMessage::LSPS2(_) => Err(()),
8281
}

src/lsps0/ser.rs

+57-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::lsps0::msgs::{
77
LSPS0_LISTPROTOCOLS_METHOD_NAME,
88
};
99

10-
#[cfg(lsps1)]
1110
use crate::lsps1::msgs::{
1211
LSPS1Message, LSPS1Request, LSPS1Response, LSPS1_CREATE_ORDER_METHOD_NAME,
1312
LSPS1_GET_INFO_METHOD_NAME, LSPS1_GET_ORDER_METHOD_NAME,
@@ -47,11 +46,8 @@ pub(crate) const _LSPS0_CLIENT_REJECTED_ERROR_CODE: i32 = 001;
4746
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4847
pub(crate) enum LSPSMethod {
4948
LSPS0ListProtocols,
50-
#[cfg(lsps1)]
5149
LSPS1GetInfo,
52-
#[cfg(lsps1)]
5350
LSPS1GetOrder,
54-
#[cfg(lsps1)]
5551
LSPS1CreateOrder,
5652
LSPS2GetInfo,
5753
LSPS2Buy,
@@ -62,11 +58,8 @@ impl FromStr for LSPSMethod {
6258
fn from_str(s: &str) -> Result<Self, Self::Err> {
6359
match s {
6460
LSPS0_LISTPROTOCOLS_METHOD_NAME => Ok(Self::LSPS0ListProtocols),
65-
#[cfg(lsps1)]
6661
LSPS1_GET_INFO_METHOD_NAME => Ok(Self::LSPS1GetInfo),
67-
#[cfg(lsps1)]
6862
LSPS1_CREATE_ORDER_METHOD_NAME => Ok(Self::LSPS1CreateOrder),
69-
#[cfg(lsps1)]
7063
LSPS1_GET_ORDER_METHOD_NAME => Ok(Self::LSPS1GetOrder),
7164
LSPS2_GET_INFO_METHOD_NAME => Ok(Self::LSPS2GetInfo),
7265
LSPS2_BUY_METHOD_NAME => Ok(Self::LSPS2Buy),
@@ -79,11 +72,8 @@ impl Display for LSPSMethod {
7972
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8073
let s = match self {
8174
Self::LSPS0ListProtocols => LSPS0_LISTPROTOCOLS_METHOD_NAME,
82-
#[cfg(lsps1)]
8375
Self::LSPS1GetInfo => LSPS1_GET_INFO_METHOD_NAME,
84-
#[cfg(lsps1)]
8576
Self::LSPS1CreateOrder => LSPS1_CREATE_ORDER_METHOD_NAME,
86-
#[cfg(lsps1)]
8777
Self::LSPS1GetOrder => LSPS1_GET_ORDER_METHOD_NAME,
8878
Self::LSPS2GetInfo => LSPS2_GET_INFO_METHOD_NAME,
8979
Self::LSPS2Buy => LSPS2_BUY_METHOD_NAME,
@@ -100,7 +90,6 @@ impl From<&LSPS0Request> for LSPSMethod {
10090
}
10191
}
10292

103-
#[cfg(lsps1)]
10493
impl From<&LSPS1Request> for LSPSMethod {
10594
fn from(value: &LSPS1Request) -> Self {
10695
match value {
@@ -216,7 +205,6 @@ pub enum LSPSMessage {
216205
/// An LSPS0 message.
217206
LSPS0(LSPS0Message),
218207
/// An LSPS1 message.
219-
#[cfg(lsps1)]
220208
LSPS1(LSPS1Message),
221209
/// An LSPS2 message.
222210
LSPS2(LSPS2Message),
@@ -241,7 +229,6 @@ impl LSPSMessage {
241229
LSPSMessage::LSPS0(LSPS0Message::Request(request_id, request)) => {
242230
Some((RequestId(request_id.0.clone()), request.into()))
243231
},
244-
#[cfg(lsps1)]
245232
LSPSMessage::LSPS1(LSPS1Message::Request(request_id, request)) => {
246233
Some((RequestId(request_id.0.clone()), request.into()))
247234
},
@@ -287,7 +274,6 @@ impl Serialize for LSPSMessage {
287274
},
288275
}
289276
},
290-
#[cfg(lsps1)]
291277
LSPSMessage::LSPS1(LSPS1Message::Request(request_id, request)) => {
292278
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
293279
jsonrpc_object
@@ -305,7 +291,6 @@ impl Serialize for LSPSMessage {
305291
},
306292
}
307293
},
308-
#[cfg(lsps1)]
309294
LSPSMessage::LSPS1(LSPS1Message::Response(request_id, response)) => {
310295
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
311296

@@ -442,7 +427,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
442427
id,
443428
LSPS0Request::ListProtocols(ListProtocolsRequest {}),
444429
))),
445-
#[cfg(lsps1)]
446430
LSPSMethod::LSPS1GetInfo => {
447431
let request = serde_json::from_value(params.unwrap_or(json!({})))
448432
.map_err(de::Error::custom)?;
@@ -451,7 +435,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
451435
LSPS1Request::GetInfo(request),
452436
)))
453437
},
454-
#[cfg(lsps1)]
455438
LSPSMethod::LSPS1CreateOrder => {
456439
let request = serde_json::from_value(params.unwrap_or(json!({})))
457440
.map_err(de::Error::custom)?;
@@ -460,7 +443,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
460443
LSPS1Request::CreateOrder(request),
461444
)))
462445
},
463-
#[cfg(lsps1)]
464446
LSPSMethod::LSPS1GetOrder => {
465447
let request = serde_json::from_value(params.unwrap_or(json!({})))
466448
.map_err(de::Error::custom)?;
@@ -502,7 +484,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
502484
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
503485
}
504486
},
505-
#[cfg(lsps1)]
506487
LSPSMethod::LSPS1GetInfo => {
507488
if let Some(error) = error {
508489
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
@@ -520,7 +501,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
520501
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
521502
}
522503
},
523-
#[cfg(lsps1)]
524504
LSPSMethod::LSPS1CreateOrder => {
525505
if let Some(error) = error {
526506
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
@@ -538,7 +518,6 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
538518
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
539519
}
540520
},
541-
#[cfg(lsps1)]
542521
LSPSMethod::LSPS1GetOrder => {
543522
if let Some(error) = error {
544523
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
@@ -654,7 +633,63 @@ pub(crate) mod string_amount_option {
654633
}
655634
}
656635

657-
#[cfg(lsps1)]
636+
pub(crate) mod unchecked_address {
637+
use crate::prelude::{String, ToString};
638+
use bitcoin::Address;
639+
use core::str::FromStr;
640+
use serde::de::Unexpected;
641+
use serde::{Deserialize, Deserializer, Serializer};
642+
643+
pub(crate) fn serialize<S>(x: &Address, s: S) -> Result<S::Ok, S::Error>
644+
where
645+
S: Serializer,
646+
{
647+
s.serialize_str(&x.to_string())
648+
}
649+
650+
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Address, D::Error>
651+
where
652+
D: Deserializer<'de>,
653+
{
654+
let buf = String::deserialize(deserializer)?;
655+
656+
let parsed_addr = Address::from_str(&buf).map_err(|_| {
657+
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
658+
})?;
659+
Ok(parsed_addr.assume_checked())
660+
}
661+
}
662+
663+
pub(crate) mod unchecked_address_option {
664+
use crate::prelude::{String, ToString};
665+
use bitcoin::Address;
666+
use core::str::FromStr;
667+
use serde::de::Unexpected;
668+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
669+
670+
pub(crate) fn serialize<S>(x: &Option<Address>, s: S) -> Result<S::Ok, S::Error>
671+
where
672+
S: Serializer,
673+
{
674+
let v = x.as_ref().map(|v| v.to_string());
675+
Option::<String>::serialize(&v, s)
676+
}
677+
678+
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Option<bitcoin::Address>, D::Error>
679+
where
680+
D: Deserializer<'de>,
681+
{
682+
if let Some(buf) = Option::<String>::deserialize(deserializer)? {
683+
let val = Address::from_str(&buf).map_err(|_| {
684+
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
685+
})?;
686+
Ok(Some(val.assume_checked()))
687+
} else {
688+
Ok(None)
689+
}
690+
}
691+
}
692+
658693
pub(crate) mod u32_fee_rate {
659694
use bitcoin::FeeRate;
660695
use serde::{Deserialize, Deserializer, Serializer};

0 commit comments

Comments
 (0)