Skip to content

Commit 28dc2c0

Browse files
yoavGrsclaude
andcommitted
blockifier,apollo_batcher: trim OS initial reads to the accessed-key set
Drop initial-reads entries whose keys are not in AccessedKeys (e.g. storage cells read only by a reverted transaction). The OS replays from the read values of the keys it accesses and needs nothing beyond that set, so the extra entries would only bloat the cende blob. Validated by the OS flow tests (113 passing), including all reverted-tx cases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4cc1677 commit 28dc2c0

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

crates/apollo_batcher/src/batcher.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,15 @@ impl Batcher {
989989
&tx_execution_infos,
990990
)?;
991991

992+
// The OS only needs the read values for the keys it accesses; drop the extra reads (e.g.
993+
// reverted-tx reads).
994+
#[cfg(feature = "os_input")]
995+
let initial_reads = {
996+
let mut initial_reads = block_execution_artifacts.initial_reads;
997+
initial_reads.trim_to_accessed_keys(&accessed_keys);
998+
initial_reads
999+
};
1000+
9921001
self.write_commitment_results_and_add_new_task(
9931002
height,
9941003
state_diff.clone(), // TODO(Nimrod): Remove the clone here.
@@ -1026,7 +1035,7 @@ impl Batcher {
10261035
#[cfg(feature = "os_input")]
10271036
accessed_keys,
10281037
#[cfg(feature = "os_input")]
1029-
initial_reads: block_execution_artifacts.initial_reads,
1038+
initial_reads,
10301039
},
10311040
})
10321041
}

crates/blockifier/src/state/cached_state.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use starknet_types_core::felt::Felt;
99

1010
use crate::context::TransactionContext;
1111
use crate::execution::contract_class::RunnableCompiledClass;
12+
use crate::state::accessed_keys::AccessedKeys;
1213
use crate::state::errors::StateError;
1314
use crate::state::state_api::{State, StateReader, StateResult, UpdatableState};
1415
use crate::transaction::objects::TransactionExecutionInfo;
@@ -400,6 +401,15 @@ impl StateMaps {
400401
self.declared_contracts.extend(&other.declared_contracts)
401402
}
402403

404+
/// Removes every entry whose key is not in `accessed_keys`.
405+
pub fn trim_to_accessed_keys(&mut self, accessed_keys: &AccessedKeys) {
406+
self.storage.retain(|key, _| accessed_keys.storage_keys.contains(key));
407+
self.nonces.retain(|address, _| accessed_keys.accessed_contracts.contains(address));
408+
self.class_hashes.retain(|address, _| accessed_keys.accessed_contracts.contains(address));
409+
self.compiled_class_hashes
410+
.retain(|class_hash, _| accessed_keys.accessed_class_hashes.contains(class_hash));
411+
}
412+
403413
/// Subtracts other's mappings from self.
404414
/// Assumes (and enforces) other's keys contains self's.
405415
pub fn diff(&self, other: &Self) -> Self {

crates/starknet_os_flow_tests/src/test_manager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ impl<S: FlowTestState> TestBuilder<S> {
905905
&event_expectations_per_tx,
906906
&execution_outputs,
907907
);
908-
let initial_reads = final_state.get_os_initial_reads().unwrap();
908+
let mut initial_reads = final_state.get_os_initial_reads().unwrap();
909909
let state_diff = final_state.to_state_diff().unwrap().state_maps;
910910
// Update the wrapped state.
911911
state = final_state.state;
@@ -932,6 +932,7 @@ impl<S: FlowTestState> TestBuilder<S> {
932932
&commitment_state_diff,
933933
&accessed_keys_versioned_constants,
934934
);
935+
initial_reads.trim_to_accessed_keys(&accessed_keys);
935936
// Commit the state diff.
936937
let committer_state_diff =
937938
commitment_state_diff_to_committer_state_diff(commitment_state_diff);

0 commit comments

Comments
 (0)