Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
udpated env to return the size of prev stored val
  • Loading branch information
agryaznov committed Apr 24, 2022
commit b19a1094fdc51697261d01964bf07b2078c577f1
12 changes: 5 additions & 7 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,18 @@ impl Engine {
}

/// Writes the encoded value into the storage at the given key.
pub fn set_storage(&mut self, key: &[u8; 32], encoded_value: &[u8]) {
/// Returns the size of the previously stored value at the key if any, or None.
pub fn set_storage(&mut self, key: &[u8; 32], encoded_value: &[u8]) -> Option<u32> {
let callee = self.get_callee();
let account_id = AccountId::from_bytes(&callee[..]);

self.debug_info.inc_writes(account_id.clone());
self.debug_info
.record_cell_for_account(account_id, key.to_vec());

// We ignore if storage is already set for this key
let _ = self.database.insert_into_contract_storage(
&callee,
key,
encoded_value.to_vec(),
);
self.database
.insert_into_contract_storage(&callee, key, encoded_value.to_vec())
.map(|v| <u32>::try_from(v.len()).expect("usize to u32 conversion failed"))
}

/// Returns the decoded contract storage at the key if any.
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where
}

/// Writes the value to the contract storage under the given key and returns
/// the size of the pre-existing value at the specified key if any, or None.
/// the size of pre-existing value at the specified key if any, or None.
///
/// # Panics
///
Expand Down
5 changes: 3 additions & 2 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ impl EnvInstance {
}

impl EnvBackend for EnvInstance {
fn set_contract_storage<V>(&mut self, _key: &Key, _value: &V) -> Option<u32>
fn set_contract_storage<V>(&mut self, key: &Key, value: &V) -> Option<u32>
where
V: scale::Encode,
{
unimplemented!("the off-chain env does not implement `seal_set_storage`, yet")
let v = scale::Encode::encode(value);
self.engine.set_storage(key.as_ref(), &v[..])
}

fn get_contract_storage<R>(&mut self, key: &Key) -> Result<Option<R>>
Expand Down
11 changes: 0 additions & 11 deletions crates/storage/src/lazy/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,6 @@ where
push_packed_root(value, &self.storage_key(&key));
}

/// Insert the given `value` to the contract storage.
/// Returns the size of the pre-existing value at the specified key if any, or None.
#[inline]
pub fn insert_checked<Q, R>(&mut self, key: Q, value: &R) -> Option<u32>
where
Q: scale::EncodeLike<K>,
R: scale::EncodeLike<V> + PackedLayout,
{
push_packed_root(value, &self.storage_key(&key))
}

/// Get the `value` at `key` from the contract storage.
///
/// Returns `None` if no `value` exists at the given `key`.
Expand Down