From: Arik Sosman Date: Fri, 16 Aug 2024 15:02:50 +0000 (-0700) Subject: Upgrade rust-bitcoin to 0.32.2. X-Git-Tag: v0.0.124-beta~9^2~7 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=176d2ad599d04f31c1acdd7cbee8e2510721408d;p=rust-lightning Upgrade rust-bitcoin to 0.32.2. --- diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 002cc54a9..666474f53 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -22,7 +22,7 @@ lightning = { path = "../lightning", features = ["regex", "hashbrown", "_test_ut lightning-invoice = { path = "../lightning-invoice" } lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync" } bech32 = "0.9.1" -bitcoin = { version = "0.31.2", features = ["secp-lowmemory"] } +bitcoin = { version = "0.32.2", features = ["secp-lowmemory"] } afl = { version = "0.12", optional = true } honggfuzz = { version = "0.5", optional = true, default-features = false } diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 4322fccb1..1cdd3ff12 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -81,8 +81,8 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature}; use bitcoin::secp256k1::schnorr; use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey}; +use lightning::io::Cursor; use std::cmp::{self, Ordering}; -use std::io::Cursor; use std::mem; use std::sync::atomic; use std::sync::{Arc, Mutex}; @@ -154,7 +154,7 @@ impl BroadcasterInterface for TestBroadcaster { pub struct VecWriter(pub Vec); impl Writer for VecWriter { - fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::lightning::io::Error> { self.0.extend_from_slice(buf); Ok(()) } @@ -394,7 +394,7 @@ impl SignerProvider for KeyProvider { } fn read_chan_signer(&self, buffer: &[u8]) -> Result { - let mut reader = std::io::Cursor::new(buffer); + let mut reader = lightning::io::Cursor::new(buffer); let inner: InMemorySigner = ReadableArgs::read(&mut reader, self)?; let state = self.make_enforcement_state_cell(inner.commitment_seed); diff --git a/fuzz/src/chanmon_deser.rs b/fuzz/src/chanmon_deser.rs index b9c012137..4a4e79c83 100644 --- a/fuzz/src/chanmon_deser.rs +++ b/fuzz/src/chanmon_deser.rs @@ -10,11 +10,11 @@ use lightning::util::test_utils::OnlyReadsKeysInterface; use crate::utils::test_logger; -use std::io::Cursor; +use lightning::io::Cursor; struct VecWriter(Vec); impl Writer for VecWriter { - fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::lightning::io::Error> { self.0.extend_from_slice(buf); Ok(()) } diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index fa0c29062..97cfe631d 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -121,8 +121,8 @@ impl InputData { Some(&self.data[old_pos..old_pos + len]) } } -impl std::io::Read for &InputData { - fn read(&mut self, buf: &mut [u8]) -> std::io::Result { +impl lightning::io::Read for &InputData { + fn read(&mut self, buf: &mut [u8]) -> lightning::io::Result { if let Some(sl) = self.get_slice(buf.len()) { buf.copy_from_slice(sl); Ok(buf.len()) diff --git a/fuzz/src/msg_targets/utils.rs b/fuzz/src/msg_targets/utils.rs index 5fa474cbf..8bf776193 100644 --- a/fuzz/src/msg_targets/utils.rs +++ b/fuzz/src/msg_targets/utils.rs @@ -12,7 +12,7 @@ use lightning::util::ser::Writer; pub struct VecWriter(pub Vec); impl Writer for VecWriter { - fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::lightning::io::Error> { self.0.extend_from_slice(buf); Ok(()) } @@ -31,7 +31,7 @@ impl Writer for VecWriter { macro_rules! test_msg { ($MsgType: path, $data: ident) => {{ use lightning::util::ser::{Readable, Writeable}; - let mut r = ::std::io::Cursor::new($data); + let mut r = ::lightning::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable>::read(&mut r) { let p = r.position() as usize; let mut w = VecWriter(Vec::new()); @@ -50,13 +50,14 @@ macro_rules! test_msg { macro_rules! test_msg_simple { ($MsgType: path, $data: ident) => {{ use lightning::util::ser::{Readable, Writeable}; - let mut r = ::std::io::Cursor::new($data); + let mut r = ::lightning::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable>::read(&mut r) { let mut w = VecWriter(Vec::new()); msg.write(&mut w).unwrap(); assert_eq!(msg.serialized_length(), w.0.len()); - let msg = <$MsgType as Readable>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap(); + let msg = + <$MsgType as Readable>::read(&mut ::lightning::io::Cursor::new(&w.0)).unwrap(); let mut w_two = VecWriter(Vec::new()); msg.write(&mut w_two).unwrap(); assert_eq!(&w.0[..], &w_two.0[..]); @@ -70,7 +71,7 @@ macro_rules! test_msg_simple { macro_rules! test_msg_exact { ($MsgType: path, $data: ident) => {{ use lightning::util::ser::{Readable, Writeable}; - let mut r = ::std::io::Cursor::new($data); + let mut r = ::lightning::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable>::read(&mut r) { let mut w = VecWriter(Vec::new()); msg.write(&mut w).unwrap(); @@ -86,7 +87,7 @@ macro_rules! test_msg_exact { macro_rules! test_msg_hole { ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {{ use lightning::util::ser::{Readable, Writeable}; - let mut r = ::std::io::Cursor::new($data); + let mut r = ::lightning::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable>::read(&mut r) { let mut w = VecWriter(Vec::new()); msg.write(&mut w).unwrap(); diff --git a/fuzz/src/onion_hop_data.rs b/fuzz/src/onion_hop_data.rs index 36aebb4e1..dd972501d 100644 --- a/fuzz/src/onion_hop_data.rs +++ b/fuzz/src/onion_hop_data.rs @@ -17,7 +17,7 @@ use lightning::util::test_utils; pub fn onion_hop_data_test(data: &[u8], _out: Out) { use bitcoin::secp256k1::PublicKey; use lightning::util::ser::ReadableArgs; - let mut r = ::std::io::Cursor::new(data); + let mut r = ::lightning::io::Cursor::new(data); let node_signer = test_utils::TestNodeSigner::new(test_utils::privkey(42)); let _ = , @@ -30,7 +30,7 @@ pub extern "C" fn onion_hop_data_run(data: *const u8, datalen: usize) { use bitcoin::secp256k1::PublicKey; use lightning::util::ser::ReadableArgs; let data = unsafe { std::slice::from_raw_parts(data, datalen) }; - let mut r = ::std::io::Cursor::new(data); + let mut r = ::lightning::io::Cursor::new(data); let node_signer = test_utils::TestNodeSigner::new(test_utils::privkey(42)); let _ = , diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 48f184af7..536204edf 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -30,7 +30,7 @@ use lightning_invoice::RawBolt11Invoice; use crate::utils::test_logger; -use std::io::{self, Cursor}; +use lightning::io::{self, Cursor}; use std::sync::atomic::{AtomicU64, Ordering}; #[inline] @@ -168,7 +168,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler { &self, _message_type: u64, buffer: &mut R, ) -> Result, msgs::DecodeError> { let mut buf = Vec::new(); - buffer.read_to_end(&mut buf)?; + buffer.read_to_limit(&mut buf, u64::MAX)?; return Ok(Some(TestCustomMessage {})); } fn release_pending_custom_messages(&self) -> Vec> { @@ -178,7 +178,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler { pub struct VecWriter(pub Vec); impl Writer for VecWriter { - fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::lightning::io::Error> { self.0.extend_from_slice(buf); Ok(()) } diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index 96046f258..ea0476c62 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -146,7 +146,7 @@ pub fn do_test(data: &[u8], out: Out) { macro_rules! decode_msg { ($MsgType: path, $len: expr) => {{ - let mut reader = ::std::io::Cursor::new(get_slice!($len)); + let mut reader = ::lightning::io::Cursor::new(get_slice!($len)); match <$MsgType>::read(&mut reader) { Ok(msg) => { assert_eq!(reader.position(), $len as u64); diff --git a/lightning-background-processor/Cargo.toml b/lightning-background-processor/Cargo.toml index df4b2c827..c780cc3d6 100644 --- a/lightning-background-processor/Cargo.toml +++ b/lightning-background-processor/Cargo.toml @@ -16,12 +16,12 @@ rustdoc-args = ["--cfg", "docsrs"] [features] futures = [ ] std = ["bitcoin/std", "lightning/std", "lightning-rapid-gossip-sync/std"] -no-std = ["bitcoin/no-std", "lightning/no-std", "lightning-rapid-gossip-sync/no-std"] +no-std = ["lightning/no-std", "lightning-rapid-gossip-sync/no-std"] default = ["std"] [dependencies] -bitcoin = { version = "0.31.2", default-features = false } +bitcoin = { version = "0.32.2", default-features = false } lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false } lightning-rapid-gossip-sync = { version = "0.0.123-beta", path = "../lightning-rapid-gossip-sync", default-features = false } diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index e39ae5a7c..3b01fb595 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -1326,7 +1326,7 @@ mod tests { && key == CHANNEL_MANAGER_PERSISTENCE_KEY { if let Some((error, message)) = self.manager_error { - return Err(std::io::Error::new(error, message)); + return Err(std::io::Error::new(error, message).into()); } } @@ -1344,7 +1344,7 @@ mod tests { }; if let Some((error, message)) = self.graph_error { - return Err(std::io::Error::new(error, message)); + return Err(std::io::Error::new(error, message).into()); } } @@ -1353,7 +1353,7 @@ mod tests { && key == SCORER_PERSISTENCE_KEY { if let Some((error, message)) = self.scorer_error { - return Err(std::io::Error::new(error, message)); + return Err(std::io::Error::new(error, message).into()); } } @@ -2002,7 +2002,7 @@ mod tests { match bp_future.await { Ok(_) => panic!("Expected error persisting manager"), Err(e) => { - assert_eq!(e.kind(), std::io::ErrorKind::Other); + assert_eq!(e.kind(), lightning::io::ErrorKind::Other); assert_eq!(e.get_ref().unwrap().to_string(), "test"); }, } diff --git a/lightning-block-sync/Cargo.toml b/lightning-block-sync/Cargo.toml index c3a230967..ecae20d80 100644 --- a/lightning-block-sync/Cargo.toml +++ b/lightning-block-sync/Cargo.toml @@ -18,7 +18,7 @@ rest-client = [ "serde_json", "chunked_transfer" ] rpc-client = [ "serde_json", "chunked_transfer" ] [dependencies] -bitcoin = "0.31.2" +bitcoin = "0.32.2" lightning = { version = "0.0.123-beta", path = "../lightning" } tokio = { version = "1.35", features = [ "io-util", "net", "time", "rt" ], optional = true } serde_json = { version = "1.0", optional = true } diff --git a/lightning-block-sync/src/convert.rs b/lightning-block-sync/src/convert.rs index ea01039f2..a31b329a5 100644 --- a/lightning-block-sync/src/convert.rs +++ b/lightning-block-sync/src/convert.rs @@ -622,10 +622,7 @@ pub(crate) mod tests { match TryInto::::try_into(response) { Err(e) => { assert_eq!(e.kind(), io::ErrorKind::InvalidData); - assert_eq!( - e.get_ref().unwrap().to_string(), - "bad hex string length 6 (expected 64)" - ); + assert_eq!(e.get_ref().unwrap().to_string(), "failed to parse hex"); }, Ok(_) => panic!("Expected error"), } @@ -637,10 +634,7 @@ pub(crate) mod tests { match TryInto::::try_into(response) { Err(e) => { assert_eq!(e.kind(), io::ErrorKind::InvalidData); - assert_eq!( - e.get_ref().unwrap().to_string(), - "bad hex string length 4 (expected 64)" - ); + assert_eq!(e.get_ref().unwrap().to_string(), "failed to parse hex"); }, Ok(_) => panic!("Expected error"), } diff --git a/lightning-block-sync/src/init.rs b/lightning-block-sync/src/init.rs index cd414f55d..623f54042 100644 --- a/lightning-block-sync/src/init.rs +++ b/lightning-block-sync/src/init.rs @@ -59,7 +59,7 @@ where /// /// use lightning_block_sync::*; /// -/// use std::io::Cursor; +/// use lightning::io::Cursor; /// /// async fn init_sync< /// B: BlockSource, diff --git a/lightning-block-sync/src/utils.rs b/lightning-block-sync/src/utils.rs index 494a1e53f..44027734e 100644 --- a/lightning-block-sync/src/utils.rs +++ b/lightning-block-sync/src/utils.rs @@ -9,7 +9,6 @@ pub fn hex_to_work(hex: &str) -> Result { #[cfg(test)] mod tests { use super::*; - use bitcoin::hex::HexToBytesError; use bitcoin::pow::Work; #[test] @@ -20,31 +19,25 @@ mod tests { #[test] fn hex_to_work_too_short_str() { let hex = String::from_utf8(vec![b'0'; 32]).unwrap(); - assert_eq!(hex_to_work(&hex), Err(HexToArrayError::InvalidLength(32, 64))); + assert!(hex_to_work(&hex).is_err()); } #[test] fn hex_to_work_too_long_str() { let hex = String::from_utf8(vec![b'0'; 128]).unwrap(); - assert_eq!(hex_to_work(&hex), Err(HexToArrayError::InvalidLength(128, 64))); + assert!(hex_to_work(&hex).is_err()); } #[test] fn hex_to_work_odd_length_str() { let hex = String::from_utf8(vec![b'0'; 65]).unwrap(); - assert_eq!( - hex_to_work(&hex), - Err(HexToArrayError::Conversion(HexToBytesError::OddLengthString(65))) - ); + assert!(hex_to_work(&hex).is_err()); } #[test] fn hex_to_work_invalid_char() { let hex = String::from_utf8(vec![b'G'; 64]).unwrap(); - assert_eq!( - hex_to_work(&hex), - Err(HexToArrayError::Conversion(HexToBytesError::InvalidChar(b'G'))) - ); + assert!(hex_to_work(&hex).is_err()); } #[test] diff --git a/lightning-custom-message/Cargo.toml b/lightning-custom-message/Cargo.toml index f74ead81c..d3c2e5708 100644 --- a/lightning-custom-message/Cargo.toml +++ b/lightning-custom-message/Cargo.toml @@ -14,7 +14,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] -bitcoin = "0.31.2" +bitcoin = "0.32.2" lightning = { version = "0.0.123-beta", path = "../lightning" } [lints] diff --git a/lightning-invoice/Cargo.toml b/lightning-invoice/Cargo.toml index f73321296..59f411409 100644 --- a/lightning-invoice/Cargo.toml +++ b/lightning-invoice/Cargo.toml @@ -16,15 +16,15 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["std"] -no-std = ["bitcoin/no-std"] -std = ["bitcoin/std", "bech32/std"] +no-std = [] +std = ["bech32/std"] [dependencies] bech32 = { version = "0.9.1", default-features = false } lightning-types = { version = "0.1", path = "../lightning-types", default-features = false } -secp256k1 = { version = "0.28.0", default-features = false, features = ["recovery", "alloc"] } +secp256k1 = { version = "0.29.0", default-features = false, features = ["recovery", "alloc"] } serde = { version = "1.0.118", optional = true } -bitcoin = { version = "0.31.2", default-features = false } +bitcoin = { version = "0.32.2", default-features = false } [dev-dependencies] serde_json = { version = "1"} diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 56ca21a9b..fea0b6850 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -39,7 +39,6 @@ use std::time::SystemTime; use bech32::{FromBase32, u5}; use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion}; -use bitcoin::address::Payload; use bitcoin::hashes::{Hash, sha256}; use lightning_types::features::Bolt11InvoiceFeatures; @@ -1434,22 +1433,22 @@ impl Bolt11Invoice { /// Returns a list of all fallback addresses as [`Address`]es pub fn fallback_addresses(&self) -> Vec
{ self.fallbacks().iter().filter_map(|fallback| { - let payload = match fallback { + let address = match fallback { Fallback::SegWitProgram { version, program } => { - match WitnessProgram::new(*version, program.clone()) { - Ok(witness_program) => Payload::WitnessProgram(witness_program), + match WitnessProgram::new(*version, &program) { + Ok(witness_program) => Address::from_witness_program(witness_program, self.network()), Err(_) => return None, } } Fallback::PubKeyHash(pkh) => { - Payload::PubkeyHash(*pkh) + Address::p2pkh(*pkh, self.network()) } Fallback::ScriptHash(sh) => { - Payload::ScriptHash(*sh) + Address::p2sh_from_hash(*sh, self.network()) } }; - Some(Address::new(self.network(), payload)) + Some(address) }).collect() } diff --git a/lightning-net-tokio/Cargo.toml b/lightning-net-tokio/Cargo.toml index c56553711..ede8cc0a7 100644 --- a/lightning-net-tokio/Cargo.toml +++ b/lightning-net-tokio/Cargo.toml @@ -15,7 +15,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] -bitcoin = "0.31.2" +bitcoin = "0.32.2" lightning = { version = "0.0.123-beta", path = "../lightning" } tokio = { version = "1.35", features = [ "rt", "sync", "net", "time" ] } diff --git a/lightning-persister/Cargo.toml b/lightning-persister/Cargo.toml index 5f1e27740..e315fb791 100644 --- a/lightning-persister/Cargo.toml +++ b/lightning-persister/Cargo.toml @@ -14,7 +14,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] -bitcoin = "0.31.2" +bitcoin = "0.32.2" lightning = { version = "0.0.123-beta", path = "../lightning" } [target.'cfg(windows)'.dependencies] @@ -25,7 +25,7 @@ criterion = { version = "0.4", optional = true, default-features = false } [dev-dependencies] lightning = { version = "0.0.123-beta", path = "../lightning", features = ["_test_utils"] } -bitcoin = { version = "0.31.2", default-features = false } +bitcoin = { version = "0.32.2", default-features = false } [lints] workspace = true diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index d1e19421c..dd3a78191 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -91,7 +91,7 @@ impl FilesystemStore { } impl KVStore for FilesystemStore { - fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> std::io::Result> { + fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> lightning::io::Result> { check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "read")?; let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; @@ -114,7 +114,7 @@ impl KVStore for FilesystemStore { Ok(buf) } - fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> { + fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> lightning::io::Result<()> { check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "write")?; let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; @@ -191,7 +191,7 @@ impl KVStore for FilesystemStore { dest_file.sync_all()?; Ok(()) } - Err(e) => Err(e), + Err(e) => Err(e.into()), } } }; @@ -201,7 +201,7 @@ impl KVStore for FilesystemStore { res } - fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> std::io::Result<()> { + fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> lightning::io::Result<()> { check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "remove")?; let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; @@ -290,7 +290,7 @@ impl KVStore for FilesystemStore { Ok(()) } - fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> std::io::Result> { + fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> lightning::io::Result> { check_namespace_key_validity(primary_namespace, secondary_namespace, None, "list")?; let prefixed_dest = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; @@ -331,7 +331,7 @@ impl KVStore for FilesystemStore { PrintableString(primary_namespace), PrintableString(secondary_namespace)); let msg = format!("Failed to list keys of {}/{}: file couldn't be accessed.", PrintableString(primary_namespace), PrintableString(secondary_namespace)); - return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); + return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg)); } match p.strip_prefix(&prefixed_dest) { @@ -345,7 +345,7 @@ impl KVStore for FilesystemStore { PrintableString(primary_namespace), PrintableString(secondary_namespace)); let msg = format!("Failed to list keys of {}/{}: file path is not valid UTF-8", PrintableString(primary_namespace), PrintableString(secondary_namespace)); - return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); + return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg)); } } Err(e) => { @@ -353,7 +353,7 @@ impl KVStore for FilesystemStore { PrintableString(primary_namespace), PrintableString(secondary_namespace), e); let msg = format!("Failed to list keys of {}/{}: {}", PrintableString(primary_namespace), PrintableString(secondary_namespace), e); - return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); + return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg)); } } } diff --git a/lightning-rapid-gossip-sync/Cargo.toml b/lightning-rapid-gossip-sync/Cargo.toml index 7ca67a38e..9d9f3d23e 100644 --- a/lightning-rapid-gossip-sync/Cargo.toml +++ b/lightning-rapid-gossip-sync/Cargo.toml @@ -16,7 +16,7 @@ std = ["lightning/std"] [dependencies] lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false } -bitcoin = { version = "0.31.2", default-features = false } +bitcoin = { version = "0.32.2", default-features = false } [target.'cfg(ldk_bench)'.dependencies] criterion = { version = "0.4", optional = true, default-features = false } diff --git a/lightning-rapid-gossip-sync/src/lib.rs b/lightning-rapid-gossip-sync/src/lib.rs index 25a94e776..04b564f04 100644 --- a/lightning-rapid-gossip-sync/src/lib.rs +++ b/lightning-rapid-gossip-sync/src/lib.rs @@ -70,9 +70,6 @@ extern crate alloc; use core::ops::Deref; use core::sync::atomic::{AtomicBool, Ordering}; -#[cfg(feature = "std")] -use std::fs::File; - use lightning::io; use lightning::ln::msgs::{DecodeError, LightningError}; use lightning::routing::gossip::NetworkGraph; @@ -146,12 +143,16 @@ where /// /// `sync_path`: Path to the file where the gossip update data is located /// - #[cfg(feature = "std")] + #[cfg(all(feature = "std", not(feature = "no-std")))] pub fn sync_network_graph_with_file_path( &self, sync_path: &str, ) -> Result { - let mut file = File::open(sync_path)?; - self.update_network_graph_from_byte_stream(&mut file) + let file = std::fs::File::open(sync_path).map_err(|e| { + let bitcoin_error: lightning::io::Error = e.into(); + bitcoin_error + })?; + let mut buf_reader = std::io::BufReader::new(file); + self.update_network_graph_from_byte_stream(&mut buf_reader) } /// Update network graph from binary data. @@ -190,7 +191,7 @@ where } } -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(feature = "no-std")))] #[cfg(test)] mod tests { use std::fs; diff --git a/lightning-transaction-sync/Cargo.toml b/lightning-transaction-sync/Cargo.toml index 5ef9ef3e4..a53e04cc7 100644 --- a/lightning-transaction-sync/Cargo.toml +++ b/lightning-transaction-sync/Cargo.toml @@ -24,18 +24,18 @@ async-interface = [] [dependencies] lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false, features = ["std"] } -bitcoin = { version = "0.31.2", default-features = false } +bitcoin = { version = "0.32.2", default-features = false } bdk-macros = "0.6" futures = { version = "0.3", optional = true } -esplora-client = { version = "0.7", default-features = false, optional = true } -electrum-client = { version = "0.19.0", optional = true } +esplora-client = { version = "0.9", default-features = false, optional = true } +electrum-client = { version = "0.21.0", optional = true } [dev-dependencies] lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false, features = ["std", "_test_utils"] } tokio = { version = "1.35.0", features = ["full"] } [target.'cfg(not(target_os = "windows"))'.dev-dependencies] -electrsd = { version = "0.27.3", default-features = false, features = ["legacy"] } +electrsd = { version = "0.28.0", default-features = false, features = ["legacy"] } [lints] workspace = true diff --git a/lightning-transaction-sync/tests/integration_tests.rs b/lightning-transaction-sync/tests/integration_tests.rs index 56c0f6a01..2cf913b56 100644 --- a/lightning-transaction-sync/tests/integration_tests.rs +++ b/lightning-transaction-sync/tests/integration_tests.rs @@ -205,7 +205,7 @@ macro_rules! test_syncing { None, ) .unwrap(); - $tx_sync.register_tx(&txid, &new_address.payload().script_pubkey()); + $tx_sync.register_tx(&txid, &new_address.script_pubkey()); maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap(); diff --git a/lightning-types/Cargo.toml b/lightning-types/Cargo.toml index a063176ae..9e8feedf6 100644 --- a/lightning-types/Cargo.toml +++ b/lightning-types/Cargo.toml @@ -16,7 +16,7 @@ rustdoc-args = ["--cfg", "docsrs"] _test_utils = [] [dependencies] -bitcoin = { version = "0.31", default-features = false } +bitcoin = { version = "0.32.2", default-features = false } # TODO: Once we switch to bitcoin 0.32 drop this explicit dep: hex-conservative = { version = "0.2", default-features = false } bech32 = { version = "0.9", default-features = false } diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml index c6d821130..9f310c12d 100644 --- a/lightning/Cargo.toml +++ b/lightning/Cargo.toml @@ -31,8 +31,8 @@ unsafe_revoked_tx_signing = [] # Override signing to not include randomness when generating signatures for test vectors. _test_vectors = [] -no-std = ["hashbrown", "possiblyrandom", "bitcoin/no-std", "lightning-invoice/no-std", "core2/alloc", "libm"] -std = ["bitcoin/std", "bech32/std", "lightning-invoice/std"] +no-std = ["hashbrown", "possiblyrandom", "lightning-invoice/no-std", "libm"] +std = ["lightning-invoice/std", "bech32/std"] # Generates low-r bitcoin signatures, which saves 1 byte in 50% of the cases grind_signatures = [] @@ -44,14 +44,13 @@ lightning-types = { version = "0.1", path = "../lightning-types", default-featur lightning-invoice = { version = "0.31.0-beta", path = "../lightning-invoice", default-features = false } bech32 = { version = "0.9.1", default-features = false } -bitcoin = { version = "0.31.2", default-features = false, features = ["secp-recovery"] } +bitcoin = { version = "0.32.2", default-features = false, features = ["secp-recovery"] } hashbrown = { version = "0.13", optional = true, default-features = false } possiblyrandom = { version = "0.2", optional = true, default-features = false } regex = { version = "1.5.6", optional = true } backtrace = { version = "0.3", optional = true } -core2 = { version = "0.3.0", optional = true, default-features = false } libm = { version = "0.2", optional = true, default-features = false } [dev-dependencies] @@ -59,7 +58,7 @@ regex = "1.5.6" lightning-types = { version = "0.1", path = "../lightning-types", features = ["_test_utils"] } [dev-dependencies.bitcoin] -version = "0.31.2" +version = "0.32.2" default-features = false features = ["bitcoinconsensus", "secp-recovery"] @@ -67,7 +66,7 @@ features = ["bitcoinconsensus", "secp-recovery"] criterion = { version = "0.4", optional = true, default-features = false } [target.'cfg(taproot)'.dependencies] -musig2 = { git = "https://github.com/arik-so/rust-musig2", rev = "739533fc" } +musig2 = { git = "https://github.com/arik-so/rust-musig2", rev = "6f95a05718cbb44d8fe3fa6021aea8117aa38d50" } [lints] workspace = true diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index b9ced6553..68bdba1c9 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -30,9 +30,7 @@ use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hash_types::{Txid, BlockHash}; use bitcoin::ecdsa::Signature as BitcoinSignature; -use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature}; -use bitcoin::secp256k1::{SecretKey, PublicKey}; -use bitcoin::secp256k1; +use bitcoin::secp256k1::{self, SecretKey, PublicKey, Secp256k1, ecdsa::Signature}; use crate::ln::channel::INITIAL_COMMITMENT_NUMBER; use crate::ln::types::{PaymentHash, PaymentPreimage, ChannelId}; @@ -4235,7 +4233,7 @@ impl ChannelMonitorImpl { assert_eq!(&bitcoin::Address::p2wsh(&ScriptBuf::from(input.witness.last().unwrap().to_vec()), bitcoin::Network::Bitcoin).script_pubkey(), _script_pubkey); } else if _script_pubkey.is_p2wpkh() { - assert_eq!(&bitcoin::Address::p2wpkh(&bitcoin::PublicKey::from_slice(&input.witness.last().unwrap()).unwrap(), bitcoin::Network::Bitcoin).unwrap().script_pubkey(), _script_pubkey); + assert_eq!(&bitcoin::Address::p2wpkh(&bitcoin::CompressedPublicKey(bitcoin::PublicKey::from_slice(&input.witness.last().unwrap()).unwrap().inner), bitcoin::Network::Bitcoin).script_pubkey(), _script_pubkey); } else { panic!(); } } return true; diff --git a/lightning/src/io/mod.rs b/lightning/src/io/mod.rs index 1efcd4511..2f742e242 100644 --- a/lightning/src/io/mod.rs +++ b/lightning/src/io/mod.rs @@ -1,9 +1,4 @@ -#[cfg(not(feature = "std"))] -/// Re-export of either `core2::io` or `std::io`, depending on the `std` feature flag. -pub use core2::io::*; -#[cfg(feature = "std")] -/// Re-export of either `core2::io` or `std::io`, depending on the `std` feature flag. -pub use std::io::*; +pub use bitcoin::io::*; /// Emulation of std::io::Cursor #[derive(Clone, Debug, Default, Eq, PartialEq)] diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs index fc860adaa..2215f82bd 100644 --- a/lightning/src/lib.rs +++ b/lightning/src/lib.rs @@ -70,7 +70,6 @@ extern crate core; #[cfg(any(test, feature = "_test_utils"))] extern crate regex; -#[cfg(not(feature = "std"))] extern crate core2; #[cfg(not(feature = "std"))] extern crate libm; #[cfg(ldk_bench)] extern crate criterion; @@ -91,35 +90,15 @@ pub(crate) mod crypto; /// Extension of the bitcoin::io module pub mod io; -#[cfg(not(feature = "std"))] #[doc(hidden)] /// IO utilities public only for use by in-crate macros. These should not be used externally /// /// This is not exported to bindings users as it is not intended for public consumption. pub mod io_extras { - use core2::io::{self, Read, Write}; - - /// A writer which will move data into the void. - pub struct Sink { - _priv: (), - } + use bitcoin::io::{self, Read, Write}; /// Creates an instance of a writer which will successfully consume all data. - pub const fn sink() -> Sink { - Sink { _priv: () } - } - - impl core2::io::Write for Sink { - #[inline] - fn write(&mut self, buf: &[u8]) -> core2::io::Result { - Ok(buf.len()) - } - - #[inline] - fn flush(&mut self) -> core2::io::Result<()> { - Ok(()) - } - } + pub use bitcoin::io::sink; pub fn copy(reader: &mut R, writer: &mut W) -> Result where @@ -155,21 +134,6 @@ pub mod io_extras { } } -#[cfg(feature = "std")] -#[doc(hidden)] -/// IO utilities public only for use by in-crate macros. These should not be used externally -/// -/// This is not exported to bindings users as it is not intended for public consumption. -mod io_extras { - pub fn read_to_end(mut d: D) -> Result, ::std::io::Error> { - let mut buf = Vec::new(); - d.read_to_end(&mut buf)?; - Ok(buf) - } - - pub use std::io::{copy, sink}; -} - mod prelude { #![allow(unused_imports)] diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index 6d47e1b9d..407bfd3db 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -766,7 +766,7 @@ pub fn build_htlc_input_witness( let mut witness = Witness::new(); // First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element. witness.push(vec![]); - witness.push_ecdsa_signature(&BitcoinSignature { sig: *remote_sig, hash_ty: remote_sighash_type }); + witness.push_ecdsa_signature(&BitcoinSignature { signature: *remote_sig, sighash_type: remote_sighash_type }); witness.push_ecdsa_signature(&BitcoinSignature::sighash_all(*local_sig)); if let Some(preimage) = preimage { witness.push(preimage.0.to_vec()); @@ -1872,11 +1872,10 @@ mod tests { use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1}; use crate::util::test_utils; use crate::sign::{ChannelSigner, SignerProvider}; - use bitcoin::{Network, Txid, ScriptBuf}; + use bitcoin::{Network, Txid, ScriptBuf, CompressedPublicKey}; use bitcoin::hashes::Hash; use bitcoin::hex::FromHex; use crate::ln::types::PaymentHash; - use bitcoin::address::Payload; use bitcoin::PublicKey as BitcoinPublicKey; use crate::ln::features::ChannelTypeFeatures; @@ -1949,7 +1948,7 @@ mod tests { // Generate broadcaster and counterparty outputs let tx = builder.build(1000, 2000); assert_eq!(tx.built.transaction.output.len(), 2); - assert_eq!(tx.built.transaction.output[1].script_pubkey, Payload::p2wpkh(&BitcoinPublicKey::new(builder.counterparty_pubkeys.payment_point)).unwrap().script_pubkey()); + assert_eq!(tx.built.transaction.output[1].script_pubkey, bitcoin::address::Address::p2wpkh(&CompressedPublicKey(builder.counterparty_pubkeys.payment_point), Network::Testnet).script_pubkey()); // Generate broadcaster and counterparty outputs as well as two anchors builder.channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(); diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index d04cfa7fa..9fa130006 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -2786,7 +2786,7 @@ impl ReadableArgs<(Option, &NS)> for InboundOnionPayload w }, |msg_type: u64, msg_reader: &mut FixedLengthReader<_>| -> Result { if msg_type < 1 << 16 { return Ok(false) } let mut value = Vec::new(); - msg_reader.read_to_end(&mut value)?; + msg_reader.read_to_limit(&mut value, u64::MAX)?; custom_tlvs.push((msg_type, value)); Ok(true) }); @@ -4007,11 +4007,11 @@ mod tests { output: vec![ TxOut { value: Amount::from_sat(12704566), - script_pubkey: Address::from_str("bc1qzlffunw52jav8vwdu5x3jfk6sr8u22rmq3xzw2").unwrap().payload().script_pubkey(), + script_pubkey: Address::from_str("bc1qzlffunw52jav8vwdu5x3jfk6sr8u22rmq3xzw2").unwrap().assume_checked().script_pubkey(), }, TxOut { value: Amount::from_sat(245148), - script_pubkey: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().payload().script_pubkey(), + script_pubkey: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().assume_checked().script_pubkey(), }, ], }).unwrap(), @@ -4030,7 +4030,7 @@ mod tests { channel_id: ChannelId::from_bytes([2; 32]), serial_id: 4886718345, sats: 4886718345, - script: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().payload().script_pubkey(), + script: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().assume_checked().script_pubkey(), }; let encoded_value = tx_add_output.encode(); let target_value = >::from_hex("0202020202020202020202020202020202020202020202020202020202020202000000012345678900000001234567890016001436ec78d514df462da95e6a00c24daa8915362d42").unwrap(); @@ -4179,7 +4179,7 @@ mod tests { scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).unwrap().script_pubkey() } - else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).unwrap().script_pubkey() } + else if script_type == 3 { Address::p2wpkh(&::bitcoin::CompressedPublicKey(pubkey_1), Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() }, }; let encoded_value = shutdown.encode(); diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 39423f94c..c94851135 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -106,7 +106,7 @@ use bitcoin::{WitnessProgram, Network, WitnessVersion}; use bitcoin::constants::ChainHash; use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, self}; use bitcoin::secp256k1::schnorr::Signature; -use bitcoin::address::{Address, Payload}; +use bitcoin::address::Address; use core::time::Duration; use core::hash::{Hash, Hasher}; use crate::io; @@ -1098,12 +1098,11 @@ pub(super) fn filter_fallbacks( Err(_) => return None, }; - let program = address.program.clone(); - let witness_program = match WitnessProgram::new(version, program) { + let witness_program = match WitnessProgram::new(version, &address.program) { Ok(witness_program) => witness_program, Err(_) => return None, }; - Some(Address::new(network, Payload::WitnessProgram(witness_program))) + Some(Address::from_witness_program(witness_program, network)) }; fallbacks.iter().filter_map(to_valid_address).collect() @@ -1422,13 +1421,13 @@ pub(super) fn check_invoice_signing_pubkey( mod tests { use super::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, FallbackAddress, FullInvoiceTlvStreamRef, InvoiceTlvStreamRef, SIGNATURE_TAG, UnsignedBolt12Invoice}; - use bitcoin::{WitnessProgram, WitnessVersion}; + use bitcoin::{CompressedPublicKey, WitnessProgram, WitnessVersion}; use bitcoin::constants::ChainHash; use bitcoin::script::ScriptBuf; use bitcoin::hashes::Hash; use bitcoin::network::Network; use bitcoin::secp256k1::{Keypair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self}; - use bitcoin::address::{Address, Payload}; + use bitcoin::address::Address; use bitcoin::key::TweakedPublicKey; use core::time::Duration; @@ -1993,7 +1992,7 @@ mod tests { invoice.fallbacks(), vec![ Address::p2wsh(&script, Network::Bitcoin), - Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(), + Address::p2wpkh(&CompressedPublicKey(pubkey.inner), Network::Bitcoin), Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin), ], ); @@ -2063,7 +2062,7 @@ mod tests { .sign(payer_sign) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, SignError::Verification(secp256k1::Error::InvalidSignature)), + Err(e) => assert_eq!(e, SignError::Verification(secp256k1::Error::IncorrectSignature)), } } @@ -2301,16 +2300,16 @@ mod tests { match Bolt12Invoice::try_from(buffer) { Ok(invoice) => { - let v1_witness_program = WitnessProgram::new(WitnessVersion::V1, vec![0u8; 33]).unwrap(); - let v2_witness_program = WitnessProgram::new(WitnessVersion::V2, vec![0u8; 40]).unwrap(); + let v1_witness_program = WitnessProgram::new(WitnessVersion::V1, &[0u8; 33]).unwrap(); + let v2_witness_program = WitnessProgram::new(WitnessVersion::V2, &[0u8; 40]).unwrap(); assert_eq!( invoice.fallbacks(), vec![ Address::p2wsh(&script, Network::Bitcoin), - Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(), + Address::p2wpkh(&CompressedPublicKey(pubkey.inner), Network::Bitcoin), Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin), - Address::new(Network::Bitcoin, Payload::WitnessProgram(v1_witness_program)), - Address::new(Network::Bitcoin, Payload::WitnessProgram(v2_witness_program)), + Address::from_witness_program(v1_witness_program, Network::Bitcoin), + Address::from_witness_program(v2_witness_program, Network::Bitcoin), ], ); }, @@ -2474,7 +2473,7 @@ mod tests { match Bolt12Invoice::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, Bolt12ParseError::InvalidSignature(secp256k1::Error::InvalidSignature)); + assert_eq!(e, Bolt12ParseError::InvalidSignature(secp256k1::Error::IncorrectSignature)); }, } } diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index f8815beb6..4b73231f6 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -1889,7 +1889,7 @@ mod tests { .sign(recipient_sign) { Ok(_) => panic!("expected error"), - Err(e) => assert_eq!(e, SignError::Verification(secp256k1::Error::InvalidSignature)), + Err(e) => assert_eq!(e, SignError::Verification(secp256k1::Error::IncorrectSignature)), } } @@ -2276,7 +2276,7 @@ mod tests { match InvoiceRequest::try_from(buffer) { Ok(_) => panic!("expected error"), Err(e) => { - assert_eq!(e, Bolt12ParseError::InvalidSignature(secp256k1::Error::InvalidSignature)); + assert_eq!(e, Bolt12ParseError::InvalidSignature(secp256k1::Error::IncorrectSignature)); }, } } diff --git a/lightning/src/offers/static_invoice.rs b/lightning/src/offers/static_invoice.rs index 93014fce2..a49b20bdf 100644 --- a/lightning/src/offers/static_invoice.rs +++ b/lightning/src/offers/static_invoice.rs @@ -1128,7 +1128,7 @@ mod tests { Err(e) => { assert_eq!( e, - Bolt12ParseError::InvalidSignature(secp256k1::Error::InvalidSignature) + Bolt12ParseError::InvalidSignature(secp256k1::Error::IncorrectSignature) ); }, } diff --git a/lightning/src/onion_message/offers.rs b/lightning/src/onion_message/offers.rs index 6884ca77e..d1ed089ed 100644 --- a/lightning/src/onion_message/offers.rs +++ b/lightning/src/onion_message/offers.rs @@ -172,7 +172,7 @@ impl ReadableArgs<(u64, &L)> for OffersMessage { } let mut bytes = Vec::new(); - r.read_to_end(&mut bytes).unwrap(); + r.read_to_limit(&mut bytes, u64::MAX).unwrap(); match Self::parse(tlv_type, bytes) { Ok(message) => Ok(message), diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index bf894f0eb..6f378a561 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -8785,7 +8785,7 @@ mod tests { pub(crate) mod bench_utils { use super::*; use std::fs::File; - + use std::io::Read; use bitcoin::hashes::Hash; use bitcoin::secp256k1::SecretKey; @@ -8845,9 +8845,13 @@ pub(crate) mod bench_utils { pub(crate) fn read_graph_scorer(logger: &TestLogger) -> Result<(Arc>, ProbabilisticScorer>, &TestLogger>), &'static str> { let (mut graph_file, mut scorer_file) = get_graph_scorer_file()?; - let graph = Arc::new(NetworkGraph::read(&mut graph_file, logger).unwrap()); + let mut graph_buffer = Vec::new(); + let mut scorer_buffer = Vec::new(); + graph_file.read_to_end(&mut graph_buffer).unwrap(); + scorer_file.read_to_end(&mut scorer_buffer).unwrap(); + let graph = Arc::new(NetworkGraph::read(&mut &graph_buffer[..], logger).unwrap()); let scorer_args = (Default::default(), Arc::clone(&graph), logger); - let scorer = ProbabilisticScorer::read(&mut scorer_file, scorer_args).unwrap(); + let scorer = ProbabilisticScorer::read(&mut &scorer_buffer[..], scorer_args).unwrap(); Ok((graph, scorer)) } diff --git a/lightning/src/sign/mod.rs b/lightning/src/sign/mod.rs index 1dd8c90f6..63692bab5 100644 --- a/lightning/src/sign/mod.rs +++ b/lightning/src/sign/mod.rs @@ -1327,8 +1327,8 @@ impl InMemorySigner { .unwrap()[..] ); let local_delayedsig = EcdsaSignature { - sig: sign_with_aux_rand(secp_ctx, &sighash, &delayed_payment_key, &self), - hash_ty: EcdsaSighashType::All, + signature: sign_with_aux_rand(secp_ctx, &sighash, &delayed_payment_key, &self), + sighash_type: EcdsaSighashType::All, }; let payment_script = bitcoin::Address::p2wsh(&witness_script, Network::Bitcoin).script_pubkey(); @@ -2106,13 +2106,12 @@ impl KeysManager { }; let pubkey = Xpub::from_priv(&secp_ctx, &secret).to_pub(); if derivation_idx == 2 { - assert_eq!(pubkey.inner, self.shutdown_pubkey); + assert_eq!(pubkey.0, self.shutdown_pubkey); } let witness_script = bitcoin::Address::p2pkh(&pubkey, Network::Testnet).script_pubkey(); - let payment_script = bitcoin::Address::p2wpkh(&pubkey, Network::Testnet) - .expect("uncompressed key found") - .script_pubkey(); + let payment_script = + bitcoin::Address::p2wpkh(&pubkey, Network::Testnet).script_pubkey(); if payment_script != output.script_pubkey { return Err(()); @@ -2131,8 +2130,7 @@ impl KeysManager { let sig = sign_with_aux_rand(secp_ctx, &sighash, &secret.private_key, &self); let mut sig_ser = sig.serialize_der().to_vec(); sig_ser.push(EcdsaSighashType::All as u8); - let witness = - Witness::from_slice(&[&sig_ser, &pubkey.inner.serialize().to_vec()]); + let witness = Witness::from_slice(&[&sig_ser, &pubkey.0.serialize().to_vec()]); psbt.inputs[input_idx].final_script_witness = Some(witness); }, } diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index e3e97f2d6..7c1f97071 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -14,7 +14,7 @@ //! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor use crate::prelude::*; -use crate::io::{self, Read, Write}; +use crate::io::{self, BufRead, Read, Write}; use crate::io_extras::{copy, sink}; use core::hash::Hash; use crate::sync::{Mutex, RwLock}; @@ -64,6 +64,64 @@ impl Writer for W { } } +/// Wrap buffering support for implementations of Read. +/// A [`Read`]er which keeps an internal buffer to avoid hitting the underlying stream directly for +/// every read, implementing [`BufRead`]. +/// +/// In order to avoid reading bytes past the first object, and those bytes then ending up getting +/// dropped, this BufReader operates in one-byte-increments. +struct BufReader<'a, R: Read> { + inner: &'a mut R, + buf: [u8; 1], + is_consumed: bool +} + +impl<'a, R: Read> BufReader<'a, R> { + /// Creates a [`BufReader`] which will read from the given `inner`. + pub fn new(inner: &'a mut R) -> Self { + BufReader { + inner, + buf: [0; 1], + is_consumed: true + } + } +} + +impl<'a, R: Read> Read for BufReader<'a, R> { + #[inline] + fn read(&mut self, output: &mut [u8]) -> io::Result { + let input = self.fill_buf()?; + let count = cmp::min(input.len(), output.len()); + output[..count].copy_from_slice(&input[..count]); + self.consume(count); + Ok(count) + } +} + +impl<'a, R: Read> BufRead for BufReader<'a, R> { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { + if self.is_consumed { + let count = self.inner.read(&mut self.buf[..])?; + debug_assert!(count <= 1, "read gave us a garbage length"); + + // upon hitting EOF, assume the byte is already consumed + self.is_consumed = count == 0; + } + + Ok(&self.buf[..]) + } + + #[inline] + fn consume(&mut self, amount: usize) { + if amount >= 1 { + debug_assert_eq!(amount, 1, "Can only consume one byte"); + debug_assert!(!self.is_consumed, "Cannot consume more than had been read"); + self.is_consumed = true; + } + } +} + pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W); impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> { #[inline] @@ -1247,10 +1305,11 @@ macro_rules! impl_consensus_ser { impl Readable for $bitcoin_type { fn read(r: &mut R) -> Result { - match consensus::encode::Decodable::consensus_decode(r) { + let mut reader = BufReader::<_>::new(r); + match consensus::encode::Decodable::consensus_decode(&mut reader) { Ok(t) => Ok(t), Err(consensus::encode::Error::Io(ref e)) if e.kind() == io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead), - Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e.kind())), + Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e.kind().into())), Err(_) => Err(DecodeError::InvalidValue), } } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index f5256064e..b639cb673 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -1592,8 +1592,8 @@ impl WalletSource for TestWalletSource { let sighash = SighashCache::new(&tx) .legacy_signature_hash(i, &utxo.output.script_pubkey, EcdsaSighashType::All as u32) .map_err(|_| ())?; - let sig = self.secp.sign_ecdsa(&secp256k1::Message::from_digest(sighash.to_byte_array()), &self.secret_key); - let bitcoin_sig = bitcoin::ecdsa::Signature { sig, hash_ty: EcdsaSighashType::All }; + let signature = self.secp.sign_ecdsa(&secp256k1::Message::from_digest(sighash.to_byte_array()), &self.secret_key); + let bitcoin_sig = bitcoin::ecdsa::Signature { signature, sighash_type: EcdsaSighashType::All }; tx.input[i].script_sig = Builder::new() .push_slice(&bitcoin_sig.serialize()) .push_slice(&self.secret_key.public_key(&self.secp).serialize())