Skip to content

Commit 083230f

Browse files
committed
Merge rust-bitcoin#5030: Add decoder I/O drivers
0ac8e91 consensus_encoding: API update (Nick Johnson) 93a085b consensus_encoding: add read interface for decoders (Nick Johnson) 9fb668e consensus_encoding: add decoder I/O drivers (Nick Johnson) Pull request description: Getting the ball rolling on I/O drivers for the decode side, which I think is going to be more interesting than the encode side since the buffered interface is different. ## First Patch The simple drivers for slice and bufreader. These are water'd down versions of push_decode. Some simplifications were made like making `ReadError` less generic by hard-coding to the std::io error type. But this does get right to the chase for the decode-side complexity: is it cool to have a separate driver for `BufReader` and `Read`? ## Second Patch The complex driver for `Read`. Following push_decode's design, introduced a new requirement on the `Decoder`'s to expose a `min_bytes_needed`. This allows the read driver to efficiently manage its internal buffer. I just put this requirement straight on `Decoder` because I think we can assume all bitcoin decodables will know their min bytes. ACKs for top commit: tcharding: ACK 0ac8e91 apoelstra: ACK 0ac8e91; successfully ran local tests Tree-SHA512: da5b0ca57e07338db11a1db73370bd21fe06ae699e971f3184780fd5dd7707b8992ea0b2d8a0944e25ebc047c4d93344b16388bace917b009e5007bdd0ef8fed
2 parents 3abd567 + 0ac8e91 commit 083230f

File tree

10 files changed

+401
-1
lines changed

10 files changed

+401
-1
lines changed

api/units/all-features.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,7 @@ pub fn bitcoin_units::Weight::try_from(s: alloc::boxed::Box<str>) -> core::resul
18931893
pub fn bitcoin_units::Weight::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
18941894
pub fn bitcoin_units::amount::AmountDecoder::default() -> Self
18951895
pub fn bitcoin_units::amount::AmountDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
1896+
pub fn bitcoin_units::amount::AmountDecoder::min_bytes_needed(&self) -> usize
18961897
pub fn bitcoin_units::amount::AmountDecoder::new() -> Self
18971898
pub fn bitcoin_units::amount::AmountDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
18981899
pub fn bitcoin_units::amount::Denomination::arbitrary(u: &mut arbitrary::unstructured::Unstructured<'a>) -> arbitrary::error::Result<Self>
@@ -2004,6 +2005,7 @@ pub fn bitcoin_units::block::BlockHeight::try_from(s: alloc::boxed::Box<str>) ->
20042005
pub fn bitcoin_units::block::BlockHeight::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
20052006
pub fn bitcoin_units::block::BlockHeightDecoder::default() -> Self
20062007
pub fn bitcoin_units::block::BlockHeightDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2008+
pub fn bitcoin_units::block::BlockHeightDecoder::min_bytes_needed(&self) -> usize
20072009
pub fn bitcoin_units::block::BlockHeightDecoder::new() -> Self
20082010
pub fn bitcoin_units::block::BlockHeightDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
20092011
pub fn bitcoin_units::block::BlockHeightDecoderError::clone(&self) -> bitcoin_units::block::BlockHeightDecoderError
@@ -2153,6 +2155,7 @@ pub fn bitcoin_units::locktime::absolute::LockTime::try_from(s: alloc::boxed::Bo
21532155
pub fn bitcoin_units::locktime::absolute::LockTime::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
21542156
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::default() -> Self
21552157
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2158+
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::min_bytes_needed(&self) -> usize
21562159
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::new() -> Self
21572160
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
21582161
pub fn bitcoin_units::locktime::absolute::LockTimeEncoder::advance(&mut self) -> bool
@@ -2418,6 +2421,7 @@ pub fn bitcoin_units::sequence::Sequence::try_from(s: alloc::boxed::Box<str>) ->
24182421
pub fn bitcoin_units::sequence::Sequence::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
24192422
pub fn bitcoin_units::sequence::SequenceDecoder::default() -> Self
24202423
pub fn bitcoin_units::sequence::SequenceDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2424+
pub fn bitcoin_units::sequence::SequenceDecoder::min_bytes_needed(&self) -> usize
24212425
pub fn bitcoin_units::sequence::SequenceDecoder::new() -> Self
24222426
pub fn bitcoin_units::sequence::SequenceDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
24232427
pub fn bitcoin_units::sequence::SequenceDecoderError::clone(&self) -> bitcoin_units::sequence::SequenceDecoderError
@@ -2429,6 +2433,7 @@ pub fn bitcoin_units::sequence::SequenceEncoder::advance(&mut self) -> bool
24292433
pub fn bitcoin_units::sequence::SequenceEncoder::current_chunk(&self) -> core::option::Option<&[u8]>
24302434
pub fn bitcoin_units::time::BlockTimeDecoder::default() -> Self
24312435
pub fn bitcoin_units::time::BlockTimeDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
2436+
pub fn bitcoin_units::time::BlockTimeDecoder::min_bytes_needed(&self) -> usize
24322437
pub fn bitcoin_units::time::BlockTimeDecoder::new() -> Self
24332438
pub fn bitcoin_units::time::BlockTimeDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
24342439
pub fn bitcoin_units::time::BlockTimeDecoderError::clone(&self) -> bitcoin_units::time::BlockTimeDecoderError

consensus_encoding/src/decode/decoders.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ impl<const N: usize> Decoder for ArrayDecoder<N> {
4747
Err(UnexpectedEofError { missing: N - self.bytes_written })
4848
}
4949
}
50+
51+
#[inline]
52+
fn min_bytes_needed(&self) -> usize { N - self.bytes_written }
5053
}
5154

5255
/// A decoder which wraps two inner decoders and returns the output of both.
@@ -165,6 +168,17 @@ where
165168
}
166169
}
167170
}
171+
172+
#[inline]
173+
fn min_bytes_needed(&self) -> usize {
174+
match &self.state {
175+
Decoder2State::First(first_decoder, second_decoder) =>
176+
first_decoder.min_bytes_needed() + second_decoder.min_bytes_needed(),
177+
Decoder2State::Second(_, second_decoder) => second_decoder.min_bytes_needed(),
178+
Decoder2State::Errored => 0,
179+
Decoder2State::Transitioning => 0,
180+
}
181+
}
168182
}
169183

170184
/// A decoder which decodes three objects, one after the other.
@@ -215,6 +229,9 @@ where
215229
let ((first, second), third) = self.inner.end()?;
216230
Ok((first, second, third))
217231
}
232+
233+
#[inline]
234+
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
218235
}
219236

220237
/// A decoder which decodes four objects, one after the other.
@@ -268,6 +285,9 @@ where
268285
let ((first, second), (third, fourth)) = self.inner.end()?;
269286
Ok((first, second, third, fourth))
270287
}
288+
289+
#[inline]
290+
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
271291
}
272292

273293
/// A decoder which decodes six objects, one after the other.
@@ -346,6 +366,9 @@ where
346366
let ((first, second, third), (fourth, fifth, sixth)) = self.inner.end()?;
347367
Ok((first, second, third, fourth, fifth, sixth))
348368
}
369+
370+
#[inline]
371+
fn min_bytes_needed(&self) -> usize { self.inner.min_bytes_needed() }
349372
}
350373

351374
/// Not enough bytes given to decoder.

0 commit comments

Comments
 (0)