Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(firehose-protos): use utility function for conversion error #20

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions crates/firehose-protos/src/ethereum_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use alloy_primitives::{Address, Bloom, FixedBytes, Uint};
use ethportal_api::types::execution::header::Header;
use prost::Message;
use prost_wkt_types::Any;
use reth_primitives::TxType;
use transaction_trace::Type;

Expand Down Expand Up @@ -115,23 +116,44 @@ impl From<Type> for TxType {
}
}

fn decode_block<M>(response: M) -> Result<Block, ProtosError>
where
M: MessageWithBlock,
{
let any = response.block().ok_or(ProtosError::NullBlock)?;
let block = Block::decode(any.value.as_ref())?;
Ok(block)
}

trait MessageWithBlock {
fn block(&self) -> Option<&Any>;
}

impl MessageWithBlock for SingleBlockResponse {
fn block(&self) -> Option<&Any> {
self.block.as_ref()
}
}

impl MessageWithBlock for Response {
fn block(&self) -> Option<&Any> {
self.block.as_ref()
}
}

impl TryFrom<SingleBlockResponse> for Block {
type Error = ProtosError;

fn try_from(response: SingleBlockResponse) -> Result<Self, Self::Error> {
let any = response.block.ok_or(ProtosError::NullBlock)?;
let block = Block::decode(any.value.as_ref())?;
Ok(block)
decode_block(response)
}
}

impl TryFrom<Response> for Block {
type Error = ProtosError;

fn try_from(response: Response) -> Result<Self, Self::Error> {
let any = response.block.ok_or(ProtosError::NullBlock)?;
let block = Block::decode(any.value.as_ref())?;
Ok(block)
decode_block(response)
}
}

Expand Down
Loading