Skip to content

Commit f3d29e2

Browse files
committed
Manipulate an Onion packet for testing Bolt 4
1 parent 2258d2b commit f3d29e2

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

src/ln/channelmanager.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ impl ChannelManager {
949949
if msg.onion_routing_packet.version != 0 {
950950
//TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
951951
//sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
952-
//the hash doesn't really serve any purpuse - in the case of hashing all data, the
952+
//the hash doesn't really serve any purpose - in the case of hashing all data, the
953953
//receiving node would have to brute force to figure out which version was put in the
954954
//packet by the node that send us the message, in the case of hashing the hop_data, the
955955
//node knows the HMAC matched, so they already know what is there...
@@ -3222,7 +3222,7 @@ mod tests {
32223222
use util::logger::Logger;
32233223
use util::ser::{Writeable, Writer, ReadableArgs};
32243224
use util::config::UserConfig;
3225-
3225+
use util::rng;
32263226
use bitcoin::util::hash::{BitcoinHash, Sha256dHash};
32273227
use bitcoin::blockdata::block::{Block, BlockHeader};
32283228
use bitcoin::blockdata::transaction::{Transaction, TxOut};
@@ -7532,4 +7532,58 @@ mod tests {
75327532
assert_eq!(msg.channel_id, channel_id);
75337533
} else { panic!("Unexpected result"); }
75347534
}
7535+
7536+
#[test]
7537+
///Test that forwarding an onion packet with version other than 0 fails
7538+
fn test_bolt4_onion_packet_version_error() {
7539+
let nodes = create_network(3);
7540+
7541+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
7542+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
7543+
7544+
let expected_route = vec!(&nodes[1], &nodes[2]);
7545+
let value = 1000000;
7546+
7547+
let our_payment_preimage = route_payment(&nodes[0], &expected_route, value).0;
7548+
let route = nodes[0].router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), value, TEST_FINAL_CLTV).unwrap();
7549+
7550+
let secp_ctx = Secp256k1::new();
7551+
let session_priv = SecretKey::from_slice(&secp_ctx, &{
7552+
let mut session_key = [0; 32];
7553+
rng::fill_bytes(&mut session_key);
7554+
session_key
7555+
}).expect("RNG is bad!");
7556+
7557+
let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
7558+
7559+
let onion_keys = ChannelManager::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
7560+
7561+
let (onion_payloads, htlc_msat, htlc_cltv) = ChannelManager::build_onion_payloads(&route, cur_height).unwrap();
7562+
let mut onion_packet = ChannelManager::construct_onion_packet(onion_payloads, onion_keys, &our_payment_preimage);
7563+
7564+
onion_packet.version = 1;
7565+
7566+
let mut channel_state = nodes[0].node.channel_state.lock().unwrap();
7567+
let (update_add, commitment_signed, chan_monitor) = {
7568+
let chan = channel_state.by_id.get_mut(&chan_1.2).unwrap();
7569+
chan.send_htlc_and_commit(htlc_msat, our_payment_preimage.clone(), htlc_cltv, super::channel_held_info::HTLCSource::OutboundRoute {
7570+
route: route.clone(),
7571+
session_priv: session_priv.clone(),
7572+
first_hop_htlc_msat: htlc_msat,
7573+
}, onion_packet).unwrap().unwrap()//.map_err(|he| APIError::ChannelUnavailable{err: he.err}).unwrap()
7574+
};
7575+
7576+
println!("Mesg: {:?}", update_add.onion_routing_packet.version);
7577+
7578+
let res = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(),&update_add);
7579+
//This does create a PendingHTLCStatus::Fail situation that seems is maybe sent back to the node
7580+
//and is finally handled in channel::revoke_and_ack(...)
7581+
7582+
println!("Bolt4: {:?}", res);
7583+
7584+
7585+
assert!(false);
7586+
}
7587+
75357588
}
7589+

0 commit comments

Comments
 (0)