-
Notifications
You must be signed in to change notification settings - Fork 115
Description
This is essentially a follow-up to the things discovered while implementing the fix in #2077.
As mentioned there, the account storage delta we currently have was designed for representing a change to existing slots, while with #1879 and, in the future, account storage updates (e.g. creating new slots or removing existing ones), this structure should be updated to better allow for representing these states. In particular, this is intended to replace the fix from #2077.
One idea for a structure to represent such storage deltas in the Rust API could be (part of this is taken from #2025 and assumes we implement this after named storage slots):
pub struct AccountStorageDelta {
/// The updates to the value slots of the account.
values: BTreeMap<SlotName, StorageValueDelta>,
/// The updates to the map slots of the account.
maps: BTreeMap<SlotName, StorageMapDelta>,
}
pub enum SlotDeltaOperation {
/// Represents a new slot being created in account storage.
///
/// A slot with the associated slot name must not exist.
Create = 0,
/// Represents an update to an existing slot in the account storage.
///
/// A slot with the associated slot name must exist.
Update = 1,
/// Represents the removal of an existing slot in the account storage.
///
/// A slot with the associated slot name must exist.
Remove = 2,
}
pub struct StorageValueDelta {
operation: SlotDeltaOperation,
value: Word,
}
pub struct StorageMapDelta {
operation: SlotDeltaOperation,
entries: BTreeMap<LexicographicWord, Word>,
}For the storage part of the delta commitment, we could have the following updates:
- Storage Slots are sorted by slot name ID and are iterated in this order. For each slot, depending
on the slot type:- Value Slot
- Append
[[domain = 2, delta_op, name_id_suffix, name_id_prefix], NEW_VALUE]where:NEW_VALUEis the new value of the slotname_id_{suffix, prefix}are the slot name identifiers of the slot.delta_opindicates whether the slot was created (0), updated (1) or removed (2).
- Append
- Map Slot
- For each key-value pair, sorted by key, whose new value is different from the previous
value in the map:- Append
[KEY, NEW_VALUE].
- Append
- Append
[[domain = 3, delta_op, name_id_suffix, name_id_prefix], [num_changed_entries, 0, 0, 0]], where:delta_opindicates whether the slot was created (0), updated (1) or removed (2).name_id_{suffix, prefix}are the slot name identifiers of the slot.num_changed_entriesis the number of entries that have changed.- If delta_op is Update, the map header must only be included if
num_changed_entries != 0. This allows normalizing away such no-op updates.
- If delta_op is Update, the map header must only be included if
- For each key-value pair, sorted by key, whose new value is different from the previous
- Value Slot
A bit more thought needs to be put into:
- the security of this, i.e. whether it creates ambiguities in the commitment format.
- whether this is easy to implement in MASM.
- What
num_changed_entriesshould be set to when delta_op is Remove (probably 0 as we won't have the number of removed entries available?).
Other open points are:
- Where to store the delta operation in kernel memory (probably in
ACCOUNT_DELTA_STORAGE_MAP_SECTION) - How to do storage updates more broadly, which may make sense to think about before implementing this to make sure it is compatible.
As part of this we can also try to address this TODO: https://github.com/0xMiden/miden-base/blob/c532ae91fdf711ac2f8b253342c253ee2eac0bed/crates/miden-objects/src/account/delta/mod.rs#L355-L366.