Skip to content

Commit 6dc9e93

Browse files
authored
apollo_storage: add scan_compiled_class_hashes_in_range (#13555)
1 parent 1c58125 commit 6dc9e93

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

crates/apollo_storage/src/state/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,18 @@ impl<'env, Mode: TransactionKind> StateReader<'env, Mode> {
579579
let entries = scan_at_block(&mut cursor, (addr, start), (addr, end), block_target, limit)?;
580580
Ok(entries.into_iter().map(|((_, key), value)| (key, value)).collect())
581581
}
582+
583+
/// Returns all compiled class hashes in [start, end] at `block_target`.
584+
pub fn scan_compiled_class_hashes_in_range(
585+
&self,
586+
start: ClassHash,
587+
end: ClassHash,
588+
block_target: BlockNumber,
589+
limit: usize,
590+
) -> StorageResult<Vec<(ClassHash, CompiledClassHash)>> {
591+
let mut cursor = self.compiled_class_hash_table.cursor(self.txn)?;
592+
scan_at_block(&mut cursor, start, end, block_target, limit)
593+
}
582594
}
583595

584596
impl StateStorageWriter for StorageTxn<'_, RW> {

crates/apollo_storage/src/state/state_test.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use indexmap::{indexmap, IndexMap};
55
use pretty_assertions::assert_eq;
66
use rstest::rstest;
77
use starknet_api::block::BlockNumber;
8-
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
8+
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
99
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
1010
use starknet_api::hash::StarkHash;
1111
use starknet_api::state::{SierraContractClass, StateNumber, StorageKey, ThinStateDiff};
@@ -1096,3 +1096,36 @@ scan_cases!(
10961096
);
10971097
}
10981098
);
1099+
1100+
scan_cases!(
1101+
key_macro = class_hash,
1102+
value_macro = compiled_class_hash,
1103+
fn test_scan_compiled_class_hashes_in_range(
1104+
#[case] start: ClassHash,
1105+
#[case] end: ClassHash,
1106+
#[case] block_target: BlockNumber,
1107+
#[case] limit: usize,
1108+
#[case] expected: Vec<(ClassHash, CompiledClassHash)>,
1109+
) {
1110+
let ((reader, mut writer), _temp_dir) = get_test_storage();
1111+
write_two_block_state_diffs(
1112+
&mut writer,
1113+
ThinStateDiff {
1114+
class_hash_to_compiled_class_hash: block_0_entries!(),
1115+
..Default::default()
1116+
},
1117+
ThinStateDiff {
1118+
class_hash_to_compiled_class_hash: block_1_entries!(),
1119+
..Default::default()
1120+
},
1121+
);
1122+
let txn = reader.begin_ro_txn().unwrap();
1123+
let state_reader = txn.get_state_reader().unwrap();
1124+
assert_eq!(
1125+
state_reader
1126+
.scan_compiled_class_hashes_in_range(start, end, block_target, limit)
1127+
.unwrap(),
1128+
expected,
1129+
);
1130+
}
1131+
);

0 commit comments

Comments
 (0)