1use 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#[derive(Debug)]
40#[non_exhaustive]
41pub enum Error {
42 Io(io::Error),
44 OversizedVectorAllocation {
46 requested: usize,
48 max: usize,
50 },
51 InvalidChecksum {
53 expected: [u8; 4],
55 actual: [u8; 4],
57 },
58 NonMinimalVarInt,
60 ParseFailed(&'static str),
62 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#[derive(Debug)]
108pub enum FromHexError {
109 OddLengthString(OddLengthStringError),
111 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
144pub 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
152pub fn serialize_hex<T: Encodable + ?Sized>(data: &T) -> String {
154 serialize(data).to_lower_hex_string()
155}
156
157pub fn deserialize<T: Decodable>(data: &[u8]) -> Result<T, Error> {
160 let (rv, consumed) = deserialize_partial(data)?;
161
162 if consumed == data.len() {
164 Ok(rv)
165 } else {
166 Err(Error::ParseFailed("data not consumed entirely when explicitly deserializing"))
167 }
168}
169
170pub 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
178pub 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
188pub trait WriteExt: Write {
190 fn emit_u64(&mut self, v: u64) -> Result<(), io::Error>;
192 fn emit_u32(&mut self, v: u32) -> Result<(), io::Error>;
194 fn emit_u16(&mut self, v: u16) -> Result<(), io::Error>;
196 fn emit_u8(&mut self, v: u8) -> Result<(), io::Error>;
198
199 fn emit_i64(&mut self, v: i64) -> Result<(), io::Error>;
201 fn emit_i32(&mut self, v: i32) -> Result<(), io::Error>;
203 fn emit_i16(&mut self, v: i16) -> Result<(), io::Error>;
205 fn emit_i8(&mut self, v: i8) -> Result<(), io::Error>;
207
208 fn emit_bool(&mut self, v: bool) -> Result<(), io::Error>;
210
211 fn emit_slice(&mut self, v: &[u8]) -> Result<(), io::Error>;
213}
214
215pub trait ReadExt: Read {
217 fn read_u64(&mut self) -> Result<u64, Error>;
219 fn read_u32(&mut self) -> Result<u32, Error>;
221 fn read_u16(&mut self) -> Result<u16, Error>;
223 fn read_u8(&mut self) -> Result<u8, Error>;
225
226 fn read_i64(&mut self) -> Result<i64, Error>;
228 fn read_i32(&mut self) -> Result<i32, Error>;
230 fn read_i16(&mut self) -> Result<i16, Error>;
232 fn read_i8(&mut self) -> Result<i8, Error>;
234
235 fn read_bool(&mut self) -> Result<bool, Error>;
237
238 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
308pub const MAX_VEC_SIZE: usize = 4_000_000;
310
311pub trait Encodable {
313 fn consensus_encode<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error>;
320}
321
322pub trait Decodable: Sized {
324 #[inline]
353 fn consensus_decode_from_finite_reader<R: Read + ?Sized>(
354 reader: &mut R,
355 ) -> Result<Self, Error> {
356 Self::consensus_decode(reader)
360 }
361
362 #[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#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
378pub struct VarInt(pub u64);
379
380#[derive(PartialEq, Eq, Clone, Debug)]
382pub struct CheckedData {
383 data: Vec<u8>,
384 checksum: [u8; 4],
385}
386
387impl CheckedData {
388 pub fn new(data: Vec<u8>) -> Self {
390 let checksum = sha2_checksum(&data);
391 Self { data, checksum }
392 }
393
394 pub fn data(&self) -> &[u8] { &self.data }
396
397 pub fn into_data(self) -> Vec<u8> { self.data }
399
400 pub fn checksum(&self) -> [u8; 4] { self.checksum }
402}
403
404macro_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)] impl VarInt {
439 #[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
453macro_rules! impl_var_int_from {
457 ($($ty:tt),*) => {
458 $(
459 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 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#[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 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
769fn 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#[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 assert_eq!(serialize(&false), vec![0u8]);
910 assert_eq!(serialize(&true), vec![1u8]);
911 assert_eq!(serialize(&1u8), vec![1u8]);
913 assert_eq!(serialize(&0u8), vec![0u8]);
914 assert_eq!(serialize(&255u8), vec![255u8]);
915 assert_eq!(serialize(&1u16), vec![1u8, 0]);
917 assert_eq!(serialize(&256u16), vec![0u8, 1]);
918 assert_eq!(serialize(&5000u16), vec![136u8, 19]);
919 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 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 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 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_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 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 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 assert_eq!(deserialize(&[58u8]).ok(), Some(58u8));
1093
1094 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 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 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 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 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 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 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 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"); 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(); hex.push_str("abcdef");
1306 assert!(matches!(
1307 deserialize_hex::<Transaction>(&hex).unwrap_err(),
1308 FromHexError::Decode(DecodeError::TooManyBytes)
1309 ));
1310 }
1311}