Skip to content

Commit

Permalink
Add functionality for iterator and parallel access for loadtesting (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
udpatil authored Dec 16, 2023
1 parent 09d523a commit 1b0c603
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -15,24 +15,36 @@ docker run --rm -v "$(pwd)":/code \

### Store Contract Code

`seid tx wasm store artifacts/sei_tester.wasm -y --from <account> --chain-id <name> -b block --gas=3000000 --fees=1000sei`
```shell
seid tx wasm store artifacts/sei_tester.wasm -y --from <account> --chain-id <name> -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 <code-id> '{}' -y --no-admin --from <account> --chain-id <name> --gas=1500000 --fees=1000sei -b block --label sei-tester`
```shell
seid tx wasm instantiate <code-id> '{}' -y --no-admin --from <account> --chain-id <name> --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 <code-id>`
```shell
seid q wasm list-contract-by-code <code-id>
```

### Query Smart Contract

`seid q wasm contract-state smart <contract-address> <json-query-literal>`
```shell
seid q wasm contract-state smart <contract-address> <json-query-literal>
```

The json literal may look something like this:

`'{"exchange_rates": {}}'`
```json
'{"exchange_rates": {}}'
```
61 changes: 60 additions & 1 deletion contracts/sei-tester/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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<SeiQueryWrapper>,
_env: Env,
info: MessageInfo,
values: Vec<(u64, u64)>,
) -> Result<Response<SeiMsg>, 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<SeiQueryWrapper>,
_env: Env,
info: MessageInfo,
start: u64,
end: u64,
) -> Result<Response<SeiMsg>, 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::<Result<Vec<(u64, u64)>, 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<SeiQueryWrapper>,
_env: Env,
info: MessageInfo,
value: u64,
) -> Result<Response<SeiMsg>, 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<SeiQueryWrapper>,
_env: Env,
Expand Down
1 change: 1 addition & 0 deletions contracts/sei-tester/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod contract;
pub mod msg;
pub mod types;
pub mod state;
10 changes: 10 additions & 0 deletions contracts/sei-tester/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 15 additions & 0 deletions contracts/sei-tester/src/state.rs
Original file line number Diff line number Diff line change
@@ -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<u64, u64> =
Map::new("values");

pub const USER_SUMS: Map<Addr, u64> =
Map::new("user_sums");

pub const PARALLEL_VALS: Map<Addr, u64> =
Map::new("parallel_vals");

0 comments on commit 1b0c603

Please sign in to comment.