evm_note/
ibc.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
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
    DepsMut, Env, HexBinary, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
    IbcChannelOpenMsg, IbcChannelOpenResponse, IbcPacketAckMsg, IbcPacketReceiveMsg,
    IbcPacketTimeoutMsg, IbcReceiveResponse, Never, Reply, Response, SubMsg,
};
use polytone::{accounts, ack::Ack, callbacks, handshake::note, ibc::Packet};

use crate::{
    error::ContractError,
    state::{BLOCK_MAX_GAS, CHANNEL, CONNECTION_REMOTE_PORT},
};

// TODO: Remove before production
pub(crate) mod temp_test {
    use cosmwasm_schema::cw_serde;
    use cosmwasm_std::HexBinary;
    use cw_storage_plus::Map;
    use polytone::ibc::ExecuteResponsePacket;

    use super::*;

    #[cw_serde]
    pub enum SentPacketData {
        Binary(HexBinary),
        Request(Packet),
    }

    #[cw_serde]
    pub enum ReceivedPacketData {
        Binary(HexBinary),
        Result(ExecuteResponsePacket),
    }

    #[cw_serde]
    pub struct AckInfo {
        pub height: u64,
        pub index: Option<u32>,
        pub sent: SentPacketData,
        pub result: Ack,
    }

    pub const ACK_DATAS: Map<u64, AckInfo> = Map::new("ack_datas");
}

/// The amount of gas that needs to be reserved for handling a
/// callback error in the reply method. See `TestNoteOutOfGas` in the
/// simulation tests for a test that can be used to tune thise.
pub(crate) const ERR_GAS_NEEDED: u64 = 101_000;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_open(
    deps: DepsMut,
    _env: Env,
    msg: IbcChannelOpenMsg,
) -> Result<IbcChannelOpenResponse, ContractError> {
    let response = note::open(&msg, &["EVM"])?;
    match CONNECTION_REMOTE_PORT.may_load(deps.storage)? {
        Some((conn, port)) => {
            if msg.channel().counterparty_endpoint.port_id != port
                || msg.channel().connection_id != conn
            {
                Err(ContractError::AlreadyPaired {
                    suggested_connection: msg.channel().connection_id.clone(),
                    suggested_port: msg.channel().counterparty_endpoint.port_id.clone(),
                    pair_connection: conn,
                    pair_port: port,
                })
            } else {
                Ok(response)
            }
        }
        None => Ok(response),
    }
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_connect(
    deps: DepsMut,
    _env: Env,
    msg: IbcChannelConnectMsg,
) -> Result<IbcBasicResponse, ContractError> {
    note::connect(&msg, &["EvmMsg"])?;
    CONNECTION_REMOTE_PORT.save(
        deps.storage,
        &(
            msg.channel().connection_id.clone(),
            msg.channel().counterparty_endpoint.port_id.clone(),
        ),
    )?;
    CHANNEL.save(deps.storage, &msg.channel().endpoint.channel_id)?;
    Ok(IbcBasicResponse::new()
        .add_attribute("method", "ibc_channel_connect")
        .add_attribute("channel_id", &msg.channel().endpoint.channel_id))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_close(
    deps: DepsMut,
    _env: Env,
    msg: IbcChannelCloseMsg,
) -> Result<IbcBasicResponse, ContractError> {
    CHANNEL.remove(deps.storage);
    Ok(IbcBasicResponse::default()
        .add_attribute("method", "ibc_channel_close")
        .add_attribute("connection_id", msg.channel().connection_id.clone())
        .add_attribute(
            "counterparty_port_id",
            msg.channel().counterparty_endpoint.port_id.clone(),
        ))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_receive(
    _deps: DepsMut,
    _env: Env,
    _msg: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse, Never> {
    unreachable!("voice should never send a packet")
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_ack(
    deps: DepsMut,
    _env: Env,
    ack: IbcPacketAckMsg,
) -> Result<IbcBasicResponse, ContractError> {
    let maybe_packet = Packet::decode(&ack.original_packet.data);

    // TODO: Remove error catching for production
    let packet = match maybe_packet {
        Ok(decoded) => temp_test::SentPacketData::Request(decoded),
        Err(err) => {
            deps.api.debug(&format!("Error decoding packet: {:?}", err));
            temp_test::SentPacketData::Binary(HexBinary::from(ack.original_packet.data.clone()))
        }
    };

    let (callback, executed_by, result) = callbacks::on_ack(deps.storage, &ack);

    let callback_msg = callback.map(|callback| {
        SubMsg::reply_on_error(callback, ack.original_packet.sequence).with_gas_limit(
            BLOCK_MAX_GAS
                .load(deps.storage)
                .expect("set during instantiation")
                - ERR_GAS_NEEDED,
        )
    });

    accounts::on_ack(
        deps.storage,
        ack.original_packet.src.channel_id.clone(),
        ack.original_packet.sequence,
        executed_by,
    );

    let ack_info = temp_test::AckInfo {
        height: _env.block.height,
        index: _env.transaction.map(|tx| tx.index),
        sent: packet,
        result,
    };

    temp_test::ACK_DATAS
        .save(deps.storage, ack.original_packet.sequence, &ack_info)
        .ok();

    Ok(IbcBasicResponse::default()
        .add_attribute("method", "ibc_packet_ack")
        .add_attribute("sequence_number", ack.original_packet.sequence.to_string())
        .add_submessages(callback_msg))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_timeout(
    deps: DepsMut,
    _env: Env,
    msg: IbcPacketTimeoutMsg,
) -> Result<IbcBasicResponse, ContractError> {
    let callback = callbacks::on_timeout(deps.storage, &msg).map(|cosmos_msg| {
        SubMsg::reply_on_error(cosmos_msg, msg.packet.sequence).with_gas_limit(
            BLOCK_MAX_GAS
                .load(deps.storage)
                .expect("set during instantiation")
                - ERR_GAS_NEEDED,
        )
    });

    accounts::on_timeout(deps.storage, msg.packet.src.channel_id, msg.packet.sequence);

    Ok(IbcBasicResponse::default()
        .add_attribute("method", "ibc_packet_timeout")
        .add_attribute("sequence_number", msg.packet.sequence.to_string())
        .add_submessages(callback))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
    let sequence = msg.id;
    Ok(Response::default()
        .add_attribute("method", "reply_callback_error")
        .add_attribute("packet_sequence", sequence.to_string())
        .add_attribute("callback_error", msg.result.unwrap_err()))
}