Cairo 1.x storage schema change and unresolved rollback safety
0 reputation · 24 Mar 2022, 02:53 UTC
0 reputation · 24 Mar 2022, 02:53 UTC
The goal is to determine whether contract storage can be safely rolled back to a previous layout after a storage schema change in Cairo 1.x. Sierra compilation enforces memory safety and bounded computation, but it does not define database-style schema migration or transactional rollback of contract storage.
The Cairo language specification assumes Cairo 1.x and the StarkNet storage model, and it does not document a standard on-chain upgrade or rollback pattern after storage fields are added, removed, or reordered. Whether Sierra safety assertions extend to application-level state is not addressed by the specification.
Is storage rollback safe after a schema change in Cairo 1.x? Which Sierra assertions cover storage schema migration? What on-chain pattern is intended when storage layout changes?
33775 reputation · 24 Mar 2022, 06:12 UTC
No. Rolling back a contract to a previous class hash after a storage schema change does not revert the persistent storage map. Sierra's safety guarantees are per-transaction (gas bounds, array bounds, panic-free execution, type-safe memory access) and do not extend to cross-transaction storage schema compatibility. There is no automatic migration, versioning, or rollback mechanism in the Cairo 1.x / StarkNet storage model.
Cairo 1.x contract storage is a flat key-value map felt252 → felt252. Each storage variable is assigned a deterministic slot derived from its name via Pedersen hash at compile time. The layout is fixed in the Sierra artifact (.sierra.json storage section). When you deploy a new class hash with a different layout (added, removed, or reordered variables), the slot assignments change. A subsequent replace_class or proxy upgrade to the old class hash swaps the executable bytecode but leaves every raw slot value untouched. The old layout will then interpret those slots as different variables, producing silent corruption.
panic opcodes)Sierra emits no schema version, layout hash, or migration metadata. The starknet::storage_access syscall reads/writes raw slots; the compiler inserts no layout guards.
#[storage] struct; never reorder or remove used variables.StoragePointer or fixed slots.scarb build and compare the storage section in each .sierra.json. Every variable present in the old layout must have an identical slot hash in the new layout.starknet::call_contract_syscall with the old class hash) to confirm interpretation matches.replace_class to the old class hash after the layout change, then query raw slots via starknet_getStorageAt to observe that values persist unchanged.Does your new layout only append variables after all existing ones, or does it reorder, remove, or insert variables before existing ones? If the latter, rollback is unsafe regardless of proxy usage and a migration transaction is mandatory.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.