reth_downloaders/file_codec.rs
1//! Codec for reading raw block bodies from a file.
2
3use crate::file_client::FileClientError;
4use alloy_primitives::bytes::{Buf, BytesMut};
5use alloy_rlp::{Decodable, Encodable};
6use tokio_util::codec::{Decoder, Encoder};
7
8/// Codec for reading raw block bodies from a file.
9///
10/// If using with [`FramedRead`](tokio_util::codec::FramedRead), the user should make sure the
11/// framed reader has capacity for the entire block file. Otherwise, the decoder will return
12/// [`InputTooShort`](alloy_rlp::Error::InputTooShort), because RLP headers can only be
13/// decoded if the internal buffer is large enough to contain the entire block body.
14///
15/// Without ensuring the framed reader has capacity for the entire file, a block body is likely to
16/// fall across two read buffers, the decoder will not be able to decode the header, which will
17/// cause it to fail.
18///
19/// It's recommended to use [`with_capacity`](tokio_util::codec::FramedRead::with_capacity) to set
20/// the capacity of the framed reader to the size of the file.
21pub(crate) struct BlockFileCodec<B>(std::marker::PhantomData<B>);
22
23impl<B> Default for BlockFileCodec<B> {
24 fn default() -> Self {
25 Self(std::marker::PhantomData)
26 }
27}
28
29impl<B: Decodable> Decoder for BlockFileCodec<B> {
30 type Item = B;
31 type Error = FileClientError;
32
33 fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
34 if src.is_empty() {
35 return Ok(None)
36 }
37
38 let buf_slice = &mut src.as_ref();
39 let body = B::decode(buf_slice).map_err(|err| FileClientError::Rlp(err, src.to_vec()))?;
40 src.advance(src.len() - buf_slice.len());
41
42 Ok(Some(body))
43 }
44}
45
46impl<B: Encodable> Encoder<B> for BlockFileCodec<B> {
47 type Error = FileClientError;
48
49 fn encode(&mut self, item: B, dst: &mut BytesMut) -> Result<(), Self::Error> {
50 item.encode(dst);
51 Ok(())
52 }
53}