Skip to content

Commit 55a2f22

Browse files
authored
Fix host_height, host_timestamp return value (#243)
* Change host_height, host_timestamp return Value * fmt code * Create 242-change-return-value.md * fix clippy * update .changelog * Change host_height, host_timestamp return value in ValidationContext * update Ics18context to RelayerContext in mock/context.rs Signed-off-by: Davirain <[email protected]>
1 parent 092382d commit 55a2f22

File tree

18 files changed

+67
-54
lines changed

18 files changed

+67
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Change `host_height`, `host_timestamp` return value to a `Result` in `ClientReader`, `ConnectionReader`, `ChannelReader` and `ValidationContext`
2+
([#242](https://github.com/cosmos/ibc-rs/issues/242))

crates/ibc/src/clients/ics07_tendermint/client_state.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl Ics2ClientState for ClientState {
403403
untrusted_state,
404404
trusted_state,
405405
&options,
406-
ctx.host_timestamp().into_tm_time().unwrap(),
406+
ctx.host_timestamp()?.into_tm_time().unwrap(),
407407
);
408408

409409
match verdict {
@@ -574,7 +574,7 @@ impl Ics2ClientState for ClientState {
574574
untrusted_state,
575575
trusted_state,
576576
&options,
577-
ctx.host_timestamp().into_tm_time().unwrap(),
577+
ctx.host_timestamp()?.into_tm_time().unwrap(),
578578
);
579579

580580
match verdict {
@@ -915,8 +915,12 @@ fn verify_delay_passed(
915915
height: Height,
916916
connection_end: &ConnectionEnd,
917917
) -> Result<(), Ics02Error> {
918-
let current_timestamp = ctx.host_timestamp();
919-
let current_height = ctx.host_height();
918+
let current_timestamp = ctx
919+
.host_timestamp()
920+
.map_err(|e| Ics02Error::other(e.to_string()))?;
921+
let current_height = ctx
922+
.host_height()
923+
.map_err(|e| Ics02Error::other(e.to_string()))?;
920924

921925
let client_id = connection_end.client_id();
922926
let processed_time = ctx

crates/ibc/src/core/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ pub trait ValidationContext {
9090
) -> Result<Option<Box<dyn ConsensusState>>, ClientError>;
9191

9292
/// Returns the current height of the local chain.
93-
fn host_height(&self) -> Height;
93+
fn host_height(&self) -> Result<Height, ClientError>;
9494

9595
/// Returns the current timestamp of the local chain.
96-
fn host_timestamp(&self) -> Timestamp {
96+
fn host_timestamp(&self) -> Result<Timestamp, ClientError> {
9797
let pending_consensus_state = self
9898
.pending_host_consensus_state()
9999
.expect("host must have pending consensus state");
100-
pending_consensus_state.timestamp()
100+
Ok(pending_consensus_state.timestamp())
101101
}
102102

103103
/// Returns the pending `ConsensusState` of the host (local) chain.

crates/ibc/src/core/ics02_client/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ pub trait ClientReader {
5151
) -> Result<Option<Box<dyn ConsensusState>>, Error>;
5252

5353
/// Returns the current height of the local chain.
54-
fn host_height(&self) -> Height;
54+
fn host_height(&self) -> Result<Height, Error>;
5555

5656
/// Returns the current timestamp of the local chain.
57-
fn host_timestamp(&self) -> Timestamp {
57+
fn host_timestamp(&self) -> Result<Timestamp, Error> {
5858
let pending_consensus_state = self
5959
.pending_host_consensus_state()
6060
.expect("host must have pending consensus state");
61-
pending_consensus_state.timestamp()
61+
Ok(pending_consensus_state.timestamp())
6262
}
6363

6464
/// Returns the `ConsensusState` of the host (local) chain at a specific height.

crates/ibc/src/core/ics02_client/handler/create_client.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ where
9090
ctx.store_update_time(
9191
client_id.clone(),
9292
client_state.latest_height(),
93-
ctx.host_timestamp(),
93+
ctx.host_timestamp()?,
9494
)?;
9595
ctx.store_update_height(
9696
client_id.clone(),
9797
client_state.latest_height(),
98-
ctx.host_height(),
98+
ctx.host_height()?,
9999
)?;
100100

101101
ctx.emit_ibc_event(IbcEvent::CreateClient(CreateClient::new(
@@ -141,8 +141,8 @@ pub fn process(ctx: &dyn ClientReader, msg: MsgCreateClient) -> HandlerResult<Cl
141141
client_type: client_type.clone(),
142142
client_state,
143143
consensus_state,
144-
processed_time: ctx.host_timestamp(),
145-
processed_height: ctx.host_height(),
144+
processed_time: ctx.host_timestamp()?,
145+
processed_height: ctx.host_height()?,
146146
});
147147

148148
output.emit(IbcEvent::CreateClient(CreateClient::new(

crates/ibc/src/core/ics02_client/handler/update_client.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ where
5656

5757
debug!("latest consensus state: {:?}", latest_consensus_state);
5858

59-
let now = ctx.host_timestamp();
59+
let now = ctx.host_timestamp()?;
6060
let duration = now
6161
.duration_since(&latest_consensus_state.timestamp())
6262
.ok_or_else(|| {
@@ -109,12 +109,12 @@ where
109109
ctx.store_update_time(
110110
client_id.clone(),
111111
client_state.latest_height(),
112-
ctx.host_timestamp(),
112+
ctx.host_timestamp()?,
113113
)?;
114114
ctx.store_update_height(
115115
client_id.clone(),
116116
client_state.latest_height(),
117-
ctx.host_height(),
117+
ctx.host_height()?,
118118
)?;
119119

120120
{
@@ -160,7 +160,7 @@ pub fn process<Ctx: ClientReader>(
160160

161161
debug!("latest consensus state: {:?}", latest_consensus_state);
162162

163-
let now = ClientReader::host_timestamp(ctx);
163+
let now = ClientReader::host_timestamp(ctx)?;
164164
let duration = now
165165
.duration_since(&latest_consensus_state.timestamp())
166166
.ok_or_else(|| {
@@ -191,8 +191,8 @@ pub fn process<Ctx: ClientReader>(
191191
client_id: client_id.clone(),
192192
client_state,
193193
consensus_state,
194-
processed_time: ClientReader::host_timestamp(ctx),
195-
processed_height: ctx.host_height(),
194+
processed_time: ClientReader::host_timestamp(ctx)?,
195+
processed_height: ctx.host_height()?,
196196
});
197197

198198
output.emit(IbcEvent::UpdateClient(UpdateClient::new(

crates/ibc/src/core/ics03_connection/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ pub trait ConnectionReader {
2828
fn decode_client_state(&self, client_state: Any) -> Result<Box<dyn ClientState>, Error>;
2929

3030
/// Returns the current height of the local chain.
31-
fn host_current_height(&self) -> Height;
31+
fn host_current_height(&self) -> Result<Height, Error>;
3232

3333
#[deprecated(since = "0.20.0")]
3434
/// Returns the oldest height available on the local chain.
35-
fn host_oldest_height(&self) -> Height;
35+
fn host_oldest_height(&self) -> Result<Height, Error>;
3636

3737
/// Returns the prefix that the local chain uses in the KV store.
3838
fn commitment_prefix(&self) -> CommitmentPrefix;

crates/ibc/src/core/ics03_connection/handler/conn_open_ack.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub(crate) fn process(
1919
) -> HandlerResult<ConnectionResult, Error> {
2020
let mut output = HandlerOutput::builder();
2121

22-
if msg.consensus_height_of_a_on_b > ctx_a.host_current_height() {
22+
if msg.consensus_height_of_a_on_b > ctx_a.host_current_height()? {
2323
return Err(Error::invalid_consensus_height(
2424
msg.consensus_height_of_a_on_b,
25-
ctx_a.host_current_height(),
25+
ctx_a.host_current_height()?,
2626
));
2727
}
2828

crates/ibc/src/core/ics03_connection/handler/conn_open_try.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ pub(crate) fn process(
2424

2525
ctx_b.validate_self_client(msg.client_state_of_b_on_a.clone())?;
2626

27-
if msg.consensus_height_of_b_on_a > ctx_b.host_current_height() {
27+
if msg.consensus_height_of_b_on_a > ctx_b.host_current_height()? {
2828
// Fail if the consensus height is too advanced.
2929
return Err(Error::invalid_consensus_height(
3030
msg.consensus_height_of_b_on_a,
31-
ctx_b.host_current_height(),
31+
ctx_b.host_current_height()?,
3232
));
3333
}
3434

crates/ibc/src/core/ics04_channel/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ pub trait ChannelReader {
113113
fn hash(&self, value: Vec<u8>) -> Vec<u8>;
114114

115115
/// Returns the current height of the local chain.
116-
fn host_height(&self) -> Height;
116+
fn host_height(&self) -> Result<Height, Error>;
117117

118118
/// Returns the current timestamp of the local chain.
119-
fn host_timestamp(&self) -> Timestamp {
119+
fn host_timestamp(&self) -> Result<Timestamp, Error> {
120120
let pending_consensus_state = self
121121
.pending_host_consensus_state()
122122
.expect("host must have pending consensus state");
123-
pending_consensus_state.timestamp()
123+
Ok(pending_consensus_state.timestamp())
124124
}
125125

126126
/// Returns the `ConsensusState` of the host (local) chain at a specific height.

crates/ibc/src/core/ics04_channel/handler/chan_close_confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ mod tests {
133133
let client_id = ClientId::new(mock_client_type(), 24).unwrap();
134134
let conn_id = ConnectionId::new(2);
135135
let default_context = MockContext::default();
136-
let client_consensus_state_height = default_context.host_height();
136+
let client_consensus_state_height = default_context.host_height().unwrap();
137137

138138
let conn_end = ConnectionEnd::new(
139139
ConnectionState::Open,

crates/ibc/src/core/ics04_channel/handler/chan_close_init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ mod tests {
111111

112112
let context = {
113113
let default_context = MockContext::default();
114-
let client_consensus_state_height = default_context.host_height();
114+
let client_consensus_state_height = default_context.host_height().unwrap();
115115

116116
default_context
117117
.with_client(&client_id, client_consensus_state_height)

crates/ibc/src/core/ics04_channel/handler/chan_open_confirm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ mod tests {
140140
let client_id = ClientId::new(mock_client_type(), 24).unwrap();
141141
let conn_id = ConnectionId::new(2);
142142
let context = MockContext::default();
143-
let client_consensus_state_height = context.host_current_height().revision_height();
143+
let client_consensus_state_height =
144+
context.host_current_height().unwrap().revision_height();
144145

145146
// The connection underlying the channel we're trying to open.
146147
let conn_end = ConnectionEnd::new(

crates/ibc/src/core/ics04_channel/handler/recv_packet.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ pub fn process<Ctx: ChannelReader>(
6666
));
6767
}
6868

69-
let latest_height = ChannelReader::host_height(ctx);
69+
let latest_height = ChannelReader::host_height(ctx)?;
7070
if packet.timeout_height.has_expired(latest_height) {
7171
return Err(Error::low_packet_height(
7272
latest_height,
7373
packet.timeout_height,
7474
));
7575
}
7676

77-
let latest_timestamp = ChannelReader::host_timestamp(ctx);
77+
let latest_timestamp = ChannelReader::host_timestamp(ctx)?;
7878
if let Expiry::Expired = latest_timestamp.check_expiry(&packet.timeout_timestamp) {
7979
return Err(Error::low_packet_timestamp());
8080
}
@@ -186,7 +186,7 @@ mod tests {
186186

187187
let context = MockContext::default();
188188

189-
let host_height = context.query_latest_height().increment();
189+
let host_height = context.query_latest_height().unwrap().increment();
190190

191191
let client_height = host_height.increment();
192192

crates/ibc/src/mock/context.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -845,12 +845,12 @@ impl ChannelReader for MockContext {
845845
sha2::Sha256::digest(value).to_vec()
846846
}
847847

848-
fn host_height(&self) -> Height {
849-
self.latest_height()
848+
fn host_height(&self) -> Result<Height, Ics04Error> {
849+
Ok(self.latest_height())
850850
}
851851

852-
fn host_timestamp(&self) -> Timestamp {
853-
ClientReader::host_timestamp(self)
852+
fn host_timestamp(&self) -> Result<Timestamp, Ics04Error> {
853+
ClientReader::host_timestamp(self).map_err(|e| Ics04Error::other(e.to_string()))
854854
}
855855

856856
fn host_consensus_state(&self, height: Height) -> Result<Box<dyn ConsensusState>, Ics04Error> {
@@ -1103,13 +1103,13 @@ impl ConnectionReader for MockContext {
11031103
ClientReader::decode_client_state(self, client_state).map_err(Ics03Error::ics02_client)
11041104
}
11051105

1106-
fn host_current_height(&self) -> Height {
1107-
self.latest_height()
1106+
fn host_current_height(&self) -> Result<Height, Ics03Error> {
1107+
Ok(self.latest_height())
11081108
}
11091109

1110-
fn host_oldest_height(&self) -> Height {
1110+
fn host_oldest_height(&self) -> Result<Height, Ics03Error> {
11111111
// history must be non-empty, so `self.history[0]` is valid
1112-
self.history[0].height()
1112+
Ok(self.history[0].height())
11131113
}
11141114

11151115
fn commitment_prefix(&self) -> CommitmentPrefix {
@@ -1275,17 +1275,18 @@ impl ClientReader for MockContext {
12751275
Ok(None)
12761276
}
12771277

1278-
fn host_height(&self) -> Height {
1279-
self.latest_height()
1278+
fn host_height(&self) -> Result<Height, Ics02Error> {
1279+
Ok(self.latest_height())
12801280
}
12811281

1282-
fn host_timestamp(&self) -> Timestamp {
1283-
self.history
1282+
fn host_timestamp(&self) -> Result<Timestamp, Ics02Error> {
1283+
Ok(self
1284+
.history
12841285
.last()
12851286
.expect("history cannot be empty")
12861287
.timestamp()
12871288
.add(self.block_time)
1288-
.unwrap()
1289+
.unwrap())
12891290
}
12901291

12911292
fn host_consensus_state(&self, height: Height) -> Result<Box<dyn ConsensusState>, Ics02Error> {
@@ -1401,8 +1402,8 @@ impl ClientKeeper for MockContext {
14011402
}
14021403

14031404
impl RelayerContext for MockContext {
1404-
fn query_latest_height(&self) -> Height {
1405-
self.host_current_height()
1405+
fn query_latest_height(&self) -> Result<Height, Ics18Error> {
1406+
self.host_current_height().map_err(Ics18Error::ics03)
14061407
}
14071408

14081409
fn query_client_full_state(&self, client_id: &ClientId) -> Option<Box<dyn ClientState>> {
@@ -1411,7 +1412,7 @@ impl RelayerContext for MockContext {
14111412
}
14121413

14131414
fn query_latest_header(&self) -> Option<Box<dyn Header>> {
1414-
let block_ref = self.host_block(self.host_current_height());
1415+
let block_ref = self.host_block(self.host_current_height().unwrap());
14151416
block_ref.cloned().map(Header::into_box)
14161417
}
14171418

crates/ibc/src/relayer/ics18_relayer/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::Height;
1717
/// types, light client, RPC client, etc.)
1818
pub trait RelayerContext {
1919
/// Returns the latest height of the chain.
20-
fn query_latest_height(&self) -> Height;
20+
fn query_latest_height(&self) -> Result<Height, Error>;
2121

2222
/// Returns this client state for the given `client_id` on this chain.
2323
/// Wrapper over the `/abci_query?path=..` endpoint.

crates/ibc/src/relayer/ics18_relayer/error.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::core::ics03_connection;
12
use crate::core::ics24_host::identifier::ClientId;
23
use crate::core::ics26_routing::error::Error as RoutingError;
34
use crate::Height;
@@ -34,5 +35,9 @@ define_error! {
3435
TransactionFailed
3536
[ RoutingError ]
3637
| _ | { "transaction processing by modules failed" },
38+
39+
Ics03
40+
[ ics03_connection::error::Error ]
41+
| _ | { "ics03 connection error" }
3742
}
3843
}

crates/ibc/src/relayer/ics18_relayer/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod tests {
149149
.query_client_full_state(&client_on_b_for_a)
150150
.unwrap()
151151
.latest_height();
152-
assert_eq!(client_height_b, ctx_a.query_latest_height());
152+
assert_eq!(client_height_b, ctx_a.query_latest_height().unwrap());
153153

154154
// Update client on chain B to latest height of B.
155155
// - create the client update message with the latest header from B
@@ -206,7 +206,7 @@ mod tests {
206206
.query_client_full_state(&client_on_a_for_b)
207207
.unwrap()
208208
.latest_height();
209-
assert_eq!(client_height_a, ctx_b.query_latest_height());
209+
assert_eq!(client_height_a, ctx_b.query_latest_height().unwrap());
210210
}
211211
}
212212
}

0 commit comments

Comments
 (0)