Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/y-pakorn/cw-storage-gas-meter
A simple CosmWasm storage gas meter for estimating gas usage from kv store.
https://github.com/y-pakorn/cw-storage-gas-meter
cosmos cosmwasm rust
Last synced: about 1 month ago
JSON representation
A simple CosmWasm storage gas meter for estimating gas usage from kv store.
- Host: GitHub
- URL: https://github.com/y-pakorn/cw-storage-gas-meter
- Owner: y-pakorn
- License: gpl-3.0
- Created: 2022-07-28T08:28:35.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-07-28T17:24:34.000Z (over 2 years ago)
- Last Synced: 2024-09-14T01:32:33.453Z (2 months ago)
- Topics: cosmos, cosmwasm, rust
- Language: Rust
- Homepage:
- Size: 36.1 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Storage Gas Meter
A simple CosmWasm storage gas meter for estimating gas usage from kv store.
## Usage
### Unit Test
Use `MemoryStorageWithGas` instead of `MemoryStorage` or `MockStorage`.
```rust
// let mut storage = MockStorage::new();
let mut storage = MemoryStorageWithGas::new();
let map = Map::>::new("0");let data = b"hello";
map.save(&mut storage, 0, &data.to_vec())?;let gas = storage.last_gas_used();
assert_eq!(gas, 2960);
```### Multi Test
Instantiate `cw_multi_test::App` with `MemoryStorageWithGas` instead of `MemoryStorage` or `MockStorage`.
Due to the nature of `cosmwasm_std::Storage` trait, we cannot downcast the `dyn Storage` back to `MemoryStorage` directly.
So we pass the pointer to the storage as trait object instead and access the gas log through that pointer.
```rust
let storage = MemoryStorageWithGas::new();AppBuilder::new()
.with_storage(&storage) // <- ref ptr here
.build(|r, _, storage| {
r.bank
.init_balance(
storage,
&Addr::unchecked("admin"),
vec![Coin::new(100, "uluna")],
)
.unwrap();
});let gas = storage.last_gas_used();
assert_eq!(gas, 3650);
```