-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Component storage collision example +
Switchable
component re…
…factor (#153) * feat: Components How-To with simple `Switch` Component * chore: remove `ch0*` prefix for improved modularity * Refactor switch component to switchable component * feat: Component storage collisions
- Loading branch information
Showing
10 changed files
with
167 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
mod switch; | ||
mod switch_collision; | ||
|
||
#[cfg(test)] | ||
mod tests; |
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
49 changes: 49 additions & 0 deletions
49
listings/applications/components/src/contracts/switch_collision.cairo
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,49 @@ | ||
// ANCHOR: interface | ||
#[starknet::interface] | ||
trait ISwitchCollision<TContractState> { | ||
fn set(ref self: TContractState, value: bool); | ||
fn get(ref self: TContractState) -> bool; | ||
} | ||
// ANCHOR_END: interface | ||
|
||
#[starknet::contract] | ||
mod SwitchCollisionContract { | ||
use components::switchable::switchable_component; | ||
|
||
component!(path: switchable_component, storage: switch, event: SwitchableEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl SwitchableImpl = switchable_component::Switchable<ContractState>; | ||
impl SwitchableInternalImpl = switchable_component::InternalImpl<ContractState>; | ||
|
||
// ANCHOR: storage | ||
#[storage] | ||
struct Storage { | ||
switchable_value: bool, | ||
#[substorage(v0)] | ||
switch: switchable_component::Storage, | ||
} | ||
// ANCHOR_END: storage | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState) { | ||
self.switch._off(); | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
SwitchableEvent: switchable_component::Event, | ||
} | ||
|
||
#[external(v0)] | ||
impl SwitchCollisionContract of super::ISwitchCollision<ContractState> { | ||
fn set(ref self: ContractState, value: bool) { | ||
self.switchable_value.write(value); | ||
} | ||
|
||
fn get(ref self: ContractState) -> bool { | ||
self.switchable_value.read() | ||
} | ||
} | ||
} |
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 @@ | ||
mod switch_collision_tests; |
44 changes: 44 additions & 0 deletions
44
listings/applications/components/src/contracts/tests/switch_collision_tests.cairo
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,44 @@ | ||
mod switch_collision_tests { | ||
use components::switchable::switchable_component::InternalTrait; | ||
use components::switchable::{ISwitchable, ISwitchableDispatcher, ISwitchableDispatcherTrait}; | ||
|
||
use components::contracts::switch_collision::{ | ||
SwitchCollisionContract, ISwitchCollisionDispatcher, ISwitchCollisionDispatcherTrait | ||
}; | ||
|
||
use core::starknet::storage::StorageMemberAccessTrait; | ||
use starknet::deploy_syscall; | ||
|
||
fn deploy() -> (ISwitchCollisionDispatcher, ISwitchableDispatcher) { | ||
let (contract_address, _) = deploy_syscall( | ||
SwitchCollisionContract::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false | ||
) | ||
.unwrap(); | ||
|
||
( | ||
ISwitchCollisionDispatcher { contract_address }, | ||
ISwitchableDispatcher { contract_address }, | ||
) | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
// ANCHOR: collision | ||
fn test_collision() { | ||
let (mut contract, mut contract_iswitch) = deploy(); | ||
|
||
assert(contract.get() == false, 'value !off'); | ||
assert(contract_iswitch.value() == false, 'switch !off'); | ||
|
||
contract_iswitch.switch(); | ||
assert(contract_iswitch.value() == true, 'switch !on'); | ||
assert(contract.get() == true, 'value !on'); | ||
|
||
// `collision` between component storage 'value' and contract storage 'value' | ||
assert(contract.get() == contract_iswitch.value(), 'value != switch'); | ||
|
||
contract.set(false); | ||
assert(contract.get() == contract_iswitch.value(), 'value != switch'); | ||
} | ||
// ANCHOR_END: collision | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
// Components | ||
mod switchable; | ||
|
||
mod contracts; | ||
mod switch; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Component-Contract Storage Collision | ||
|
||
Components can declare their own storage variables. | ||
|
||
When a contract use a component, the component storage is merged with the contract storage. | ||
The storage layout is only determined by the variables names, so variables with the same name will collide. | ||
|
||
> In a future release, the `#[substorage(v1)]` will determine the storage layout based on the component as well, so collisions will be avoided. | ||
A good practice is to prefix the component storage variables with the component name, as shown in the [Switchable component example](./how_to.md). | ||
|
||
#### Example | ||
|
||
Here's an example of a collision on the `switchable_value` storage variable of the `Switchable` component. | ||
|
||
Interface: | ||
```rust | ||
{{#include ../../listings/applications/components/src/contracts/switch_collision.cairo:interface}} | ||
``` | ||
|
||
Here's the storage of the contract (you can expand the code snippet to see the full contract): | ||
```rust | ||
{{#rustdoc_include ../../listings/applications/components/src/contracts/switch_collision.cairo:storage}} | ||
``` | ||
|
||
Both the contract and the component have a `switchable_value` storage variable, so they collide: | ||
|
||
```rust | ||
{{#rustdoc_include ../../listings/applications/components/src/contracts/tests/switch_collision_tests.cairo:collision}} | ||
``` |
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