From 1b0c60398d19de89f47dcb0bb15cf1eb99882751 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Sat, 16 Dec 2023 14:37:54 +0800 Subject: [PATCH] Add functionality for iterator and parallel access for loadtesting (#70) --- README.md | 26 ++++++++---- contracts/sei-tester/src/contract.rs | 61 +++++++++++++++++++++++++++- contracts/sei-tester/src/lib.rs | 1 + contracts/sei-tester/src/msg.rs | 10 +++++ contracts/sei-tester/src/state.rs | 15 +++++++ 5 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 contracts/sei-tester/src/state.rs diff --git a/README.md b/README.md index fe94599..7cb69de 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This repository contains the sei-cosmwasm package to support smart contract quer ## Build Sei Tester Contract -``` +```shell docker run --rm -v "$(pwd)":/code \ --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \ --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ @@ -15,24 +15,36 @@ docker run --rm -v "$(pwd)":/code \ ### Store Contract Code -`seid tx wasm store artifacts/sei_tester.wasm -y --from --chain-id -b block --gas=3000000 --fees=1000sei` +```shell +seid tx wasm store artifacts/sei_tester.wasm -y --from --chain-id -b block --gas=3000000 --fees=1000sei +``` Make sure to note the code ID for the contract from the tx response. You can also find it in the list of uploaded code with this query: -`seid q wasm list-code` +```shell +seid q wasm list-code +``` ### Instantiate Contract -`seid tx wasm instantiate '{}' -y --no-admin --from --chain-id --gas=1500000 --fees=1000sei -b block --label sei-tester` +```shell +seid tx wasm instantiate '{}' -y --no-admin --from --chain-id --gas=1500000 --fees=1000sei -b block --label sei-tester +``` Make sure to note the contract address for the contract from the tx response. You can also find it with this query: -`seid q wasm list-contract-by-code ` +```shell +seid q wasm list-contract-by-code +``` ### Query Smart Contract -`seid q wasm contract-state smart ` +```shell +seid q wasm contract-state smart +``` The json literal may look something like this: -`'{"exchange_rates": {}}'` +```json +'{"exchange_rates": {}}' +``` diff --git a/contracts/sei-tester/src/contract.rs b/contracts/sei-tester/src/contract.rs index 8216f76..b3a5986 100644 --- a/contracts/sei-tester/src/contract.rs +++ b/contracts/sei-tester/src/contract.rs @@ -1,12 +1,14 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::{ coin, entry_point, to_binary, BankMsg, Binary, Coin, Decimal, Deps, DepsMut, Env, MessageInfo, - Reply, Response, StdError, StdResult, SubMsg, SubMsgResponse, Uint128, + Reply, Response, StdError, StdResult, SubMsg, SubMsgResponse, Uint128, Order as IteratorOrder }; +use cw_storage_plus::Bound; use crate::{ msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, types::{OrderData, PositionEffect}, + state::{VALUES, USER_SUMS, PARALLEL_VALS}, }; use protobuf::Message; use sei_cosmwasm::{ @@ -63,9 +65,66 @@ pub fn execute( ExecuteMsg::Burn {} => burn(deps, env, info), ExecuteMsg::ChangeAdmin {} => change_admin(deps, env, info), ExecuteMsg::SetMetadata {} => set_metadata(deps, env, info), + ExecuteMsg::TestOccIteratorWrite { values } => test_occ_iterator_write(deps, env, info, values), + ExecuteMsg::TestOccIteratorRange { start, end } => test_occ_iterator_range(deps, env, info, start, end), + ExecuteMsg::TestOccParallelism { value } => test_occ_parallelism(deps, env, info, value), } } +fn test_occ_iterator_write( + deps: DepsMut, + _env: Env, + info: MessageInfo, + values: Vec<(u64, u64)>, +) -> Result, StdError> { + // writes all of the values (index, value) to the store + for (key, value) in values { + VALUES.save(deps.storage, key, &value)?; + } + Ok(Response::new()) +} + +fn test_occ_iterator_range( + deps: DepsMut, + _env: Env, + info: MessageInfo, + start: u64, + end: u64, +) -> Result, StdError> { + // iterates through the `VALUES` and for all that exist, sums them and writes them to user_sums for the sender + let mut sum: u64 = 0; + let values: Vec<(u64, u64)> = VALUES.range( + deps.storage, + Some(Bound::inclusive(start)), + Some(Bound::inclusive(end)), + IteratorOrder::Ascending + ).collect::, StdError>>().unwrap(); + + for (_, val) in values { + sum += val; + } + USER_SUMS.save(deps.storage, info.sender.clone(), &sum)?; + + Ok(Response::new() + .add_attribute("user", info.sender.to_string()) + .add_attribute("sum", sum.to_string()) + ) +} + +fn test_occ_parallelism( + deps: DepsMut, + _env: Env, + info: MessageInfo, + value: u64, +) -> Result, StdError> { + // writes the value to the store for the sender + PARALLEL_VALS.save(deps.storage, info.sender.clone(), &value)?; + Ok(Response::new() + .add_attribute("user", info.sender.to_string()) + .add_attribute("val", value.to_string()) + ) +} + pub fn place_orders( deps: DepsMut, _env: Env, diff --git a/contracts/sei-tester/src/lib.rs b/contracts/sei-tester/src/lib.rs index 2228886..261db7a 100644 --- a/contracts/sei-tester/src/lib.rs +++ b/contracts/sei-tester/src/lib.rs @@ -1,3 +1,4 @@ pub mod contract; pub mod msg; pub mod types; +pub mod state; diff --git a/contracts/sei-tester/src/msg.rs b/contracts/sei-tester/src/msg.rs index c8ad745..4b88e53 100644 --- a/contracts/sei-tester/src/msg.rs +++ b/contracts/sei-tester/src/msg.rs @@ -15,6 +15,16 @@ pub enum ExecuteMsg { Burn {}, ChangeAdmin {}, SetMetadata {}, + TestOccIteratorWrite { + values: Vec<(u64,u64)> + }, + TestOccIteratorRange { + start: u64, + end: u64, + }, + TestOccParallelism { + value: u64, + } } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] diff --git a/contracts/sei-tester/src/state.rs b/contracts/sei-tester/src/state.rs new file mode 100644 index 0000000..b47a4cd --- /dev/null +++ b/contracts/sei-tester/src/state.rs @@ -0,0 +1,15 @@ +use cosmwasm_std::{Addr, Decimal, Uint128, Uint64}; +use cw_storage_plus::{Item, Map}; +use schemars::JsonSchema; +use sei_cosmwasm::ExchangeRatesResponse; +use serde::{Deserialize, Serialize}; + + +pub const VALUES: Map = + Map::new("values"); + +pub const USER_SUMS: Map = + Map::new("user_sums"); + +pub const PARALLEL_VALS: Map = + Map::new("parallel_vals"); \ No newline at end of file