Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement storage access via storage keyword #646

Merged
merged 41 commits into from
Mar 24, 2022

Conversation

sezna
Copy link
Contributor

@sezna sezna commented Jan 15, 2022

This closes #645. This is a WIP currently.

Remaining:

  • parse storage accesses
  • type check storage accesses
  • build out some storage data structures and rules around what works and what doesn't in a storage block (tl;dr: implement mapping)
  • correctness tests

This enables storage declarations, accesses, and reassignment via the storage keyword.

Syntax

Storage Declaration

storage {
    <id>: <type>,
    ....
}

Storage Accesses

    ... = storage.<var>;
    ... = storage.<struct_var>;
    ... = storage.<struct_var>.<field>....

Storage Reassignment

    storage.<var> = ...
    storage.<struct_var> = ...
    storage.<struct_var>.<field> = ....

Notes

  • Each subfield is stored in its own storage slot (i.e. gets its own key). By subfield, I mean the most primitive type of an aggregate, though not all aggregates are fully supported yet. For example for the following storage:
struct T { x: u64, y: b256 } 
struct S { t: T };
storage {
    s: S,    
}

x and y will get their own storage slots.

@sezna sezna added enhancement New feature or request compiler General compiler. Should eventually become more specific as the issue is triaged labels Jan 15, 2022
@sezna sezna self-assigned this Jan 15, 2022
mitchmindtree added a commit to mitchmindtree/sway that referenced this pull request Feb 11, 2022
mitchmindtree added a commit that referenced this pull request Feb 15, 2022
… mdbook preprocessor. (#766)

* Extract `hello_world` example from the book into a standalone project

Related to #544.
Follows the example set in #731.
Tested with `mdbook serve`.

Also adds the `hello_world` and `subcurrency` examples to the Sway
workspace so that we can test both projects under CI.

* Fix hello_world example project name in forc manifest

* Move fizzbuzz and wallet_smart_contract examples to standalone projects

* Add `build-all-examples` script and associated CI passes

This script walks the examples directory and, for every directory
containing a `forc.toml` file, runs `forc build` for that directory.
Examples that encounter a failure of some kind have their `stdout` and
`stderr` piped through so we can easily identify cause of failure in CI.

This also removes the examples from the workspace.

Currently unsure whether or not the added CI pass to install `forc` will
result in `forc` actually being available on the PATH in the CI worker,
but will find out soon.

Still need to work out how to run `forc test` for each project without
having cargo complain about building within the parent sway workspace.

* Add a clear build summary at the end of `build-all-examples`

Currently, it's tricky to get an idea of how many successes/failures
there were without combing through the stdout/stderr. This adds a little
summary to the end that looks something like this:

```
Build all examples summary:
  [✓]: /home/mindtree/programming/rust/fuel/sway/examples/fizzbuzz succeeded!
  [✓]: /home/mindtree/programming/rust/fuel/sway/examples/hello_world succeeded!
  [x]: /home/mindtree/programming/rust/fuel/sway/examples/wallet_smart_contract failed!
  [✓]: /home/mindtree/programming/rust/fuel/sway/examples/subcurrency succeeded!
3 successes, 1 failure
```

* Set build-all-examples script version to 0.0.0

* Exclude examples from workspace, Fix hello_world test harness path

* Temporarily comment out storage related code in wallet example

This example should be updated after #646 lands.

* Install forc with `--debug` to share build artifacts

Currently it takes CI another 7 mins to build and install forc from
scratch. This should allow `cargo` to re-use the artifacts it has
already built during the sway building and testing steps earlier in the
CI job.

* Change author field in examples from mindtree -> Fuel Labs
@mohammadfawaz
Copy link
Contributor

mohammadfawaz commented Mar 21, 2022

I will do all the purity/impurity checks and warnings in a separate PR.

Also, had to force push because consolidating my branch and this one would've been a nightmare.

@mohammadfawaz mohammadfawaz marked this pull request as ready for review March 21, 2022 19:36
@mohammadfawaz mohammadfawaz self-assigned this Mar 21, 2022

// Test 1
caller.set_x {
gas: 10000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to specify gas here instead of default? My concern is that if we set the gas cost to a storage write to e.g. 20k like on Ethereum, then all these tests will fail. Why not just forward all gas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of our contract tests seem to be overriding gas.
When I remove this, I get an OutOfGas panic.

I get the same behavior with this test: https://github.com/FuelLabs/sway/blob/master/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw and the assembly for it looks correct. Here's the relevant snippet

...
lw   $r4 $cgas i0             ; loading $cgas (gas) into abi function
lw   $r3 data_3               ; loading the default coins value for call
lw   $r2 data_4               ; loading the default asset_id value for call
lw   $r1 data_5               ; literal instantiation
move $r7 $sp
cfei i48
mcpi $r7 $r1 i32              ; copy contract address for call
sw   $r7 $r0 i4               ; write fn selector to rA + 32 for call
sw   $r7 $r5 i5               ; move user param for call
call $r7 $r3 $r2 $r4          ; call external contract
...

isn't $cgas meant to start with a tx.gasLimit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$cgas starts at tx.gasLimit, yes. But with each contract call, it's initially the gas forwarded. Note that if you try to forward more than $cgas, then only $cgas is forwarded. It should not be a panic in the VM.

test/src/e2e_vm_tests/mod.rs Outdated Show resolved Hide resolved
@adlerjohn
Copy link
Contributor

Can't seem to add @sezna as a reviewer (maybe because he's the PR author?)

@sezna
Copy link
Contributor Author

sezna commented Mar 21, 2022

Can't seem to add @sezna as a reviewer (maybe because he's the PR author?)

taking a look!

Co-authored-by: Alex Hansen <alex@alex-hansen.com>
@sezna
Copy link
Contributor Author

sezna commented Mar 22, 2022

I would approve but I'm the author, so I can't. Cc @otrho perhaps since he has some context?

Cargo.lock Outdated Show resolved Hide resolved
@mohammadfawaz
Copy link
Contributor

mohammadfawaz commented Mar 23, 2022

@emilyaherbert merging with master was slightly non-trivial due to the new ParserLifter that you introduced. Will you please make sure I didn't mess anything up?

@emilyaherbert
Copy link
Contributor

@emilyaherbert merging with master was slightly non-trivial due to the new ParserLifter that you introduced. Will you please make sure I didn't mess anything up?

LGTM

@mohammadfawaz
Copy link
Contributor

I'm merging this even though the test stopped working now due some changes that Toby had to (rightfully) make to the contract call stuff in IR to make the verifier work as it needs. Toby and I will be dealing with that asap.

The AST generated for storage has not changed and so, this change continues to do the right thing as it was tested before Toby made the verifier changes.

@mohammadfawaz mohammadfawaz merged commit 85032fb into master Mar 24, 2022
@mohammadfawaz mohammadfawaz deleted the sezna/storage-access branch March 24, 2022 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Implement storage value access with storage syntax
4 participants