Skip to content

Commit

Permalink
feat(network): add wrapper for Option<SignedBlockHeader> and add conv…
Browse files Browse the repository at this point in the history
…erters to/from Vec<u8>
  • Loading branch information
ShahakShama committed May 28, 2024
1 parent 18167eb commit bdd85e8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
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
6 changes: 6 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 starknet_api::block::{BlockHash, BlockHeader, BlockNumber, BlockSignature};

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default, Hash)]
Expand Down Expand Up @@ -34,6 +36,10 @@ 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.
#[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

0 comments on commit bdd85e8

Please sign in to comment.