Skip to content

Commit

Permalink
refactor(firehose-protos): use utility function for conversion error
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 22, 2024
1 parent ad9dca6 commit 78b0d38
Showing 1 changed file with 28 additions and 6 deletions.
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

0 comments on commit 78b0d38

Please sign in to comment.