Skip to content

Commit

Permalink
Added with_block description (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta authored Dec 10, 2024
2 parents b947409 + e803482 commit 9d527ed
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/pages/cw-multi-test/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"installation": "Installation",
"features": "Features",
"getting-started": "Getting started"
"getting-started": "Getting started",
"app-builder": "AppBuilder",
"blocks": "Blocks"
}
130 changes: 130 additions & 0 deletions src/pages/cw-multi-test/app-builder.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
tags: ["multitest", "AppBuilder"]
---

[AppBuilder]: https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.AppBuilder.html
[Builder Pattern]: https://en.wikipedia.org/wiki/Builder_pattern
[BlockInfo]: https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.BlockInfo.html
[with_block]:
https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.AppBuilder.html#method.with_block
[build]: #build

# `AppBuilder`

[`AppBuilder{:rust}`][AppBuilder] is an implementation of the [Builder Pattern] that provides a flexible
and modular way to construct the [`App{:rust}`](app) blockchain simulator. It allows smart contract developers
to configure various components of the blockchain simulator (e.g., Bank, Staking, Gov, IBC) individually
through dedicated `with_*` methods. Each method modifies and returns a new instance of the builder, enabling
method chaining for a fluent interface. The [`build{:rust}`][build] method finalizes the construction
process, ensuring that all components are correctly initialized and integrated.

The following sections detail all builder functions, providing specific usage examples.

## `default`

(WIP)

## `new`

(WIP)

## `new_custom`

(WIP)

## `with_api`

(WIP)

## `with_bank`

(WIP)

## `with_block`

While the default block configuration is sufficient for most test cases, you can initialize the
chain with a custom [`BlockInfo{:rust}`][BlockInfo] using the [`with_block{:rust}`][with_block]
method provided by [`AppBuilder{:rust}`][AppBuilder].

The following example demonstrates this use case in detail.

```rust showLineNumbers copy {15} /with_block/
use cosmwasm_std::{BlockInfo, Timestamp};
use cw_multi_test::{no_init, AppBuilder};

// create the chain builder
let builder = AppBuilder::default();

// prepare the custom block
let block = BlockInfo {
height: 1,
time: Timestamp::from_seconds(1723627489),
chain_id: "starship-testnet".to_string(),
};

// build the chain initialized with the custom block
let app = builder.with_block(block).build(no_init);

// get the current block properties
let block = app.block_info();

// now the block height is 21
assert_eq!(1, block.height);

// now the block timestamp is Wed Aug 14 2024 09:24:49 GMT+0000
assert_eq!(1723627489, block.time.seconds());

// now the chain identifier is "starship-testnet"
assert_eq!("starship-testnet", block.chain_id);
```

The [`AppBuilder{:rust}`][AppBuilder] is initialized with default settings in line 5. A custom block
is created in lines 8-12 and passed to the [`with_block{:rust}`][with_block] method in line 15.
Since this is the only customization in this example, the blockchain construction is finalized in
the same line by calling the [`build{:rust}`][build] method.

The current block metadata is retrieved in line 18, followed by value checks:

- line 21: the block height is now `1{:rust}`,
- line 24: the block time is set to the Unix timestamp `1723627489{:rust}`, representing
`Wednesday, August 14, 2024, 09:24:49 GMT`,
- line 27: the chain identifier is now `"starship-testnet"{:rust}`.

The [`with_block{:rust}`][with_block] method of [`AppBuilder{:rust}`][AppBuilder] can be combined
with any other `with_*` methods to configure a custom starting block.

## `with_custom`

(WIP)

## `with_distribution`

(WIP)

## `with_gov`

(WIP)

## `with_ibc`

(WIP)

## `with_staking`

(WIP)

## `with_stargate`

(WIP)

## `with_storage`

(WIP)

## `with_wasm`

(WIP)

## `build`

(WIP)
26 changes: 13 additions & 13 deletions src/pages/cw-multi-test/features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ you can provide your own, using `AppBuilder`'s function listed in **AppBuilder&n
column. Names of **`MultiTest`** feature flags required to enable specific functionality are shown
in the column **Feature flag**.

| Feature | Default<br/>implementation | Feature<br/>flag | AppBuilder<br/>constructor | Functionality |
| ---------------- | ---------------------------------------------------------------------------------------------------------------- | :--------------: | -------------------------- | -------------------------------------------------- |
| [Blocks](blocks) | [`mock_env().block{:rust}`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/testing/fn.mock_env.html) | | `with_block` | Operations on blocks. |
| API | [`MockApi{:rust}`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/testing/struct.MockApi.html) | | `with_api` | Access to CosmWasm API. |
| Storage | [`MockStorage{:rust}`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/testing/type.MockStorage.html) | | `with_storage` | Access to storage. |
| Bank | [`BankKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.BankKeeper.html) | | `with_bank` | Interactions with **Bank** module. |
| Staking | [`StakeKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.StakeKeeper.html) | `staking` | `with_staking` | Interactions with **Staking** module. |
| Distribution | [`DistributionKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.DistributionKeeper.html) | `staking` | `with_distribution` | Interactions with **Distribution** module. |
| Governance | [`GovFailingModule{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/type.GovFailingModule.html) | | `with_gov` | Interactions with **Governance** module. |
| Stargate | [`StargateFailing{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.StargateFailing.html) | `stargate` | `with_stargate` | Operations using `Stargate` and/or `Any` messages. |
| Wasm | [`WasmKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.WasmKeeper.html) | | `with_wasm` | Interactions with **Wasm** module. |
| Custom | [`FailingModule{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.FailingModule.html) | | `new_custom` | Operations using custom module. |
| IBC | [`IbcFailingModule{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/type.IbcFailingModule.html) | `stargate` | `with_ibc` | Inter-blockchain communication operations. |
| Feature | Default<br/>implementation | Feature<br/>flag | AppBuilder<br/>constructor | Functionality |
| ---------------- | ---------------------------------------------------------------------------------------------------------------- | :--------------: | ---------------------------------------------------- | -------------------------------------------------- |
| [Blocks](blocks) | [`mock_env().block{:rust}`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/testing/fn.mock_env.html) | | [`with_block`](app-builder#with_block) | Operations on blocks. |
| API | [`MockApi{:rust}`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/testing/struct.MockApi.html) | | [`with_api`](app-builder#with_api) | Access to CosmWasm API. |
| Storage | [`MockStorage{:rust}`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/testing/type.MockStorage.html) | | [`with_storage`](app-builder#with_storage) | Access to storage. |
| Bank | [`BankKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.BankKeeper.html) | | [`with_bank`](app-builder#with_bank) | Interactions with **Bank** module. |
| Staking | [`StakeKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.StakeKeeper.html) | `staking` | [`with_staking`](app-builder#with_staking) | Interactions with **Staking** module. |
| Distribution | [`DistributionKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.DistributionKeeper.html) | `staking` | [`with_distribution`](app-builder#with_distribution) | Interactions with **Distribution** module. |
| Governance | [`GovFailingModule{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/type.GovFailingModule.html) | | [`with_gov`](app-builder#with_gov) | Interactions with **Governance** module. |
| Stargate | [`StargateFailing{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.StargateFailing.html) | `stargate` | [`with_stargate`](app-builder#with_stargate) | Operations using `Stargate` and/or `Any` messages. |
| Wasm | [`WasmKeeper{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.WasmKeeper.html) | | [`with_wasm`](app-builder#with_wasm) | Interactions with **Wasm** module. |
| Custom | [`FailingModule{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.FailingModule.html) | | [`new_custom`](app-builder#new_custom) | Operations using custom module. |
| IBC | [`IbcFailingModule{:rust}`](https://docs.rs/cw-multi-test/latest/cw_multi_test/type.IbcFailingModule.html) | `stargate` | [`with_ibc`](app-builder#with_ibc) | Inter-blockchain communication operations. |

## Feature flags summary

Expand Down

0 comments on commit 9d527ed

Please sign in to comment.