Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallible storage get and set methods for Lazy and Mapping #1910

Merged
merged 25 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
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 @@ -202,7 +202,7 @@ impl EnvBackend for EnvInstance {
K: scale::Encode,
R: Storable,
{
let mut output: [u8; 9600] = [0; 9600];
let mut output: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
match self.engine.get_storage(&key.encode(), &mut &mut output[..]) {
Ok(_) => (),
Err(ext::Error::KeyNotFound) => return Ok(None),
Expand All @@ -217,7 +217,7 @@ impl EnvBackend for EnvInstance {
K: scale::Encode,
R: Storable,
{
let mut output: [u8; 9600] = [0; 9600];
let mut output: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
match self
.engine
.take_storage(&key.encode(), &mut &mut output[..])
Expand Down
2 changes: 2 additions & 0 deletions crates/env/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use crate::engine::off_chain::OffChainError;
pub enum Error {
/// Error upon decoding an encoded value.
Decode(scale::Error),
/// The static buffer used during ABI encoding or ABI decoding is too small.
BufferTooSmall,
/// An error that can only occur in the off-chain environment.
#[cfg(any(feature = "std", test, doc))]
OffChain(OffChainError),
Expand Down
53 changes: 53 additions & 0 deletions crates/ink/macro/src/storage/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ fn storable_struct_derive(s: &synstructure::Structure) -> TokenStream2 {
::ink::storage::traits::Storable::encode(#binding, __dest);
)
});
let encoded_size_body = variant.each(|binding| {
let span = binding.ast().ty.span();
quote_spanned!(span =>
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(#binding));
)
});

s.gen_impl(quote! {
gen impl ::ink::storage::traits::Storable for @Self {
Expand All @@ -50,6 +57,15 @@ fn storable_struct_derive(s: &synstructure::Structure) -> TokenStream2 {
fn encode<__ink_O: ::ink::scale::Output + ?::core::marker::Sized>(&self, __dest: &mut __ink_O) {
match self { #encode_body }
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size(&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
match self { #encoded_size_body }
encoded_size
}
}
})
}
Expand Down Expand Up @@ -108,6 +124,30 @@ fn storable_enum_derive(s: &synstructure::Structure) -> TokenStream2 {
}
}
});

let encoded_size_body = s.variants().iter().enumerate().map(|(index, variant)| {
let pat = variant.pat();
let index = index as u8;
let fields = variant.bindings().iter().map(|field| {
let span = field.ast().ty.span();
quote_spanned!(span =>
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(#field));
)
});
quote! {
#pat => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive::u8 as ::ink::storage::traits::Storable>::encoded_size(&#index));
}
#(
{ #fields }
)*
}
}
});

s.gen_impl(quote! {
gen impl ::ink::storage::traits::Storable for @Self {
#[inline(always)]
Expand All @@ -130,6 +170,19 @@ fn storable_enum_derive(s: &synstructure::Structure) -> TokenStream2 {
)*
}
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size(&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
ascjones marked this conversation as resolved.
Show resolved Hide resolved
match self {
#(
#encoded_size_body
)*
}
encoded_size
}
}
})
}
Expand Down
153 changes: 153 additions & 0 deletions crates/ink/macro/src/tests/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ fn unit_struct_works() {
UnitStruct => { }
}
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size(&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
match self {
UnitStruct => {}
}
encoded_size
}
}
};
}
Expand Down Expand Up @@ -106,6 +117,30 @@ fn struct_works() {
}
}
}

#[inline (always)]
#[allow (non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size(&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
match self {
NamedFields { a : __binding_0 , b : __binding_1 , d : __binding_2 , } => {
{
encoded_size = encoded_size
.saturating_add(::ink::storage:: traits:: Storable:: encoded_size(__binding_0));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_1));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_2));
}
}
}
encoded_size
}
}
};
}
Expand Down Expand Up @@ -149,6 +184,22 @@ fn one_variant_enum_works() {
}
}
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size (&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
match self {
OneVariantEnum::A => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive::u8 as ::ink::storage::traits::Storable>::encoded_size(&0u8));
}
}
}
encoded_size
}
}
};
}
Expand Down Expand Up @@ -244,6 +295,50 @@ fn enum_works() {
}
}
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size (&self) -> ::core::primitive::usize{
let mut encoded_size: ::core::primitive::usize = 0;
match self {
MixedEnum::A => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive::u8 as ::ink::storage::traits::Storable>::encoded_size(&0u8));
}
}
MixedEnum::B(__binding_0, __binding_1,) => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive::u8 as ::ink::storage::traits::Storable>::encoded_size(&1u8));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_0));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_1));
}
}
MixedEnum::C { a: __binding_0, b: __binding_1, } => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive::u8 as ::ink::storage::traits::Storable>::encoded_size(&2u8));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_0));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_1));
}
}
}
encoded_size
}
}
};
}
Expand Down Expand Up @@ -310,6 +405,26 @@ fn generic_struct_works() {
}
}
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size(&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
match self {
GenericStruct { a : __binding_0 , b : __binding_1 , } => {
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_0));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_1));
}
}
}
encoded_size
}
ascjones marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
Expand Down Expand Up @@ -394,6 +509,44 @@ fn generic_enum_works() {
}
}
}

#[inline(always)]
#[allow(non_camel_case_types)]
#[allow(unused_mut)]
fn encoded_size(&self) -> ::core::primitive::usize {
let mut encoded_size: ::core::primitive::usize = 0;
match self {
GenericEnum::Tuple (__binding_0, __binding_1,) => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive:: u8 as::ink::storage::traits::Storable>::encoded_size(&0u8));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_0));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_1));
}
}
GenericEnum::Named { a: __binding_0, b: __binding_1,} => {
{
encoded_size = encoded_size
.saturating_add(<::core::primitive::u8 as::ink::storage::traits::Storable>::encoded_size(&1u8));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_0));
}
{
encoded_size = encoded_size
.saturating_add(::ink::storage::traits::Storable::encoded_size(__binding_1));
}
}
}
encoded_size
}
}
};
}
Expand Down
Loading