Skip to content

Commit d45fcee

Browse files
Re-enable tests in ics02_client and foreign_client (#535)
* ClientId constructor; re-enabled ClientCreate tests * Improved ics18; Fixed cfg cargo annotation. * Prep for fixing ForeignClient tests. * Enable tests in the foreign_client module * Reuse Height parsing * More idiomatic methods in ibc::mock::context::MockContext * Improve assert in build_create_client_and_send * Remove ibc::handler::Event * Fix extract_connection_id * Check IBCEvent produced in create_client * Check IBCEvent produced in test_tm_create_client_ok * update CHANGELOG * relayer: improve tests in foreign_client Co-authored-by: Vitor Enes <vitorenesduarte@gmail.com>
1 parent a1c393f commit d45fcee

32 files changed

+579
-583
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ Cargo.lock
1818
.idea
1919

2020
# Ignore VisualStudio
21-
.vscode
21+
.vscode
22+
23+
# Ignore chain's data
24+
data

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [relayer-cli]
2020
- Replace `ChannelConfig` in `Channel::new` ([#511])
2121
- Add `packet-send` CLI ([#470])
22-
- UX improvements for relayer txs ([#536, 540])
22+
- UX improvements for relayer txs ([#536, #540])
2323

2424
- [relayer]
2525
- Performance improvements ([#514], [#537])
@@ -34,6 +34,11 @@
3434
- [modules]
3535
- Fix for storing `ClientType` upon 'create-client' ([#513])
3636

37+
### BREAKING CHANGES:
38+
39+
- [modules]
40+
- The `ibc::handler::Event` is removed and handlers now produce `ibc::events::IBCEvent`s ([#535])
41+
3742
[#94]: https://github.com/informalsystems/ibc-rs/issues/94
3843
[#306]: https://github.com/informalsystems/ibc-rs/issues/306
3944
[#470]: https://github.com/informalsystems/ibc-rs/issues/470
@@ -50,6 +55,7 @@
5055
[#536]: https://github.com/informalsystems/ibc-rs/issues/536
5156
[#537]: https://github.com/informalsystems/ibc-rs/issues/537
5257
[#540]: https://github.com/informalsystems/ibc-rs/issues/540
58+
[#535]: https://github.com/informalsystems/ibc-rs/issues/535
5359

5460

5561
## v0.0.6

modules/src/events.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::collections::HashMap;
1313
use std::convert::{TryFrom, TryInto};
1414
use tendermint::block::Height;
1515

16-
use tendermint::abci::Event;
1716
use tracing::warn;
1817

1918
/// Events types
@@ -70,25 +69,24 @@ pub enum IBCEvent {
7069
}
7170

7271
// This is tendermint specific
73-
pub fn from_tx_response_event(event: Event) -> Option<IBCEvent> {
72+
pub fn from_tx_response_event(event: &tendermint::abci::Event) -> Option<IBCEvent> {
7473
// Return the first hit we find
75-
// Look for client event...
76-
if let Some(client_res) = ClientEvents::try_from_tx(event.clone()) {
77-
return Some(client_res);
78-
// Look for connection event...
79-
} else if let Some(conn_res) = ConnectionEvents::try_from_tx(event.clone()) {
80-
return Some(conn_res);
74+
if let Some(client_res) = ClientEvents::try_from_tx(event) {
75+
Some(client_res)
76+
} else if let Some(conn_res) = ConnectionEvents::try_from_tx(event) {
77+
Some(conn_res)
8178
} else if let Some(chan_res) = ChannelEvents::try_from_tx(event) {
82-
return Some(chan_res);
79+
Some(chan_res)
80+
} else {
81+
None
8382
}
84-
85-
None
8683
}
8784

8885
impl IBCEvent {
8986
pub fn to_json(&self) -> String {
9087
serde_json::to_string(self).unwrap()
9188
}
89+
9290
pub fn height(&self) -> Height {
9391
match self {
9492
IBCEvent::NewBlock(bl) => bl.height,
@@ -102,6 +100,7 @@ impl IBCEvent {
102100
_ => unimplemented!(),
103101
}
104102
}
103+
105104
pub fn set_height(&mut self, height: ICSHeight) {
106105
match self {
107106
IBCEvent::SendPacketChannel(ev) => {

modules/src/handler.rs

+9-57
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,13 @@
1+
use crate::events::IBCEvent;
12
use std::marker::PhantomData;
23

3-
#[derive(Clone, Debug, PartialEq, Eq)]
4-
pub struct Attribute {
5-
key: String,
6-
value: String,
7-
}
8-
9-
impl Attribute {
10-
pub fn new(key: String, value: String) -> Self {
11-
Self { key, value }
12-
}
13-
14-
pub fn value(&self) -> String {
15-
self.value.clone()
16-
}
17-
18-
pub fn key(&self) -> String {
19-
self.key.clone()
20-
}
21-
}
22-
23-
#[derive(Clone, Debug, PartialEq, Eq)]
24-
pub enum EventType {
25-
Message,
26-
Custom(String),
27-
}
28-
29-
#[derive(Clone, Debug, PartialEq, Eq)]
30-
pub struct Event {
31-
pub tpe: EventType,
32-
pub attributes: Vec<Attribute>,
33-
}
34-
35-
impl Event {
36-
pub fn new(tpe: EventType, attrs: Vec<(String, String)>) -> Self {
37-
Self {
38-
tpe,
39-
attributes: attrs
40-
.into_iter()
41-
.map(|(k, v)| Attribute::new(k, v))
42-
.collect(),
43-
}
44-
}
45-
46-
/// Returns a vector containing the values within all attributes of this event
47-
pub fn attribute_values(&self) -> Vec<String> {
48-
self.attributes.iter().map(|a| a.value.clone()).collect()
49-
}
50-
}
51-
524
pub type HandlerResult<T, E> = Result<HandlerOutput<T>, E>;
535

54-
#[derive(Clone, Debug, PartialEq, Eq)]
6+
#[derive(Clone, Debug)]
557
pub struct HandlerOutput<T> {
568
pub result: T,
579
pub log: Vec<String>,
58-
pub events: Vec<Event>,
10+
pub events: Vec<IBCEvent>,
5911
}
6012

6113
impl<T> HandlerOutput<T> {
@@ -64,10 +16,10 @@ impl<T> HandlerOutput<T> {
6416
}
6517
}
6618

67-
#[derive(Clone, Debug, Default, PartialEq, Eq)]
19+
#[derive(Clone, Debug, Default)]
6820
pub struct HandlerOutputBuilder<T> {
6921
log: Vec<String>,
70-
events: Vec<Event>,
22+
events: Vec<IBCEvent>,
7123
marker: PhantomData<T>,
7224
}
7325

@@ -89,13 +41,13 @@ impl<T> HandlerOutputBuilder<T> {
8941
self.log.push(log.into());
9042
}
9143

92-
pub fn with_events(mut self, events: impl Into<Vec<Event>>) -> Self {
93-
self.events.append(&mut events.into());
44+
pub fn with_events(mut self, mut events: Vec<IBCEvent>) -> Self {
45+
self.events.append(&mut events);
9446
self
9547
}
9648

97-
pub fn emit(&mut self, event: impl Into<Event>) {
98-
self.events.push(event.into());
49+
pub fn emit(&mut self, event: IBCEvent) {
50+
self.events.push(event);
9951
}
10052

10153
pub fn with_result(self, result: T) -> HandlerOutput<T> {

modules/src/ics02_client/client_type.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use super::error;
1+
use std::fmt;
2+
23
use serde_derive::{Deserialize, Serialize};
34

5+
use super::error;
6+
47
/// Type of the client, depending on the specific consensus algorithm.
58
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
69
pub enum ClientType {
@@ -17,11 +20,17 @@ impl ClientType {
1720
Self::Tendermint => "07-tendermint",
1821

1922
#[cfg(any(test, feature = "mocks"))]
20-
Self::Mock => "mock",
23+
Self::Mock => "9999-mock",
2124
}
2225
}
2326
}
2427

28+
impl fmt::Display for ClientType {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
write!(f, "ClientType({})", self.as_string())
31+
}
32+
}
33+
2534
impl std::str::FromStr for ClientType {
2635
type Err = error::Error;
2736

@@ -39,9 +48,10 @@ impl std::str::FromStr for ClientType {
3948

4049
#[cfg(test)]
4150
mod tests {
42-
use super::ClientType;
4351
use std::str::FromStr;
4452

53+
use super::ClientType;
54+
4555
#[test]
4656
fn parse_tendermint_client_type() {
4757
let client_type = ClientType::from_str("07-tendermint");

modules/src/ics02_client/context.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
66
use crate::ics02_client::client_type::ClientType;
77
use crate::ics02_client::error::Error;
8-
use crate::ics02_client::handler::ClientResult::{Create, Update};
9-
use crate::ics02_client::handler::{ClientEvent, ClientResult};
8+
use crate::ics02_client::handler::ClientResult::{self, Create, Update};
109
use crate::ics24_host::identifier::ClientId;
1110
use crate::Height;
1211

@@ -15,25 +14,28 @@ pub trait ClientReader {
1514
fn client_type(&self, client_id: &ClientId) -> Option<ClientType>;
1615
fn client_state(&self, client_id: &ClientId) -> Option<AnyClientState>;
1716
fn consensus_state(&self, client_id: &ClientId, height: Height) -> Option<AnyConsensusState>;
17+
18+
/// Returns a natural number, counting how many clients have been created thus far.
19+
/// The value of this counter should increase only via method `ClientKeeper::increase_client_counter`.
20+
fn client_counter(&self) -> u64;
1821
}
1922

2023
/// Defines the write-only part of ICS2 (client functions) context.
2124
pub trait ClientKeeper {
22-
fn store_client_result(
23-
&mut self,
24-
handler_res: ClientResult,
25-
) -> Result<Vec<ClientEvent>, Error> {
25+
fn store_client_result(&mut self, handler_res: ClientResult) -> Result<(), Error> {
2626
match handler_res {
2727
Create(res) => {
28-
let client_id = self.next_client_id();
28+
let client_id = res.client_id.clone();
29+
2930
self.store_client_type(client_id.clone(), res.client_type)?;
3031
self.store_client_state(client_id.clone(), res.client_state.clone())?;
3132
self.store_consensus_state(
32-
client_id.clone(),
33+
client_id,
3334
res.client_state.latest_height(),
3435
res.consensus_state,
3536
)?;
36-
Ok(vec![ClientEvent::ClientCreated(client_id)])
37+
self.increase_client_counter();
38+
Ok(())
3739
}
3840
Update(res) => {
3941
self.store_client_state(res.client_id.clone(), res.client_state.clone())?;
@@ -42,13 +44,11 @@ pub trait ClientKeeper {
4244
res.client_state.latest_height(),
4345
res.consensus_state,
4446
)?;
45-
Ok(vec![ClientEvent::ClientUpdated(res.client_id)])
47+
Ok(())
4648
}
4749
}
4850
}
4951

50-
fn next_client_id(&mut self) -> ClientId;
51-
5252
/// Called upon successful client creation
5353
fn store_client_type(
5454
&mut self,
@@ -70,4 +70,9 @@ pub trait ClientKeeper {
7070
height: Height,
7171
consensus_state: AnyConsensusState,
7272
) -> Result<(), Error>;
73+
74+
/// Called upon client creation.
75+
/// Increases the counter which keeps track of how many clients have been created.
76+
/// Should never fail.
77+
fn increase_client_counter(&mut self);
7378
}

modules/src/ics02_client/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub enum Kind {
1212
#[error("unknown client type: {0}")]
1313
UnknownClientType(String),
1414

15+
#[error("Client identifier constructor failed for type {0} with counter {1}")]
16+
ClientIdentifierConstructor(ClientType, u64),
17+
1518
#[error("client already exists: {0}")]
1619
ClientAlreadyExists(ClientId),
1720

0 commit comments

Comments
 (0)