Skip to content

Commit e865e86

Browse files
committed
Add the stfu message as well
1 parent 4072e8b commit e865e86

File tree

14 files changed

+213
-1
lines changed

14 files changed

+213
-1
lines changed

fuzz/src/bin/gen_target.sh

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ GEN_TEST msg_tx_init_rbf msg_targets::
7171
GEN_TEST msg_tx_ack_rbf msg_targets::
7272
GEN_TEST msg_tx_abort msg_targets::
7373

74+
GEN_TEST msg_stfu msg_targets::
7475
GEN_TEST msg_splice msg_targets::
7576
GEN_TEST msg_splice_ack msg_targets::
7677
GEN_TEST msg_splice_locked msg_targets::

fuzz/src/bin/msg_stfu_target.rs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
// This file is auto-generated by gen_target.sh based on target_template.txt
11+
// To modify it, modify target_template.txt and run gen_target.sh instead.
12+
13+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14+
15+
#[cfg(not(fuzzing))]
16+
compile_error!("Fuzz targets need cfg=fuzzing");
17+
18+
extern crate lightning_fuzz;
19+
use lightning_fuzz::msg_targets::msg_stfu::*;
20+
21+
#[cfg(feature = "afl")]
22+
#[macro_use] extern crate afl;
23+
#[cfg(feature = "afl")]
24+
fn main() {
25+
fuzz!(|data| {
26+
msg_stfu_run(data.as_ptr(), data.len());
27+
});
28+
}
29+
30+
#[cfg(feature = "honggfuzz")]
31+
#[macro_use] extern crate honggfuzz;
32+
#[cfg(feature = "honggfuzz")]
33+
fn main() {
34+
loop {
35+
fuzz!(|data| {
36+
msg_stfu_run(data.as_ptr(), data.len());
37+
});
38+
}
39+
}
40+
41+
#[cfg(feature = "libfuzzer_fuzz")]
42+
#[macro_use] extern crate libfuzzer_sys;
43+
#[cfg(feature = "libfuzzer_fuzz")]
44+
fuzz_target!(|data: &[u8]| {
45+
msg_stfu_run(data.as_ptr(), data.len());
46+
});
47+
48+
#[cfg(feature = "stdin_fuzz")]
49+
fn main() {
50+
use std::io::Read;
51+
52+
let mut data = Vec::with_capacity(8192);
53+
std::io::stdin().read_to_end(&mut data).unwrap();
54+
msg_stfu_run(data.as_ptr(), data.len());
55+
}
56+
57+
#[test]
58+
fn run_test_cases() {
59+
use std::fs;
60+
use std::io::Read;
61+
use lightning_fuzz::utils::test_logger::StringBuffer;
62+
63+
use std::sync::{atomic, Arc};
64+
{
65+
let data: Vec<u8> = vec![0];
66+
msg_stfu_run(data.as_ptr(), data.len());
67+
}
68+
let mut threads = Vec::new();
69+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
70+
if let Ok(tests) = fs::read_dir("test_cases/msg_stfu") {
71+
for test in tests {
72+
let mut data: Vec<u8> = Vec::new();
73+
let path = test.unwrap().path();
74+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
75+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
76+
77+
let thread_count_ref = Arc::clone(&threads_running);
78+
let main_thread_ref = std::thread::current();
79+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
80+
std::thread::spawn(move || {
81+
let string_logger = StringBuffer::new();
82+
83+
let panic_logger = string_logger.clone();
84+
let res = if ::std::panic::catch_unwind(move || {
85+
msg_stfu_test(&data, panic_logger);
86+
}).is_err() {
87+
Some(string_logger.into_string())
88+
} else { None };
89+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
90+
main_thread_ref.unpark();
91+
res
92+
})
93+
));
94+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
95+
std::thread::park();
96+
}
97+
}
98+
}
99+
let mut failed_outputs = Vec::new();
100+
for (test, thread) in threads.drain(..) {
101+
if let Some(output) = thread.join().unwrap() {
102+
println!("\nOutput of {}:\n{}\n", test, output);
103+
failed_outputs.push(test);
104+
}
105+
}
106+
if !failed_outputs.is_empty() {
107+
println!("Test cases which failed: ");
108+
for case in failed_outputs {
109+
println!("{}", case);
110+
}
111+
panic!();
112+
}
113+
}

fuzz/src/msg_targets/gen_target.sh

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ GEN_TEST lightning::ln::msgs::TxInitRbf test_msg_simple ""
5959
GEN_TEST lightning::ln::msgs::TxAckRbf test_msg_simple ""
6060
GEN_TEST lightning::ln::msgs::TxAbort test_msg_simple ""
6161

62+
GEN_TEST lightning::ln::msgs::Stfu test_msg_simple ""
6263
GEN_TEST lightning::ln::msgs::Splice test_msg_simple ""
6364
GEN_TEST lightning::ln::msgs::SpliceAck test_msg_simple ""
6465
GEN_TEST lightning::ln::msgs::SpliceLocked test_msg_simple ""

fuzz/src/msg_targets/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub mod msg_tx_signatures;
4141
pub mod msg_tx_init_rbf;
4242
pub mod msg_tx_ack_rbf;
4343
pub mod msg_tx_abort;
44+
pub mod msg_stfu;
4445
pub mod msg_splice;
4546
pub mod msg_splice_ack;
4647
pub mod msg_splice_locked;

fuzz/src/msg_targets/msg_stfu.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
11+
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
12+
13+
use crate::msg_targets::utils::VecWriter;
14+
use crate::utils::test_logger;
15+
16+
#[inline]
17+
pub fn msg_stfu_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
18+
test_msg_simple!(lightning::ln::msgs::Stfu, data);
19+
}
20+
21+
#[no_mangle]
22+
pub extern "C" fn msg_stfu_run(data: *const u8, datalen: usize) {
23+
let data = unsafe { std::slice::from_raw_parts(data, datalen) };
24+
test_msg_simple!(lightning::ln::msgs::Stfu, data);
25+
}

fuzz/targets.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void msg_tx_signatures_run(const unsigned char* data, size_t data_len);
5757
void msg_tx_init_rbf_run(const unsigned char* data, size_t data_len);
5858
void msg_tx_ack_rbf_run(const unsigned char* data, size_t data_len);
5959
void msg_tx_abort_run(const unsigned char* data, size_t data_len);
60+
void msg_stfu_run(const unsigned char* data, size_t data_len);
6061
void msg_splice_run(const unsigned char* data, size_t data_len);
6162
void msg_splice_ack_run(const unsigned char* data, size_t data_len);
6263
void msg_splice_locked_run(const unsigned char* data, size_t data_len);

lightning-net-tokio/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ mod tests {
605605
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &ChannelUpdate) {}
606606
fn handle_open_channel_v2(&self, _their_node_id: &PublicKey, _msg: &OpenChannelV2) {}
607607
fn handle_accept_channel_v2(&self, _their_node_id: &PublicKey, _msg: &AcceptChannelV2) {}
608+
fn handle_stfu(&self, _their_node_id: &PublicKey, _msg: &Stfu) {}
608609
fn handle_splice(&self, _their_node_id: &PublicKey, _msg: &Splice) {}
609610
fn handle_splice_ack(&self, _their_node_id: &PublicKey, _msg: &SpliceAck) {}
610611
fn handle_splice_locked(&self, _their_node_id: &PublicKey, _msg: &SpliceLocked) {}

lightning/src/events/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,13 @@ pub enum MessageSendEvent {
16421642
/// The message which should be sent.
16431643
msg: msgs::FundingSigned,
16441644
},
1645+
/// Used to indicate that a stfu message should be sent to the peer with the given node id.
1646+
SendStfu {
1647+
/// The node_id of the node which should receive this message
1648+
node_id: PublicKey,
1649+
/// The message which should be sent.
1650+
msg: msgs::Stfu,
1651+
},
16451652
/// Used to indicate that a splice message should be sent to the peer with the given node id.
16461653
SendSplice {
16471654
/// The node_id of the node which should receive this message

lightning/src/ln/channelmanager.rs

+7
Original file line numberDiff line numberDiff line change
@@ -8521,6 +8521,12 @@ where
85218521
});
85228522
}
85238523

8524+
fn handle_stfu(&self, counterparty_node_id: &PublicKey, msg: &msgs::Stfu) {
8525+
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8526+
"Splicing not supported".to_owned(),
8527+
msg.channel_id.clone())), *counterparty_node_id);
8528+
}
8529+
85248530
fn handle_splice(&self, counterparty_node_id: &PublicKey, msg: &msgs::Splice) {
85258531
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
85268532
"Splicing not supported".to_owned(),
@@ -8708,6 +8714,7 @@ where
87088714
&events::MessageSendEvent::SendChannelReady { .. } => false,
87098715
&events::MessageSendEvent::SendAnnouncementSignatures { .. } => false,
87108716
// Splicing
8717+
&events::MessageSendEvent::SendStfu { .. } => false,
87118718
&events::MessageSendEvent::SendSplice { .. } => false,
87128719
&events::MessageSendEvent::SendSpliceAck { .. } => false,
87138720
&events::MessageSendEvent::SendSpliceLocked { .. } => false,

lightning/src/ln/functional_test_utils.rs

+3
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &mut
814814
MessageSendEvent::SendOpenChannelV2 { node_id, .. } => {
815815
node_id == msg_node_id
816816
},
817+
MessageSendEvent::SendStfu { node_id, .. } => {
818+
node_id == msg_node_id
819+
},
817820
MessageSendEvent::SendSplice { node_id, .. } => {
818821
node_id == msg_node_id
819822
},

lightning/src/ln/msgs.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,17 @@ pub struct ChannelReady {
441441
pub short_channel_id_alias: Option<u64>,
442442
}
443443

444-
/// A splice message to be sent by or received from the splice initiator.
444+
/// An stfu (quiescence) message to be sent by or received from the stfu initiator.
445+
// TODO(splicing): Add spec link for `splice`; still in draft, using from https://github.com/lightning/bolts/pull/863
446+
#[derive(Clone, Debug, PartialEq, Eq)]
447+
pub struct Stfu {
448+
/// The channel ID where quiescence is intended
449+
pub channel_id: ChannelId,
450+
/// Initiator flag, 1 if initiating, 0 if replying to an stfu.
451+
pub initiator: u8,
452+
}
453+
454+
/// A splice message to be sent by or received from the stfu initiator (splice initiator).
445455
// TODO(splicing): Add spec link for `splice`; still in draft, using from https://github.com/lightning/bolts/pull/863
446456
#[derive(Clone, Debug, PartialEq, Eq)]
447457
pub struct Splice {
@@ -1454,6 +1464,8 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
14541464
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned);
14551465

14561466
// Splicing
1467+
/// Handle an incoming `stfu` message from the given peer.
1468+
fn handle_stfu(&self, their_node_id: &PublicKey, msg: &Stfu);
14571469
/// Handle an incoming `splice` message from the given peer.
14581470
fn handle_splice(&self, their_node_id: &PublicKey, msg: &Splice);
14591471
/// Handle an incoming `splice_ack` message from the given peer.
@@ -1865,6 +1877,11 @@ impl_writeable_msg!(AcceptChannelV2, {
18651877
(2, require_confirmed_inputs, option),
18661878
});
18671879

1880+
impl_writeable_msg!(Stfu, {
1881+
channel_id,
1882+
initiator,
1883+
}, {});
1884+
18681885
impl_writeable_msg!(Splice, {
18691886
channel_id,
18701887
chain_hash,
@@ -3453,6 +3470,16 @@ mod tests {
34533470
assert_eq!(hex::encode(encoded_value), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240000007d000000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
34543471
}
34553472

3473+
#[test]
3474+
fn encoding_stfu() {
3475+
let stfu = msgs::Stfu {
3476+
channel_id: ChannelId::from_bytes([2; 32]),
3477+
initiator: 1,
3478+
};
3479+
let encoded_value = stfu.encode();
3480+
assert_eq!(hex::encode(encoded_value), "020202020202020202020202020202020202020202020202020202020202020201");
3481+
}
3482+
34563483
#[test]
34573484
fn encoding_splice_ack() {
34583485
let secp_ctx = Secp256k1::new();

lightning/src/ln/peer_handler.rs

+12
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ impl ChannelMessageHandler for ErroringMessageHandler {
231231
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
232232
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
233233
}
234+
fn handle_stfu(&self, their_node_id: &PublicKey, msg: &msgs::Stfu) {
235+
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
236+
}
234237
fn handle_splice(&self, their_node_id: &PublicKey, msg: &msgs::Splice) {
235238
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
236239
}
@@ -1653,6 +1656,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
16531656
},
16541657

16551658
// Splicing messages:
1659+
wire::Message::Stfu(msg) => {
1660+
self.message_handler.chan_handler.handle_stfu(&their_node_id, &msg);
1661+
}
16561662
wire::Message::Splice(msg) => {
16571663
self.message_handler.chan_handler.handle_splice(&their_node_id, &msg);
16581664
}
@@ -1980,6 +1986,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
19801986
&msg.channel_id);
19811987
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
19821988
},
1989+
MessageSendEvent::SendStfu { ref node_id, ref msg} => {
1990+
log_debug!(self.logger, "Handling SendStfu event in peer_handler for node {} for channel {}",
1991+
log_pubkey!(node_id),
1992+
&msg.channel_id);
1993+
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
1994+
}
19831995
MessageSendEvent::SendSplice { ref node_id, ref msg} => {
19841996
log_debug!(self.logger, "Handling SendSplice event in peer_handler for node {} for channel {}",
19851997
log_pubkey!(node_id),

lightning/src/ln/wire.rs

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type + TestEq {
5959
AcceptChannelV2(msgs::AcceptChannelV2),
6060
FundingCreated(msgs::FundingCreated),
6161
FundingSigned(msgs::FundingSigned),
62+
Stfu(msgs::Stfu),
6263
Splice(msgs::Splice),
6364
SpliceAck(msgs::SpliceAck),
6465
SpliceLocked(msgs::SpliceLocked),
@@ -113,6 +114,7 @@ impl<T> Writeable for Message<T> where T: core::fmt::Debug + Type + TestEq {
113114
&Message::AcceptChannelV2(ref msg) => msg.write(writer),
114115
&Message::FundingCreated(ref msg) => msg.write(writer),
115116
&Message::FundingSigned(ref msg) => msg.write(writer),
117+
&Message::Stfu(ref msg) => msg.write(writer),
116118
&Message::Splice(ref msg) => msg.write(writer),
117119
&Message::SpliceAck(ref msg) => msg.write(writer),
118120
&Message::SpliceLocked(ref msg) => msg.write(writer),
@@ -167,6 +169,7 @@ impl<T> Type for Message<T> where T: core::fmt::Debug + Type + TestEq {
167169
&Message::AcceptChannelV2(ref msg) => msg.type_id(),
168170
&Message::FundingCreated(ref msg) => msg.type_id(),
169171
&Message::FundingSigned(ref msg) => msg.type_id(),
172+
&Message::Stfu(ref msg) => msg.type_id(),
170173
&Message::Splice(ref msg) => msg.type_id(),
171174
&Message::SpliceAck(ref msg) => msg.type_id(),
172175
&Message::SpliceLocked(ref msg) => msg.type_id(),
@@ -270,6 +273,9 @@ fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u1
270273
msgs::Splice::TYPE => {
271274
Ok(Message::Splice(Readable::read(buffer)?))
272275
},
276+
msgs::Stfu::TYPE => {
277+
Ok(Message::Stfu(Readable::read(buffer)?))
278+
},
273279
msgs::SpliceAck::TYPE => {
274280
Ok(Message::SpliceAck(Readable::read(buffer)?))
275281
},
@@ -426,6 +432,10 @@ impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
426432
fn type_id(&self) -> u16 { T::TYPE }
427433
}
428434

435+
impl Encode for msgs::Stfu {
436+
const TYPE: u16 = 2;
437+
}
438+
429439
impl Encode for msgs::Init {
430440
const TYPE: u16 = 16;
431441
}

lightning/src/util/test_utils.rs

+3
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
651651
fn handle_closing_signed(&self, _their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
652652
self.received_msg(wire::Message::ClosingSigned(msg.clone()));
653653
}
654+
fn handle_stfu(&self, _their_node_id: &PublicKey, msg: &msgs::Stfu) {
655+
self.received_msg(wire::Message::Stfu(msg.clone()));
656+
}
654657
fn handle_splice(&self, _their_node_id: &PublicKey, msg: &msgs::Splice) {
655658
self.received_msg(wire::Message::Splice(msg.clone()));
656659
}

0 commit comments

Comments
 (0)