Skip to content

Commit

Permalink
Refactor e2e test cosmos bind ports
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Dec 19, 2024
1 parent 9c67203 commit cf5f97f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
34 changes: 21 additions & 13 deletions crates/tests/src/e2e/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,10 @@ fn make_hermes_chain_config_for_cosmos(
) -> Value {
let mut table = toml::map::Map::new();
table.insert("mode".to_owned(), Value::String("push".to_owned()));
let offset = chain_type.get_offset();
let url = format!("ws://127.0.0.1:6416{}/websocket", offset);
let url = format!(
"ws://127.0.0.1:{}/websocket",
chain_type.get_rpc_port_number()
);
table.insert("url".to_owned(), Value::String(url));
table.insert("batch_delay".to_owned(), Value::String("500ms".to_owned()));
let event_source = Value::Table(table);
Expand All @@ -623,11 +625,17 @@ fn make_hermes_chain_config_for_cosmos(

chain.insert(
"rpc_addr".to_owned(),
Value::String(format!("http://127.0.0.1:6416{}", offset)),
Value::String(format!(
"http://127.0.0.1:{}",
chain_type.get_rpc_port_number()
)),
);
chain.insert(
"grpc_addr".to_owned(),
Value::String(format!("http://127.0.0.1:{}", offset + 9090)),
Value::String(format!(
"http://127.0.0.1:{}",
chain_type.get_grpc_port_number()
)),
);

chain.insert("event_source".to_owned(), event_source);
Expand Down Expand Up @@ -672,9 +680,8 @@ pub fn update_cosmos_config(test: &Test) -> Result<()> {
*timeout_propose = "1s".into();
}
}
let offset = CosmosChainType::chain_type(test.net.chain_id.as_str())
.unwrap()
.get_offset();
let chain_type =
CosmosChainType::chain_type(test.net.chain_id.as_str()).unwrap();
let p2p = values
.get_mut("p2p")
.expect("Test failed")
Expand All @@ -683,7 +690,8 @@ pub fn update_cosmos_config(test: &Test) -> Result<()> {
let Some(laddr) = p2p.get_mut("laddr") else {
panic!("Test failed")
};
*laddr = format!("tcp://0.0.0.0:266{}{}", offset, offset).into();
*laddr =
format!("tcp://0.0.0.0:{}", chain_type.get_p2p_port_number()).into();
let rpc = values
.get_mut("rpc")
.expect("Test failed")
Expand All @@ -692,7 +700,8 @@ pub fn update_cosmos_config(test: &Test) -> Result<()> {
let Some(laddr) = rpc.get_mut("laddr") else {
panic!("Test failed")
};
*laddr = format!("tcp://0.0.0.0:6416{offset}").into();
*laddr =
format!("tcp://0.0.0.0:{}", chain_type.get_rpc_port_number()).into();

let mut file = OpenOptions::new()
.write(true)
Expand Down Expand Up @@ -761,10 +770,9 @@ pub fn update_cosmos_config(test: &Test) -> Result<()> {
}

pub fn get_cosmos_rpc_address(test: &Test) -> String {
let offset = CosmosChainType::chain_type(test.net.chain_id.as_str())
.unwrap()
.get_offset();
format!("127.0.0.1:6416{offset}")
let chain_type =
CosmosChainType::chain_type(test.net.chain_id.as_str()).unwrap();
format!("127.0.0.1:{}", chain_type.get_rpc_port_number())
}

pub fn find_cosmos_address(
Expand Down
16 changes: 7 additions & 9 deletions crates/tests/src/e2e/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,7 @@ fn run_cosmos(test: &Test, kill: bool) -> Result<NamadaCmd> {
.output()
.unwrap();
}
let port_arg = format!("0.0.0.0:{}", 9090 + chain_type.get_offset());
let port_arg = format!("0.0.0.0:{}", chain_type.get_grpc_port_number());
let args = ["start", "--pruning", "nothing", "--grpc.address", &port_arg];
let cosmos = run_cosmos_cmd(test, args, Some(40))?;
Ok(cosmos)
Expand Down Expand Up @@ -2853,10 +2853,9 @@ fn transfer_from_cosmos(
let port_id = port_id.to_string();
let channel_id = channel_id.to_string();
let amount = format!("{}{}", amount, token.as_ref());
let offset = CosmosChainType::chain_type(test.net.chain_id.as_str())
.unwrap()
.get_offset();
let rpc = format!("tcp://127.0.0.1:6416{offset}");
let chain_type =
CosmosChainType::chain_type(test.net.chain_id.as_str()).unwrap();
let rpc = format!("tcp://127.0.0.1:{}", chain_type.get_rpc_port_number());
// If the receiver is a pyament address we want to mask it to the more
// general MASP internal address to improve on privacy
let receiver = match PaymentAddress::from_str(receiver.as_ref()) {
Expand Down Expand Up @@ -3015,10 +3014,9 @@ fn check_cosmos_balance(
expected_amount: u64,
) -> Result<()> {
let addr = find_cosmos_address(test, owner)?;
let offset = CosmosChainType::chain_type(test.net.chain_id.as_str())
.unwrap()
.get_offset();
let rpc = format!("tcp://127.0.0.1:6416{offset}");
let chain_type =
CosmosChainType::chain_type(test.net.chain_id.as_str()).unwrap();
let rpc = format!("tcp://127.0.0.1:{}", chain_type.get_rpc_port_number());
let args = ["query", "bank", "balances", &addr, "--node", &rpc];
let mut cosmos = run_cosmos_cmd(test, args, Some(40))?;
cosmos.exp_string(&format!("amount: \"{expected_amount}\""))?;
Expand Down
21 changes: 17 additions & 4 deletions crates/tests/src/e2e/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,24 @@ impl CosmosChainType {
}
}

pub fn get_offset(&self) -> u64 {
pub fn get_p2p_port_number(&self) -> u64 {
10_000 + self.get_offset()
}

pub fn get_rpc_port_number(&self) -> u64 {
20_000 + self.get_offset()
}

pub fn get_grpc_port_number(&self) -> u64 {
30_000 + self.get_offset()
}

fn get_offset(&self) -> u64 {
// NB: ensure none of these ever conflict
match self {
Self::Gaia(Some(off)) => *off,
_ => 0,
Self::CosmWasm => 0,
Self::Gaia(None) => 1,
Self::Gaia(Some(off)) => 2 + *off,
}
}
}
Expand Down Expand Up @@ -1604,7 +1618,6 @@ pub mod constants {
// Gaia or CosmWasm
pub const GAIA_CHAIN_ID: &str = "gaia";
pub const COSMWASM_CHAIN_ID: &str = "cosmwasm";
pub const COSMOS_RPC: &str = "127.0.0.1:64160";
pub const COSMOS_USER: &str = "user";
pub const COSMOS_RELAYER: &str = "relayer";
pub const COSMOS_VALIDATOR: &str = "validator";
Expand Down

0 comments on commit cf5f97f

Please sign in to comment.