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>(r: &mut R) -> core::result::Result<Self, Error> {
410                ReadExt::$meth_dec(r)
411            }
412        }
413        impl Encodable for $ty {
414            #[inline]
415            fn consensus_encode<W: Write + ?Sized>(
416                &self,
417                w: &mut W,
418            ) -> core::result::Result<usize, io::Error> {
419                w.$meth_enc(*self)?;
420                Ok(mem::size_of::<$ty>())
421            }
422        }
423    };
424}
425
426impl_int_encodable!(u8, read_u8, emit_u8);
427impl_int_encodable!(u16, read_u16, emit_u16);
428impl_int_encodable!(u32, read_u32, emit_u32);
429impl_int_encodable!(u64, read_u64, emit_u64);
430impl_int_encodable!(i8, read_i8, emit_i8);
431impl_int_encodable!(i16, read_i16, emit_i16);
432impl_int_encodable!(i32, read_i32, emit_i32);
433impl_int_encodable!(i64, read_i64, emit_i64);
434
435#[allow(clippy::len_without_is_empty)] // VarInt has on concept of 'is_empty'.
436impl VarInt {
437    /// Returns the number of bytes this varint contributes to a transaction size.
438    ///
439    /// Returns 1 for 0..=0xFC, 3 for 0xFD..=(2^16-1), 5 for 0x10000..=(2^32-1), and 9 otherwise.
440    #[inline]
441    pub const fn size(&self) -> usize {
442        match self.0 {
443            0..=0xFC => 1,
444            0xFD..=0xFFFF => 3,
445            0x10000..=0xFFFFFFFF => 5,
446            _ => 9,
447        }
448    }
449}
450
451/// Implements `From<T> for VarInt`.
452///
453/// `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`.
454macro_rules! impl_var_int_from {
455    ($($ty:tt),*) => {
456        $(
457            /// Creates a `VarInt` from a `usize` by casting the to a `u64`.
458            impl From<$ty> for VarInt {
459                fn from(x: $ty) -> Self { VarInt(x as u64) }
460            }
461        )*
462    }
463}
464impl_var_int_from!(u8, u16, u32, u64, usize);
465
466impl Encodable for VarInt {
467    #[inline]
468    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
469        match self.0 {
470            0..=0xFC => {
471                (self.0 as u8).consensus_encode(w)?;
472                Ok(1)
473            }
474            0xFD..=0xFFFF => {
475                w.emit_u8(0xFD)?;
476                (self.0 as u16).consensus_encode(w)?;
477                Ok(3)
478            }
479            0x10000..=0xFFFFFFFF => {
480                w.emit_u8(0xFE)?;
481                (self.0 as u32).consensus_encode(w)?;
482                Ok(5)
483            }
484            _ => {
485                w.emit_u8(0xFF)?;
486                self.0.consensus_encode(w)?;
487                Ok(9)
488            }
489        }
490    }
491}
492
493impl Decodable for VarInt {
494    #[inline]
495    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
496        let n = ReadExt::read_u8(r)?;
497        match n {
498            0xFF => {
499                let x = ReadExt::read_u64(r)?;
500                if x < 0x100000000 {
501                    Err(self::Error::NonMinimalVarInt)
502                } else {
503                    Ok(VarInt::from(x))
504                }
505            }
506            0xFE => {
507                let x = ReadExt::read_u32(r)?;
508                if x < 0x10000 {
509                    Err(self::Error::NonMinimalVarInt)
510                } else {
511                    Ok(VarInt::from(x))
512                }
513            }
514            0xFD => {
515                let x = ReadExt::read_u16(r)?;
516                if x < 0xFD {
517                    Err(self::Error::NonMinimalVarInt)
518                } else {
519                    Ok(VarInt::from(x))
520                }
521            }
522            n => Ok(VarInt::from(n)),
523        }
524    }
525}
526
527impl Encodable for bool {
528    #[inline]
529    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
530        w.emit_bool(*self)?;
531        Ok(1)
532    }
533}
534
535impl Decodable for bool {
536    #[inline]
537    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<bool, Error> {
538        ReadExt::read_bool(r)
539    }
540}
541
542impl Encodable for String {
543    #[inline]
544    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
545        let b = self.as_bytes();
546        let vi_len = VarInt(b.len() as u64).consensus_encode(w)?;
547        w.emit_slice(b)?;
548        Ok(vi_len + b.len())
549    }
550}
551
552impl Decodable for String {
553    #[inline]
554    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<String, Error> {
555        String::from_utf8(Decodable::consensus_decode(r)?)
556            .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
557    }
558}
559
560impl Encodable for Cow<'static, str> {
561    #[inline]
562    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
563        let b = self.as_bytes();
564        let vi_len = VarInt(b.len() as u64).consensus_encode(w)?;
565        w.emit_slice(b)?;
566        Ok(vi_len + b.len())
567    }
568}
569
570impl Decodable for Cow<'static, str> {
571    #[inline]
572    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Cow<'static, str>, Error> {
573        String::from_utf8(Decodable::consensus_decode(r)?)
574            .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
575            .map(Cow::Owned)
576    }
577}
578
579macro_rules! impl_array {
580    ( $size:literal ) => {
581        impl Encodable for [u8; $size] {
582            #[inline]
583            fn consensus_encode<W: WriteExt + ?Sized>(
584                &self,
585                w: &mut W,
586            ) -> core::result::Result<usize, io::Error> {
587                w.emit_slice(&self[..])?;
588                Ok(self.len())
589            }
590        }
591
592        impl Decodable for [u8; $size] {
593            #[inline]
594            fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> core::result::Result<Self, Error> {
595                let mut ret = [0; $size];
596                r.read_slice(&mut ret)?;
597                Ok(ret)
598            }
599        }
600    };
601}
602
603impl_array!(2);
604impl_array!(4);
605impl_array!(6);
606impl_array!(8);
607impl_array!(10);
608impl_array!(12);
609impl_array!(16);
610impl_array!(32);
611impl_array!(33);
612
613impl Decodable for [u16; 8] {
614    #[inline]
615    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
616        let mut res = [0; 8];
617        for item in &mut res {
618            *item = Decodable::consensus_decode(r)?;
619        }
620        Ok(res)
621    }
622}
623
624impl Encodable for [u16; 8] {
625    #[inline]
626    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
627        for c in self.iter() {
628            c.consensus_encode(w)?;
629        }
630        Ok(16)
631    }
632}
633
634macro_rules! impl_vec {
635    ($type: ty) => {
636        impl Encodable for Vec<$type> {
637            #[inline]
638            fn consensus_encode<W: Write + ?Sized>(
639                &self,
640                w: &mut W,
641            ) -> core::result::Result<usize, io::Error> {
642                let mut len = 0;
643                len += VarInt(self.len() as u64).consensus_encode(w)?;
644                for c in self.iter() {
645                    len += c.consensus_encode(w)?;
646                }
647                Ok(len)
648            }
649        }
650
651        impl Decodable for Vec<$type> {
652            #[inline]
653            fn consensus_decode_from_finite_reader<R: Read + ?Sized>(
654                r: &mut R,
655            ) -> core::result::Result<Self, Error> {
656                let len = VarInt::consensus_decode_from_finite_reader(r)?.0;
657                // Do not allocate upfront more items than if the sequence of type
658                // occupied roughly quarter a block. This should never be the case
659                // for normal data, but even if that's not true - `push` will just
660                // reallocate.
661                // Note: OOM protection relies on reader eventually running out of
662                // data to feed us.
663                let max_capacity = MAX_VEC_SIZE / 4 / mem::size_of::<$type>();
664                let mut ret = Vec::with_capacity(core::cmp::min(len as usize, max_capacity));
665                for _ in 0..len {
666                    ret.push(Decodable::consensus_decode_from_finite_reader(r)?);
667                }
668                Ok(ret)
669            }
670        }
671    };
672}
673impl_vec!(BlockHash);
674impl_vec!(block::Header);
675impl_vec!(FilterHash);
676impl_vec!(FilterHeader);
677impl_vec!(TxMerkleNode);
678impl_vec!(Transaction);
679impl_vec!(TxOut);
680impl_vec!(TxIn);
681impl_vec!(Vec<u8>);
682impl_vec!(u64);
683impl_vec!(TapLeafHash);
684impl_vec!(VarInt);
685impl_vec!(ShortId);
686impl_vec!(PrefilledTransaction);
687
688#[cfg(feature = "std")]
689impl_vec!(Inventory);
690#[cfg(feature = "std")]
691impl_vec!((u32, Address));
692#[cfg(feature = "std")]
693impl_vec!(AddrV2Message);
694
695pub(crate) fn consensus_encode_with_size<W: Write + ?Sized>(
696    data: &[u8],
697    w: &mut W,
698) -> Result<usize, io::Error> {
699    let vi_len = VarInt(data.len() as u64).consensus_encode(w)?;
700    w.emit_slice(data)?;
701    Ok(vi_len + data.len())
702}
703
704struct ReadBytesFromFiniteReaderOpts {
705    len: usize,
706    chunk_size: usize,
707}
708
709/// Read `opts.len` bytes from reader, where `opts.len` could potentially be malicious.
710///
711/// This function relies on reader being bound in amount of data
712/// it returns for OOM protection. See [`Decodable::consensus_decode_from_finite_reader`].
713#[inline]
714fn read_bytes_from_finite_reader<D: Read + ?Sized>(
715    d: &mut D,
716    mut opts: ReadBytesFromFiniteReaderOpts,
717) -> Result<Vec<u8>, Error> {
718    let mut ret = vec![];
719
720    assert_ne!(opts.chunk_size, 0);
721
722    while opts.len > 0 {
723        let chunk_start = ret.len();
724        let chunk_size = core::cmp::min(opts.len, opts.chunk_size);
725        let chunk_end = chunk_start + chunk_size;
726        ret.resize(chunk_end, 0u8);
727        d.read_slice(&mut ret[chunk_start..chunk_end])?;
728        opts.len -= chunk_size;
729    }
730
731    Ok(ret)
732}
733
734impl Encodable for Vec<u8> {
735    #[inline]
736    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
737        consensus_encode_with_size(self, w)
738    }
739}
740
741impl Decodable for Vec<u8> {
742    #[inline]
743    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
744        let len = VarInt::consensus_decode(r)?.0 as usize;
745        // most real-world vec of bytes data, wouldn't be larger than 128KiB
746        let opts = ReadBytesFromFiniteReaderOpts { len, chunk_size: 128 * 1024 };
747        read_bytes_from_finite_reader(r, opts)
748    }
749}
750
751impl Encodable for Box<[u8]> {
752    #[inline]
753    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
754        consensus_encode_with_size(self, w)
755    }
756}
757
758impl Decodable for Box<[u8]> {
759    #[inline]
760    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
761        <Vec<u8>>::consensus_decode_from_finite_reader(r).map(From::from)
762    }
763}
764
765/// Does a double-SHA256 on `data` and returns the first 4 bytes.
766fn sha2_checksum(data: &[u8]) -> [u8; 4] {
767    let checksum = <sha256d::Hash as Hash>::hash(data);
768    [checksum[0], checksum[1], checksum[2], checksum[3]]
769}
770
771impl Encodable for CheckedData {
772    #[inline]
773    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
774        u32::try_from(self.data.len())
775            .expect("network message use u32 as length")
776            .consensus_encode(w)?;
777        self.checksum().consensus_encode(w)?;
778        w.emit_slice(&self.data)?;
779        Ok(8 + self.data.len())
780    }
781}
782
783impl Decodable for CheckedData {
784    #[inline]
785    fn consensus_decode_from_finite_reader<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
786        let len = u32::consensus_decode_from_finite_reader(r)? as usize;
787
788        let checksum = <[u8; 4]>::consensus_decode_from_finite_reader(r)?;
789        let opts = ReadBytesFromFiniteReaderOpts { len, chunk_size: MAX_VEC_SIZE };
790        let data = read_bytes_from_finite_reader(r, opts)?;
791        let expected_checksum = sha2_checksum(&data);
792        if expected_checksum != checksum {
793            Err(self::Error::InvalidChecksum { expected: expected_checksum, actual: checksum })
794        } else {
795            Ok(CheckedData { data, checksum })
796        }
797    }
798}
799
800impl<T: Encodable> Encodable for &T {
801    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
802        (**self).consensus_encode(w)
803    }
804}
805
806impl<T: Encodable> Encodable for &mut T {
807    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
808        (**self).consensus_encode(w)
809    }
810}
811
812impl<T: Encodable> Encodable for rc::Rc<T> {
813    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
814        (**self).consensus_encode(w)
815    }
816}
817
818/// Note: This will fail to compile on old Rust for targets that don't support atomics
819#[cfg(any(not(rust_v_1_60), target_has_atomic = "ptr"))]
820impl<T: Encodable> Encodable for sync::Arc<T> {
821    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
822        (**self).consensus_encode(w)
823    }
824}
825
826macro_rules! tuple_encode {
827    ($($x:ident),*) => {
828        impl <$($x: Encodable),*> Encodable for ($($x),*) {
829            #[inline]
830            #[allow(non_snake_case)]
831            fn consensus_encode<W: Write + ?Sized>(
832                &self,
833                w: &mut W,
834            ) -> core::result::Result<usize, io::Error> {
835                let &($(ref $x),*) = self;
836                let mut len = 0;
837                $(len += $x.consensus_encode(w)?;)*
838                Ok(len)
839            }
840        }
841
842        impl<$($x: Decodable),*> Decodable for ($($x),*) {
843            #[inline]
844            #[allow(non_snake_case)]
845            fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> core::result::Result<Self, Error> {
846                Ok(($({let $x = Decodable::consensus_decode(r)?; $x }),*))
847            }
848        }
849    };
850}
851
852tuple_encode!(T0, T1);
853tuple_encode!(T0, T1, T2);
854tuple_encode!(T0, T1, T2, T3);
855tuple_encode!(T0, T1, T2, T3, T4);
856tuple_encode!(T0, T1, T2, T3, T4, T5);
857tuple_encode!(T0, T1, T2, T3, T4, T5, T6);
858tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
859
860impl Encodable for sha256d::Hash {
861    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
862        self.as_byte_array().consensus_encode(w)
863    }
864}
865
866impl Decodable for sha256d::Hash {
867    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
868        Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
869    }
870}
871
872impl Encodable for sha256::Hash {
873    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
874        self.as_byte_array().consensus_encode(w)
875    }
876}
877
878impl Decodable for sha256::Hash {
879    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
880        Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
881    }
882}
883
884impl Encodable for TapLeafHash {
885    fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
886        self.as_byte_array().consensus_encode(w)
887    }
888}
889
890impl Decodable for TapLeafHash {
891    fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
892        Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
893    }
894}
895
896#[cfg(test)]
897mod tests {
898    use core::mem::discriminant;
899
900    use super::*;
901
902    #[test]
903    fn serialize_int_test() {
904        // bool
905        assert_eq!(serialize(&false), vec![0u8]);
906        assert_eq!(serialize(&true), vec![1u8]);
907        // u8
908        assert_eq!(serialize(&1u8), vec![1u8]);
909        assert_eq!(serialize(&0u8), vec![0u8]);
910        assert_eq!(serialize(&255u8), vec![255u8]);
911        // u16
912        assert_eq!(serialize(&1u16), vec![1u8, 0]);
913        assert_eq!(serialize(&256u16), vec![0u8, 1]);
914        assert_eq!(serialize(&5000u16), vec![136u8, 19]);
915        // u32
916        assert_eq!(serialize(&1u32), vec![1u8, 0, 0, 0]);
917        assert_eq!(serialize(&256u32), vec![0u8, 1, 0, 0]);
918        assert_eq!(serialize(&5000u32), vec![136u8, 19, 0, 0]);
919        assert_eq!(serialize(&500000u32), vec![32u8, 161, 7, 0]);
920        assert_eq!(serialize(&168430090u32), vec![10u8, 10, 10, 10]);
921        // i32
922        assert_eq!(serialize(&-1i32), vec![255u8, 255, 255, 255]);
923        assert_eq!(serialize(&-256i32), vec![0u8, 255, 255, 255]);
924        assert_eq!(serialize(&-5000i32), vec![120u8, 236, 255, 255]);
925        assert_eq!(serialize(&-500000i32), vec![224u8, 94, 248, 255]);
926        assert_eq!(serialize(&-168430090i32), vec![246u8, 245, 245, 245]);
927        assert_eq!(serialize(&1i32), vec![1u8, 0, 0, 0]);
928        assert_eq!(serialize(&256i32), vec![0u8, 1, 0, 0]);
929        assert_eq!(serialize(&5000i32), vec![136u8, 19, 0, 0]);
930        assert_eq!(serialize(&500000i32), vec![32u8, 161, 7, 0]);
931        assert_eq!(serialize(&168430090i32), vec![10u8, 10, 10, 10]);
932        // u64
933        assert_eq!(serialize(&1u64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
934        assert_eq!(serialize(&256u64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
935        assert_eq!(serialize(&5000u64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
936        assert_eq!(serialize(&500000u64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
937        assert_eq!(serialize(&723401728380766730u64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
938        // i64
939        assert_eq!(serialize(&-1i64), vec![255u8, 255, 255, 255, 255, 255, 255, 255]);
940        assert_eq!(serialize(&-256i64), vec![0u8, 255, 255, 255, 255, 255, 255, 255]);
941        assert_eq!(serialize(&-5000i64), vec![120u8, 236, 255, 255, 255, 255, 255, 255]);
942        assert_eq!(serialize(&-500000i64), vec![224u8, 94, 248, 255, 255, 255, 255, 255]);
943        assert_eq!(
944            serialize(&-723401728380766730i64),
945            vec![246u8, 245, 245, 245, 245, 245, 245, 245]
946        );
947        assert_eq!(serialize(&1i64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
948        assert_eq!(serialize(&256i64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
949        assert_eq!(serialize(&5000i64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
950        assert_eq!(serialize(&500000i64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
951        assert_eq!(serialize(&723401728380766730i64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
952    }
953
954    #[test]
955    fn serialize_varint_test() {
956        assert_eq!(serialize(&VarInt(10)), vec![10u8]);
957        assert_eq!(serialize(&VarInt(0xFC)), vec![0xFCu8]);
958        assert_eq!(serialize(&VarInt(0xFD)), vec![0xFDu8, 0xFD, 0]);
959        assert_eq!(serialize(&VarInt(0xFFF)), vec![0xFDu8, 0xFF, 0xF]);
960        assert_eq!(serialize(&VarInt(0xF0F0F0F)), vec![0xFEu8, 0xF, 0xF, 0xF, 0xF]);
961        assert_eq!(
962            serialize(&VarInt(0xF0F0F0F0F0E0)),
963            vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0]
964        );
965        assert_eq!(
966            test_varint_encode(0xFF, &0x100000000_u64.to_le_bytes()).unwrap(),
967            VarInt(0x100000000)
968        );
969        assert_eq!(test_varint_encode(0xFE, &0x10000_u64.to_le_bytes()).unwrap(), VarInt(0x10000));
970        assert_eq!(test_varint_encode(0xFD, &0xFD_u64.to_le_bytes()).unwrap(), VarInt(0xFD));
971
972        // Test that length calc is working correctly
973        test_varint_len(VarInt(0), 1);
974        test_varint_len(VarInt(0xFC), 1);
975        test_varint_len(VarInt(0xFD), 3);
976        test_varint_len(VarInt(0xFFFF), 3);
977        test_varint_len(VarInt(0x10000), 5);
978        test_varint_len(VarInt(0xFFFFFFFF), 5);
979        test_varint_len(VarInt(0xFFFFFFFF + 1), 9);
980        test_varint_len(VarInt(u64::MAX), 9);
981    }
982
983    fn test_varint_len(varint: VarInt, expected: usize) {
984        let mut encoder = vec![];
985        assert_eq!(varint.consensus_encode(&mut encoder).unwrap(), expected);
986        assert_eq!(varint.size(), expected);
987    }
988
989    fn test_varint_encode(n: u8, x: &[u8]) -> Result<VarInt, Error> {
990        let mut input = [0u8; 9];
991        input[0] = n;
992        input[1..x.len() + 1].copy_from_slice(x);
993        deserialize_partial::<VarInt>(&input).map(|t| t.0)
994    }
995
996    #[test]
997    fn deserialize_nonminimal_vec() {
998        // Check the edges for variant int
999        assert_eq!(
1000            discriminant(
1001                &test_varint_encode(0xFF, &(0x100000000_u64 - 1).to_le_bytes()).unwrap_err()
1002            ),
1003            discriminant(&Error::NonMinimalVarInt)
1004        );
1005        assert_eq!(
1006            discriminant(&test_varint_encode(0xFE, &(0x10000_u64 - 1).to_le_bytes()).unwrap_err()),
1007            discriminant(&Error::NonMinimalVarInt)
1008        );
1009        assert_eq!(
1010            discriminant(&test_varint_encode(0xFD, &(0xFD_u64 - 1).to_le_bytes()).unwrap_err()),
1011            discriminant(&Error::NonMinimalVarInt)
1012        );
1013
1014        assert_eq!(
1015            discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0x00, 0x00]).unwrap_err()),
1016            discriminant(&Error::NonMinimalVarInt)
1017        );
1018        assert_eq!(
1019            discriminant(&deserialize::<Vec<u8>>(&[0xfd, 0xfc, 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>>(&[0xfe, 0xff, 0x00, 0x00, 0x00]).unwrap_err()),
1028            discriminant(&Error::NonMinimalVarInt)
1029        );
1030        assert_eq!(
1031            discriminant(&deserialize::<Vec<u8>>(&[0xfe, 0xff, 0xff, 0x00, 0x00]).unwrap_err()),
1032            discriminant(&Error::NonMinimalVarInt)
1033        );
1034        assert_eq!(
1035            discriminant(
1036                &deserialize::<Vec<u8>>(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
1037                    .unwrap_err()
1038            ),
1039            discriminant(&Error::NonMinimalVarInt)
1040        );
1041        assert_eq!(
1042            discriminant(
1043                &deserialize::<Vec<u8>>(&[0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00])
1044                    .unwrap_err()
1045            ),
1046            discriminant(&Error::NonMinimalVarInt)
1047        );
1048
1049        let mut vec_256 = vec![0; 259];
1050        vec_256[0] = 0xfd;
1051        vec_256[1] = 0x00;
1052        vec_256[2] = 0x01;
1053        assert!(deserialize::<Vec<u8>>(&vec_256).is_ok());
1054
1055        let mut vec_253 = vec![0; 256];
1056        vec_253[0] = 0xfd;
1057        vec_253[1] = 0xfd;
1058        vec_253[2] = 0x00;
1059        assert!(deserialize::<Vec<u8>>(&vec_253).is_ok());
1060    }
1061
1062    #[test]
1063    fn serialize_checkeddata_test() {
1064        let cd = CheckedData::new(vec![1u8, 2, 3, 4, 5]);
1065        assert_eq!(serialize(&cd), vec![5, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
1066    }
1067
1068    #[test]
1069    fn serialize_vector_test() {
1070        assert_eq!(serialize(&vec![1u8, 2, 3]), vec![3u8, 1, 2, 3]);
1071    }
1072
1073    #[test]
1074    fn serialize_strbuf_test() {
1075        assert_eq!(serialize(&"Andrew".to_string()), vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]);
1076    }
1077
1078    #[test]
1079    fn deserialize_int_test() {
1080        // bool
1081        assert!((deserialize(&[58u8, 0]) as Result<bool, _>).is_err());
1082        assert_eq!(deserialize(&[58u8]).ok(), Some(true));
1083        assert_eq!(deserialize(&[1u8]).ok(), Some(true));
1084        assert_eq!(deserialize(&[0u8]).ok(), Some(false));
1085        assert!((deserialize(&[0u8, 1]) as Result<bool, _>).is_err());
1086
1087        // u8
1088        assert_eq!(deserialize(&[58u8]).ok(), Some(58u8));
1089
1090        // u16
1091        assert_eq!(deserialize(&[0x01u8, 0x02]).ok(), Some(0x0201u16));
1092        assert_eq!(deserialize(&[0xABu8, 0xCD]).ok(), Some(0xCDABu16));
1093        assert_eq!(deserialize(&[0xA0u8, 0x0D]).ok(), Some(0xDA0u16));
1094        let failure16: Result<u16, _> = deserialize(&[1u8]);
1095        assert!(failure16.is_err());
1096
1097        // i16
1098        assert_eq!(deserialize(&[0x32_u8, 0xF4]).ok(), Some(-0x0bce_i16));
1099        assert_eq!(deserialize(&[0xFF_u8, 0xFE]).ok(), Some(-0x0101_i16));
1100        assert_eq!(deserialize(&[0x00_u8, 0x00]).ok(), Some(-0_i16));
1101        assert_eq!(deserialize(&[0xFF_u8, 0xFA]).ok(), Some(-0x0501_i16));
1102
1103        // u32
1104        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABu32));
1105        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD]).ok(), Some(0xCDAB0DA0u32));
1106
1107        let failure32: Result<u32, _> = deserialize(&[1u8, 2, 3]);
1108        assert!(failure32.is_err());
1109
1110        // i32
1111        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABi32));
1112        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0x2D]).ok(), Some(0x2DAB0DA0i32));
1113
1114        assert_eq!(deserialize(&[0, 0, 0, 0]).ok(), Some(-0_i32));
1115        assert_eq!(deserialize(&[0, 0, 0, 0]).ok(), Some(0_i32));
1116
1117        assert_eq!(deserialize(&[0xFF, 0xFF, 0xFF, 0xFF]).ok(), Some(-1_i32));
1118        assert_eq!(deserialize(&[0xFE, 0xFF, 0xFF, 0xFF]).ok(), Some(-2_i32));
1119        assert_eq!(deserialize(&[0x01, 0xFF, 0xFF, 0xFF]).ok(), Some(-255_i32));
1120        assert_eq!(deserialize(&[0x02, 0xFF, 0xFF, 0xFF]).ok(), Some(-254_i32));
1121
1122        let failurei32: Result<i32, _> = deserialize(&[1u8, 2, 3]);
1123        assert!(failurei32.is_err());
1124
1125        // u64
1126        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABu64));
1127        assert_eq!(
1128            deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(),
1129            Some(0x99000099CDAB0DA0u64)
1130        );
1131        let failure64: Result<u64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
1132        assert!(failure64.is_err());
1133
1134        // i64
1135        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABi64));
1136        assert_eq!(
1137            deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(),
1138            Some(-0x66ffff663254f260i64)
1139        );
1140        assert_eq!(
1141            deserialize(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1142            Some(-1_i64)
1143        );
1144        assert_eq!(
1145            deserialize(&[0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1146            Some(-2_i64)
1147        );
1148        assert_eq!(
1149            deserialize(&[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1150            Some(-255_i64)
1151        );
1152        assert_eq!(
1153            deserialize(&[0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).ok(),
1154            Some(-254_i64)
1155        );
1156
1157        let failurei64: Result<i64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
1158        assert!(failurei64.is_err());
1159    }
1160
1161    #[test]
1162    fn deserialize_vec_test() {
1163        assert_eq!(deserialize(&[3u8, 2, 3, 4]).ok(), Some(vec![2u8, 3, 4]));
1164        assert!((deserialize(&[4u8, 2, 3, 4, 5, 6]) as Result<Vec<u8>, _>).is_err());
1165        // found by cargo fuzz
1166        assert!(deserialize::<Vec<u64>>(&[
1167            0xff, 0xff, 0xff, 0xff, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b, 0x6b,
1168            0x6b, 0x6b, 0xa, 0xa, 0x3a
1169        ])
1170        .is_err());
1171
1172        let rand_io_err = Error::Io(io::Error::new(io::ErrorKind::Other, ""));
1173
1174        // Check serialization that `if len > MAX_VEC_SIZE {return err}` isn't inclusive,
1175        // by making sure it fails with IO Error and not an `OversizedVectorAllocation` Error.
1176        let err =
1177            deserialize::<CheckedData>(&serialize(&(super::MAX_VEC_SIZE as u32))).unwrap_err();
1178        assert_eq!(discriminant(&err), discriminant(&rand_io_err));
1179
1180        test_len_is_max_vec::<u8>();
1181        test_len_is_max_vec::<BlockHash>();
1182        test_len_is_max_vec::<FilterHash>();
1183        test_len_is_max_vec::<TxMerkleNode>();
1184        test_len_is_max_vec::<Transaction>();
1185        test_len_is_max_vec::<TxOut>();
1186        test_len_is_max_vec::<TxIn>();
1187        test_len_is_max_vec::<Vec<u8>>();
1188        test_len_is_max_vec::<u64>();
1189        #[cfg(feature = "std")]
1190        test_len_is_max_vec::<(u32, Address)>();
1191        #[cfg(feature = "std")]
1192        test_len_is_max_vec::<Inventory>();
1193    }
1194
1195    fn test_len_is_max_vec<T>()
1196    where
1197        Vec<T>: Decodable,
1198        T: fmt::Debug,
1199    {
1200        let rand_io_err = Error::Io(io::Error::new(io::ErrorKind::Other, ""));
1201        let varint = VarInt((super::MAX_VEC_SIZE / mem::size_of::<T>()) as u64);
1202        let err = deserialize::<Vec<T>>(&serialize(&varint)).unwrap_err();
1203        assert_eq!(discriminant(&err), discriminant(&rand_io_err));
1204    }
1205
1206    #[test]
1207    fn deserialize_strbuf_test() {
1208        assert_eq!(
1209            deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(),
1210            Some("Andrew".to_string())
1211        );
1212        assert_eq!(
1213            deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(),
1214            Some(Cow::Borrowed("Andrew"))
1215        );
1216    }
1217
1218    #[test]
1219    fn deserialize_checkeddata_test() {
1220        let cd: Result<CheckedData, _> =
1221            deserialize(&[5u8, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
1222        assert_eq!(cd.ok(), Some(CheckedData::new(vec![1u8, 2, 3, 4, 5])));
1223    }
1224
1225    #[test]
1226    fn limit_read_test() {
1227        let witness = vec![vec![0u8; 3_999_999]; 2];
1228        let ser = serialize(&witness);
1229        let mut reader = io::Cursor::new(ser);
1230        let err = Vec::<Vec<u8>>::consensus_decode(&mut reader);
1231        assert!(err.is_err());
1232    }
1233
1234    #[test]
1235    #[cfg(feature = "rand-std")]
1236    fn serialization_round_trips() {
1237        use secp256k1::rand::{thread_rng, Rng};
1238
1239        macro_rules! round_trip {
1240            ($($val_type:ty),*) => {
1241                $(
1242                    let r: $val_type = thread_rng().gen();
1243                    assert_eq!(deserialize::<$val_type>(&serialize(&r)).unwrap(), r);
1244                )*
1245            };
1246        }
1247        macro_rules! round_trip_bytes {
1248            ($(($val_type:ty, $data:expr)),*) => {
1249                $(
1250                    thread_rng().fill(&mut $data[..]);
1251                    assert_eq!(deserialize::<$val_type>(&serialize(&$data)).unwrap()[..], $data[..]);
1252                )*
1253            };
1254        }
1255
1256        let mut data = Vec::with_capacity(256);
1257        let mut data64 = Vec::with_capacity(256);
1258        for _ in 0..10 {
1259            round_trip! {bool, i8, u8, i16, u16, i32, u32, i64, u64,
1260            (bool, i8, u16, i32), (u64, i64, u32, i32, u16, i16), (i8, u8, i16, u16, i32, u32, i64, u64),
1261            [u8; 2], [u8; 4], [u8; 8], [u8; 12], [u8; 16], [u8; 32]};
1262
1263            data.clear();
1264            data64.clear();
1265            let len = thread_rng().gen_range(1..256);
1266            data.resize(len, 0u8);
1267            data64.resize(len, 0u64);
1268            let mut arr33 = [0u8; 33];
1269            let mut arr16 = [0u16; 8];
1270            round_trip_bytes! {(Vec<u8>, data), ([u8; 33], arr33), ([u16; 8], arr16), (Vec<u64>, data64)};
1271        }
1272    }
1273
1274    #[test]
1275    fn test_read_bytes_from_finite_reader() {
1276        let data: Vec<u8> = (0..10).collect();
1277
1278        for chunk_size in 1..20 {
1279            assert_eq!(
1280                read_bytes_from_finite_reader(
1281                    &mut io::Cursor::new(&data),
1282                    ReadBytesFromFiniteReaderOpts { len: data.len(), chunk_size }
1283                )
1284                .unwrap(),
1285                data
1286            );
1287        }
1288    }
1289
1290    #[test]
1291    fn deserialize_tx_hex() {
1292        let hex = include_str!("../../tests/data/previous_tx_0_hex"); // An arbitrary transaction.
1293        assert!(deserialize_hex::<Transaction>(hex).is_ok())
1294    }
1295
1296    #[test]
1297    fn deserialize_tx_hex_too_many_bytes() {
1298        use crate::consensus::DecodeError;
1299
1300        let mut hex = include_str!("../../tests/data/previous_tx_0_hex").to_string(); // An arbitrary transaction.
1301        hex.push_str("abcdef");
1302        assert!(matches!(
1303            deserialize_hex::<Transaction>(&hex).unwrap_err(),
1304            FromHexError::Decode(DecodeError::TooManyBytes)
1305        ));
1306    }
1307}