Skip to content

Commit

Permalink
Merge pull request #1463 from multiversx/map-mapper-read-at-address
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca authored Mar 8, 2024
2 parents 9565b35 + 04564ca commit 46b8a50
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,69 @@
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "fill map mapper",
"tx": {
"from": "address:an_account",
"to": "sc:to-be-called",
"function": "fill_map_mapper",
"arguments": [
"5"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "keys at address",
"tx": {
"from": "address:an_account",
"to": "sc:caller",
"function": "keys_at_address",
"arguments": [],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x0000271100002712000027130000271400002715"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "values at address",
"tx": {
"from": "address:an_account",
"to": "sc:caller",
"function": "values_at_address",
"arguments": [],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x0000000100000002000000030000000400000005"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,42 @@ pub trait StorageMapperGetAtAddress {
mapper.back().unwrap()
}

#[endpoint]
fn keys_at_address(&self) -> ManagedVec<u32> {
let address = self.contract_address().get();
let mapper: MapMapper<u32, u32, _> =
MapMapper::new_from_address(address, StorageKey::from("map_mapper"));
mapper.keys().collect()
}

#[endpoint]
fn values_at_address(&self) -> ManagedVec<u32> {
let address = self.contract_address().get();
let mapper: MapMapper<u32, u32, _> =
MapMapper::new_from_address(address, StorageKey::from("map_mapper"));
mapper.values().collect()
}

/// Storage to be called. For testing, this contract is deployed twice,
/// and this module acts both as caller and receiver
#[storage_mapper("set_mapper")]
fn set_mapper(&self) -> SetMapper<u32>;

#[storage_mapper("map_mapper")]
fn map_mapper(&self) -> MapMapper<u32, u32>;

#[endpoint]
fn fill_set_mapper(&self, value: u32) {
for item in 1u32..=value {
self.set_mapper().insert(item);
}
}

#[endpoint]
fn fill_map_mapper(&self, value: u32) {
for item in 1u32..=value {
let key = 10_000u32 + item;
self.map_mapper().insert(key, item);
}
}
}
7 changes: 5 additions & 2 deletions contracts/feature-tests/basic-features/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 384
// Endpoints: 387
// Async Callback: 1
// Total number of exported functions: 386
// Total number of exported functions: 389

#![no_std]
#![allow(internal_features)]
Expand Down Expand Up @@ -403,7 +403,10 @@ multiversx_sc_wasm_adapter::endpoints! {
previous_at_address => previous_at_address
front_at_address => front_at_address
back_at_address => back_at_address
keys_at_address => keys_at_address
values_at_address => values_at_address
fill_set_mapper => fill_set_mapper
fill_map_mapper => fill_map_mapper
)
}

Expand Down
8 changes: 6 additions & 2 deletions framework/base/src/storage/mappers/map_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
multi_encode_iter_or_handle_err, multi_types::MultiValue2, CodecFrom, EncodeErrorHandler,
NestedDecode, NestedEncode, TopDecode, TopEncode, TopEncodeMulti, TopEncodeMultiOutput,
},
storage::{storage_clear, storage_get, storage_set, StorageKey},
storage::{storage_clear, storage_set, StorageKey},
types::{ManagedAddress, ManagedType, MultiValueEncoded},
};

Expand All @@ -26,6 +26,7 @@ where
V: TopEncode + TopDecode + 'static,
{
_phantom_api: PhantomData<SA>,
address: A,
base_key: StorageKey<SA>,
keys_set: SetMapper<SA, K, A>,
_phantom_value: PhantomData<V>,
Expand All @@ -40,6 +41,7 @@ where
fn new(base_key: StorageKey<SA>) -> Self {
MapMapper {
_phantom_api: PhantomData,
address: CurrentStorage,
base_key: base_key.clone(),
keys_set: SetMapper::new(base_key),
_phantom_value: PhantomData,
Expand Down Expand Up @@ -106,6 +108,7 @@ where
pub fn new_from_address(address: ManagedAddress<SA>, base_key: StorageKey<SA>) -> Self {
MapMapper {
_phantom_api: PhantomData,
address: address.clone(),
base_key: base_key.clone(),
keys_set: SetMapper::new_from_address(address, base_key),
_phantom_value: PhantomData,
Expand Down Expand Up @@ -149,7 +152,8 @@ where
}

fn get_mapped_value(&self, key: &K) -> V {
storage_get(self.build_named_key(MAPPED_VALUE_IDENTIFIER, key).as_ref())
self.address
.address_storage_get(self.build_named_key(MAPPED_VALUE_IDENTIFIER, key).as_ref())
}

/// Gets a reference to the value in the entry.
Expand Down

0 comments on commit 46b8a50

Please sign in to comment.