Remove IncrementalInput and fix wasm storage_into* (#3224)

This commit is contained in:
Bastian Köcher
2019-07-29 10:44:40 +02:00
committed by thiolliere
parent 852ed92861
commit 9303b9b7df
4 changed files with 20 additions and 56 deletions
+3 -3
View File
@@ -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())
}
+10 -8
View File
@@ -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<Vec<u8>>;
/// 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<usize>;
/// 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<usize>;
/// Set the storage of some particular key to Some value.
+4 -38
View File
@@ -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<T: Codec + Sized>(storage_key: &[u8], key: &[u8]) -> Option<T> {
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")
})
}
@@ -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<T: Decode + Sized>(key: &[u8]) -> Option<T> {
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")
})
}