From d57911e2e5f04e596387db03dd1ba2f14c8e5ef5 Mon Sep 17 00:00:00 2001
From: Luca Joss <luca@informal.systems>
Date: Mon, 13 Nov 2023 13:48:24 +0100
Subject: [PATCH] Handle genesis config 'voting_perdiod' and CLI 'query txs'
 changes for Cosmos SDK v0.50.1

---
 .../src/tests/client_upgrade.rs               |  2 +-
 tools/integration-test/src/tests/ics31.rs     |  2 +-
 .../src/tests/interchain_security/icq.rs      |  2 +-
 tools/test-framework/src/chain/cli/query.rs   | 23 +++++++++++--
 tools/test-framework/src/chain/config.rs      | 32 +++++++++++++++++--
 .../src/util/interchain_security.rs           |  2 +-
 6 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/tools/integration-test/src/tests/client_upgrade.rs b/tools/integration-test/src/tests/client_upgrade.rs
index 9f6ceff850..91a9ee0f53 100644
--- a/tools/integration-test/src/tests/client_upgrade.rs
+++ b/tools/integration-test/src/tests/client_upgrade.rs
@@ -26,7 +26,7 @@ use ibc_test_framework::{
 };
 
 const MAX_DEPOSIT_PERIOD: &str = "10s";
-const VOTING_PERIOD: &str = "10s";
+const VOTING_PERIOD: u64 = 10;
 const DELTA_HEIGHT: u64 = 15;
 const WAIT_CHAIN_UPGRADE: Duration = Duration::from_secs(4);
 const MAX_WAIT_FOR_CHAIN_HEIGHT: Duration = Duration::from_secs(60);
diff --git a/tools/integration-test/src/tests/ics31.rs b/tools/integration-test/src/tests/ics31.rs
index 93454b0820..33065dc091 100644
--- a/tools/integration-test/src/tests/ics31.rs
+++ b/tools/integration-test/src/tests/ics31.rs
@@ -51,7 +51,7 @@ impl TestOverrides for ICS31Test {
                     *duration = serde_json::Value::String("20s".to_owned());
                 }
             }
-            set_voting_period(genesis, "20s")?;
+            set_voting_period(genesis, 20)?;
             set_staking_max_entries(genesis, "10")?;
             set_staking_bond_denom(genesis, "stake")?;
             set_mint_mint_denom(genesis, "stake")?;
diff --git a/tools/integration-test/src/tests/interchain_security/icq.rs b/tools/integration-test/src/tests/interchain_security/icq.rs
index 8b305f92e2..d1fde8a0c9 100644
--- a/tools/integration-test/src/tests/interchain_security/icq.rs
+++ b/tools/integration-test/src/tests/interchain_security/icq.rs
@@ -57,7 +57,7 @@ impl TestOverrides for InterchainSecurityIcqTest {
                     *duration = serde_json::Value::String("20s".to_owned());
                 }
             }
-            set_voting_period(genesis, "10s")?;
+            set_voting_period(genesis, 10)?;
             set_staking_max_entries(genesis, "10")?;
             set_staking_bond_denom(genesis, "stake")?;
             set_mint_mint_denom(genesis, "stake")?;
diff --git a/tools/test-framework/src/chain/cli/query.rs b/tools/test-framework/src/chain/cli/query.rs
index 8178b940ac..b546f54ef8 100644
--- a/tools/test-framework/src/chain/cli/query.rs
+++ b/tools/test-framework/src/chain/cli/query.rs
@@ -111,7 +111,7 @@ pub fn query_recipient_transactions(
     rpc_listen_address: &str,
     recipient_address: &str,
 ) -> Result<json::Value, Error> {
-    let res = simple_exec(
+    let res = match simple_exec(
         chain_id,
         command_path,
         &[
@@ -122,8 +122,25 @@ pub fn query_recipient_transactions(
             "--events",
             &format!("transfer.recipient={recipient_address}"),
         ],
-    )?
-    .stdout;
+    ) {
+        Ok(output) => output.stdout,
+        // Cosmos SDK v0.50.1 changed the `query txs` CLI flag from `--events` to `--query`
+        Err(_) => {
+            simple_exec(
+                chain_id,
+                command_path,
+                &[
+                    "--node",
+                    rpc_listen_address,
+                    "query",
+                    "txs",
+                    "--query",
+                    &format!("transfer.recipient='{recipient_address}'"),
+                ],
+            )?
+            .stdout
+        }
+    };
 
     tracing::debug!("parsing tx result: {}", res);
 
diff --git a/tools/test-framework/src/chain/config.rs b/tools/test-framework/src/chain/config.rs
index a8133dd048..4c74ea3b11 100644
--- a/tools/test-framework/src/chain/config.rs
+++ b/tools/test-framework/src/chain/config.rs
@@ -9,6 +9,7 @@
 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> {
@@ -253,7 +254,10 @@ pub fn set_crisis_denom(genesis: &mut serde_json::Value, crisis_denom: &str) ->
     Ok(())
 }
 
-pub fn set_voting_period(genesis: &mut serde_json::Value, period: &str) -> Result<(), Error> {
+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"))
@@ -264,10 +268,34 @@ pub fn set_voting_period(genesis: &mut serde_json::Value, period: &str) -> Resul
     voting_period
         .insert(
             "voting_period".to_owned(),
-            serde_json::Value::String(period.to_string()),
+            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(())
 }
 
diff --git a/tools/test-framework/src/util/interchain_security.rs b/tools/test-framework/src/util/interchain_security.rs
index ec808a539c..035efb3c84 100644
--- a/tools/test-framework/src/util/interchain_security.rs
+++ b/tools/test-framework/src/util/interchain_security.rs
@@ -10,7 +10,7 @@ pub fn update_genesis_for_consumer_chain(genesis: &mut serde_json::Value) -> Res
         .and_then(|app_state| app_state.get("gov"))
         .is_some()
     {
-        set_voting_period(genesis, "10s")?;
+        set_voting_period(genesis, 10)?;
     }
     Ok(())
 }