Ideas and design for implementing the equivalent of Solidity’s immutable
storage on Aztec Contracts, after some discussions with @Mike.
What is immutable
?
Immutable storage is a Solidity construct for EVM contracts. If there’s a piece of state that gets set during initialization and is never updated, that state gets embedded into the bytecode itself, instead of saved to contract storage. Main reason for this is cost: bytecode is much cheaper than contract storage. This applies not just to EVM chains but also for Aztec.
Why not do the same in Aztec Contracts?
Unlike Ethereum, we have the concept of contract classes, which push for reusing the same bytecode for multiple instances. Mixing state with bytecode would mean contract classes can no longer be reused, which defeats their purpose.
What can we do?
Same as we always do: hash everything and add it to the address preimage. We can add an extra field, along with initialization hash, salt, and deployer, that is the “immutable storage hash”, ie the hash of all immutable pieces of state (either a plain hash or a small merkle tree).
In private-land
Immutable private state gets stored in the client-side database, along with the rest of the contract instance data. Whenever a contract wants to access its private immutable storage, it issues an oracle call to retrieve it, and then proves it can be hashed back to the immutable storage hash, which can get hashed back to its own address.
In public-land
Things get trickier in public land. If a contract needs to access its immutable storage in public, then that data needs to be provably available to every node. This means that broadcasting immutable storage is needed for public deployment, which means changing the contract instance deployer, which is a protocol change. It also means adding a new opcode to the AVM for retrieving public immutable storage.
What can we do with the least amount of changes?
We can handle immutable state in private-land only for now. And instead of introducing a new field, we could hijack an existing one (like salt or init hash) to store the immutable state hash, though I’m not a fan of these hacks. However, we’d still need to introduce a new oracle call to retrieve the immutable state.