penumbra_ibc/
ibc_action.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use ibc_proto::ibc::core::{
    channel::v1::{
        MsgAcknowledgement as RawMsgAcknowledgement,
        MsgChannelCloseConfirm as RawMsgChannelCloseConfirm,
        MsgChannelCloseInit as RawMsgChannelCloseInit, MsgChannelOpenAck as RawMsgChannelOpenAck,
        MsgChannelOpenConfirm as RawMsgChannelOpenConfirm,
        MsgChannelOpenInit as RawMsgChannelOpenInit, MsgChannelOpenTry as RawMsgChannelOpenTry,
        MsgRecvPacket as RawMsgRecvPacket, MsgTimeout as RawMsgTimeout,
    },
    client::v1::{
        MsgCreateClient as RawMsgCreateClient, MsgSubmitMisbehaviour as RawMsgSubmitMisbehaviour,
        MsgUpdateClient as RawMsgUpdateClient, MsgUpgradeClient as RawMsgUpgradeClient,
    },
    connection::v1::{
        MsgConnectionOpenAck as RawMsgConnectionOpenAck,
        MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm,
        MsgConnectionOpenInit as RawMsgConnectionOpenInit,
        MsgConnectionOpenTry as RawMsgConnectionOpenTry,
    },
};
use ibc_types::core::{
    channel::msgs::{
        MsgAcknowledgement, MsgChannelCloseConfirm, MsgChannelCloseInit, MsgChannelOpenAck,
        MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, MsgRecvPacket, MsgTimeout,
    },
    client::msgs::{MsgCreateClient, MsgSubmitMisbehaviour, MsgUpdateClient, MsgUpgradeClient},
    connection::msgs::{
        MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, MsgConnectionOpenTry,
    },
};

use ibc_types::DomainType as IbcTypesDomainType;

use penumbra_proto::penumbra::core::component::ibc::v1::{self as pb};
use penumbra_proto::{DomainType, Name};
use penumbra_txhash::{EffectHash, EffectingData};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "pb::IbcRelay", into = "pb::IbcRelay")]
pub enum IbcRelay {
    CreateClient(MsgCreateClient),
    UpdateClient(MsgUpdateClient),
    UpgradeClient(MsgUpgradeClient),
    SubmitMisbehavior(MsgSubmitMisbehaviour),
    ConnectionOpenInit(MsgConnectionOpenInit),
    ConnectionOpenTry(MsgConnectionOpenTry),
    ConnectionOpenAck(MsgConnectionOpenAck),
    ConnectionOpenConfirm(MsgConnectionOpenConfirm),
    ChannelOpenInit(MsgChannelOpenInit),
    ChannelOpenTry(MsgChannelOpenTry),
    ChannelOpenAck(MsgChannelOpenAck),
    ChannelOpenConfirm(MsgChannelOpenConfirm),
    ChannelCloseInit(MsgChannelCloseInit),
    ChannelCloseConfirm(MsgChannelCloseConfirm),
    RecvPacket(MsgRecvPacket),
    Acknowledgement(MsgAcknowledgement),
    Timeout(MsgTimeout),
    Unknown(pbjson_types::Any),
}

impl IbcRelay {
    /// Create a tracing span to track execution related to this action.
    ///
    /// The parent span is provided explicitly, so that this span can be constructed
    /// nested under an `Action` span.
    pub fn create_span(&self, parent: &tracing::Span) -> tracing::Span {
        match self {
            IbcRelay::CreateClient(msg) => {
                // HACK: not a better way to get tm light client data
                match ibc_types::lightclients::tendermint::client_state::ClientState::try_from(
                    msg.client_state.clone(),
                ) {
                    Ok(tm_client) => {
                        tracing::info_span!(parent: parent, "CreateClient", chain_id = %tm_client.chain_id)
                    }
                    Err(_) => tracing::info_span!(parent: parent, "CreateClient"),
                }
            }
            IbcRelay::UpdateClient(msg) => {
                tracing::info_span!(parent: parent, "UpdateClient", client_id = %msg.client_id)
            }
            IbcRelay::UpgradeClient(msg) => {
                tracing::info_span!(parent: parent, "UpgradeClient", client_id = %msg.client_id)
            }
            IbcRelay::SubmitMisbehavior(msg) => {
                tracing::info_span!(parent: parent, "SubmitMisbehavior", client_id = %msg.client_id)
            }
            IbcRelay::ConnectionOpenInit(msg) => {
                tracing::info_span!(parent: parent, "ConnectionOpenInit", client_id = %msg.client_id_on_a)
            }
            IbcRelay::ConnectionOpenTry(msg) => {
                tracing::info_span!(parent: parent, "ConnectionOpenTry", client_id = %msg.client_id_on_b)
            }
            IbcRelay::ConnectionOpenAck(msg) => {
                tracing::info_span!(parent: parent, "ConnectionOpenAck", connection_id = %msg.conn_id_on_a)
            }
            IbcRelay::ConnectionOpenConfirm(msg) => {
                tracing::info_span!(parent: parent, "ConnectionOpenConfirm", connection_id = %msg.conn_id_on_b)
            }
            IbcRelay::ChannelOpenInit(msg) => {
                tracing::info_span!(parent: parent, "ChannelOpenInit", port_id = %msg.port_id_on_a)
            }
            IbcRelay::ChannelOpenTry(msg) => {
                tracing::info_span!(parent: parent, "ChannelOpenTry", port_id = %msg.port_id_on_b)
            }
            IbcRelay::ChannelOpenAck(msg) => {
                tracing::info_span!(parent: parent, "ChannelOpenAck", chan_id = %msg.chan_id_on_a)
            }
            IbcRelay::ChannelOpenConfirm(msg) => {
                tracing::info_span!(parent: parent, "ChannelOpenConfirm", chan_id = %msg.chan_id_on_b)
            }
            IbcRelay::ChannelCloseInit(msg) => {
                tracing::info_span!(parent: parent, "ChannelCloseInit", chan_id = %msg.chan_id_on_a)
            }
            IbcRelay::ChannelCloseConfirm(msg) => {
                tracing::info_span!(parent: parent, "ChannelCloseConfirm", chan_id = %msg.chan_id_on_b)
            }
            IbcRelay::RecvPacket(msg) => {
                tracing::info_span!(parent: parent, "RecvPacket", chan_id = %msg.packet.chan_on_b, seq = %msg.packet.sequence)
            }
            IbcRelay::Acknowledgement(msg) => {
                tracing::info_span!(parent: parent, "Acknowledgement", chan_id = %msg.packet.chan_on_a, seq = %msg.packet.sequence)
            }
            IbcRelay::Timeout(msg) => {
                tracing::info_span!(parent: parent, "Timeout", chan_id = %msg.packet.chan_on_a, seq = %msg.packet.sequence)
            }
            IbcRelay::Unknown(_) => {
                tracing::info_span!(parent: parent, "Unknown")
            }
        }
    }
}

impl EffectingData for IbcRelay {
    fn effect_hash(&self) -> EffectHash {
        EffectHash::from_proto_effecting_data(&self.to_proto())
    }
}

impl DomainType for IbcRelay {
    type Proto = pb::IbcRelay;
}

impl TryFrom<pb::IbcRelay> for IbcRelay {
    type Error = anyhow::Error;
    fn try_from(value: pb::IbcRelay) -> Result<Self, Self::Error> {
        let raw_action = value
            .raw_action
            .ok_or_else(|| anyhow::anyhow!("empty IBC transaction is not allowed"))?;

        let action_type = raw_action.type_url.as_str();
        let raw_action_bytes = raw_action.value.clone();

        // fn calls not allowed in match patterns, so we have a huge if else block
        let outer_msg = if action_type == RawMsgCreateClient::type_url() {
            let msg = MsgCreateClient::decode(raw_action_bytes)?;
            IbcRelay::CreateClient(msg)
        } else if action_type == RawMsgUpdateClient::type_url() {
            let msg = MsgUpdateClient::decode(raw_action_bytes)?;
            IbcRelay::UpdateClient(msg)
        } else if action_type == RawMsgUpgradeClient::type_url() {
            let msg = MsgUpgradeClient::decode(raw_action_bytes)?;
            IbcRelay::UpgradeClient(msg)
        } else if action_type == RawMsgSubmitMisbehaviour::type_url() {
            let msg = MsgSubmitMisbehaviour::decode(raw_action_bytes)?;
            IbcRelay::SubmitMisbehavior(msg)
        } else if action_type == RawMsgConnectionOpenInit::type_url() {
            let msg = MsgConnectionOpenInit::decode(raw_action_bytes)?;
            IbcRelay::ConnectionOpenInit(msg)
        } else if action_type == RawMsgConnectionOpenTry::type_url() {
            let msg = MsgConnectionOpenTry::decode(raw_action_bytes)?;
            IbcRelay::ConnectionOpenTry(msg)
        } else if action_type == RawMsgConnectionOpenAck::type_url() {
            let msg = MsgConnectionOpenAck::decode(raw_action_bytes)?;
            IbcRelay::ConnectionOpenAck(msg)
        } else if action_type == RawMsgConnectionOpenConfirm::type_url() {
            let msg = MsgConnectionOpenConfirm::decode(raw_action_bytes)?;
            IbcRelay::ConnectionOpenConfirm(msg)
        } else if action_type == RawMsgAcknowledgement::type_url() {
            let msg = MsgAcknowledgement::decode(raw_action_bytes)?;
            IbcRelay::Acknowledgement(msg)
        } else if action_type == RawMsgChannelOpenInit::type_url() {
            let msg = MsgChannelOpenInit::decode(raw_action_bytes)?;
            IbcRelay::ChannelOpenInit(msg)
        } else if action_type == RawMsgChannelOpenTry::type_url() {
            let msg = MsgChannelOpenTry::decode(raw_action_bytes)?;
            IbcRelay::ChannelOpenTry(msg)
        } else if action_type == RawMsgChannelOpenAck::type_url() {
            let msg = MsgChannelOpenAck::decode(raw_action_bytes)?;
            IbcRelay::ChannelOpenAck(msg)
        } else if action_type == RawMsgChannelOpenConfirm::type_url() {
            let msg = MsgChannelOpenConfirm::decode(raw_action_bytes)?;
            IbcRelay::ChannelOpenConfirm(msg)
        } else if action_type == RawMsgChannelCloseInit::type_url() {
            let msg = MsgChannelCloseInit::decode(raw_action_bytes)?;
            IbcRelay::ChannelCloseInit(msg)
        } else if action_type == RawMsgChannelCloseConfirm::type_url() {
            let msg = MsgChannelCloseConfirm::decode(raw_action_bytes)?;
            IbcRelay::ChannelCloseConfirm(msg)
        } else if action_type == RawMsgRecvPacket::type_url() {
            let msg = MsgRecvPacket::decode(raw_action_bytes)?;
            IbcRelay::RecvPacket(msg)
        } else if action_type == RawMsgTimeout::type_url() {
            let msg = MsgTimeout::decode(raw_action_bytes)?;
            IbcRelay::Timeout(msg)
        } else {
            IbcRelay::Unknown(raw_action)
        };

        Ok(outer_msg)
    }
}

impl From<IbcRelay> for pb::IbcRelay {
    fn from(value: IbcRelay) -> Self {
        let raw_action = match value {
            IbcRelay::CreateClient(msg) => pbjson_types::Any {
                type_url: RawMsgCreateClient::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::UpdateClient(msg) => pbjson_types::Any {
                type_url: RawMsgUpdateClient::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::UpgradeClient(msg) => pbjson_types::Any {
                type_url: RawMsgUpgradeClient::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::SubmitMisbehavior(msg) => pbjson_types::Any {
                type_url: RawMsgSubmitMisbehaviour::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ConnectionOpenInit(msg) => pbjson_types::Any {
                type_url: RawMsgConnectionOpenInit::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ConnectionOpenTry(msg) => pbjson_types::Any {
                type_url: RawMsgConnectionOpenTry::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ConnectionOpenAck(msg) => pbjson_types::Any {
                type_url: RawMsgConnectionOpenAck::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ConnectionOpenConfirm(msg) => pbjson_types::Any {
                type_url: RawMsgConnectionOpenConfirm::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::Acknowledgement(msg) => pbjson_types::Any {
                type_url: RawMsgAcknowledgement::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ChannelOpenInit(msg) => pbjson_types::Any {
                type_url: RawMsgChannelOpenInit::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ChannelOpenTry(msg) => pbjson_types::Any {
                type_url: RawMsgChannelOpenTry::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ChannelOpenAck(msg) => pbjson_types::Any {
                type_url: RawMsgChannelOpenAck::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ChannelOpenConfirm(msg) => pbjson_types::Any {
                type_url: RawMsgChannelOpenConfirm::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ChannelCloseInit(msg) => pbjson_types::Any {
                type_url: RawMsgChannelCloseInit::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::ChannelCloseConfirm(msg) => pbjson_types::Any {
                type_url: RawMsgChannelCloseConfirm::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::RecvPacket(msg) => pbjson_types::Any {
                type_url: RawMsgRecvPacket::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::Timeout(msg) => pbjson_types::Any {
                type_url: RawMsgTimeout::type_url(),
                value: msg.encode_to_vec().into(),
            },
            IbcRelay::Unknown(raw_action) => raw_action,
        };
        pb::IbcRelay {
            raw_action: Some(raw_action),
        }
    }
}