Skip to content

Commit fdb5fa0

Browse files
committed
apollo_state_sync: fix bug in state sync get_block_hash
1 parent cae4263 commit fdb5fa0

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

crates/apollo_state_sync/src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,19 @@ impl StateSync {
154154
}
155155

156156
async fn get_block_hash(&self, block_number: BlockNumber) -> StateSyncResult<BlockHash> {
157-
// Getting the next block because the Sync block only contains parent hash.
158-
match (self.get_block(block_number).await, self.starknet_client.as_ref()) {
159-
(Ok(block), _) => Ok(block.block_header_without_hash.parent_hash),
160-
(Err(StateSyncError::BlockNotFound(_)), Some(starknet_client)) => {
157+
let storage_reader = self.storage_reader.clone();
158+
let block_hash_opt = tokio::task::spawn_blocking(move || {
159+
Ok::<_, StateSyncError>(
160+
storage_reader
161+
.begin_ro_txn()?
162+
.get_block_header(block_number)?
163+
.map(|header| header.block_hash),
164+
)
165+
})
166+
.await??;
167+
match (block_hash_opt, self.starknet_client.as_ref()) {
168+
(Some(block_hash), _) => Ok(block_hash),
169+
(None, Some(starknet_client)) => {
161170
// As a fallback, try to get the block hash through the feeder directly. This
162171
// method is faster than get_block which the sync runner uses.
163172
// TODO(shahak): Test this flow.
@@ -166,7 +175,7 @@ impl StateSync {
166175
.await?
167176
.ok_or(StateSyncError::BlockNotFound(block_number))
168177
}
169-
(Err(err), _) => Err(err),
178+
(None, _) => Err(StateSyncError::BlockNotFound(block_number)),
170179
}
171180
}
172181

crates/apollo_state_sync/src/test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ async fn test_get_block() {
7272
async fn test_get_block_hash() {
7373
let (mut state_sync, mut storage_writer) = setup();
7474

75-
let Block { header: expected_header, body: expected_body } =
75+
let Block { header: mut expected_header, body: expected_body } =
7676
get_test_block(1, None, None, None);
7777

78+
// get_test_block returns a block with parent_hash == block_hash. Need to change that to make
79+
// sure we don't return the parent hash
80+
expected_header.block_hash.0 = expected_header.block_hash.0 + Felt::from(1);
81+
7882
storage_writer
7983
.begin_rw_txn()
8084
.unwrap()

0 commit comments

Comments
 (0)