1 // This file is Copyright its original authors, visible in version control
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
10 use bitcoin::blockdata::constants::ChainHash;
11 use bitcoin::blockdata::script::Builder;
12 use bitcoin::blockdata::transaction::TxOut;
14 use lightning::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
15 use lightning::chain::transaction::OutPoint;
16 use lightning::ln::ChannelId;
17 use lightning::ln::channelmanager::{self, ChannelDetails, ChannelCounterparty};
18 use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
19 use lightning::ln::msgs;
20 use lightning::offers::invoice::BlindedPayInfo;
21 use lightning::routing::gossip::{NetworkGraph, RoutingFees};
22 use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoLookupError, UtxoResult};
23 use lightning::routing::router::{find_route, PaymentParameters, RouteHint, RouteHintHop, RouteParameters};
24 use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
25 use lightning::util::config::UserConfig;
26 use lightning::util::hash_tables::*;
27 use lightning::util::ser::Readable;
29 use bitcoin::hashes::Hash;
30 use bitcoin::secp256k1::PublicKey;
31 use bitcoin::network::constants::Network;
33 use crate::utils::test_logger;
35 use std::convert::TryInto;
37 use std::sync::atomic::{AtomicUsize, Ordering};
40 pub fn slice_to_be16(v: &[u8]) -> u16 {
41 ((v[0] as u16) << 8*1) |
42 ((v[1] as u16) << 8*0)
46 pub fn slice_to_be32(v: &[u8]) -> u32 {
47 ((v[0] as u32) << 8*3) |
48 ((v[1] as u32) << 8*2) |
49 ((v[2] as u32) << 8*1) |
50 ((v[3] as u32) << 8*0)
54 pub fn slice_to_be64(v: &[u8]) -> u64 {
55 ((v[0] as u64) << 8*7) |
56 ((v[1] as u64) << 8*6) |
57 ((v[2] as u64) << 8*5) |
58 ((v[3] as u64) << 8*4) |
59 ((v[4] as u64) << 8*3) |
60 ((v[5] as u64) << 8*2) |
61 ((v[6] as u64) << 8*1) |
62 ((v[7] as u64) << 8*0)
68 read_pos: AtomicUsize,
71 fn get_slice(&self, len: usize) -> Option<&[u8]> {
72 let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
73 if self.data.len() < old_pos + len {
76 Some(&self.data[old_pos..old_pos + len])
78 fn get_slice_nonadvancing(&self, len: usize) -> Option<&[u8]> {
79 let old_pos = self.read_pos.load(Ordering::Acquire);
80 if self.data.len() < old_pos + len {
83 Some(&self.data[old_pos..old_pos + len])
87 struct FuzzChainSource<'a, 'b, Out: test_logger::Output> {
88 input: Arc<InputData>,
89 net_graph: &'a NetworkGraph<&'b test_logger::TestLogger<Out>>,
91 impl<Out: test_logger::Output> UtxoLookup for FuzzChainSource<'_, '_, Out> {
92 fn get_utxo(&self, _chain_hash: &ChainHash, _short_channel_id: u64) -> UtxoResult {
93 let input_slice = self.input.get_slice(2);
94 if input_slice.is_none() { return UtxoResult::Sync(Err(UtxoLookupError::UnknownTx)); }
95 let input_slice = input_slice.unwrap();
97 value: if input_slice[0] % 2 == 0 { 1_000_000 } else { 1_000 },
98 script_pubkey: Builder::new().push_int(input_slice[1] as i64).into_script().to_v0_p2wsh(),
101 &[0, _] => UtxoResult::Sync(Err(UtxoLookupError::UnknownChain)),
102 &[1, _] => UtxoResult::Sync(Err(UtxoLookupError::UnknownTx)),
104 let future = UtxoFuture::new();
105 future.resolve_without_forwarding(self.net_graph, Ok(txo_res));
106 UtxoResult::Async(future.clone())
109 let future = UtxoFuture::new();
110 future.resolve_without_forwarding(self.net_graph, Err(UtxoLookupError::UnknownTx));
111 UtxoResult::Async(future.clone())
114 UtxoResult::Async(UtxoFuture::new()) // the future will never resolve
116 &[..] => UtxoResult::Sync(Ok(txo_res)),
122 pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
123 let input = Arc::new(InputData {
125 read_pos: AtomicUsize::new(0),
127 macro_rules! get_slice_nonadvancing {
129 match input.get_slice_nonadvancing($len as usize) {
130 Some(slice) => slice,
135 macro_rules! get_slice {
137 match input.get_slice($len as usize) {
138 Some(slice) => slice,
144 macro_rules! decode_msg {
145 ($MsgType: path, $len: expr) => {{
146 let mut reader = ::std::io::Cursor::new(get_slice!($len));
147 match <$MsgType>::read(&mut reader) {
149 assert_eq!(reader.position(), $len as u64);
153 msgs::DecodeError::UnknownVersion => return,
154 msgs::DecodeError::UnknownRequiredFeature => return,
155 msgs::DecodeError::InvalidValue => return,
156 msgs::DecodeError::BadLengthDescriptor => return,
157 msgs::DecodeError::ShortRead => panic!("We picked the length..."),
158 msgs::DecodeError::Io(e) => panic!("{:?}", e),
159 msgs::DecodeError::UnsupportedCompression => return,
160 msgs::DecodeError::DangerousValue => return,
166 macro_rules! decode_msg_with_len16 {
167 ($MsgType: path, $excess: expr) => {
169 let extra_len = slice_to_be16(get_slice_nonadvancing!(2));
170 decode_msg!($MsgType, 2 + (extra_len as usize) + $excess)
175 macro_rules! get_pubkey_from_node_id {
176 ($node_id: expr ) => {
177 match PublicKey::from_slice($node_id.as_slice()) {
184 macro_rules! get_pubkey {
186 match PublicKey::from_slice(get_slice!(33)) {
193 let logger = test_logger::TestLogger::new("".to_owned(), out);
195 let our_pubkey = get_pubkey!();
196 let net_graph = NetworkGraph::new(Network::Bitcoin, &logger);
197 let chain_source = FuzzChainSource {
198 input: Arc::clone(&input),
199 net_graph: &net_graph,
202 let mut node_pks = new_hash_map();
205 macro_rules! first_hops {
206 ($first_hops_vec: expr) => {
207 match get_slice!(1)[0] {
213 node_pks.iter().skip(u16::from_be_bytes(get_slice!(2).try_into().unwrap()) as usize % node_pks.len()).next().unwrap();
214 let capacity = u64::from_be_bytes(get_slice!(8).try_into().unwrap());
215 $first_hops_vec.push(ChannelDetails {
216 channel_id: ChannelId::new_zero(),
217 counterparty: ChannelCounterparty {
219 features: channelmanager::provided_init_features(&UserConfig::default()),
220 unspendable_punishment_reserve: 0,
221 forwarding_info: None,
222 outbound_htlc_minimum_msat: None,
223 outbound_htlc_maximum_msat: None,
225 funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
227 short_channel_id: Some(scid),
228 inbound_scid_alias: None,
229 outbound_scid_alias: None,
230 channel_value_satoshis: capacity,
231 user_channel_id: 0, inbound_capacity_msat: 0,
232 unspendable_punishment_reserve: None,
233 confirmations_required: None,
235 force_close_spend_delay: None,
236 is_outbound: true, is_channel_ready: true,
237 is_usable: true, is_public: true,
239 outbound_capacity_msat: capacity.saturating_mul(1000),
240 next_outbound_htlc_limit_msat: capacity.saturating_mul(1000),
241 next_outbound_htlc_minimum_msat: 0,
242 inbound_htlc_minimum_msat: None,
243 inbound_htlc_maximum_msat: None,
245 feerate_sat_per_1000_weight: None,
246 channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
247 pending_inbound_htlcs: Vec::new(),
248 pending_outbound_htlcs: Vec::new(),
251 Some(&$first_hops_vec[..])
257 macro_rules! last_hops {
258 ($last_hops: expr) => {
259 let count = get_slice!(1)[0];
263 node_pks.iter().skip(slice_to_be16(get_slice!(2)) as usize % node_pks.len()).next().unwrap();
264 $last_hops.push(RouteHint(vec![RouteHintHop {
266 short_channel_id: scid,
268 base_msat: slice_to_be32(get_slice!(4)),
269 proportional_millionths: slice_to_be32(get_slice!(4)),
271 cltv_expiry_delta: slice_to_be16(get_slice!(2)),
272 htlc_minimum_msat: Some(slice_to_be64(get_slice!(8))),
273 htlc_maximum_msat: None,
279 macro_rules! find_routes {
280 ($first_hops: expr, $node_pks: expr, $route_params: expr) => {
281 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &net_graph, &logger);
282 let random_seed_bytes: [u8; 32] = [get_slice!(1)[0]; 32];
283 for (target, ()) in $node_pks {
284 let final_value_msat = slice_to_be64(get_slice!(8));
285 let final_cltv_expiry_delta = slice_to_be32(get_slice!(4));
286 let route_params = $route_params(final_value_msat, final_cltv_expiry_delta, target);
287 let _ = find_route(&our_pubkey, &route_params, &net_graph,
288 $first_hops.map(|c| c.iter().collect::<Vec<_>>()).as_ref().map(|a| a.as_slice()),
289 &logger, &scorer, &ProbabilisticScoringFeeParameters::default(), &random_seed_bytes);
295 match get_slice!(1)[0] {
297 let start_len = slice_to_be16(&get_slice_nonadvancing!(2)[0..2]) as usize;
298 let addr_len = slice_to_be16(&get_slice_nonadvancing!(start_len+2 + 74)[start_len+2 + 72..start_len+2 + 74]);
299 if addr_len > (37+1)*4 {
302 let msg = decode_msg_with_len16!(msgs::UnsignedNodeAnnouncement, 288);
303 node_pks.insert(get_pubkey_from_node_id!(msg.node_id), ());
304 let _ = net_graph.update_node_from_unsigned_announcement(&msg);
307 let msg = decode_msg_with_len16!(msgs::UnsignedChannelAnnouncement, 32+8+33*4);
308 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_1), ());
309 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_2), ());
310 let _ = net_graph.update_channel_from_unsigned_announcement::
311 <&FuzzChainSource<'_, '_, Out>>(&msg, &None);
314 let msg = decode_msg_with_len16!(msgs::UnsignedChannelAnnouncement, 32+8+33*4);
315 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_1), ());
316 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_2), ());
317 let _ = net_graph.update_channel_from_unsigned_announcement(&msg, &Some(&chain_source));
320 let _ = net_graph.update_channel_unsigned(&decode_msg!(msgs::UnsignedChannelUpdate, 72));
323 let short_channel_id = slice_to_be64(get_slice!(8));
324 net_graph.channel_failed_permanent(short_channel_id);
326 _ if node_pks.is_empty() => {},
328 let mut first_hops_vec = Vec::new();
329 // Use macros here and in the blinded match arm to ensure values are fetched from the fuzz
330 // input in the same order, for better coverage.
331 let first_hops = first_hops!(first_hops_vec);
332 let mut last_hops = Vec::new();
333 last_hops!(last_hops);
334 find_routes!(first_hops, node_pks.iter(), |final_amt, final_delta, target: &PublicKey| {
335 RouteParameters::from_payment_params_and_value(
336 PaymentParameters::from_node_id(*target, final_delta)
337 .with_route_hints(last_hops.clone()).unwrap(),
342 let mut first_hops_vec = Vec::new();
343 let first_hops = first_hops!(first_hops_vec);
344 let mut last_hops_unblinded = Vec::new();
345 last_hops!(last_hops_unblinded);
346 let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap();
347 let last_hops: Vec<(BlindedPayInfo, BlindedPath)> = last_hops_unblinded.into_iter().map(|hint| {
348 let hop = &hint.0[0];
349 let payinfo = BlindedPayInfo {
350 fee_base_msat: hop.fees.base_msat,
351 fee_proportional_millionths: hop.fees.proportional_millionths,
352 htlc_minimum_msat: hop.htlc_minimum_msat.unwrap(),
353 htlc_maximum_msat: hop.htlc_minimum_msat.unwrap().saturating_mul(100),
354 cltv_expiry_delta: hop.cltv_expiry_delta,
355 features: BlindedHopFeatures::empty(),
357 let num_blinded_hops = x % 250;
358 let mut blinded_hops = Vec::new();
359 for _ in 0..num_blinded_hops {
360 blinded_hops.push(BlindedHop {
361 blinded_node_id: dummy_pk,
362 encrypted_payload: Vec::new()
365 (payinfo, BlindedPath {
366 introduction_node: IntroductionNode::NodeId(hop.src_node_id),
367 blinding_point: dummy_pk,
371 let mut features = Bolt12InvoiceFeatures::empty();
372 features.set_basic_mpp_optional();
373 find_routes!(first_hops, [(dummy_pk, ())].iter(), |final_amt, _, _| {
374 RouteParameters::from_payment_params_and_value(PaymentParameters::blinded(last_hops.clone())
375 .with_bolt12_features(features.clone()).unwrap(),
383 pub fn router_test<Out: test_logger::Output>(data: &[u8], out: Out) {
388 pub extern "C" fn router_run(data: *const u8, datalen: usize) {
389 do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});