diff --git a/substrate/core/executor/src/wasm_executor.rs b/substrate/core/executor/src/wasm_executor.rs index 1b82b97d76..d7a8640e32 100644 --- a/substrate/core/executor/src/wasm_executor.rs +++ b/substrate/core/executor/src/wasm_executor.rs @@ -411,7 +411,7 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, let written = std::cmp::min(value_len as usize, value.len()); this.memory.set(value_data, &value[..written]) .map_err(|_| "Invalid attempt to set value in ext_get_storage_into")?; - Ok(written as u32) + Ok(value.len() as u32) } else { Ok(u32::max_value()) } @@ -458,10 +458,10 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, if let Some(value) = maybe_value { let value = &value[value_offset as usize..]; - let written = ::std::cmp::min(value_len as usize, value.len()); + let written = std::cmp::min(value_len as usize, value.len()); this.memory.set(value_data, &value[..written]) .map_err(|_| "Invalid attempt to set value in ext_get_child_storage_into")?; - Ok(written as u32) + Ok(value.len() as u32) } else { Ok(u32::max_value()) } diff --git a/substrate/core/sr-io/src/lib.rs b/substrate/core/sr-io/src/lib.rs index 4a65ecfefe..7729a1e573 100644 --- a/substrate/core/sr-io/src/lib.rs +++ b/substrate/core/sr-io/src/lib.rs @@ -104,16 +104,18 @@ export_api! { /// Get `key` from child storage and return a `Vec`, empty if there's a problem. fn child_storage(storage_key: &[u8], key: &[u8]) -> Option>; - /// Get `key` from storage, placing the value into `value_out` (as much of it as possible) and return - /// the number of bytes that the entry in storage had beyond the offset or None if the storage entry - /// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned - /// number of bytes is not equal to the number of bytes written to the `value_out`. + /// Get `key` from storage, placing the value into `value_out` and return the number of + /// bytes that the entry in storage has beyond the offset or `None` if the storage entry + /// doesn't exist at all. + /// If `value_out` length is smaller than the returned length, only `value_out` length bytes + /// are copied into `value_out`. fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> Option; - /// Get `key` from child storage, placing the value into `value_out` (as much of it as possible) and return - /// the number of bytes that the entry in storage had beyond the offset or None if the storage entry - /// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned - /// number of bytes is not equal to the number of bytes written to the `value_out`. + /// Get `key` from child storage, placing the value into `value_out` and return the number + /// of bytes that the entry in storage has beyond the offset or `None` if the storage entry + /// doesn't exist at all. + /// If `value_out` length is smaller than the returned length, only `value_out` length bytes + /// are copied into `value_out`. fn read_child_storage(storage_key: &[u8], key: &[u8], value_out: &mut [u8], value_offset: usize) -> Option; /// Set the storage of some particular key to Some value. diff --git a/substrate/srml/support/src/storage/mod.rs b/substrate/srml/support/src/storage/mod.rs index 446855b55c..46b6603d91 100644 --- a/substrate/srml/support/src/storage/mod.rs +++ b/substrate/srml/support/src/storage/mod.rs @@ -18,7 +18,7 @@ use crate::rstd::prelude::*; use crate::rstd::borrow::Borrow; -use codec::{Codec, Encode, Decode, KeyedVec, Input, EncodeAppend}; +use codec::{Codec, Encode, Decode, KeyedVec, EncodeAppend}; use hashed::generator::{HashedStorage, StorageHasher}; use unhashed::generator::UnhashedStorage; @@ -27,35 +27,6 @@ pub mod storage_items; pub mod unhashed; pub mod hashed; -struct IncrementalInput<'a> { - key: &'a [u8], - pos: usize, -} - -impl<'a> Input for IncrementalInput<'a> { - fn read(&mut self, into: &mut [u8]) -> usize { - let len = runtime_io::read_storage(self.key, into, self.pos).unwrap_or(0); - let read = crate::rstd::cmp::min(len, into.len()); - self.pos += read; - read - } -} - -struct IncrementalChildInput<'a> { - storage_key: &'a [u8], - key: &'a [u8], - pos: usize, -} - -impl<'a> Input for IncrementalChildInput<'a> { - fn read(&mut self, into: &mut [u8]) -> usize { - let len = runtime_io::read_child_storage(self.storage_key, self.key, into, self.pos).unwrap_or(0); - let read = crate::rstd::cmp::min(len, into.len()); - self.pos += read; - read - } -} - /// The underlying runtime storage. pub struct RuntimeStorage; @@ -522,17 +493,12 @@ where /// Note that `storage_key` must be unique and strong (strong in the sense of being long enough to /// avoid collision from a resistant hash function (which unique implies)). pub mod child { - use super::{Codec, Decode, Vec, IncrementalChildInput}; + use super::{Codec, Decode, Vec}; /// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. pub fn get(storage_key: &[u8], key: &[u8]) -> Option { - runtime_io::read_child_storage(storage_key, key, &mut [0; 0][..], 0).map(|_| { - let mut input = IncrementalChildInput { - storage_key, - key, - pos: 0, - }; - Decode::decode(&mut input).expect("storage is not null, therefore must be a valid type") + runtime_io::child_storage(storage_key, key).map(|v| { + Decode::decode(&mut &v[..]).expect("storage is not null, therefore must be a valid type") }) } diff --git a/substrate/srml/support/src/storage/unhashed/mod.rs b/substrate/srml/support/src/storage/unhashed/mod.rs index 77e6c2b37b..5d086c36c4 100644 --- a/substrate/srml/support/src/storage/unhashed/mod.rs +++ b/substrate/srml/support/src/storage/unhashed/mod.rs @@ -17,18 +17,14 @@ //! Operation on unhashed runtime storage use crate::rstd::borrow::Borrow; -use super::{Codec, Encode, Decode, KeyedVec, Vec, IncrementalInput}; +use super::{Codec, Encode, Decode, KeyedVec, Vec}; pub mod generator; /// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. pub fn get(key: &[u8]) -> Option { - runtime_io::read_storage(key, &mut [0; 0][..], 0).map(|_| { - let mut input = IncrementalInput { - key, - pos: 0, - }; - Decode::decode(&mut input).expect("storage is not null, therefore must be a valid type") + runtime_io::storage(key).map(|val| { + Decode::decode(&mut &val[..]).expect("storage is not null, therefore must be a valid type") }) }