ibc-test-framework 0.32.2

Framework for writing integration tests for IBC relayers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*!
    Helper functions for modifying the Gaia chain config in TOML.

    Since we do not need to understand the full structure of the
    CosmosSDK config, we are updating the config as dynamic TOML
    values instead of serializing them into proper types.
*/

use core::time::Duration;
use eyre::{eyre, Report as Error};
use toml::Value;
use tracing::debug;

/// Set the `rpc` field in the full node config.
pub fn set_rpc_port(config: &mut Value, port: u16) -> Result<(), Error> {
    config
        .get_mut("rpc")
        .ok_or_else(|| eyre!("expect rpc section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("laddr".to_string(), format!("tcp://0.0.0.0:{port}").into());

    Ok(())
}

pub fn enable_grpc(config: &mut Value) -> Result<(), Error> {
    config
        .get_mut("grpc")
        .ok_or_else(|| eyre!("expect grpc section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("enable".to_string(), true.into());

    Ok(())
}

pub fn set_grpc_port(config: &mut Value, port: u16) -> Result<(), Error> {
    config
        .get_mut("grpc")
        .ok_or_else(|| eyre!("expect grpc section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("address".to_string(), format!("0.0.0.0:{port}").into());

    Ok(())
}

pub fn set_grpc_port_ipv6(config: &mut Value, port: u16) -> Result<(), Error> {
    config
        .get_mut("grpc")
        .ok_or_else(|| eyre!("expect grpc section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("address".to_string(), format!("[::]:{port}").into());

    Ok(())
}

pub fn disable_grpc_web(config: &mut Value) -> Result<(), Error> {
    if let Some(field) = config.get_mut("grpc-web") {
        field
            .as_table_mut()
            .ok_or_else(|| eyre!("expect object"))?
            .insert("enable".to_string(), false.into());
    }

    Ok(())
}

pub fn disable_api(config: &mut Value) -> Result<(), Error> {
    if let Some(field) = config.get_mut("api") {
        field
            .as_table_mut()
            .ok_or_else(|| eyre!("expect object"))?
            .insert("enable".to_string(), false.into());
    }

    Ok(())
}

/// Set the `p2p` field in the full node config.
pub fn set_p2p_port(config: &mut Value, port: u16) -> Result<(), Error> {
    config
        .get_mut("p2p")
        .ok_or_else(|| eyre!("expect p2p section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("laddr".to_string(), format!("tcp://0.0.0.0:{port}").into());

    Ok(())
}

/// Set the `pprof_laddr` field in the full node config.
pub fn set_pprof_port(config: &mut Value, port: u16) -> Result<(), Error> {
    config
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert(
            "pprof_laddr".to_string(),
            format!("tcp://0.0.0.0:{port}").into(),
        );

    Ok(())
}

/// Set the `pprof_laddr` field in the full node config.
pub fn set_block_sync(config: &mut Value, value: bool) -> Result<(), Error> {
    config
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("block_sync".to_string(), value.into());

    Ok(())
}

pub fn set_mempool_version(config: &mut Value, version: &str) -> Result<(), Error> {
    config
        .get_mut("mempool")
        .ok_or_else(|| eyre!("expect mempool section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("version".to_string(), version.into());

    Ok(())
}

/// Set the `consensus.timeout_commit` field in the full node config.
pub fn set_timeout_commit(config: &mut Value, duration: Duration) -> Result<(), Error> {
    config
        .get_mut("consensus")
        .ok_or_else(|| eyre!("expect consensus section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert(
            "timeout_commit".to_string(),
            format!("{}ms", duration.as_millis()).into(),
        );

    Ok(())
}

/// Set the `consensus.timeout_propose` field in the full node config.
pub fn set_timeout_propose(config: &mut Value, duration: Duration) -> Result<(), Error> {
    config
        .get_mut("consensus")
        .ok_or_else(|| eyre!("expect consensus section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert(
            "timeout_propose".to_string(),
            format!("{}ms", duration.as_millis()).into(),
        );

    Ok(())
}

/// Set the `log_level` field in the full node config.
pub fn set_log_level(config: &mut Value, log_level: &str) -> Result<(), Error> {
    config
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("log_level".to_string(), log_level.into());

    Ok(())
}

pub fn set_minimum_gas_price(config: &mut Value, price: &str) -> Result<(), Error> {
    config
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("minimum-gas-prices".to_string(), price.into());

    Ok(())
}

pub fn set_mode(config: &mut Value, mode: &str) -> Result<(), Error> {
    config
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("mode".to_string(), mode.into());

    Ok(())
}

pub fn set_indexer(config: &mut Value, mode: &str) -> Result<(), Error> {
    config
        .get_mut("tx_index")
        .ok_or_else(|| eyre!("expect tx_index section"))?
        .as_table_mut()
        .ok_or_else(|| eyre!("expect object"))?
        .insert("indexer".to_string(), mode.into());

    Ok(())
}

pub fn set_max_deposit_period(genesis: &mut serde_json::Value, period: &str) -> Result<(), Error> {
    let max_deposit_period = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("gov"))
        .and_then(|gov| get_mut_with_fallback(gov, "params", "deposit_params"))
        .and_then(|deposit_params| deposit_params.as_object_mut())
        .ok_or_else(|| eyre!("failed to update max_deposit_period in genesis file"))?;

    max_deposit_period
        .insert(
            "max_deposit_period".to_owned(),
            serde_json::Value::String(period.to_string()),
        )
        .ok_or_else(|| eyre!("failed to update max_deposit_period in genesis file"))?;

    Ok(())
}

pub fn add_allow_message_interchainaccounts(
    genesis: &mut serde_json::Value,
    message: &str,
) -> Result<(), Error> {
    let allow_messages = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("interchainaccounts"))
        .and_then(|ica| ica.get_mut("host_genesis_state"))
        .and_then(|state| state.get_mut("params"))
        .and_then(|params| params.get_mut("allow_messages"))
        .and_then(|allow_messages| allow_messages.as_array_mut())
        .ok_or_else(|| {
            eyre!("failed to retrieve allow_messages as a vector, in the genesis file")
        })?;

    // Only add `MsgSend` if the wildcard '*' is not specified
    if allow_messages.iter().all(|v| v.as_str() != Some("*")) {
        allow_messages.push(serde_json::Value::String(message.to_string()));
    }

    Ok(())
}

pub fn add_allow_message_interchainquery(
    genesis: &mut serde_json::Value,
    message: &str,
) -> Result<(), Error> {
    let allow_messages = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("interchainquery"))
        .and_then(|ica| ica.get_mut("params"))
        .and_then(|params| params.get_mut("allow_queries"))
        .and_then(|allow_messages| allow_messages.as_array_mut())
        .ok_or_else(|| {
            eyre!("failed to retrieve allow_messages as a vector, in the genesis file")
        })?;

    // Only add `MsgSend` if the wildcard '*' is not specified
    if allow_messages.iter().all(|v| v.as_str() != Some("*")) {
        allow_messages.push(serde_json::Value::String(message.to_string()));
    }

    Ok(())
}

pub fn set_min_deposit_amount(
    genesis: &mut serde_json::Value,
    min_deposit_amount: u64,
) -> Result<(), Error> {
    let min_deposit = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("gov"))
        .and_then(|gov| get_mut_with_fallback(gov, "params", "deposit_params"))
        .and_then(|deposit_params| deposit_params.get_mut("min_deposit"))
        .and_then(|min_deposit| min_deposit.as_array_mut())
        .ok_or_else(|| eyre!("failed to find min_deposit in genesis file"))?
        .get_mut(0)
        .and_then(|min_deposit_entry| min_deposit_entry.as_object_mut())
        .ok_or_else(|| eyre!("failed to find first entry of min_deposit in genesis file"))?;

    min_deposit
        .insert(
            "amount".to_owned(),
            serde_json::Value::String(min_deposit_amount.to_string()),
        )
        .ok_or_else(|| eyre!("failed to update deposit_params amount in genesis file"))?;

    Ok(())
}

pub fn set_staking_bond_denom(genesis: &mut serde_json::Value, denom: &str) -> Result<(), Error> {
    let bond_denom = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("staking"))
        .and_then(|staking| staking.get_mut("params"))
        .and_then(|params| params.as_object_mut())
        .ok_or_else(|| eyre!("failed to update bond_denom in genesis file"))?;

    bond_denom
        .insert(
            "bond_denom".to_owned(),
            serde_json::Value::String(denom.to_string()),
        )
        .ok_or_else(|| eyre!("failed to update bond_denom in genesis file"))?;

    Ok(())
}

pub fn set_staking_max_entries(
    genesis: &mut serde_json::Value,
    entries: &str,
) -> Result<(), Error> {
    let max_entries = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("staking"))
        .and_then(|staking| staking.get_mut("params"))
        .and_then(|params| params.as_object_mut())
        .ok_or_else(|| eyre!("failed to update max_entries in genesis file"))?;

    max_entries
        .insert(
            "max_entries".to_owned(),
            serde_json::Value::String(entries.to_string()),
        )
        .ok_or_else(|| eyre!("failed to update max_entries in genesis file"))?;

    Ok(())
}

pub fn set_mint_mint_denom(genesis: &mut serde_json::Value, denom: &str) -> Result<(), Error> {
    let mint_denom = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("mint"))
        .and_then(|mint| mint.get_mut("params"))
        .and_then(|params| params.as_object_mut())
        .ok_or_else(|| eyre!("failed to update mint_denom in genesis file"))?;

    mint_denom
        .insert(
            "mint_denom".to_owned(),
            serde_json::Value::String(denom.to_string()),
        )
        .ok_or_else(|| eyre!("failed to update mint_denom in genesis file"))?;

    Ok(())
}

pub fn set_crisis_denom(genesis: &mut serde_json::Value, crisis_denom: &str) -> Result<(), Error> {
    let denom = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("crisis"))
        .and_then(|crisis| crisis.get_mut("constant_fee"))
        .and_then(|constant_fee| constant_fee.as_object_mut())
        .ok_or_else(|| eyre!("failed to update denom in genesis file"))?;

    denom
        .insert(
            "denom".to_owned(),
            serde_json::Value::String(crisis_denom.to_string()),
        )
        .ok_or_else(|| eyre!("failed to update denom in genesis file"))?;

    Ok(())
}

pub fn set_voting_period(genesis: &mut serde_json::Value, period: u64) -> Result<(), Error> {
    // Expedited voting period must be strictly less that the regular voting period
    let regular_period = format!("{period}s");
    let expedited_period = format!("{}s", period - 1);
    let voting_period = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("gov"))
        .and_then(|gov| get_mut_with_fallback(gov, "params", "voting_params"))
        .and_then(|voting_params| voting_params.as_object_mut())
        .ok_or_else(|| eyre!("failed to get voting_params in genesis file"))?;

    voting_period
        .insert(
            "voting_period".to_owned(),
            serde_json::Value::String(regular_period),
        )
        .ok_or_else(|| eyre!("failed to update voting_period in genesis file"))?;

    let maybe_expedited_voting_period = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("gov"))
        .and_then(|gov| get_mut_with_fallback(gov, "params", "expedited_voting_period"));

    if let Some(expedited_voting_period) = maybe_expedited_voting_period {
        let expedited_voting_period = expedited_voting_period
            .as_object_mut()
            .ok_or_else(|| eyre!("failed to get voting_params in genesis file"))?;

        // Only insert `expedited_voting_period` if it already exists in order to avoid adding an unknown configuration in
        // chains using Cosmos SDK pre v0.50
        match expedited_voting_period.get("expedited_voting_period") {
            Some(_) => {
                expedited_voting_period
                .insert(
                    "expedited_voting_period".to_owned(),
                    serde_json::Value::String(expedited_period),
                ).ok_or_else(|| eyre!("failed to update expedited_voting_period in genesis file"))?;
            },
            None => debug!("`expedited_voting_period` was not updated, this configuration was introduced in Cosmos SDK v0.50"),
        }
    }

    Ok(())
}

pub fn set_soft_opt_out_threshold(
    genesis: &mut serde_json::Value,
    threshold: &str,
) -> Result<(), Error> {
    let params = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("ccvconsumer"))
        .and_then(|ccvconsumer| ccvconsumer.get_mut("params"))
        .and_then(|params| params.as_object_mut())
        .ok_or_else(|| eyre!("failed to get ccvconsumer params in genesis file"))?;

    // Might be none if the entry `soft_opt_out_threshold` didn't exist
    params.insert(
        "soft_opt_out_threshold".to_owned(),
        serde_json::Value::String(threshold.to_string()),
    );

    Ok(())
}

pub fn consensus_params_max_gas(
    genesis: &mut serde_json::Value,
    max_gas: &str,
) -> Result<(), Error> {
    let block = match genesis.get_mut("consensus_params") {
        Some(consensus_params) => consensus_params
            .get_mut("block")
            .and_then(|block| block.as_object_mut())
            .ok_or_else(|| eyre!("failed to get `block` field in genesis file"))?,
        None => genesis
            .get_mut("consensus")
            .and_then(|consensus| consensus.get_mut("params"))
            .and_then(|params| params.get_mut("block"))
            .and_then(|block| block.as_object_mut())
            .ok_or_else(|| eyre!("failed to get `block` field in genesis file"))?,
    };

    block.insert(
        "max_gas".to_owned(),
        serde_json::Value::String(max_gas.to_string()),
    );

    Ok(())
}

pub fn globalfee_minimum_gas_prices(
    genesis: &mut serde_json::Value,
    minimum_gas_prices: serde_json::Value,
) -> Result<(), Error> {
    let globalfee = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("globalfee"));

    // Only update `minimum_gas_prices` if `globalfee` is enabled
    match globalfee {
        Some(globalfee) => {
            let params = globalfee
                .get_mut("params")
                .and_then(|params| params.as_object_mut())
                .ok_or_else(|| eyre!("failed to get `params` fields in genesis file"))?;

            params.insert("minimum_gas_prices".to_owned(), minimum_gas_prices);
        }
        None => debug!("chain doesn't have `globalfee`"),
    }

    Ok(())
}

pub fn set_retry_delay_period(
    genesis: &mut serde_json::Value,
    retry_delay_period: &str,
) -> Result<(), Error> {
    let params = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("ccvconsumer"))
        .and_then(|ccvconsumer| ccvconsumer.get_mut("params"))
        .and_then(|params| params.as_object_mut())
        .ok_or_else(|| eyre!("failed to get ccvconsumer params in genesis file"))?;

    params.insert(
        "retry_delay_period".to_owned(),
        serde_json::Value::String(retry_delay_period.to_string()),
    );

    Ok(())
}

pub fn set_floor_gas_price(
    genesis: &mut serde_json::Value,
    amount: &str,
    denom: &str,
    nhash_per_usd_mil: &str,
) -> Result<(), Error> {
    let params = genesis
        .get_mut("app_state")
        .and_then(|app_state| app_state.get_mut("msgfees"))
        .and_then(|msgfees| msgfees.get_mut("params"))
        .and_then(|params| params.as_object_mut())
        .ok_or_else(|| eyre!("failed to get `msgfees params` in genesis file"))?;

    params.insert(
        "nhash_per_usd_mil".to_owned(),
        serde_json::Value::String(nhash_per_usd_mil.to_string()),
    );

    let floor_gas_price = params
        .get_mut("floor_gas_price")
        .and_then(|floor_gas_price| floor_gas_price.as_object_mut())
        .ok_or_else(|| eyre!("failed to get `floor_gas_price` params in genesis file"))?;

    floor_gas_price.insert(
        "amount".to_owned(),
        serde_json::Value::String(amount.to_string()),
    );
    floor_gas_price.insert(
        "denom".to_owned(),
        serde_json::Value::String(denom.to_string()),
    );

    Ok(())
}

/// Look up a key in a JSON object, falling back to the second key if the first one cannot be found.
///
/// This lets us support both Tendermint 0.34 and 0.37, which sometimes use different keys for the
/// same configuration object in the genesis file.
///
/// Eg. in 0.34, the voting params are at `app_state.gov.voting_params`,
/// but in 0.37 they are at `app_state.gov.params`.
///
/// Note: This function is needed to avoid having to inline its code every time we need it.
/// The more obvious way to write it inline would be:
///
/// value.get_mut(key_034).or_else(|| value.get_mut(key_037))
///
/// but that does not work because of the first `get_mut` borrows `value` mutably, which
/// prevents the second `get_mut` from borrowing it again.
fn get_mut_with_fallback<'a>(
    value: &'a mut serde_json::Value,
    key: &str,
    fallback_key: &str,
) -> Option<&'a mut serde_json::Value> {
    let key = match value.get(key) {
        Some(value) if !value.is_null() => key,
        _ => fallback_key,
    };
    value.get_mut(key)
}