bitcoin/consensus/
encode.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//     Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! Consensus-encodable types
16//!
17//! This is basically a replacement of the `Encodable` trait which does
18//! normalization for endianness, etc., to ensure that the encoding
19//! matches for endianness, etc., to ensure that the encoding matches
20//! the network consensus encoding.
21//!
22//! Essentially, anything that must go on the -disk- or -network- must
23//! be encoded using the `Encodable` trait, since this data
24//! must be the same for all systems. Any data going to the -user-, e.g.
25//! over JSONRPC, should use the ordinary `Encodable` trait. (This
26//! should also be the same across systems, of course, but has some
27//! critical differences from the network format, e.g. scripts come
28//! with an opcode decode, hashes are big-endian, numbers are typically
29//! big-endian decimals, etc.)
30//!
31
32use std::collections::HashMap;
33use std::hash::Hash;
34use std::{mem, u32};
35
36use std::error;
37use std::fmt;
38use std::io;
39use std::io::{Cursor, Read, Write};
40use byteorder::{LittleEndian, WriteBytesExt, ReadBytesExt};
41use hex::encode as hex_encode;
42
43use bitcoin_bech32;
44use bitcoin_hashes::{sha256d, Hash as HashTrait};
45
46use util::base58;
47
48/// Encoding error
49#[derive(Debug)]
50pub enum Error {
51    /// And I/O error
52    Io(io::Error),
53    /// Base58 encoding error
54    Base58(base58::Error),
55    /// Bech32 encoding error
56    Bech32(bitcoin_bech32::Error),
57    /// Error from the `byteorder` crate
58    ByteOrder(io::Error),
59    /// Network magic was not expected
60    UnexpectedNetworkMagic {
61        /// The expected network magic
62        expected: u32,
63        /// The unexpected network magic
64        actual: u32,
65    },
66    /// Tried to allocate an oversized vector
67    OversizedVectorAllocation{
68        /// The capacity requested
69        requested: usize,
70        /// The maximum capacity
71        max: usize,
72    },
73    /// Checksum was invalid
74    InvalidChecksum {
75        /// The expected checksum
76        expected: [u8; 4],
77        /// The invalid checksum
78        actual: [u8; 4],
79    },
80    /// Network magic was unknown
81    UnknownNetworkMagic(u32),
82    /// Parsing error
83    ParseFailed(&'static str),
84    /// Unsupported witness version
85    UnsupportedWitnessVersion(u8),
86    /// Unsupported Segwit flag
87    UnsupportedSegwitFlag(u8),
88    /// Unrecognized network command
89    UnrecognizedNetworkCommand(String),
90    /// Unexpected hex digit
91    UnexpectedHexDigit(char),
92}
93
94impl fmt::Display for Error {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        match *self {
97            Error::Io(ref e) => fmt::Display::fmt(e, f),
98            Error::Base58(ref e) => fmt::Display::fmt(e, f),
99            Error::Bech32(ref e) => fmt::Display::fmt(e, f),
100            Error::ByteOrder(ref e) => fmt::Display::fmt(e, f),
101            Error::UnexpectedNetworkMagic { expected: ref e, actual: ref a } => write!(f, "{}: expected {}, actual {}", error::Error::description(self), e, a),
102            Error::OversizedVectorAllocation { requested: ref r, max: ref m } => write!(f, "{}: requested {}, maximum {}", error::Error::description(self), r, m),
103            Error::InvalidChecksum { expected: ref e, actual: ref a } => write!(f, "{}: expected {}, actual {}", error::Error::description(self), hex_encode(e), hex_encode(a)),
104            Error::UnknownNetworkMagic(ref m) => write!(f, "{}: {}", error::Error::description(self), m),
105            Error::ParseFailed(ref e) => write!(f, "{}: {}", error::Error::description(self), e),
106            Error::UnsupportedWitnessVersion(ref wver) => write!(f, "{}: {}", error::Error::description(self), wver),
107            Error::UnsupportedSegwitFlag(ref swflag) => write!(f, "{}: {}", error::Error::description(self), swflag),
108            Error::UnrecognizedNetworkCommand(ref nwcmd) => write!(f, "{}: {}", error::Error::description(self), nwcmd),
109            Error::UnexpectedHexDigit(ref d) => write!(f, "{}: {}", error::Error::description(self), d),
110        }
111    }
112}
113
114impl error::Error for Error {
115    fn cause(&self) -> Option<&error::Error> {
116        match *self {
117            Error::Io(ref e) => Some(e),
118            Error::Base58(ref e) => Some(e),
119            Error::Bech32(ref e) => Some(e),
120            Error::ByteOrder(ref e) => Some(e),
121            Error::UnexpectedNetworkMagic { .. }
122            | Error::OversizedVectorAllocation { .. }
123            | Error::InvalidChecksum { .. }
124            | Error::UnknownNetworkMagic(..)
125            | Error::ParseFailed(..)
126            | Error::UnsupportedWitnessVersion(..)
127            | Error::UnsupportedSegwitFlag(..)
128            | Error::UnrecognizedNetworkCommand(..)
129            | Error::UnexpectedHexDigit(..) => None,
130        }
131    }
132
133    fn description(&self) -> &str {
134        match *self {
135            Error::Io(ref e) => e.description(),
136            Error::Base58(ref e) => e.description(),
137            Error::Bech32(ref e) => e.description(),
138            Error::ByteOrder(ref e) => e.description(),
139            Error::UnexpectedNetworkMagic { .. } => "unexpected network magic",
140            Error::OversizedVectorAllocation { .. } => "allocation of oversized vector requested",
141            Error::InvalidChecksum { .. } => "invalid checksum",
142            Error::UnknownNetworkMagic(..) => "unknown network magic",
143            Error::ParseFailed(..) => "parse failed",
144            Error::UnsupportedWitnessVersion(..) => "unsupported witness version",
145            Error::UnsupportedSegwitFlag(..) => "unsupported segwit version",
146            Error::UnrecognizedNetworkCommand(..) => "unrecognized network command",
147            Error::UnexpectedHexDigit(..) => "unexpected hex digit",
148        }
149    }
150}
151
152#[doc(hidden)]
153impl From<base58::Error> for Error {
154    fn from(e: base58::Error) -> Error {
155        Error::Base58(e)
156    }
157}
158
159#[doc(hidden)]
160impl From<bitcoin_bech32::Error> for Error {
161    fn from(e: bitcoin_bech32::Error) -> Error {
162        Error::Bech32(e)
163    }
164}
165
166
167#[doc(hidden)]
168impl From<io::Error> for Error {
169    fn from(error: io::Error) -> Self {
170        Error::Io(error)
171    }
172}
173
174/// Encode an object into a vector
175pub fn serialize<T: ?Sized>(data: &T) -> Vec<u8>
176     where T: Encodable<Cursor<Vec<u8>>>,
177{
178    let mut encoder = Cursor::new(vec![]);
179    data.consensus_encode(&mut encoder).unwrap();
180    encoder.into_inner()
181}
182
183/// Encode an object into a hex-encoded string
184pub fn serialize_hex<T: ?Sized>(data: &T) -> String
185     where T: Encodable<Cursor<Vec<u8>>>
186{
187    hex_encode(serialize(data))
188}
189
190/// Deserialize an object from a vector, will error if said deserialization
191/// doesn't consume the entire vector.
192pub fn deserialize<'a, T>(data: &'a [u8]) -> Result<T, Error>
193     where T: Decodable<Cursor<&'a [u8]>>
194{
195    let mut decoder = Cursor::new(data);
196    let rv = Decodable::consensus_decode(&mut decoder)?;
197
198    // Fail if data is not consumed entirely.
199    if decoder.position() == data.len() as u64 {
200        Ok(rv)
201    } else {
202        Err(Error::ParseFailed("data not consumed entirely when explicitly deserializing"))
203    }
204}
205
206/// A simple Encoder trait
207pub trait Encoder {
208    /// Output a 64-bit uint
209    fn emit_u64(&mut self, v: u64) -> Result<(), Error>;
210    /// Output a 32-bit uint
211    fn emit_u32(&mut self, v: u32) -> Result<(), Error>;
212    /// Output a 16-bit uint
213    fn emit_u16(&mut self, v: u16) -> Result<(), Error>;
214    /// Output a 8-bit uint
215    fn emit_u8(&mut self, v: u8) -> Result<(), Error>;
216
217    /// Output a 64-bit int
218    fn emit_i64(&mut self, v: i64) -> Result<(), Error>;
219    /// Output a 32-bit int
220    fn emit_i32(&mut self, v: i32) -> Result<(), Error>;
221    /// Output a 16-bit int
222    fn emit_i16(&mut self, v: i16) -> Result<(), Error>;
223    /// Output a 8-bit int
224    fn emit_i8(&mut self, v: i8) -> Result<(), Error>;
225
226    /// Output a boolean
227    fn emit_bool(&mut self, v: bool) -> Result<(), Error>;
228}
229
230/// A simple Decoder trait
231pub trait Decoder {
232    /// Read a 64-bit uint
233    fn read_u64(&mut self) -> Result<u64, Error>;
234    /// Read a 32-bit uint
235    fn read_u32(&mut self) -> Result<u32, Error>;
236    /// Read a 16-bit uint
237    fn read_u16(&mut self) -> Result<u16, Error>;
238    /// Read a 8-bit uint
239    fn read_u8(&mut self) -> Result<u8, Error>;
240
241    /// Read a 64-bit int
242    fn read_i64(&mut self) -> Result<i64, Error>;
243    /// Read a 32-bit int
244    fn read_i32(&mut self) -> Result<i32, Error>;
245    /// Read a 16-bit int
246    fn read_i16(&mut self) -> Result<i16, Error>;
247    /// Read a 8-bit int
248    fn read_i8(&mut self) -> Result<i8, Error>;
249
250    /// Read a boolean
251    fn read_bool(&mut self) -> Result<bool, Error>;
252}
253
254macro_rules! encoder_fn {
255    ($name:ident, $val_type:ty, $writefn:ident) => {
256        #[inline]
257        fn $name(&mut self, v: $val_type) -> Result<(), Error> {
258            WriteBytesExt::$writefn::<LittleEndian>(self, v).map_err(Error::Io)
259        }
260    }
261}
262
263macro_rules! decoder_fn {
264    ($name:ident, $val_type:ty, $readfn:ident) => {
265        #[inline]
266        fn $name(&mut self) -> Result<$val_type, Error> {
267            ReadBytesExt::$readfn::<LittleEndian>(self).map_err(Error::Io)
268        }
269    }
270}
271
272impl<W: Write> Encoder for W {
273    encoder_fn!(emit_u64, u64, write_u64);
274    encoder_fn!(emit_u32, u32, write_u32);
275    encoder_fn!(emit_u16, u16, write_u16);
276    encoder_fn!(emit_i64, i64, write_i64);
277    encoder_fn!(emit_i32, i32, write_i32);
278    encoder_fn!(emit_i16, i16, write_i16);
279
280    #[inline]
281    fn emit_i8(&mut self, v: i8) -> Result<(), Error> {
282        self.write_i8(v).map_err(Error::Io)
283    }
284    #[inline]
285    fn emit_u8(&mut self, v: u8) -> Result<(), Error> {
286        self.write_u8(v).map_err(Error::Io)
287    }
288    #[inline]
289    fn emit_bool(&mut self, v: bool) -> Result<(), Error> {
290        self.write_i8(if v {1} else {0}).map_err(Error::Io)
291    }
292}
293
294impl<R: Read> Decoder for R {
295    decoder_fn!(read_u64, u64, read_u64);
296    decoder_fn!(read_u32, u32, read_u32);
297    decoder_fn!(read_u16, u16, read_u16);
298    decoder_fn!(read_i64, i64, read_i64);
299    decoder_fn!(read_i32, i32, read_i32);
300    decoder_fn!(read_i16, i16, read_i16);
301
302    #[inline]
303    fn read_u8(&mut self) -> Result<u8, Error> {
304        ReadBytesExt::read_u8(self).map_err(Error::Io)
305    }
306    #[inline]
307    fn read_i8(&mut self) -> Result<i8, Error> {
308        ReadBytesExt::read_i8(self).map_err(Error::Io)
309    }
310    #[inline]
311    fn read_bool(&mut self) -> Result<bool, Error> {
312        Decoder::read_i8(self).map(|bit| bit != 0)
313    }
314}
315
316/// Maximum size, in bytes, of a vector we are allowed to decode
317pub const MAX_VEC_SIZE: usize = 32 * 1024 * 1024;
318
319/// Data which can be encoded in a consensus-consistent way
320pub trait Encodable<S: Encoder> {
321    /// Encode an object with a well-defined format, should only ever error if
322    /// the underlying Encoder errors.
323    fn consensus_encode(&self, e: &mut S) -> Result<(), self::Error>;
324}
325
326/// Data which can be encoded in a consensus-consistent way
327pub trait Decodable<D: Decoder>: Sized {
328    /// Decode an object with a well-defined format
329    fn consensus_decode(d: &mut D) -> Result<Self, self::Error>;
330}
331
332/// A variable-length unsigned integer
333#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
334pub struct VarInt(pub u64);
335
336/// Data which must be preceded by a 4-byte checksum
337#[derive(PartialEq, Eq, Clone, Debug)]
338pub struct CheckedData(pub Vec<u8>);
339
340// Primitive types
341macro_rules! impl_int_encodable{
342    ($ty:ident, $meth_dec:ident, $meth_enc:ident) => (
343        impl<D: Decoder> Decodable<D> for $ty {
344            #[inline]
345            fn consensus_decode(d: &mut D) -> Result<$ty, self::Error> { d.$meth_dec().map($ty::from_le) }
346        }
347
348        impl<S: Encoder> Encodable<S> for $ty {
349            #[inline]
350            fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { s.$meth_enc(self.to_le()) }
351        }
352    )
353}
354
355impl_int_encodable!(u8,  read_u8,  emit_u8);
356impl_int_encodable!(u16, read_u16, emit_u16);
357impl_int_encodable!(u32, read_u32, emit_u32);
358impl_int_encodable!(u64, read_u64, emit_u64);
359impl_int_encodable!(i8,  read_i8,  emit_i8);
360impl_int_encodable!(i16, read_i16, emit_i16);
361impl_int_encodable!(i32, read_i32, emit_i32);
362impl_int_encodable!(i64, read_i64, emit_i64);
363
364impl VarInt {
365    /// Gets the length of this VarInt when encoded.
366    /// Returns 1 for 0...0xFC, 3 for 0xFD...(2^16-1), 5 for 0x10000...(2^32-1),
367    /// and 9 otherwise.
368    #[inline]
369    pub fn encoded_length(&self) -> u64 {
370        match self.0 {
371            0...0xFC             => { 1 }
372            0xFD...0xFFFF        => { 3 }
373            0x10000...0xFFFFFFFF => { 5 }
374            _                    => { 9 }
375        }
376    }
377}
378
379impl<S: Encoder> Encodable<S> for VarInt {
380    #[inline]
381    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
382        match self.0 {
383            0...0xFC             => { (self.0 as u8).consensus_encode(s) }
384            0xFD...0xFFFF        => { s.emit_u8(0xFD)?; (self.0 as u16).consensus_encode(s) }
385            0x10000...0xFFFFFFFF => { s.emit_u8(0xFE)?; (self.0 as u32).consensus_encode(s) }
386            _                    => { s.emit_u8(0xFF)?; (self.0 as u64).consensus_encode(s) }
387        }
388    }
389}
390
391impl<D: Decoder> Decodable<D> for VarInt {
392    #[inline]
393    fn consensus_decode(d: &mut D) -> Result<VarInt, self::Error> {
394        let n = d.read_u8()?;
395        match n {
396            0xFF => {
397                let x = d.read_u64()?;
398                if x < 0x100000000 {
399                    Err(self::Error::ParseFailed("non-minimal varint"))
400                } else {
401                    Ok(VarInt(x))
402                }
403            }
404            0xFE => {
405                let x = d.read_u32()?;
406                if x < 0x10000 {
407                    Err(self::Error::ParseFailed("non-minimal varint"))
408                } else {
409                    Ok(VarInt(x as u64))
410                }
411            }
412            0xFD => {
413                let x = d.read_u16()?;
414                if x < 0xFD {
415                    Err(self::Error::ParseFailed("non-minimal varint"))
416                } else {
417                    Ok(VarInt(x as u64))
418                }
419            }
420            n => Ok(VarInt(n as u64))
421        }
422    }
423}
424
425
426// Booleans
427impl<S: Encoder> Encodable<S> for bool {
428    #[inline]
429    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { s.emit_u8(if *self {1} else {0}) }
430}
431
432impl<D: Decoder> Decodable<D> for bool {
433    #[inline]
434    fn consensus_decode(d: &mut D) -> Result<bool, self::Error> { d.read_u8().map(|n| n != 0) }
435}
436
437// Strings
438impl<S: Encoder> Encodable<S> for String {
439    #[inline]
440    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
441        self.as_bytes().consensus_encode(s)
442    }
443}
444
445impl<D: Decoder> Decodable<D> for String {
446    #[inline]
447    fn consensus_decode(d: &mut D) -> Result<String, self::Error> {
448        String::from_utf8(Decodable::consensus_decode(d)?)
449            .map_err(|_| self::Error::ParseFailed("String was not valid UTF8"))
450    }
451}
452
453
454// Arrays
455macro_rules! impl_array {
456    ( $size:expr ) => (
457        impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T; $size] {
458            #[inline]
459            fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
460                for i in self.iter() { i.consensus_encode(s)?; }
461                Ok(())
462            }
463        }
464
465        impl<D: Decoder, T:Decodable<D> + Copy> Decodable<D> for [T; $size] {
466            #[inline]
467            fn consensus_decode(d: &mut D) -> Result<[T; $size], self::Error> {
468                // Set everything to the first decode
469                let mut ret = [Decodable::consensus_decode(d)?; $size];
470                // Set the rest
471                for item in ret.iter_mut().take($size).skip(1) { *item = Decodable::consensus_decode(d)?; }
472                Ok(ret)
473            }
474        }
475    );
476}
477
478impl_array!(2);
479impl_array!(4);
480impl_array!(8);
481impl_array!(12);
482impl_array!(16);
483impl_array!(32);
484
485impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
486    #[inline]
487    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
488        VarInt(self.len() as u64).consensus_encode(s)?;
489        for c in self.iter() { c.consensus_encode(s)?; }
490        Ok(())
491    }
492}
493
494// Cannot decode a slice
495
496// Vectors
497impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
498    #[inline]
499    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (&self[..]).consensus_encode(s) }
500}
501
502impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
503    #[inline]
504    fn consensus_decode(d: &mut D) -> Result<Vec<T>, self::Error> {
505        let len = VarInt::consensus_decode(d)?.0;
506        let byte_size = (len as usize)
507                            .checked_mul(mem::size_of::<T>())
508                            .ok_or(self::Error::ParseFailed("Invalid length"))?;
509        if byte_size > MAX_VEC_SIZE {
510            return Err(self::Error::OversizedVectorAllocation { requested: byte_size, max: MAX_VEC_SIZE })
511        }
512        let mut ret = Vec::with_capacity(len as usize);
513        for _ in 0..len { ret.push(Decodable::consensus_decode(d)?); }
514        Ok(ret)
515    }
516}
517
518impl<S: Encoder, T: Encodable<S>> Encodable<S> for Box<[T]> {
519    #[inline]
520    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (&self[..]).consensus_encode(s) }
521}
522
523impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
524    #[inline]
525    fn consensus_decode(d: &mut D) -> Result<Box<[T]>, self::Error> {
526        let len = VarInt::consensus_decode(d)?.0;
527        let len = len as usize;
528        if len > MAX_VEC_SIZE {
529            return Err(self::Error::OversizedVectorAllocation { requested: len, max: MAX_VEC_SIZE })
530        }
531        let mut ret = Vec::with_capacity(len);
532        for _ in 0..len { ret.push(Decodable::consensus_decode(d)?); }
533        Ok(ret.into_boxed_slice())
534    }
535}
536
537// Options (encoded as vectors of length 0 or 1)
538impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
539    #[inline]
540    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
541        match *self {
542            Some(ref data) => {
543                1u8.consensus_encode(s)?;
544                data.consensus_encode(s)?;
545            }
546            None => { 0u8.consensus_encode(s)?; }
547        }
548        Ok(())
549    }
550}
551
552impl<D: Decoder, T:Decodable<D>> Decodable<D> for Option<T> {
553    #[inline]
554    fn consensus_decode(d: &mut D) -> Result<Option<T>, self::Error> {
555        let bit: u8 = Decodable::consensus_decode(d)?;
556        Ok(if bit != 0 {
557            Some(Decodable::consensus_decode(d)?)
558        } else {
559            None
560        })
561    }
562}
563
564
565/// Do a double-SHA256 on some data and return the first 4 bytes
566fn sha2_checksum(data: &[u8]) -> [u8; 4] {
567    let checksum = <sha256d::Hash as HashTrait>::hash(data);
568    [checksum[0], checksum[1], checksum[2], checksum[3]]
569}
570
571// Checked data
572impl<S: Encoder> Encodable<S> for CheckedData {
573    #[inline]
574    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
575        (self.0.len() as u32).consensus_encode(s)?;
576        sha2_checksum(&self.0).consensus_encode(s)?;
577        // We can't just pass to the slice encoder since it'll insert a length
578        for ch in &self.0 {
579            ch.consensus_encode(s)?;
580        }
581        Ok(())
582    }
583}
584
585impl<D: Decoder> Decodable<D> for CheckedData {
586    #[inline]
587    fn consensus_decode(d: &mut D) -> Result<CheckedData, self::Error> {
588        let len: u32 = Decodable::consensus_decode(d)?;
589        let checksum: [u8; 4] = Decodable::consensus_decode(d)?;
590        let mut ret = Vec::with_capacity(len as usize);
591        for _ in 0..len { ret.push(Decodable::consensus_decode(d)?); }
592        let expected_checksum = sha2_checksum(&ret);
593        if expected_checksum != checksum {
594            Err(self::Error::InvalidChecksum {
595                expected: expected_checksum,
596                actual: checksum,
597            })
598        } else {
599            Ok(CheckedData(ret))
600        }
601    }
602}
603
604// Tuples
605macro_rules! tuple_encode {
606    ($($x:ident),*) => (
607        impl <S: Encoder, $($x: Encodable<S>),*> Encodable<S> for ($($x),*) {
608            #[inline]
609            #[allow(non_snake_case)]
610            fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
611                let &($(ref $x),*) = self;
612                $( $x.consensus_encode(s)?; )*
613                Ok(())
614            }
615        }
616
617        impl<D: Decoder, $($x: Decodable<D>),*> Decodable<D> for ($($x),*) {
618            #[inline]
619            #[allow(non_snake_case)]
620            fn consensus_decode(d: &mut D) -> Result<($($x),*), self::Error> {
621                Ok(($({let $x = Decodable::consensus_decode(d)?; $x }),*))
622            }
623        }
624    );
625}
626
627tuple_encode!(T0, T1);
628tuple_encode!(T0, T1, T2, T3);
629tuple_encode!(T0, T1, T2, T3, T4, T5);
630tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
631
632// References
633impl<S: Encoder, T: Encodable<S>> Encodable<S> for Box<T> {
634    #[inline]
635    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (**self).consensus_encode(s) }
636}
637
638impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
639    #[inline]
640    fn consensus_decode(d: &mut D) -> Result<Box<T>, self::Error> {
641        Decodable::consensus_decode(d).map(Box::new)
642    }
643}
644
645// HashMap
646impl<S, K, V> Encodable<S> for HashMap<K, V>
647    where S: Encoder,
648          K: Encodable<S> + Eq + Hash,
649          V: Encodable<S>
650{
651    #[inline]
652    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
653        VarInt(self.len() as u64).consensus_encode(s)?;
654        for (key, value) in self.iter() {
655            key.consensus_encode(s)?;
656            value.consensus_encode(s)?;
657        }
658        Ok(())
659    }
660}
661
662impl<D, K, V> Decodable<D> for HashMap<K, V>
663    where D: Decoder,
664          K: Decodable<D> + Eq + Hash,
665          V: Decodable<D>
666{
667    #[inline]
668    fn consensus_decode(d: &mut D) -> Result<HashMap<K, V>, self::Error> {
669        let len = VarInt::consensus_decode(d)?.0;
670
671        let mut ret = HashMap::with_capacity(len as usize);
672        for _ in 0..len {
673            ret.insert(Decodable::consensus_decode(d)?,
674                                 Decodable::consensus_decode(d)?);
675        }
676        Ok(ret)
677    }
678}
679
680impl<S: Encoder> Encodable<S> for sha256d::Hash {
681    fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
682        self.into_inner().consensus_encode(s)
683    }
684}
685
686impl<D: Decoder> Decodable<D> for sha256d::Hash {
687    fn consensus_decode(d: &mut D) -> Result<sha256d::Hash, self::Error> {
688        let inner: [u8; 32] = Decodable::consensus_decode(d)?;
689        Ok(sha256d::Hash::from_slice(&inner).unwrap())
690    }
691}
692
693// Tests
694#[cfg(test)]
695mod tests {
696    use super::{CheckedData, VarInt};
697
698    use super::{deserialize, serialize, Error};
699
700    #[test]
701    fn serialize_int_test() {
702        // bool
703        assert_eq!(serialize(&false), vec![0u8]);
704        assert_eq!(serialize(&true), vec![1u8]);
705        // u8
706        assert_eq!(serialize(&1u8), vec![1u8]);
707        assert_eq!(serialize(&0u8), vec![0u8]);
708        assert_eq!(serialize(&255u8), vec![255u8]);
709        // u16
710        assert_eq!(serialize(&1u16), vec![1u8, 0]);
711        assert_eq!(serialize(&256u16), vec![0u8, 1]);
712        assert_eq!(serialize(&5000u16), vec![136u8, 19]);
713        // u32
714        assert_eq!(serialize(&1u32), vec![1u8, 0, 0, 0]);
715        assert_eq!(serialize(&256u32), vec![0u8, 1, 0, 0]);
716        assert_eq!(serialize(&5000u32), vec![136u8, 19, 0, 0]);
717        assert_eq!(serialize(&500000u32), vec![32u8, 161, 7, 0]);
718        assert_eq!(serialize(&168430090u32), vec![10u8, 10, 10, 10]);
719        // TODO: test negative numbers
720        assert_eq!(serialize(&1i32), vec![1u8, 0, 0, 0]);
721        assert_eq!(serialize(&256i32), vec![0u8, 1, 0, 0]);
722        assert_eq!(serialize(&5000i32), vec![136u8, 19, 0, 0]);
723        assert_eq!(serialize(&500000i32), vec![32u8, 161, 7, 0]);
724        assert_eq!(serialize(&168430090i32), vec![10u8, 10, 10, 10]);
725        // u64
726        assert_eq!(serialize(&1u64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
727        assert_eq!(serialize(&256u64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
728        assert_eq!(serialize(&5000u64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
729        assert_eq!(serialize(&500000u64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
730        assert_eq!(serialize(&723401728380766730u64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
731        // TODO: test negative numbers
732        assert_eq!(serialize(&1i64), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
733        assert_eq!(serialize(&256i64), vec![0u8, 1, 0, 0, 0, 0, 0, 0]);
734        assert_eq!(serialize(&5000i64), vec![136u8, 19, 0, 0, 0, 0, 0, 0]);
735        assert_eq!(serialize(&500000i64), vec![32u8, 161, 7, 0, 0, 0, 0, 0]);
736        assert_eq!(serialize(&723401728380766730i64), vec![10u8, 10, 10, 10, 10, 10, 10, 10]);
737    }
738
739    #[test]
740    fn serialize_varint_test() {
741        assert_eq!(serialize(&VarInt(10)), vec![10u8]);
742        assert_eq!(serialize(&VarInt(0xFC)), vec![0xFCu8]);
743        assert_eq!(serialize(&VarInt(0xFD)), vec![0xFDu8, 0xFD, 0]);
744        assert_eq!(serialize(&VarInt(0xFFF)), vec![0xFDu8, 0xFF, 0xF]);
745        assert_eq!(serialize(&VarInt(0xF0F0F0F)), vec![0xFEu8, 0xF, 0xF, 0xF, 0xF]);
746        assert_eq!(serialize(&VarInt(0xF0F0F0F0F0E0)), vec![0xFFu8, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0, 0]);
747    }
748
749    #[test]
750    fn deserialize_nonminimal_vec() {
751        match deserialize::<Vec<u8>>(&[0xfd, 0x00, 0x00]) {
752            Err(Error::ParseFailed("non-minimal varint")) => {},
753            x => panic!(x)
754        }
755        match deserialize::<Vec<u8>>(&[0xfd, 0xfc, 0x00]) {
756            Err(Error::ParseFailed("non-minimal varint")) => {},
757            x => panic!(x)
758        }
759        match deserialize::<Vec<u8>>(&[0xfe, 0xff, 0x00, 0x00, 0x00]) {
760            Err(Error::ParseFailed("non-minimal varint")) => {},
761            x => panic!(x)
762        }
763        match deserialize::<Vec<u8>>(&[0xfe, 0xff, 0xff, 0x00, 0x00]) {
764            Err(Error::ParseFailed("non-minimal varint")) => {},
765            x => panic!(x)
766        }
767        match deserialize::<Vec<u8>>(&[0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) {
768            Err(Error::ParseFailed("non-minimal varint")) => {},
769            x => panic!(x)
770        }
771        match deserialize::<Vec<u8>>(&[0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00]) {
772            Err(Error::ParseFailed("non-minimal varint")) => {},
773            x => panic!(x)
774        }
775
776        let mut vec_256 = vec![0; 259];
777        vec_256[0] = 0xfd;
778        vec_256[1] = 0x00;
779        vec_256[2] = 0x01;
780        assert!(deserialize::<Vec<u8>>(&vec_256).is_ok());
781
782        let mut vec_253 = vec![0; 256];
783        vec_253[0] = 0xfd;
784        vec_253[1] = 0xfd;
785        vec_253[2] = 0x00;
786        assert!(deserialize::<Vec<u8>>(&vec_253).is_ok());
787    }
788
789    #[test]
790    fn serialize_checkeddata_test() {
791        let cd = CheckedData(vec![1u8, 2, 3, 4, 5]);
792        assert_eq!(serialize(&cd), vec![5, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
793    }
794
795    #[test]
796    fn serialize_vector_test() {
797        assert_eq!(serialize(&vec![1u8, 2, 3]), vec![3u8, 1, 2, 3]);
798        assert_eq!(serialize(&[1u8, 2, 3][..]), vec![3u8, 1, 2, 3]);
799        // TODO: test vectors of more interesting objects
800    }
801
802    #[test]
803    fn serialize_strbuf_test() {
804        assert_eq!(serialize(&"Andrew".to_string()), vec![6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]);
805    }
806
807    #[test]
808    fn serialize_box_test() {
809        assert_eq!(serialize(&Box::new(1u8)), vec![1u8]);
810        assert_eq!(serialize(&Box::new(1u16)), vec![1u8, 0]);
811        assert_eq!(serialize(&Box::new(1u64)), vec![1u8, 0, 0, 0, 0, 0, 0, 0]);
812    }
813
814    #[test]
815    fn serialize_option_test() {
816        assert_eq!(serialize(&None::<u8>), vec![0]);
817        assert_eq!(serialize(&Some(0xFFu8)), vec![1, 0xFF]);
818    }
819
820    #[test]
821    fn deserialize_int_test() {
822        // bool
823        assert!((deserialize(&[58u8, 0]) as Result<bool, _>).is_err());
824        assert_eq!(deserialize(&[58u8]).ok(), Some(true));
825        assert_eq!(deserialize(&[1u8]).ok(), Some(true));
826        assert_eq!(deserialize(&[0u8]).ok(), Some(false));
827        assert!((deserialize(&[0u8, 1]) as Result<bool, _>).is_err());
828
829        // u8
830        assert_eq!(deserialize(&[58u8]).ok(), Some(58u8));
831
832        // u16
833        assert_eq!(deserialize(&[0x01u8, 0x02]).ok(), Some(0x0201u16));
834        assert_eq!(deserialize(&[0xABu8, 0xCD]).ok(), Some(0xCDABu16));
835        assert_eq!(deserialize(&[0xA0u8, 0x0D]).ok(), Some(0xDA0u16));
836        let failure16: Result<u16, _> = deserialize(&[1u8]);
837        assert!(failure16.is_err());
838
839        // u32
840        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABu32));
841        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD]).ok(), Some(0xCDAB0DA0u32));
842        let failure32: Result<u32, _> = deserialize(&[1u8, 2, 3]);
843        assert!(failure32.is_err());
844        // TODO: test negative numbers
845        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0]).ok(), Some(0xCDABi32));
846        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0x2D]).ok(), Some(0x2DAB0DA0i32));
847        let failurei32: Result<i32, _> = deserialize(&[1u8, 2, 3]);
848        assert!(failurei32.is_err());
849
850        // u64
851        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABu64));
852        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(), Some(0x99000099CDAB0DA0u64));
853        let failure64: Result<u64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
854        assert!(failure64.is_err());
855        // TODO: test negative numbers
856        assert_eq!(deserialize(&[0xABu8, 0xCD, 0, 0, 0, 0, 0, 0]).ok(), Some(0xCDABi64));
857        assert_eq!(deserialize(&[0xA0u8, 0x0D, 0xAB, 0xCD, 0x99, 0, 0, 0x99]).ok(), Some(-0x66ffff663254f260i64));
858        let failurei64: Result<i64, _> = deserialize(&[1u8, 2, 3, 4, 5, 6, 7]);
859        assert!(failurei64.is_err());
860    }
861
862    #[test]
863    fn deserialize_vec_test() {
864        assert_eq!(deserialize(&[3u8, 2, 3, 4]).ok(), Some(vec![2u8, 3, 4]));
865        assert!((deserialize(&[4u8, 2, 3, 4, 5, 6]) as Result<Vec<u8>, _>).is_err());
866        // found by cargo fuzz
867        assert!(deserialize::<Vec<u64>>(&[0xff,0xff,0xff,0xff,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0xa,0xa,0x3a]).is_err());
868    }
869
870    #[test]
871    fn deserialize_strbuf_test() {
872        assert_eq!(deserialize(&[6u8, 0x41, 0x6e, 0x64, 0x72, 0x65, 0x77]).ok(), Some("Andrew".to_string()));
873    }
874
875    #[test]
876    fn deserialize_checkeddata_test() {
877        let cd: Result<CheckedData, _> = deserialize(&[5u8, 0, 0, 0, 162, 107, 175, 90, 1, 2, 3, 4, 5]);
878        assert_eq!(cd.ok(), Some(CheckedData(vec![1u8, 2, 3, 4, 5])));
879    }
880
881    #[test]
882    fn deserialize_option_test() {
883        let none: Result<Option<u8>, _> = deserialize(&[0u8]);
884        let good: Result<Option<u8>, _> = deserialize(&[1u8, 0xFF]);
885        let bad: Result<Option<u8>, _> = deserialize(&[2u8]);
886        assert!(bad.is_err());
887        assert_eq!(none.ok(), Some(None));
888        assert_eq!(good.ok(), Some(Some(0xFF)));
889    }
890
891    #[test]
892    fn deserialize_box_test() {
893        let zero: Result<Box<u8>, _> = deserialize(&[0u8]);
894        let one: Result<Box<u8>, _> = deserialize(&[1u8]);
895        assert_eq!(zero.ok(), Some(Box::new(0)));
896        assert_eq!(one.ok(), Some(Box::new(1)));
897    }
898}
899