Merge pull request #3063 from jirijakes/upgrade-bitcoin-031
[rust-lightning] / fuzz / src / router.rs
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 use bitcoin::amount::Amount;
11 use bitcoin::blockdata::constants::ChainHash;
12 use bitcoin::blockdata::script::Builder;
13 use bitcoin::blockdata::transaction::TxOut;
14
15 use lightning::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
16 use lightning::chain::transaction::OutPoint;
17 use lightning::ln::ChannelId;
18 use lightning::ln::channelmanager::{self, ChannelDetails, ChannelCounterparty};
19 use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
20 use lightning::ln::msgs;
21 use lightning::offers::invoice::BlindedPayInfo;
22 use lightning::routing::gossip::{NetworkGraph, RoutingFees};
23 use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoLookupError, UtxoResult};
24 use lightning::routing::router::{find_route, PaymentParameters, RouteHint, RouteHintHop, RouteParameters};
25 use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
26 use lightning::util::config::UserConfig;
27 use lightning::util::hash_tables::*;
28 use lightning::util::ser::Readable;
29
30 use bitcoin::hashes::Hash;
31 use bitcoin::secp256k1::PublicKey;
32 use bitcoin::network::Network;
33
34 use crate::utils::test_logger;
35
36 use std::convert::TryInto;
37 use std::sync::Arc;
38 use std::sync::atomic::{AtomicUsize, Ordering};
39
40 #[inline]
41 pub fn slice_to_be16(v: &[u8]) -> u16 {
42         ((v[0] as u16) << 8*1) |
43         ((v[1] as u16) << 8*0)
44 }
45
46 #[inline]
47 pub fn slice_to_be32(v: &[u8]) -> u32 {
48         ((v[0] as u32) << 8*3) |
49         ((v[1] as u32) << 8*2) |
50         ((v[2] as u32) << 8*1) |
51         ((v[3] as u32) << 8*0)
52 }
53
54 #[inline]
55 pub fn slice_to_be64(v: &[u8]) -> u64 {
56         ((v[0] as u64) << 8*7) |
57         ((v[1] as u64) << 8*6) |
58         ((v[2] as u64) << 8*5) |
59         ((v[3] as u64) << 8*4) |
60         ((v[4] as u64) << 8*3) |
61         ((v[5] as u64) << 8*2) |
62         ((v[6] as u64) << 8*1) |
63         ((v[7] as u64) << 8*0)
64 }
65
66
67 struct InputData {
68         data: Vec<u8>,
69         read_pos: AtomicUsize,
70 }
71 impl InputData {
72         fn get_slice(&self, len: usize) -> Option<&[u8]> {
73                 let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
74                 if self.data.len() < old_pos + len {
75                         return None;
76                 }
77                 Some(&self.data[old_pos..old_pos + len])
78         }
79         fn get_slice_nonadvancing(&self, len: usize) -> Option<&[u8]> {
80                 let old_pos = self.read_pos.load(Ordering::Acquire);
81                 if self.data.len() < old_pos + len {
82                         return None;
83                 }
84                 Some(&self.data[old_pos..old_pos + len])
85         }
86 }
87
88 struct FuzzChainSource<'a, 'b, Out: test_logger::Output> {
89         input: Arc<InputData>,
90         net_graph: &'a NetworkGraph<&'b test_logger::TestLogger<Out>>,
91 }
92 impl<Out: test_logger::Output> UtxoLookup for FuzzChainSource<'_, '_, Out> {
93         fn get_utxo(&self, _chain_hash: &ChainHash, _short_channel_id: u64) -> UtxoResult {
94                 let input_slice = self.input.get_slice(2);
95                 if input_slice.is_none() { return UtxoResult::Sync(Err(UtxoLookupError::UnknownTx)); }
96                 let input_slice = input_slice.unwrap();
97                 let txo_res = TxOut {
98                         value: Amount::from_sat(if input_slice[0] % 2 == 0 { 1_000_000 } else { 1_000 }),
99                         script_pubkey: Builder::new().push_int(input_slice[1] as i64).into_script().to_p2wsh(),
100                 };
101                 match input_slice {
102                         &[0, _] => UtxoResult::Sync(Err(UtxoLookupError::UnknownChain)),
103                         &[1, _] => UtxoResult::Sync(Err(UtxoLookupError::UnknownTx)),
104                         &[2, _] => {
105                                 let future = UtxoFuture::new();
106                                 future.resolve_without_forwarding(self.net_graph, Ok(txo_res));
107                                 UtxoResult::Async(future.clone())
108                         },
109                         &[3, _] => {
110                                 let future = UtxoFuture::new();
111                                 future.resolve_without_forwarding(self.net_graph, Err(UtxoLookupError::UnknownTx));
112                                 UtxoResult::Async(future.clone())
113                         },
114                         &[4, _] => {
115                                 UtxoResult::Async(UtxoFuture::new()) // the future will never resolve
116                         },
117                         &[..] => UtxoResult::Sync(Ok(txo_res)),
118                 }
119         }
120 }
121
122 #[inline]
123 pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
124         let input = Arc::new(InputData {
125                 data: data.to_vec(),
126                 read_pos: AtomicUsize::new(0),
127         });
128         macro_rules! get_slice_nonadvancing {
129                 ($len: expr) => {
130                         match input.get_slice_nonadvancing($len as usize) {
131                                 Some(slice) => slice,
132                                 None => return,
133                         }
134                 }
135         }
136         macro_rules! get_slice {
137                 ($len: expr) => {
138                         match input.get_slice($len as usize) {
139                                 Some(slice) => slice,
140                                 None => return,
141                         }
142                 }
143         }
144
145         macro_rules! decode_msg {
146                 ($MsgType: path, $len: expr) => {{
147                         let mut reader = ::std::io::Cursor::new(get_slice!($len));
148                         match <$MsgType>::read(&mut reader) {
149                                 Ok(msg) => {
150                                         assert_eq!(reader.position(), $len as u64);
151                                         msg
152                                 },
153                                 Err(e) => match e {
154                                         msgs::DecodeError::UnknownVersion => return,
155                                         msgs::DecodeError::UnknownRequiredFeature => return,
156                                         msgs::DecodeError::InvalidValue => return,
157                                         msgs::DecodeError::BadLengthDescriptor => return,
158                                         msgs::DecodeError::ShortRead => panic!("We picked the length..."),
159                                         msgs::DecodeError::Io(e) => panic!("{:?}", e),
160                                         msgs::DecodeError::UnsupportedCompression => return,
161                                         msgs::DecodeError::DangerousValue => return,
162                                 }
163                         }
164                 }}
165         }
166
167         macro_rules! decode_msg_with_len16 {
168                 ($MsgType: path, $excess: expr) => {
169                         {
170                                 let extra_len = slice_to_be16(get_slice_nonadvancing!(2));
171                                 decode_msg!($MsgType, 2 + (extra_len as usize) + $excess)
172                         }
173                 }
174         }
175
176         macro_rules! get_pubkey_from_node_id {
177                 ($node_id: expr ) => {
178                         match PublicKey::from_slice($node_id.as_slice()) {
179                                 Ok(pk) => pk,
180                                 Err(_) => return,
181                         }
182                 }
183         }
184
185         macro_rules! get_pubkey {
186                 () => {
187                         match PublicKey::from_slice(get_slice!(33)) {
188                                 Ok(key) => key,
189                                 Err(_) => return,
190                         }
191                 }
192         }
193
194         let logger = test_logger::TestLogger::new("".to_owned(), out);
195
196         let our_pubkey = get_pubkey!();
197         let net_graph = NetworkGraph::new(Network::Bitcoin, &logger);
198         let chain_source = FuzzChainSource {
199                 input: Arc::clone(&input),
200                 net_graph: &net_graph,
201         };
202
203         let mut node_pks = new_hash_map();
204         let mut scid = 42;
205
206         macro_rules! first_hops {
207                 ($first_hops_vec: expr) => {
208                         match get_slice!(1)[0] {
209                                 0 => None,
210                                 count => {
211                                         for _ in 0..count {
212                                                 scid += 1;
213                                                 let (rnid, _) =
214                                                         node_pks.iter().skip(u16::from_be_bytes(get_slice!(2).try_into().unwrap()) as usize % node_pks.len()).next().unwrap();
215                                                 let capacity = u64::from_be_bytes(get_slice!(8).try_into().unwrap());
216                                                 $first_hops_vec.push(ChannelDetails {
217                                                         channel_id: ChannelId::new_zero(),
218                                                         counterparty: ChannelCounterparty {
219                                                                 node_id: *rnid,
220                                                                 features: channelmanager::provided_init_features(&UserConfig::default()),
221                                                                 unspendable_punishment_reserve: 0,
222                                                                 forwarding_info: None,
223                                                                 outbound_htlc_minimum_msat: None,
224                                                                 outbound_htlc_maximum_msat: None,
225                                                         },
226                                                         funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
227                                                         channel_type: None,
228                                                         short_channel_id: Some(scid),
229                                                         inbound_scid_alias: None,
230                                                         outbound_scid_alias: None,
231                                                         channel_value_satoshis: capacity,
232                                                         user_channel_id: 0, inbound_capacity_msat: 0,
233                                                         unspendable_punishment_reserve: None,
234                                                         confirmations_required: None,
235                                                         confirmations: None,
236                                                         force_close_spend_delay: None,
237                                                         is_outbound: true, is_channel_ready: true,
238                                                         is_usable: true, is_public: true,
239                                                         balance_msat: 0,
240                                                         outbound_capacity_msat: capacity.saturating_mul(1000),
241                                                         next_outbound_htlc_limit_msat: capacity.saturating_mul(1000),
242                                                         next_outbound_htlc_minimum_msat: 0,
243                                                         inbound_htlc_minimum_msat: None,
244                                                         inbound_htlc_maximum_msat: None,
245                                                         config: None,
246                                                         feerate_sat_per_1000_weight: None,
247                                                         channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
248                                                         pending_inbound_htlcs: Vec::new(),
249                                                         pending_outbound_htlcs: Vec::new(),
250                                                 });
251                                         }
252                                         Some(&$first_hops_vec[..])
253                                 },
254                         }
255                 }
256         }
257
258         macro_rules! last_hops {
259                 ($last_hops: expr) => {
260                         let count = get_slice!(1)[0];
261                         for _ in 0..count {
262                                 scid += 1;
263                                 let (rnid, _) =
264                                         node_pks.iter().skip(slice_to_be16(get_slice!(2)) as usize % node_pks.len()).next().unwrap();
265                                 $last_hops.push(RouteHint(vec![RouteHintHop {
266                                         src_node_id: *rnid,
267                                         short_channel_id: scid,
268                                         fees: RoutingFees {
269                                                 base_msat: slice_to_be32(get_slice!(4)),
270                                                 proportional_millionths: slice_to_be32(get_slice!(4)),
271                                         },
272                                         cltv_expiry_delta: slice_to_be16(get_slice!(2)),
273                                         htlc_minimum_msat: Some(slice_to_be64(get_slice!(8))),
274                                         htlc_maximum_msat: None,
275                                 }]));
276                         }
277                 }
278         }
279
280         macro_rules! find_routes {
281                 ($first_hops: expr, $node_pks: expr, $route_params: expr) => {
282                         let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &net_graph, &logger);
283                         let random_seed_bytes: [u8; 32] = [get_slice!(1)[0]; 32];
284                         for (target, ()) in $node_pks {
285                                 let final_value_msat = slice_to_be64(get_slice!(8));
286                                 let final_cltv_expiry_delta = slice_to_be32(get_slice!(4));
287                                 let route_params = $route_params(final_value_msat, final_cltv_expiry_delta, target);
288                                 let _ = find_route(&our_pubkey, &route_params, &net_graph,
289                                         $first_hops.map(|c| c.iter().collect::<Vec<_>>()).as_ref().map(|a| a.as_slice()),
290                                         &logger, &scorer, &ProbabilisticScoringFeeParameters::default(), &random_seed_bytes);
291                         }
292                 }
293         }
294
295         loop {
296                 match get_slice!(1)[0] {
297                         0 => {
298                                 let start_len = slice_to_be16(&get_slice_nonadvancing!(2)[0..2]) as usize;
299                                 let addr_len = slice_to_be16(&get_slice_nonadvancing!(start_len+2 + 74)[start_len+2 + 72..start_len+2 + 74]);
300                                 if addr_len > (37+1)*4 {
301                                         return;
302                                 }
303                                 let msg = decode_msg_with_len16!(msgs::UnsignedNodeAnnouncement, 288);
304                                 node_pks.insert(get_pubkey_from_node_id!(msg.node_id), ());
305                                 let _ = net_graph.update_node_from_unsigned_announcement(&msg);
306                         },
307                         1 => {
308                                 let msg = decode_msg_with_len16!(msgs::UnsignedChannelAnnouncement, 32+8+33*4);
309                                 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_1), ());
310                                 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_2), ());
311                                 let _ = net_graph.update_channel_from_unsigned_announcement::
312                                         <&FuzzChainSource<'_, '_, Out>>(&msg, &None);
313                         },
314                         2 => {
315                                 let msg = decode_msg_with_len16!(msgs::UnsignedChannelAnnouncement, 32+8+33*4);
316                                 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_1), ());
317                                 node_pks.insert(get_pubkey_from_node_id!(msg.node_id_2), ());
318                                 let _ = net_graph.update_channel_from_unsigned_announcement(&msg, &Some(&chain_source));
319                         },
320                         3 => {
321                                 let _ = net_graph.update_channel_unsigned(&decode_msg!(msgs::UnsignedChannelUpdate, 72));
322                         },
323                         4 => {
324                                 let short_channel_id = slice_to_be64(get_slice!(8));
325                                 net_graph.channel_failed_permanent(short_channel_id);
326                         },
327                         _ if node_pks.is_empty() => {},
328                         x if x < 250 => {
329                                 let mut first_hops_vec = Vec::new();
330                                 // Use macros here and in the blinded match arm to ensure values are fetched from the fuzz
331                                 // input in the same order, for better coverage.
332                                 let first_hops = first_hops!(first_hops_vec);
333                                 let mut last_hops = Vec::new();
334                                 last_hops!(last_hops);
335                                 find_routes!(first_hops, node_pks.iter(), |final_amt, final_delta, target: &PublicKey| {
336                                         RouteParameters::from_payment_params_and_value(
337                                                 PaymentParameters::from_node_id(*target, final_delta)
338                                                         .with_route_hints(last_hops.clone()).unwrap(),
339                                                 final_amt)
340                                 });
341                         },
342                         x => {
343                                 let mut first_hops_vec = Vec::new();
344                                 let first_hops = first_hops!(first_hops_vec);
345                                 let mut last_hops_unblinded = Vec::new();
346                                 last_hops!(last_hops_unblinded);
347                                 let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap();
348                                 let last_hops: Vec<(BlindedPayInfo, BlindedPath)> = last_hops_unblinded.into_iter().map(|hint| {
349                                         let hop = &hint.0[0];
350                                         let payinfo = BlindedPayInfo {
351                                                 fee_base_msat: hop.fees.base_msat,
352                                                 fee_proportional_millionths: hop.fees.proportional_millionths,
353                                                 htlc_minimum_msat: hop.htlc_minimum_msat.unwrap(),
354                                                 htlc_maximum_msat: hop.htlc_minimum_msat.unwrap().saturating_mul(100),
355                                                 cltv_expiry_delta: hop.cltv_expiry_delta,
356                                                 features: BlindedHopFeatures::empty(),
357                                         };
358                                         let num_blinded_hops = x % 250;
359                                         let mut blinded_hops = Vec::new();
360                                         for _ in 0..num_blinded_hops {
361                                                 blinded_hops.push(BlindedHop {
362                                                         blinded_node_id: dummy_pk,
363                                                         encrypted_payload: Vec::new()
364                                                 });
365                                         }
366                                         (payinfo, BlindedPath {
367                                                 introduction_node: IntroductionNode::NodeId(hop.src_node_id),
368                                                 blinding_point: dummy_pk,
369                                                 blinded_hops,
370                                         })
371                                 }).collect();
372                                 let mut features = Bolt12InvoiceFeatures::empty();
373                                 features.set_basic_mpp_optional();
374                                 find_routes!(first_hops, [(dummy_pk, ())].iter(), |final_amt, _, _| {
375                                         RouteParameters::from_payment_params_and_value(PaymentParameters::blinded(last_hops.clone())
376                                                 .with_bolt12_features(features.clone()).unwrap(),
377                                         final_amt)
378                                 });
379                         }
380                 }
381         }
382 }
383
384 pub fn router_test<Out: test_logger::Output>(data: &[u8], out: Out) {
385         do_test(data, out);
386 }
387
388 #[no_mangle]
389 pub extern "C" fn router_run(data: *const u8, datalen: usize) {
390         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
391 }