Merge pull request #1031 from p2pderivatives/dlc-version-generic
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 25 Aug 2021 17:22:20 +0000 (17:22 +0000)
committerGitHub <noreply@github.com>
Wed, 25 Aug 2021 17:22:20 +0000 (17:22 +0000)
Dlc version generic

lightning-background-processor/src/lib.rs
lightning-block-sync/src/convert.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/peer_handler.rs
lightning/src/util/macro_logger.rs

index 7284355c905badcd5beb981b77a1ed8cc68fd2ba..34cddd1a6aa8b83ea36699e0bb39c4a21b97480a 100644 (file)
@@ -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.
 ///
index 37b2c4323972fb1152fb27b1ee2c195859806104..e8e1427bdb654ffa432d069faf37457fc5a74cea 100644 (file)
@@ -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<u32>)> for JsonResponse {
        }
 }
 
+impl TryInto<Txid> for JsonResponse {
+       type Error = std::io::Error;
+       fn try_into(self) -> std::io::Result<Txid> {
+               match self.0.as_str() {
+                       None => Err(std::io::Error::new(
+                               std::io::ErrorKind::InvalidData,
+                               "expected JSON string",
+                       )),
+                       Some(hex_data) => match Vec::<u8>::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::<Txid>::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::<Txid>::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::<Txid>::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::<Txid>::try_into(response) {
+                       Err(e) => panic!("Unexpected error: {:?}", e),
+                       Ok(txid) => assert_eq!(txid, target_txid),
+               }
+       }
 }
index 980c6ea40f1f0e812d99fbeca432421cf9cc5744..1328939570eb9204b31153597841fbda151f2ef3 100644 (file)
@@ -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(),
-                       };
+                       }
                }
        }
 
index 2a724c74ad9550030d44fc275523521b5652f6c8..34bcda3a009a850b780bfffd6d4c087eb0256b28 100644 (file)
@@ -784,7 +784,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
                                                                                                },
                                                                                        }
                                                                                }
-                                                                       };
+                                                                       }
                                                                }
                                                        }
 
index 3ac294fbff9097686d4d28df669bcab76c402f5e..9644411bc147310b51a506e9a6634adcdb98bfc3 100644 (file)
@@ -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!()))
        );
 }