Skip to content

Commit

Permalink
Revert backward-incompatible piece of #1224: dependency on `[seal1] s…
Browse files Browse the repository at this point in the history
…eal_set_storage`
  • Loading branch information
agryaznov committed Jun 6, 2022
1 parent 0a96173 commit cd26767
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 44 deletions.
12 changes: 7 additions & 5 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,20 @@ impl Engine {
}

/// Writes the encoded value into the storage at the given key.
/// Returns the size of the previously stored value at the key if any.
pub fn set_storage(&mut self, key: &[u8; 32], encoded_value: &[u8]) -> Option<u32> {
pub fn set_storage(&mut self, key: &[u8; 32], encoded_value: &[u8]) {
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());

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"))
// We ignore if storage is already set for this key
let _ = self.database.insert_into_contract_storage(
&callee,
key,
encoded_value.to_vec(),
);
}

/// Returns the decoded contract storage at the key if any.
Expand Down
5 changes: 2 additions & 3 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,12 @@ where
})
}

/// Writes the value to the contract storage under the given key and returns
/// the size of pre-existing value at the specified key if any.
/// Writes the value to the contract storage under the given key.
///
/// # Panics
///
/// - If the encode length of value exceeds the configured maximum value length of a storage entry.
pub fn set_contract_storage<V>(key: &Key, value: &V) -> Option<u32>
pub fn set_contract_storage<V>(key: &Key, value: &V)
where
V: scale::Encode,
{
Expand Down
5 changes: 2 additions & 3 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ impl CallFlags {

/// Environmental contract functionality that does not require `Environment`.
pub trait EnvBackend {
/// 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.
fn set_contract_storage<V>(&mut self, key: &Key, value: &V) -> Option<u32>
/// Writes the value to the contract storage under the given key.
fn set_contract_storage<V>(&mut self, key: &Key, value: &V)
where
V: scale::Encode;

Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +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)
where
V: scale::Encode,
{
let v = scale::Encode::encode(value);
self.engine.set_storage(key.as_ref(), &v[..])
self.engine.set_storage(key.as_ref(), &v[..]);
}

fn get_contract_storage<R>(&mut self, key: &Key) -> Result<Option<R>>
Expand Down
19 changes: 9 additions & 10 deletions crates/env/src/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ mod sys {
data_len: u32,
);

pub fn seal_set_storage(
key_ptr: Ptr32<[u8]>,
value_ptr: Ptr32<[u8]>,
value_len: u32,
);

pub fn seal_get_storage(
key_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
Expand Down Expand Up @@ -374,12 +380,6 @@ mod sys {
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

pub fn seal_set_storage(
key_ptr: Ptr32<[u8]>,
value_ptr: Ptr32<[u8]>,
value_len: u32,
) -> ReturnCode;
}
}

Expand Down Expand Up @@ -495,15 +495,14 @@ pub fn deposit_event(topics: &[u8], data: &[u8]) {
}
}

pub fn set_storage(key: &[u8], encoded_value: &[u8]) -> Option<u32> {
let ret_code = unsafe {
pub fn set_storage(key: &[u8], encoded_value: &[u8]) {
unsafe {
sys::seal_set_storage(
Ptr32::from_slice(key),
Ptr32::from_slice(encoded_value),
encoded_value.len() as u32,
)
};
ret_code.into()
}
}

pub fn clear_storage(key: &[u8]) {
Expand Down
8 changes: 2 additions & 6 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,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)
where
V: scale::Encode,
{
let buffer = self.scoped_buffer().take_encoded(value);
ext::set_storage(key.as_ref(), buffer)
ext::set_storage(key.as_ref(), buffer);
}

fn get_contract_storage<R>(&mut self, key: &Key) -> Result<Option<R>>
Expand All @@ -241,10 +241,6 @@ impl EnvBackend for EnvInstance {
Ok(Some(decoded))
}

fn contract_storage_contains(&mut self, key: &Key) -> Option<u32> {
ext::storage_contains(key.as_ref())
}

fn clear_contract_storage(&mut self, key: &Key) {
ext::clear_storage(key.as_ref())
}
Expand Down
12 changes: 0 additions & 12 deletions crates/storage/src/lazy/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +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.
#[inline]
pub fn insert_return_size<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
2 changes: 1 addition & 1 deletion crates/storage/src/traits/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub fn forward_push_packed<T>(entity: &T, ptr: &mut KeyPtr)
where
T: PackedLayout,
{
push_packed_root::<T>(entity, ptr.next_for::<T>());
push_packed_root::<T>(entity, ptr.next_for::<T>())
}

/// Clears an instance of type `T` in packed fashion from the contract storage.
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ where
/// packed layout.
/// - Users should prefer using this function directly instead of using the
/// trait methods on [`PackedLayout`].
pub fn push_packed_root<T>(entity: &T, root_key: &Key) -> Option<u32>
pub fn push_packed_root<T>(entity: &T, root_key: &Key)
where
T: PackedLayout,
{
<T as PackedLayout>::push_packed(entity, root_key);
ink_env::set_contract_storage(root_key, entity)
ink_env::set_contract_storage(root_key, entity);
}

/// Clears the entity from the contract storage using packed layout.
Expand Down

0 comments on commit cd26767

Please sign in to comment.