Skip to content

Commit 3a94e06

Browse files
committed
Merge rust-bitcoin#5107: Rename min_bytes_needed to read_limit
38b47c7 Update API (Nick Johnson) ac51770 consensus_encoding: rename min_bytes_needed (Nick Johnson) Pull request description: The `min_bytes_needed` name is a bit of a misnomer because it implies a lower bound instead of an upper. I believe the original intention was to capture "minimum bytes needed to advance the state of the decoder", but that is a little wordy. Updated to `read_limit` since this is an upper bound to protect against over consuming bytes the decoder doesn't know what to do with. ACKs for top commit: apoelstra: ACK 38b47c7; successfully ran local tests tcharding: ACK 38b47c7 Tree-SHA512: 29f7d6d4bf9905c6cc8ee3f13d35c18326e91aa681916524a6d3ce8466088a544c727a0b5cb876144508f25d306daa020f3a2c5e08ae5fc42fc27008066584cd
2 parents 85ba847 + 38b47c7 commit 3a94e06

File tree

9 files changed

+37
-38
lines changed

9 files changed

+37
-38
lines changed

api/units/all-features.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,9 +1900,9 @@ pub fn bitcoin_units::Weight::try_from(s: alloc::boxed::Box<str>) -> core::resul
19001900
pub fn bitcoin_units::Weight::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
19011901
pub fn bitcoin_units::amount::AmountDecoder::default() -> Self
19021902
pub fn bitcoin_units::amount::AmountDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
1903-
pub fn bitcoin_units::amount::AmountDecoder::min_bytes_needed(&self) -> usize
19041903
pub fn bitcoin_units::amount::AmountDecoder::new() -> Self
19051904
pub fn bitcoin_units::amount::AmountDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
1905+
pub fn bitcoin_units::amount::AmountDecoder::read_limit(&self) -> usize
19061906
pub fn bitcoin_units::amount::AmountEncoder::advance(&mut self) -> bool
19071907
pub fn bitcoin_units::amount::AmountEncoder::current_chunk(&self) -> core::option::Option<&[u8]>
19081908
pub fn bitcoin_units::amount::Denomination::arbitrary(u: &mut arbitrary::unstructured::Unstructured<'a>) -> arbitrary::error::Result<Self>
@@ -2014,9 +2014,9 @@ pub fn bitcoin_units::block::BlockHeight::try_from(s: alloc::boxed::Box<str>) ->
20142014
pub fn bitcoin_units::block::BlockHeight::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
20152015
pub fn bitcoin_units::block::BlockHeightDecoder::default() -> Self
20162016
pub fn bitcoin_units::block::BlockHeightDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2017-
pub fn bitcoin_units::block::BlockHeightDecoder::min_bytes_needed(&self) -> usize
20182017
pub fn bitcoin_units::block::BlockHeightDecoder::new() -> Self
20192018
pub fn bitcoin_units::block::BlockHeightDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
2019+
pub fn bitcoin_units::block::BlockHeightDecoder::read_limit(&self) -> usize
20202020
pub fn bitcoin_units::block::BlockHeightDecoderError::clone(&self) -> bitcoin_units::block::BlockHeightDecoderError
20212021
pub fn bitcoin_units::block::BlockHeightDecoderError::eq(&self, other: &bitcoin_units::block::BlockHeightDecoderError) -> bool
20222022
pub fn bitcoin_units::block::BlockHeightDecoderError::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
@@ -2164,9 +2164,9 @@ pub fn bitcoin_units::locktime::absolute::LockTime::try_from(s: alloc::boxed::Bo
21642164
pub fn bitcoin_units::locktime::absolute::LockTime::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
21652165
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::default() -> Self
21662166
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2167-
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::min_bytes_needed(&self) -> usize
21682167
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::new() -> Self
21692168
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
2169+
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::read_limit(&self) -> usize
21702170
pub fn bitcoin_units::locktime::absolute::LockTimeEncoder::advance(&mut self) -> bool
21712171
pub fn bitcoin_units::locktime::absolute::LockTimeEncoder::current_chunk(&self) -> core::option::Option<&[u8]>
21722172
pub fn bitcoin_units::locktime::absolute::MedianTimePast::arbitrary(u: &mut arbitrary::unstructured::Unstructured<'a>) -> arbitrary::error::Result<Self>
@@ -2430,9 +2430,9 @@ pub fn bitcoin_units::sequence::Sequence::try_from(s: alloc::boxed::Box<str>) ->
24302430
pub fn bitcoin_units::sequence::Sequence::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
24312431
pub fn bitcoin_units::sequence::SequenceDecoder::default() -> Self
24322432
pub fn bitcoin_units::sequence::SequenceDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2433-
pub fn bitcoin_units::sequence::SequenceDecoder::min_bytes_needed(&self) -> usize
24342433
pub fn bitcoin_units::sequence::SequenceDecoder::new() -> Self
24352434
pub fn bitcoin_units::sequence::SequenceDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
2435+
pub fn bitcoin_units::sequence::SequenceDecoder::read_limit(&self) -> usize
24362436
pub fn bitcoin_units::sequence::SequenceDecoderError::clone(&self) -> bitcoin_units::sequence::SequenceDecoderError
24372437
pub fn bitcoin_units::sequence::SequenceDecoderError::eq(&self, other: &bitcoin_units::sequence::SequenceDecoderError) -> bool
24382438
pub fn bitcoin_units::sequence::SequenceDecoderError::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
@@ -2442,9 +2442,9 @@ pub fn bitcoin_units::sequence::SequenceEncoder::advance(&mut self) -> bool
24422442
pub fn bitcoin_units::sequence::SequenceEncoder::current_chunk(&self) -> core::option::Option<&[u8]>
24432443
pub fn bitcoin_units::time::BlockTimeDecoder::default() -> Self
24442444
pub fn bitcoin_units::time::BlockTimeDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2445-
pub fn bitcoin_units::time::BlockTimeDecoder::min_bytes_needed(&self) -> usize
24462445
pub fn bitcoin_units::time::BlockTimeDecoder::new() -> Self
24472446
pub fn bitcoin_units::time::BlockTimeDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
2447+
pub fn bitcoin_units::time::BlockTimeDecoder::read_limit(&self) -> usize
24482448
pub fn bitcoin_units::time::BlockTimeDecoderError::clone(&self) -> bitcoin_units::time::BlockTimeDecoderError
24492449
pub fn bitcoin_units::time::BlockTimeDecoderError::eq(&self, other: &bitcoin_units::time::BlockTimeDecoderError) -> bool
24502450
pub fn bitcoin_units::time::BlockTimeDecoderError::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result

consensus_encoding/src/decode/decoders.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ impl Decoder for ByteVecDecoder {
9595
}
9696
}
9797

98-
fn min_bytes_needed(&self) -> usize {
98+
fn read_limit(&self) -> usize {
9999
if let Some(prefix_decoder) = &self.prefix_decoder {
100-
prefix_decoder.min_bytes_needed()
100+
prefix_decoder.read_limit()
101101
} else {
102102
self.bytes_expected - self.bytes_written
103103
}
@@ -193,20 +193,20 @@ impl<T: Decodable> Decoder for VecDecoder<T> {
193193
}
194194
}
195195

196-
fn min_bytes_needed(&self) -> usize {
196+
fn read_limit(&self) -> usize {
197197
if let Some(prefix_decoder) = &self.prefix_decoder {
198-
prefix_decoder.min_bytes_needed()
198+
prefix_decoder.read_limit()
199199
} else if let Some(decoder) = &self.decoder {
200-
decoder.min_bytes_needed()
200+
decoder.read_limit()
201201
} else if self.buffer.len() == self.length {
202202
// Totally done.
203203
0
204204
} else {
205205
let items_left_to_decode = self.length - self.buffer.len();
206206
let decoder = T::decoder();
207207
// This could be inaccurate (eg 1 for a `ByteVecDecoder`) but its the best we can do.
208-
let min_per_decoder = decoder.min_bytes_needed();
209-
items_left_to_decode * min_per_decoder
208+
let limit_per_decoder = decoder.read_limit();
209+
items_left_to_decode * limit_per_decoder
210210
}
211211
}
212212
}
@@ -274,7 +274,7 @@ impl<const N: usize> Decoder for ArrayDecoder<N> {
274274
}
275275

276276
#[inline]
277-
fn min_bytes_needed(&self) -> usize { N - self.bytes_written }
277+
fn read_limit(&self) -> usize { N - self.bytes_written }
278278
}
279279

280280
/// A decoder which wraps two inner decoders and returns the output of both.
@@ -395,11 +395,11 @@ where
395395
}
396396

397397
#[inline]
398-
fn min_bytes_needed(&self) -> usize {
398+
fn read_limit(&self) -> usize {
399399
match &self.state {
400400
Decoder2State::First(first_decoder, second_decoder) =>
401-
first_decoder.min_bytes_needed() + second_decoder.min_bytes_needed(),
402-
Decoder2State::Second(_, second_decoder) => second_decoder.min_bytes_needed(),
401+
first_decoder.read_limit() + second_decoder.read_limit(),
402+
Decoder2State::Second(_, second_decoder) => second_decoder.read_limit(),
403403
Decoder2State::Errored => 0,
404404
Decoder2State::Transitioning => 0,
405405
}
@@ -453,7 +453,7 @@ where
453453
}
454454

455455
#[inline]
456-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
456+
fn read_limit(&self) -> usize { self.inner.read_limit() }
457457
}
458458

459459
/// A decoder which decodes four objects, one after the other.
@@ -509,7 +509,7 @@ where
509509
}
510510

511511
#[inline]
512-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
512+
fn read_limit(&self) -> usize { self.inner.read_limit() }
513513
}
514514

515515
/// A decoder which decodes six objects, one after the other.
@@ -590,7 +590,7 @@ where
590590
}
591591

592592
#[inline]
593-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
593+
fn read_limit(&self) -> usize { self.inner.read_limit() }
594594
}
595595

596596
/// Decodes a compact size encoded integer.
@@ -679,7 +679,7 @@ impl Decoder for CompactSizeDecoder {
679679
}
680680
}
681681

682-
fn min_bytes_needed(&self) -> usize {
682+
fn read_limit(&self) -> usize {
683683
match self.buf.len() {
684684
0 => 1,
685685
already_read => match self.buf[0] {
@@ -984,7 +984,7 @@ mod tests {
984984
Ok(Inner(n))
985985
}
986986

987-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
987+
fn read_limit(&self) -> usize { self.0.read_limit() }
988988
}
989989

990990
#[cfg(feature = "alloc")]
@@ -1016,7 +1016,7 @@ mod tests {
10161016
Ok(Test(v))
10171017
}
10181018

1019-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
1019+
fn read_limit(&self) -> usize { self.0.read_limit() }
10201020
}
10211021

10221022
#[cfg(feature = "alloc")]

consensus_encoding/src/decode/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ pub trait Decoder: Sized {
5959
#[must_use = "must check result to avoid panics on subsequent calls"]
6060
fn end(self) -> Result<Self::Output, Self::Error>;
6161

62-
/// Returns the minimum number of bytes needed to advance the state of the
63-
/// decoder while ensuring there are no over-reads.
62+
/// Returns the maximum number of bytes this decoder can consume without over-reading.
6463
///
6564
/// Returns 0 if the decoder is complete and ready to finalize with [`Self::end`].
6665
/// This is used by [`decode_from_read_unbuffered`] to optimize read sizes,
67-
/// avoiding both inefficient under reads and unnecessary over-reads.
68-
fn min_bytes_needed(&self) -> usize;
66+
/// avoiding both inefficient under-reads and unnecessary over-reads.
67+
fn read_limit(&self) -> usize;
6968
}
7069

7170
/// Decodes an object from a byte slice.
@@ -192,9 +191,9 @@ where
192191
let mut decoder = T::decoder();
193192
let mut buffer = [0u8; BUFFER_SIZE];
194193

195-
while decoder.min_bytes_needed() > 0 {
194+
while decoder.read_limit() > 0 {
196195
// Only read what we need, up to buffer size.
197-
let clamped_buffer = &mut buffer[..decoder.min_bytes_needed().min(BUFFER_SIZE)];
196+
let clamped_buffer = &mut buffer[..decoder.read_limit().min(BUFFER_SIZE)];
198197
match reader.read(clamped_buffer) {
199198
Ok(0) => {
200199
// EOF, but still try to finalize the decoder.
@@ -288,7 +287,7 @@ mod tests {
288287

289288
fn end(self) -> Result<Self::Output, Self::Error> { self.inner.end().map(TestArray) }
290289

291-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
290+
fn read_limit(&self) -> usize { self.inner.read_limit() }
292291
}
293292

294293
#[test]

consensus_encoding/tests/composition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Decoder for CompositeDataDecoder {
6969
Ok(CompositeData { first, second })
7070
}
7171

72-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
72+
fn read_limit(&self) -> usize { self.inner.read_limit() }
7373
}
7474

7575
impl Decodable for CompositeData {
@@ -220,7 +220,7 @@ fn composition_error_unification() {
220220
Ok((first, second))
221221
}
222222

223-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
223+
fn read_limit(&self) -> usize { self.inner.read_limit() }
224224
}
225225

226226
/// Another test composite decoder.
@@ -245,7 +245,7 @@ fn composition_error_unification() {
245245
Ok(result)
246246
}
247247

248-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
248+
fn read_limit(&self) -> usize { self.inner.read_limit() }
249249
}
250250

251251
/// A decoder which can fail.
@@ -274,7 +274,7 @@ fn composition_error_unification() {
274274
}
275275
}
276276

277-
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
277+
fn read_limit(&self) -> usize { self.inner.read_limit() }
278278
}
279279

280280
// A multi-layer, nested, decoder structure with a unified top level error type.

units/src/amount/unsigned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl encoding::Decoder for AmountDecoder {
610610
}
611611

612612
#[inline]
613-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
613+
fn read_limit(&self) -> usize { self.0.read_limit() }
614614
}
615615

616616
#[cfg(feature = "encoding")]

units/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl encoding::Decoder for BlockHeightDecoder {
193193
}
194194

195195
#[inline]
196-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
196+
fn read_limit(&self) -> usize { self.0.read_limit() }
197197
}
198198

199199
#[cfg(feature = "encoding")]

units/src/locktime/absolute/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl encoding::Decoder for LockTimeDecoder {
451451
}
452452

453453
#[inline]
454-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
454+
fn read_limit(&self) -> usize { self.0.read_limit() }
455455
}
456456

457457
#[cfg(feature = "encoding")]

units/src/sequence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl encoding::Decoder for SequenceDecoder {
316316
}
317317

318318
#[inline]
319-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
319+
fn read_limit(&self) -> usize { self.0.read_limit() }
320320
}
321321

322322
#[cfg(feature = "encoding")]

units/src/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl encoding::Decoder for BlockTimeDecoder {
126126
}
127127

128128
#[inline]
129-
fn min_bytes_needed(&self) -> usize { self.0.min_bytes_needed() }
129+
fn read_limit(&self) -> usize { self.0.read_limit() }
130130
}
131131

132132
#[cfg(feature = "encoding")]

0 commit comments

Comments
 (0)