ibc_testkit/testapp/ibc/clients/mock/
misbehaviour.rs

1use ibc::core::host::types::error::DecodingError;
2use ibc::core::host::types::identifiers::ClientId;
3use ibc::core::primitives::prelude::*;
4use ibc::primitives::proto::{Any, Protobuf};
5
6use crate::testapp::ibc::clients::mock::header::MockHeader;
7use crate::testapp::ibc::clients::mock::proto::Misbehaviour as RawMisbehaviour;
8
9pub const MOCK_MISBEHAVIOUR_TYPE_URL: &str = "/ibc.mock.Misbehavior";
10
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct Misbehaviour {
14    pub client_id: ClientId,
15    pub header1: MockHeader,
16    pub header2: MockHeader,
17}
18
19impl Protobuf<RawMisbehaviour> for Misbehaviour {}
20
21impl TryFrom<RawMisbehaviour> for Misbehaviour {
22    type Error = DecodingError;
23
24    fn try_from(raw: RawMisbehaviour) -> Result<Self, Self::Error> {
25        Ok(Self {
26            client_id: ClientId::new("07-tendermint", 0).expect("no error"),
27            header1: raw
28                .header1
29                .ok_or(DecodingError::missing_raw_data("misbehaviour header1"))?
30                .try_into()?,
31            header2: raw
32                .header2
33                .ok_or(DecodingError::missing_raw_data("misbehaviour header2"))?
34                .try_into()?,
35        })
36    }
37}
38
39impl From<Misbehaviour> for RawMisbehaviour {
40    fn from(value: Misbehaviour) -> Self {
41        Self {
42            client_id: value.client_id.to_string(),
43            header1: Some(value.header1.into()),
44            header2: Some(value.header2.into()),
45        }
46    }
47}
48
49impl Protobuf<Any> for Misbehaviour {}
50
51impl TryFrom<Any> for Misbehaviour {
52    type Error = DecodingError;
53
54    fn try_from(raw: Any) -> Result<Self, Self::Error> {
55        if let MOCK_MISBEHAVIOUR_TYPE_URL = raw.type_url.as_str() {
56            Protobuf::<RawMisbehaviour>::decode(raw.value.as_ref()).map_err(Into::into)
57        } else {
58            Err(DecodingError::MismatchedResourceName {
59                expected: MOCK_MISBEHAVIOUR_TYPE_URL.to_string(),
60                actual: raw.type_url,
61            })
62        }
63    }
64}
65
66impl From<Misbehaviour> for Any {
67    fn from(misbehaviour: Misbehaviour) -> Self {
68        Self {
69            type_url: MOCK_MISBEHAVIOUR_TYPE_URL.to_string(),
70            value: Protobuf::<RawMisbehaviour>::encode_vec(misbehaviour),
71        }
72    }
73}