From a24a3b01941b6661e30ef910b5d6e9699752b313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Mon, 30 May 2022 18:14:09 -0700 Subject: [PATCH] Forward `consensus_decode` to `consensus_decode_from_finite_reader` --- src/blockdata/script.rs | 7 ------- src/blockdata/transaction.rs | 11 ----------- src/consensus/encode.rs | 34 +++++++++++++--------------------- src/util/psbt/mod.rs | 7 ------- 4 files changed, 13 insertions(+), 46 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index bb6e757f..16b9fcae 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -23,11 +23,9 @@ //! This module provides the structures and functions needed to support scripts. //! -use crate::consensus::encode::MAX_VEC_SIZE; use crate::prelude::*; use crate::io; -use io::Read as _; use core::{fmt, default::Default}; use core::ops::Index; @@ -1103,11 +1101,6 @@ impl Decodable for Script { fn consensus_decode_from_finite_reader(r: &mut R) -> Result { Ok(Script(Decodable::consensus_decode_from_finite_reader(r)?)) } - - #[inline] - fn consensus_decode(r: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(r.take(MAX_VEC_SIZE as u64).by_ref()) - } } #[cfg(test)] diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 105ff285..1311d175 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -26,7 +26,6 @@ use crate::prelude::*; use crate::io; -use io::Read as _; use core::{fmt, str, default::Default}; use crate::hashes::{self, Hash, sha256d}; @@ -38,7 +37,6 @@ use crate::blockdata::constants::WITNESS_SCALE_FACTOR; use crate::blockdata::script::Script; use crate::blockdata::witness::Witness; use crate::consensus::{encode, Decodable, Encodable}; -use crate::consensus::encode::MAX_VEC_SIZE; use crate::hash_types::{Sighash, Txid, Wtxid}; use crate::VarInt; @@ -687,11 +685,6 @@ impl Decodable for TxIn { witness: Witness::default(), }) } - - #[inline] - fn consensus_decode(r: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(r.take(MAX_VEC_SIZE as u64).by_ref()) - } } impl Encodable for Transaction { @@ -763,10 +756,6 @@ impl Decodable for Transaction { }) } } - - fn consensus_decode(r: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(&mut r.take(MAX_VEC_SIZE as u64)) - } } /// This type is consensus valid but an input including it would prevent the transaction from diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index b2b254c9..0ddcd46a 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -353,6 +353,7 @@ pub trait Decodable: Sized { /// should also implement it applying same rules, and in addition make sure to call /// `consensus_decode_from_finite_reader` on all members, to avoid creating redundant /// `Take` wrappers. Failure to do so might result only in a tiny performance hit. + #[inline] fn consensus_decode_from_finite_reader(reader: &mut R) -> Result { // This method is always strictly less general than, `consensus_decode`, // so it's safe and make sense to default to just calling it. @@ -361,8 +362,18 @@ pub trait Decodable: Sized { Self::consensus_decode(reader) } - /// Decode an object with a well-defined format - fn consensus_decode(reader: &mut R) -> Result; + /// Decode an object with a well-defined format. + /// + /// This is the method that should be implemented for a typical, fixed sized type + /// implementing this trait. Default implementation is wrapping the reader + /// in [`crate::io::Take`] to limit the input size to [`MAX_VEC_SIZE`], and forwards the call to + /// [`Self::consensus_decode_from_finite_reader`], which is convenient + /// for types that override [`Self::consensus_decode_from_finite_reader`] + /// instead. + #[inline] + fn consensus_decode(reader: &mut R) -> Result { + Self::consensus_decode_from_finite_reader(reader.take(MAX_VEC_SIZE as u64).by_ref()) + } } /// A variable-length unsigned integer @@ -616,11 +627,6 @@ macro_rules! impl_vec { } Ok(ret) } - - #[inline] - fn consensus_decode(d: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(&mut d.take(MAX_VEC_SIZE as u64)) - } } } } @@ -687,11 +693,6 @@ impl Decodable for Vec { // most real-world vec of bytes data, wouldn't be larger than 128KiB read_bytes_from_finite_reader(r, ReadBytesFromFiniteReaderOpts { len, chunk_size: 128 * 1024 }) } - - #[inline] - fn consensus_decode(r: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(&mut r.take(MAX_VEC_SIZE as u64)) - } } impl Encodable for Box<[u8]> { @@ -706,11 +707,6 @@ impl Decodable for Box<[u8]> { fn consensus_decode_from_finite_reader(r: &mut R) -> Result { >::consensus_decode_from_finite_reader(r).map(From::from) } - - #[inline] - fn consensus_decode(r: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(&mut r.take(MAX_VEC_SIZE as u64)) - } } @@ -748,10 +744,6 @@ impl Decodable for CheckedData { Ok(CheckedData(ret)) } } - - fn consensus_decode(d: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(&mut d.take(MAX_VEC_SIZE as u64)) - } } // References diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index b750671e..f874c55b 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -23,14 +23,12 @@ use core::cmp; use crate::blockdata::script::Script; use crate::blockdata::transaction::{ TxOut, Transaction}; -use crate::consensus::encode::MAX_VEC_SIZE; use crate::consensus::{encode, Encodable, Decodable}; pub use crate::util::sighash::Prevouts; use crate::prelude::*; use crate::io; -use io::Read as _; mod error; pub use self::error::Error; @@ -342,11 +340,6 @@ impl Decodable for PartiallySignedTransaction { global.outputs = outputs; Ok(global) } - - #[inline] - fn consensus_decode(d: &mut R) -> Result { - Self::consensus_decode_from_finite_reader(d.take(MAX_VEC_SIZE as u64).by_ref()) - } } #[cfg(test)]