Skip to content

Commit 6aba54f

Browse files
committed
refactor(ics24-host): remove From<&str> to avoid panic on host
1 parent 596eccb commit 6aba54f

File tree

10 files changed

+58
-39
lines changed

10 files changed

+58
-39
lines changed

ibc-core/ics03-connection/types/src/connection.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Defines the types that define a connection
22
33
use core::fmt::{Display, Error as FmtError, Formatter};
4+
use core::str::FromStr;
45
use core::time::Duration;
56
use core::u64;
67

@@ -210,8 +211,12 @@ impl TryFrom<RawConnectionEnd> for ConnectionEnd {
210211
if state == State::Uninitialized {
211212
return ConnectionEnd::new(
212213
State::Uninitialized,
213-
"07-tendermint-0".into(),
214-
Counterparty::new("07-tendermint-0".into(), None, CommitmentPrefix::empty()),
214+
ClientId::from_str("07-tendermint-0").expect("should not fail"),
215+
Counterparty::new(
216+
ClientId::from_str("07-tendermint-0").expect("should not fail"),
217+
None,
218+
CommitmentPrefix::empty(),
219+
),
215220
Vec::new(),
216221
ZERO_DURATION,
217222
);

ibc-core/ics24-host/types/src/identifiers/client_id.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ impl FromStr for ClientId {
7676
}
7777
}
7878

79-
impl From<&str> for ClientId {
80-
fn from(s: &str) -> Self {
81-
Self::from_str(s).expect("Invalid client id")
82-
}
83-
}
84-
8579
/// Equality check against string literal (satisfies &ClientId == &str).
8680
/// ```
8781
/// use core::str::FromStr;

ibc-testkit/src/testapp/ibc/clients/mock/misbehaviour.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
use crate::testapp::ibc::clients::mock::header::MockHeader;
2+
use crate::testapp::ibc::clients::mock::proto::Misbehaviour as RawMisbehaviour;
13
use ibc::core::client::types::error::ClientError;
24
use ibc::core::host::types::identifiers::ClientId;
35
use ibc::core::primitives::prelude::*;
46
use ibc::primitives::proto::{Any, Protobuf};
57

6-
use crate::testapp::ibc::clients::mock::header::MockHeader;
7-
use crate::testapp::ibc::clients::mock::proto::Misbehaviour as RawMisbehaviour;
8-
98
pub const MOCK_MISBEHAVIOUR_TYPE_URL: &str = "/ibc.mock.Misbehavior";
109

1110
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -23,7 +22,7 @@ impl TryFrom<RawMisbehaviour> for Misbehaviour {
2322

2423
fn try_from(raw: RawMisbehaviour) -> Result<Self, Self::Error> {
2524
Ok(Self {
26-
client_id: "07-tendermint-0".into(),
25+
client_id: ClientId::new("07-tendermint", 0).expect("no error"),
2726
header1: raw
2827
.header1
2928
.ok_or(ClientError::MissingRawMisbehaviour)?

ibc-testkit/src/testapp/ibc/core/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub struct MockContext {
125125
pub struct MockClientConfig {
126126
#[builder(default = ChainId::new("mockZ-1").expect("no error"))]
127127
client_chain_id: ChainId,
128-
#[builder(default = "07-tendermint-0".into())]
128+
#[builder(default = ClientId::new("07-tendermint", 0).expect("no error"))]
129129
client_id: ClientId,
130130
#[builder(default = mock_client_type())]
131131
client_type: ClientType,

ibc-testkit/tests/core/ics02_client/update_client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct Fixture {
4343

4444
#[fixture]
4545
fn fixture() -> Fixture {
46-
let client_id = ClientId::from("07-tendermint-0");
46+
let client_id = ClientId::new("07-tendermint", 0).expect("no error");
4747

4848
let ctx = MockContext::default().with_client_config(
4949
MockClientConfig::builder()
@@ -82,7 +82,7 @@ fn test_update_client_ok(fixture: Fixture) {
8282
mut router,
8383
} = fixture;
8484

85-
let client_id = ClientId::from("07-tendermint-0");
85+
let client_id = ClientId::new("07-tendermint", 0).expect("no error");
8686
let signer = dummy_account_id();
8787
let timestamp = Timestamp::now();
8888

@@ -114,7 +114,7 @@ fn test_update_client_ok(fixture: Fixture) {
114114
// client's height and ensures that `ConsensusState` is stored at the correct
115115
// path (header height).
116116
fn test_update_client_with_prev_header() {
117-
let client_id = ClientId::from("07-tendermint-0");
117+
let client_id = ClientId::new("07-tendermint", 0).expect("no error");
118118
let chain_id_b = ChainId::new("mockgaiaA-0").unwrap();
119119
let latest_height = Height::new(0, 42).unwrap();
120120
let height_1 = Height::new(0, 43).unwrap();
@@ -780,7 +780,7 @@ fn test_update_client_events(fixture: Fixture) {
780780
mut router,
781781
} = fixture;
782782

783-
let client_id = ClientId::from("07-tendermint-0");
783+
let client_id = ClientId::new("07-tendermint", 0).expect("no error");
784784
let signer = dummy_account_id();
785785
let timestamp = Timestamp::now();
786786

@@ -841,7 +841,7 @@ fn test_misbehaviour_client_ok(fixture: Fixture) {
841841
mut router,
842842
} = fixture;
843843

844-
let client_id = ClientId::from("07-tendermint-0");
844+
let client_id = ClientId::new("07-tendermint", 0).expect("no error");
845845
let msg_envelope = msg_update_client(&client_id);
846846

847847
let res = validate(&ctx, &router, msg_envelope.clone());

ibc-testkit/tests/core/ics04_channel/acknowledgement.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ibc::core::connection::types::{
1212
use ibc::core::entrypoint::{execute, validate};
1313
use ibc::core::handler::types::events::{IbcEvent, MessageEvent};
1414
use ibc::core::handler::types::msgs::MsgEnvelope;
15-
use ibc::core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
15+
use ibc::core::host::types::identifiers::{ChannelId, ClientId, ConnectionId, PortId};
1616
use ibc::core::host::ExecutionContext;
1717
use ibc::core::primitives::*;
1818
use ibc_testkit::fixtures::core::channel::dummy_raw_msg_acknowledgement;
@@ -34,6 +34,8 @@ struct Fixture {
3434

3535
#[fixture]
3636
fn fixture() -> Fixture {
37+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
38+
3739
let client_height = Height::new(0, 2).unwrap();
3840
let ctx = MockContext::default().with_client_config(
3941
MockClientConfig::builder()
@@ -70,9 +72,9 @@ fn fixture() -> Fixture {
7072

7173
let conn_end_on_a = ConnectionEnd::new(
7274
ConnectionState::Open,
73-
"07-tendermint-0".into(),
75+
default_client_id.clone(),
7476
ConnectionCounterparty::new(
75-
"07-tendermint-0".into(),
77+
default_client_id.clone(),
7678
Some(ConnectionId::zero()),
7779
CommitmentPrefix::empty(),
7880
),
@@ -146,6 +148,7 @@ fn ack_success_no_packet_commitment(fixture: Fixture) {
146148

147149
#[rstest]
148150
fn ack_success_happy_path(fixture: Fixture) {
151+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
149152
let Fixture {
150153
ctx,
151154
router,
@@ -176,7 +179,7 @@ fn ack_success_happy_path(fixture: Fixture) {
176179
);
177180
ctx.get_client_execution_context()
178181
.store_update_meta(
179-
"07-tendermint-0".into(),
182+
default_client_id,
180183
client_height,
181184
Timestamp::from_nanoseconds(1000).unwrap(),
182185
Height::new(0, 4).unwrap(),

ibc-testkit/tests/core/ics04_channel/recv_packet.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ibc::core::connection::types::{
1212
use ibc::core::entrypoint::{execute, validate};
1313
use ibc::core::handler::types::events::{IbcEvent, MessageEvent};
1414
use ibc::core::handler::types::msgs::MsgEnvelope;
15-
use ibc::core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
15+
use ibc::core::host::types::identifiers::{ChannelId, ClientId, ConnectionId, PortId};
1616
use ibc::core::host::ExecutionContext;
1717
use ibc::core::primitives::*;
1818
use ibc_testkit::fixtures::core::channel::{dummy_msg_recv_packet, dummy_raw_msg_recv_packet};
@@ -31,10 +31,13 @@ pub struct Fixture {
3131
pub msg: MsgRecvPacket,
3232
pub conn_end_on_b: ConnectionEnd,
3333
pub chan_end_on_b: ChannelEnd,
34+
pub default_client_id: ClientId,
3435
}
3536

3637
#[fixture]
3738
fn fixture() -> Fixture {
39+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
40+
3841
let context = MockContext::default();
3942

4043
let router = MockRouter::new_with_transfer();
@@ -59,9 +62,9 @@ fn fixture() -> Fixture {
5962

6063
let conn_end_on_b = ConnectionEnd::new(
6164
ConnectionState::Open,
62-
"07-tendermint-0".into(),
65+
default_client_id.clone(),
6366
ConnectionCounterparty::new(
64-
"07-tendermint-0".into(),
67+
default_client_id.clone(),
6568
Some(ConnectionId::zero()),
6669
CommitmentPrefix::empty(),
6770
),
@@ -78,6 +81,7 @@ fn fixture() -> Fixture {
7881
msg,
7982
conn_end_on_b,
8083
chan_end_on_b,
84+
default_client_id,
8185
}
8286
}
8387

@@ -110,6 +114,7 @@ fn recv_packet_validate_happy_path(fixture: Fixture) {
110114
chan_end_on_b,
111115
client_height,
112116
host_height,
117+
default_client_id,
113118
..
114119
} = fixture;
115120

@@ -142,7 +147,7 @@ fn recv_packet_validate_happy_path(fixture: Fixture) {
142147
context
143148
.get_client_execution_context()
144149
.store_update_meta(
145-
"07-tendermint-0".into(),
150+
default_client_id,
146151
client_height,
147152
Timestamp::from_nanoseconds(1000).unwrap(),
148153
Height::new(0, 5).unwrap(),

ibc-testkit/tests/core/ics04_channel/send_packet.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ use ibc::core::connection::types::{
1212
ConnectionEnd, Counterparty as ConnectionCounterparty, State as ConnectionState,
1313
};
1414
use ibc::core::handler::types::events::{IbcEvent, MessageEvent};
15-
use ibc::core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
15+
use ibc::core::host::types::identifiers::{ChannelId, ClientId, ConnectionId, PortId};
1616
use ibc::core::primitives::*;
1717
use ibc_testkit::fixtures::core::channel::dummy_raw_packet;
1818
use ibc_testkit::testapp::ibc::core::types::{MockClientConfig, MockContext};
1919
use test_log::test;
2020

2121
#[test]
2222
fn send_packet_processing() {
23+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
24+
2325
struct Test {
2426
name: String,
2527
ctx: MockContext,
@@ -40,9 +42,9 @@ fn send_packet_processing() {
4042

4143
let conn_end_on_a = ConnectionEnd::new(
4244
ConnectionState::Open,
43-
"07-tendermint-0".into(),
45+
default_client_id.clone(),
4446
ConnectionCounterparty::new(
45-
"07-tendermint-0".into(),
47+
default_client_id.clone(),
4648
Some(ConnectionId::zero()),
4749
CommitmentPrefix::empty(),
4850
),

ibc-testkit/tests/core/ics04_channel/timeout.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ibc::core::connection::types::{
1212
use ibc::core::entrypoint::{execute, validate};
1313
use ibc::core::handler::types::events::{IbcEvent, MessageEvent};
1414
use ibc::core::handler::types::msgs::MsgEnvelope;
15-
use ibc::core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
15+
use ibc::core::host::types::identifiers::{ChannelId, ClientId, ConnectionId, PortId};
1616
use ibc::core::host::ExecutionContext;
1717
use ibc::core::primitives::*;
1818
use ibc_testkit::fixtures::core::channel::dummy_raw_msg_timeout;
@@ -29,10 +29,13 @@ struct Fixture {
2929
conn_end_on_a: ConnectionEnd,
3030
chan_end_on_a_ordered: ChannelEnd,
3131
chan_end_on_a_unordered: ChannelEnd,
32+
default_client_id: ClientId,
3233
}
3334

3435
#[fixture]
3536
fn fixture() -> Fixture {
37+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
38+
3639
let client_height = Height::new(0, 2).unwrap();
3740
let ctx = MockContext::default().with_client_config(
3841
MockClientConfig::builder()
@@ -77,9 +80,9 @@ fn fixture() -> Fixture {
7780

7881
let conn_end_on_a = ConnectionEnd::new(
7982
ConnectionState::Open,
80-
"07-tendermint-0".into(),
83+
default_client_id.clone(),
8184
ConnectionCounterparty::new(
82-
"07-tendermint-0".into(),
85+
default_client_id.clone(),
8386
Some(ConnectionId::zero()),
8487
CommitmentPrefix::empty(),
8588
),
@@ -97,6 +100,7 @@ fn fixture() -> Fixture {
97100
conn_end_on_a,
98101
chan_end_on_a_ordered,
99102
chan_end_on_a_unordered,
103+
default_client_id,
100104
}
101105
}
102106

@@ -170,6 +174,7 @@ fn timeout_fail_proof_timeout_not_reached(fixture: Fixture) {
170174
chan_end_on_a_unordered,
171175
conn_end_on_a,
172176
client_height,
177+
default_client_id,
173178
..
174179
} = fixture;
175180

@@ -205,7 +210,7 @@ fn timeout_fail_proof_timeout_not_reached(fixture: Fixture) {
205210
);
206211

207212
ctx.store_update_meta(
208-
"07-tendermint-0".into(),
213+
default_client_id,
209214
client_height,
210215
Timestamp::from_nanoseconds(5).unwrap(),
211216
Height::new(0, 4).unwrap(),
@@ -261,6 +266,7 @@ fn timeout_unordered_channel_validate(fixture: Fixture) {
261266
conn_end_on_a,
262267
packet_commitment,
263268
client_height,
269+
default_client_id,
264270
..
265271
} = fixture;
266272

@@ -287,7 +293,7 @@ fn timeout_unordered_channel_validate(fixture: Fixture) {
287293

288294
ctx.get_client_execution_context()
289295
.store_update_meta(
290-
"07-tendermint-0".into(),
296+
default_client_id,
291297
client_height,
292298
Timestamp::from_nanoseconds(1000).unwrap(),
293299
Height::new(0, 5).unwrap(),
@@ -311,6 +317,7 @@ fn timeout_ordered_channel_validate(fixture: Fixture) {
311317
conn_end_on_a,
312318
packet_commitment,
313319
client_height,
320+
default_client_id,
314321
..
315322
} = fixture;
316323

@@ -332,7 +339,7 @@ fn timeout_ordered_channel_validate(fixture: Fixture) {
332339
);
333340

334341
ctx.store_update_meta(
335-
"07-tendermint-0".into(),
342+
default_client_id,
336343
client_height,
337344
Timestamp::from_nanoseconds(1000).unwrap(),
338345
Height::new(0, 4).unwrap(),

ibc-testkit/tests/core/ics04_channel/timeout_on_close.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ibc::core::connection::types::{
1111
};
1212
use ibc::core::entrypoint::validate;
1313
use ibc::core::handler::types::msgs::MsgEnvelope;
14-
use ibc::core::host::types::identifiers::{ChannelId, ConnectionId, PortId};
14+
use ibc::core::host::types::identifiers::{ChannelId, ClientId, ConnectionId, PortId};
1515
use ibc::core::host::ExecutionContext;
1616
use ibc::core::primitives::*;
1717
use ibc_testkit::fixtures::core::channel::dummy_raw_msg_timeout_on_close;
@@ -30,6 +30,8 @@ pub struct Fixture {
3030

3131
#[fixture]
3232
fn fixture() -> Fixture {
33+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
34+
3335
let client_height = Height::new(0, 2).unwrap();
3436
let context = MockContext::default().with_client_config(
3537
MockClientConfig::builder()
@@ -64,9 +66,9 @@ fn fixture() -> Fixture {
6466

6567
let conn_end_on_a = ConnectionEnd::new(
6668
ConnectionState::Open,
67-
"07-tendermint-0".into(),
69+
default_client_id.clone(),
6870
ConnectionCounterparty::new(
69-
"07-tendermint-0".into(),
71+
default_client_id.clone(),
7072
Some(ConnectionId::zero()),
7173
CommitmentPrefix::empty(),
7274
),
@@ -131,6 +133,8 @@ fn timeout_on_close_success_no_packet_commitment(fixture: Fixture) {
131133

132134
#[rstest]
133135
fn timeout_on_close_success_happy_path(fixture: Fixture) {
136+
let default_client_id = ClientId::new("07-tendermint", 0).expect("no error");
137+
134138
let Fixture {
135139
context,
136140
router,
@@ -153,7 +157,7 @@ fn timeout_on_close_success_happy_path(fixture: Fixture) {
153157
context
154158
.get_client_execution_context()
155159
.store_update_meta(
156-
"07-tendermint-0".into(),
160+
default_client_id,
157161
Height::new(0, 2).unwrap(),
158162
Timestamp::from_nanoseconds(5000).unwrap(),
159163
Height::new(0, 5).unwrap(),

0 commit comments

Comments
 (0)