aztec-packages icon indicating copy to clipboard operation
aztec-packages copied to clipboard

refactor(StateVariables): Migrate Storage<T> to Storage<N>

Open nventuro opened this issue 10 months ago • 0 comments

We currently allocate storage slots using the Storage trait, which reads:

trait Storage<T> where T: Serialize<N> + Deserialize<N> {
    fn get_storage_slot(self) -> Field {
        self.storage_slot
    }
}

Note that we don't use the serde traits anywhere, nor the N value. However, the macros do rely on N to determine how many slots to allocate. This implies that a state variable that holds some type T will require exactly as many slots as it'd need to store a serialized version of T. However, there's many scenarios in which this is not true.

SharedMutable requires 2 * N + 1 slots (a ScheduledValueChange struct, with pre post and block_of_change). A dynamic array will likely require a single slot to store the length, regardless of T. Map similarly also uses a single slot.

I propose we change the storage definition to:

trait Storage<N> {
    fn get_storage_slot(self) -> Field {
        self.storage_slot
    }
}

and use this N value directly in the macro to determine how many slots to allocate. We could then have things like:

impl<K, T> Storage<1> for Map<K, T> {}

impl<T> Storage<N> for PublicMutable<T> where T: Serialize<N> + Deserialize<N> {}

impl<T> Storage<2 * N + 1> for SharedMutable<T> where T: Serialize<N> + Deserialize<N> {}

Note that we won't be able to do arithmetic until https://github.com/noir-lang/noir/issues/4784 is implemented, but we could already do the rest.

nventuro avatar Apr 12 '24 15:04 nventuro