]> git.bitcoin.ninja Git - rust-lightning/commitdiff
dont use & 2024-08-io-no-ref-for-arik
authorMatt Corallo <git@bluematt.me>
Mon, 12 Aug 2024 19:35:48 +0000 (19:35 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 12 Aug 2024 19:35:48 +0000 (19:35 +0000)
Cargo.toml
lightning/Cargo.toml
lightning/src/blinded_path/mod.rs
lightning/src/crypto/streams.rs
lightning/src/lib.rs
lightning/src/ln/msgs.rs
lightning/src/routing/router.rs
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs

index 59b5459924f38a328ab5aca09dda73e1f955ea45..0aa7f7624d8e8ec9d739431adde26d8a4553ec29 100644 (file)
@@ -43,11 +43,6 @@ panic = "abort"
 [patch.crates-io.possiblyrandom]
 path = "possiblyrandom"
 
-[patch.crates-io]
-bitcoin = { path = "../../rust-bitcoin/bitcoin" }
-bitcoin-io = {path = "../../rust-bitcoin/io" }
-bitcoin_hashes = {path = "../../rust-bitcoin/hashes"}
-
 [workspace.lints.rust.unexpected_cfgs]
 level = "forbid"
 # When adding a new cfg attribute, ensure that it is added to this list.
index b0d68fc10383513b0fcacdbe313cbee0f5841844..f0566433ee4418263414f1bb3ed288ee059dfe83 100644 (file)
@@ -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", "core2/alloc", "libm"]
-std = ["bitcoin/std", "bech32/std", "core2/alloc"]
+no-std = ["hashbrown", "possiblyrandom", "libm"]
+std = ["bech32/std"]
 
 # Generates low-r bitcoin signatures, which saves 1 byte in 50% of the cases
 grind_signatures = []
@@ -49,7 +49,6 @@ hex = { package = "hex-conservative", version = "0.1.1", default-features = fals
 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]
index e34e64c7ac29132ba53131cd1be7fdafbae7b222..84d8b3ca79016ec46b9d9f063f0e9f936cf3a1ee 100644 (file)
@@ -281,8 +281,10 @@ impl Readable for BlindedPath {
                        1 => IntroductionNode::DirectedShortChannelId(Direction::NodeTwo, Readable::read(r)?),
                        2|3 => {
                                use io::Read;
-                               let mut pubkey_read = core::slice::from_mut(&mut first_byte).chain(r.by_ref());
-                               IntroductionNode::NodeId(Readable::read(&mut pubkey_read)?)
+                               let mut bytes = [0; 33];
+                               bytes[0] = first_byte;
+                               r.read_exact(&mut bytes[1..])?;
+                               IntroductionNode::NodeId(Readable::read(&mut &bytes[..])?)
                        },
                        _ => return Err(DecodeError::InvalidValue),
                };
index e8217b8d9d3999a1feb647fbb1e9d5456bed5ea8..7a2ab86d18489dea269c2ee9048e463e67d27bbe 100644 (file)
@@ -62,7 +62,7 @@ impl<T: Readable> LengthReadableArgs<[u8; 32]> for ChaChaPolyReadAdapter<T> {
 
                let mut chacha = ChaCha20Poly1305RFC::new(&secret, &[0; 12], &[]);
                let decrypted_len = r.total_bytes() - 16;
-               let s = FixedLengthReader::new(&mut r, decrypted_len);
+               let s = FixedLengthReader::new(r, decrypted_len);
                let mut chacha_stream = ChaChaPolyReader { chacha: &mut chacha, read: s };
                let readable: T = Readable::read(&mut chacha_stream)?;
                chacha_stream.read.eat_remaining()?;
@@ -194,7 +194,7 @@ mod tests {
 
                // Now deserialize the object back and make sure it matches the original.
                let mut read_adapter: Option<ChaChaPolyReadAdapter<TestWriteable>> = None;
-               decode_tlv_stream!(&writer.0[..], {
+               decode_tlv_stream!(&mut &writer.0[..], {
                        (1, read_adapter, (option: LengthReadableArgs, rho)),
                });
                assert_eq!(writeable, read_adapter.unwrap().readable);
index 431fee5a00381ea1ebcc1157baeea9b688296c34..749fd8685eb921ea6092ffc3a93d9c8f56d167b3 100644 (file)
@@ -68,7 +68,6 @@ extern crate core;
 extern crate hex;
 #[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;
@@ -403,7 +402,7 @@ pub mod io_extras {
                Ok(count)
        }
 
-       pub fn read_to_end<D: Read>(mut d: D) -> Result<alloc::vec::Vec<u8>, io::Error> {
+       pub fn read_to_end<D: Read>(mut d: &mut D) -> Result<alloc::vec::Vec<u8>, io::Error> {
                let mut result = vec![];
                let mut buf = [0u8; 64];
                loop {
index 3d606f4b96640fce06b6ca65928d5885d0a4dc35..cca0b8cf666d4c57191d9ea39db5fb41d9b59c82 100644 (file)
@@ -2721,8 +2721,8 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload w
                let mut custom_tlvs = Vec::new();
 
                let tlv_len = BigSize::read(r)?;
-               let rd = FixedLengthReader::new(r, tlv_len.0);
-               decode_tlv_stream_with_custom_tlv_decode!(rd, {
+               let mut rd = FixedLengthReader::new(r, tlv_len.0);
+               decode_tlv_stream_with_custom_tlv_decode!(&mut rd, {
                        (2, amt, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
                        (4, cltv_value, (option, encoding: (u32, HighZeroBytesDroppedBigSize))),
                        (6, short_id, option),
index 71fde86d0b6964b4ad75d167c62b02001134c9e6..c47a26ffb256775c508a8747c30c1fcade2c1623 100644 (file)
@@ -8868,9 +8868,9 @@ pub(crate) mod bench_utils {
        pub(crate) fn read_graph_scorer(logger: &TestLogger)
        -> Result<(Arc<NetworkGraph<&TestLogger>>, ProbabilisticScorer<Arc<NetworkGraph<&TestLogger>>, &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 graph = Arc::new(NetworkGraph::read(&mut std::io::BufReader::new(graph_file), 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 std::io::BufReader::new(scorer_file), scorer_args).unwrap();
                Ok((graph, scorer))
        }
 
index 35baf71e5ead2f783524b28737844d460643dafe..69ed8f94755712406dcf3cf24ff6087ee19ec0b0 100644 (file)
@@ -65,15 +65,15 @@ impl<W: Write> Writer for W {
 }
 
 // pub(crate) struct ReadBufReadAdapter<R: Read + ?Sized>(pub R);
-pub struct BufReader<R, const S: usize> {
-       inner: R,
+pub struct BufReader<'a, R, const S: usize = 4096> {
+       inner: &'a mut R,
        buf: [u8; S],
        pos: usize,
        cap: usize,
 }
 
-impl<R: Read, const S: usize> BufReader<R, S> {
-       pub fn new(inner: R) -> BufReader<R, S> {
+impl<'a, R: Read, const S: usize> BufReader<'a, R, S> {
+       pub fn new(inner: &'a mut R) -> BufReader<'a, R, S> {
                BufReader {
                        inner,
                        buf: [0; S],
@@ -83,13 +83,13 @@ impl<R: Read, const S: usize> BufReader<R, S> {
        }
 }
 
-impl<R: Read, const S: usize> Read for BufReader<R, S> {
+impl<'a, R: Read, const S: usize> Read for BufReader<'a, R, S> {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
                // If we don't have any buffered data and we're doing a massive read
                // (larger than our internal buffer), bypass our internal buffer
                // entirely.
                if self.pos == self.cap && buf.len() >= S {
-                       self.discard_buffer();
+                       //self.discard_buffer();
                        return self.inner.read(buf);
                }
                let nread = {
@@ -101,7 +101,7 @@ impl<R: Read, const S: usize> Read for BufReader<R, S> {
        }
 }
 
-impl<R: Read, const S: usize> BufRead for BufReader<R, S> {
+impl<'a, R: Read, const S: usize> BufRead for BufReader<'a, R, S> {
        fn fill_buf(&mut self) -> io::Result<&[u8]> {
                // If we've reached the end of our internal buffer then we need to fetch
                // some more data from the underlying reader.
@@ -249,18 +249,18 @@ impl<'a, R: Read> LengthRead for FixedLengthReader<'a, R> {
 /// between "EOF reached before we started" and "EOF reached mid-read".
 ///
 /// This is not exported to bindings users as manual TLV building is not currently supported in bindings
-pub struct ReadTrackingReader<R: Read> {
-       read: R,
+pub struct ReadTrackingReader<'a, R: Read> {
+       read: &'a mut R,
        /// Returns whether we have read from this reader or not yet.
        pub have_read: bool,
 }
-impl<R: Read> ReadTrackingReader<R> {
+impl<'a, R: Read> ReadTrackingReader<'a, R> {
        /// Returns a new [`ReadTrackingReader`].
-       pub fn new(read: R) -> Self {
+       pub fn new(read: &'a mut R) -> Self {
                Self { read, have_read: false }
        }
 }
-impl<R: Read> Read for ReadTrackingReader<R> {
+impl<'a, R: Read> Read for ReadTrackingReader<'a, R> {
        #[inline]
        fn read(&mut self, dest: &mut [u8]) -> Result<usize, io::Error> {
                match self.read.read(dest) {
@@ -766,7 +766,7 @@ impl<T: MaybeReadable> Readable for WithoutLength<Vec<T>> {
        fn read<R: Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
                let mut values = Vec::new();
                loop {
-                       let mut track_read = ReadTrackingReader::new(&mut reader);
+                       let mut track_read = ReadTrackingReader::new(reader);
                        match MaybeReadable::read(&mut track_read) {
                                Ok(Some(v)) => { values.push(v); },
                                Ok(None) => { },
@@ -1344,7 +1344,7 @@ macro_rules! impl_consensus_ser {
 
                impl Readable for $bitcoin_type {
                        fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
-                               match consensus::encode::Decodable::consensus_decode(&mut BufReader::new(r)) {
+                               match consensus::encode::Decodable::consensus_decode(&mut BufReader::<_, 4096>::new(r)) {
                                        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().into())),
index f7a299d9b21a70e1767797eec158331ca9196ff3..ce7f0987256277d49d7b4c5e7bd1b759d35295c2 100644 (file)
@@ -519,7 +519,7 @@ macro_rules! _decode_tlv_stream_range {
                                // determine whether we should break or return ShortRead if we get an
                                // UnexpectedEof. This should in every case be largely cosmetic, but its nice to
                                // pass the TLV test vectors exactly, which require this distinction.
-                               let mut tracking_reader = ser::ReadTrackingReader::new(&mut stream_ref);
+                               let mut tracking_reader = ser::ReadTrackingReader::new(stream_ref);
                                match <$crate::util::ser::BigSize as $crate::util::ser::Readable>::read(&mut tracking_reader) {
                                        Err(DecodeError::ShortRead) => {
                                                if !tracking_reader.have_read {
@@ -555,8 +555,8 @@ macro_rules! _decode_tlv_stream_range {
                        last_seen_type = Some(typ.0);
 
                        // Finally, read the length and value itself:
-                       let length: ser::BigSize = $crate::util::ser::Readable::read(&mut stream_ref)?;
-                       let mut s = ser::FixedLengthReader::new(&mut stream_ref, length.0);
+                       let length: ser::BigSize = $crate::util::ser::Readable::read(stream_ref)?;
+                       let mut s = ser::FixedLengthReader::new(stream_ref, length.0);
                        match typ.0 {
                                $(_t if $crate::_decode_tlv_stream_match_check!(_t, $type, $fieldty) => {
                                        $crate::_decode_tlv!($stream, s, $field, $fieldty);
@@ -1102,7 +1102,7 @@ macro_rules! impl_writeable_tlv_based_enum {
                                        }),*
                                        $($tuple_variant_id => {
                                                let length: $crate::util::ser::BigSize = $crate::util::ser::Readable::read(reader)?;
-                                               let mut s = $crate::util::ser::FixedLengthReader::new(&mut reader, length.0);
+                                               let mut s = $crate::util::ser::FixedLengthReader::new(reader, length.0);
                                                let res = $crate::util::ser::Readable::read(&mut s)?;
                                                if s.bytes_remain() {
                                                        s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
@@ -1214,7 +1214,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
                                        }),*
                                        $($tuple_variant_id => {
                                                let length: $crate::util::ser::BigSize = $crate::util::ser::Readable::read(reader)?;
-                                               let mut s = $crate::util::ser::FixedLengthReader::new(&mut reader, length.0);
+                                               let mut s = $crate::util::ser::FixedLengthReader::new(reader, length.0);
                                                let res = $crate::util::ser::Readable::read(&mut s)?;
                                                if s.bytes_remain() {
                                                        s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes