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