@@ -5,6 +5,7 @@ tags: ["multitest", "blocks"]
55import { Callout } from " nextra/components" ;
66
77[ BlockInfo ] : https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.BlockInfo.html
8+ [ AppBuilder ] : https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.AppBuilder.html
89[ block_info ] : https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html#method.block_info
910[ set_block ] : https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html#method.set_block
1011[ update_block] :
@@ -52,7 +53,7 @@ In **`MultiTest`**, the chain is initialized with a default block that has the f
5253
5354These properties constitute the [ ` BlockInfo{:rust} ` ] [ BlockInfo ] structure, which is defined in the
5455CosmWasm library for representing block metadata. [ ` BlockInfo{:rust} ` ] [ BlockInfo ] can be retrieved
55- at any point in a test by calling the [ ` block_info() {:rust} ` ] [ block_info ] function provided by
56+ at any point in a test by calling the [ ` block_info{:rust} ` ] [ block_info ] method provided by
5657` App{:rust} ` .
5758
5859The following example illustrates this case in detail.
@@ -77,21 +78,21 @@ assert_eq!("cosmos-testnet-14002", block.chain_id);
7778```
7879
7980In line 4, the chain is initialized and _ started_ with the default settings by calling the function
80- [ ` default() {:rust} ` ] [ app_default ] on ` App{:rust} ` . In line 7, the current block metadata (of type
81+ [ ` default{:rust} ` ] [ app_default ] on ` App{:rust} ` . In line 7, the current block metadata (of type
8182[ ` BlockInfo{:rust} ` ] [ BlockInfo ] ) is retrieved and assigned to variable ` block{:rust} ` . In the next
8283few lines, the default values are checked:
8384
8485- line 10: block ` height{:rust} ` is ` 12345{:rust} ` ,
8586- line 13: block ` time{:rust} ` is a Unix timestamp of value ` 1571797419879305533{:rust} ` , which is
86- the numeric representation of Wednesday, October 23, 2019 02:23:39 GMT,
87+ the numeric representation of ` Wednesday, October 23, 2019 02:23:39 GMT ` ,
8788- line 16: block ` chain_id{:rust} ` has a default value ` "cosmos-testnet-14002"{:rust} ` .
8889
8990## Initialization with the custom block
9091
9192Although the chain initialization with the default block may be suitable for most test cases, it is
9293possible to initialize the chain with a custom [ ` BlockInfo{:rust} ` ] [ BlockInfo ] using the
93- [ ` set_block() {:rust} ` ] [ set_block ] function provided by ` App{:rust} ` . The following example explains
94- this case in detail.
94+ [ ` set_block{:rust} ` ] [ set_block ] method provided by ` App{:rust} ` . The following example explains this
95+ case in detail.
9596
9697``` rust showLineNumbers {8-12} /set_block/ /BlockInfo/
9798use cosmwasm_std :: {BlockInfo , Timestamp };
@@ -121,25 +122,30 @@ assert_eq!("starship-testnet", block.chain_id);
121122```
122123
123124The chain is started with the default settings in line 5. Then in line 8, the
124- [ ` set_block() {:rust} ` ] [ set_block ] function is called with custom block properties provided as
125+ [ ` set_block{:rust} ` ] [ set_block ] method is called with custom block properties provided as
125126[ ` BlockInfo{:rust} ` ] [ BlockInfo ] structure. The current block metadata is retrieved in line 12 and in
126127the next few lines the detailed values are checked:
127128
128129- line 18: block ` height{:rust} ` is now ` 1{:rust} ` ,
129130- line 21: new block ` time{:rust} ` is a Unix timestamp of value ` 1723627489{:rust} ` , which is the
130- numeric representation (in seconds) of Wednesday, August 14, 2024 09:24:49 GMT,
131+ numeric representation (in seconds) of ` Wednesday, August 14, 2024 09:24:49 GMT ` ,
131132- line 24: block ` chain_id{:rust} ` has now a value ` "starship-testnet"{:rust} ` .
132133
133134This way, you can initialize the current block in tests to best suit your requirements.
134135
136+ <Callout type = " info" emoji = " ☉" >
137+ You can also initialize the blockchain simulator with the custom block using [ AppBuilder] , as
138+ shown in this [ example] ( app-builder#with_block ) .
139+ </Callout >
140+
135141## Generating next block with the default step
136142
137- To generate a new block in tests, you can use the [ ` update_block() {:rust} ` ] [ update_block ] function
138- provided by ` App{:rust} ` . In the following example, [ ` update_block() {:rust} ` ] [ update_block ] function
139- takes a function [ ` next_block() {:rust} ` ] [ next_block ] as an argument.
140- [ ` next_block(){:rust} ` ] [ next_block ] function - provided by ** ` MultiTest ` ** - increases the block
141- height by ` 1 ` and advances the block time by ` 5 ` seconds, simulating the generation of a new block
142- every ` 5 ` seconds in a real-life blockchain. The ` chain_id{:rust} ` remains unchanged.
143+ To generate a new block in tests, you can use the [ ` update_block{:rust} ` ] [ update_block ] method
144+ provided by ` App{:rust} ` . In the following example, [ ` update_block{:rust} ` ] [ update_block ] method
145+ takes a function [ ` next_block{:rust} ` ] [ next_block ] as an argument. [ ` next_block{:rust} ` ] [ next_block ]
146+ function - provided by ** ` MultiTest ` ** - increases the block height by ` 1 ` and advances the block
147+ time by ` 5 ` seconds, simulating the generation of a new block every ` 5 ` seconds in a real-life
148+ blockchain. The ` chain_id{:rust} ` remains unchanged.
143149
144150``` rust showLineNumbers {7} /update_block/ /next_block/
145151use cw_multi_test :: {next_block, App };
@@ -165,8 +171,8 @@ assert_eq!(1571797424879305533, block.time.nanos());
165171assert_eq! (" cosmos-testnet-14002" , block . chain_id);
166172```
167173
168- In line 7 the [ ` update_block() {:rust} ` ] [ update_block ] function is called and in the next few lines
169- the current block properties are checked. In line 10 the block metadata is retrieved and assigned to
174+ In line 7 the [ ` update_block{:rust} ` ] [ update_block ] method is called and in the next few lines the
175+ current block properties are checked. In line 10 the block metadata is retrieved and assigned to
170176variable ` block{:rust} ` . In line 13, we check if the block height has changed. The default block
171177height is ` 12345{:rust} ` , and we check it against the value ` 12346{:rust} ` . As this assertion
172178passes, you can see that the block height was indeed incremented by ` 1 ` . Similarly, the block's time
@@ -178,7 +184,7 @@ is checked against the Unix timestamp, which is 5 seconds later than the default
178184Several events on the blockchain occur after a period longer than a few seconds. In such cases,
179185incrementing the block time multiple times until the desired time is reached would be impractical.
180186In ** ` MultiTest ` ** , it is possible to advance the block using a specific block height and time. To
181- achieve this, pass a custom closure to the [ ` update_block() {:rust} ` ] [ update_block ] function . This
187+ achieve this, pass a custom closure to the [ ` update_block{:rust} ` ] [ update_block ] method . This
182188closure takes a mutable [ ` BlockInfo{:rust} ` ] [ BlockInfo ] structure as an argument and modifies its
183189properties, as shown in the example below.
184190
@@ -210,7 +216,7 @@ assert_eq!("cosmos-testnet-14002", block.chain_id);
210216```
211217
212218In line 4, the default block is initialized during the chain start. Then in line 7, the
213- [ ` update_block() {:rust} ` ] [ update_block ] function takes a custom closure as an argument. This closure
219+ [ ` update_block{:rust} ` ] [ update_block ] method takes a custom closure as an argument. This closure
214220increments the block height by ** 10000** and advances the block time by ** 6 days** . Similarly like
215221in prevous examples, the new block metadata is retrieved and the current properties are checked. The
216222new block height should be ` 12345{:rust} ` + ` 10000{:rust} ` = ` 22345{:rust} ` . The new block time
0 commit comments