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

feat(network): add wrapper for Option<SignedBlockHeader> and add converters to/from Vec<u8> #2063

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion crates/papyrus_protobuf/src/converters/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<protobuf::BlockHeadersResponse> for BlockHeaderResponse {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::BlockHeadersResponse) -> Result<Self, Self::Error> {
Ok(Self(value.try_into()?))
}
}

impl TryFrom<protobuf::BlockHeadersResponse> for Option<SignedBlockHeader> {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::BlockHeadersResponse) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -195,6 +202,12 @@ impl TryFrom<protobuf::SignedBlockHeader> for SignedBlockHeader {
}
}

impl From<BlockHeaderResponse> for protobuf::BlockHeadersResponse {
fn from(value: BlockHeaderResponse) -> Self {
value.0.into()
}
}

impl From<(BlockHeader, Vec<BlockSignature>)> for protobuf::SignedBlockHeader {
fn from((header, signatures): (BlockHeader, Vec<BlockSignature>)) -> Self {
Self {
Expand Down Expand Up @@ -287,6 +300,8 @@ impl From<Option<SignedBlockHeader>> 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<protobuf::BlockHeadersRequest> for Query {
type Error = ProtobufConversionError;
Expand Down
28 changes: 17 additions & 11 deletions crates/papyrus_protobuf/src/converters/header_test.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>::from(data.clone());

let res_data = Option::<SignedBlockHeader>::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::<u8>::from(BlockHeaderResponse(None));

let res_data = Option::<SignedBlockHeader>::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]
Expand Down
7 changes: 7 additions & 0 deletions crates/papyrus_protobuf/src/sync.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<SignedBlockHeader>);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignedBlockHeader {
pub block_header: BlockHeader,
Expand Down
Loading