bitcoin/consensus/
encode.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Bitcoin consensus-encodable types.
4//!
5//! This is basically a replacement of the `Encodable` trait which does
6//! normalization of endianness etc., to ensure that the encoding matches
7//! the network consensus encoding.
8//!
9//! Essentially, anything that must go on the _disk_ or _network_ must be
10//! encoded using the `Encodable` trait, since this data must be the same for
11//! all systems. Any data going to the _user_ e.g., over JSONRPC, should use the
12//! ordinary `Encodable` trait. (This should also be the same across systems, of
13//! course, but has some critical differences from the network format e.g.,
14//! scripts come with an opcode decode, hashes are big-endian, numbers are
15//! typically big-endian decimals, etc.)
16//!
17
18use core::{fmt, mem};
19
20use hashes::{sha256, sha256d, Hash};
21use hex::error::{InvalidCharError, OddLengthStringError};
22use internals::write_err;
23use io::{Cursor, Read, Write};
24
25use crate::bip152::{PrefilledTransaction, ShortId};
26use crate::bip158::{FilterHash, FilterHeader};
27use crate::blockdata::block::{self, BlockHash, TxMerkleNode};
28use crate::blockdata::transaction::{Transaction, TxIn, TxOut};
29use crate::consensus::{DecodeError, IterReader};
30#[cfg(feature = "std")]
31use crate::p2p::{
32    address::{AddrV2Message, Address},
33    message_blockdata::Inventory,
34};
35use crate::prelude::*;
36use crate::taproot::TapLeafHash;
37
38/// Encoding error.
39#[derive(Debug)]
40#[non_exhaustive]
41pub enum Error {
42    /// And I/O error.
43    Io(io::Error),
44    /// Tried to allocate an oversized vector.
45    OversizedVectorAllocation {
46        /// The capacity requested.
47        requested: usize,
48        /// The maximum capacity.
49        max: usize,
50    },
51    /// Checksum was invalid.
52    InvalidChecksum {
53        /// The expected checksum.
54        expected: [u8; 4],
55        /// The invalid checksum.
56        actual: [u8; 4],
57    },
58    /// VarInt was encoded in a non-minimal way.
59    NonMinimalVarInt,
60    /// Parsing error.
61    ParseFailed(&'static str),
62    /// Unsupported Segwit flag.
63    UnsupportedSegwitFlag(u8),
64}
65
66internals::impl_from_infallible!(Error);
67
68impl fmt::Display for Error {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        use Error::*;
71
72        match *self {
73            Io(ref e) => write_err!(f, "IO error"; e),
74            OversizedVectorAllocation { requested: ref r, max: ref m } =>
75                write!(f, "allocation of oversized vector: requested {}, maximum {}", r, m),
76            InvalidChecksum { expected: ref e, actual: ref a } =>
77                write!(f, "invalid checksum: expected {:x}, actual {:x}", e.as_hex(), a.as_hex()),
78            NonMinimalVarInt => write!(f, "non-minimal varint"),
79            ParseFailed(ref s) => write!(f, "parse failed: {}", s),
80            UnsupportedSegwitFlag(ref swflag) =>
81                write!(f, "unsupported segwit version: {}", swflag),
82        }
83    }
84}
85
86#[cfg(feature = "std")]
87impl std::error::Error for Error {
88    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
89        use Error::*;
90
91        match self {
92            Io(e) => Some(e),
93            OversizedVectorAllocation { .. }
94            | InvalidChecksum { .. }
95            | NonMinimalVarInt
96            | ParseFailed(_)
97            | UnsupportedSegwitFlag(_) => None,
98        }
99    }
100}
101
102impl From<io::Error> for Error {
103    fn from(error: io::Error) -> Self { Error::Io(error) }
104}
105
106/// Hex deserialization error.
107#[derive(Debug)]
108pub enum FromHexError {
109    /// Purported hex string had odd length.
110    OddLengthString(OddLengthStringError),
111    /// Decoding error.
112    Decode(DecodeError<InvalidCharError>),
113}
114
115impl fmt::Display for FromHexError {
116    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117        use FromHexError::*;
118
119        match *self {
120            OddLengthString(ref e) =>
121                write_err!(f, "odd length, failed to create bytes from hex"; e),
122            Decode(ref e) => write_err!(f, "decoding error"; e),
123        }
124    }
125}
126
127#[cfg(feature = "std")]
128impl std::error::Error for FromHexError {
129    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
130        use FromHexError::*;
131
132        match *self {
133            OddLengthString(ref e) => Some(e),
134            Decode(ref e) => Some(e),
135        }
136    }
137}
138
139impl From<OddLengthStringError> for FromHexError {
140    #[inline]
141    fn from(e: OddLengthStringError) -> Self { Self::OddLengthString(e) }
142}
143
144/// Encodes an object into a vector.
145pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
146    let mut encoder = Vec::new();
147    let len = data.consensus_encode(&mut encoder).expect("in-memory writers don't error");
148    debug_assert_eq!(len, encoder.len());
149    encoder
150}
151
152/// Encodes an object into a hex-encoded string.
153pub fn serialize_hex<T: Encodable + ?Sized>(data: &T) -> String {
154    serialize(data).to_lower_hex_string()
155}
156
157/// Deserializes an object from a vector, will error if said deserialization
158/// doesn't consume the entire vector.
159pub fn deserialize<T: Decodable>(data: &[u8]) -> Result<T, Error> {
160    let (rv, consumed) = deserialize_partial(data)?;
161
162    // Fail if data are not consumed entirely.
163    if consumed == data.len() {
164        Ok(rv)
165    } else {
166        Err(Error::ParseFailed("data not consumed entirely when explicitly deserializing"))
167    }
168}
169
170/// Deserialize any decodable type from a hex string, will error if said deserialization
171/// doesn't consume the entire vector.
172pub fn deserialize_hex<T: Decodable>(hex: &str) -> Result<T, FromHexError> {
173    let iter = hex::HexSliceToBytesIter::new(hex)?;
174    let reader = IterReader::new(iter);
175    Ok(reader.decode().map_err(FromHexError::Decode)?)
176}
177
178/// Deserializes an object from a vector, but will not report an error if said deserialization
179/// doesn't consume the entire vector.
180pub fn deserialize_partial<T: Decodable>(data: &[u8]) -> Result<(T, usize), Error> {
181    let mut decoder = Cursor::new(data);
182    let rv = Decodable::consensus_decode_from_finite_reader(&mut decoder)?;
183    let consumed = decoder.position() as usize;
184
185    Ok((rv, consumed))
186}
187
188/// Extensions of `Write` to encode data as per Bitcoin consensus.
189pub trait WriteExt: Write {
190    /// Outputs a 64-bit unsigned integer.
191    fn emit_u64(&mut self, v: u64) -> Result<(), io::Error>;
192    /// Outputs a 32-bit unsigned integer.
193    fn emit_u32(&mut self, v: u32) -> Result<(), io::Error>;
194    /// Outputs a 16-bit unsigned integer.
195    fn emit_u16(&mut self, v: u16) -> Result<(), io::Error>;
196    /// Outputs an 8-bit unsigned integer.
197    fn emit_u8(&mut self, v: u8) -> Result<(), io::Error>;
198
199    /// Outputs a 64-bit signed integer.
200    fn emit_i64(&mut self, v: i64) -> Result<(), io::Error>;
201    /// Outputs a 32-bit signed integer.
202    fn emit_i32(&mut self, v: i32) -> Result<(), io::Error>;
203    /// Outputs a 16-bit signed integer.
204    fn emit_i16(&mut self, v: i16) -> Result<(), io::Error>;
205    /// Outputs an 8-bit signed integer.
206    fn emit_i8(&mut self, v: i8) -> Result<(), io::Error>;
207
208    /// Outputs a boolean.
209    fn emit_bool(&mut self, v: bool) -> Result<(), io::Error>;
210
211    /// Outputs a byte slice.
212    fn emit_slice(&mut self, v: &[u8]) -> Result<(), io::Error>;
213}
214
215/// Extensions of `Read` to decode data as per Bitcoin consensus.
216pub trait ReadExt: Read {
217    /// Reads a 64-bit unsigned integer.
218    fn read_u64(&mut self) -> Result<u64, Error>;
219    /// Reads a 32-bit unsigned integer.
220    fn read_u32(&mut self) -> Result<u32, Error>;
221    /// Reads a 16-bit unsigned integer.
222    fn read_u16(&mut self) -> Result<u16, Error>;
223    /// Reads an 8-bit unsigned integer.
224    fn read_u8(&mut self) -> Result<u8, Error>;
225
226    /// Reads a 64-bit signed integer.
227    fn read_i64(&mut self) -> Result<i64, Error>;
228    /// Reads a 32-bit signed integer.
229    fn read_i32(&mut self) -> Result<i32, Error>;
230    /// Reads a 16-bit signed integer.
231    fn read_i16(&mut self) -> Result<i16, Error>;
232    /// Reads an 8-bit signed integer.
233    fn read_i8(&mut self) -> Result<i8, Error>;
234
235    /// Reads a boolean.
236    fn read_bool(&mut self) -> Result<bool, Error>;
237
238    /// Reads a byte slice.
239    fn read_slice(&mut self, slice: &mut [u8]) -> Result<(), Error>;
240}
241
242macro_rules! encoder_fn {
243    ($name:ident, $val_type:ty) => {
244        #[inline]
245        fn $name(&mut self, v: $val_type) -> core::result::Result<(), io::Error> {
246            self.write_all(&v.to_le_bytes())
247        }
248    };
249}
250
251macro_rules! decoder_fn {
252    ($name:ident, $val_type:ty, $byte_len: expr) => {
253        #[inline]
254        fn $name(&mut self) -> core::result::Result<$val_type, Error> {
255            let mut val = [0; $byte_len];
256            self.read_exact(&mut val[..]).map_err(Error::Io)?;
257            Ok(<$val_type>::from_le_bytes(val))
258        }
259    };
260}
261
262impl<W: Write + ?Sized> WriteExt for W {
263    encoder_fn!(emit_u64, u64);
264    encoder_fn!(emit_u32, u32);
265    encoder_fn!(emit_u16, u16);
266    encoder_fn!(emit_i64, i64);
267    encoder_fn!(emit_i32, i32);
268    encoder_fn!(emit_i16, i16);
269
270    #[inline]
271    fn emit_i8(&mut self, v: i8) -> Result<(), io::Error> { self.write_all(&[v as u8]) }
272    #[inline]
273    fn emit_u8(&mut self, v: u8) -> Result<(), io::Error> { self.write_all(&[v]) }
274    #[inline]
275    fn emit_bool(&mut self, v: bool) -> Result<(), io::Error> { self.write_all(&[v as u8]) }
276    #[inline]
277    fn emit_slice(&mut self, v: &[u8]) -> Result<(), io::Error> { self.write_all(v) }
278}
279
280impl<R: Read + ?Sized> ReadExt for R {
281    decoder_fn!(read_u64, u64, 8);
282    decoder_fn!(read_u32, u32, 4);
283    decoder_fn!(read_u16, u16, 2);
284    decoder_fn!(read_i64, i64, 8);
285    decoder_fn!(read_i32, i32, 4);
286    decoder_fn!(read_i16, i16, 2);
287
288    #[inline]
289    fn read_u8(&mut self) -> Result<u8, Error> {
290        let mut slice = [0u8; 1];
291        self.read_exact(&mut slice)?;
292        Ok(slice[0])
293    }
294    #[inline]
295    fn read_i8(&mut self) -> Result<i8, Error> {
296        let mut slice = [0u8; 1];
297        self.read_exact(&mut slice)?;
298        Ok(slice[0] as i8)
299    }
300    #[inline]
301    fn read_bool(&mut self) -> Result<bool, Error> { ReadExt::read_i8(self).map(|bit| bit != 0) }
302    #[inline]
303    fn read_slice(&mut self, slice: &mut [u8]) -> Result<(), Error> {
304        self.read_exact(slice).map_err(Error::Io)
305    }
306}
307
308/// Maximum size, in bytes, of a vector we are allowed to decode.
309pub const MAX_VEC_SIZE: usize = 4_000_000;
310
311/// Data which can be encoded in a consensus-consistent way.
312pub trait Encodable {
313    /// Encodes an object with a well-defined format.
314    ///
315    /// # Returns
316    ///
317    /// The number of bytes written on success. The only errors returned are errors propagated from
318    /// the writer.
319    fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error>;
320}
321
322/// Data which can be encoded in a consensus-consistent way.
323pub trait Decodable: Sized {
324    /// Decode `Self` from a size-limited reader.
325    ///
326    /// Like `consensus_decode` but relies on the reader being limited in the amount of data it
327    /// returns, e.g. by being wrapped in [`std::io::Take`].
328    ///
329    /// Failing to abide to this requirement might lead to memory exhaustion caused by malicious
330    /// inputs.
331    ///
332    /// Users should default to `consensus_decode`, but when data to be decoded is already in a byte
333    /// vector of a limited size, calling this function directly might be marginally faster (due to
334    /// avoiding extra checks).
335    ///
336    /// ### Rules for trait implementations
337    ///
338    /// * Simple types that that have a fixed size (own and member fields), don't have to overwrite
339    ///   this method, or be concern with it.
340    /// * Types that deserialize using externally provided length should implement it:
341    ///   * Make `consensus_decode` forward to `consensus_decode_bytes_from_finite_reader` with the
342    ///     reader wrapped by `Take`. Failure to do so, without other forms of memory exhaustion
343    ///     protection might lead to resource exhaustion vulnerability.
344    ///   * Put a max cap on things like `Vec::with_capacity` to avoid oversized allocations, and
345    ///     rely on the reader running out of data, and collections reallocating on a legitimately
346    ///     oversized input data, instead of trying to enforce arbitrary length limits.
347    /// * Types that contain other types that implement custom
348    ///   `consensus_decode_from_finite_reader`, should also implement it applying same rules, and
349    ///   in addition make sure to call `consensus_decode_from_finite_reader` on all members, to
350    ///   avoid creating redundant `Take` wrappers. Failure to do so might result only in a tiny
351    ///   performance hit.
352    #[inline]
353    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(
354        reader: &mut R,
355    ) -> Result<Self, Error> {
356        // This method is always strictly less general than, `consensus_decode`, so it's safe and
357        // make sense to default to just calling it. This way most types, that don't care about
358        // protecting against resource exhaustion due to malicious input, can just ignore it.
359        Self::consensus_decode(reader)
360    }
361
362    /// Decode an object with a well-defined format.
363    ///
364    /// This is the method that should be implemented for a typical, fixed sized type
365    /// implementing this trait. Default implementation is wrapping the reader
366    /// in [`crate::io::Take`] to limit the input size to [`MAX_VEC_SIZE`], and forwards the call to
367    /// [`Self::consensus_decode_from_finite_reader`], which is convenient
368    /// for types that override [`Self::consensus_decode_from_finite_reader`]
369    /// instead.
370    #[inline]
371    fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
372        Self::consensus_decode_from_finite_reader(&mut reader.take(MAX_VEC_SIZE as u64))
373    }
374}
375
376/// A variable-length unsigned integer.
377#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
378pub struct VarInt(pub u64);
379
380/// Data and a 4-byte checksum.
381#[derive(PartialEq, Eq, Clone, Debug)]
382pub struct CheckedData {
383    data: Vec<u8>,
384    checksum: [u8; 4],
385}
386
387impl CheckedData {
388    /// Creates a new `CheckedData` computing the checksum of given data.
389    pub fn new(data: Vec<u8>) -> Self {
390        let checksum = sha2_checksum(&data);
391        Self { data, checksum }
392    }
393
394    /// Returns a reference to the raw data without the checksum.
395    pub fn data(&self) -> &[u8] { &self.data }
396
397    /// Returns the raw data without the checksum.
398    pub fn into_data(self) -> Vec<u8> { self.data }
399
400    /// Returns the checksum of the data.
401    pub fn checksum(&self) -> [u8; 4] { self.checksum }
402}
403
404// Primitive types
405macro_rules! impl_int_encodable {
406    ($ty:ident, $meth_dec:ident, $meth_enc:ident) => {
407        impl Decodable for $ty {
408            #[inline]
409            fn consensus_decode<R: Read + ?Sized>(
410                r: &mut R,
411            ) -> core::result::Result<Self, Error> {
412                ReadExt::$meth_dec(r)
413            }
414        }
415        impl Encodable for $ty {
416            #[inline]
417            fn consensus_encode<W: Write + ?Sized>(
418                &self,
419                w: &mut W,
420            ) -> core::result::Result<usize, io::Error> {
421                w.$meth_enc(*self)?;
422                Ok(mem::size_of::<$ty>())
423            }
424        }
425    };
426}
427
428impl_int_encodable!(u8, read_u8, emit_u8);
429impl_int_encodable!(u16, read_u16, emit_u16);
430impl_int_encodable!(u32, read_u32, emit_u32);
431impl_int_encodable!(u64, read_u64, emit_u64);
432impl_int_encodable!(i8, read_i8, emit_i8);
433impl_int_encodable!(i16, read_i16, emit_i16);
434impl_int_encodable!(i32, read_i32, emit_i32);
435impl_int_encodable!(i64, read_i64, emit_i64);
436
437#[allow(clippy::len_without_is_empty)] // VarInt has on concept of 'is_empty'.
438impl VarInt {
439    /// Returns the number of bytes this varint contributes to a transaction size.
440    ///
441    /// Returns 1 for 0..=0xFC, 3 for 0xFD..=(2^16-1), 5 for 0x10000..=(2^32-1), and 9 otherwise.
442    #[inline]
443    pub const fn size(&self) -> usize {
444        match self.0 {
445            0..=0xFC => 1,
446            0xFD..=0xFFFF => 3,
447            0x10000..=0xFFFFFFFF => 5,
448            _ => 9,
449        }
450    }
451}
452
453/// Implements `From<T> for VarInt`.
454///
455/// `VarInt`s are consensus encoded as `u64`s so we store them as such. Casting from any integer size smaller than or equal to `u64` is always safe and the cast value is correctly handled by `consensus_encode`.
456macro_rules! impl_var_int_from {
457    ($($ty:tt),*) => {
458        $(
459            /// Creates a `VarInt` from a `usize` by casting the to a `u64`.
460            impl From<$ty> for VarInt {
461                fn from(x: $ty) -> Self { VarInt(x as u64) }
462            }
463        )*
464    }
465}
466impl_var_int_from!(u8, u16, u32, u64, usize);
467
468impl Encodable for VarInt {
469    #[inline]
470    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
471        match self.0 {
472            0..=0xFC => {
473                (self.0 as u8).consensus_encode(w)?;
474                Ok(1)
475            }
476            0xFD..=0xFFFF => {
477                w.emit_u8(0xFD)?;
478                (self.0 as u16).consensus_encode(w)?;
479                Ok(3)
480            }
481            0x10000..=0xFFFFFFFF => {
482                w.emit_u8(0xFE)?;
483                (self.0 as u32).consensus_encode(w)?;
484                Ok(5)
485            }
486            _ => {
487                w.emit_u8(0xFF)?;
488                self.0.consensus_encode(w)?;
489                Ok(9)
490            }
491        }
492    }
493}
494
495impl Decodable for VarInt {
496    #[inline]
497    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
498        let n = ReadExt::read_u8(r)?;
499        match n {
500            0xFF => {
501                let x = ReadExt::read_u64(r)?;
502                if x < 0x100000000 {
503                    Err(self::Error::NonMinimalVarInt)
504                } else {
505                    Ok(VarInt::from(x))
506                }
507            }
508            0xFE => {
509                let x = ReadExt::read_u32(r)?;
510                if x < 0x10000 {
511                    Err(self::Error::NonMinimalVarInt)
512                } else {
513                    Ok(VarInt::from(x))
514                }
515            }
516            0xFD => {
517                let x = ReadExt::read_u16(r)?;
518                if x < 0xFD {
519                    Err(self::Error::NonMinimalVarInt)
520                } else {
521                    Ok(VarInt::from(x))
522                }
523            }
524            n => Ok(VarInt::from(n)),
525        }
526    }
527}
528
529impl Encodable for bool {
530    #[inline]
531    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
532        w.emit_bool(*self)?;
533        Ok(1)
534    }
535}
536
537impl Decodable for bool {
538    #[inline]
539    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<bool, Error> {
540        ReadExt::read_bool(r)
541    }
542}
543
544impl Encodable for String {
545    #[inline]
546    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
547        let b = self.as_bytes();
548        let vi_len = VarInt(b.len() as u64).consensus_encode(w)?;
549        w.emit_slice(b)?;
550        Ok(vi_len + b.len())
551    }
552}
553
554impl Decodable for String {
555    #[inline]
556    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<String, Error> {
557        String::from_utf8(Decodable::consensus_decode(r)?)
558            .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
559    }
560}
561
562impl Encodable for Cow<'static, str> {
563    #[inline]
564    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
565        let b = self.as_bytes();
566        let vi_len = VarInt(b.len() as u64).consensus_encode(w)?;
567        w.emit_slice(b)?;
568        Ok(vi_len + b.len())
569    }
570}
571
572impl Decodable for Cow<'static, str> {
573    #[inline]
574    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Cow<'static, str>, Error> {
575        String::from_utf8(Decodable::consensus_decode(r)?)
576            .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
577            .map(Cow::Owned)
578    }
579}
580
581macro_rules! impl_array {
582    ( $size:literal ) => {
583        impl Encodable for [u8; $size] {
584            #[inline]
585            fn consensus_encode<W: WriteExt + ?Sized>(
586                &self,
587                w: &mut W,
588            ) -> core::result::Result<usize, io::Error> {
589                w.emit_slice(&self[..])?;
590                Ok(self.len())
591            }
592        }
593
594        impl Decodable for [u8; $size] {
595            #[inline]
596            fn consensus_decode<R: Read + ?Sized>(
597                r: &mut R,
598            ) -> core::result::Result<Self, Error> {
599                let mut ret = [0; $size];
600                r.read_slice(&mut ret)?;
601                Ok(ret)
602            }
603        }
604    };
605}
606
607impl_array!(2);
608impl_array!(4);
609impl_array!(6);
610impl_array!(8);
611impl_array!(10);
612impl_array!(12);
613impl_array!(16);
614impl_array!(32);
615impl_array!(33);
616
617impl Decodable for [u16; 8] {
618    #[inline]
619    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
620        let mut res = [0; 8];
621        for item in &mut res {
622            *item = Decodable::consensus_decode(r)?;
623        }
624        Ok(res)
625    }
626}
627
628impl Encodable for [u16; 8] {
629    #[inline]
630    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
631        for c in self.iter() {
632            c.consensus_encode(w)?;
633        }
634        Ok(16)
635    }
636}
637
638macro_rules! impl_vec {
639    ($type: ty) => {
640        impl Encodable for Vec<$type> {
641            #[inline]
642            fn consensus_encode<W: Write + ?Sized>(
643                &self,
644                w: &mut W,
645            ) -> core::result::Result<usize, io::Error> {
646                let mut len = 0;
647                len += VarInt(self.len() as u64).consensus_encode(w)?;
648                for c in self.iter() {
649                    len += c.consensus_encode(w)?;
650                }
651                Ok(len)
652            }
653        }
654
655        impl Decodable for Vec<$type> {
656            #[inline]
657            fn consensus_decode_from_finite_reader<R: Read + ?Sized>(
658                r: &mut R,
659            ) -> core::result::Result<Self, Error> {
660                let len = VarInt::consensus_decode_from_finite_reader(r)?.0;
661                // Do not allocate upfront more items than if the sequence of type
662                // occupied roughly quarter a block. This should never be the case
663                // for normal data, but even if that's not true - `push` will just
664                // reallocate.
665                // Note: OOM protection relies on reader eventually running out of
666                // data to feed us.
667                let max_capacity = MAX_VEC_SIZE / 4 / mem::size_of::<$type>();
668                let mut ret = Vec::with_capacity(core::cmp::min(len as usize, max_capacity));
669                for _ in 0..len {
670                    ret.push(Decodable::consensus_decode_from_finite_reader(r)?);
671                }
672                Ok(ret)
673            }
674        }
675    };
676}
677impl_vec!(BlockHash);
678impl_vec!(block::Header);
679impl_vec!(FilterHash);
680impl_vec!(FilterHeader);
681impl_vec!(TxMerkleNode);
682impl_vec!(Transaction);
683impl_vec!(TxOut);
684impl_vec!(TxIn);
685impl_vec!(Vec<u8>);
686impl_vec!(u64);
687impl_vec!(TapLeafHash);
688impl_vec!(VarInt);
689impl_vec!(ShortId);
690impl_vec!(PrefilledTransaction);
691
692#[cfg(feature = "std")]
693impl_vec!(Inventory);
694#[cfg(feature = "std")]
695impl_vec!((u32, Address));
696#[cfg(feature = "std")]
697impl_vec!(AddrV2Message);
698
699pub(crate) fn consensus_encode_with_size<W: Write + ?Sized>(
700    data: &[u8],
701    w: &mut W,
702) -> Result<usize, io::Error> {
703    let vi_len = VarInt(data.len() as u64).consensus_encode(w)?;
704    w.emit_slice(data)?;
705    Ok(vi_len + data.len())
706}
707
708struct ReadBytesFromFiniteReaderOpts {
709    len: usize,
710    chunk_size: usize,
711}
712
713/// Read `opts.len` bytes from reader, where `opts.len` could potentially be malicious.
714///
715/// This function relies on reader being bound in amount of data
716/// it returns for OOM protection. See [`Decodable::consensus_decode_from_finite_reader`].
717#[inline]
718fn read_bytes_from_finite_reader<D: Read + ?Sized>(
719    d: &mut D,
720    mut opts: ReadBytesFromFiniteReaderOpts,
721) -> Result<Vec<u8>, Error> {
722    let mut ret = vec![];
723
724    assert_ne!(opts.chunk_size, 0);
725
726    while opts.len > 0 {
727        let chunk_start = ret.len();
728        let chunk_size = core::cmp::min(opts.len, opts.chunk_size);
729        let chunk_end = chunk_start + chunk_size;
730        ret.resize(chunk_end, 0u8);
731        d.read_slice(&mut ret[chunk_start..chunk_end])?;
732        opts.len -= chunk_size;
733    }
734
735    Ok(ret)
736}
737
738impl Encodable for Vec<u8> {
739    #[inline]
740    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
741        consensus_encode_with_size(self, w)
742    }
743}
744
745impl Decodable for Vec<u8> {
746    #[inline]
747    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
748        let len = VarInt::consensus_decode(r)?.0 as usize;
749        // most real-world vec of bytes data, wouldn't be larger than 128KiB
750        let opts = ReadBytesFromFiniteReaderOpts { len, chunk_size: 128 * 1024 };
751        read_bytes_from_finite_reader(r, opts)
752    }
753}
754
755impl Encodable for Box<[u8]> {
756    #[inline]
757    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
758        consensus_encode_with_size(self, w)
759    }
760}
761
762impl Decodable for Box<[u8]> {
763    #[inline]
764    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
765        <Vec<u8>>::consensus_decode_from_finite_reader(r).map(From::from)
766    }
767}
768
769/// Does a double-SHA256 on `data` and returns the first 4 bytes.
770fn sha2_checksum(data: &[u8]) -> [u8; 4] {
771    let checksum = <sha256d::Hash as Hash>::hash(data);
772    [checksum[0], checksum[1], checksum[2], checksum[3]]
773}
774
775impl Encodable for CheckedData {
776    #[inline]
777    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
778        u32::try_from(self.data.len())
779            .expect("network message use u32 as length")
780            .consensus_encode(w)?;
781        self.checksum().consensus_encode(w)?;
782        w.emit_slice(&self.data)?;
783        Ok(8 + self.data.len())
784    }
785}
786
787impl Decodable for CheckedData {
788    #[inline]
789    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
790        let len = u32::consensus_decode_from_finite_reader(r)? as usize;
791
792        let checksum = <[u8; 4]>::consensus_decode_from_finite_reader(r)?;
793        let opts = ReadBytesFromFiniteReaderOpts { len, chunk_size: MAX_VEC_SIZE };
794        let data = read_bytes_from_finite_reader(r, opts)?;
795        let expected_checksum = sha2_checksum(&data);
796        if expected_checksum != checksum {
797            Err(self::Error::InvalidChecksum { expected: expected_checksum, actual: checksum })
798        } else {
799            Ok(CheckedData { data, checksum })
800        }
801    }
802}
803
804impl<'a, T: Encodable> Encodable for &'a T {
805    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
806        (**self).consensus_encode(w)
807    }
808}
809
810impl<'a, T: Encodable> Encodable for &'a mut T {
811    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
812        (**self).consensus_encode(w)
813    }
814}
815
816impl<T: Encodable> Encodable for rc::Rc<T> {
817    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
818        (**self).consensus_encode(w)
819    }
820}
821
822/// Note: This will fail to compile on old Rust for targets that don't support atomics
823#[cfg(any(not(rust_v_1_60), target_has_atomic = "ptr"))]
824impl<T: Encodable> Encodable for sync::Arc<T> {
825    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
826        (**self).consensus_encode(w)
827    }
828}
829
830macro_rules! tuple_encode {
831    ($($x:ident),*) => {
832        impl <$($x: Encodable),*> Encodable for ($($x),*) {
833            #[inline]
834            #[allow(non_snake_case)]
835            fn consensus_encode<W: Write + ?Sized>(
836                &self,
837                w: &mut W,
838            ) -> core::result::Result<usize, io::Error> {
839                let &($(ref $x),*) = self;
840                let mut len = 0;
841                $(len += $x.consensus_encode(w)?;)*
842                Ok(len)
843            }
844        }
845
846        impl<$($x: Decodable),*> Decodable for ($($x),*) {
847            #[inline]
848            #[allow(non_snake_case)]
849            fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> core::result::Result<Self, Error> {
850                Ok(($({let $x = Decodable::consensus_decode(r)?; $x }),*))
851            }
852        }
853    };
854}
855
856tuple_encode!(T0, T1);
857tuple_encode!(T0, T1, T2);
858tuple_encode!(T0, T1, T2, T3);
859tuple_encode!(T0, T1, T2, T3, T4);
860tuple_encode!(T0, T1, T2, T3, T4, T5);
861tuple_encode!(T0, T1, T2, T3, T4, T5, T6);
862tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
863
864impl Encodable for sha256d::Hash {
865    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
866        self.as_byte_array().consensus_encode(w)
867    }
868}
869
870impl Decodable for sha256d::Hash {
871    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
872        Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
873    }
874}
875
876impl Encodable for sha256::Hash {
877    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
878        self.as_byte_array().consensus_encode(w)
879    }
880}
881
882impl Decodable for sha256::Hash {
883    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
884        Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
885    }
886}
887
888impl Encodable for TapLeafHash {
889    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
890        self.as_byte_array().consensus_encode(w)
891    }
892}
893
894impl Decodable for TapLeafHash {
895    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
896        Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
897    }
898}
899
900#[cfg(test)]
901mod tests {
902    use core::mem::discriminant;
903
904    use super::*;
905
906    #[test]
907    fn serialize_int_test() {
908        // bool
909        assert_eq!(serialize(&false), vec![0u8]);
910        assert_eq!(serialize(&true), vec![1u8]);
911        // u8
912        assert_eq!(serialize(&1u8), vec![1u8]);
913        assert_eq!(serialize(&0u8), vec![0u8]);
914        assert_eq!(serialize(&255u8), vec![255u8]);
915        // u16
916        assert_eq!(serialize(&1u16), vec![1u8, 0]);
917        assert_eq!(serialize(&256u16), vec![0u8, 1]);
918        assert_eq!(serialize(&5000u16), vec![136u8, 19]);
919        // u32
920        assert_eq!(serialize(&1u32), vec![1u8, 0, 0, 0]);
921        assert_eq!(serialize(&256u32), vec![0u8, 1, 0, 0]);
922        assert_eq!(serialize(&5000u32), vec![136u8, 19, 0, 0]);
923        assert_eq!(serialize(&500000u32), vec![32u8, 161, 7, 0]);
924        assert_eq!(serialize(&168430090u32), vec![10u8, 10, 10, 10]);
925        // i32
926        assert_eq!(serialize(&-1i32), vec![255u8, 255, 255, 255]);
927        assert_eq!(serialize(&-256i32), vec![0u8, 255, 255, 255]);
928        assert_eq!(serialize(&-5000i32), vec![120u8, 236, 255, 255]);
929        assert_eq!(serialize(&-500000i32), vec![224u8, 94, 248, 255]);
930        assert_eq!(serialize(&-168430090i32), vec![246u8, 245, 245, 245]);
931        assert_eq!(serialize(&1i32), vec![1u8, 0, 0, 0]);
932        assert_eq!(serialize(&256i32), vec![0u8, 1, 0, 0]);
933        assert_eq!(serialize(&5000i32), vec![136u8, 19, 0, 0]);
934        assert_eq!(serialize(&500000i32), vec![32u8, 161, 7, 0]);
935        assert_eq!(serialize(&168430090i32), vec![10u8, 10, 10, 10]);
936        // u64
937        assert_eq!(serialize(&1u64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
938        assert_eq!(serialize(&256u64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
939        assert_eq!(serialize(&5000u64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
940        assert_eq!(serialize(&500000u64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
941        assert_eq!(serialize(&723401728380766730u64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
942        // i64
943        assert_eq!(serialize(&-1i64), vec![255u8, 255, 255, 255, 255, 255, 255, 255]);
944        assert_eq!(serialize(&-256i64), vec![0u8, 255, 255, 255, 255, 255, 255, 255]);
945        assert_eq!(serialize(&-5000i64), vec![120u8, 236, 255, 255, 255, 255, 255, 255]);
946        assert_eq!(serialize(&-500000i64), vec![224u8, 94, 248, 255, 255, 255, 255, 255]);
947        assert_eq!(
948            serialize(&-723401728380766730i64),
949            vec![246u8, 245, 245, 245, 245, 245, 245, 245]
950        );
951        assert_eq!(serialize(&1i64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
952        assert_eq!(serialize(&256i64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
953        assert_eq!(serialize(&5000i64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
954        assert_eq!(serialize(&500000i64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
955        assert_eq!(serialize(&723401728380766730i64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
956    }
957
958    #[test]
959    fn serialize_varint_test() {
960        assert_eq!(serialize(&VarInt(10)), vec![10u8]);
961        assert_eq!(serialize(&VarInt(0xFC)), vec![0xFCu8]);
962        assert_eq!(serialize(&VarInt(0xFD)), vec![0xFDu8, 0xFD, 0]);
963        assert_eq!(serialize(&VarInt(0xFFF)), vec![0xFDu8, 0xFF, 0xF]);
964        assert_eq!(serialize(&VarInt(0xF0F0F0F)), vec![0xFEu8, 0xF, 0xF, 0xF, 0xF]);
965        assert_eq!(
966            serialize(&VarInt(0xF0F0F0F0F0E0)),
967            vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0]
968        );
969        assert_eq!(
970            test_varint_encode(0xFF, &0x100000000_u64.to_le_bytes()).unwrap(),
971            VarInt(0x100000000)
972        );
973        assert_eq!(test_varint_encode(0xFE, &0x10000_u64.to_le_bytes()).unwrap(), VarInt(0x10000));
974        assert_eq!(test_varint_encode(0xFD, &0xFD_u64.to_le_bytes()).unwrap(), VarInt(0xFD));
975
976        // Test that length calc is working correctly
977        test_varint_len(VarInt(0), 1);
978        test_varint_len(VarInt(0xFC), 1);
979        test_varint_len(VarInt(0xFD), 3);
980        test_varint_len(VarInt(0xFFFF), 3);
981        test_varint_len(VarInt(0x10000), 5);
982        test_varint_len(VarInt(0xFFFFFFFF), 5);
983        test_varint_len(VarInt(0xFFFFFFFF + 1), 9);
984        test_varint_len(VarInt(u64::MAX), 9);
985    }
986
987    fn test_varint_len(varint: VarInt, expected: usize) {
988        let mut encoder = vec![];
989        assert_eq!(varint.consensus_encode(&mut encoder).unwrap(), expected);
990        assert_eq!(varint.size(), expected);
991    }
992
993    fn test_varint_encode(n: u8, x: &[u8]) -> Result<VarInt, Error> {
994        let mut input = [0u8; 9];
995        input[0] = n;
996        input[1..x.len() + 1].copy_from_slice(x);
997        deserialize_partial::<VarInt>(&input).map(|t| t.0)
998    }
999
1000    #[test]
1001    fn deserialize_nonminimal_vec() {
1002        // Check the edges for variant int
1003        assert_eq!(
1004            discriminant(
1005                &test_varint_encode(0xFF, &(0x100000000_u64 - 1).to_le_bytes()).unwrap_err()
1006            ),
1007            discriminant(&Error::NonMinimalVarInt)
1008        );
1009        assert_eq!(
1010            discriminant(&test_varint_encode(0xFE, &(0x10000_u64 - 1).to_le_bytes()).unwrap_err()),
1011            discriminant(&Error::NonMinimalVarInt)
1012        );
1013        assert_eq!(
1014            discriminant(&test_varint_encode(0xFD, &(0xFD_u64 - 1).to_le_bytes()).unwrap_err()),
1015            discriminant(&Error::NonMinimalVarInt)
1016        );
1017
1018        assert_eq!(
1019            discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0x00, 0x00]).unwrap_err()),
1020            discriminant(&Error::NonMinimalVarInt)
1021        );
1022        assert_eq!(
1023            discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]).unwrap_err()),
1024            discriminant(&Error::NonMinimalVarInt)
1025        );
1026        assert_eq!(
1027            discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]).unwrap_err()),
1028            discriminant(&Error::NonMinimalVarInt)
1029        );
1030        assert_eq!(
1031            discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0x00, 0x00, 0x00]).unwrap_err()),
1032            discriminant(&Error::NonMinimalVarInt)
1033        );
1034        assert_eq!(
1035            discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0xff, 0x00, 0x00]).unwrap_err()),
1036            discriminant(&Error::NonMinimalVarInt)
1037        );
1038        assert_eq!(
1039            discriminant(
1040                &deserialize::<Vec<u8>>(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
1041                    .unwrap_err()
1042            ),
1043            discriminant(&Error::NonMinimalVarInt)
1044        );
1045        assert_eq!(
1046            discriminant(
1047                &deserialize::<Vec<u8>>(&[0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00])
1048                    .unwrap_err()
1049            ),
1050            discriminant(&Error::NonMinimalVarInt)
1051        );
1052
1053        let mut vec_256 = vec![0; 259];
1054        vec_256[0] = 0xfd;
1055        vec_256[1] = 0x00;
1056        vec_256[2] = 0x01;
1057        assert!(deserialize::<Vec<u8>>(&vec_256).is_ok());
1058
1059        let mut vec_253 = vec![0; 256];
1060        vec_253[0] = 0xfd;
1061        vec_253[1] = 0xfd;
1062        vec_253[2] = 0x00;
1063        assert!(deserialize::<Vec<u8>>(&vec_253).is_ok());
1064    }
1065
1066    #[test]
1067    fn serialize_checkeddata_test() {
1068        let cd = CheckedData::new(vec![1u8, 2, 3, 4, 5]);
1069        assert_eq!(serialize(&cd), vec![5, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
1070    }
1071
1072    #[test]
1073    fn serialize_vector_test() {
1074        assert_eq!(serialize(&vec![1u8, 2, 3]), vec![3u8, 1, 2, 3]);
1075    }
1076
1077    #[test]
1078    fn serialize_strbuf_test() {
1079        assert_eq!(serialize(&"Andrew".to_string()), vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]);
1080    }
1081
1082    #[test]
1083    fn deserialize_int_test() {
1084        // bool
1085        assert!((deserialize(&[58u8, 0]) as Result<bool, _>).is_err());
1086        assert_eq!(deserialize(&[58u8]).ok(), Some(true));
1087        assert_eq!(deserialize(&[1u8]).ok(), Some(true));
1088        assert_eq!(deserialize(&[0u8]).ok(), Some(false));
1089        assert!((deserialize(&[0u8, 1]) as Result<bool, _>).is_err());
1090
1091        // u8
1092        assert_eq!(deserialize(&[58u8]).ok(), Some(58u8));
1093
1094        // u16
1095        assert_eq!(deserialize(&[0x01u8, 0x02]).ok(), Some(0x0201u16));
1096        assert_eq!(deserialize(&[0xABu8, 0xCD]).ok(), Some(0xCDABu16));
1097        assert_eq!(deserialize(&[0xA0u8, 0x0D]).ok(), Some(0xDA0u16));
1098        let failure16: Result<u16, _> = deserialize(&[1u8]);
1099        assert!(failure16.is_err());
1100
1101        // i16
1102        assert_eq!(deserialize(&[0x32_u8, 0xF4]).ok(), Some(-0x0bce_i16));
1103        assert_eq!(deserialize(&[0xFF_u8, 0xFE]).ok(), Some(-0x0101_i16));
1104        assert_eq!(deserialize(&[0x00_u8, 0x00]).ok(), Some(-0_i16));
1105        assert_eq!(deserialize(&[0xFF_u8, 0xFA]).ok(), Some(-0x0501_i16));
1106
1107        // u32
1108        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABu32));
1109        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD]).ok(), Some(0xCDAB0DA0u32));
1110
1111        let failure32: Result<u32, _> = deserialize(&[1u8, 2, 3]);
1112        assert!(failure32.is_err());
1113
1114        // i32
1115        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABi32));
1116        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0x2D]).ok(), Some(0x2DAB0DA0i32));
1117
1118        assert_eq!(deserialize(&[0, 0, 0, 0]).ok(), Some(-0_i32));
1119        assert_eq!(deserialize(&[0, 0, 0, 0]).ok(), Some(0_i32));
1120
1121        assert_eq!(deserialize(&[0xFF, 0xFF, 0xFF, 0xFF]).ok(), Some(-1_i32));
1122        assert_eq!(deserialize(&[0xFE, 0xFF, 0xFF, 0xFF]).ok(), Some(-2_i32));
1123        assert_eq!(deserialize(&[0x01, 0xFF, 0xFF, 0xFF]).ok(), Some(-255_i32));
1124        assert_eq!(deserialize(&[0x02, 0xFF, 0xFF, 0xFF]).ok(), Some(-254_i32));
1125
1126        let failurei32: Result<i32, _> = deserialize(&[1u8, 2, 3]);
1127        assert!(failurei32.is_err());
1128
1129        // u64
1130        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABu64));
1131        assert_eq!(
1132            deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(),
1133            Some(0x99000099CDAB0DA0u64)
1134        );
1135        let failure64: Result<u64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
1136        assert!(failure64.is_err());
1137
1138        // i64
1139        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABi64));
1140        assert_eq!(
1141            deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(),
1142            Some(-0x66ffff663254f260i64)
1143        );
1144        assert_eq!(
1145            deserialize(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1146            Some(-1_i64)
1147        );
1148        assert_eq!(
1149            deserialize(&[0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1150            Some(-2_i64)
1151        );
1152        assert_eq!(
1153            deserialize(&[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1154            Some(-255_i64)
1155        );
1156        assert_eq!(
1157            deserialize(&[0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1158            Some(-254_i64)
1159        );
1160
1161        let failurei64: Result<i64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
1162        assert!(failurei64.is_err());
1163    }
1164
1165    #[test]
1166    fn deserialize_vec_test() {
1167        assert_eq!(deserialize(&[3u8, 2, 3, 4]).ok(), Some(vec![2u8, 3, 4]));
1168        assert!((deserialize(&[4u8, 2, 3, 4, 5, 6]) as Result<Vec<u8>, _>).is_err());
1169        // found by cargo fuzz
1170        assert!(deserialize::<Vec<u64>>(&[
1171            0xff, 0xff, 0xff, 0xff, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b,
1172            0x6b, 0x6b, 0xa, 0xa, 0x3a
1173        ])
1174        .is_err());
1175
1176        let rand_io_err = Error::Io(io::Error::new(io::ErrorKind::Other, ""));
1177
1178        // Check serialization that `if len > MAX_VEC_SIZE {return err}` isn't inclusive,
1179        // by making sure it fails with IO Error and not an `OversizedVectorAllocation` Error.
1180        let err =
1181            deserialize::<CheckedData>(&serialize(&(super::MAX_VEC_SIZE as u32))).unwrap_err();
1182        assert_eq!(discriminant(&err), discriminant(&rand_io_err));
1183
1184        test_len_is_max_vec::<u8>();
1185        test_len_is_max_vec::<BlockHash>();
1186        test_len_is_max_vec::<FilterHash>();
1187        test_len_is_max_vec::<TxMerkleNode>();
1188        test_len_is_max_vec::<Transaction>();
1189        test_len_is_max_vec::<TxOut>();
1190        test_len_is_max_vec::<TxIn>();
1191        test_len_is_max_vec::<Vec<u8>>();
1192        test_len_is_max_vec::<u64>();
1193        #[cfg(feature = "std")]
1194        test_len_is_max_vec::<(u32, Address)>();
1195        #[cfg(feature = "std")]
1196        test_len_is_max_vec::<Inventory>();
1197    }
1198
1199    fn test_len_is_max_vec<T>()
1200    where
1201        Vec<T>: Decodable,
1202        T: fmt::Debug,
1203    {
1204        let rand_io_err = Error::Io(io::Error::new(io::ErrorKind::Other, ""));
1205        let varint = VarInt((super::MAX_VEC_SIZE / mem::size_of::<T>()) as u64);
1206        let err = deserialize::<Vec<T>>(&serialize(&varint)).unwrap_err();
1207        assert_eq!(discriminant(&err), discriminant(&rand_io_err));
1208    }
1209
1210    #[test]
1211    fn deserialize_strbuf_test() {
1212        assert_eq!(
1213            deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(),
1214            Some("Andrew".to_string())
1215        );
1216        assert_eq!(
1217            deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(),
1218            Some(Cow::Borrowed("Andrew"))
1219        );
1220    }
1221
1222    #[test]
1223    fn deserialize_checkeddata_test() {
1224        let cd: Result<CheckedData, _> =
1225            deserialize(&[5u8, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
1226        assert_eq!(cd.ok(), Some(CheckedData::new(vec![1u8, 2, 3, 4, 5])));
1227    }
1228
1229    #[test]
1230    fn limit_read_test() {
1231        let witness = vec![vec![0u8; 3_999_999]; 2];
1232        let ser = serialize(&witness);
1233        let mut reader = io::Cursor::new(ser);
1234        let err = Vec::<Vec<u8>>::consensus_decode(&mut reader);
1235        assert!(err.is_err());
1236    }
1237
1238    #[test]
1239    #[cfg(feature = "rand-std")]
1240    fn serialization_round_trips() {
1241        use secp256k1::rand::{thread_rng, Rng};
1242
1243        macro_rules! round_trip {
1244            ($($val_type:ty),*) => {
1245                $(
1246                    let r: $val_type = thread_rng().gen();
1247                    assert_eq!(deserialize::<$val_type>(&serialize(&r)).unwrap(), r);
1248                )*
1249            };
1250        }
1251        macro_rules! round_trip_bytes {
1252            ($(($val_type:ty, $data:expr)),*) => {
1253                $(
1254                    thread_rng().fill(&mut $data[..]);
1255                    assert_eq!(deserialize::<$val_type>(&serialize(&$data)).unwrap()[..], $data[..]);
1256                )*
1257            };
1258        }
1259
1260        let mut data = Vec::with_capacity(256);
1261        let mut data64 = Vec::with_capacity(256);
1262        for _ in 0..10 {
1263            round_trip! {bool, i8, u8, i16, u16, i32, u32, i64, u64,
1264            (bool, i8, u16, i32), (u64, i64, u32, i32, u16, i16), (i8, u8, i16, u16, i32, u32, i64, u64),
1265            [u8; 2], [u8; 4], [u8; 8], [u8; 12], [u8; 16], [u8; 32]};
1266
1267            data.clear();
1268            data64.clear();
1269            let len = thread_rng().gen_range(1..256);
1270            data.resize(len, 0u8);
1271            data64.resize(len, 0u64);
1272            let mut arr33 = [0u8; 33];
1273            let mut arr16 = [0u16; 8];
1274            round_trip_bytes! {(Vec<u8>, data), ([u8; 33], arr33), ([u16; 8], arr16), (Vec<u64>, data64)};
1275        }
1276    }
1277
1278    #[test]
1279    fn test_read_bytes_from_finite_reader() {
1280        let data: Vec<u8> = (0..10).collect();
1281
1282        for chunk_size in 1..20 {
1283            assert_eq!(
1284                read_bytes_from_finite_reader(
1285                    &mut io::Cursor::new(&data),
1286                    ReadBytesFromFiniteReaderOpts { len: data.len(), chunk_size }
1287                )
1288                .unwrap(),
1289                data
1290            );
1291        }
1292    }
1293
1294    #[test]
1295    fn deserialize_tx_hex() {
1296        let hex = include_str!("../../tests/data/previous_tx_0_hex"); // An arbitrary transaction.
1297        assert!(deserialize_hex::<Transaction>(hex).is_ok())
1298    }
1299
1300    #[test]
1301    fn deserialize_tx_hex_too_many_bytes() {
1302        use crate::consensus::DecodeError;
1303
1304        let mut hex = include_str!("../../tests/data/previous_tx_0_hex").to_string(); // An arbitrary transaction.
1305        hex.push_str("abcdef");
1306        assert!(matches!(
1307            deserialize_hex::<Transaction>(&hex).unwrap_err(),
1308            FromHexError::Decode(DecodeError::TooManyBytes)
1309        ));
1310    }
1311}