From 2941946aa9c7fbf0cebd315df99f71286b44e5fa Mon Sep 17 00:00:00 2001 From: Dmytro Kozhevin Date: Fri, 14 Jul 2023 19:41:37 -0400 Subject: [PATCH] Make `del_contract_data` no-op for removing non-existent instance storage key. (#951) * Make `del_contract_data` no-op for removing non-existent instance storage key. This was inconsistent with persistent/temp storage, which doesn't return error for non-existent keys. --------- Co-authored-by: Siddharth Suresh --- soroban-env-host/src/host.rs | 15 +++------------ soroban-env-host/src/test/storage.rs | 7 +++++++ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/soroban-env-host/src/host.rs b/soroban-env-host/src/host.rs index 1d7e5df8c..b8ce13f48 100644 --- a/soroban-env-host/src/host.rs +++ b/soroban-env-host/src/host.rs @@ -2108,18 +2108,9 @@ impl VmCallerEnv for Host { } StorageType::Instance => { self.with_mut_instance_storage(|s| { - s.map = s - .map - .remove(&k, self)? - .ok_or_else(|| { - self.err( - ScErrorType::Storage, - ScErrorCode::MissingValue, - "key is missing from instance storage", - &[k], - ) - })? - .0; + if let Some((new_map, _)) = s.map.remove(&k, self)? { + s.map = new_map; + } Ok(()) })?; } diff --git a/soroban-env-host/src/test/storage.rs b/soroban-env-host/src/test/storage.rs index cd2b0699d..26ad1634f 100644 --- a/soroban-env-host/src/test/storage.rs +++ b/soroban-env-host/src/test/storage.rs @@ -149,6 +149,13 @@ fn test_storage(host: &Host, contract_id: AddressObject, storage: &str) { host_vec![host, key_1].into(), ) .unwrap(); + // Delete again - that's a no-op, but it shouldn't fail either. + host.call( + contract_id, + storage_fn_name(host, "del", storage), + host_vec![host, key_1].into(), + ) + .unwrap(); // Only the second key is now present assert_eq!( bool::try_from_val(