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

core/lazy_map+set: avoid calling delete when given key is not present #1984

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/unreleased/bug-fixes/1984-rm-redundant-writes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Avoid redundant storage deletions in lazy collections that would incur
extra gas cause and appear in transaction result as changed keys even if not
changed occurred. This may have caused PoS transactions to run out of gas.
([\#1984](https://github.com/anoma/namada/pull/1984))
10 changes: 6 additions & 4 deletions core/src/ledger/storage_api/collections/lazy_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,18 @@ where
Ok(previous)
}

/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
/// Removes a key from the map if it's present, returning the value at the
/// key if the key was previously in the map.
pub fn remove<S>(&self, storage: &mut S, key: &K) -> Result<Option<V>>
where
S: StorageWrite + StorageRead,
{
let value = self.get(storage, key)?;

let data_key = self.get_data_key(key);
storage.delete(&data_key)?;
if value.is_some() {
let data_key = self.get_data_key(key);
storage.delete(&data_key)?;
}

Ok(value)
}
Expand Down
8 changes: 5 additions & 3 deletions core/src/ledger/storage_api/collections/lazy_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,18 @@ where
storage.write(&key, ())
}

/// Removes a key from the set, returning `true` if the key
/// Removes a key from the set if it's present, returning `true` if the key
/// was in the set.
pub fn remove<S>(&self, storage: &mut S, key: &K) -> Result<bool>
where
S: StorageWrite + StorageRead,
{
let present = self.contains(storage, key)?;

let key = self.get_key(key);
storage.delete(&key)?;
if present {
let key = self.get_key(key);
storage.delete(&key)?;
}

Ok(present)
}
Expand Down