Skip to content

Commit 847b459

Browse files
committed
Add EnvsOptions to make block_time, initial_height, initial_time, chain_id
configurable
1 parent 3ed54a4 commit 847b459

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

packages/std/src/testing/mock.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ pub fn mock_env() -> Env {
443443
/// }
444444
/// ```
445445
pub struct Envs {
446+
chain_id: String,
446447
contract_address: Addr,
447448
/// The number of nanoseconds between two consecutive blocks
448449
block_time: u64,
@@ -451,17 +452,45 @@ pub struct Envs {
451452
envs_produced: u64,
452453
}
453454

455+
pub struct EnvsOptions {
456+
bech32_prefix: &'static str, /* static due to MockApi's Copy requirement. No better idea for now. */
457+
block_time: u64,
458+
// The hight before the first `make` call
459+
initial_height: u64,
460+
// The block time before the first `make` call
461+
initial_time: Timestamp,
462+
chain_id: String,
463+
}
464+
465+
impl Default for EnvsOptions {
466+
fn default() -> Self {
467+
EnvsOptions {
468+
bech32_prefix: BECH32_PREFIX,
469+
block_time: 5_000_000_000, // 5s
470+
initial_height: 12_344,
471+
initial_time: Timestamp::from_nanos(1_571_797_419_879_305_533).minus_seconds(5),
472+
chain_id: "cosmos-testnet-14002".to_string(),
473+
}
474+
}
475+
}
476+
454477
impl Envs {
455-
pub fn new(
456-
bech32_prefix: &'static str, /* static due to MockApi's Copy requirement. No better idea for now. */
457-
) -> Self {
458-
let api = MockApi::default().with_prefix(bech32_prefix);
478+
pub fn new(bech32_prefix: &'static str) -> Self {
479+
Self::with_options(EnvsOptions {
480+
bech32_prefix,
481+
..Default::default()
482+
})
483+
}
484+
485+
pub fn with_options(options: EnvsOptions) -> Self {
486+
let api = MockApi::default().with_prefix(options.bech32_prefix);
459487
Envs {
488+
chain_id: options.chain_id,
460489
// Default values here for compatibility with old `mock_env` function. They could be changed to anything else if there is a good reason.
461490
contract_address: api.addr_make("cosmos2contract"),
462-
block_time: 5_000_000_000, // 5s
463-
last_height: 12_344,
464-
last_time: Timestamp::from_nanos(1_571_797_419_879_305_533).minus_seconds(5),
491+
block_time: options.block_time,
492+
last_height: options.initial_height,
493+
last_time: options.initial_time,
465494
envs_produced: 0,
466495
}
467496
}
@@ -482,7 +511,7 @@ impl Envs {
482511
block: BlockInfo {
483512
height,
484513
time,
485-
chain_id: "cosmos-testnet-14002".to_string(),
514+
chain_id: self.chain_id.clone(),
486515
},
487516
transaction: Some(TransactionInfo { index: 3 }),
488517
contract: ContractInfo {
@@ -492,6 +521,12 @@ impl Envs {
492521
}
493522
}
494523

524+
impl Default for Envs {
525+
fn default() -> Self {
526+
Envs::with_options(EnvsOptions::default())
527+
}
528+
}
529+
495530
// The iterator implementation ends in case of overflows to avoid panics.
496531
// Using this is recommended for very long running test suites.
497532
impl Iterator for Envs {

packages/std/src/testing/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub use mock::DistributionQuerier;
2121
pub use mock::StakingQuerier;
2222
pub use mock::{
2323
mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances, mock_env,
24-
mock_wasmd_attr, BankQuerier, Envs, MockApi, MockQuerier, MockQuerierCustomHandlerResult,
25-
MOCK_CONTRACT_ADDR,
24+
mock_wasmd_attr, BankQuerier, Envs, EnvsOptions, MockApi, MockQuerier,
25+
MockQuerierCustomHandlerResult, MOCK_CONTRACT_ADDR,
2626
};
2727
#[cfg(feature = "stargate")]
2828
pub use mock::{

0 commit comments

Comments
 (0)