Skip to content

Commit 96aa4f8

Browse files
tzemanovicbrentstone
authored andcommitted
Merge branch 'tomas/rm-redundant-writes' (#1984)
* tomas/rm-redundant-writes: changelog: add #1984 core/lazy_map+set: avoid calling delete when given key is not present
2 parents 3e5ec73 + 672e153 commit 96aa4f8

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- Avoid redundant storage deletions in lazy collections that would incur
2+
extra gas cause and appear in transaction result as changed keys even if not
3+
changed occurred. This may have caused PoS transactions to run out of gas.
4+
([\#1984](https://github.com/anoma/namada/pull/1984))

core/src/ledger/storage_api/collections/lazy_map.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,16 +585,18 @@ where
585585
Ok(previous)
586586
}
587587

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

596-
let data_key = self.get_data_key(key);
597-
storage.delete(&data_key)?;
596+
if value.is_some() {
597+
let data_key = self.get_data_key(key);
598+
storage.delete(&data_key)?;
599+
}
598600

599601
Ok(value)
600602
}

core/src/ledger/storage_api/collections/lazy_set.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,18 @@ where
183183
storage.write(&key, ())
184184
}
185185

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

194-
let key = self.get_key(key);
195-
storage.delete(&key)?;
194+
if present {
195+
let key = self.get_key(key);
196+
storage.delete(&key)?;
197+
}
196198

197199
Ok(present)
198200
}

0 commit comments

Comments
 (0)