From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Wed, 25 Aug 2021 17:22:20 +0000 (+0000) Subject: Merge pull request #1031 from p2pderivatives/dlc-version-generic X-Git-Tag: v0.0.101~30 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=45853b3a956f83569dff1010a43a33c57487698a;hp=eb46f477f4b2459731e78c23423415cde278be09;p=rust-lightning Merge pull request #1031 from p2pderivatives/dlc-version-generic Dlc version generic --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 7284355c..34cddd1a 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -51,7 +51,13 @@ const FRESHNESS_TIMER: u64 = 60; #[cfg(test)] const FRESHNESS_TIMER: u64 = 1; +#[cfg(not(debug_assertions))] const PING_TIMER: u64 = 5; +/// Signature operations take a lot longer without compiler optimisations. +/// Increasing the ping timer allows for this but slower devices will be disconnected if the +/// timeout is reached. +#[cfg(debug_assertions)] +const PING_TIMER: u64 = 30; /// Trait which handles persisting a [`ChannelManager`] to disk. /// diff --git a/lightning-block-sync/src/convert.rs b/lightning-block-sync/src/convert.rs index 37b2c432..e8e1427b 100644 --- a/lightning-block-sync/src/convert.rs +++ b/lightning-block-sync/src/convert.rs @@ -4,7 +4,7 @@ use crate::utils::hex_to_uint256; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::consensus::encode; -use bitcoin::hash_types::{BlockHash, TxMerkleNode}; +use bitcoin::hash_types::{BlockHash, TxMerkleNode, Txid}; use bitcoin::hashes::hex::{ToHex, FromHex}; use serde::Deserialize; @@ -156,11 +156,37 @@ impl TryInto<(BlockHash, Option)> for JsonResponse { } } +impl TryInto for JsonResponse { + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + match self.0.as_str() { + None => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "expected JSON string", + )), + Some(hex_data) => match Vec::::from_hex(hex_data) { + Err(_) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "invalid hex data", + )), + Ok(txid_data) => match encode::deserialize(&txid_data) { + Err(_) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "invalid txid", + )), + Ok(txid) => Ok(txid), + }, + }, + } + } +} + #[cfg(test)] pub(crate) mod tests { use super::*; use bitcoin::blockdata::constants::genesis_block; use bitcoin::consensus::encode; + use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; /// Converts from `BlockHeaderData` into a `GetHeaderResponse` JSON value. @@ -469,4 +495,50 @@ pub(crate) mod tests { }, } } + + #[test] + fn into_txid_from_json_response_with_unexpected_type() { + let response = JsonResponse(serde_json::json!({ "result": "foo" })); + match TryInto::::try_into(response) { + Err(e) => { + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string"); + } + Ok(_) => panic!("Expected error"), + } + } + + #[test] + fn into_txid_from_json_response_with_invalid_hex_data() { + let response = JsonResponse(serde_json::json!("foobar")); + match TryInto::::try_into(response) { + Err(e) => { + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert_eq!(e.get_ref().unwrap().to_string(), "invalid hex data"); + } + Ok(_) => panic!("Expected error"), + } + } + + #[test] + fn into_txid_from_json_response_with_invalid_txid_data() { + let response = JsonResponse(serde_json::json!("abcd")); + match TryInto::::try_into(response) { + Err(e) => { + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert_eq!(e.get_ref().unwrap().to_string(), "invalid txid"); + } + Ok(_) => panic!("Expected error"), + } + } + + #[test] + fn into_txid_from_json_response_with_valid_txid_data() { + let target_txid = Txid::from_slice(&[1; 32]).unwrap(); + let response = JsonResponse(serde_json::json!(encode::serialize_hex(&target_txid))); + match TryInto::::try_into(response) { + Err(e) => panic!("Unexpected error: {:?}", e), + Ok(txid) => assert_eq!(txid, target_txid), + } + } } diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 980c6ea4..13289395 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -4048,7 +4048,7 @@ fn test_invalid_channel_announcement() { bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key }, bitcoin_key_2: if were_node_one { bs_bitcoin_key } else { as_bitcoin_key }, excess_data: Vec::new(), - }; + } } } diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 2a724c74..34bcda3a 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -784,7 +784,7 @@ impl P }, } } - }; + } } } diff --git a/lightning/src/util/macro_logger.rs b/lightning/src/util/macro_logger.rs index 3ac294fb..9644411b 100644 --- a/lightning/src/util/macro_logger.rs +++ b/lightning/src/util/macro_logger.rs @@ -160,7 +160,7 @@ macro_rules! log_spendable { #[macro_export] macro_rules! log_internal { ($logger: expr, $lvl:expr, $($arg:tt)+) => ( - $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())); + $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())) ); }