Skip to content

Commit

Permalink
StorageVec fixing issues and adding features (FuelLabs#2345)
Browse files Browse the repository at this point in the history
* Added "set" method to storagevec and fixed edgecase for insert

* added tuple/arr tests and fixed set

* cargo fmt

* removed dead files
  • Loading branch information
SwayStar123 authored Jul 22, 2022
1 parent 95968a0 commit 02b7e94
Show file tree
Hide file tree
Showing 43 changed files with 1,461 additions and 28 deletions.
33 changes: 32 additions & 1 deletion sway-lib-std/src/storage.sw
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ impl<V> StorageVec<V> {
element_to_be_removed
}

/// Sets/mutates the value at the given index
///
/// # Arguments
///
/// * `index` - The index of the vec to set the value at
/// * `value` - The value to be set
///
/// # Reverts
///
/// Reverts if index is larger than or equal to the length of the vec
#[storage(read, write)]
pub fn set(self, index: u64, value: V) {
let len = get::<u64>(__get_storage_key());
// if the index is higher than or equal len, there is no element to set
assert(index < len);

let key = sha256((index, __get_storage_key()));
store::<V>(key, value);
}

/// Inserts the value at the given index, moving the current index's value aswell as the following's
/// Up one index
///
Expand All @@ -257,10 +277,21 @@ impl<V> StorageVec<V> {
// if the index is larger than len, there is no space to insert
assert(index <= len);

// if len is 0, index must also be 0 due to above check
if len == index {
let key = sha256((index, __get_storage_key()));
store::<V>(key, value);

// increments len by 1
store(__get_storage_key(), len + 1);

return;
}

// for every element in the vec with an index larger than the input index,
// move the element up one index.
// performed in reverse to prevent data overwriting
let mut count = len-1;
let mut count = len - 1;
while count >= index {
let key = sha256((count + 1, __get_storage_key()));
// shifts all the values up one index
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-F1A9B05C249770CB'
dependencies = []

[[package]]
name = 'std'
source = 'path+from-root-F1A9B05C249770CB'
dependencies = ['core']

[[package]]
name = 'svec_array'
source = 'root'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "svec_array"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
contract;

use std::{option::Option, result::Result, storage::StorageVec};

abi MyContract {
#[storage(read, write)]
fn array_push(value: [u8; 3]);
#[storage(read)]
fn array_get(index: u64) -> [u8; 3];
#[storage(read, write)]
fn array_pop() -> [u8; 3];
#[storage(read, write)]
fn array_remove(index: u64) -> [u8; 3];
#[storage(read, write)]
fn array_swap_remove(index: u64) -> [u8; 3];
#[storage(read, write)]
fn array_set(index: u64, value: [u8; 3]);
#[storage(read, write)]
fn array_insert(index: u64, value: [u8; 3]);
#[storage(read)]
fn array_len() -> u64;
#[storage(read)]
fn array_is_empty() -> bool;
#[storage(write)]
fn array_clear();
}

storage {
my_vec: StorageVec<[u8; 3]> = StorageVec {},
}

impl MyContract for Contract {
#[storage(read, write)]
fn array_push(value: [u8; 3]) {
storage.my_vec.push(value);
}
#[storage(read)]
fn array_get(index: u64) -> [u8; 3] {
storage.my_vec.get(index).unwrap()
}
#[storage(read, write)]
fn array_pop() -> [u8; 3] {
storage.my_vec.pop().unwrap()
}
#[storage(read, write)]
fn array_remove(index: u64) -> [u8; 3] {
storage.my_vec.remove(index)
}
#[storage(read, write)]
fn array_swap_remove(index: u64) -> [u8; 3] {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn array_set(index: u64, value: [u8; 3]) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn array_insert(index: u64, value: [u8; 3]) {
storage.my_vec.insert(index, value);
}
#[storage(read)]
fn array_len() -> u64 {
storage.my_vec.len()
}
#[storage(read)]
fn array_is_empty() -> bool {
storage.my_vec.is_empty()
}
#[storage(write)]
fn array_clear() {
storage.my_vec.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abi MyContract {
#[storage(read, write)]
fn b256_swap_remove(index: u64) -> b256;
#[storage(read, write)]
fn b256_set(index: u64, value: b256);
#[storage(read, write)]
fn b256_insert(index: u64, value: b256);
#[storage(read)]
fn b256_len() -> u64;
Expand Down Expand Up @@ -49,6 +51,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn b256_set(index: u64, value: b256) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn b256_insert(index: u64, value: b256) {
storage.my_vec.insert(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abi MyContract {
#[storage(read, write)]
fn bool_swap_remove(index: u64) -> bool;
#[storage(read, write)]
fn bool_set(index: u64, value: bool);
#[storage(read, write)]
fn bool_insert(index: u64, value: bool);
#[storage(read)]
fn bool_len() -> u64;
Expand Down Expand Up @@ -49,6 +51,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn bool_set(index: u64, value: bool) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn bool_insert(index: u64, value: bool) {
storage.my_vec.insert(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ abi MyContract {
#[storage(read, write)]
fn enum_swap_remove(index: u64) -> TestEnum;
#[storage(read, write)]
fn enum_set(index: u64, value: TestEnum);
#[storage(read, write)]
fn enum_insert(index: u64, value: TestEnum);
#[storage(read)]
fn enum_len() -> u64;
Expand Down Expand Up @@ -54,6 +56,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn enum_set(index: u64, value: TestEnum) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn enum_insert(index: u64, value: TestEnum) {
storage.my_vec.insert(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abi MyContract {
#[storage(read, write)]
fn str_swap_remove(index: u64) -> str[4];
#[storage(read, write)]
fn str_set(index: u64, value: str[4]);
#[storage(read, write)]
fn str_insert(index: u64, value: str[4]);
#[storage(read)]
fn str_len() -> u64;
Expand Down Expand Up @@ -49,6 +51,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn str_set(index: u64, value: str[4]) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn str_insert(index: u64, value: str[4]) {
storage.my_vec.insert(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ abi MyContract {
#[storage(read, write)]
fn struct_swap_remove(index: u64) -> TestStruct;
#[storage(read, write)]
fn struct_set(index: u64, value: TestStruct);
#[storage(read, write)]
fn struct_insert(index: u64, value: TestStruct);
#[storage(read)]
fn struct_len() -> u64;
Expand Down Expand Up @@ -54,6 +56,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn struct_set(index: u64, value: TestStruct) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn struct_insert(index: u64, value: TestStruct) {
storage.my_vec.insert(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'core'
source = 'path+from-root-ADD23FE8604712AD'
dependencies = []

[[package]]
name = 'std'
source = 'path+from-root-ADD23FE8604712AD'
dependencies = ['core']

[[package]]
name = 'svec_tuple'
source = 'root'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "svec_tuple"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
contract;

use std::{option::Option, result::Result, storage::StorageVec};

abi MyContract {
#[storage(read, write)]
fn tuple_push(value: (u8, u8, u8));
#[storage(read)]
fn tuple_get(index: u64) -> (u8, u8, u8);
#[storage(read, write)]
fn tuple_pop() -> (u8, u8, u8);
#[storage(read, write)]
fn tuple_remove(index: u64) -> (u8, u8, u8);
#[storage(read, write)]
fn tuple_swap_remove(index: u64) -> (u8, u8, u8);
#[storage(read, write)]
fn tuple_set(index: u64, value: (u8, u8, u8));
#[storage(read, write)]
fn tuple_insert(index: u64, value: (u8, u8, u8));
#[storage(read)]
fn tuple_len() -> u64;
#[storage(read)]
fn tuple_is_empty() -> bool;
#[storage(write)]
fn tuple_clear();
}

storage {
my_vec: StorageVec<(u8, u8, u8)> = StorageVec {},
}

impl MyContract for Contract {
#[storage(read, write)]
fn tuple_push(value: (u8, u8, u8)) {
storage.my_vec.push(value);
}
#[storage(read)]
fn tuple_get(index: u64) -> (u8, u8, u8) {
storage.my_vec.get(index).unwrap()
}
#[storage(read, write)]
fn tuple_pop() -> (u8, u8, u8) {
storage.my_vec.pop().unwrap()
}
#[storage(read, write)]
fn tuple_remove(index: u64) -> (u8, u8, u8) {
storage.my_vec.remove(index)
}
#[storage(read, write)]
fn tuple_swap_remove(index: u64) -> (u8, u8, u8) {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn tuple_set(index: u64, value: (u8, u8, u8)) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn tuple_insert(index: u64, value: (u8, u8, u8)) {
storage.my_vec.insert(index, value);
}
#[storage(read)]
fn tuple_len() -> u64 {
storage.my_vec.len()
}
#[storage(read)]
fn tuple_is_empty() -> bool {
storage.my_vec.is_empty()
}
#[storage(write)]
fn tuple_clear() {
storage.my_vec.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abi MyContract {
#[storage(read, write)]
fn u16_swap_remove(index: u64) -> u16;
#[storage(read, write)]
fn u16_set(index: u64, value: u16);
#[storage(read, write)]
fn u16_insert(index: u64, value: u16);
#[storage(read)]
fn u16_len() -> u64;
Expand Down Expand Up @@ -49,6 +51,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn u16_set(index: u64, value: u16) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn u16_insert(index: u64, value: u16) {
storage.my_vec.insert(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abi MyContract {
#[storage(read, write)]
fn u32_swap_remove(index: u64) -> u32;
#[storage(read, write)]
fn u32_set(index: u64, value: u32);
#[storage(read, write)]
fn u32_insert(index: u64, value: u32);
#[storage(read)]
fn u32_len() -> u64;
Expand Down Expand Up @@ -49,6 +51,10 @@ impl MyContract for Contract {
storage.my_vec.swap_remove(index)
}
#[storage(read, write)]
fn u32_set(index: u64, value: u32) {
storage.my_vec.set(index, value);
}
#[storage(read, write)]
fn u32_insert(index: u64, value: u32) {
storage.my_vec.insert(index, value);
}
Expand Down
Loading

0 comments on commit 02b7e94

Please sign in to comment.