Forward `consensus_decode` to `consensus_decode_from_finite_reader`

This commit is contained in:
Dawid Ciężarkiewicz 2022-05-30 18:14:09 -07:00
parent 9c754ca4de
commit a24a3b0194
4 changed files with 13 additions and 46 deletions

View File

@ -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: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
Ok(Script(Decodable::consensus_decode_from_finite_reader(r)?))
}
#[inline]
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
Self::consensus_decode_from_finite_reader(r.take(MAX_VEC_SIZE as u64).by_ref())
}
}
#[cfg(test)]

View File

@ -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: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
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: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
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

View File

@ -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<R: io::Read>(reader: &mut R) -> Result<Self, Error> {
// 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<R: io::Read>(reader: &mut R) -> Result<Self, Error>;
/// 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<R: io::Read>(reader: &mut R) -> Result<Self, Error> {
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<R: io::Read>(d: &mut R) -> Result<Self, Error> {
Self::consensus_decode_from_finite_reader(&mut d.take(MAX_VEC_SIZE as u64))
}
}
}
}
@ -687,11 +693,6 @@ impl Decodable for Vec<u8> {
// 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: io::Read>(r: &mut R) -> Result<Self, Error> {
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: io::Read>(r: &mut R) -> Result<Self, Error> {
<Vec<u8>>::consensus_decode_from_finite_reader(r).map(From::from)
}
#[inline]
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, Error> {
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<R: io::Read>(d: &mut R) -> Result<Self, Error> {
Self::consensus_decode_from_finite_reader(&mut d.take(MAX_VEC_SIZE as u64))
}
}
// References

View File

@ -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<R: io::Read>(d: &mut R) -> Result<Self, encode::Error> {
Self::consensus_decode_from_finite_reader(d.take(MAX_VEC_SIZE as u64).by_ref())
}
}
#[cfg(test)]