Skip to content

Commit

Permalink
set mapper new features
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Nov 17, 2023
1 parent 63c6221 commit efab4d4
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,209 @@
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "first-insert",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_insert",
"arguments": [
"111"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"true"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "second-insert",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_insert",
"arguments": [
"222"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"true"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "third-insert",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_insert",
"arguments": [
"333"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"true"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "check-front",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_front",
"arguments": [],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"111"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "check-back",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_back",
"arguments": [],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"333"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "check-next",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_next",
"arguments": [
"222"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"333"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "check-next-out-of-bounds",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_next",
"arguments": [
"333"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "check-previous",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_previous",
"arguments": [
"222"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"111"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "check-iter-from",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "set_mapper_iter_from_and_count",
"arguments": [
"222"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"2"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
}
]
}
35 changes: 35 additions & 0 deletions contracts/feature-tests/basic-features/src/storage_mapper_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,39 @@ pub trait SetMapperFeatures {
let mut set_mapper = self.set_mapper();
set_mapper.remove(&item)
}

#[endpoint]
fn set_mapper_front(&self) -> u32 {
let set_mapper = self.set_mapper();
set_mapper.front().unwrap_or_default()
}

#[endpoint]
fn set_mapper_back(&self) -> u32 {
let set_mapper = self.set_mapper();
set_mapper.back().unwrap_or_default()
}

#[endpoint]
fn set_mapper_next(&self, item: u32) -> u32 {
let set_mapper = self.set_mapper();
set_mapper.next(&item).unwrap_or_default()
}

#[endpoint]
fn set_mapper_previous(&self, item: u32) -> u32 {
let set_mapper = self.set_mapper();
set_mapper.previous(&item).unwrap_or_default()
}

#[endpoint]
fn set_mapper_iter_from_and_count(&self, item: u32) -> u32 {
let set_mapper = self.set_mapper();
let mut count = 0;
for _element in set_mapper.iter_from(&item) {
count += 1;
}

count
}
}
11 changes: 8 additions & 3 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: 368
// Endpoints: 373
// Async Callback: 1
// Total number of exported functions: 370
// Total number of exported functions: 375

#![no_std]

Expand Down Expand Up @@ -310,6 +310,11 @@ multiversx_sc_wasm_adapter::endpoints! {
set_mapper_insert => set_mapper_insert
set_mapper_contains => set_mapper_contains
set_mapper_remove => set_mapper_remove
set_mapper_front => set_mapper_front
set_mapper_back => set_mapper_back
set_mapper_next => set_mapper_next
set_mapper_previous => set_mapper_previous
set_mapper_iter_from_and_count => set_mapper_iter_from_and_count
map_my_single_value_mapper => map_my_single_value_mapper
my_single_value_mapper_increment_1 => my_single_value_mapper_increment_1
my_single_value_mapper_increment_2 => my_single_value_mapper_increment_2
Expand Down Expand Up @@ -369,7 +374,7 @@ multiversx_sc_wasm_adapter::endpoints! {
no_overflow_u8 => no_overflow_u8
no_overflow_u16 => no_overflow_u16
no_overflow_u32 => no_overflow_u32
u64 => u64
no_overflow_u64 => no_overflow_u64
overflow_usize => overflow_usize
overflow_u8 => overflow_u8
overflow_u16 => overflow_u16
Expand Down
14 changes: 11 additions & 3 deletions framework/base/src/storage/mappers/queue_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
storage_set(self.build_name_key(INFO_IDENTIFIER).as_ref(), &value);
}

fn get_node(&self, node_id: u32) -> Node {
pub fn get_node(&self, node_id: u32) -> Node {
storage_get(
self.build_node_id_named_key(NODE_IDENTIFIER, node_id)
.as_ref(),
Expand All @@ -153,14 +153,14 @@ where
);
}

fn get_value(&self, node_id: u32) -> T {
pub fn get_value(&self, node_id: u32) -> T {
storage_get(
self.build_node_id_named_key(VALUE_IDENTIFIER, node_id)
.as_ref(),
)
}

fn get_value_option(&self, node_id: u32) -> Option<T> {
pub fn get_value_option(&self, node_id: u32) -> Option<T> {
if node_id == NULL_ENTRY {
return None;
}
Expand Down Expand Up @@ -333,6 +333,10 @@ where
Iter::new(self)
}

pub fn iter_from_node_id(&self, node_id: u32) -> Iter<SA, T> {
Iter::new_from_node_id(self, node_id)
}

/// Runs several checks in order to verify that both forwards and backwards iteration
/// yields the same node entries and that the number of items in the queue is correct.
/// Used for unit testing.
Expand Down Expand Up @@ -443,6 +447,10 @@ where
queue,
}
}

fn new_from_node_id(queue: &'a QueueMapper<SA, T>, node_id: u32) -> Iter<'a, SA, T> {
Iter { node_id, queue }
}
}

impl<'a, SA, T> Iterator for Iter<'a, SA, T>
Expand Down
35 changes: 35 additions & 0 deletions framework/base/src/storage/mappers/set_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ where
self.get_node_id(value) != NULL_ENTRY
}

pub fn next(&self, value: &T) -> Option<T> {
let node_id = self.get_node_id(value);
if node_id == NULL_ENTRY {
return None;
}

let next_node_id = self.queue_mapper.get_node(node_id).next;

self.queue_mapper.get_value_option(next_node_id)
}

pub fn previous(&self, value: &T) -> Option<T> {
let node_id = self.get_node_id(value);
if node_id == NULL_ENTRY {
return None;
}

let next_node_id = self.queue_mapper.get_node(node_id).previous;

self.queue_mapper.get_value_option(next_node_id)
}

pub fn front(&self) -> Option<T> {
self.queue_mapper.front()
}

pub fn back(&self) -> Option<T> {
self.queue_mapper.back()
}

/// Adds a value to the set.
///
/// If the set did not have this value present, `true` is returned.
Expand Down Expand Up @@ -144,6 +174,11 @@ where
self.queue_mapper.iter()
}

pub fn iter_from(&self, value: &T) -> Iter<SA, T> {
let node_id = self.get_node_id(value);
self.queue_mapper.iter_from_node_id(node_id)
}

/// Checks the internal consistency of the collection. Used for unit tests.
pub fn check_internal_consistency(&self) -> bool {
self.queue_mapper.check_internal_consistency()
Expand Down

0 comments on commit efab4d4

Please sign in to comment.