diff --git a/crates/papyrus_protobuf/src/converters/header.rs b/crates/papyrus_protobuf/src/converters/header.rs index 144a029b4d..3811d7f026 100644 --- a/crates/papyrus_protobuf/src/converters/header.rs +++ b/crates/papyrus_protobuf/src/converters/header.rs @@ -22,9 +22,16 @@ use starknet_api::crypto::Signature; use super::common::{enum_int_to_l1_data_availability_mode, l1_data_availability_mode_to_enum_int}; use super::ProtobufConversionError; -use crate::sync::{HeaderQuery, Query, SignedBlockHeader}; +use crate::sync::{BlockHeaderResponse, HeaderQuery, Query, SignedBlockHeader}; use crate::{auto_impl_into_and_try_from_vec_u8, protobuf}; +impl TryFrom for BlockHeaderResponse { + type Error = ProtobufConversionError; + fn try_from(value: protobuf::BlockHeadersResponse) -> Result { + Ok(Self(value.try_into()?)) + } +} + impl TryFrom for Option { type Error = ProtobufConversionError; fn try_from(value: protobuf::BlockHeadersResponse) -> Result { @@ -195,6 +202,12 @@ impl TryFrom for SignedBlockHeader { } } +impl From for protobuf::BlockHeadersResponse { + fn from(value: BlockHeaderResponse) -> Self { + value.0.into() + } +} + impl From<(BlockHeader, Vec)> for protobuf::SignedBlockHeader { fn from((header, signatures): (BlockHeader, Vec)) -> Self { Self { @@ -287,6 +300,8 @@ impl From> for protobuf::BlockHeadersResponse { } } +auto_impl_into_and_try_from_vec_u8!(BlockHeaderResponse, protobuf::BlockHeadersResponse); + // TODO(shahak): Erase this once network stops using it. impl TryFrom for Query { type Error = ProtobufConversionError; diff --git a/crates/papyrus_protobuf/src/converters/header_test.rs b/crates/papyrus_protobuf/src/converters/header_test.rs index 8daf243682..0442955c1b 100644 --- a/crates/papyrus_protobuf/src/converters/header_test.rs +++ b/crates/papyrus_protobuf/src/converters/header_test.rs @@ -1,29 +1,35 @@ use starknet_api::block::{BlockHeader, BlockNumber}; -use crate::protobuf; -use crate::sync::{BlockHashOrNumber, Direction, HeaderQuery, Query, SignedBlockHeader}; +use crate::sync::{ + BlockHashOrNumber, + BlockHeaderResponse, + Direction, + HeaderQuery, + Query, + SignedBlockHeader, +}; #[test] -fn block_header_to_protobuf_and_back() { - let data = SignedBlockHeader { +fn block_header_to_bytes_and_back() { + let data = BlockHeaderResponse(Some(SignedBlockHeader { // TODO(shahak): Remove state_diff_length from here once we correctly deduce if it should // be None or Some. block_header: BlockHeader { state_diff_length: Some(0), ..Default::default() }, signatures: vec![], - }; + })); dbg!(&data); - let proto_data = protobuf::BlockHeadersResponse::from(Some(data.clone())); + let bytes_data = Vec::::from(data.clone()); - let res_data = Option::::try_from(proto_data).unwrap().unwrap(); + let res_data = BlockHeaderResponse::try_from(bytes_data).unwrap(); assert_eq!(res_data, data); } #[test] -fn fin_to_protobuf_and_back() { - let proto_data = protobuf::BlockHeadersResponse::from(None); +fn fin_to_bytes_and_back() { + let bytes_data = Vec::::from(BlockHeaderResponse(None)); - let res_data = Option::::try_from(proto_data).unwrap(); - assert!(res_data.is_none()); + let res_data = BlockHeaderResponse::try_from(bytes_data).unwrap(); + assert!(res_data.0.is_none()); } #[test] diff --git a/crates/papyrus_protobuf/src/sync.rs b/crates/papyrus_protobuf/src/sync.rs index eb35ad7a58..4e9f8c7971 100644 --- a/crates/papyrus_protobuf/src/sync.rs +++ b/crates/papyrus_protobuf/src/sync.rs @@ -1,3 +1,5 @@ +use std::fmt::Debug; + use indexmap::IndexMap; use starknet_api::block::{BlockHash, BlockHeader, BlockNumber, BlockSignature}; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; @@ -38,6 +40,11 @@ pub struct HeaderQuery(pub Query); #[derive(Default, Clone, Debug, PartialEq, Eq, Hash)] pub struct StateDiffQuery(pub Query); +// We have this struct in order to implement on it traits. +// TODO(shahak): Rename the protobuf message from BlockHeadersResponse to BlockHeaderResponse. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BlockHeaderResponse(pub Option); + #[derive(Debug, Clone, PartialEq, Eq)] pub struct SignedBlockHeader { pub block_header: BlockHeader,