Skip to content

Commit

Permalink
Use CosmosSdk as the chain type if omitted, for backward compatibil…
Browse files Browse the repository at this point in the history
…ity (#3787)

* Use `CosmosSdk` as the chain type if omitted, for backward compatibility

Doing this requires a bit of hack, due to serde's lack of support
for specifying a default variant when a tag is not provided.

See the doc comment on the `ChainConfig` struct for more details.

* Add parsing test for default chain type

* Check we parse the correct default type

---------

Co-authored-by: Luca Joss <luca@informal.systems>
  • Loading branch information
romac and ljoss17 authored Jan 18, 2024
1 parent 1bf1319 commit baef139
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ port = 5555
# Specify the chain ID. Required
id = 'ibc-0'

# Specify the chain type, currently only `"CosmosSdk"` is supported.
# Specify the chain type, currently only `CosmosSdk` is supported.
# Default: CosmosSdk
type = "CosmosSdk"

# Whether or not this is a CCV consumer chain. Default: false
Expand Down Expand Up @@ -422,7 +423,6 @@ memo_prefix = ''

[[chains]]
id = 'ibc-1'
type = "CosmosSdk"
rpc_addr = 'http://127.0.0.1:26557'
grpc_addr = 'http://127.0.0.1:9091'
event_source = { mode = 'push', url = 'ws://127.0.0.1:26557/websocket', batch_delay = '500ms' }
Expand Down
1 change: 1 addition & 0 deletions crates/relayer-cli/src/chain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ where
};

Ok(ChainConfig::CosmosSdk(CosmosSdkConfig {
r#type: Default::default(),
id: chain_data.chain_id,
rpc_addr: rpc_data.rpc_address,
grpc_addr: grpc_address,
Expand Down
1 change: 1 addition & 0 deletions crates/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ strum = { version = "0.25", features = ["derive"] }
tokio-stream = "0.1.14"
once_cell = "1.19.0"
tracing-subscriber = { version = "0.3.14", features = ["fmt", "env-filter", "json"] }
monostate = "0.1.11"

[dependencies.byte-unit]
version = "4.0.19"
Expand Down
6 changes: 6 additions & 0 deletions crates/relayer/src/chain/cosmos/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::time::Duration;
use std::path::PathBuf;

use byte_unit::Byte;
use monostate::MustBe;
use serde_derive::{Deserialize, Serialize};
use tendermint_rpc::Url;

Expand All @@ -23,6 +24,11 @@ pub mod error;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CosmosSdkConfig {
/// The type of this chain, must be "CosmosSdk"
/// This is the default if not specified.
#[serde(default)]
pub r#type: MustBe!("CosmosSdk"),

/// The chain's network identifier
pub id: ChainId,

Expand Down
32 changes: 29 additions & 3 deletions crates/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use core::time::Duration;
use std::{fs, fs::File, io::Write, ops::Range, path::Path};

use byte_unit::Byte;
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use tendermint::block::Height as BlockHeight;
use tendermint_rpc::Url;
use tendermint_rpc::WebSocketClientUrl;
Expand Down Expand Up @@ -618,9 +618,18 @@ pub enum EventSourceMode {
},
}

// NOTE:
// To work around a limitation of serde, which does not allow
// to specify a default variant if not tag is present,
// every underlying chain config MUST have a field `r#type` of
// type `monotstate::MustBe!("VariantName")`.
//
// For chains other than CosmosSdk, this field MUST NOT be annotated
// with `#[serde(default)]`.
//
// See https://github.com/serde-rs/serde/issues/2231
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
#[serde(tag = "type")]
#[serde(untagged)]
pub enum ChainConfig {
CosmosSdk(CosmosSdkConfig),
}
Expand Down Expand Up @@ -770,6 +779,7 @@ mod tests {

use super::{load, parse_gas_prices, store_writer};
use crate::config::GasPrice;
use monostate::MustBe;
use test_log::test;

#[test]
Expand Down Expand Up @@ -830,6 +840,22 @@ mod tests {
assert!(load(path).is_err());
}

#[test]
fn parse_default_chain_type() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/config/fixtures/relayer_conf_example_default_chain_type.toml"
);

let config = load(path).expect("could not parse config");

match config.chains[0] {
super::ChainConfig::CosmosSdk(ref config) => {
assert_eq!(config.r#type, MustBe!("CosmosSdk"));
}
}
}

#[test]
fn serialize_valid_config() {
let path = concat!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[global]
log_level = 'error'

[mode]

[mode.clients]
enabled = true
refresh = true
misbehaviour = true

[mode.connections]
enabled = false

[mode.channels]
enabled = false

[mode.packets]
enabled = true
clear_interval = 100
clear_on_start = true
tx_confirmation = true

[[chains]]
id = 'chain_A'
rpc_addr = 'http://127.0.0.1:26657'
grpc_addr = 'http://127.0.0.1:9090'
event_source = { mode = 'push', url = 'ws://localhost:26657/websocket', batch_delay = '500ms' }
rpc_timeout = '10s'
account_prefix = 'cosmos'
key_name = 'testkey'
store_prefix = 'ibc'
max_gas = 200000
gas_price = { price = 0.001, denom = 'stake' }
max_msg_num = 4
max_tx_size = 1048576
max_grpc_decoding_size = '4MiB'
clock_drift = '5s'
trusting_period = '14days'
trust_threshold = { numerator = '1', denominator = '3' }
address_type = { derivation = 'cosmos' }

[[chains]]
id = 'chain_B'
rpc_addr = 'http://127.0.0.1:26557'
grpc_addr = 'http://127.0.0.1:9090'
event_source = { mode = 'push', url = 'ws://localhost:26557/websocket', batch_delay = '500ms' }
rpc_timeout = '10s'
account_prefix = 'cosmos'
key_name = 'testkey'
store_prefix = 'ibc'
gas_price = { price = 0.001, denom = 'stake' }
max_grpc_decoding_size = '5.91 MB'
clock_drift = '5s'
trusting_period = '14days'
trust_threshold = { numerator = '1', denominator = '3' }
address_type = { derivation = 'ethermint', proto_type = { pk_type = '/injective.crypto.v1beta1.ethsecp256k1.PubKey' } }
1 change: 1 addition & 0 deletions tools/test-framework/src/types/single/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl FullNode {
};

Ok(config::ChainConfig::CosmosSdk(CosmosSdkConfig {
r#type: Default::default(),
id: self.chain_driver.chain_id.clone(),
rpc_addr: Url::from_str(&self.chain_driver.rpc_address())?,
grpc_addr: Url::from_str(&self.chain_driver.grpc_address())?,
Expand Down

0 comments on commit baef139

Please sign in to comment.