-
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.
New method
get_handle
to help implementing nested storage maps
- Loading branch information
1 parent
100653a
commit 3945de9
Showing
8 changed files
with
238 additions
and
30 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
13 changes: 13 additions & 0 deletions
13
test/src/sdk-harness/test_projects/experimental_storage_nested_maps/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-2064A4F50B965AB3' | ||
|
||
[[package]] | ||
name = 'experimental_storage_nested_maps' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-2064A4F50B965AB3' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
test/src/sdk-harness/test_projects/experimental_storage_nested_maps/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]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "experimental_storage_nested_maps" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../sway-lib-std" } |
51 changes: 51 additions & 0 deletions
51
test/src/sdk-harness/test_projects/experimental_storage_nested_maps/mod.rs
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,51 @@ | ||
use fuels::prelude::*; | ||
|
||
abigen!(Contract( | ||
name = "TestExperimentalStorageNestedMapsContract", | ||
abi = "test_projects/experimental_storage_nested_maps/out/debug/experimental_storage_nested_maps-abi.json", | ||
)); | ||
|
||
async fn test_experimental_storage_nested_maps_instance( | ||
) -> TestExperimentalStorageNestedMapsContract { | ||
let wallet = launch_provider_and_get_wallet().await; | ||
let id = Contract::deploy( | ||
"test_projects/experimental_storage_nested_maps/out/debug/experimental_storage_nested_maps.bin", | ||
&wallet, | ||
TxParameters::default(), | ||
StorageConfiguration::with_storage_path(Some( | ||
"test_projects/experimental_storage_nested_maps/out/debug/experimental_storage_nested_maps-storage_slots.json" | ||
.to_string(), | ||
)), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
TestExperimentalStorageNestedMapsContract::new(id.clone(), wallet) | ||
} | ||
|
||
#[tokio::test] | ||
async fn nested_map_1_access() { | ||
let methods = test_experimental_storage_nested_maps_instance() | ||
.await | ||
.methods(); | ||
|
||
methods.nested_map_1_access().call().await.unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn nested_map_2_access() { | ||
let methods = test_experimental_storage_nested_maps_instance() | ||
.await | ||
.methods(); | ||
|
||
methods.nested_map_2_access().call().await.unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn nested_map_3_access() { | ||
let methods = test_experimental_storage_nested_maps_instance() | ||
.await | ||
.methods(); | ||
|
||
methods.nested_map_3_access().call().await.unwrap(); | ||
} |
136 changes: 136 additions & 0 deletions
136
test/src/sdk-harness/test_projects/experimental_storage_nested_maps/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,136 @@ | ||
contract; | ||
|
||
use core::experimental::storage::*; | ||
use std::experimental::storage::*; | ||
|
||
struct M { | ||
u: b256, | ||
v: u64, | ||
} | ||
|
||
impl core::ops::Eq for M { | ||
fn eq(self, other: Self) -> bool { | ||
self.u == other.u && self.v == other.v | ||
} | ||
} | ||
|
||
pub enum E { | ||
A: u64, | ||
B: b256, | ||
} | ||
|
||
impl core::ops::Eq for E { | ||
fn eq(self, other: Self) -> bool { | ||
match (self, other) { | ||
(E::A(l), E::A(r)) => l == r, | ||
(E::B(l), E::B(r)) => l == r, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
storage { | ||
nested_map_1: StorageMap<u64, StorageMap<u64, StorageMap<u64, u64>>> = StorageMap {}, | ||
nested_map_2: StorageMap<(u64, u64), StorageMap<str[4], StorageMap<u64, M>>> = StorageMap {}, | ||
nested_map_3: StorageMap<u64, StorageMap<M, StorageMap<u64, E>>> = StorageMap {}, | ||
} | ||
|
||
abi ExperimentalStorageTest { | ||
#[storage(read, write)] | ||
fn nested_map_1_access(); | ||
|
||
#[storage(read, write)] | ||
fn nested_map_2_access(); | ||
|
||
#[storage(read, write)] | ||
fn nested_map_3_access(); | ||
} | ||
|
||
impl ExperimentalStorageTest for Contract { | ||
#[storage(read, write)] | ||
fn nested_map_1_access() { | ||
storage.nested_map_1.get_handle(0).get_handle(0).insert(0, 1); | ||
storage.nested_map_1.get_handle(0).get_handle(0).insert(1, 2); | ||
storage.nested_map_1.get_handle(0).get_handle(1).insert(0, 3); | ||
storage.nested_map_1.get_handle(0).get_handle(1).insert(1, 4); | ||
storage.nested_map_1.get_handle(1).get_handle(0).insert(0, 5); | ||
storage.nested_map_1.get_handle(1).get_handle(0).insert(1, 6); | ||
storage.nested_map_1.get_handle(1).get_handle(1).insert(0, 7); | ||
storage.nested_map_1.get_handle(1).get_handle(1).insert(1, 8); | ||
|
||
assert(storage.nested_map_1.get_handle(0).get_handle(0).get(0).unwrap() == 1); | ||
assert(storage.nested_map_1.get_handle(0).get_handle(0).get(1).unwrap() == 2); | ||
assert(storage.nested_map_1.get_handle(0).get_handle(1).get(0).unwrap() == 3); | ||
assert(storage.nested_map_1.get_handle(0).get_handle(1).get(1).unwrap() == 4); | ||
assert(storage.nested_map_1.get_handle(1).get_handle(0).get(0).unwrap() == 5); | ||
assert(storage.nested_map_1.get_handle(1).get_handle(0).get(1).unwrap() == 6); | ||
assert(storage.nested_map_1.get_handle(1).get_handle(1).get(0).unwrap() == 7); | ||
assert(storage.nested_map_1.get_handle(1).get_handle(1).get(1).unwrap() == 8); | ||
|
||
assert(storage.nested_map_1.get_handle(2).get_handle(1).get(1).is_none()); | ||
assert(storage.nested_map_1.get_handle(1).get_handle(2).get(1).is_none()); | ||
assert(storage.nested_map_1.get_handle(1).get_handle(1).get(2).is_none()); | ||
} | ||
|
||
#[storage(read, write)] | ||
fn nested_map_2_access() { | ||
let m1 = M { | ||
u: 0x1111111111111111111111111111111111111111111111111111111111111111, | ||
v: 1 | ||
}; | ||
let m2 = M { | ||
u: 0x2222222222222222222222222222222222222222222222222222222222222222, | ||
v: 2 | ||
}; | ||
|
||
storage.nested_map_2.get_handle((0, 0)).get_handle("0000").insert(0, m1); | ||
storage.nested_map_2.get_handle((0, 0)).get_handle("0001").insert(1, m2); | ||
storage.nested_map_2.get_handle((0, 1)).get_handle("0000").insert(0, m1); | ||
storage.nested_map_2.get_handle((0, 1)).get_handle("0001").insert(1, m2); | ||
storage.nested_map_2.get_handle((1, 0)).get_handle("0000").insert(0, m1); | ||
storage.nested_map_2.get_handle((1, 0)).get_handle("0001").insert(1, m2); | ||
storage.nested_map_2.get_handle((1, 1)).get_handle("0000").insert(0, m1); | ||
storage.nested_map_2.get_handle((1, 1)).get_handle("0001").insert(1, m2); | ||
|
||
assert(storage.nested_map_2.get_handle((0, 0)).get_handle("0000").get(0).unwrap() == m1); | ||
assert(storage.nested_map_2.get_handle((0, 0)).get_handle("0001").get(1).unwrap() == m2); | ||
assert(storage.nested_map_2.get_handle((0, 1)).get_handle("0000").get(0).unwrap() == m1); | ||
assert(storage.nested_map_2.get_handle((0, 1)).get_handle("0001").get(1).unwrap() == m2); | ||
assert(storage.nested_map_2.get_handle((1, 0)).get_handle("0000").get(0).unwrap() == m1); | ||
assert(storage.nested_map_2.get_handle((1, 0)).get_handle("0001").get(1).unwrap() == m2); | ||
assert(storage.nested_map_2.get_handle((1, 1)).get_handle("0000").get(0).unwrap() == m1); | ||
assert(storage.nested_map_2.get_handle((1, 1)).get_handle("0001").get(1).unwrap() == m2); | ||
} | ||
|
||
#[storage(read, write)] | ||
fn nested_map_3_access() { | ||
let m1 = M { | ||
u: 0x1111111111111111111111111111111111111111111111111111111111111111, | ||
v: 1 | ||
}; | ||
let m2 = M { | ||
u: 0x2222222222222222222222222222222222222222222222222222222222222222, | ||
v: 2 | ||
}; | ||
let e1 = E::A(42); | ||
let e2 = E::B(0x3333333333333333333333333333333333333333333333333333333333333333); | ||
|
||
storage.nested_map_3.get_handle(0).get_handle(m1).insert(0, e1); | ||
storage.nested_map_3.get_handle(0).get_handle(m2).insert(1, e2); | ||
storage.nested_map_3.get_handle(0).get_handle(m1).insert(0, e1); | ||
storage.nested_map_3.get_handle(0).get_handle(m2).insert(1, e2); | ||
storage.nested_map_3.get_handle(1).get_handle(m1).insert(0, e1); | ||
storage.nested_map_3.get_handle(1).get_handle(m2).insert(1, e2); | ||
storage.nested_map_3.get_handle(1).get_handle(m1).insert(0, e1); | ||
storage.nested_map_3.get_handle(1).get_handle(m2).insert(1, e2); | ||
|
||
assert(storage.nested_map_3.get_handle(0).get_handle(m1).get(0).unwrap() == e1); | ||
assert(storage.nested_map_3.get_handle(0).get_handle(m2).get(1).unwrap() == e2); | ||
assert(storage.nested_map_3.get_handle(0).get_handle(m1).get(0).unwrap() == e1); | ||
assert(storage.nested_map_3.get_handle(0).get_handle(m2).get(1).unwrap() == e2); | ||
assert(storage.nested_map_3.get_handle(1).get_handle(m1).get(0).unwrap() == e1); | ||
assert(storage.nested_map_3.get_handle(1).get_handle(m2).get(1).unwrap() == e2); | ||
assert(storage.nested_map_3.get_handle(1).get_handle(m1).get(0).unwrap() == e1); | ||
assert(storage.nested_map_3.get_handle(1).get_handle(m2).get(1).unwrap() == e2); | ||
} | ||
} |
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