Skip to content

Commit

Permalink
chore(starknet_state_sync): boxing SyncBlock in requests and responses
Browse files Browse the repository at this point in the history
commit-id:fba480c4
  • Loading branch information
lev-starkware committed Jan 13, 2025
1 parent 42d4dce commit 026e3c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 7 additions & 2 deletions crates/starknet_state_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync
async fn handle_request(&mut self, request: StateSyncRequest) -> StateSyncResponse {
match request {
StateSyncRequest::GetBlock(block_number) => {
StateSyncResponse::GetBlock(self.get_block(block_number))
let result: StateSyncResult<Box<Option<SyncBlock>>> =
match self.get_block(block_number) {
Ok(sync_block) => Ok(Box::new(sync_block)),
Err(err) => Err(err),
};
StateSyncResponse::GetBlock(result)
}
StateSyncRequest::AddNewBlock(sync_block) => StateSyncResponse::AddNewBlock(
self.new_block_sender.send(sync_block).await.map_err(StateSyncError::from),
self.new_block_sender.send(*sync_block).await.map_err(StateSyncError::from),
),
StateSyncRequest::GetStorageAt(block_number, contract_address, storage_key) => {
StateSyncResponse::GetStorageAt(self.get_storage_at(
Expand Down
7 changes: 5 additions & 2 deletions crates/starknet_state_sync/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ async fn test_get_block() {
expected_header.block_header_without_hash.block_number,
))
.await;
let StateSyncResponse::GetBlock(Ok(Some(block))) = response else {
panic!("Expected StateSyncResponse::GetBlock::Ok(Some(_)), but got {:?}", response);
let StateSyncResponse::GetBlock(Ok(boxed_sync_block)) = response else {
panic!("Expected StateSyncResponse::GetBlock::Ok(Box(Some(_))), but got {:?}", response);
};
let Some(block) = *boxed_sync_block else {
panic!("Expected Box(Some(_)), but got {:?}", boxed_sync_block);
};

assert_eq!(block.block_header_without_hash, expected_header.block_header_without_hash);
Expand Down
17 changes: 11 additions & 6 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use async_trait::async_trait;
#[cfg(any(feature = "testing", test))]
use mockall::automock;
use papyrus_proc_macros::handle_response_variants;
use papyrus_proc_macros::{handle_all_response_variants, handle_response_variants};
use serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use starknet_api::contract_class::ContractClass;
Expand Down Expand Up @@ -105,7 +105,7 @@ pub type StateSyncRequestAndResponseSender =
#[allow(clippy::large_enum_variant)]
pub enum StateSyncRequest {
GetBlock(BlockNumber),
AddNewBlock(SyncBlock),
AddNewBlock(Box<SyncBlock>),
GetStorageAt(BlockNumber, ContractAddress, StorageKey),
GetNonceAt(BlockNumber, ContractAddress),
GetClassHashAt(BlockNumber, ContractAddress),
Expand All @@ -116,7 +116,7 @@ pub enum StateSyncRequest {
#[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum StateSyncResponse {
GetBlock(StateSyncResult<Option<SyncBlock>>),
GetBlock(StateSyncResult<Box<Option<SyncBlock>>>),
AddNewBlock(StateSyncResult<()>),
GetStorageAt(StateSyncResult<Felt>),
GetNonceAt(StateSyncResult<Nonce>),
Expand All @@ -135,12 +135,17 @@ where
block_number: BlockNumber,
) -> StateSyncClientResult<Option<SyncBlock>> {
let request = StateSyncRequest::GetBlock(block_number);
let response = self.send(request).await;
handle_response_variants!(StateSyncResponse, GetBlock, StateSyncClientError, StateSyncError)
handle_all_response_variants!(
StateSyncResponse,
GetBlock,
StateSyncClientError,
StateSyncError,
Boxed
)
}

async fn add_new_block(&self, sync_block: SyncBlock) -> StateSyncClientResult<()> {
let request = StateSyncRequest::AddNewBlock(sync_block);
let request = StateSyncRequest::AddNewBlock(Box::new(sync_block));
let response = self.send(request).await;
handle_response_variants!(
StateSyncResponse,
Expand Down

0 comments on commit 026e3c3

Please sign in to comment.