blockifier,apollo_consensus_orchestrator: avoid state-diff clone in synced-block accessed keys - #14971
Conversation
When fetch_accessed_keys_from_centralized is enabled, try_sync fetches the synced block's accessed keys from the recorder and forwards them to the batcher so it can build the block's witness locally; on recorder failure it logs and falls back to CommitBlock with None. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… computing synced-block accessed keys try_sync cloned the entire synced block's ThinStateDiff on every synced block (when fetch_accessed_keys_from_centralized is enabled) just to convert it into a CommitmentStateDiff for accessed-keys computation. AccessedKeys::from_call_infos only ever reads the state diff by reference, so add a StateDiffAccessedKeysView trait implemented for both CommitmentStateDiff and ThinStateDiff, letting the accessed-keys computation borrow the synced block's state diff directly instead of cloning it.
…_addresses Code review follow-up on the previous commit: move the state-diff view trait next to CommitmentStateDiff (its natural home, and avoids a module cycle with stateful_compression), give its accessors neutral names shared by both impls instead of names borrowed from CommitmentStateDiff's own fields, fold the repeated storage/nonce/class-hash address chain into one default `contract_addresses` method instead of duplicating it in two files, and delete CommitmentStateDiff::get_contract_addresses now that both of its callers were migrated to the trait.
PR SummaryLow Risk Overview Introduces a Tests are updated to use Reviewed by Cursor Bugbot for commit 20b07d6. Bugbot is set up for automated code reviews on this repo. Configure here. |
Summary
Follow-up performance optimization on #14826 (merged), which added accessed-keys computation to
try_syncinsequencer_consensus_context.rs.When
fetch_accessed_keys_from_centralizedis enabled,try_synccomputed the synced block's accessed keys via:sync_block.state_diffis aThinStateDiff;.clone().into()deep-clones it and converts it into aCommitmentStateDiffpurely socompute_accessed_keys(which only reads the state diff by reference) can use it — the clone is thrown away once the call returns, whilesync_block(with its original, un-clonedstate_diff) is then moved whole intoself.deps.batcher.add_sync_block(sync_block, accessed_keys). This clone runs on every synced block once the feature is enabled, at a cost proportional to the block's state-diff size.Since
AccessedKeys::from_call_infos(and the alias-compression prediction it calls into) only ever read the state diff by reference, this PR adds aStateDiffViewtrait (crates/blockifier/src/state/cached_state.rs) implemented for bothCommitmentStateDiff(used when building a block) andThinStateDiff(used when syncing one), and generalizesAccessedKeys::new/from_call_infosandpredicted_alias_storage_entriesto accept&impl StateDiffViewinstead of a concrete&CommitmentStateDiff.compute_accessed_keysnow takes&ThinStateDiffdirectly, and thetry_synccall site borrowssync_block.state_diffinstead of cloning it.As a side effect,
CommitmentStateDiff::get_contract_addresses— whose only two callers were migrated to the trait'scontract_addresses()default method — is now dead and was removed.All existing callers of
AccessedKeys::new(apollo_batcher,starknet_os_flow_tests) pass a concrete&CommitmentStateDiff, which still implements the trait, so they're unaffected.Test plan
cargo build -p blockifier,cargo build -p apollo_consensus_orchestrator— cleancargo test -p blockifier(state::accessed_keys,state::stateful_compression, includingtest_predicted_alias_storage_entries_parity) — all passcargo test -p apollo_consensus_orchestrator(cende::, includingcompute_accessed_keys, andtry_sync) — all passcargo check -p apollo_batcher --tests,cargo check -p starknet_os_flow_tests --tests,cargo check -p central_systest_blobs --tests— clean (confirms the otherAccessedKeys::newcall sites are unaffected)cargo clippy -p blockifier -p apollo_consensus_orchestrator -p apollo_batcher --tests --all-targets -- -D warnings— cleanscripts/rust_fmt.sh— no changes neededThinStateDiffandCommitmentStateDiff(in particular the intentional drop ofdeprecated_declared_classes, mirroring the existingFrom<ThinStateDiff> for CommitmentStateDiffimpl) and trait design; findings addressed (moved the trait next toCommitmentStateDiff, gave accessors neutral names, deduplicated the repeated address-chain into a default trait method, removed the now-deadget_contract_addresses)Generated by Claude Code