2020-08-10 15:00:09 -04:00
|
|
|
// 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.
|
|
|
|
|
2018-08-31 17:06:30 +00:00
|
|
|
use bitcoin::blockdata::script::{Script, Builder};
|
2020-06-16 15:10:17 -07:00
|
|
|
use bitcoin::blockdata::block::BlockHeader;
|
|
|
|
use bitcoin::blockdata::transaction::Transaction;
|
2020-04-27 17:53:13 +02:00
|
|
|
use bitcoin::hash_types::{Txid, BlockHash};
|
2018-08-30 01:37:37 +00:00
|
|
|
|
2019-11-18 16:40:05 -05:00
|
|
|
use lightning::chain::chaininterface::{ChainError,ChainWatchInterface};
|
2018-07-25 15:28:25 -04:00
|
|
|
use lightning::ln::channelmanager::ChannelDetails;
|
2019-12-28 01:10:14 -05:00
|
|
|
use lightning::ln::features::InitFeatures;
|
2018-07-25 15:28:25 -04:00
|
|
|
use lightning::ln::msgs;
|
2019-12-28 01:10:14 -05:00
|
|
|
use lightning::ln::msgs::RoutingMessageHandler;
|
2020-05-02 15:05:04 -04:00
|
|
|
use lightning::routing::router::{get_route, RouteHint};
|
2018-07-25 02:34:51 +00:00
|
|
|
use lightning::util::logger::Logger;
|
2018-09-14 17:50:48 -04:00
|
|
|
use lightning::util::ser::Readable;
|
2020-05-02 15:05:04 -04:00
|
|
|
use lightning::routing::network_graph::{NetGraphMsgHandler, RoutingFees};
|
2018-07-25 15:28:25 -04:00
|
|
|
|
2020-04-27 16:51:59 +02:00
|
|
|
use bitcoin::secp256k1::key::PublicKey;
|
2018-07-25 15:28:25 -04:00
|
|
|
|
2018-07-25 02:34:51 +00:00
|
|
|
use utils::test_logger;
|
|
|
|
|
2019-11-18 16:40:05 -05:00
|
|
|
use std::sync::Arc;
|
2018-08-31 17:06:30 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2018-07-25 02:34:51 +00:00
|
|
|
|
2018-07-25 15:28:25 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn slice_to_be16(v: &[u8]) -> u16 {
|
|
|
|
((v[0] as u16) << 8*1) |
|
|
|
|
((v[1] as u16) << 8*0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn slice_to_be32(v: &[u8]) -> u32 {
|
|
|
|
((v[0] as u32) << 8*3) |
|
|
|
|
((v[1] as u32) << 8*2) |
|
|
|
|
((v[2] as u32) << 8*1) |
|
|
|
|
((v[3] as u32) << 8*0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn slice_to_be64(v: &[u8]) -> u64 {
|
|
|
|
((v[0] as u64) << 8*7) |
|
|
|
|
((v[1] as u64) << 8*6) |
|
|
|
|
((v[2] as u64) << 8*5) |
|
|
|
|
((v[3] as u64) << 8*4) |
|
|
|
|
((v[4] as u64) << 8*3) |
|
|
|
|
((v[5] as u64) << 8*2) |
|
|
|
|
((v[6] as u64) << 8*1) |
|
|
|
|
((v[7] as u64) << 8*0)
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:06:30 +00:00
|
|
|
|
|
|
|
struct InputData {
|
|
|
|
data: Vec<u8>,
|
|
|
|
read_pos: AtomicUsize,
|
|
|
|
}
|
|
|
|
impl InputData {
|
|
|
|
fn get_slice(&self, len: usize) -> Option<&[u8]> {
|
|
|
|
let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
|
|
|
|
if self.data.len() < old_pos + len {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(&self.data[old_pos..old_pos + len])
|
|
|
|
}
|
2018-09-03 17:38:25 -04:00
|
|
|
fn get_slice_nonadvancing(&self, len: usize) -> Option<&[u8]> {
|
|
|
|
let old_pos = self.read_pos.load(Ordering::Acquire);
|
|
|
|
if self.data.len() < old_pos + len {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(&self.data[old_pos..old_pos + len])
|
|
|
|
}
|
2018-08-31 17:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DummyChainWatcher {
|
|
|
|
input: Arc<InputData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChainWatchInterface for DummyChainWatcher {
|
2020-04-27 17:53:13 +02:00
|
|
|
fn install_watch_tx(&self, _txid: &Txid, _script_pub_key: &Script) { }
|
|
|
|
fn install_watch_outpoint(&self, _outpoint: (Txid, u32), _out_script: &Script) { }
|
2018-09-03 17:38:38 -04:00
|
|
|
fn watch_all_txn(&self) { }
|
2020-06-16 15:10:17 -07:00
|
|
|
fn filter_block(&self, _header: &BlockHeader, _txdata: &[(usize, &Transaction)]) -> Vec<usize> {
|
2020-06-13 17:33:21 -04:00
|
|
|
Vec::new()
|
2019-11-18 16:40:05 -05:00
|
|
|
}
|
|
|
|
fn reentered(&self) -> usize { 0 }
|
2018-08-31 17:06:30 +00:00
|
|
|
|
2020-04-27 17:53:13 +02:00
|
|
|
fn get_chain_utxo(&self, _genesis_hash: BlockHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
|
2018-09-03 17:38:38 -04:00
|
|
|
match self.input.get_slice(2) {
|
|
|
|
Some(&[0, _]) => Err(ChainError::NotSupported),
|
|
|
|
Some(&[1, _]) => Err(ChainError::NotWatched),
|
|
|
|
Some(&[2, _]) => Err(ChainError::UnknownTx),
|
|
|
|
Some(&[_, x]) => Ok((Builder::new().push_int(x as i64).into_script().to_v0_p2wsh(), 0)),
|
2018-08-31 17:06:30 +00:00
|
|
|
None => Err(ChainError::UnknownTx),
|
2018-09-03 17:38:38 -04:00
|
|
|
_ => unreachable!(),
|
2018-08-31 17:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:28:25 -04:00
|
|
|
#[inline]
|
2020-02-20 20:11:40 -05:00
|
|
|
pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
|
2018-09-03 17:38:25 -04:00
|
|
|
let input = Arc::new(InputData {
|
|
|
|
data: data.to_vec(),
|
|
|
|
read_pos: AtomicUsize::new(0),
|
|
|
|
});
|
2018-07-25 15:28:25 -04:00
|
|
|
macro_rules! get_slice_nonadvancing {
|
|
|
|
($len: expr) => {
|
2018-09-03 17:38:25 -04:00
|
|
|
match input.get_slice_nonadvancing($len as usize) {
|
|
|
|
Some(slice) => slice,
|
|
|
|
None => return,
|
2018-07-25 15:28:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
macro_rules! get_slice {
|
|
|
|
($len: expr) => {
|
2018-09-03 17:38:25 -04:00
|
|
|
match input.get_slice($len as usize) {
|
|
|
|
Some(slice) => slice,
|
|
|
|
None => return,
|
2018-07-25 15:28:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! decode_msg {
|
2018-09-12 19:00:10 +09:00
|
|
|
($MsgType: path, $len: expr) => {{
|
2018-09-14 17:50:48 -04:00
|
|
|
let mut reader = ::std::io::Cursor::new(get_slice!($len));
|
2020-01-16 13:54:38 -05:00
|
|
|
match <$MsgType>::read(&mut reader) {
|
2018-07-25 15:28:25 -04:00
|
|
|
Ok(msg) => msg,
|
|
|
|
Err(e) => match e {
|
2018-09-19 13:06:35 -04:00
|
|
|
msgs::DecodeError::UnknownVersion => return,
|
2018-08-29 17:52:26 -04:00
|
|
|
msgs::DecodeError::UnknownRequiredFeature => return,
|
2018-09-19 13:06:35 -04:00
|
|
|
msgs::DecodeError::InvalidValue => return,
|
2018-07-25 15:28:25 -04:00
|
|
|
msgs::DecodeError::BadLengthDescriptor => return,
|
|
|
|
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
|
2018-09-12 19:00:10 +09:00
|
|
|
msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
|
2018-07-25 15:28:25 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 19:00:10 +09:00
|
|
|
}}
|
2018-07-25 15:28:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! decode_msg_with_len16 {
|
|
|
|
($MsgType: path, $begin_len: expr, $excess: expr) => {
|
|
|
|
{
|
|
|
|
let extra_len = slice_to_be16(&get_slice_nonadvancing!($begin_len as usize + 2)[$begin_len..$begin_len + 2]);
|
|
|
|
decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize) + $excess)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! get_pubkey {
|
|
|
|
() => {
|
2019-01-16 15:45:05 -05:00
|
|
|
match PublicKey::from_slice(get_slice!(33)) {
|
2018-07-25 15:28:25 -04:00
|
|
|
Ok(key) => key,
|
|
|
|
Err(_) => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:11:40 -05:00
|
|
|
let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
|
2018-08-31 17:06:30 +00:00
|
|
|
let chain_monitor = Arc::new(DummyChainWatcher {
|
2018-09-03 17:38:25 -04:00
|
|
|
input: Arc::clone(&input),
|
2018-08-31 17:06:30 +00:00
|
|
|
});
|
2018-07-25 02:34:51 +00:00
|
|
|
|
2018-07-25 15:28:25 -04:00
|
|
|
let our_pubkey = get_pubkey!();
|
2020-05-02 15:05:04 -04:00
|
|
|
let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger));
|
2018-07-25 15:28:25 -04:00
|
|
|
|
|
|
|
loop {
|
|
|
|
match get_slice!(1)[0] {
|
|
|
|
0 => {
|
|
|
|
let start_len = slice_to_be16(&get_slice_nonadvancing!(64 + 2)[64..64 + 2]) as usize;
|
|
|
|
let addr_len = slice_to_be16(&get_slice_nonadvancing!(64+start_len+2 + 74)[64+start_len+2 + 72..64+start_len+2 + 74]);
|
|
|
|
if addr_len > (37+1)*4 {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-02 15:05:04 -04:00
|
|
|
let _ = net_graph_msg_handler.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288));
|
2018-07-25 15:28:25 -04:00
|
|
|
},
|
|
|
|
1 => {
|
2020-05-02 15:05:04 -04:00
|
|
|
let _ = net_graph_msg_handler.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4));
|
2018-07-25 15:28:25 -04:00
|
|
|
},
|
|
|
|
2 => {
|
2020-06-28 14:43:10 +03:00
|
|
|
let _ = net_graph_msg_handler.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 136));
|
2018-07-25 15:28:25 -04:00
|
|
|
},
|
|
|
|
3 => {
|
|
|
|
match get_slice!(1)[0] {
|
|
|
|
0 => {
|
2020-06-28 14:43:10 +03:00
|
|
|
net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 136)});
|
2018-07-25 15:28:25 -04:00
|
|
|
},
|
|
|
|
1 => {
|
|
|
|
let short_channel_id = slice_to_be64(get_slice!(8));
|
2020-05-02 15:05:04 -04:00
|
|
|
net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id, is_permanent: false});
|
2018-07-25 15:28:25 -04:00
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
4 => {
|
|
|
|
let target = get_pubkey!();
|
|
|
|
let mut first_hops_vec = Vec::new();
|
|
|
|
let first_hops = match get_slice!(1)[0] {
|
|
|
|
0 => None,
|
|
|
|
1 => {
|
|
|
|
let count = slice_to_be16(get_slice!(2));
|
|
|
|
for _ in 0..count {
|
|
|
|
first_hops_vec.push(ChannelDetails {
|
|
|
|
channel_id: [0; 32],
|
|
|
|
short_channel_id: Some(slice_to_be64(get_slice!(8))),
|
|
|
|
remote_network_id: get_pubkey!(),
|
2019-12-28 01:10:14 -05:00
|
|
|
counterparty_features: InitFeatures::empty(),
|
2018-07-25 15:28:25 -04:00
|
|
|
channel_value_satoshis: slice_to_be64(get_slice!(8)),
|
|
|
|
user_id: 0,
|
2019-06-01 12:11:27 -04:00
|
|
|
inbound_capacity_msat: 0,
|
|
|
|
is_live: true,
|
|
|
|
outbound_capacity_msat: 0,
|
2018-07-25 15:28:25 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Some(&first_hops_vec[..])
|
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
let mut last_hops_vec = Vec::new();
|
|
|
|
let last_hops = {
|
|
|
|
let count = slice_to_be16(get_slice!(2));
|
|
|
|
for _ in 0..count {
|
|
|
|
last_hops_vec.push(RouteHint {
|
|
|
|
src_node_id: get_pubkey!(),
|
|
|
|
short_channel_id: slice_to_be64(get_slice!(8)),
|
2020-05-02 15:05:04 -04:00
|
|
|
fees: RoutingFees {
|
|
|
|
base_msat: slice_to_be32(get_slice!(4)),
|
|
|
|
proportional_millionths: slice_to_be32(get_slice!(4)),
|
2020-05-11 21:12:23 -04:00
|
|
|
},
|
|
|
|
cltv_expiry_delta: slice_to_be16(get_slice!(2)),
|
2018-07-25 15:28:25 -04:00
|
|
|
htlc_minimum_msat: slice_to_be64(get_slice!(8)),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
&last_hops_vec[..]
|
|
|
|
};
|
2020-07-23 16:10:29 -04:00
|
|
|
let _ = get_route(&our_pubkey, &net_graph_msg_handler.network_graph.read().unwrap(), &target,
|
|
|
|
first_hops.map(|c| c.iter().collect::<Vec<_>>()).as_ref().map(|a| a.as_slice()),
|
|
|
|
&last_hops.iter().collect::<Vec<_>>(),
|
|
|
|
slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4)), Arc::clone(&logger));
|
2018-07-25 15:28:25 -04:00
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:11:40 -05:00
|
|
|
pub fn router_test<Out: test_logger::Output>(data: &[u8], out: Out) {
|
|
|
|
do_test(data, out);
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:18:43 -05:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn router_run(data: *const u8, datalen: usize) {
|
2020-02-20 20:11:40 -05:00
|
|
|
do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
|
2018-07-25 15:28:25 -04:00
|
|
|
}
|