X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fonion_utils.rs;h=8feef1697af40ad33032afe23b7bf5f77e8a6b21;hb=021959f822222cd3782a212f0b7ad3819ee56349;hp=ef0d6d2e8a0b6b5617615438fa11f055e45558d7;hpb=5260e81033c6008fdcef1cbf973243268f4ca373;p=rust-lightning diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index ef0d6d2e..8feef169 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -1,24 +1,33 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + use ln::channelmanager::{PaymentHash, PaymentSecret, HTLCSource}; use ln::msgs; -use ln::router::RouteHop; +use routing::router::RouteHop; use util::byte_utils; use util::chacha20::ChaCha20; use util::errors::{self, APIError}; use util::ser::{Readable, Writeable, LengthCalculatingWriter}; -use util::logger::{Logger, LogHolder}; +use util::logger::Logger; -use bitcoin_hashes::{Hash, HashEngine}; -use bitcoin_hashes::cmp::fixed_time_eq; -use bitcoin_hashes::hmac::{Hmac, HmacEngine}; -use bitcoin_hashes::sha256::Hash as Sha256; +use bitcoin::hashes::{Hash, HashEngine}; +use bitcoin::hashes::cmp::fixed_time_eq; +use bitcoin::hashes::hmac::{Hmac, HmacEngine}; +use bitcoin::hashes::sha256::Hash as Sha256; -use secp256k1::key::{SecretKey,PublicKey}; -use secp256k1::Secp256k1; -use secp256k1::ecdh::SharedSecret; -use secp256k1; +use bitcoin::secp256k1::key::{SecretKey,PublicKey}; +use bitcoin::secp256k1::Secp256k1; +use bitcoin::secp256k1::ecdh::SharedSecret; +use bitcoin::secp256k1; use std::io::Cursor; -use std::sync::Arc; +use std::ops::Deref; pub(super) struct OnionKeys { #[cfg(test)] @@ -108,7 +117,7 @@ pub(super) fn construct_onion_keys(secp_ctx: &Secp256k1, payment_secret_option: &Option, starting_htlc_offset: u32) -> Result<(Vec, u64, u32), APIError> { +pub(super) fn build_onion_payloads(path: &Vec, total_msat: u64, payment_secret_option: &Option, starting_htlc_offset: u32) -> Result<(Vec, u64, u32), APIError> { let mut cur_value_msat = 0u64; let mut cur_cltv = starting_htlc_offset; let mut last_short_channel_id = 0; @@ -127,7 +136,7 @@ pub(super) fn build_onion_payloads(path: &Vec, payment_secret_option: payment_data: if let &Some(ref payment_secret) = payment_secret_option { Some(msgs::FinalOnionHopData { payment_secret: payment_secret.clone(), - total_msat: hop.fee_msat, + total_msat, }) } else { None }, } @@ -146,11 +155,11 @@ pub(super) fn build_onion_payloads(path: &Vec, payment_secret_option: }); cur_value_msat += hop.fee_msat; if cur_value_msat >= 21000000 * 100000000 * 1000 { - return Err(APIError::RouteError{err: "Channel fees overflowed?!"}); + return Err(APIError::RouteError{err: "Channel fees overflowed?"}); } cur_cltv += hop.cltv_expiry_delta as u32; if cur_cltv >= 500000000 { - return Err(APIError::RouteError{err: "Channel CLTV overflowed?!"}); + return Err(APIError::RouteError{err: "Channel CLTV overflowed?"}); } last_short_channel_id = hop.short_channel_id; } @@ -297,8 +306,8 @@ pub(super) fn build_failure_packet(shared_secret: &[u8], failure_type: u16, fail }; let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], - failuremsg: failuremsg, - pad: pad, + failuremsg, + pad, }; let mut hmac = HmacEngine::::new(&um); @@ -317,11 +326,13 @@ pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type: /// Process failure we got back from upstream on a payment we sent (implying htlc_source is an /// OutboundRoute). /// Returns update, a boolean indicating that the payment itself failed, and the error code. -pub(super) fn process_onion_failure(secp_ctx: &Secp256k1, logger: &Arc, htlc_source: &HTLCSource, mut packet_decrypted: Vec) -> (Option, bool, Option) { +#[inline] +pub(super) fn process_onion_failure(secp_ctx: &Secp256k1, logger: &L, htlc_source: &HTLCSource, mut packet_decrypted: Vec) -> (Option, bool, Option, Option>) where L::Target: Logger { if let &HTLCSource::OutboundRoute { ref path, ref session_priv, ref first_hop_htlc_msat } = htlc_source { let mut res = None; let mut htlc_msat = *first_hop_htlc_msat; let mut error_code_ret = None; + let mut error_packet_ret = None; let mut next_route_hop_ix = 0; let mut is_from_final_node = false; @@ -356,6 +367,7 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< let error_code = byte_utils::slice_to_be16(&error_code_slice); error_code_ret = Some(error_code); + error_packet_ret = Some(err_packet.failuremsg[2..].to_vec()); let (debug_field, debug_field_size) = errors::get_onion_debug_field(error_code); @@ -437,12 +449,10 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< let (description, title) = errors::get_onion_error_description(error_code); if debug_field_size > 0 && err_packet.failuremsg.len() >= 4 + debug_field_size { - let log_holder = LogHolder { logger }; - log_warn!(log_holder, "Onion Error[{}({:#x}) {}({})] {}", title, error_code, debug_field, log_bytes!(&err_packet.failuremsg[4..4+debug_field_size]), description); + log_warn!(logger, "Onion Error[from {}: {}({:#x}) {}({})] {}", route_hop.pubkey, title, error_code, debug_field, log_bytes!(&err_packet.failuremsg[4..4+debug_field_size]), description); } else { - let log_holder = LogHolder { logger }; - log_warn!(log_holder, "Onion Error[{}({:#x})] {}", title, error_code, description); + log_warn!(logger, "Onion Error[from {}: {}({:#x})] {}", route_hop.pubkey, title, error_code, description); } } else { // Useless packet that we can't use but it passed HMAC, so it @@ -456,11 +466,11 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< } }).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?"); if let Some((channel_update, payment_retryable)) = res { - (channel_update, payment_retryable, error_code_ret) + (channel_update, payment_retryable, error_code_ret, error_packet_ret) } else { // only not set either packet unparseable or hmac does not match with any // payment not retryable only when garbage is from the final node - (None, !is_from_final_node, None) + (None, !is_from_final_node, None, None) } } else { unreachable!(); } } @@ -469,14 +479,14 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< mod tests { use ln::channelmanager::PaymentHash; use ln::features::{ChannelFeatures, NodeFeatures}; - use ln::router::{Route, RouteHop}; + use routing::router::{Route, RouteHop}; use ln::msgs; use util::ser::{Writeable, Writer}; use hex; - use secp256k1::Secp256k1; - use secp256k1::key::{PublicKey,SecretKey}; + use bitcoin::secp256k1::Secp256k1; + use bitcoin::secp256k1::key::{PublicKey,SecretKey}; use super::OnionKeys;