Skip to content

Commit

Permalink
Update StorageBytes to use field_id over slot (FuelLabs#4541)
Browse files Browse the repository at this point in the history
## Description

The same was done for `StorageMap` and `StorageVec` such that they can
reside within the same nested struct in
FuelLabs#4537

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: bitzoic <[email protected]>
  • Loading branch information
bitzoic and bitzoic authored May 8, 2023
1 parent 1d6ab3b commit 90859c2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 60 deletions.
11 changes: 4 additions & 7 deletions sway-lib-std/src/storage/storage_bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ impl StorableSlice<Bytes> for StorageKey<StorageBytes> {
/// ```
#[storage(read, write)]
fn store(self, bytes: Bytes) {
let key = self.slot;
store_slice(key, bytes.as_raw_slice());
store_slice(self.field_id, bytes.as_raw_slice());
}

/// Constructs a `Bytes` type from a collection of tightly packed bytes in storage.
Expand Down Expand Up @@ -68,8 +67,7 @@ impl StorableSlice<Bytes> for StorageKey<StorageBytes> {
/// ```
#[storage(read)]
fn load(self) -> Option<Bytes> {
let key = self.slot;
match get_slice(key) {
match get_slice(self.field_id) {
Some(slice) => {
Some(Bytes::from_raw_slice(slice))
},
Expand Down Expand Up @@ -107,8 +105,7 @@ impl StorableSlice<Bytes> for StorageKey<StorageBytes> {
/// ```
#[storage(read, write)]
fn clear(self) -> bool {
let key = self.slot;
clear_slice(key)
clear_slice(self.field_id)
}

/// Returns the length of tightly packed bytes in storage.
Expand Down Expand Up @@ -137,6 +134,6 @@ impl StorableSlice<Bytes> for StorageKey<StorageBytes> {
/// ```
#[storage(read)]
fn len(self) -> u64 {
read::<u64>(self.slot, 0).unwrap_or(0)
read::<u64>(self.field_id, 0).unwrap_or(0)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
contract;

use std::bytes::Bytes;
use std::storage::storage_bytes::*;
use std::storage::storage_map::*;
use std::storage::storage_vec::*;

abi ReproAttempt {
#[storage(read, write)]
fn bytes_foo_store(bytes: Bytes);

#[storage(read)]
fn bytes_foo_get() -> Option<Bytes>;

#[storage(read)]
fn bytes_foo_len() -> u64;

#[storage(read, write)]
fn bytes_bar_store(bytes: Bytes);

#[storage(read)]
fn bytes_bar_get() -> Option<Bytes>;

#[storage(read)]
fn bytes_bar_len() -> u64;

#[storage(read, write)]
fn map_foo_insert(key: u64, value: u64);

Expand Down Expand Up @@ -35,6 +55,11 @@ abi ReproAttempt {
fn vec_bar_len() -> u64;
}

struct StructOfStorageBytes {
foo: StorageBytes,
bar: StorageBytes,
}

struct StructOfStorageMaps {
foo: StorageMap<u64, u64>,
bar: StorageMap<u64, u64>,
Expand All @@ -46,6 +71,10 @@ struct StructOfStorageVecs {
}

storage {
struct_of_bytes: StructOfStorageBytes = StructOfStorageBytes {
foo: StorageBytes {},
bar: StorageBytes {},
},
struct_of_maps: StructOfStorageMaps = StructOfStorageMaps {
foo: StorageMap {},
bar: StorageMap {},
Expand All @@ -57,6 +86,36 @@ storage {
}

impl ReproAttempt for Contract {
#[storage(read, write)]
fn bytes_foo_store(bytes: Bytes) {
storage.struct_of_bytes.foo.store(bytes);
}

#[storage(read)]
fn bytes_foo_get() -> Option<Bytes> {
storage.struct_of_bytes.foo.load()
}

#[storage(read)]
fn bytes_foo_len() -> u64 {
storage.struct_of_bytes.foo.len()
}

#[storage(read, write)]
fn bytes_bar_store(bytes: Bytes) {
storage.struct_of_bytes.bar.store(bytes);
}

#[storage(read)]
fn bytes_bar_get() -> Option<Bytes> {
storage.struct_of_bytes.bar.load()
}

#[storage(read)]
fn bytes_bar_len() -> u64 {
storage.struct_of_bytes.bar.len()
}

#[storage(read, write)]
fn map_foo_insert(key: u64, value: u64) {
storage.struct_of_maps.foo.insert(key, value);
Expand Down Expand Up @@ -118,6 +177,27 @@ impl ReproAttempt for Contract {
}
}

#[test()]
fn test_read_write_bytes() {
let repro = abi(ReproAttempt, CONTRACT_ID);

let mut my_bytes = Bytes::new();
my_bytes.push(1_u8);
my_bytes.push(2_u8);

assert(repro.bytes_foo_get().is_none());
assert(repro.bytes_bar_get().is_none());
assert(repro.bytes_foo_len() == 0);
assert(repro.bytes_bar_len() == 0);

repro.bytes_foo_store(my_bytes);

assert(repro.bytes_foo_get().unwrap() == my_bytes);
assert(repro.bytes_bar_get().is_none());
assert(repro.bytes_foo_len() == 2);
assert(repro.bytes_bar_len() == 0);
}

#[test()]
fn test_read_write_map() {
let repro = abi(ReproAttempt, CONTRACT_ID);
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 90859c2

Please sign in to comment.