Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor apply_delta for Storage Maps #751

Closed
Dominik1999 opened this issue Jun 13, 2024 · 3 comments
Closed

Refactor apply_delta for Storage Maps #751

Dominik1999 opened this issue Jun 13, 2024 · 3 comments
Assignees
Milestone

Comments

@Dominik1999
Copy link
Collaborator

Dominik1999 commented Jun 13, 2024

As discussed in #737 (comment), we need a PR that fixes how account storage delta is put together and applied.

Specifically:

  • AccountStorageDelta.cleared_items and AccountStorageDelta.updated_items should contain updates only to value slots.
  • AccountStorageDelta.updated_maps should contain updates to storage maps.

This should be sufficient to apply the delta because new roots of storage maps can be computed from the data in updated_maps and can then use these roots to update the corresponding storage slots.

To make the above work, we may need to modify MASM - but there may also be a way around it. For example, if the a set_item event is fired on a map storage slot, transaction host can verify that it is correct, but then ignore it.

Separately, we should also implement AccountStorage::set_map_item() and AccountStorage::get_map_item() methods to mimic the ones in the transaction kernel.

Originally posted by @bobbinth in #737 (review)

@bobbinth bobbinth added this to the v0.4 milestone Jun 13, 2024
@Dominik1999 Dominik1999 moved this to In Progress in User's testnet Jun 17, 2024
@Dominik1999
Copy link
Collaborator Author

Dominik1999 commented Jun 17, 2024

I am starting this issue today.

I am not sure I understood 100% why this refactoring is needed. In my understanding, for every change in a StorageMap there is always a change, i.e. the root of the map in a StorageSlot.

This is what the MASM code in api.masm does.

# set the new map item
loc_load.0 exec.account::set_map_item swapw
# => [NEW_MAP_ROOT, OLD_MAP_VALUE, ...]
# set the root of the map in the respective account storage slot
loc_load.0 exec.account::set_item
# => [OLD_MAP_ROOT, OLD_MAP_VALUE, ...]

That means we have two updated entities, AccountStorageDelta.updated_items and AccountStorageDelta.updated_maps, because updated_items tracks changes in StorageSlots, and updated_maps tracks changes in StorageMaps.

Currently, there is also a check that whenever there is a updated_map, there must be a corresponding change in updated_items to ensure consistency.

// make sure storage map deltas are valid
for (index, storage_map_delta) in self.updated_maps.iter() {
if index > &MAX_MUTABLE_STORAGE_SLOT_IDX {
return Err(AccountDeltaError::ImmutableStorageSlot(*index as usize));
}
// for every storage map delta there should be one corresponding storage item update
if !self.updated_items.iter().any(|(idx, _)| idx == index) {
return Err(AccountDeltaError::StorageMapDeltaWithoutStorageItemChange(
*index as usize,
));
}
if !storage_map_delta.is_empty() {
storage_map_delta.validate()?;
}

It could be implemented differently, without question, but I don't know what the reason behind it is.

@bobbinth
Copy link
Contributor

The main reasons is that having both storage map updates and storage slot updates (for slots which contain storage maps) is redundant. This means:

  • The deltas are slightly bigger (this is very minor).
  • It is possible to encode data in the delta inconsistently (e.g., storage slot updates would indicate one root for a storage map, while corresponding storage map updates would result in a different root). It is possible to deal with this, but that means extra code in various places (e.g., during application of the delta, during deserialization of the delta etc.).

@Dominik1999 Dominik1999 moved this from In Progress to In Review in User's testnet Jun 19, 2024
@Dominik1999 Dominik1999 moved this from In Review to Done in User's testnet Jun 24, 2024
@bobbinth
Copy link
Contributor

Closed by #758.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants