Skip to content

Commit

Permalink
Merge branch 'tomas/rm-redundant-writes' (#1984)
Browse files Browse the repository at this point in the history
* tomas/rm-redundant-writes:
  changelog: add #1984
  core/lazy_map+set: avoid calling delete when given key is not present
  • Loading branch information
brentstone committed Oct 16, 2023
2 parents 1cb323a + 672e153 commit 07058bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
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 @@ -585,16 +585,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

0 comments on commit 07058bf

Please sign in to comment.