lightning 0.0.116

A Bitcoin Lightning library in Rust. Does most of the hard work, without implying a specific runtime, requiring clients implement basic network logic, chain interactions and disk storage. Still missing tons of error-handling. See GitHub issues for suggested projects if you want to contribute. Don't have to bother telling you not to use this for anything serious, because you'd have to build a client around it to even try.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// 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
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Onion message testing and test utilities live here.

use crate::blinded_path::BlindedPath;
use crate::sign::{NodeSigner, Recipient};
use crate::ln::features::InitFeatures;
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
use crate::util::ser::{Writeable, Writer};
use crate::util::test_utils;

use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{PublicKey, Secp256k1};

use crate::io;
use crate::io_extras::read_to_end;
use crate::sync::{Arc, Mutex};

use crate::prelude::*;

struct MessengerNode {
	keys_manager: Arc<test_utils::TestKeysInterface>,
	messenger: OnionMessenger<
		Arc<test_utils::TestKeysInterface>,
		Arc<test_utils::TestKeysInterface>,
		Arc<test_utils::TestLogger>,
		Arc<TestMessageRouter>,
		Arc<TestOffersMessageHandler>,
		Arc<TestCustomMessageHandler>
	>,
	custom_message_handler: Arc<TestCustomMessageHandler>,
}

impl MessengerNode {
	fn get_node_pk(&self) -> PublicKey {
		self.keys_manager.get_node_id(Recipient::Node).unwrap()
	}
}

struct TestMessageRouter {}

impl MessageRouter for TestMessageRouter {
	fn find_path(
		&self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination
	) -> Result<OnionMessagePath, ()> {
		Ok(OnionMessagePath {
			intermediate_nodes: vec![],
			destination,
		})
	}
}

struct TestOffersMessageHandler {}

impl OffersMessageHandler for TestOffersMessageHandler {
	fn handle_message(&self, _message: OffersMessage) -> Option<OffersMessage> {
		None
	}
}

#[derive(Clone, Debug, PartialEq)]
enum TestCustomMessage {
	Request,
	Response,
}

const CUSTOM_REQUEST_MESSAGE_TYPE: u64 = 4242;
const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];

impl CustomOnionMessageContents for TestCustomMessage {
	fn tlv_type(&self) -> u64 {
		match self {
			TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
			TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
		}
	}
}

impl Writeable for TestCustomMessage {
	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
		match self {
			TestCustomMessage::Request => Ok(CUSTOM_REQUEST_MESSAGE_CONTENTS.write(w)?),
			TestCustomMessage::Response => Ok(CUSTOM_RESPONSE_MESSAGE_CONTENTS.write(w)?),
		}
	}
}

struct TestCustomMessageHandler {
	expected_messages: Mutex<VecDeque<TestCustomMessage>>,
}

impl TestCustomMessageHandler {
	fn new() -> Self {
		Self { expected_messages: Mutex::new(VecDeque::new()) }
	}

	fn expect_message(&self, message: TestCustomMessage) {
		self.expected_messages.lock().unwrap().push_back(message);
	}
}

impl Drop for TestCustomMessageHandler {
	fn drop(&mut self) {
		#[cfg(feature = "std")] {
			if std::thread::panicking() {
				return;
			}
		}
		assert!(self.expected_messages.lock().unwrap().is_empty());
	}
}

impl CustomOnionMessageHandler for TestCustomMessageHandler {
	type CustomMessage = TestCustomMessage;
	fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
		match self.expected_messages.lock().unwrap().pop_front() {
			Some(expected_msg) => assert_eq!(expected_msg, msg),
			None => panic!("Unexpected message: {:?}", msg),
		}

		match msg {
			TestCustomMessage::Request => Some(TestCustomMessage::Response),
			TestCustomMessage::Response => None,
		}
	}
	fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
		match message_type {
			CUSTOM_REQUEST_MESSAGE_TYPE => {
				let buf = read_to_end(buffer)?;
				assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
				Ok(Some(TestCustomMessage::Request))
			},
			CUSTOM_RESPONSE_MESSAGE_TYPE => {
				let buf = read_to_end(buffer)?;
				assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
				Ok(Some(TestCustomMessage::Response))
			},
			_ => Ok(None),
		}
	}
}

fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
	let mut nodes = Vec::new();
	for i in 0..num_messengers {
		let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
		let seed = [i as u8; 32];
		let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
		let message_router = Arc::new(TestMessageRouter {});
		let offers_message_handler = Arc::new(TestOffersMessageHandler {});
		let custom_message_handler = Arc::new(TestCustomMessageHandler::new());
		nodes.push(MessengerNode {
			keys_manager: keys_manager.clone(),
			messenger: OnionMessenger::new(
				keys_manager.clone(), keys_manager, logger.clone(), message_router,
				offers_message_handler, custom_message_handler.clone()
			),
			custom_message_handler,
		});
	}
	for idx in 0..num_messengers - 1 {
		let i = idx as usize;
		let mut features = InitFeatures::empty();
		features.set_onion_messages_optional();
		let init_msg = msgs::Init { features, networks: None, remote_network_address: None };
		nodes[i].messenger.peer_connected(&nodes[i + 1].get_node_pk(), &init_msg.clone(), true).unwrap();
		nodes[i + 1].messenger.peer_connected(&nodes[i].get_node_pk(), &init_msg.clone(), false).unwrap();
	}
	nodes
}

fn pass_along_path(path: &Vec<MessengerNode>) {
	let mut prev_node = &path[0];
	for node in path.into_iter().skip(1) {
		let events = prev_node.messenger.release_pending_msgs();
		let onion_msg =  {
			let msgs = events.get(&node.get_node_pk()).unwrap();
			assert_eq!(msgs.len(), 1);
			msgs[0].clone()
		};
		node.messenger.handle_onion_message(&prev_node.get_node_pk(), &onion_msg);
		prev_node = node;
	}
}

#[test]
fn one_hop() {
	let nodes = create_nodes(2);
	let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);

	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::Node(nodes[1].get_node_pk()),
	};
	nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
	nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
	pass_along_path(&nodes);
}

#[test]
fn two_unblinded_hops() {
	let nodes = create_nodes(3);
	let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);

	let path = OnionMessagePath {
		intermediate_nodes: vec![nodes[1].get_node_pk()],
		destination: Destination::Node(nodes[2].get_node_pk()),
	};
	nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
	nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
	pass_along_path(&nodes);
}

#[test]
fn two_unblinded_two_blinded() {
	let nodes = create_nodes(5);
	let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);

	let secp_ctx = Secp256k1::new();
	let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
	let path = OnionMessagePath {
		intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
		destination: Destination::BlindedPath(blinded_path),
	};

	nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
	nodes[4].custom_message_handler.expect_message(TestCustomMessage::Response);
	pass_along_path(&nodes);
}

#[test]
fn three_blinded_hops() {
	let nodes = create_nodes(4);
	let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);

	let secp_ctx = Secp256k1::new();
	let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::BlindedPath(blinded_path),
	};

	nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
	nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
	pass_along_path(&nodes);
}

#[test]
fn too_big_packet_error() {
	// Make sure we error as expected if a packet is too big to send.
	let nodes = create_nodes(2);
	let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);

	let hop_node_id = nodes[1].get_node_pk();
	let hops = vec![hop_node_id; 400];
	let path = OnionMessagePath {
		intermediate_nodes: hops,
		destination: Destination::Node(hop_node_id),
	};
	let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
	assert_eq!(err, SendError::TooBigPacket);
}

#[test]
fn we_are_intro_node() {
	// If we are sending straight to a blinded path and we are the introduction node, we need to
	// advance the blinded path by 1 hop so the second hop is the new introduction node.
	let mut nodes = create_nodes(3);
	let test_msg = TestCustomMessage::Response;

	let secp_ctx = Secp256k1::new();
	let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::BlindedPath(blinded_path),
	};

	nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
	nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
	pass_along_path(&nodes);

	// Try with a two-hop blinded path where we are the introduction node.
	let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::BlindedPath(blinded_path),
	};
	nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
	nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
	nodes.remove(2);
	pass_along_path(&nodes);
}

#[test]
fn invalid_blinded_path_error() {
	// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
	let nodes = create_nodes(3);
	let test_msg = TestCustomMessage::Response;

	// 0 hops
	let secp_ctx = Secp256k1::new();
	let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
	blinded_path.blinded_hops.clear();
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::BlindedPath(blinded_path),
	};
	let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
	assert_eq!(err, SendError::TooFewBlindedHops);

	// 1 hop
	let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
	blinded_path.blinded_hops.remove(0);
	assert_eq!(blinded_path.blinded_hops.len(), 1);
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::BlindedPath(blinded_path),
	};
	let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
	assert_eq!(err, SendError::TooFewBlindedHops);
}

#[test]
fn reply_path() {
	let mut nodes = create_nodes(4);
	let test_msg = TestCustomMessage::Request;
	let secp_ctx = Secp256k1::new();

	// Destination::Node
	let path = OnionMessagePath {
		intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
		destination: Destination::Node(nodes[3].get_node_pk()),
	};
	let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
	nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
	nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
	pass_along_path(&nodes);
	// Make sure the last node successfully decoded the reply path.
	nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
	nodes.reverse();
	pass_along_path(&nodes);

	// Destination::BlindedPath
	let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::BlindedPath(blinded_path),
	};
	let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();

	nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
	nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
	pass_along_path(&nodes);

	// Make sure the last node successfully decoded the reply path.
	nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
	nodes.reverse();
	pass_along_path(&nodes);
}

#[test]
fn invalid_custom_message_type() {
	let nodes = create_nodes(2);

	struct InvalidCustomMessage{}
	impl CustomOnionMessageContents for InvalidCustomMessage {
		fn tlv_type(&self) -> u64 {
			// Onion message contents must have a TLV >= 64.
			63
		}
	}

	impl Writeable for InvalidCustomMessage {
		fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
	}

	let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {});
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::Node(nodes[1].get_node_pk()),
	};
	let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
	assert_eq!(err, SendError::InvalidMessage);
}

#[test]
fn peer_buffer_full() {
	let nodes = create_nodes(2);
	let test_msg = TestCustomMessage::Request;
	let path = OnionMessagePath {
		intermediate_nodes: vec![],
		destination: Destination::Node(nodes[1].get_node_pk()),
	};
	for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
		nodes[0].messenger.send_onion_message(path.clone(), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
	}
	let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
	assert_eq!(err, SendError::BufferFull);
}

#[test]
fn many_hops() {
	// Check we can send over a route with many hops. This will exercise our logic for onion messages
	// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
	let num_nodes: usize = 25;
	let nodes = create_nodes(num_nodes as u8);
	let test_msg = TestCustomMessage::Response;

	let mut intermediate_nodes = vec![];
	for i in 1..(num_nodes-1) {
		intermediate_nodes.push(nodes[i].get_node_pk());
	}

	let path = OnionMessagePath {
		intermediate_nodes,
		destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
	};
	nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
	nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
	pass_along_path(&nodes);
}