Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion from &IbcEvent to abci::Event #438

Merged
merged 6 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Allow conversion from &IbcEvent to abci::Event
([#416](https://github.com/cosmos/ibc-rs/issues/416))
88 changes: 48 additions & 40 deletions crates/ibc/src/core/ics02_client/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct ClientIdAttribute {
client_id: ClientId,
}

impl From<ClientIdAttribute> for abci::EventAttribute {
fn from(attr: ClientIdAttribute) -> Self {
impl From<&ClientIdAttribute> for abci::EventAttribute {
fn from(attr: &ClientIdAttribute) -> Self {
(CLIENT_ID_ATTRIBUTE_KEY, attr.client_id.as_str()).into()
}
}
Expand All @@ -67,8 +67,8 @@ struct ClientTypeAttribute {
client_type: ClientType,
}

impl From<ClientTypeAttribute> for abci::EventAttribute {
fn from(attr: ClientTypeAttribute) -> Self {
impl From<&ClientTypeAttribute> for abci::EventAttribute {
fn from(attr: &ClientTypeAttribute) -> Self {
(CLIENT_TYPE_ATTRIBUTE_KEY, attr.client_type.as_str()).into()
}
}
Expand All @@ -91,8 +91,8 @@ struct ConsensusHeightAttribute {
consensus_height: Height,
}

impl From<ConsensusHeightAttribute> for abci::EventAttribute {
fn from(attr: ConsensusHeightAttribute) -> Self {
impl From<&ConsensusHeightAttribute> for abci::EventAttribute {
fn from(attr: &ConsensusHeightAttribute) -> Self {
(CONSENSUS_HEIGHT_ATTRIBUTE_KEY, attr.consensus_height).into()
}
}
Expand All @@ -115,11 +115,11 @@ struct ConsensusHeightsAttribute {
consensus_heights: Vec<Height>,
}

impl From<ConsensusHeightsAttribute> for abci::EventAttribute {
fn from(attr: ConsensusHeightsAttribute) -> Self {
impl From<&ConsensusHeightsAttribute> for abci::EventAttribute {
fn from(attr: &ConsensusHeightsAttribute) -> Self {
let consensus_heights: Vec<String> = attr
.consensus_heights
.into_iter()
.iter()
.map(|consensus_height| consensus_height.to_string())
.collect();
(CONSENSUS_HEIGHTS_ATTRIBUTE_KEY, consensus_heights.join(",")).into()
Expand All @@ -144,11 +144,11 @@ struct HeaderAttribute {
header: Any,
}

impl From<HeaderAttribute> for abci::EventAttribute {
fn from(attr: HeaderAttribute) -> Self {
impl From<&HeaderAttribute> for abci::EventAttribute {
fn from(attr: &HeaderAttribute) -> Self {
(
HEADER_ATTRIBUTE_KEY,
String::from_utf8(hex::encode(attr.header.value)).unwrap(),
String::from_utf8(hex::encode(&attr.header.value)).unwrap(),
)
.into()
}
Expand Down Expand Up @@ -197,14 +197,14 @@ impl CreateClient {
}
}

impl From<CreateClient> for abci::Event {
fn from(c: CreateClient) -> Self {
impl From<&CreateClient> for abci::Event {
fn from(c: &CreateClient) -> Self {
Self {
kind: IbcEventType::CreateClient.as_str().to_owned(),
attributes: vec![
c.client_id.into(),
c.client_type.into(),
c.consensus_height.into(),
abci::EventAttribute::from(&c.client_id),
abci::EventAttribute::from(&c.client_type),
abci::EventAttribute::from(&c.consensus_height),
],
}
}
Expand Down Expand Up @@ -273,16 +273,16 @@ impl UpdateClient {
}
}

impl From<UpdateClient> for abci::Event {
fn from(u: UpdateClient) -> Self {
impl From<&UpdateClient> for abci::Event {
fn from(u: &UpdateClient) -> Self {
Self {
kind: IbcEventType::UpdateClient.as_str().to_owned(),
attributes: vec![
u.client_id.into(),
u.client_type.into(),
u.consensus_height.into(),
u.consensus_heights.into(),
u.header.into(),
abci::EventAttribute::from(&u.client_id),
abci::EventAttribute::from(&u.client_type),
abci::EventAttribute::from(&u.consensus_height),
abci::EventAttribute::from(&u.consensus_heights),
abci::EventAttribute::from(&u.header),
],
}
}
Expand Down Expand Up @@ -326,11 +326,14 @@ impl ClientMisbehaviour {
}
}

impl From<ClientMisbehaviour> for abci::Event {
fn from(c: ClientMisbehaviour) -> Self {
impl From<&ClientMisbehaviour> for abci::Event {
fn from(c: &ClientMisbehaviour) -> Self {
Self {
kind: IbcEventType::ClientMisbehaviour.as_str().to_owned(),
attributes: vec![c.client_id.into(), c.client_type.into()],
attributes: vec![
abci::EventAttribute::from(&c.client_id),
abci::EventAttribute::from(&c.client_type),
],
}
}
}
Expand Down Expand Up @@ -378,14 +381,14 @@ impl UpgradeClient {
}
}

impl From<UpgradeClient> for abci::Event {
fn from(u: UpgradeClient) -> Self {
impl From<&UpgradeClient> for abci::Event {
fn from(u: &UpgradeClient) -> Self {
Self {
kind: IbcEventType::UpgradeClient.as_str().to_owned(),
attributes: vec![
u.client_id.into(),
u.client_type.into(),
u.consensus_height.into(),
abci::EventAttribute::from(&u.client_id),
abci::EventAttribute::from(&u.client_type),
abci::EventAttribute::from(&u.consensus_height),
],
}
}
Expand Down Expand Up @@ -434,34 +437,39 @@ mod tests {
let tests: Vec<Test> = vec![
Test {
kind: IbcEventType::CreateClient,
event: CreateClient::new(client_id.clone(), client_type.clone(), consensus_height)
.into(),
event: AbciEvent::from(&CreateClient::new(
client_id.clone(),
client_type.clone(),
consensus_height,
)),
expected_keys: expected_keys[0..3].to_vec(),
expected_values: expected_values[0..3].to_vec(),
},
Test {
kind: IbcEventType::UpdateClient,
event: UpdateClient::new(
event: AbciEvent::from(&UpdateClient::new(
client_id.clone(),
client_type.clone(),
consensus_height,
consensus_heights,
header,
)
.into(),
)),
expected_keys: expected_keys.clone(),
expected_values: expected_values.clone(),
},
Test {
kind: IbcEventType::UpgradeClient,
event: UpgradeClient::new(client_id.clone(), client_type.clone(), consensus_height)
.into(),
event: AbciEvent::from(&UpgradeClient::new(
client_id.clone(),
client_type.clone(),
consensus_height,
)),
expected_keys: expected_keys[0..3].to_vec(),
expected_values: expected_values[0..3].to_vec(),
},
Test {
kind: IbcEventType::ClientMisbehaviour,
event: ClientMisbehaviour::new(client_id, client_type).into(),
event: AbciEvent::from(&ClientMisbehaviour::new(client_id, client_type)),
expected_keys: expected_keys[0..2].to_vec(),
expected_values: expected_values[0..2].to_vec(),
},
Expand Down
51 changes: 26 additions & 25 deletions crates/ibc/src/core/ics03_connection/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct Attributes {
}

/// Convert attributes to Tendermint ABCI tags
impl From<Attributes> for Vec<abci::EventAttribute> {
fn from(a: Attributes) -> Self {
impl From<&Attributes> for Vec<abci::EventAttribute> {
fn from(a: &Attributes) -> Self {
let conn_id = (CONN_ID_ATTRIBUTE_KEY, a.connection_id.as_str()).into();
let client_id = (CLIENT_ID_ATTRIBUTE_KEY, a.client_id.as_str()).into();

Expand Down Expand Up @@ -108,11 +108,11 @@ impl OpenInit {
}
}

impl From<OpenInit> for abci::Event {
fn from(v: OpenInit) -> Self {
impl From<&OpenInit> for abci::Event {
fn from(v: &OpenInit) -> Self {
abci::Event {
kind: IbcEventType::OpenInitConnection.as_str().to_owned(),
attributes: v.0.into(),
attributes: Vec::<abci::EventAttribute>::from(&v.0),
}
}
}
Expand Down Expand Up @@ -163,11 +163,11 @@ impl OpenTry {
}
}

impl From<OpenTry> for abci::Event {
fn from(v: OpenTry) -> Self {
impl From<&OpenTry> for abci::Event {
fn from(v: &OpenTry) -> Self {
abci::Event {
kind: IbcEventType::OpenTryConnection.as_str().to_owned(),
attributes: v.0.into(),
attributes: Vec::<abci::EventAttribute>::from(&v.0),
}
}
}
Expand Down Expand Up @@ -218,11 +218,11 @@ impl OpenAck {
}
}

impl From<OpenAck> for abci::Event {
fn from(v: OpenAck) -> Self {
impl From<&OpenAck> for abci::Event {
fn from(v: &OpenAck) -> Self {
abci::Event {
kind: IbcEventType::OpenAckConnection.as_str().to_owned(),
attributes: v.0.into(),
attributes: Vec::<abci::EventAttribute>::from(&v.0),
}
}
}
Expand Down Expand Up @@ -273,11 +273,11 @@ impl OpenConfirm {
}
}

impl From<OpenConfirm> for abci::Event {
fn from(v: OpenConfirm) -> Self {
impl From<&OpenConfirm> for abci::Event {
fn from(v: &OpenConfirm) -> Self {
abci::Event {
kind: IbcEventType::OpenConfirmConnection.as_str().to_owned(),
attributes: v.0.into(),
attributes: Vec::<abci::EventAttribute>::from(&v.0),
}
}
}
Expand Down Expand Up @@ -318,12 +318,11 @@ mod tests {
let tests: Vec<Test> = vec![
Test {
kind: IbcEventType::OpenInitConnection,
event: OpenInit::new(
event: AbciEvent::from(&OpenInit::new(
conn_id_on_a.clone(),
client_id_on_a.clone(),
client_id_on_b.clone(),
)
.into(),
)),
expected_keys: expected_keys.clone(),
expected_values: expected_values
.iter()
Expand All @@ -333,32 +332,34 @@ mod tests {
},
Test {
kind: IbcEventType::OpenTryConnection,
event: OpenTry::new(
event: AbciEvent::from(&OpenTry::new(
conn_id_on_b.clone(),
client_id_on_b.clone(),
conn_id_on_a.clone(),
client_id_on_a.clone(),
)
.into(),
)),
expected_keys: expected_keys.clone(),
expected_values: expected_values.iter().rev().cloned().collect(),
},
Test {
kind: IbcEventType::OpenAckConnection,
event: OpenAck::new(
event: AbciEvent::from(&OpenAck::new(
conn_id_on_a.clone(),
client_id_on_a.clone(),
conn_id_on_b.clone(),
client_id_on_b.clone(),
)
.into(),
)),
expected_keys: expected_keys.clone(),
expected_values: expected_values.clone(),
},
Test {
kind: IbcEventType::OpenConfirmConnection,
event: OpenConfirm::new(conn_id_on_b, client_id_on_b, conn_id_on_a, client_id_on_a)
.into(),
event: AbciEvent::from(&OpenConfirm::new(
conn_id_on_b,
client_id_on_b,
conn_id_on_a,
client_id_on_a,
)),
expected_keys: expected_keys.clone(),
expected_values: expected_values.iter().rev().cloned().collect(),
},
Expand Down
Loading