Skip to content

Commit e7d1903

Browse files
apollo_storage: implement deployed contracts query in storage reader (#11219)
1 parent be77f4d commit e7d1903

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

crates/apollo_storage/src/state/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,25 @@ impl<'env, Mode: TransactionKind> StateReader<'env, Mode> {
271271
}
272272
}
273273

274+
/// Returns the class hash for a given contract address and block number using direct key-value
275+
/// lookup.
276+
///
277+
/// # Arguments
278+
/// * address - contract address to search for.
279+
/// * block_number - block number to search at.
280+
///
281+
/// # Errors
282+
/// Returns [`StorageError`] if there was an error searching the table.
283+
pub fn get_class_hash_by_key(
284+
&self,
285+
address: &ContractAddress,
286+
block_number: BlockNumber,
287+
) -> StorageResult<Option<ClassHash>> {
288+
let db_key = (*address, block_number);
289+
let class_hash = self.deployed_contracts_table.get(self.txn, &db_key)?;
290+
Ok(class_hash)
291+
}
292+
274293
/// Returns the nonce at a given state number.
275294
/// If there is no nonce at the given state number, returns `None`.
276295
///

crates/apollo_storage/src/storage_reader_types.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,15 @@ impl StorageReaderServerHandler<StorageReaderRequest, StorageReaderResponse>
209209
)?;
210210
Ok(StorageReaderResponse::Nonces(nonce))
211211
}
212-
StorageReaderRequest::DeployedContracts(_address, _block_number) => {
213-
unimplemented!()
212+
StorageReaderRequest::DeployedContracts(address, block_number) => {
213+
let state_reader = txn.get_state_reader()?;
214+
let class_hash = state_reader
215+
.get_class_hash_by_key(&address, block_number)?
216+
.ok_or(StorageError::NotFound {
217+
resource_type: "Deployed contract".to_string(),
218+
resource_id: format!("address: {address}, block_number: {block_number:?}"),
219+
})?;
220+
Ok(StorageReaderResponse::DeployedContracts(class_hash))
214221
}
215222
StorageReaderRequest::Events(address, tx_index) => txn
216223
.has_event(address, tx_index)?

0 commit comments

Comments
 (0)