Skip to content

Commit 78d26f8

Browse files
committed
Forward consensus_decode to consensus_decode_from_finite_reader
1 parent 96a8eb8 commit 78d26f8

File tree

4 files changed

+13
-46
lines changed

4 files changed

+13
-46
lines changed

src/blockdata/script.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
//! This module provides the structures and functions needed to support scripts.
2424
//!
2525
26-
use crate::consensus::encode::MAX_VEC_SIZE;
2726
use crate::prelude::*;
2827

2928
use crate::io;
30-
use io::Read as _;
3129
use core::{fmt, default::Default};
3230
use core::ops::Index;
3331

@@ -1103,11 +1101,6 @@ impl Decodable for Script {
11031101
fn consensus_decode_from_finite_reader<R: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
11041102
Ok(Script(Decodable::consensus_decode_from_finite_reader(r)?))
11051103
}
1106-
1107-
#[inline]
1108-
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
1109-
Self::consensus_decode_from_finite_reader(r.take(MAX_VEC_SIZE as u64).by_ref())
1110-
}
11111104
}
11121105

11131106
#[cfg(test)]

src/blockdata/transaction.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use crate::prelude::*;
2727

2828
use crate::io;
29-
use io::Read as _;
3029
use core::{fmt, str, default::Default};
3130

3231
use crate::hashes::{self, Hash, sha256d};
@@ -38,7 +37,6 @@ use crate::blockdata::constants::WITNESS_SCALE_FACTOR;
3837
use crate::blockdata::script::Script;
3938
use crate::blockdata::witness::Witness;
4039
use crate::consensus::{encode, Decodable, Encodable};
41-
use crate::consensus::encode::MAX_VEC_SIZE;
4240
use crate::hash_types::{Sighash, Txid, Wtxid};
4341
use crate::VarInt;
4442

@@ -687,11 +685,6 @@ impl Decodable for TxIn {
687685
witness: Witness::default(),
688686
})
689687
}
690-
691-
#[inline]
692-
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
693-
Self::consensus_decode_from_finite_reader(r.take(MAX_VEC_SIZE as u64).by_ref())
694-
}
695688
}
696689

697690
impl Encodable for Transaction {
@@ -763,10 +756,6 @@ impl Decodable for Transaction {
763756
})
764757
}
765758
}
766-
767-
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, encode::Error> {
768-
Self::consensus_decode_from_finite_reader(&mut r.take(MAX_VEC_SIZE as u64))
769-
}
770759
}
771760

772761
/// This type is consensus valid but an input including it would prevent the transaction from

src/consensus/encode.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ pub trait Decodable: Sized {
353353
/// should also implement it applying same rules, and in addition make sure to call
354354
/// `consensus_decode_from_finite_reader` on all members, to avoid creating redundant
355355
/// `Take` wrappers. Failure to do so might result only in a tiny performance hit.
356+
#[inline]
356357
fn consensus_decode_from_finite_reader<R: io::Read>(reader: &mut R) -> Result<Self, Error> {
357358
// This method is always strictly less general than, `consensus_decode`,
358359
// so it's safe and make sense to default to just calling it.
@@ -361,8 +362,18 @@ pub trait Decodable: Sized {
361362
Self::consensus_decode(reader)
362363
}
363364

364-
/// Decode an object with a well-defined format
365-
fn consensus_decode<R: io::Read>(reader: &mut R) -> Result<Self, Error>;
365+
/// Decode an object with a well-defined format.
366+
///
367+
/// This is the method that should be implemented for a typical, fixed sized type
368+
/// implementing this trait. Default implementation is wrapping the reader
369+
/// in [`crate::io::Take`] to limit the input size to [`MAX_VEC_SIZE`], and forwards the call to
370+
/// [`Self::consensus_decode_from_finite_reader`], which is convenient
371+
/// for types that override [`Self::consensus_decode_from_finite_reader`]
372+
/// instead.
373+
#[inline]
374+
fn consensus_decode<R: io::Read>(reader: &mut R) -> Result<Self, Error> {
375+
Self::consensus_decode_from_finite_reader(reader.take(MAX_VEC_SIZE as u64).by_ref())
376+
}
366377
}
367378

368379
/// A variable-length unsigned integer
@@ -616,11 +627,6 @@ macro_rules! impl_vec {
616627
}
617628
Ok(ret)
618629
}
619-
620-
#[inline]
621-
fn consensus_decode<R: io::Read>(d: &mut R) -> Result<Self, Error> {
622-
Self::consensus_decode_from_finite_reader(&mut d.take(MAX_VEC_SIZE as u64))
623-
}
624630
}
625631
}
626632
}
@@ -687,11 +693,6 @@ impl Decodable for Vec<u8> {
687693
// most real-world vec of bytes data, wouldn't be larger than 128KiB
688694
read_bytes_from_finite_reader(r, ReadBytesFromFiniteReaderOpts { len, chunk_size: 128 * 1024 })
689695
}
690-
691-
#[inline]
692-
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, Error> {
693-
Self::consensus_decode_from_finite_reader(&mut r.take(MAX_VEC_SIZE as u64))
694-
}
695696
}
696697

697698
impl Encodable for Box<[u8]> {
@@ -706,11 +707,6 @@ impl Decodable for Box<[u8]> {
706707
fn consensus_decode_from_finite_reader<R: io::Read>(r: &mut R) -> Result<Self, Error> {
707708
<Vec<u8>>::consensus_decode_from_finite_reader(r).map(From::from)
708709
}
709-
710-
#[inline]
711-
fn consensus_decode<R: io::Read>(r: &mut R) -> Result<Self, Error> {
712-
Self::consensus_decode_from_finite_reader(&mut r.take(MAX_VEC_SIZE as u64))
713-
}
714710
}
715711

716712

@@ -748,10 +744,6 @@ impl Decodable for CheckedData {
748744
Ok(CheckedData(ret))
749745
}
750746
}
751-
752-
fn consensus_decode<R: io::Read>(d: &mut R) -> Result<Self, Error> {
753-
Self::consensus_decode_from_finite_reader(&mut d.take(MAX_VEC_SIZE as u64))
754-
}
755747
}
756748

757749
// References

src/util/psbt/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ use core::cmp;
2323

2424
use crate::blockdata::script::Script;
2525
use crate::blockdata::transaction::{ TxOut, Transaction};
26-
use crate::consensus::encode::MAX_VEC_SIZE;
2726
use crate::consensus::{encode, Encodable, Decodable};
2827
pub use crate::util::sighash::Prevouts;
2928

3029
use crate::prelude::*;
3130

3231
use crate::io;
33-
use io::Read as _;
3432
mod error;
3533
pub use self::error::Error;
3634

@@ -342,11 +340,6 @@ impl Decodable for PartiallySignedTransaction {
342340
global.outputs = outputs;
343341
Ok(global)
344342
}
345-
346-
#[inline]
347-
fn consensus_decode<R: io::Read>(d: &mut R) -> Result<Self, encode::Error> {
348-
Self::consensus_decode_from_finite_reader(d.take(MAX_VEC_SIZE as u64).by_ref())
349-
}
350343
}
351344

352345
#[cfg(test)]

0 commit comments

Comments
 (0)