Skip to content

Commit

Permalink
Provide a StorageVec datastructure (#1995)
Browse files Browse the repository at this point in the history
Provide a vector like data structure, built on top of Mapping.

This allows to retrieve elements from the vector and grow the vector without loading and pushing all elements.
  • Loading branch information
xermicus authored Nov 30, 2023
1 parent e84a87c commit 9acc3c2
Show file tree
Hide file tree
Showing 10 changed files with 957 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .config/cargo_spellcheck.dic
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ payability
unpayable
initializer
WebSocket/S
StorageVec
KiB
GB
BufferTooSmall
KeyNotFound
ink_env
^
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Linter: `no_main` lint - [#2001](https://github.com/paritytech/ink/pull/2001)
- Clean E2E configuration parsing - [#1922](https://github.com/paritytech/ink/pull/1922)
- Make `set_code_hash` generic - [#1906](https://github.com/paritytech/ink/pull/1906)
- Provide a `StorageVec` datastructure built on top of `Lazy` - [#1995](https://github.com/paritytech/ink/pull/1955)

### Changed
- Messages return `TypeSpec` directly - #[1999](https://github.com/paritytech/ink/pull/1999)
Expand Down
1 change: 1 addition & 0 deletions crates/ink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub mod storage {
pub use ink_storage::{
Lazy,
Mapping,
StorageVec,
};
}

Expand Down
8 changes: 5 additions & 3 deletions crates/storage/src/lazy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
//! extra care has to be taken when operating directly on them.

mod mapping;
mod vec;

#[doc(inline)]
pub use self::mapping::Mapping;
pub use self::vec::StorageVec;

use crate::traits::{
AutoKey,
Expand Down Expand Up @@ -159,15 +161,15 @@ where
let key_size = <Key as Storable>::encoded_size(&KeyType::KEY);

if key_size >= ink_env::BUFFER_SIZE {
return Some(Err(ink_env::Error::BufferTooSmall))
return Some(Err(ink_env::Error::BufferTooSmall));
}

let value_size: usize = ink_env::contains_contract_storage(&KeyType::KEY)?
.try_into()
.expect("targets of less than 32bit pointer size are not supported; qed");

if key_size.saturating_add(value_size) > ink_env::BUFFER_SIZE {
return Some(Err(ink_env::Error::BufferTooSmall))
return Some(Err(ink_env::Error::BufferTooSmall));
}

self.get().map(Ok)
Expand All @@ -191,7 +193,7 @@ where
let value_size = <V as Storable>::encoded_size(value);

if key_size.saturating_add(value_size) > ink_env::BUFFER_SIZE {
return Err(ink_env::Error::BufferTooSmall)
return Err(ink_env::Error::BufferTooSmall);
};

self.set(value);
Expand Down
Loading

0 comments on commit 9acc3c2

Please sign in to comment.