From cbfa98a68a10e255ebe545072b21ab5be031110c Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 6 Sep 2023 14:22:36 +0200 Subject: [PATCH 1/2] Add new() function to StorageKey --- sway-lib-std/src/storage/storage_key.sw | 39 +++++++++++++++++++++++++ sway-lib-std/src/storage/storage_map.sw | 6 +--- sway-lib-std/src/storage/storage_vec.sw | 18 ++---------- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/sway-lib-std/src/storage/storage_key.sw b/sway-lib-std/src/storage/storage_key.sw index 49bcb6aa660..08d2e53b91e 100644 --- a/sway-lib-std/src/storage/storage_key.sw +++ b/sway-lib-std/src/storage/storage_key.sw @@ -97,4 +97,43 @@ impl StorageKey { pub fn write(self, value: T) { write(self.slot, self.offset, value); } + + /// Create a new `StorageKey`. + /// + /// # Arguments + /// + /// * `slot`: [b256] - The assigned location in storage for the new `StorageKey`. + /// * `offset`: [u64] - The assigned offset based on the data structure `T` for the new `StorageKey`. + /// * `field_id`: [b256] - A unqiue identifier for the new `StorageKey`. + /// + /// # Returns + /// + /// * [StorageKey] - The newly create `StorageKey`. + /// + /// # Examples + /// + /// ```sway + /// use std::{constants::ZERO_B256, hash::sha256}; + /// + /// fn foo() { + /// let my_key = StorageKey::::new(ZERO_B256, 0, sha256(ZERO_B256)); + /// assert(my_key.slot == ZERO_B256); + /// } + /// ``` + pub fn new(slot: b256, offset: u64, field_id: b256) -> Self { + Self { + slot, offset, field_id + } + } +} + +#[test] +fn test_storage_key_new() { + use ::constants::ZERO_B256; + use ::assert::assert; + + let key = StorageKey::::new(ZERO_B256, 0, ZERO_B256); + assert(key.slot == ZERO_B256); + assert(key.offset == 0); + assert(key.field_id == ZERO_B256); } diff --git a/sway-lib-std/src/storage/storage_map.sw b/sway-lib-std/src/storage/storage_map.sw index 51f93313451..7d3eb8f5999 100644 --- a/sway-lib-std/src/storage/storage_map.sw +++ b/sway-lib-std/src/storage/storage_map.sw @@ -68,11 +68,7 @@ impl StorageKey> where K: Hash { /// } /// ``` pub fn get(self, key: K) -> StorageKey where K: Hash { - StorageKey { - slot: sha256((key, self.field_id)), - offset: 0, - field_id: sha256((key, self.field_id)), - } + StorageKey::::new(sha256((key, self.field_id)), 0, sha256((key, self.field_id))) } /// Clears a value previously stored using a key diff --git a/sway-lib-std/src/storage/storage_vec.sw b/sway-lib-std/src/storage/storage_vec.sw index 44437a86ef4..7f2ffa2ac0e 100644 --- a/sway-lib-std/src/storage/storage_vec.sw +++ b/sway-lib-std/src/storage/storage_vec.sw @@ -134,11 +134,7 @@ impl StorageKey> { return None; } - Some(StorageKey { - slot: sha256((index, self.field_id)), - offset: 0, - field_id: sha256((index, self.field_id)), - }) + Some(StorageKey::::new(sha256((index, self.field_id)), 0, sha256((index, self.field_id)))) } /// Removes the element in the given index and moves all the elements in the following indexes @@ -575,11 +571,7 @@ impl StorageKey> { pub fn first(self) -> Option> { match read::(self.field_id, 0).unwrap_or(0) { 0 => None, - _ => Some(StorageKey { - slot: sha256((0, self.field_id)), - offset: 0, - field_id: sha256((0, self.field_id)), - }), + _ => Some(StorageKey::::new(sha256((0, self.field_id)), 0, sha256((0, self.field_id)))), } } @@ -614,11 +606,7 @@ impl StorageKey> { pub fn last(self) -> Option> { match read::(self.field_id, 0).unwrap_or(0) { 0 => None, - len => Some(StorageKey { - slot: sha256((len - 1, self.field_id)), - offset: 0, - field_id: sha256((0, self.field_id)), - }), + len => Some(StorageKey::::new(sha256((len - 1, self.field_id)), 0, sha256((0, self.field_id)))), } } From cf118de81366dfb0416c1301b89c56bade44c2ca Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 7 Sep 2023 10:28:39 +0200 Subject: [PATCH 2/2] Updates to address PR review comments --- sway-lib-core/src/storage.sw | 2 +- sway-lib-std/src/storage/storage_key.sw | 2 +- sway-lib-std/src/storage/storage_map.sw | 6 +++++- sway-lib-std/src/storage/storage_vec.sw | 18 +++++++++++++++--- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/sway-lib-core/src/storage.sw b/sway-lib-core/src/storage.sw index 3eb08801233..959a7f1dfa6 100644 --- a/sway-lib-core/src/storage.sw +++ b/sway-lib-core/src/storage.sw @@ -15,6 +15,6 @@ pub struct StorageKey { slot: b256, /// The assigned offset based on the data structure `T`. offset: u64, - /// A unqiue identifier. + /// A unique identifier. field_id: b256, } diff --git a/sway-lib-std/src/storage/storage_key.sw b/sway-lib-std/src/storage/storage_key.sw index 08d2e53b91e..a4cb12f7d91 100644 --- a/sway-lib-std/src/storage/storage_key.sw +++ b/sway-lib-std/src/storage/storage_key.sw @@ -104,7 +104,7 @@ impl StorageKey { /// /// * `slot`: [b256] - The assigned location in storage for the new `StorageKey`. /// * `offset`: [u64] - The assigned offset based on the data structure `T` for the new `StorageKey`. - /// * `field_id`: [b256] - A unqiue identifier for the new `StorageKey`. + /// * `field_id`: [b256] - A unique identifier for the new `StorageKey`. /// /// # Returns /// diff --git a/sway-lib-std/src/storage/storage_map.sw b/sway-lib-std/src/storage/storage_map.sw index 7d3eb8f5999..92022d153bd 100644 --- a/sway-lib-std/src/storage/storage_map.sw +++ b/sway-lib-std/src/storage/storage_map.sw @@ -68,7 +68,11 @@ impl StorageKey> where K: Hash { /// } /// ``` pub fn get(self, key: K) -> StorageKey where K: Hash { - StorageKey::::new(sha256((key, self.field_id)), 0, sha256((key, self.field_id))) + StorageKey::::new( + sha256((key, self.field_id)), + 0, + sha256((key, self.field_id)) + ) } /// Clears a value previously stored using a key diff --git a/sway-lib-std/src/storage/storage_vec.sw b/sway-lib-std/src/storage/storage_vec.sw index 7f2ffa2ac0e..ff7cdde7b35 100644 --- a/sway-lib-std/src/storage/storage_vec.sw +++ b/sway-lib-std/src/storage/storage_vec.sw @@ -134,7 +134,11 @@ impl StorageKey> { return None; } - Some(StorageKey::::new(sha256((index, self.field_id)), 0, sha256((index, self.field_id)))) + Some(StorageKey::::new( + sha256((index, self.field_id)), + 0, + sha256((index, self.field_id)) + )) } /// Removes the element in the given index and moves all the elements in the following indexes @@ -571,7 +575,11 @@ impl StorageKey> { pub fn first(self) -> Option> { match read::(self.field_id, 0).unwrap_or(0) { 0 => None, - _ => Some(StorageKey::::new(sha256((0, self.field_id)), 0, sha256((0, self.field_id)))), + _ => Some(StorageKey::::new( + sha256((0, self.field_id)), + 0, + sha256((0, self.field_id)) + )), } } @@ -606,7 +614,11 @@ impl StorageKey> { pub fn last(self) -> Option> { match read::(self.field_id, 0).unwrap_or(0) { 0 => None, - len => Some(StorageKey::::new(sha256((len - 1, self.field_id)), 0, sha256((0, self.field_id)))), + len => Some(StorageKey::::new( + sha256((len - 1, self.field_id)), + 0, + sha256((0, self.field_id)) + )), } }