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

Commit 83381aa

Browse files
fix docs, extract request_id util, and remove logger
1 parent 487b391 commit 83381aa

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ jobs:
3131
- name: Pin serde for MSRV
3232
if: matrix.msrv
3333
run: cargo update -p serde --precise "1.0.156" --verbose
34+
- name: Cargo check
35+
run: cargo check --release
3436
- name: Check documentation
35-
run: cargo check --release && cargo doc --release
37+
run: cargo doc --release
3638
- name: Build on Rust ${{ matrix.toolchain }}
3739
run: cargo build --verbose --color always
3840
- name: Check formatting

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description = "Types and primitives to integrate a spec-compliant LSP with an LD
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
11-
lightning = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "498f2331459d8031031ef151a44c90d700aa8c7e", features = ["max_level_trace"] }
11+
lightning = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "498f2331459d8031031ef151a44c90d700aa8c7e", features = ["max_level_trace", "std"] }
1212
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "498f2331459d8031031ef151a44c90d700aa8c7e" }
1313
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "498f2331459d8031031ef151a44c90d700aa8c7e" }
1414

src/events.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
//! Events are returned from various bits in the library which indicate some action must be taken
10+
//! Events are surfaced by the library when some action must be taken
1111
//! by the client.
1212
//!
13-
//! Because we don't have a built-in runtime, it's up to the client to call events at a time in the
14-
//! future.
13+
//! Because we don't have a built-in runtime, it's up to the client to
14+
//! regularly poll [`crate::LSPManager::get_and_clear_pending_events()`]
1515
use bitcoin::secp256k1::PublicKey;
1616

1717
/// An Event which you should probably take some action in response to.

src/transport/message_handler.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::transport::protocol::LSPS0MessageHandler;
44

55
use bitcoin::secp256k1::PublicKey;
66
use lightning::ln::features::{InitFeatures, NodeFeatures};
7+
use lightning::ln::msgs::{ErrorAction, LightningError};
78
use lightning::ln::peer_handler::CustomMessageHandler;
89
use lightning::ln::wire::CustomMessageReader;
9-
use lightning::log_info;
1010
use lightning::sign::EntropySource;
11-
use lightning::util::logger::Logger;
11+
use lightning::util::logger::Level;
1212
use lightning::util::ser::Readable;
1313
use std::collections::HashMap;
1414
use std::convert::TryFrom;
@@ -27,7 +27,7 @@ pub(crate) trait ProtocolMessageHandler {
2727

2828
fn handle_message(
2929
&self, message: Self::ProtocolMessage, counterparty_node_id: &PublicKey,
30-
) -> Result<(), lightning::ln::msgs::LightningError>;
30+
) -> Result<(), LightningError>;
3131
}
3232

3333
/// Configuration for the LSPManager
@@ -38,40 +38,36 @@ pub struct LSPConfig {
3838
}
3939

4040
/// The main interface into LSP functionality
41-
pub struct LSPManager<L: Deref, ES: Deref>
41+
pub struct LSPManager<ES: Deref>
4242
where
43-
L::Target: Logger,
4443
ES::Target: EntropySource,
4544
{
46-
logger: L,
4745
pending_messages: Arc<Mutex<Vec<(PublicKey, LSPSMessage)>>>,
4846
pending_events: Arc<Mutex<Vec<Event>>>,
4947
request_id_to_method_map: Mutex<HashMap<String, String>>,
5048
lsps0_message_handler: LSPS0MessageHandler<ES>,
5149
config: LSPConfig,
5250
}
5351

54-
impl<L: Deref, ES: Deref> LSPManager<L, ES>
52+
impl<ES: Deref> LSPManager<ES>
5553
where
56-
L::Target: Logger,
5754
ES::Target: EntropySource,
5855
{
5956
/// Constructor for the LSPManager
6057
///
6158
/// Sets up all required protocol message handlers based on configuration passed in
62-
pub fn new(logger: L, entropy_source: ES, config: LSPConfig) -> Self {
59+
pub fn new(entropy_source: ES, config: LSPConfig) -> Self {
6360
let pending_messages = Arc::new(Mutex::new(vec![]));
6461
let pending_events = Arc::new(Mutex::new(vec![]));
6562

6663
let lsps0_message_handler = LSPS0MessageHandler::new(
6764
entropy_source,
6865
vec![],
69-
pending_messages.clone(),
70-
pending_events.clone(),
66+
Arc::clone(&pending_messages),
67+
Arc::clone(&pending_events),
7168
);
7269

7370
Self {
74-
logger,
7571
pending_messages,
7672
pending_events,
7773
request_id_to_method_map: Mutex::new(HashMap::new()),
@@ -87,7 +83,7 @@ where
8783
self.lsps0_message_handler.list_protocols(counterparty_node_id)
8884
}
8985

90-
/// Needs to be polled regularly to surface events generated by the various protocols
86+
/// Needs to be polled regularly to surface events generated by the various message handlers
9187
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
9288
self.pending_events.lock().unwrap().drain(..).collect()
9389
}
@@ -97,7 +93,7 @@ where
9793
) -> Result<(), lightning::ln::msgs::LightningError> {
9894
match msg {
9995
LSPSMessage::Invalid => {
100-
log_info!(self.logger, "Received invalid message from {:?}", sender_node_id);
96+
return Err(LightningError { err: format!("{} did not understand a message we previously sent, maybe they don't support a protocol we are trying to use?", sender_node_id), action: ErrorAction::IgnoreAndLog(Level::Error)});
10197
}
10298
LSPSMessage::LSPS0(msg) => {
10399
self.lsps0_message_handler.handle_message(msg, sender_node_id)?;
@@ -112,9 +108,8 @@ where
112108
}
113109
}
114110

115-
impl<L: Deref, ES: Deref> CustomMessageReader for LSPManager<L, ES>
111+
impl<ES: Deref> CustomMessageReader for LSPManager<ES>
116112
where
117-
L::Target: Logger,
118113
ES::Target: EntropySource,
119114
{
120115
type CustomMessage = RawLSPSMessage;
@@ -129,9 +124,8 @@ where
129124
}
130125
}
131126

132-
impl<L: Deref, ES: Deref> CustomMessageHandler for LSPManager<L, ES>
127+
impl<ES: Deref> CustomMessageHandler for LSPManager<ES>
133128
where
134-
L::Target: Logger,
135129
ES::Target: EntropySource,
136130
{
137131
fn handle_custom_message(

src/transport/protocol.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,9 @@ where
3535
Self { entropy_source, protocols, pending_messages, pending_events }
3636
}
3737

38-
fn generate_request_id(&self) -> RequestId {
39-
let bytes = self.entropy_source.get_secure_random_bytes();
40-
RequestId(utils::hex_str(&bytes[0..16]))
41-
}
42-
4338
pub fn list_protocols(&self, counterparty_node_id: PublicKey) {
4439
let msg = LSPS0Message::Request(
45-
self.generate_request_id(),
40+
utils::generate_request_id(&self.entropy_source),
4641
LSPS0Request::ListProtocols(ListProtocolsRequest {}),
4742
);
4843

src/utils.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
use bitcoin::secp256k1::PublicKey;
2-
use std::fmt::Write;
2+
use lightning::sign::EntropySource;
3+
use std::{fmt::Write, ops::Deref};
4+
5+
use crate::transport::msgs::RequestId;
6+
7+
pub(crate) fn generate_request_id<ES: Deref>(entropy_source: &ES) -> RequestId
8+
where
9+
ES::Target: EntropySource,
10+
{
11+
let bytes = entropy_source.get_secure_random_bytes();
12+
RequestId(hex_str(&bytes[0..16]))
13+
}
314

415
#[inline]
516
pub fn hex_str(value: &[u8]) -> String {

0 commit comments

Comments
 (0)