-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage name based slots and namespaces. (#6064)
## Description With this change storage keys will have a hash based on: `sha256("storage::<storage_namespace_name1>::<storage_namespace_name2>.<storage_field_name>"` or a given key. A key can be provided by using the `in` keyword in a storage field as such: ``` storage { a in 0x02dac99c283f16bc91b74f6942db7f012699a2ad51272b15207b9cc14a70dbae: u64 } ``` Or by using a const expression: ``` const C1 = 0x02dac99c283f16bc91b74f6942db7f012699a2ad51272b15207b9cc14a70dbae; storage { a in C1: u64 } ``` StorageKey.field_id is now based on: `sha256("storage::<storage_namespace_name1>::<storage_namespace_name2>.<storage_field_name>.<struct_field_name1>.<struct_field_name2>")` Nested storage namespaces such as the following are now supported. ``` storage { my_namespace1 { my_namespace2 { b:u64 = 1, } } } ``` A deprecated warning is now thrown when the attribute `#[namespace(ns)]` is used. A warning is thrown when storage fields use the same storage slot key. The warning looks like this: ``` Two storage fields are using the same storage key. First field: storage::ns1.f3 Second field: storage::ns2.f4 Key: 5F4C20CE4BD128E5393A4C2B82007DAC795FA0006D01ACF8DB4C42632BC680CA ``` This PR does not include yet the necessary documentation updates. Created dev-rel documentation request: FuelLabs/devrel-requests#14 ## Checklist - [ ] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [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: IGI-111 <igi-111@protonmail.com>
- Loading branch information
Showing
64 changed files
with
1,279 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
docs/reference/src/code/operations/storage/storage_in_keyword/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
14 changes: 14 additions & 0 deletions
14
docs/reference/src/code/operations/storage/storage_in_keyword/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-815C32BB386C1563' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'git+https://github.com/fuellabs/sway?tag=v0.25.2#dfa6224932a97c514b707dcfc300715b2a0895dc' | ||
dependencies = ['core'] | ||
|
||
[[package]] | ||
name = 'storage_init' | ||
source = 'root' | ||
dependencies = ['std'] |
8 changes: 8 additions & 0 deletions
8
docs/reference/src/code/operations/storage/storage_in_keyword/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "storage_in_keyword" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
39 changes: 39 additions & 0 deletions
39
docs/reference/src/code/operations/storage/storage_in_keyword/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
contract; | ||
|
||
// ANCHOR: data_structures | ||
struct Owner { | ||
maximum_owners: u64, | ||
role: Role, | ||
} | ||
|
||
impl Owner { | ||
// a constructor that can be evaluated to a constant `Owner` during compilation | ||
fn default() -> Self { | ||
Self { | ||
maximum_owners: 10, | ||
role: Role::FullAccess, | ||
} | ||
} | ||
} | ||
|
||
enum Role { | ||
FullAccess: (), | ||
PartialAccess: (), | ||
NoAccess: (), | ||
} | ||
|
||
const HASH_KEY: b256 = 0x7616e5793ef977b22465f0c843bcad56155c4369245f347bcc8a61edb08b7645; | ||
|
||
// ANCHOR_END: data_structures | ||
// ANCHOR: initialization | ||
storage { | ||
// ANCHOR: in_keyword | ||
current_owners in HASH_KEY: u64 = 0, | ||
// ANCHOR_END: in_keyword | ||
explicit_declaration: Owner = Owner { | ||
maximum_owners: 10, | ||
role: Role::FullAccess, | ||
}, | ||
encapsulated_declaration: Owner = Owner::default(), | ||
} | ||
// ANCHOR_END: initialization |
12 changes: 0 additions & 12 deletions
12
docs/reference/src/documentation/language/annotations/attributes/namespace.md
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
docs/reference/src/documentation/operations/storage/in-keyword.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
# Storage `in` keyword | ||
|
||
The `in` keyword can be used within a storage variable to override the position where the value is stored. | ||
|
||
## Example | ||
|
||
The `in` keyword and position value are used as follows: | ||
|
||
```sway | ||
{{#include ../../../../code/operations/storage/storage_in_keyword/src/main.sw:in_keyword}} | ||
``` | ||
|
||
The code above will force the storage of the variable into the position `HASH_KEY` which is set to `0x7616e5793ef977b22465f0c843bcad56155c4369245f347bcc8a61edb08b7645` instead of the position that would be calculated from the variable name `sha256("storage.current_owners")` which would be `0x84f905e3f560d70fbfab9ffcd92198998ce6f936e3d45f8fcb16b00f6a6a8d7e` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
docs/reference/src/documentation/operations/storage/namespace.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
# Namespace | ||
|
||
Namespaces can be used on a `storage` block and variables placed inside the namespaces. A single `storage` block may contain multiple namespaces placed sequentially and or nested. | ||
|
||
The hash calculations determining the position of variables in a block with namespace `my_namespace` that contains the variable `foobar` are calculated from `sha256("storage::my_namespace.foobar")`. | ||
|
||
## Example | ||
|
||
A namespace can be declared as follows: | ||
|
||
```sway | ||
{{#include ../../../../code/language/annotations/src/main.sw:storage_namespace}} | ||
``` | ||
|
||
A variable inside a namespace can be accessed as follows: | ||
|
||
```sway | ||
{{#include ../../../../code/language/annotations/src/main.sw:storage_namespace_access}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.