forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description This adds a field to `StorageKey` that is unique among storage fields. It aims to alleviate the problem created by having multiple zero sized fields inside of a storage struct, which are, by necessity, living in the same slot. It allows `StorageMap` (which itself is zero-sized) to address its own values on a unique address map instead of colliding with adjacent `StorageMap`s. This is a breaking change because it will generate different IR for the same code. Fixes FuelLabs#4524 Fixes FuelLabs#4523 ## 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: Sophie Dankel <[email protected]>
- Loading branch information
Showing
14 changed files
with
177 additions
and
16 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
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
13 changes: 13 additions & 0 deletions
13
test/src/e2e_vm_tests/test_programs/should_pass/empty_fields_in_storage_struct/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,13 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-C02956062CE96F8B' | ||
|
||
[[package]] | ||
name = 'empty_fields_in_storage_struct' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-C02956062CE96F8B' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
test/src/e2e_vm_tests/test_programs/should_pass/empty_fields_in_storage_struct/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 <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "empty_fields_in_storage_struct" | ||
entry = "main.sw" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../sway-lib-std" } |
62 changes: 62 additions & 0 deletions
62
test/src/e2e_vm_tests/test_programs/should_pass/empty_fields_in_storage_struct/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,62 @@ | ||
contract; | ||
|
||
use std::storage::storage_map::*; | ||
|
||
abi ReproAttempt { | ||
#[storage(read, write)] | ||
fn foo_insert(key: u64, value: u64); | ||
|
||
#[storage(read)] | ||
fn foo_get(key: u64) -> Option<u64>; | ||
|
||
#[storage(read, write)] | ||
fn bar_insert(key: u64, value: u64); | ||
|
||
#[storage(read)] | ||
fn bar_get(key: u64) -> Option<u64>; | ||
} | ||
|
||
struct StructOfStorageMaps { | ||
foo: StorageMap<u64, u64>, | ||
bar: StorageMap<u64, u64>, | ||
} | ||
|
||
storage { | ||
struct_of_maps: StructOfStorageMaps = StructOfStorageMaps { | ||
foo: StorageMap {}, | ||
bar: StorageMap {}, | ||
}, | ||
} | ||
|
||
impl ReproAttempt for Contract { | ||
#[storage(read, write)] | ||
fn foo_insert(key: u64, value: u64) { | ||
storage.struct_of_maps.foo.insert(key, value); | ||
} | ||
|
||
#[storage(read)] | ||
fn foo_get(key: u64) -> Option<u64> { | ||
storage.struct_of_maps.foo.get(key).try_read() | ||
} | ||
|
||
#[storage(read, write)] | ||
fn bar_insert(key: u64, value: u64) { | ||
storage.struct_of_maps.bar.insert(key, value); | ||
} | ||
|
||
#[storage(read)] | ||
fn bar_get(key: u64) -> Option<u64> { | ||
storage.struct_of_maps.bar.get(key).try_read() | ||
} | ||
} | ||
|
||
#[test()] | ||
fn test_read_write() { | ||
let repro = abi(ReproAttempt, CONTRACT_ID); | ||
|
||
assert(repro.foo_get(1).is_none()); | ||
repro.foo_insert(1, 2); | ||
assert(repro.foo_get(1).unwrap() == 2); | ||
|
||
assert(repro.bar_get(1).is_none()); | ||
} |
43 changes: 43 additions & 0 deletions
43
test/src/e2e_vm_tests/test_programs/should_pass/empty_fields_in_storage_struct/src/wallet.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,43 @@ | ||
contract; | ||
|
||
mod wallet_abi; | ||
|
||
use std::{ | ||
auth::AuthError, | ||
call_frames::msg_asset_id, | ||
constants::BASE_ASSET_ID, | ||
context::msg_amount, | ||
token::transfer_to_address, | ||
}; | ||
|
||
use wallet_abi::Wallet; | ||
const OWNER_ADDRESS = Address::from(0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861); | ||
|
||
storage { | ||
balance: u64 = 0, | ||
} | ||
|
||
impl Wallet for Contract { | ||
#[payable, storage(read, write)] | ||
fn receive_funds() { | ||
if msg_asset_id() == BASE_ASSET_ID { | ||
storage.balance.write(storage.balance.read() + msg_amount()); | ||
} | ||
} | ||
|
||
#[storage(read, write)] | ||
fn send_funds(amount_to_send: u64, recipient_address: Address) { | ||
let sender: Result<Identity, AuthError> = msg_sender(); | ||
match sender.unwrap() { | ||
Identity::Address(addr) => assert(addr == OWNER_ADDRESS), | ||
_ => revert(0), | ||
}; | ||
|
||
let current_balance = storage.balance.read(); | ||
assert(current_balance >= amount_to_send); | ||
|
||
storage.balance.write(current_balance - amount_to_send); | ||
|
||
transfer_to_address(amount_to_send, BASE_ASSET_ID, recipient_address); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...c/e2e_vm_tests/test_programs/should_pass/empty_fields_in_storage_struct/src/wallet_abi.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,10 @@ | ||
library; | ||
|
||
abi Wallet { | ||
#[payable, storage(read, write)] | ||
fn receive_funds(); | ||
|
||
// non-payable method | ||
#[storage(read, write)] | ||
fn send_funds(amount_to_send: u64, recipient_address: Address); | ||
} |
1 change: 1 addition & 0 deletions
1
test/src/e2e_vm_tests/test_programs/should_pass/empty_fields_in_storage_struct/test.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 @@ | ||
category = "unit_tests_pass" |
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